Skip to content

Commit

Permalink
FABN-861 fabric-network typescript definitions
Browse files Browse the repository at this point in the history
typescript definitions for current fabric-network
implementation

Change-Id: I516c1cdb1c316c05c2d188c763094a434c490f4b
Signed-off-by: Dave Kelsey <[email protected]>
  • Loading branch information
Dave Kelsey committed Aug 30, 2018
1 parent eb56c95 commit ec0dc97
Show file tree
Hide file tree
Showing 8 changed files with 230 additions and 3 deletions.
2 changes: 2 additions & 0 deletions fabric-network/lib/api/wallet.js
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,8 @@
*
* SPDX-License-Identifier: Apache-2.0
*/
/* eslint-disable no-unused-vars */


'use strict';

Expand Down
1 change: 0 additions & 1 deletion fabric-network/lib/impl/wallet/inmemorywallet.js
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,6 @@ const Client = require('fabric-client');
const BaseWallet = require('./basewallet');
const api = require('fabric-client/lib/api.js');
const logger = require('../../logger').getLogger('InMemoryWallet');
const util = require('util');

// this will be shared across all instance of a memory wallet, so really an app should
// only have one instance otherwise if you put 2 different identities with the same
Expand Down
1 change: 0 additions & 1 deletion fabric-network/lib/impl/wallet/x509walletmixin.js
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,6 @@
'use strict';

const logger = require('../../logger').getLogger('X509WalletMixin');
const util = require('util');

