Skip to content

Commit

Permalink
Expose generic set of gateways, nodes, wallets, and identities from r…
Browse files Browse the repository at this point in the history
…untime (contributes to IBM-Blockchain#776)

Signed-off-by: Simon Stone <sstone1@uk.ibm.com>
  • Loading branch information
Simon Stone committed Apr 2, 2019
1 parent fa3253e commit 61cf6a0
Show file tree
Hide file tree
Showing 6 changed files with 273 additions and 4 deletions.
19 changes: 19 additions & 0 deletions client/src/fabric/FabricGateway.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
/*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/

export class FabricGateway {
public name: string;
public path: string;
public connectionProfile: object;
}
22 changes: 22 additions & 0 deletions client/src/fabric/FabricIdentity.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
/*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/

// This is a JSON representation of a Fabric identity.
// tslint:disable variable-name
export class FabricIdentity {
public name: string;
public certificate: string;
public private_key: string;
public msp_id: string;
}
30 changes: 30 additions & 0 deletions client/src/fabric/FabricNode.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,30 @@
/*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/

export enum FabricNodeType {
PEER = 'fabric-peer',
CERTIFICATE_AUTHORITY = 'fabric-ca',
ORDERER = 'fabric-orderer'
}

// This is a JSON representation of a Fabric node.
// tslint:disable variable-name
export class FabricNode {
public short_name: string;
public name: string;
public url: string;
public type: FabricNodeType;
public wallet: string;
public identity: string;
}
71 changes: 71 additions & 0 deletions client/src/fabric/FabricRuntime.ts
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,9 @@ import { Docker, ContainerPorts } from '../docker/Docker';
import { UserInputUtil } from '../commands/UserInputUtil';
import { LogType } from '../logging/OutputAdapter';
import * as request from 'request';
import { FabricIdentity } from './FabricIdentity';
import { FabricNode, FabricNodeType } from './FabricNode';
import { FabricGateway } from './FabricGateway';

const basicNetworkPath: string = path.resolve(__dirname, '..', '..', '..', 'basic-network');
const basicNetworkConnectionProfilePath: string = path.resolve(basicNetworkPath, 'connection.json');
Expand Down Expand Up @@ -138,6 +141,74 @@ export class FabricRuntime extends EventEmitter {
}
}

public async getGateways(): Promise<FabricGateway[]> {
const connectionProfile: object = await this.getConnectionProfile();
return [
{
name: 'local_fabric',
path: this.getConnectionProfilePath(),
connectionProfile
}
];
}

public async getNodes(): Promise<FabricNode[]> {
const containerPrefix: string = this.docker.getContainerPrefix();
const peerPorts: ContainerPorts = await this.docker.getContainerPorts(`${containerPrefix}_peer0.org1.example.com`);
const peerRequestPort: string = peerPorts['7051/tcp'][0].HostPort;
const caPorts: ContainerPorts = await this.docker.getContainerPorts(`${containerPrefix}_ca.example.com`);
const caPort: string = caPorts['7054/tcp'][0].HostPort;
const ordererPorts: ContainerPorts = await this.docker.getContainerPorts(`${containerPrefix}_orderer.example.com`);
const ordererPort: string = ordererPorts['7050/tcp'][0].HostPort;
return [
{
short_name: 'peer0.org1.example.com',
name: 'peer0.org1.example.com',
type: FabricNodeType.PEER,
url: `grpc://localhost:${peerRequestPort}`,
wallet: 'local_wallet',
identity: 'Admin@org1.example.com'
},
{
short_name: 'ca.example.com',
name: 'ca.example.com',
type: FabricNodeType.CERTIFICATE_AUTHORITY,
url: `http://localhost:${caPort}`,
wallet: 'local_wallet',
identity: 'admin'
},
{
short_name: 'orderer.example.com',
name: 'orderer.example.com',
type: FabricNodeType.ORDERER,
url: `grpc://localhost:${ordererPort}`,
wallet: 'local_wallet',
identity: 'Admin@org1.example.com'
}
];
}

public async getWalletNames(): Promise<string[]> {
return [
'local_wallet'
];
}

public async getIdentities(walletName: string): Promise<FabricIdentity[]> {
if (walletName !== 'local_wallet') {
throw new Error(`The wallet ${walletName} does not exist`);
} else {
return [
{
name: 'Admin@org1.example.com',
certificate: Buffer.from(basicNetworkAdminCertificate, 'utf8').toString('base64'),
private_key: Buffer.from(basicNetworkAdminPrivateKey, 'utf8').toString('base64'),
msp_id: 'Org1MSP'
}
];
}
}

public async getConnectionProfile(): Promise<object> {
const containerPrefix: string = this.docker.getContainerPrefix();
const connectionProfile: any = basicNetworkConnectionProfile;
Expand Down
5 changes: 1 addition & 4 deletions client/src/fabric/FabricWallet.ts
Original file line number Diff line number Diff line change
Expand Up @@ -25,10 +25,7 @@ export class FabricWallet extends FileSystemWallet implements IFabricWallet {
}

public async importIdentity(certificate: string, privateKey: string, identityName: string, mspid: string): Promise<void> {

const wallet: FileSystemWallet = new FileSystemWallet(this.walletPath);
await wallet.import(identityName, X509WalletMixin.createIdentity(mspid, certificate, privateKey));

await this.import(identityName, X509WalletMixin.createIdentity(mspid, certificate, privateKey));
}

public async getIdentityNames(): Promise<string[]> {
Expand Down
130 changes: 130 additions & 0 deletions client/test/fabric/FabricRuntime.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -31,6 +31,9 @@ import { VSCodeBlockchainOutputAdapter } from '../../src/logging/VSCodeBlockchai
import { LogType } from '../../src/logging/OutputAdapter';
import { CommandUtil } from '../../src/util/CommandUtil';
import { VSCodeBlockchainDockerOutputAdapter } from '../../src/logging/VSCodeBlockchainDockerOutputAdapter';
import { FabricGateway } from '../../src/fabric/FabricGateway';
import { FabricNode } from '../../src/fabric/FabricNode';
import { FabricIdentity } from '../../src/fabric/FabricIdentity';

chai.should();

Expand Down Expand Up @@ -1107,4 +1110,131 @@ describe('FabricRuntime', () => {
abortRequestStub.should.not.have.been.called;
});
});

describe('#getGateways', () => {
it('should return an array of gateways', async () => {
const gateways: FabricGateway[] = await runtime.getGateways();
gateways.should.deep.equal([
{
name: 'local_fabric',
path: runtime.getConnectionProfilePath(),
connectionProfile: {
name: 'basic-network',
version: '1.0.0',
client: {
organization: 'Org1',
connection: {
timeout: {
peer: {
endorser: '300',
eventHub: '300',
eventReg: '300'
},
orderer: '300'
}
}
},
channels: {
mychannel: {
orderers: [
'orderer.example.com'
],
peers: {
'peer0.org1.example.com': {}
}
}
},
organizations: {
Org1: {
mspid: 'Org1MSP',
peers: [
'peer0.org1.example.com'
],
certificateAuthorities: [
'ca.org1.example.com'
]
}
},
orderers: {
'orderer.example.com': {
url: 'grpc://127.0.0.1:12347'
}
},
peers: {
'peer0.org1.example.com': {
url: 'grpc://localhost:12345',
eventUrl: 'grpc://localhost:12346'
}
},
certificateAuthorities: {
'ca.org1.example.com': {
url: 'http://127.0.0.1:12348',
caName: 'ca.example.com'
}
}
}
}
]);
});
});

describe('#getNodes', () => {
it('should return an array of nodes', async () => {
const nodes: FabricNode[] = await runtime.getNodes();
nodes.should.deep.equal([
{
short_name: 'peer0.org1.example.com',
name: 'peer0.org1.example.com',
type: 'fabric-peer',
url: 'grpc://localhost:12345',
wallet: 'local_wallet',
identity: 'Admin@org1.example.com'
},
{
short_name: 'ca.example.com',
name: 'ca.example.com',
type: 'fabric-ca',
url: 'http://localhost:12348',
wallet: 'local_wallet',
identity: 'admin'
},
{
short_name: 'orderer.example.com',
name: 'orderer.example.com',
type: 'fabric-orderer',
url: 'grpc://localhost:12347',
wallet: 'local_wallet',
identity: 'Admin@org1.example.com'
}
]);
});
});

describe('#getWalletName', () => {
it('should return an array of wallet names', async () => {
const walletNames: string[] = await runtime.getWalletNames();
walletNames.should.deep.equal([
'local_wallet'
]);
});
});

describe('#getIdentities', () => {
it('should return an array of identities for a wallet that exists', async () => {
const identities: FabricIdentity[] = await runtime.getIdentities('local_wallet');
identities.should.deep.equal([
{
name: 'Admin@org1.example.com',
certificate: 'LS0tLS1CRUdJTiBDRVJUSUZJQ0FURS0tLS0tCk1JSUNXVENDQWYrZ0F3SUJBZ0lVUllSV0JZbjUxRXRsNUNXMDJQWC96OUVQMjhZd0NnWUlLb1pJemowRUF3SXcKY3pFTE1Ba0dBMVVFQmhNQ1ZWTXhFekFSQmdOVkJBZ1RDa05oYkdsbWIzSnVhV0V4RmpBVUJnTlZCQWNURFZOaApiaUJHY21GdVkybHpZMjh4R1RBWEJnTlZCQW9URUc5eVp6RXVaWGhoYlhCc1pTNWpiMjB4SERBYUJnTlZCQU1UCkUyTmhMbTl5WnpFdVpYaGhiWEJzWlM1amIyMHdIaGNOTVRrd05EQXhNVFl3TURBd1doY05NakF3TXpNeE1UWXcKTlRBd1dqQmRNUXN3Q1FZRFZRUUdFd0pWVXpFWE1CVUdBMVVFQ0JNT1RtOXlkR2dnUTJGeWIyeHBibUV4RkRBUwpCZ05WQkFvVEMwaDVjR1Z5YkdWa1oyVnlNUTh3RFFZRFZRUUxFd1pqYkdsbGJuUXhEakFNQmdOVkJBTVRCV0ZrCmJXbHVNRmt3RXdZSEtvWkl6ajBDQVFZSUtvWkl6ajBEQVFjRFFnQUV4Q1dsWmhiQkV6dW1BZ3hNWmZBZURIbFgKU3ZOOEZmMEprcXFneDY3YmdzQWdmMk1hTVhPZmFKNC9nN2gxOWY4bERHS1NUdVVab0lRNUlQREVSOWVrOGFPQgpoakNCZ3pBT0JnTlZIUThCQWY4RUJBTUNCNEF3REFZRFZSMFRBUUgvQkFJd0FEQWRCZ05WSFE0RUZnUVUwT05yCjJLSHdMYnAzQURjQW5HZWV5YXBlYXhRd0t3WURWUjBqQkNRd0lvQWdRam1xRGMxMjJ1NjR1Z3phY0JoUjBVVUUKMHhxdEd5M2QyNnhxVnpaZVNYd3dGd1lEVlIwUkJCQXdEb0lNWkdRMk9XUmhNR1ppWWprd01Bb0dDQ3FHU000OQpCQU1DQTBnQU1FVUNJUUN1Y1N5ZmR0VlptbUVQOFEzQzNRZ1ZKd0dFK0xtcDVDMmk1ZHFoTmhTZi93SWdiZGlBCnBGK2hhT01VUVltUnhDZ2ZXbGRsN0N3eGQrMytMTjZ3UFc4QS9tND0KLS0tLS1FTkQgQ0VSVElGSUNBVEUtLS0tLQo=',
private_key: 'LS0tLS1CRUdJTiBQUklWQVRFIEtFWS0tLS0tCk1JR0hBZ0VBTUJNR0J5cUdTTTQ5QWdFR0NDcUdTTTQ5QXdFSEJHMHdhd0lCQVFRZ1JheWhQZ1lxbnV5T0pzczQKd3V3ZjZwNmpkMlRjRkVFblpmWDM0bHZiOFJxaFJBTkNBQVRFSmFWbUZzRVRPNllDREV4bDhCNE1lVmRLODN3VgovUW1TcXFESHJ0dUN3Q0IvWXhveGM1OW9uaitEdUhYMS95VU1ZcEpPNVJtZ2hEa2c4TVJIMTZUeAotLS0tLUVORCBQUklWQVRFIEtFWS0tLS0tCg==',
msp_id: 'Org1MSP'
}
]);
});

it('should throw for a wallet that does not exist', async () => {
await runtime.getIdentities('no identities here').should.be.rejectedWith(/does not exist/);
});
});

});

0 comments on commit 61cf6a0

Please sign in to comment.