From c35cfe755c75ae860fdf28eb7fc89215557635c5 Mon Sep 17 00:00:00 2001 From: AzaharaC Date: Wed, 31 Mar 2021 12:48:58 +0200 Subject: [PATCH] feat(connector-fabric): common interface Signed-off-by: AzaharaC --- .../web-services/insert-shipment-endpoint.ts | 8 ++--- .../web-services/list-shipment-endpoint.ts | 8 ++--- .../src/main/json/openapi.json | 23 ++++++++------ .../generated/openapi/typescript-axios/api.ts | 14 ++++++--- .../plugin-ledger-connector-fabric.ts | 30 +++++++++++-------- .../deploy-cc-from-golang-source.test.ts | 18 +++++------ .../run-transaction-endpoint-v1.test.ts | 26 ++++++++-------- .../run-transaction-endpoint-v1.test.ts | 28 ++++++++--------- 8 files changed, 85 insertions(+), 70 deletions(-) diff --git a/examples/cactus-example-supply-chain-business-logic-plugin/src/main/typescript/business-logic-plugin/web-services/insert-shipment-endpoint.ts b/examples/cactus-example-supply-chain-business-logic-plugin/src/main/typescript/business-logic-plugin/web-services/insert-shipment-endpoint.ts index d2779d7d28..31d083a8c4 100644 --- a/examples/cactus-example-supply-chain-business-logic-plugin/src/main/typescript/business-logic-plugin/web-services/insert-shipment-endpoint.ts +++ b/examples/cactus-example-supply-chain-business-logic-plugin/src/main/typescript/business-logic-plugin/web-services/insert-shipment-endpoint.ts @@ -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 }, diff --git a/examples/cactus-example-supply-chain-business-logic-plugin/src/main/typescript/business-logic-plugin/web-services/list-shipment-endpoint.ts b/examples/cactus-example-supply-chain-business-logic-plugin/src/main/typescript/business-logic-plugin/web-services/list-shipment-endpoint.ts index 8909aaf333..76743545e3 100644 --- a/examples/cactus-example-supply-chain-business-logic-plugin/src/main/typescript/business-logic-plugin/web-services/list-shipment-endpoint.ts +++ b/examples/cactus-example-supply-chain-business-logic-plugin/src/main/typescript/business-logic-plugin/web-services/list-shipment-endpoint.ts @@ -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 }, diff --git a/packages/cactus-plugin-ledger-connector-fabric/src/main/json/openapi.json b/packages/cactus-plugin-ledger-connector-fabric/src/main/json/openapi.json index fba169d461..1a8325a2e6 100644 --- a/packages/cactus-plugin-ledger-connector-fabric/src/main/json/openapi.json +++ b/packages/cactus-plugin-ledger-connector-fabric/src/main/json/openapi.json @@ -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 }, @@ -234,7 +234,7 @@ "maxLength": 100, "nullable": false }, - "chainCodeId": { + "contractName": { "type": "string", "minLength": 1, "maxLength": 100, @@ -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": [], @@ -265,12 +265,17 @@ "RunTransactionResponse": { "type": "object", "required": [ - "functionOutput" + "functionOutput", + "success" ], "properties": { "functionOutput": { "type": "string", "nullable": false + }, + "success": { + "type": "boolean", + "nullable": false } } }, diff --git a/packages/cactus-plugin-ledger-connector-fabric/src/main/typescript/generated/openapi/typescript-axios/api.ts b/packages/cactus-plugin-ledger-connector-fabric/src/main/typescript/generated/openapi/typescript-axios/api.ts index 3f1384e008..a865a0df48 100644 --- a/packages/cactus-plugin-ledger-connector-fabric/src/main/typescript/generated/openapi/typescript-axios/api.ts +++ b/packages/cactus-plugin-ledger-connector-fabric/src/main/typescript/generated/openapi/typescript-axios/api.ts @@ -377,7 +377,7 @@ export interface RunTransactionRequest { * @type {FabricSigningCredential} * @memberof RunTransactionRequest */ - fabricSigningCredential: FabricSigningCredential; + signingCredential: FabricSigningCredential; /** * * @type {string} @@ -389,7 +389,7 @@ export interface RunTransactionRequest { * @type {string} * @memberof RunTransactionRequest */ - chainCodeId: string; + contractName: string; /** * * @type {FabricContractInvocationType} @@ -401,13 +401,13 @@ export interface RunTransactionRequest { * @type {string} * @memberof RunTransactionRequest */ - functionName: string; + methodName: string; /** * * @type {Array} * @memberof RunTransactionRequest */ - functionArgs: Array; + params: Array; } /** * @@ -421,6 +421,12 @@ export interface RunTransactionResponse { * @memberof RunTransactionResponse */ functionOutput: string; + /** + * + * @type {boolean} + * @memberof RunTransactionResponse + */ + success: boolean; } /** * diff --git a/packages/cactus-plugin-ledger-connector-fabric/src/main/typescript/plugin-ledger-connector-fabric.ts b/packages/cactus-plugin-ledger-connector-fabric/src/main/typescript/plugin-ledger-connector-fabric.ts index 26501dbedd..c3c0f9cf00 100644 --- a/packages/cactus-plugin-ledger-connector-fabric/src/main/typescript/plugin-ledger-connector-fabric.ts +++ b/packages/cactus-plugin-ledger-connector-fabric/src/main/typescript/plugin-ledger-connector-fabric.ts @@ -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( - 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 = { @@ -483,7 +483,7 @@ export class PluginLedgerConnectorFabric const gatewayOptions: GatewayOptions = { discovery: this.opts.discoveryOptions, eventHandlerOptions, - identity: fabricSigningCredential.keychainRef, + identity: signingCredential.keychainRef, wallet, }; @@ -491,16 +491,19 @@ export class PluginLedgerConnectorFabric 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: { @@ -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(); diff --git a/packages/cactus-plugin-ledger-connector-fabric/src/test/typescript/integration/deploy-contract-go-bin-endpoint-v1/deploy-contract/deploy-cc-from-golang-source.test.ts b/packages/cactus-plugin-ledger-connector-fabric/src/test/typescript/integration/deploy-contract-go-bin-endpoint-v1/deploy-contract/deploy-cc-from-golang-source.test.ts index a35f4ea1d4..ccd3d734b0 100644 --- a/packages/cactus-plugin-ledger-connector-fabric/src/test/typescript/integration/deploy-contract-go-bin-endpoint-v1/deploy-contract/deploy-cc-from-golang-source.test.ts +++ b/packages/cactus-plugin-ledger-connector-fabric/src/test/typescript/integration/deploy-contract-go-bin-endpoint-v1/deploy-contract/deploy-cc-from-golang-source.test.ts @@ -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"); diff --git a/packages/cactus-plugin-ledger-connector-fabric/src/test/typescript/integration/fabric-v1-4-x/run-transaction-endpoint-v1.test.ts b/packages/cactus-plugin-ledger-connector-fabric/src/test/typescript/integration/fabric-v1-4-x/run-transaction-endpoint-v1.test.ts index 77f1068b26..2d1441798c 100644 --- a/packages/cactus-plugin-ledger-connector-fabric/src/test/typescript/integration/fabric-v1-4-x/run-transaction-endpoint-v1.test.ts +++ b/packages/cactus-plugin-ledger-connector-fabric/src/test/typescript/integration/fabric-v1-4-x/run-transaction-endpoint-v1.test.ts @@ -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); @@ -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); @@ -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); diff --git a/packages/cactus-plugin-ledger-connector-fabric/src/test/typescript/integration/fabric-v2-2-x/run-transaction-endpoint-v1.test.ts b/packages/cactus-plugin-ledger-connector-fabric/src/test/typescript/integration/fabric-v2-2-x/run-transaction-endpoint-v1.test.ts index daa4c77774..639b811d36 100644 --- a/packages/cactus-plugin-ledger-connector-fabric/src/test/typescript/integration/fabric-v2-2-x/run-transaction-endpoint-v1.test.ts +++ b/packages/cactus-plugin-ledger-connector-fabric/src/test/typescript/integration/fabric-v2-2-x/run-transaction-endpoint-v1.test.ts @@ -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); @@ -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); @@ -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);