class X509WalletMixin {

Expand Down
1 change: 1 addition & 0 deletions fabric-network/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@
"scripts": {
"test": "nyc mocha --recursive -t 10000"
},
"types": "./types/index.d.ts",
"dependencies": {
"fabric-ca-client": "file:../fabric-ca-client",
"fabric-client": "file:../fabric-client",
Expand Down
104 changes: 104 additions & 0 deletions fabric-network/types/index.d.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,104 @@
/**
* Copyright 2018 IBM All Rights Reserved.
*
* SPDX-License-Identifier: Apache-2.0
*/

import { User } from 'fabric-client';

import Client = require('fabric-client');


//-------------------------------------------
// Main fabric network classes
//-------------------------------------------
export interface InitOptions {
commitTimeout?: number;
wallet: Wallet;
identity: string;
clientTlsIdentity?: string;
}

export class Network {
constructor();
initialize(ccp: string | Client, options?: InitOptions): Promise<void>;
getCurrentIdentity(): User;
getClient(): Client;
getOptions(): InitOptions;
getChannel(channelName: string): Promise<FabricNetwork.Channel>;
dispose(): void;
}

// put into it's own separate namespace to avoid a clash with fabric-client Channel
declare namespace FabricNetwork {
export class Channel {
getInternalChannel(): Client.Channel;
getPeerMap(): Map<string, Client.ChannelPeer[]>;
getContract(chaincodeId: string): Contract;
// will be coming
// getEventHubs(): ChannelEventHub[];
}
}

export class Contract {
executeTransaction(transactionName: string, ...parameters: string[]): Promise<Buffer>;
submitTransaction(transactionName: string, ...parameters: string[]): Promise<Buffer>;
}

//-------------------------------------------
// Wallet Management
//-------------------------------------------
export interface Identity {
type: string
}

export interface X509Identity extends Identity {
mspId: string,
certificate: string,
privateKey: string
}

export interface IdentityInformation {
label: string,
mspId: string,
identifier: string
}

interface WalletAPI {
import(label: string, identity: Identity): Promise<void>;
export(label: string): Promise<Identity>;
list(): Promise<IdentityInformation[]>;
delete(label: string): Promise<void>;
exists(label: string): Promise<boolean>;
}

interface Wallet extends WalletAPI {
}

interface WalletMixin {
}

declare abstract class BaseWallet implements Wallet {
import(label: string, identity: Identity): Promise<void>;
export(label: string): Promise<Identity>;
list(): Promise<IdentityInformation[]>;
abstract delete(label: string): Promise<void>;
abstract exists(label: string): Promise<boolean>;
}

export class InMemoryWallet extends BaseWallet {
constructor(mixin?: WalletMixin);
delete(label: string): Promise<void>;
exists(label: string): Promise<boolean>;
}

export class FileSystemWallet extends BaseWallet {
constructor(path: string, mixin?: WalletMixin);
delete(label: string): Promise<void>;
exists(label: string): Promise<boolean>;
}

export class X509WalletMixin implements WalletMixin {
constructor();
static createIdentity(mspId: string, certificate: string, privateKey: string): X509Identity;
}
29 changes: 29 additions & 0 deletions fabric-network/types/tsconfig.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,29 @@
{
"compilerOptions": {
"removeComments": false,
"preserveConstEnums": true,
"sourceMap": true,
"declaration": true,
"noImplicitAny": true,
"noImplicitReturns": true,
"noImplicitThis": true,
"suppressImplicitAnyIndexErrors": true,
"moduleResolution": "node",
"module": "commonjs",
"target": "es6",
"outDir": "dist",
"baseUrl": ".",
"paths": {
"*": [
"node_modules/*"
]
}
},
"files": [
"index.d.ts"
],
"formatCodeOptions": {
"indentSize": 2,
"tabSize": 2
}
}
3 changes: 2 additions & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,8 @@
"elliptic": "^6.3.2",
"eslint": "^5.1.0",
"fabric-ca-client": "file:./fabric-ca-client",
"fabric-client": "file:./fabric-client",
"fabric-client": "file:./fabric-client",
"fabric-network": "file:./fabric-network",
"fs-extra": "^6.0.1",
"gulp": "^3.9.1",
"gulp-add-src": "^1.0.0",
Expand Down
92 changes: 92 additions & 0 deletions test/typescript/network.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,92 @@
/*
Copyright 2018 IBM All Rights Reserved.
SPDX-License-Identifier: Apache-2.0
*/
import {
FileSystemWallet,
InMemoryWallet,
Contract,
FabricNetwork,
Identity,
IdentityInformation,
InitOptions,
Network,
Wallet,
X509Identity,
X509WalletMixin

} from 'fabric-network';

import Client = require('fabric-client');

import {
User,
Channel,
ChannelPeer
} from 'fabric-client';

(async () => {

const cert: string = 'acertificate';
const key: string = 'akey';
const inMemoryWallet: Wallet = new InMemoryWallet();
const fileSystemWallet: FileSystemWallet = new FileSystemWallet('path');

const id1: Identity = X509WalletMixin.createIdentity('Org1MSP', cert, key);
const id2: X509Identity = X509WalletMixin.createIdentity('Org1MSP', cert, key);
let importDone: Promise<void> = inMemoryWallet.import('[email protected]', id1);
await importDone;
await fileSystemWallet.import('[email protected]', id2);
const exists: boolean = await inMemoryWallet.exists('[email protected]');

const id3: Identity = await fileSystemWallet.export('anod');
//const id4: X509Identity = await inMemoryWallet.export('anod'); can't do this
const id4: X509Identity = <X509Identity>id3;

const idList: IdentityInformation[] = await inMemoryWallet.list();

const network: Network = new Network();

const opt1: InitOptions = {
wallet: inMemoryWallet,
identity: '[email protected]',
clientTlsIdentity: 'tlsId',
commitTimeout: 1000
};

await network.initialize('accp', opt1);

const network2: Network = new Network();
const client: Client = new Client();
const opt2: InitOptions = {
wallet: fileSystemWallet,
identity: 'anod'
};

await network.initialize(client, opt2);


const channel: FabricNetwork.Channel = await network.getChannel('a channel');
const contract: Contract = await channel.getContract('chaincode');

let response: Buffer = await contract.submitTransaction('move', 'a', 'b','100');
response = await contract.executeTransaction('move', 'a', 'b','100');

const aClient: Client = network.getClient();
const user: User = network.getCurrentIdentity();
const opt3: InitOptions = network.getOptions();

const internalChannel: Channel = channel.getInternalChannel();
const peerMap: Map<string, ChannelPeer[]> = channel.getPeerMap();

const deleteDone: Promise<void> = inMemoryWallet.delete('[email protected]')
await deleteDone;
await fileSystemWallet.delete('[email protected]');
network.dispose();
network2.dispose();

})();


0 comments on commit ec0dc97

Please sign in to comment.