Skip to content

Commit

Permalink
feat(connector-fabric): common interface
Browse files Browse the repository at this point in the history
Signed-off-by: AzaharaC <a.castano.benito@accenture.com>
  • Loading branch information
AzaharaC authored and petermetz committed Apr 1, 2021
1 parent c348aa4 commit c35cfe7
Show file tree
Hide file tree
Showing 8 changed files with 85 additions and 70 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -77,15 +77,15 @@ export class InsertShipmentEndpoint implements IWebServiceEndpoint {
const { shipment } = req.body as InsertShipmentRequest;
this.log.debug(`${tag} %o`, shipment);
const request: RunTransactionRequest = {
fabricSigningCredential: {
signingCredential: {
keychainId: "PluginKeychainMemory_C",
keychainRef: "user2",
},
channelName: "mychannel",
chainCodeId: "shipment",
contractName: "shipment",
invocationType: FabricContractInvocationType.SEND,
functionName: "insertShipment",
functionArgs: [shipment.id, shipment.bookshelfId],
methodName: "insertShipment",
params: [shipment.id, shipment.bookshelfId],
};
const {
data: { functionOutput },
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -76,15 +76,15 @@ export class ListShipmentEndpoint implements IWebServiceEndpoint {
try {
this.log.debug(`${tag}`);
const request: RunTransactionRequest = {
fabricSigningCredential: {
signingCredential: {
keychainId: "PluginKeychainMemory_C",
keychainRef: "user2",
},
channelName: "mychannel",
chainCodeId: "shipment",
contractName: "shipment",
invocationType: FabricContractInvocationType.CALL,
functionName: "getListShipment",
functionArgs: [],
methodName: "getListShipment",
params: [],
};
const {
data: { functionOutput },
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -216,15 +216,15 @@
"RunTransactionRequest": {
"type": "object",
"required": [
"fabricSigningCredential",
"signingCredential",
"channelName",
"chainCodeId",
"contractName",
"invocationType",
"functionName",
"functionArgs"
"methodName",
"params"
],
"properties": {
"fabricSigningCredential": {
"signingCredential": {
"$ref": "#/components/schemas/FabricSigningCredential",
"nullable": false
},
Expand All @@ -234,7 +234,7 @@
"maxLength": 100,
"nullable": false
},
"chainCodeId": {
"contractName": {
"type": "string",
"minLength": 1,
"maxLength": 100,
Expand All @@ -245,13 +245,13 @@
"nullable": false,
"description": "Indicates if it is a CALL or a SEND type of invocation where only SEND ends up creating an actual transaction on the ledger."
},
"functionName": {
"methodName": {
"type": "string",
"minLength": 1,
"maxLength": 100,
"nullable": false
},
"functionArgs": {
"params": {
"type": "array",
"nullable": false,
"default": [],
Expand All @@ -265,12 +265,17 @@
"RunTransactionResponse": {
"type": "object",
"required": [
"functionOutput"
"functionOutput",
"success"
],
"properties": {
"functionOutput": {
"type": "string",
"nullable": false
},
"success": {
"type": "boolean",
"nullable": false
}
}
},
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -377,7 +377,7 @@ export interface RunTransactionRequest {
* @type {FabricSigningCredential}
* @memberof RunTransactionRequest
*/
fabricSigningCredential: FabricSigningCredential;
signingCredential: FabricSigningCredential;
/**
*
* @type {string}
Expand All @@ -389,7 +389,7 @@ export interface RunTransactionRequest {
* @type {string}
* @memberof RunTransactionRequest
*/
chainCodeId: string;
contractName: string;
/**
*
* @type {FabricContractInvocationType}
Expand All @@ -401,13 +401,13 @@ export interface RunTransactionRequest {
* @type {string}
* @memberof RunTransactionRequest
*/
functionName: string;
methodName: string;
/**
*
* @type {Array<string>}
* @memberof RunTransactionRequest
*/
functionArgs: Array<string>;
params: Array<string>;
}
/**
*
Expand All @@ -421,6 +421,12 @@ export interface RunTransactionResponse {
* @memberof RunTransactionResponse
*/
functionOutput: string;
/**
*
* @type {boolean}
* @memberof RunTransactionResponse
*/
success: boolean;
}
/**
*
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -439,35 +439,35 @@ export class PluginLedgerConnectorFabric

const { connectionProfile } = this.opts;
const {
fabricSigningCredential,
signingCredential,
channelName,
chainCodeId,
contractName,
invocationType,
functionName: fnName,
functionArgs,
methodName: fnName,
params,
} = req;

const gateway = new Gateway();
const wallet = new InMemoryWallet(new X509WalletMixin());
const keychain = this.opts.pluginRegistry.findOneByKeychainId(
fabricSigningCredential.keychainId,
signingCredential.keychainId,
);
this.log.debug(
"transact() obtained keychain by ID=%o OK",
fabricSigningCredential.keychainId,
signingCredential.keychainId,
);

const fabricX509IdentityJson = await keychain.get<string>(
fabricSigningCredential.keychainRef,
signingCredential.keychainRef,
);
this.log.debug(
"transact() obtained keychain entry Key=%o OK",
fabricSigningCredential.keychainRef,
signingCredential.keychainRef,
);
const identity = JSON.parse(fabricX509IdentityJson);

try {
await wallet.import(fabricSigningCredential.keychainRef, identity);
await wallet.import(signingCredential.keychainRef, identity);
this.log.debug("transact() imported identity to in-memory wallet OK");

const eventHandlerOptions: DefaultEventHandlerOptions = {
Expand All @@ -483,24 +483,27 @@ export class PluginLedgerConnectorFabric
const gatewayOptions: GatewayOptions = {
discovery: this.opts.discoveryOptions,
eventHandlerOptions,
identity: fabricSigningCredential.keychainRef,
identity: signingCredential.keychainRef,
wallet,
};

await gateway.connect(connectionProfile as any, gatewayOptions);
this.log.debug("transact() gateway connection established OK");

const network = await gateway.getNetwork(channelName);
const contract = network.getContract(chainCodeId);
const contract = network.getContract(contractName);

let out: Buffer;
let success: boolean;
switch (invocationType) {
case FabricContractInvocationType.CALL: {
out = await contract.evaluateTransaction(fnName, ...functionArgs);
out = await contract.evaluateTransaction(fnName, ...params);
success = true;
break;
}
case FabricContractInvocationType.SEND: {
out = await contract.submitTransaction(fnName, ...functionArgs);
out = await contract.submitTransaction(fnName, ...params);
success = true;
break;
}
default: {
Expand All @@ -511,6 +514,7 @@ export class PluginLedgerConnectorFabric
const outUtf8 = out.toString("utf-8");
const res: RunTransactionResponse = {
functionOutput: outUtf8,
success,
};
this.log.debug(`transact() response: %o`, res);
this.prometheusExporter.addCurrentTransaction();
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -191,30 +191,30 @@ test(testCase, async (t: Test) => {

const testKey = uuidv4();
const testValue = uuidv4();
const fabricSigningCredential: FabricSigningCredential = {
const signingCredential: FabricSigningCredential = {
keychainId,
keychainRef: keychainEntryKey,
};

const setRes = await apiClient.runTransactionV1({
chainCodeId: "hello-world",
contractName: "hello-world",
channelName: "mychannel",
functionArgs: [testKey, testValue],
functionName: "set",
params: [testKey, testValue],
methodName: "set",
invocationType: FabricContractInvocationType.SEND,
fabricSigningCredential,
signingCredential,
});
t.ok(setRes, "setRes truthy OK");
t.true(setRes.status > 199 && setRes.status < 300, "setRes status 2xx OK");
t.comment(`HelloWorld.set() ResponseBody: ${JSON.stringify(setRes.data)}`);

const getRes = await apiClient.runTransactionV1({
chainCodeId: "hello-world",
contractName: "hello-world",
channelName: "mychannel",
functionArgs: [testKey],
functionName: "get",
params: [testKey],
methodName: "get",
invocationType: FabricContractInvocationType.CALL,
fabricSigningCredential,
signingCredential,
});
t.ok(getRes, "getRes truthy OK");
t.true(getRes.status > 199 && setRes.status < 300, "getRes status 2xx OK");
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -136,19 +136,19 @@ test(testCase, async (t: Test) => {

const carId = "CAR277";
const carOwner = uuidv4();
const fabricSigningCredential: FabricSigningCredential = {
const signingCredential: FabricSigningCredential = {
keychainId,
keychainRef: keychainEntryKey,
};

{
const res = await apiClient.runTransactionV1({
fabricSigningCredential,
signingCredential,
channelName: "mychannel",
chainCodeId: "fabcar",
contractName: "fabcar",
invocationType: FabricContractInvocationType.CALL,
functionName: "queryAllCars",
functionArgs: [],
methodName: "queryAllCars",
params: [],
} as RunTransactionRequest);
t.ok(res);
t.ok(res.data);
Expand All @@ -158,12 +158,12 @@ test(testCase, async (t: Test) => {

{
const req: RunTransactionRequest = {
fabricSigningCredential,
signingCredential,
channelName: "mychannel",
invocationType: FabricContractInvocationType.SEND,
chainCodeId: "fabcar",
functionName: "createCar",
functionArgs: [carId, "Trabant", "601", "Blue", carOwner],
contractName: "fabcar",
methodName: "createCar",
params: [carId, "Trabant", "601", "Blue", carOwner],
};

const res = await apiClient.runTransactionV1(req);
Expand All @@ -173,12 +173,12 @@ test(testCase, async (t: Test) => {
}
{
const res = await apiClient.runTransactionV1({
fabricSigningCredential,
signingCredential,
channelName: "mychannel",
chainCodeId: "fabcar",
contractName: "fabcar",
invocationType: FabricContractInvocationType.CALL,
functionName: "queryAllCars",
functionArgs: [],
methodName: "queryAllCars",
params: [],
} as RunTransactionRequest);
t.ok(res);
t.ok(res.data);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -142,19 +142,19 @@ test(testCase, async (t: Test) => {
const assetOwner = uuidv4();

const channelName = "mychannel";
const chainCodeId = "basic";
const fabricSigningCredential: FabricSigningCredential = {
const contractName = "basic";
const signingCredential: FabricSigningCredential = {
keychainId,
keychainRef: keychainEntryKey,
};
{
const res = await apiClient.runTransactionV1({
fabricSigningCredential,
signingCredential,
channelName,
chainCodeId,
contractName,
invocationType: FabricContractInvocationType.CALL,
functionName: "GetAllAssets",
functionArgs: [],
methodName: "GetAllAssets",
params: [],
} as RunTransactionRequest);
t.ok(res);
t.ok(res.data);
Expand All @@ -163,12 +163,12 @@ test(testCase, async (t: Test) => {
}
{
const req: RunTransactionRequest = {
fabricSigningCredential,
signingCredential,
channelName,
invocationType: FabricContractInvocationType.SEND,
chainCodeId,
functionName: "CreateAsset",
functionArgs: [assetId, "yellow", "11", assetOwner, "199"],
contractName,
methodName: "CreateAsset",
params: [assetId, "yellow", "11", assetOwner, "199"],
};

const res = await apiClient.runTransactionV1(req);
Expand All @@ -179,12 +179,12 @@ test(testCase, async (t: Test) => {

{
const res = await apiClient.runTransactionV1({
fabricSigningCredential,
signingCredential,
channelName,
chainCodeId,
contractName,
invocationType: FabricContractInvocationType.CALL,
functionName: "GetAllAssets",
functionArgs: [],
methodName: "GetAllAssets",
params: [],
} as RunTransactionRequest);
t.ok(res);
t.ok(res.data);
Expand Down

0 comments on commit c35cfe7

Please sign in to comment.