Skip to content

Commit

Permalink
FABN-1387: Fix fabric-network TypeScript definitions
Browse files Browse the repository at this point in the history
index.d.ts referred to TypeScript source files not included in the
published npm module.

In the build, run a TypeScript compilation pass to generate definition
files in the output directory that gets published to npm. Ensure the
index.d.ts only references generated TypeScript definitions in the
output directory and not source files directly.

Change-Id: I11ab8154182a026d3c7a459108f4277c54dc4f15
Signed-off-by: Mark S. Lewis <[email protected]>
  • Loading branch information
bestbeforetoday committed Sep 27, 2019
1 parent 70a9234 commit e9e8c59
Show file tree
Hide file tree
Showing 21 changed files with 112 additions and 84 deletions.
1 change: 1 addition & 0 deletions build/tasks/tslint.js
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@ gulp.task('tslint', () => {
'fabric-network/**/*.ts',
'test/**/*.ts',
'!fabric-network/coverage/**',
'!fabric-network/lib/**',
'!fabric-network/node_modules/**',
'!fabric-client/coverage/**',
'!fabric-client/node_modules/**',
Expand Down
20 changes: 6 additions & 14 deletions docs/tutorials/wallet.md
Original file line number Diff line number Diff line change
Expand Up @@ -11,22 +11,19 @@ Fabric network.

A wallet is backed by a wallet store, which is responsible only for storing and retrieving data. Several different
store implementations are provided for convenience:

* **In-memory**: Non-persistent store. Useful for testing.
* **File system**: Stores identity information in a directory on the local file system.
* **CouchDB**: Stores identity information in a CouchDB database.

Wallets using default store implementations are created using static factory functions on the `Wallets` class, for
example:

```typescript
```javascript
const wallet = await Wallets.newFileSystemWallet('/path/to/wallet/directory');
```

You can write your own custom wallet store to suit your deployment environment by implementing the `WalletStore`
interface. A wallet backed by a custom wallet store implementation is created as follows:

```typescript
```javascript
const walletStore = new MyCustomWalletStore();
const wallet = new Wallet(walletStore);
```
Expand All @@ -37,15 +34,13 @@ An identity is a set of information and credentials required to connect to a Hyp
information is described as a simple JavaScript object using a well-defined format, including the Member Services
Provider associated with the user and a type identifier that indicates the type of credentials contained in the
identity. Two identity types are supported by default:

* **X.509**: X.509 certificate and private key in PEM format.
* **HSM-X.509**: X.509 certificate in PEM format, with the private key stored in a Hardware Security Module.

Once an identity object has been created from credentials supplied to you by your administrator or certificate
authority, it can be stored and retreived from a wallet using an arbitrary label to locate the identity within the
wallet, for example:

```typescript
```javascript
const identity: X509Identity = {
credentials: {
certificate: 'PEM format certificate string',
Expand All @@ -60,8 +55,7 @@ await wallet.put('alice', identity);
Note that a wallet may contain identities of varying types so, in TypeScript, indentity information retrieved from the
wallet is typed as `Identity` (or `undefined` if the identity does not exist in the wallet) and will need to be cast to
its specific subtype to access type-specific information, for example:

```typescript
```javascript
const identity = await wallet.get('alice');
if (identity && identity.type === 'X.509') {
const privateKey = (identity as X509Identity).credentials.privateKey;
Expand All @@ -75,8 +69,7 @@ The SDK uses the [PKCS #11](https://en.wikipedia.org/wiki/PKCS_11) interface to
with the private key omitted. However, in order to use HSM-managed identities the containing wallet must be configured
with details of the HSM that holds the private key. This is achieved by registering an `IdentityProvider` with the
wallet, for example:

```typescript
```javascript
const hsmProvider = new HsmX509Provider({
lib: '/path/to/hsm-specific/pkcs11/library',
pin: '1234567890',
Expand All @@ -87,8 +80,7 @@ wallet.getProviderRegistry().addProvider(hsmProvider);

Once the wallet has been configured with details of the HSM, HSM-managed identities can be stored and retreived from
the wallet as normal, for example:

```typescript
```javascript
const identity: HsmX509Identity = {
credentials: {
certificate: 'PEM format certificate string',
Expand Down
2 changes: 1 addition & 1 deletion fabric-network/src/impl/wallet/couchdbwalletstore.ts
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@

import nano = require('nano');

import { WalletStore } from 'fabric-network';
import { WalletStore } from './walletstore';

const encoding = 'utf8';

Expand Down
2 changes: 1 addition & 1 deletion fabric-network/src/impl/wallet/filesystemwalletstore.ts
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@
import fs = require('fs-extra');
import path = require('path');

import { WalletStore } from 'fabric-network';
import { WalletStore } from './walletstore';

const suffix = '.id';

Expand Down
8 changes: 3 additions & 5 deletions fabric-network/src/impl/wallet/hsmx509identity.ts
Original file line number Diff line number Diff line change
Expand Up @@ -6,11 +6,9 @@

import * as Client from 'fabric-client';

import {
Identity,
IdentityData,
IdentityProvider,
} from 'fabric-network';
import { Identity } from './identity';
import { IdentityData } from './identitydata';
import { IdentityProvider } from './identityprovider';

export interface HsmX509Identity extends Identity {
type: 'HSM-X.509';
Expand Down
10 changes: 10 additions & 0 deletions fabric-network/src/impl/wallet/identity.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
/**
* Copyright 2019 IBM All Rights Reserved.
*
* SPDX-License-Identifier: Apache-2.0
*/

export interface Identity {
type: string;
mspId: string;
}
10 changes: 10 additions & 0 deletions fabric-network/src/impl/wallet/identitydata.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
/**
* Copyright 2019 IBM All Rights Reserved.
*
* SPDX-License-Identifier: Apache-2.0
*/

export interface IdentityData {
readonly type: string;
readonly version: number;
}
17 changes: 17 additions & 0 deletions fabric-network/src/impl/wallet/identityprovider.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
/**
* Copyright 2019 IBM All Rights Reserved.
*
* SPDX-License-Identifier: Apache-2.0
*/

import * as Client from 'fabric-client';

import { Identity } from './identity';
import { IdentityData } from './identitydata';

export interface IdentityProvider {
readonly type: string;
fromJson(data: IdentityData): Identity;
toJson(identity: Identity): IdentityData;
setUserContext(client: Client, identity: Identity, name: string): Promise<void>;
}
2 changes: 1 addition & 1 deletion fabric-network/src/impl/wallet/identityproviderregistry.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@
* SPDX-License-Identifier: Apache-2.0
*/

import { IdentityProvider } from 'fabric-network';
import { IdentityProvider } from './identityprovider';
import { X509Provider } from './x509identity';

const defaultProviders: IdentityProvider[] = [
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 @@ -4,7 +4,7 @@
* SPDX-License-Identifier: Apache-2.0
*/

import { WalletStore } from 'fabric-network';
import { WalletStore } from './walletstore';

export class InMemoryWalletStore implements WalletStore {
private readonly map: Map<string, Buffer> = new Map();
Expand Down
9 changes: 3 additions & 6 deletions fabric-network/src/impl/wallet/wallet.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,13 +4,10 @@
* SPDX-License-Identifier: Apache-2.0
*/

import {
Identity,
IdentityData,
WalletStore,
} from 'fabric-network';

import { Identity } from './identity';
import { IdentityData } from './identitydata';
import { IdentityProviderRegistry } from './identityproviderregistry';
import { WalletStore } from './walletstore';

const encoding = 'utf8';

Expand Down
12 changes: 12 additions & 0 deletions fabric-network/src/impl/wallet/walletstore.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
/**
* Copyright 2019 IBM All Rights Reserved.
*
* SPDX-License-Identifier: Apache-2.0
*/

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

import * as Client from 'fabric-client';

import {
Identity,
IdentityData,
IdentityProvider,
} from 'fabric-network';
import { Identity } from './identity';
import { IdentityData } from './identitydata';
import { IdentityProvider } from './identityprovider';

export interface X509Identity extends Identity {
type: 'X.509';
Expand Down
14 changes: 6 additions & 8 deletions fabric-network/test/impl/wallet/identityprovider.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,22 +4,20 @@
* SPDX-License-Identifier: Apache-2.0
*/

import chai = require('chai');
const expect = chai.expect;

import {
HsmX509Identity,
HsmX509Provider,
} from '../../../src/impl/wallet/hsmx509identity';
import { Identity } from '../../../src/impl/wallet/identity';
import { IdentityData } from '../../../src/impl/wallet/identitydata';
import { IdentityProvider } from '../../../src/impl/wallet/identityprovider';
import {
X509Identity,
X509Provider,
} from '../../../src/impl/wallet/x509identity';
import {
Identity,
IdentityData,
IdentityProvider,
} from '../../../types';

import chai = require('chai');
const expect = chai.expect;

interface ProviderData {
dataVersions: { [version: string]: IdentityData };
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -4,13 +4,12 @@
* SPDX-License-Identifier: Apache-2.0
*/

import { IdentityProvider } from '../../../src/impl/wallet/identityprovider';
import { IdentityProviderRegistry } from '../../../src/impl/wallet/identityproviderregistry';

import chai = require('chai');
const expect = chai.expect;

import { IdentityProvider } from 'fabric-network';

describe('IdentityProviderRegistry', () => {
const fakeProvider: IdentityProvider = {
type: 'FAKE_PROVIDER',
Expand Down
2 changes: 1 addition & 1 deletion fabric-network/test/impl/wallet/wallet.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,10 +4,10 @@
* SPDX-License-Identifier: Apache-2.0
*/

import { Identity } from '../../../src/impl/wallet/identity';
import { Wallet } from '../../../src/impl/wallet/wallet';
import { Wallets } from '../../../src/impl/wallet/wallets';
import { X509Identity } from '../../../src/impl/wallet/x509identity';
import { Identity } from '../../../types';

import chai = require('chai');
import chaiAsPromised = require('chai-as-promised');
Expand Down
8 changes: 4 additions & 4 deletions fabric-network/test/impl/wallet/walletstore.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,9 +4,6 @@
* SPDX-License-Identifier: Apache-2.0
*/

import chai = require('chai');
const expect = chai.expect;

import fs = require('fs-extra');
import os = require('os');
import path = require('path');
Expand All @@ -17,7 +14,10 @@ const rimraf = util.promisify(rawRimraf);

import { FileSystemWalletStore } from '../../../src/impl/wallet/filesystemwalletstore';
import { InMemoryWalletStore } from '../../../src/impl/wallet/inmemorywalletstore';
import { WalletStore } from '../../../types';
import { WalletStore } from '../../../src/impl/wallet/walletstore';

import chai = require('chai');
const expect = chai.expect;

async function createTempDir(): Promise<string> {
const prefix = path.join(os.tmpdir(), 'fabric-network-test-');
Expand Down
17 changes: 17 additions & 0 deletions fabric-network/tsconfig-declaration.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
{
"compilerOptions": {
"alwaysStrict": true,
"declaration": true,
"module": "commonjs",
"outDir": "lib",
"rootDir": "src",
"strict": true,
"target": "es2017"
},
"include": [
"src/**/*"
],
"exclude": [
"node_modules/**/*"
]
}
1 change: 1 addition & 0 deletions fabric-network/tsconfig.json
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@
"alwaysStrict": true,
"module": "commonjs",
"outDir": "lib",
"rootDir": "src",
"sourceMap": true,
"strict": true,
"target": "es2017"
Expand Down
45 changes: 11 additions & 34 deletions fabric-network/types/index.d.ts
Original file line number Diff line number Diff line change
Expand Up @@ -8,16 +8,17 @@

import * as Client from 'fabric-client';

import { Wallet } from '../src/impl/wallet/wallet';

export {
Wallet,
};

export { Wallets } from '../src/impl/wallet/wallets';
export { HsmOptions, HsmX509Provider, HsmX509Identity } from '../src/impl/wallet/hsmx509identity';
export { X509Identity } from '../src/impl/wallet/x509identity';
export { IdentityProviderRegistry } from '../src/impl/wallet/identityproviderregistry';
import { Wallet } from '../lib/impl/wallet/wallet';

export { Wallet };
export { Wallets } from '../lib/impl/wallet/wallets';
export { WalletStore } from '../lib/impl/wallet/walletstore';
export { Identity } from '../lib/impl/wallet/identity';
export { IdentityData } from '../lib/impl/wallet/identitydata';
export { IdentityProvider } from '../lib/impl/wallet/identityprovider';
export { IdentityProviderRegistry } from '../lib/impl/wallet/identityproviderregistry';
export { HsmOptions, HsmX509Provider, HsmX509Identity } from '../lib/impl/wallet/hsmx509identity';
export { X509Identity } from '../lib/impl/wallet/x509identity';

// Main fabric network classes
//-------------------------------------------
Expand Down Expand Up @@ -198,27 +199,3 @@ export interface AbstractEventHubSelectionStrategy {
export class DefaultEventHubSelectionStrategies {
public static MSPID_SCOPE_ROUND_ROBIN: AbstractEventHubSelectionStrategy;
}

export interface WalletStore {
delete(label: string): Promise<void>;
get(label: string): Promise<Buffer | undefined>;
list(): Promise<string[]>;
put(label: string, data: Buffer): Promise<void>;
}

export interface Identity {
type: string;
mspId: string;
}

export interface IdentityData {
readonly type: string;
readonly version: number;
}

export interface IdentityProvider {
readonly type: string;
fromJson(data: IdentityData): Identity;
toJson(identity: Identity): IdentityData;
setUserContext(client: Client, identity: Identity, name: string): Promise<void>;
}
3 changes: 2 additions & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,7 @@
"unit-test:all": "npm run unit-test -- 'fabric-common/test/**/*.{js,ts}' && npm run unit-test -- 'fabric-ca-client/test/**/*.{js,ts}' && npm run unit-test -- 'fabric-client/test/**/*.{js,ts}' && npm run unit-test -- 'fabric-network/test/**/*.{js,ts}'",
"unit-test": "mocha --require ts-node/register --exclude 'fabric-client/test/data/**'",
"compile": "npm run compile-src && npm run compile-test",
"compile-src": "tsc --project fabric-network",
"compile-src": "tsc --project fabric-network/tsconfig-declaration.json && tsc --project fabric-network/tsconfig.json",
"compile-src:w": "tsc --project fabric-network --watch",
"compile-test": "tsc --project test/typescript",
"compile-test:w": "tsc --project test/typescript --watch",
Expand All @@ -40,6 +40,7 @@
"@types/chai-as-promised": "^7.1.2",
"@types/fs-extra": "^8.0.0",
"@types/mocha": "^5.2.7",
"@types/rimraf": "^2.0.2",
"@types/sinon": "^7.0.13",
"@types/tape": "^4.2.33",
"bn.js": "^4.11.8",
Expand Down

0 comments on commit e9e8c59

Please sign in to comment.