-
Notifications
You must be signed in to change notification settings - Fork 515
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
FABN-861 fabric-network typescript definitions
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
Showing
8 changed files
with
230 additions
and
3 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -3,6 +3,8 @@ | |
* | ||
* SPDX-License-Identifier: Apache-2.0 | ||
*/ | ||
/* eslint-disable no-unused-vars */ | ||
|
||
|
||
'use strict'; | ||
|
||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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; | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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(); | ||
|
||
})(); | ||
|
||
|