Skip to content

Commit

Permalink
Align Java and Node wallet API naming (#65)
Browse files Browse the repository at this point in the history
Signed-off-by: Mark S. Lewis <mark_lewis@uk.ibm.com>
  • Loading branch information
bestbeforetoday authored and andrew-coleman committed Dec 11, 2019
1 parent 11b5a55 commit 7b9f252
Show file tree
Hide file tree
Showing 9 changed files with 85 additions and 87 deletions.
6 changes: 3 additions & 3 deletions fabric-network/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -110,13 +110,13 @@
*/

/**
* Interface for store implementation that provide backing storage for identities in a [Wallet]{@link module:fabric-network.Wallet}.
* Interface for store implementations that provide backing storage for identities in a [Wallet]{@link module:fabric-network.Wallet}.
* @interface WalletStore
* @memberof module:fabric-network
*/
/**
* Delete data associated with a given label.
* @function module:fabric-network.WalletStore#delete
* Remove data associated with a given label.
* @function module:fabric-network.WalletStore#remove
* @async
* @param {string} label A label identifying stored data.
* @returns {Promise<void>}
Expand Down
18 changes: 9 additions & 9 deletions fabric-network/src/impl/wallet/couchdbwalletstore.ts
Original file line number Diff line number Diff line change
Expand Up @@ -8,21 +8,21 @@ import nano = require('nano');

import { WalletStore } from './walletstore';

const encoding: 'utf8' = 'utf8';
const encoding = 'utf8';

interface WalletDocument extends nano.MaybeDocument {
data: string;
}

export class CouchDBWalletStore implements WalletStore {
public static async newInstance(config: string | nano.Configuration, dbName: string): Promise<CouchDBWalletStore> {
const client: nano.ServerScope = nano(config);
const client = nano(config);
try {
await client.db.get(dbName); // Throws if database does not exist
} catch (error) {
await client.db.create(dbName);
}
const db: nano.DocumentScope<WalletDocument> = await client.use(dbName) as nano.DocumentScope<WalletDocument>;
const db = await client.use<WalletDocument>(dbName);
return new CouchDBWalletStore(db);
}

Expand All @@ -32,21 +32,21 @@ export class CouchDBWalletStore implements WalletStore {
this.db = db;
}

public async delete(label: string): Promise<void> {
const document: (nano.DocumentGetResponse & WalletDocument) | undefined = await this.getDocument(label);
public async remove(label: string): Promise<void> {
const document = await this.getDocument(label);
if (document) {
await this.db.destroy(document._id, document._rev);
}
}

public async get(label: string): Promise<Buffer|undefined> {
const document: (nano.DocumentGetResponse & WalletDocument) | undefined = await this.getDocument(label);
const document = await this.getDocument(label);
return document ? Buffer.from(document.data, encoding) : undefined;
}

public async list(): Promise<string[]> {
const response: nano.DocumentListResponse<WalletDocument> = await this.db.list();
return response.rows.map((row: nano.DocumentResponseRow<WalletDocument>) => row.id);
const response = await this.db.list();
return response.rows.map((row) => row.id);
}

public async put(label: string, data: Buffer): Promise<void> {
Expand All @@ -56,7 +56,7 @@ export class CouchDBWalletStore implements WalletStore {
};

// Overwrite any existing document revision instead of creating a new revision
const existingDocument: (nano.DocumentGetResponse & WalletDocument) | undefined = await this.getDocument(label);
const existingDocument = await this.getDocument(label);
if (existingDocument) {
newDocument._rev = existingDocument._rev;
}
Expand Down
12 changes: 6 additions & 6 deletions fabric-network/src/impl/wallet/filesystemwalletstore.ts
Original file line number Diff line number Diff line change
Expand Up @@ -9,14 +9,14 @@ import path = require('path');

import { WalletStore } from './walletstore';

const suffix: string = '.id';
const suffix = '.id';

function isIdentityFile(file: string): boolean {
return file.endsWith(suffix);
}

function toLabel(file: string): string {
const endIndex: number = file.length - suffix.length;
const endIndex = file.length - suffix.length;
return file.substring(0, endIndex);
}

Expand All @@ -32,13 +32,13 @@ export class FileSystemWalletStore implements WalletStore {
this.storePath = directory;
}

public async delete(label: string): Promise<void> {
const file: string = this.toPath(label);
public async remove(label: string): Promise<void> {
const file = this.toPath(label);
await fs.unlink(file);
}

public async get(label: string): Promise<Buffer|undefined> {
const file: string = this.toPath(label);
const file = this.toPath(label);
try {
return await fs.readFile(file);
} catch (error) {
Expand All @@ -53,7 +53,7 @@ export class FileSystemWalletStore implements WalletStore {
}

public async put(label: string, data: Buffer): Promise<void> {
const file: string = this.toPath(label);
const file = this.toPath(label);
await fs.writeFile(file, data);
}

Expand Down
2 changes: 1 addition & 1 deletion fabric-network/src/impl/wallet/inmemorywalletstore.ts
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@ import { WalletStore } from './walletstore';
export class InMemoryWalletStore implements WalletStore {
private readonly map: Map<string, Buffer> = new Map();

public async delete(label: string): Promise<void> {
public async remove(label: string): Promise<void> {
this.map.delete(label);
}

Expand Down
22 changes: 11 additions & 11 deletions fabric-network/src/impl/wallet/wallet.ts
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@ import { IdentityData } from './identitydata';
import { IdentityProviderRegistry } from './identityproviderregistry';
import { WalletStore } from './walletstore';

const encoding: 'utf8' = 'utf8';
const encoding = 'utf8';

/**
* Stores identity information for use when connecting a Gateway. The wallet is backed by a store that handles
Expand All @@ -18,7 +18,7 @@ const encoding: 'utf8' = 'utf8';
* @memberof module:fabric-network
*/
export class Wallet {
private readonly providerRegistry: IdentityProviderRegistry = new IdentityProviderRegistry();
private readonly providerRegistry = new IdentityProviderRegistry();
private readonly store: WalletStore;

/**
Expand All @@ -37,9 +37,9 @@ export class Wallet {
* @returns {Promise<void>}
*/
public async put(label: string, identity: Identity): Promise<void> {
const json: IdentityData = this.providerRegistry.getProvider(identity.type).toJson(identity);
const jsonString: string = JSON.stringify(json);
const buffer: Buffer = Buffer.from(jsonString, encoding) as Buffer;
const json = this.providerRegistry.getProvider(identity.type).toJson(identity);
const jsonString = JSON.stringify(json);
const buffer = Buffer.from(jsonString, encoding) as Buffer;
await this.store.put(label, buffer);
}

Expand All @@ -49,13 +49,13 @@ export class Wallet {
* @returns {Promise<module:fabric-network.Identity|undefined>} An identity if it exists; otherwise undefined.
*/
public async get(label: string): Promise<Identity|undefined> {
const buffer: Buffer | undefined = await this.store.get(label);
const buffer = await this.store.get(label);
if (!buffer) {
return undefined;
}

const jsonString: string = buffer.toString(encoding);
const json: IdentityData = JSON.parse(jsonString);
const jsonString = buffer.toString(encoding);
const json = JSON.parse(jsonString);
return this.providerRegistry.getProvider(json.type).fromJson(json);
}

Expand All @@ -68,12 +68,12 @@ export class Wallet {
}

/**
* Delete an identity from the wallet.
* Remove an identity from the wallet.
* @param label Label used to identify the identity within the wallet.
* @returns {Promise<void>}
*/
public async delete(label: string): Promise<void> {
await this.store.delete(label);
public async remove(label: string): Promise<void> {
await this.store.remove(label);
}

/**
Expand Down
6 changes: 3 additions & 3 deletions fabric-network/src/impl/wallet/wallets.ts
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,7 @@ export class Wallets {
* @returns {Promise<module:fabric-network.Wallet>} A wallet.
*/
public static async newInMemoryWallet(): Promise<Wallet> {
const store: InMemoryWalletStore = new InMemoryWalletStore();
const store = new InMemoryWalletStore();
return new Wallet(store);
}

Expand All @@ -32,7 +32,7 @@ export class Wallets {
* @returns {Promise<module:fabric-network.Wallet>} A wallet.
*/
public static async newFileSystemWallet(directory: string): Promise<Wallet> {
const store: FileSystemWalletStore = await FileSystemWalletStore.newInstance(directory);
const store = await FileSystemWalletStore.newInstance(directory);
return new Wallet(store);
}

Expand All @@ -43,7 +43,7 @@ export class Wallets {
* @returns {Promise<module:fabric-network.Wallet>} A wallet.
*/
public static async newCouchDBWallet(config: string | nano.Configuration, dbName: string = 'wallet'): Promise<Wallet> {
const store: CouchDBWalletStore = await CouchDBWalletStore.newInstance(config, dbName);
const store = await CouchDBWalletStore.newInstance(config, dbName);
return new Wallet(store);
}
}
2 changes: 1 addition & 1 deletion fabric-network/src/impl/wallet/walletstore.ts
Original file line number Diff line number Diff line change
Expand Up @@ -5,8 +5,8 @@
*/

export interface WalletStore {
delete(label: string): Promise<void>;
get(label: string): Promise<Buffer | undefined>;
list(): Promise<string[]>;
put(label: string, data: Buffer): Promise<void>;
remove(label: string): Promise<void>;
}
49 changes: 24 additions & 25 deletions fabric-network/test/impl/wallet/wallet.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -11,9 +11,8 @@ import { X509Identity } from '../../../src/impl/wallet/x509identity';

import chai = require('chai');
import chaiAsPromised = require('chai-as-promised');
import { InMemoryWalletStore } from '../../../src/impl/wallet/inmemorywalletstore';
chai.use(chaiAsPromised);
const expect: Chai.ExpectStatic = chai.expect;
const expect = chai.expect;

// tslint:disable: no-unused-expression

Expand All @@ -27,60 +26,60 @@ describe('Wallet', () => {
type: 'X.509',
};

const newWallet: any = async (): Promise<Wallet> => {
const newWallet = async (): Promise<Wallet> => {
return await Wallets.newInMemoryWallet();
};

it('List returns empty array for an empty wallet', async () => {
const wallet: Wallet = await newWallet();
const result: string[] = await wallet.list();
const wallet = await newWallet();
const result = await wallet.list();
expect(result).to.be.empty;
});

it('List returns added identity', async () => {
const wallet: Wallet = await newWallet();
const label: string = 'myId';
const wallet = await newWallet();
const label = 'myId';

await wallet.put(label, identity);
const result: string[] = await wallet.list();
const result = await wallet.list();

expect(result).to.have.members([label]);
});

it('List does not return deleted identities', async () => {
const wallet: Wallet = await newWallet();
const label: string = 'myId';
it('List does not return removed identities', async () => {
const wallet = await newWallet();
const label = 'myId';

await wallet.put(label, identity);
await wallet.delete(label);
const result: string[] = await wallet.list();
await wallet.remove(label);
const result = await wallet.list();

expect(result).to.be.empty;
});

it('Returns undefined when getting an identity that does not exist', async () => {
const wallet: Wallet = await newWallet();
const result: Identity | undefined = await wallet.get('NO');
const wallet = await newWallet();
const result = await wallet.get('NO');
expect(result).to.be.undefined;
});

it('Returns undefined when getting a deleted identity', async () => {
const wallet: Wallet = await newWallet();
const label: string = 'myId';
it('Returns undefined when getting a removed identity', async () => {
const wallet = await newWallet();
const label = 'myId';

await wallet.put(label, identity);
await wallet.delete(label);
const result: Identity | undefined = await wallet.get(label);
await wallet.remove(label);
const result = await wallet.get(label);

expect(result).to.be.undefined;
});

it('Get previously added identity', async () => {
const wallet: Wallet = await newWallet();
const label: string = 'myId';
const wallet = await newWallet();
const label = 'myId';

await wallet.put(label, identity);
const result: Identity | undefined = await wallet.get(label);
const result = await wallet.get(label);

expect(result).to.deep.equal(identity);
});
Expand All @@ -91,8 +90,8 @@ describe('Wallet', () => {
type: 'BAD_TYPE',
};

const wallet: Wallet = await newWallet();
const promise: Promise<void> = wallet.put('label', badIdentity);
const wallet = await newWallet();
const promise = wallet.put('label', badIdentity);

expect(promise).to.be.rejectedWith(badIdentity.type);
});
Expand Down
Loading

0 comments on commit 7b9f252

Please sign in to comment.