From b49b04580dcd4d0b33b8addad95acbbe56631fed Mon Sep 17 00:00:00 2001 From: Pavel Murygin Date: Mon, 6 Dec 2021 14:21:16 +0300 Subject: [PATCH 01/13] Implement signing service --- src/__test__/unit/builtInHandler.spec.ts | 14 ++- src/internal/FluencePeer.ts | 13 ++- src/internal/KeyPair.ts | 8 ++ src/internal/builtInServices.ts | 143 +++++++++++++++++++++++ src/internal/defaultServices.ts | 121 ------------------- 5 files changed, 169 insertions(+), 130 deletions(-) create mode 100644 src/internal/builtInServices.ts delete mode 100644 src/internal/defaultServices.ts diff --git a/src/__test__/unit/builtInHandler.spec.ts b/src/__test__/unit/builtInHandler.spec.ts index 105fe7238..f8f96d813 100644 --- a/src/__test__/unit/builtInHandler.spec.ts +++ b/src/__test__/unit/builtInHandler.spec.ts @@ -1,6 +1,14 @@ import each from 'jest-each'; import { CallServiceData } from '../../internal/commonTypes'; -import { defaultServices } from '../../internal/defaultServices'; +import { BuiltInServiceContext, builtInServices } from '../../internal/builtInServices'; +import { KeyPair } from '../../internal/KeyPair'; + +const context = (async () => { + const res: BuiltInServiceContext = { + peerKeyPair: await KeyPair.randomEd25519(), + }; + return res; +})(); describe('Tests for default handler', () => { // prettier-ignore @@ -56,7 +64,7 @@ describe('Tests for default handler', () => { }; // act - const fn = defaultServices[req.serviceId][req.fnName]; + const fn = builtInServices(await context)[req.serviceId][req.fnName]; const res = await fn(req); // assert @@ -84,7 +92,7 @@ describe('Tests for default handler', () => { }; // act - const fn = defaultServices[req.serviceId][req.fnName]; + const fn = builtInServices(await context)[req.serviceId][req.fnName]; const res = await fn(req); // assert diff --git a/src/internal/FluencePeer.ts b/src/internal/FluencePeer.ts index 401ec7d72..23a75edbc 100644 --- a/src/internal/FluencePeer.ts +++ b/src/internal/FluencePeer.ts @@ -33,7 +33,7 @@ import { createInterpreter, dataToString } from './utils'; import { filter, pipe, Subject, tap } from 'rxjs'; import { RequestFlow } from './compilerSupport/v1'; import log from 'loglevel'; -import { defaultServices } from './defaultServices'; +import { BuiltInServiceContext, builtInServices } from './builtInServices'; import { instanceOf } from 'ts-pattern'; /** @@ -210,7 +210,7 @@ export class FluencePeer { } this._legacyCallServiceHandler = new LegacyCallServiceHandler(); - registerDefaultServices(this); + registerDefaultServices(this, { peerKeyPair: this._keyPair }); this._startParticleProcessing(); } @@ -576,10 +576,11 @@ function serviceFnKey(serviceId: string, fnName: string) { return `${serviceId}/${fnName}`; } -function registerDefaultServices(peer: FluencePeer) { - for (let serviceId in defaultServices) { - for (let fnName in defaultServices[serviceId]) { - const h = defaultServices[serviceId][fnName]; +function registerDefaultServices(peer: FluencePeer, context: BuiltInServiceContext) { + const ctx = builtInServices(context); + for (let serviceId in ctx) { + for (let fnName in ctx[serviceId]) { + const h = ctx[serviceId][fnName]; peer.internals.regHandler.common(serviceId, fnName, h); } } diff --git a/src/internal/KeyPair.ts b/src/internal/KeyPair.ts index 8fadd3f61..c97f5655c 100644 --- a/src/internal/KeyPair.ts +++ b/src/internal/KeyPair.ts @@ -54,4 +54,12 @@ export class KeyPair { toEd25519PrivateKey(): Uint8Array { return this.Libp2pPeerId.privKey.marshal().subarray(0, 32); } + + signBytes(data: Uint8Array): Promise { + return this.Libp2pPeerId.privKey.sign(data); + } + + verify(data: Uint8Array, signature: Uint8Array): Promise { + return this.Libp2pPeerId.privKey.public.verify(data, signature); + } } diff --git a/src/internal/builtInServices.ts b/src/internal/builtInServices.ts new file mode 100644 index 000000000..bdf56c078 --- /dev/null +++ b/src/internal/builtInServices.ts @@ -0,0 +1,143 @@ +/* + * Copyright 2021 Fluence Labs Limited + * + * 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. + */ + +import { CallServiceResult } from '@fluencelabs/avm'; +import { encode, decode } from 'bs58'; +import { GenericCallServiceHandler, ResultCodes } from './commonTypes'; +import { KeyPair } from './KeyPair'; + +const success = (result: any): CallServiceResult => { + return { + result: result, + retCode: ResultCodes.success, + }; +}; + +const error = (error: string): CallServiceResult => { + return { + result: error, + retCode: ResultCodes.unknownError, + }; +}; + +export interface BuiltInServiceContext { + peerKeyPair: KeyPair; +} + +export function builtInServices(context: BuiltInServiceContext): { + [serviceId in string]: { [fnName in string]: GenericCallServiceHandler }; +} { + return { + op: { + noop: (req) => { + return success({}); + }, + + array: (req) => { + return success(req.args); + }, + + identity: (req) => { + if (req.args.length > 1) { + return error(`identity accepts up to 1 arguments, received ${req.args.length} arguments`); + } else { + return success(req.args.length === 0 ? {} : req.args[0]); + } + }, + + concat: (req) => { + const incorrectArgIndices = req.args // + .map((x, i) => [Array.isArray(x), i]) + .filter(([isArray, _]) => !isArray) + .map(([_, index]) => index); + + if (incorrectArgIndices.length > 0) { + const str = incorrectArgIndices.join(', '); + return error(`All arguments of 'concat' must be arrays: arguments ${str} are not`); + } else { + return success([].concat.apply([], req.args)); + } + }, + + string_to_b58: (req) => { + if (req.args.length !== 1) { + return error('string_to_b58 accepts only one string argument'); + } else { + return success(encode(new TextEncoder().encode(req.args[0]))); + } + }, + + string_from_b58: (req) => { + if (req.args.length !== 1) { + return error('string_from_b58 accepts only one string argument'); + } else { + return success(new TextDecoder().decode(decode(req.args[0]))); + } + }, + + bytes_to_b58: (req) => { + if (req.args.length !== 1 || !Array.isArray(req.args[0])) { + return error('bytes_to_b58 accepts only single argument: array of numbers'); + } else { + const argumentArray = req.args[0] as number[]; + return success(encode(new Uint8Array(argumentArray))); + } + }, + + bytes_from_b58: (req) => { + if (req.args.length !== 1) { + return error('bytes_from_b58 accepts only one string argument'); + } else { + return success(Array.from(decode(req.args[0]))); + } + }, + }, + + peer: { + timeout: (req) => { + if (req.args.length !== 2) { + return error('timeout accepts exactly two arguments: timeout duration in ms and a message string'); + } + const durationMs = req.args[0]; + const message = req.args[1]; + + return new Promise((resolve) => { + setTimeout(() => { + const res = success(message); + resolve(res); + }, durationMs); + }); + }, + + identify: (req) => { + return error('The JS implementation of Peer does not support identify'); + }, + }, + + security: { + sign: async (req) => { + const [data] = req.args; + const signedData = await context.peerKeyPair.signBytes(data); + return success(signedData); + }, + verify: async (req) => { + const [data, signature] = req.args; + const result = await context.peerKeyPair.verify(data, signature); + return success(result); + }, + }, + }; +} diff --git a/src/internal/defaultServices.ts b/src/internal/defaultServices.ts deleted file mode 100644 index b33c45a35..000000000 --- a/src/internal/defaultServices.ts +++ /dev/null @@ -1,121 +0,0 @@ -/* - * Copyright 2021 Fluence Labs Limited - * - * 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. - */ - -import { CallServiceResult } from '@fluencelabs/avm'; -import { encode, decode } from 'bs58'; -import { GenericCallServiceHandler, ResultCodes } from './commonTypes'; - -const success = (result: any): CallServiceResult => { - return { - result: result, - retCode: ResultCodes.success, - }; -}; - -const error = (error: string): CallServiceResult => { - return { - result: error, - retCode: ResultCodes.unknownError, - }; -}; - -export const defaultServices: { [serviceId in string]: { [fnName in string]: GenericCallServiceHandler } } = { - op: { - noop: (req) => { - return success({}); - }, - - array: (req) => { - return success(req.args); - }, - - identity: (req) => { - if (req.args.length > 1) { - return error(`identity accepts up to 1 arguments, received ${req.args.length} arguments`); - } else { - return success(req.args.length === 0 ? {} : req.args[0]); - } - }, - - concat: (req) => { - const incorrectArgIndices = req.args // - .map((x, i) => [Array.isArray(x), i]) - .filter(([isArray, _]) => !isArray) - .map(([_, index]) => index); - - if (incorrectArgIndices.length > 0) { - const str = incorrectArgIndices.join(', '); - return error(`All arguments of 'concat' must be arrays: arguments ${str} are not`); - } else { - return success([].concat.apply([], req.args)); - } - }, - - string_to_b58: (req) => { - if (req.args.length !== 1) { - return error('string_to_b58 accepts only one string argument'); - } else { - return success(encode(new TextEncoder().encode(req.args[0]))); - } - }, - - string_from_b58: (req) => { - if (req.args.length !== 1) { - return error('string_from_b58 accepts only one string argument'); - } else { - return success(new TextDecoder().decode(decode(req.args[0]))); - } - }, - - bytes_to_b58: (req) => { - if (req.args.length !== 1 || !Array.isArray(req.args[0])) { - return error('bytes_to_b58 accepts only single argument: array of numbers'); - } else { - const argumentArray = req.args[0] as number[]; - return success(encode(new Uint8Array(argumentArray))); - } - }, - - bytes_from_b58: (req) => { - if (req.args.length !== 1) { - return error('bytes_from_b58 accepts only one string argument'); - } else { - return success(Array.from(decode(req.args[0]))); - } - }, - }, - - peer: { - timeout: (req) => { - if (req.args.length !== 2) { - return error('timeout accepts exactly two arguments: timeout duration in ms and a message string'); - } - const durationMs = req.args[0]; - const message = req.args[1]; - - return new Promise((resolve) => { - setTimeout(() => { - const res = success(message); - resolve(res); - }, durationMs); - }); - }, - - identify: (req) => { - return error('The JS implementation of Peer does not support identify'); - }, - }, -}; From a9beeca4f84eca6ee50934bf4b735348bb51ee22 Mon Sep 17 00:00:00 2001 From: Pavel Murygin Date: Mon, 6 Dec 2021 18:27:22 +0300 Subject: [PATCH 02/13] Add tests for signing service --- package-lock.json | 12 +++- package.json | 2 +- src/__test__/unit/builtInHandler.spec.ts | 86 +++++++++++++++--------- src/internal/builtInServices.ts | 6 +- 4 files changed, 68 insertions(+), 38 deletions(-) diff --git a/package-lock.json b/package-lock.json index 3e2456137..93dab17d5 100644 --- a/package-lock.json +++ b/package-lock.json @@ -12,11 +12,11 @@ "@chainsafe/libp2p-noise": "4.0.0", "@fluencelabs/avm": "0.16.0-restriction-operator.9", "async": "3.2.0", - "base64-js": "1.5.1", "bs58": "4.0.1", "cids": "0.8.1", "it-length-prefixed": "3.0.1", "it-pipe": "1.1.0", + "js-base64": "^3.7.2", "libp2p": "0.32.3", "libp2p-crypto": "0.19.7", "libp2p-mplex": "0.10.4", @@ -4668,6 +4668,11 @@ "node": ">= 10.14.2" } }, + "node_modules/js-base64": { + "version": "3.7.2", + "resolved": "https://registry.npmjs.org/js-base64/-/js-base64-3.7.2.tgz", + "integrity": "sha512-NnRs6dsyqUXejqk/yv2aiXlAvOs56sLkX6nUdeaNezI5LFFLlsZjOThmwnrcwh5ZZRwZlCMnVAY3CvhIhoVEKQ==" + }, "node_modules/js-tokens": { "version": "4.0.0", "resolved": "https://registry.npmjs.org/js-tokens/-/js-tokens-4.0.0.tgz", @@ -11936,6 +11941,11 @@ "supports-color": "^7.0.0" } }, + "js-base64": { + "version": "3.7.2", + "resolved": "https://registry.npmjs.org/js-base64/-/js-base64-3.7.2.tgz", + "integrity": "sha512-NnRs6dsyqUXejqk/yv2aiXlAvOs56sLkX6nUdeaNezI5LFFLlsZjOThmwnrcwh5ZZRwZlCMnVAY3CvhIhoVEKQ==" + }, "js-tokens": { "version": "4.0.0", "resolved": "https://registry.npmjs.org/js-tokens/-/js-tokens-4.0.0.tgz", diff --git a/package.json b/package.json index df976bf14..83efe7bdf 100644 --- a/package.json +++ b/package.json @@ -23,11 +23,11 @@ "@chainsafe/libp2p-noise": "4.0.0", "@fluencelabs/avm": "0.16.0-restriction-operator.9", "async": "3.2.0", - "base64-js": "1.5.1", "bs58": "4.0.1", "cids": "0.8.1", "it-length-prefixed": "3.0.1", "it-pipe": "1.1.0", + "js-base64": "^3.7.2", "libp2p": "0.32.3", "libp2p-crypto": "0.19.7", "libp2p-mplex": "0.10.4", diff --git a/src/__test__/unit/builtInHandler.spec.ts b/src/__test__/unit/builtInHandler.spec.ts index f8f96d813..bd44be467 100644 --- a/src/__test__/unit/builtInHandler.spec.ts +++ b/src/__test__/unit/builtInHandler.spec.ts @@ -2,48 +2,68 @@ import each from 'jest-each'; import { CallServiceData } from '../../internal/commonTypes'; import { BuiltInServiceContext, builtInServices } from '../../internal/builtInServices'; import { KeyPair } from '../../internal/KeyPair'; +import { toUint8Array } from 'js-base64'; + +const key = '+cmeYlZKj+MfSa9dpHV+BmLPm6wq4inGlsPlQ1GvtPk='; const context = (async () => { + const keyBytes = toUint8Array(key); const res: BuiltInServiceContext = { - peerKeyPair: await KeyPair.randomEd25519(), + peerKeyPair: await KeyPair.fromEd25519SK(keyBytes), }; return res; })(); +const testData = [1, 2, 3, 4, 5, 6, 7, 9, 10]; +const testDataSig = [ + 224, 104, 245, 206, 140, 248, 27, 72, 68, 133, 111, 10, 164, 197, 242, 132, 107, 77, 224, 67, 99, 106, 76, 29, 144, + 121, 122, 169, 36, 173, 58, 80, 170, 102, 137, 253, 157, 247, 168, 87, 162, 223, 188, 214, 203, 220, 52, 246, 29, + 86, 77, 71, 224, 248, 16, 213, 254, 75, 78, 239, 243, 222, 241, 15, +]; + +const testDataWrongSig = [ + 116, 247, 189, 118, 236, 53, 147, 123, 219, 75, 176, 105, 101, 108, 233, 137, 97, 14, 146, 132, 252, 70, 51, 153, + 237, 167, 156, 150, 36, 90, 229, 108, 166, 231, 255, 137, 8, 246, 125, 0, 213, 150, 83, 196, 237, 221, 131, 159, + 157, 159, 25, 109, 95, 160, 181, 65, 254, 238, 47, 156, 240, 151, 58, 14, +]; + describe('Tests for default handler', () => { // prettier-ignore each` - serviceId | fnName | args | retCode | result - ${'op'} | ${'identity'} | ${[]} | ${0} | ${{}} - ${'op'} | ${'identity'} | ${[1]} | ${0} | ${1} - ${'op'} | ${'identity'} | ${[1, 2]} | ${1} | ${'identity accepts up to 1 arguments, received 2 arguments'} - - ${'op'} | ${'noop'} | ${[1, 2]} | ${0} | ${{}} - - ${'op'} | ${'array'} | ${[1, 2, 3]} | ${0} | ${[1, 2, 3]} - - ${'op'} | ${'concat'} | ${[[1, 2], [3, 4], [5, 6]]} | ${0} | ${[1, 2, 3, 4, 5, 6]} - ${'op'} | ${'concat'} | ${[[1, 2]]} | ${0} | ${[1, 2]} - ${'op'} | ${'concat'} | ${[]} | ${0} | ${[]} - ${'op'} | ${'concat'} | ${[1, [1, 2], 1]} | ${1} | ${"All arguments of 'concat' must be arrays: arguments 0, 2 are not"} - - ${'op'} | ${'string_to_b58'} | ${["test"]} | ${0} | ${"3yZe7d"} - ${'op'} | ${'string_to_b58'} | ${["test", 1]} | ${1} | ${"string_to_b58 accepts only one string argument"} - - ${'op'} | ${'string_from_b58'} | ${["3yZe7d"]} | ${0} | ${"test"} - ${'op'} | ${'string_from_b58'} | ${["3yZe7d", 1]} | ${1} | ${"string_from_b58 accepts only one string argument"} - - ${'op'} | ${'bytes_to_b58'} | ${[[116, 101, 115, 116]]} | ${0} | ${"3yZe7d"} - ${'op'} | ${'bytes_to_b58'} | ${[[116, 101, 115, 116], 1]} | ${1} | ${"bytes_to_b58 accepts only single argument: array of numbers"} - - ${'op'} | ${'bytes_from_b58'} | ${["3yZe7d"]} | ${0} | ${[116, 101, 115, 116]} - ${'op'} | ${'bytes_from_b58'} | ${["3yZe7d", 1]} | ${1} | ${"bytes_from_b58 accepts only one string argument"} - - ${'peer'} | ${'timeout'} | ${[200, []]} | ${0} | ${[]}} - ${'peer'} | ${'timeout'} | ${[200, ['test']]} | ${0} | ${['test']}} - ${'peer'} | ${'timeout'} | ${[]} | ${1} | ${'timeout accepts exactly two arguments: timeout duration in ms and a message string'}} - ${'peer'} | ${'timeout'} | ${[200, 'test', 1]} | ${1} | ${'timeout accepts exactly two arguments: timeout duration in ms and a message string'}} - + serviceId | fnName | args | retCode | result + ${'op'} | ${'identity'} | ${[]} | ${0} | ${{}} + ${'op'} | ${'identity'} | ${[1]} | ${0} | ${1} + ${'op'} | ${'identity'} | ${[1, 2]} | ${1} | ${'identity accepts up to 1 arguments, received 2 arguments'} + + ${'op'} | ${'noop'} | ${[1, 2]} | ${0} | ${{}} + + ${'op'} | ${'array'} | ${[1, 2, 3]} | ${0} | ${[1, 2, 3]} + + ${'op'} | ${'concat'} | ${[[1, 2], [3, 4], [5, 6]]} | ${0} | ${[1, 2, 3, 4, 5, 6]} + ${'op'} | ${'concat'} | ${[[1, 2]]} | ${0} | ${[1, 2]} + ${'op'} | ${'concat'} | ${[]} | ${0} | ${[]} + ${'op'} | ${'concat'} | ${[1, [1, 2], 1]} | ${1} | ${"All arguments of 'concat' must be arrays: arguments 0, 2 are not"} + + ${'op'} | ${'string_to_b58'} | ${["test"]} | ${0} | ${"3yZe7d"} + ${'op'} | ${'string_to_b58'} | ${["test", 1]} | ${1} | ${"string_to_b58 accepts only one string argument"} + + ${'op'} | ${'string_from_b58'} | ${["3yZe7d"]} | ${0} | ${"test"} + ${'op'} | ${'string_from_b58'} | ${["3yZe7d", 1]} | ${1} | ${"string_from_b58 accepts only one string argument"} + + ${'op'} | ${'bytes_to_b58'} | ${[[116, 101, 115, 116]]} | ${0} | ${"3yZe7d"} + ${'op'} | ${'bytes_to_b58'} | ${[[116, 101, 115, 116], 1]} | ${1} | ${"bytes_to_b58 accepts only single argument: array of numbers"} + + ${'op'} | ${'bytes_from_b58'} | ${["3yZe7d"]} | ${0} | ${[116, 101, 115, 116]} + ${'op'} | ${'bytes_from_b58'} | ${["3yZe7d", 1]} | ${1} | ${"bytes_from_b58 accepts only one string argument"} + + ${'peer'} | ${'timeout'} | ${[200, []]} | ${0} | ${[]}} + ${'peer'} | ${'timeout'} | ${[200, ['test']]} | ${0} | ${['test']}} + ${'peer'} | ${'timeout'} | ${[]} | ${1} | ${'timeout accepts exactly two arguments: timeout duration in ms and a message string'}} + ${'peer'} | ${'timeout'} | ${[200, 'test', 1]} | ${1} | ${'timeout accepts exactly two arguments: timeout duration in ms and a message string'}} + + ${'security'} | ${'sign'} | ${[testData]} | ${0} | ${testDataSig}} + ${'security'} | ${'verify'} | ${[testData, testDataSig]} | ${0} | ${true}} + ${'security'} | ${'verify'} | ${[testData, testDataWrongSig]} | ${0} | ${false}} `.test( // '$fnName with $args expected retcode: $retCode and result: $result', @@ -101,4 +121,4 @@ describe('Tests for default handler', () => { result: 'The JS implementation of Peer does not support identify', }); }); -}); +}); \ No newline at end of file diff --git a/src/internal/builtInServices.ts b/src/internal/builtInServices.ts index bdf56c078..fe2e9b04a 100644 --- a/src/internal/builtInServices.ts +++ b/src/internal/builtInServices.ts @@ -130,12 +130,12 @@ export function builtInServices(context: BuiltInServiceContext): { security: { sign: async (req) => { const [data] = req.args; - const signedData = await context.peerKeyPair.signBytes(data); - return success(signedData); + const signedData = await context.peerKeyPair.signBytes(Uint8Array.from(data)); + return success(Array.from(signedData)); }, verify: async (req) => { const [data, signature] = req.args; - const result = await context.peerKeyPair.verify(data, signature); + const result = await context.peerKeyPair.verify(Uint8Array.from(data), Uint8Array.from(signature)); return success(result); }, }, From f91062dacc1ea0baaa59a1d95dc04c43347c6912 Mon Sep 17 00:00:00 2001 From: Pavel Murygin Date: Mon, 6 Dec 2021 18:36:23 +0300 Subject: [PATCH 03/13] Handle negative cases --- src/__test__/unit/builtInHandler.spec.ts | 2 ++ src/internal/builtInServices.ts | 8 ++++++++ 2 files changed, 10 insertions(+) diff --git a/src/__test__/unit/builtInHandler.spec.ts b/src/__test__/unit/builtInHandler.spec.ts index bd44be467..e7a610269 100644 --- a/src/__test__/unit/builtInHandler.spec.ts +++ b/src/__test__/unit/builtInHandler.spec.ts @@ -64,6 +64,8 @@ describe('Tests for default handler', () => { ${'security'} | ${'sign'} | ${[testData]} | ${0} | ${testDataSig}} ${'security'} | ${'verify'} | ${[testData, testDataSig]} | ${0} | ${true}} ${'security'} | ${'verify'} | ${[testData, testDataWrongSig]} | ${0} | ${false}} + ${'security'} | ${'sign'} | ${[]} | ${1} | ${'sign accepts exactly one argument: data be signed in format of u8 array of bytes'}} + ${'security'} | ${'verify'} | ${[testData]} | ${1} | ${'verify accepts exactly two arguments: data and signature, both in format of u8 array of bytes'}} `.test( // '$fnName with $args expected retcode: $retCode and result: $result', diff --git a/src/internal/builtInServices.ts b/src/internal/builtInServices.ts index fe2e9b04a..38eb73055 100644 --- a/src/internal/builtInServices.ts +++ b/src/internal/builtInServices.ts @@ -129,11 +129,19 @@ export function builtInServices(context: BuiltInServiceContext): { security: { sign: async (req) => { + if (req.args.length !== 1) { + return error('sign accepts exactly one argument: data be signed in format of u8 array of bytes'); + } const [data] = req.args; const signedData = await context.peerKeyPair.signBytes(Uint8Array.from(data)); return success(Array.from(signedData)); }, verify: async (req) => { + if (req.args.length !== 2) { + return error( + 'verify accepts exactly two arguments: data and signature, both in format of u8 array of bytes', + ); + } const [data, signature] = req.args; const result = await context.peerKeyPair.verify(Uint8Array.from(data), Uint8Array.from(signature)); return success(result); From f4b49477269a6e1cd9bade0e6b7ab8ae02bcd836 Mon Sep 17 00:00:00 2001 From: Pavel Murygin Date: Mon, 6 Dec 2021 18:45:39 +0300 Subject: [PATCH 04/13] js-base64 to dev deps --- package-lock.json | 8 +++++--- package.json | 4 ++-- 2 files changed, 7 insertions(+), 5 deletions(-) diff --git a/package-lock.json b/package-lock.json index 93dab17d5..bef0bbdf7 100644 --- a/package-lock.json +++ b/package-lock.json @@ -16,7 +16,6 @@ "cids": "0.8.1", "it-length-prefixed": "3.0.1", "it-pipe": "1.1.0", - "js-base64": "^3.7.2", "libp2p": "0.32.3", "libp2p-crypto": "0.19.7", "libp2p-mplex": "0.10.4", @@ -31,6 +30,7 @@ "devDependencies": { "@types/jest": "^26.0.22", "jest": "^26.6.3", + "js-base64": "^3.7.2", "ts-jest": "^26.5.4", "typedoc": "^0.21.9", "typescript": "^4.0.0" @@ -4671,7 +4671,8 @@ "node_modules/js-base64": { "version": "3.7.2", "resolved": "https://registry.npmjs.org/js-base64/-/js-base64-3.7.2.tgz", - "integrity": "sha512-NnRs6dsyqUXejqk/yv2aiXlAvOs56sLkX6nUdeaNezI5LFFLlsZjOThmwnrcwh5ZZRwZlCMnVAY3CvhIhoVEKQ==" + "integrity": "sha512-NnRs6dsyqUXejqk/yv2aiXlAvOs56sLkX6nUdeaNezI5LFFLlsZjOThmwnrcwh5ZZRwZlCMnVAY3CvhIhoVEKQ==", + "dev": true }, "node_modules/js-tokens": { "version": "4.0.0", @@ -11944,7 +11945,8 @@ "js-base64": { "version": "3.7.2", "resolved": "https://registry.npmjs.org/js-base64/-/js-base64-3.7.2.tgz", - "integrity": "sha512-NnRs6dsyqUXejqk/yv2aiXlAvOs56sLkX6nUdeaNezI5LFFLlsZjOThmwnrcwh5ZZRwZlCMnVAY3CvhIhoVEKQ==" + "integrity": "sha512-NnRs6dsyqUXejqk/yv2aiXlAvOs56sLkX6nUdeaNezI5LFFLlsZjOThmwnrcwh5ZZRwZlCMnVAY3CvhIhoVEKQ==", + "dev": true }, "js-tokens": { "version": "4.0.0", diff --git a/package.json b/package.json index 83efe7bdf..7951b9cbb 100644 --- a/package.json +++ b/package.json @@ -27,7 +27,6 @@ "cids": "0.8.1", "it-length-prefixed": "3.0.1", "it-pipe": "1.1.0", - "js-base64": "^3.7.2", "libp2p": "0.32.3", "libp2p-crypto": "0.19.7", "libp2p-mplex": "0.10.4", @@ -44,6 +43,7 @@ "jest": "^26.6.3", "ts-jest": "^26.5.4", "typedoc": "^0.21.9", - "typescript": "^4.0.0" + "typescript": "^4.0.0", + "js-base64": "^3.7.2" } } From 164d38b444a88401d567e636fc7ec60b775ce873 Mon Sep 17 00:00:00 2001 From: Pavel Date: Mon, 6 Dec 2021 18:55:12 +0300 Subject: [PATCH 05/13] Update src/__test__/unit/builtInHandler.spec.ts Co-authored-by: Aleksey Proshutisnkiy --- src/__test__/unit/builtInHandler.spec.ts | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/__test__/unit/builtInHandler.spec.ts b/src/__test__/unit/builtInHandler.spec.ts index e7a610269..cc079ba11 100644 --- a/src/__test__/unit/builtInHandler.spec.ts +++ b/src/__test__/unit/builtInHandler.spec.ts @@ -123,4 +123,4 @@ describe('Tests for default handler', () => { result: 'The JS implementation of Peer does not support identify', }); }); -}); \ No newline at end of file +}); From 20ed85ae647f88c6e510b39c091ac2657678501e Mon Sep 17 00:00:00 2001 From: Pavel Murygin Date: Tue, 7 Dec 2021 16:48:25 +0300 Subject: [PATCH 06/13] Checking peer id and tetraplets --- src/__test__/unit/builtInHandler.spec.ts | 44 ++++++++++++++++++++++-- src/internal/FluencePeer.ts | 5 ++- src/internal/builtInServices.ts | 24 +++++++++++++ 3 files changed, 69 insertions(+), 4 deletions(-) diff --git a/src/__test__/unit/builtInHandler.spec.ts b/src/__test__/unit/builtInHandler.spec.ts index e7a610269..f203e6173 100644 --- a/src/__test__/unit/builtInHandler.spec.ts +++ b/src/__test__/unit/builtInHandler.spec.ts @@ -8,8 +8,10 @@ const key = '+cmeYlZKj+MfSa9dpHV+BmLPm6wq4inGlsPlQ1GvtPk='; const context = (async () => { const keyBytes = toUint8Array(key); + const kp = await KeyPair.fromEd25519SK(keyBytes); const res: BuiltInServiceContext = { - peerKeyPair: await KeyPair.fromEd25519SK(keyBytes), + peerKeyPair: kp, + peerId: kp.Libp2pPeerId.toB58String(), }; return res; })(); @@ -61,7 +63,6 @@ describe('Tests for default handler', () => { ${'peer'} | ${'timeout'} | ${[]} | ${1} | ${'timeout accepts exactly two arguments: timeout duration in ms and a message string'}} ${'peer'} | ${'timeout'} | ${[200, 'test', 1]} | ${1} | ${'timeout accepts exactly two arguments: timeout duration in ms and a message string'}} - ${'security'} | ${'sign'} | ${[testData]} | ${0} | ${testDataSig}} ${'security'} | ${'verify'} | ${[testData, testDataSig]} | ${0} | ${true}} ${'security'} | ${'verify'} | ${[testData, testDataWrongSig]} | ${0} | ${false}} ${'security'} | ${'sign'} | ${[]} | ${1} | ${'sign accepts exactly one argument: data be signed in format of u8 array of bytes'}} @@ -123,4 +124,41 @@ describe('Tests for default handler', () => { result: 'The JS implementation of Peer does not support identify', }); }); -}); \ No newline at end of file + + it('sign should work', async () => { + // arrange + const ctx = await context; + const req: CallServiceData = { + serviceId: 'security', + fnName: 'sign', + args: [testData], + tetraplets: [ + [ + { + function_name: 'get_trust_bytes', + json_path: '', + peer_pk: '', + service_id: 'TrustGraph', + }, + ], + ], + particleContext: { + particleId: 'some', + initPeerId: ctx.peerId, + timestamp: 595951200, + ttl: 595961200, + signature: 'sig', + }, + }; + + // act + const fn = builtInServices(ctx)[req.serviceId][req.fnName]; + const res = await fn(req); + + // assert + expect(res).toMatchObject({ + retCode: 0, + result: testDataSig, + }); + }); +}); diff --git a/src/internal/FluencePeer.ts b/src/internal/FluencePeer.ts index 23a75edbc..d60ad74f6 100644 --- a/src/internal/FluencePeer.ts +++ b/src/internal/FluencePeer.ts @@ -210,7 +210,10 @@ export class FluencePeer { } this._legacyCallServiceHandler = new LegacyCallServiceHandler(); - registerDefaultServices(this, { peerKeyPair: this._keyPair }); + registerDefaultServices(this, { + peerKeyPair: this._keyPair, + peerId: this.getStatus().peerId, + }); this._startParticleProcessing(); } diff --git a/src/internal/builtInServices.ts b/src/internal/builtInServices.ts index 38eb73055..5642a645a 100644 --- a/src/internal/builtInServices.ts +++ b/src/internal/builtInServices.ts @@ -16,6 +16,7 @@ import { CallServiceResult } from '@fluencelabs/avm'; import { encode, decode } from 'bs58'; +import { PeerIdB58 } from 'src'; import { GenericCallServiceHandler, ResultCodes } from './commonTypes'; import { KeyPair } from './KeyPair'; @@ -35,6 +36,7 @@ const error = (error: string): CallServiceResult => { export interface BuiltInServiceContext { peerKeyPair: KeyPair; + peerId: PeerIdB58; } export function builtInServices(context: BuiltInServiceContext): { @@ -132,10 +134,32 @@ export function builtInServices(context: BuiltInServiceContext): { if (req.args.length !== 1) { return error('sign accepts exactly one argument: data be signed in format of u8 array of bytes'); } + + if (req.particleContext.initPeerId !== context.peerId) { + return error('sign is only allowed to be called on the same peer the particle was initiated from'); + } + + const t = req.tetraplets[0][0]; + const serviceFnPair = `${t.service_id}.${t.function_name}`; + + const allowedServices = [ + 'TrustGraph.get_trust_bytes', + 'TrustGraph.get_revocation_bytes', + 'Registry.get_key_bytes', + 'Registry.get_record_bytes', + ]; + + if (allowedServices.indexOf(serviceFnPair) === -1) { + return error( + 'Only data from the following services is allowed to be signed: ' + allowedServices.join(', '), + ); + } + const [data] = req.args; const signedData = await context.peerKeyPair.signBytes(Uint8Array.from(data)); return success(Array.from(signedData)); }, + verify: async (req) => { if (req.args.length !== 2) { return error( From f2d9f716631a8ce802bb91d133fbd2a17f5a4963 Mon Sep 17 00:00:00 2001 From: Pavel Murygin Date: Wed, 8 Dec 2021 16:10:22 +0300 Subject: [PATCH 07/13] Fix trust graph and registry names --- src/__test__/unit/builtInHandler.spec.ts | 2 +- src/internal/builtInServices.ts | 8 ++++---- 2 files changed, 5 insertions(+), 5 deletions(-) diff --git a/src/__test__/unit/builtInHandler.spec.ts b/src/__test__/unit/builtInHandler.spec.ts index f203e6173..e96c7f771 100644 --- a/src/__test__/unit/builtInHandler.spec.ts +++ b/src/__test__/unit/builtInHandler.spec.ts @@ -138,7 +138,7 @@ describe('Tests for default handler', () => { function_name: 'get_trust_bytes', json_path: '', peer_pk: '', - service_id: 'TrustGraph', + service_id: 'trust-graph', }, ], ], diff --git a/src/internal/builtInServices.ts b/src/internal/builtInServices.ts index 5642a645a..2cac09f42 100644 --- a/src/internal/builtInServices.ts +++ b/src/internal/builtInServices.ts @@ -143,10 +143,10 @@ export function builtInServices(context: BuiltInServiceContext): { const serviceFnPair = `${t.service_id}.${t.function_name}`; const allowedServices = [ - 'TrustGraph.get_trust_bytes', - 'TrustGraph.get_revocation_bytes', - 'Registry.get_key_bytes', - 'Registry.get_record_bytes', + 'trust-graph.get_trust_bytes', + 'trust-graph.get_revocation_bytes', + 'registry.get_key_bytes', + 'registry.get_record_bytes', ]; if (allowedServices.indexOf(serviceFnPair) === -1) { From 87a347142ed165761a8f3e5fdb3f083c522aa58c Mon Sep 17 00:00:00 2001 From: Pavel Murygin Date: Wed, 8 Dec 2021 16:13:42 +0300 Subject: [PATCH 08/13] Impove comments in builtin handler test --- src/__test__/unit/builtInHandler.spec.ts | 3 +++ 1 file changed, 3 insertions(+) diff --git a/src/__test__/unit/builtInHandler.spec.ts b/src/__test__/unit/builtInHandler.spec.ts index e96c7f771..7dd84a9ee 100644 --- a/src/__test__/unit/builtInHandler.spec.ts +++ b/src/__test__/unit/builtInHandler.spec.ts @@ -17,12 +17,15 @@ const context = (async () => { })(); const testData = [1, 2, 3, 4, 5, 6, 7, 9, 10]; + +// signature produced by KeyPair created from key above (`key` variable) const testDataSig = [ 224, 104, 245, 206, 140, 248, 27, 72, 68, 133, 111, 10, 164, 197, 242, 132, 107, 77, 224, 67, 99, 106, 76, 29, 144, 121, 122, 169, 36, 173, 58, 80, 170, 102, 137, 253, 157, 247, 168, 87, 162, 223, 188, 214, 203, 220, 52, 246, 29, 86, 77, 71, 224, 248, 16, 213, 254, 75, 78, 239, 243, 222, 241, 15, ]; +// signature produced by KeyPair created from some random KeyPair const testDataWrongSig = [ 116, 247, 189, 118, 236, 53, 147, 123, 219, 75, 176, 105, 101, 108, 233, 137, 97, 14, 146, 132, 252, 70, 51, 153, 237, 167, 156, 150, 36, 90, 229, 108, 166, 231, 255, 137, 8, 246, 125, 0, 213, 150, 83, 196, 237, 221, 131, 159, From efc887cc96dd658245fd9dc903b1a00a25b4ceda Mon Sep 17 00:00:00 2001 From: Pavel Murygin Date: Thu, 9 Dec 2021 11:52:12 +0300 Subject: [PATCH 09/13] rename security to signer --- src/__test__/unit/builtInHandler.spec.ts | 10 +++++----- src/internal/builtInServices.ts | 2 +- 2 files changed, 6 insertions(+), 6 deletions(-) diff --git a/src/__test__/unit/builtInHandler.spec.ts b/src/__test__/unit/builtInHandler.spec.ts index 7dd84a9ee..2cf70534a 100644 --- a/src/__test__/unit/builtInHandler.spec.ts +++ b/src/__test__/unit/builtInHandler.spec.ts @@ -66,10 +66,10 @@ describe('Tests for default handler', () => { ${'peer'} | ${'timeout'} | ${[]} | ${1} | ${'timeout accepts exactly two arguments: timeout duration in ms and a message string'}} ${'peer'} | ${'timeout'} | ${[200, 'test', 1]} | ${1} | ${'timeout accepts exactly two arguments: timeout duration in ms and a message string'}} - ${'security'} | ${'verify'} | ${[testData, testDataSig]} | ${0} | ${true}} - ${'security'} | ${'verify'} | ${[testData, testDataWrongSig]} | ${0} | ${false}} - ${'security'} | ${'sign'} | ${[]} | ${1} | ${'sign accepts exactly one argument: data be signed in format of u8 array of bytes'}} - ${'security'} | ${'verify'} | ${[testData]} | ${1} | ${'verify accepts exactly two arguments: data and signature, both in format of u8 array of bytes'}} + ${'signer'} | ${'verify'} | ${[testData, testDataSig]} | ${0} | ${true}} + ${'signer'} | ${'verify'} | ${[testData, testDataWrongSig]} | ${0} | ${false}} + ${'signer'} | ${'sign'} | ${[]} | ${1} | ${'sign accepts exactly one argument: data be signed in format of u8 array of bytes'}} + ${'signer'} | ${'verify'} | ${[testData]} | ${1} | ${'verify accepts exactly two arguments: data and signature, both in format of u8 array of bytes'}} `.test( // '$fnName with $args expected retcode: $retCode and result: $result', @@ -132,7 +132,7 @@ describe('Tests for default handler', () => { // arrange const ctx = await context; const req: CallServiceData = { - serviceId: 'security', + serviceId: 'signer', fnName: 'sign', args: [testData], tetraplets: [ diff --git a/src/internal/builtInServices.ts b/src/internal/builtInServices.ts index 2cac09f42..dc506c10a 100644 --- a/src/internal/builtInServices.ts +++ b/src/internal/builtInServices.ts @@ -129,7 +129,7 @@ export function builtInServices(context: BuiltInServiceContext): { }, }, - security: { + signer: { sign: async (req) => { if (req.args.length !== 1) { return error('sign accepts exactly one argument: data be signed in format of u8 array of bytes'); From 6cd28fa6ad4cc24144c077a56724ec829d58d73a Mon Sep 17 00:00:00 2001 From: Pavel Murygin Date: Thu, 9 Dec 2021 13:34:21 +0300 Subject: [PATCH 10/13] finally rename signer to sig --- src/__test__/unit/builtInHandler.spec.ts | 10 +++++----- src/internal/builtInServices.ts | 2 +- 2 files changed, 6 insertions(+), 6 deletions(-) diff --git a/src/__test__/unit/builtInHandler.spec.ts b/src/__test__/unit/builtInHandler.spec.ts index 2cf70534a..5c278ca93 100644 --- a/src/__test__/unit/builtInHandler.spec.ts +++ b/src/__test__/unit/builtInHandler.spec.ts @@ -66,10 +66,10 @@ describe('Tests for default handler', () => { ${'peer'} | ${'timeout'} | ${[]} | ${1} | ${'timeout accepts exactly two arguments: timeout duration in ms and a message string'}} ${'peer'} | ${'timeout'} | ${[200, 'test', 1]} | ${1} | ${'timeout accepts exactly two arguments: timeout duration in ms and a message string'}} - ${'signer'} | ${'verify'} | ${[testData, testDataSig]} | ${0} | ${true}} - ${'signer'} | ${'verify'} | ${[testData, testDataWrongSig]} | ${0} | ${false}} - ${'signer'} | ${'sign'} | ${[]} | ${1} | ${'sign accepts exactly one argument: data be signed in format of u8 array of bytes'}} - ${'signer'} | ${'verify'} | ${[testData]} | ${1} | ${'verify accepts exactly two arguments: data and signature, both in format of u8 array of bytes'}} + ${'sig'} | ${'verify'} | ${[testData, testDataSig]} | ${0} | ${true}} + ${'sig'} | ${'verify'} | ${[testData, testDataWrongSig]} | ${0} | ${false}} + ${'sig'} | ${'sign'} | ${[]} | ${1} | ${'sign accepts exactly one argument: data be signed in format of u8 array of bytes'}} + ${'sig'} | ${'verify'} | ${[testData]} | ${1} | ${'verify accepts exactly two arguments: data and signature, both in format of u8 array of bytes'}} `.test( // '$fnName with $args expected retcode: $retCode and result: $result', @@ -132,7 +132,7 @@ describe('Tests for default handler', () => { // arrange const ctx = await context; const req: CallServiceData = { - serviceId: 'signer', + serviceId: 'sig', fnName: 'sign', args: [testData], tetraplets: [ diff --git a/src/internal/builtInServices.ts b/src/internal/builtInServices.ts index dc506c10a..6ee7cfb67 100644 --- a/src/internal/builtInServices.ts +++ b/src/internal/builtInServices.ts @@ -129,7 +129,7 @@ export function builtInServices(context: BuiltInServiceContext): { }, }, - signer: { + sig: { sign: async (req) => { if (req.args.length !== 1) { return error('sign accepts exactly one argument: data be signed in format of u8 array of bytes'); From ddd7f41786f7d5e37fe3b3a3f533fc2703f88f23 Mon Sep 17 00:00:00 2001 From: Pavel Murygin Date: Thu, 9 Dec 2021 13:42:41 +0300 Subject: [PATCH 11/13] Additional unit tests for signing service --- src/__test__/unit/builtInHandler.spec.ts | 76 +++++++++++++++++++++++- 1 file changed, 75 insertions(+), 1 deletion(-) diff --git a/src/__test__/unit/builtInHandler.spec.ts b/src/__test__/unit/builtInHandler.spec.ts index 5c278ca93..4c020df5e 100644 --- a/src/__test__/unit/builtInHandler.spec.ts +++ b/src/__test__/unit/builtInHandler.spec.ts @@ -128,7 +128,7 @@ describe('Tests for default handler', () => { }); }); - it('sign should work', async () => { + it('sig.sign should create the correct signature', async () => { // arrange const ctx = await context; const req: CallServiceData = { @@ -164,4 +164,78 @@ describe('Tests for default handler', () => { result: testDataSig, }); }); + + it('sig.sign should not allow data from incorrect services', async () => { + // arrange + const ctx = await context; + const req: CallServiceData = { + serviceId: 'sig', + fnName: 'sign', + args: [testData], + tetraplets: [ + [ + { + function_name: 'some-other-fn', + json_path: '', + peer_pk: '', + service_id: 'cool-service', + }, + ], + ], + particleContext: { + particleId: 'some', + initPeerId: ctx.peerId, + timestamp: 595951200, + ttl: 595961200, + signature: 'sig', + }, + }; + + // act + const fn = builtInServices(ctx)[req.serviceId][req.fnName]; + const res = await fn(req); + + // assert + expect(res).toMatchObject({ + retCode: 1, + result: expect.stringContaining("Only data from the following services is allowed to be signed:"), + }); + }); + + it('sig.sign should not allow particles initiated from other peers', async () => { + // arrange + const ctx = await context; + const req: CallServiceData = { + serviceId: 'sig', + fnName: 'sign', + args: [testData], + tetraplets: [ + [ + { + function_name: 'some-other-fn', + json_path: '', + peer_pk: '', + service_id: 'cool-service', + }, + ], + ], + particleContext: { + particleId: 'some', + initPeerId: (await KeyPair.randomEd25519()).Libp2pPeerId.toB58String(), + timestamp: 595951200, + ttl: 595961200, + signature: 'sig', + }, + }; + + // act + const fn = builtInServices(ctx)[req.serviceId][req.fnName]; + const res = await fn(req); + + // assert + expect(res).toMatchObject({ + retCode: 1, + result: 'sign is only allowed to be called on the same peer the particle was initiated from', + }); + }); }); From e0e0747433216e326b40f0dd35507f8299dec52c Mon Sep 17 00:00:00 2001 From: Pavel Murygin Date: Thu, 9 Dec 2021 14:14:37 +0300 Subject: [PATCH 12/13] moar tests --- src/__test__/unit/builtInHandler.spec.ts | 53 +++++++++++++++++++++++- 1 file changed, 52 insertions(+), 1 deletion(-) diff --git a/src/__test__/unit/builtInHandler.spec.ts b/src/__test__/unit/builtInHandler.spec.ts index 4c020df5e..a8fe5ce13 100644 --- a/src/__test__/unit/builtInHandler.spec.ts +++ b/src/__test__/unit/builtInHandler.spec.ts @@ -1,5 +1,5 @@ -import each from 'jest-each'; import { CallServiceData } from '../../internal/commonTypes'; +import each from 'jest-each'; import { BuiltInServiceContext, builtInServices } from '../../internal/builtInServices'; import { KeyPair } from '../../internal/KeyPair'; import { toUint8Array } from 'js-base64'; @@ -165,6 +165,57 @@ describe('Tests for default handler', () => { }); }); + it('sign-verify call chain should work', async () => { + const ctx = await context; + const signReq: CallServiceData = { + serviceId: 'sig', + fnName: 'sign', + args: [testData], + tetraplets: [ + [ + { + function_name: 'get_trust_bytes', + json_path: '', + peer_pk: '', + service_id: 'trust-graph', + }, + ], + ], + particleContext: { + particleId: 'some', + initPeerId: ctx.peerId, + timestamp: 595951200, + ttl: 595961200, + signature: 'sig', + }, + }; + + const signFn = builtInServices(ctx)[signReq.serviceId][signReq.fnName]; + const signRes = await signFn(signReq); + + const verifyReq: CallServiceData = { + serviceId: 'sig', + fnName: 'verify', + args: [testData, signRes.result], + tetraplets: [], + particleContext: { + particleId: 'some', + initPeerId: ctx.peerId, + timestamp: 595951200, + ttl: 595961200, + signature: 'sig', + }, + }; + + const verifyFn = builtInServices(ctx)[verifyReq.serviceId][verifyReq.fnName]; + const verifyRes = await verifyFn(verifyReq); + + expect(verifyRes).toMatchObject({ + retCode: 0, + result: true, + }); + }); + it('sig.sign should not allow data from incorrect services', async () => { // arrange const ctx = await context; From e1f1448b76aa76bdc33e49cc45450f6390d2d868 Mon Sep 17 00:00:00 2001 From: Pavel Murygin Date: Thu, 9 Dec 2021 19:04:00 +0300 Subject: [PATCH 13/13] unknownError => error --- src/internal/FluencePeer.ts | 4 ++-- src/internal/builtInServices.ts | 2 +- src/internal/commonTypes.ts | 3 +-- src/internal/compilerSupport/LegacyCallServiceHandler.ts | 2 +- 4 files changed, 5 insertions(+), 6 deletions(-) diff --git a/src/internal/FluencePeer.ts b/src/internal/FluencePeer.ts index d60ad74f6..2516e3518 100644 --- a/src/internal/FluencePeer.ts +++ b/src/internal/FluencePeer.ts @@ -462,7 +462,7 @@ export class FluencePeer { this._execSingleCallRequest(req) .catch( (err): CallServiceResult => ({ - retCode: ResultCodes.exceptionInHandler, + retCode: ResultCodes.error, result: `Handler failed. fnName="${req.fnName}" serviceId="${ req.serviceId }" error: ${err.toString()}`, @@ -532,7 +532,7 @@ export class FluencePeer { res = handler ? await handler(req) : { - retCode: ResultCodes.unknownError, + retCode: ResultCodes.error, result: `No handler has been registered for serviceId='${req.serviceId}' fnName='${req.fnName}' args='${req.args}'`, }; } diff --git a/src/internal/builtInServices.ts b/src/internal/builtInServices.ts index 6ee7cfb67..3e34c5573 100644 --- a/src/internal/builtInServices.ts +++ b/src/internal/builtInServices.ts @@ -30,7 +30,7 @@ const success = (result: any): CallServiceResult => { const error = (error: string): CallServiceResult => { return { result: error, - retCode: ResultCodes.unknownError, + retCode: ResultCodes.error, }; }; diff --git a/src/internal/commonTypes.ts b/src/internal/commonTypes.ts index 02d2e92d5..6e1ddea4c 100644 --- a/src/internal/commonTypes.ts +++ b/src/internal/commonTypes.ts @@ -59,8 +59,7 @@ export interface CallParams { export enum ResultCodes { success = 0, - unknownError = 1, - exceptionInHandler = 2, + error = 1, } /** diff --git a/src/internal/compilerSupport/LegacyCallServiceHandler.ts b/src/internal/compilerSupport/LegacyCallServiceHandler.ts index 34c484a31..73b1de01d 100644 --- a/src/internal/compilerSupport/LegacyCallServiceHandler.ts +++ b/src/internal/compilerSupport/LegacyCallServiceHandler.ts @@ -37,7 +37,7 @@ export const callLegacyCallServiceHandler = ( if (res.retCode === undefined) { res = { - retCode: ResultCodes.unknownError, + retCode: ResultCodes.error, result: `The handler did not set any result. Make sure you are calling the right peer and the handler has been registered. Original request data was: serviceId='${req.serviceId}' fnName='${req.fnName}' args='${req.args}'`, }; }