diff --git a/package-lock.json b/package-lock.json index 9907a45b..94efcf0b 100644 --- a/package-lock.json +++ b/package-lock.json @@ -26,6 +26,7 @@ "libp2p-websockets": "^0.16.2", "loglevel": "1.7.0", "multiaddr": "^10.0.1", + "multiformats": "^9.6.4", "peer-id": "=0.15.4", "rxjs": "^7.3.0", "ts-pattern": "^3.3.3", @@ -6995,9 +6996,9 @@ "integrity": "sha512-lKxKYG6H03yCZUpAGOPOsMcGxd1RHCu1iKvEHYDPmTyq2HueGhD73ssNBqqQWfvYs04G9iUFRvmAVLW20Jw6ow==" }, "node_modules/multiformats": { - "version": "9.4.6", - "resolved": "https://registry.npmjs.org/multiformats/-/multiformats-9.4.6.tgz", - "integrity": "sha512-ngZRO82P7mPvw/3gu5NQ2QiUJGYTS0LAxvQnEAlWCJakvn7YpK2VAd9JWM5oosYUeqoVbkylH/FsqRc4fc2+ag==" + "version": "9.6.4", + "resolved": "https://registry.npmjs.org/multiformats/-/multiformats-9.6.4.tgz", + "integrity": "sha512-fCCB6XMrr6CqJiHNjfFNGT0v//dxOBMrOMqUIzpPc/mmITweLEyhvMpY9bF+jZ9z3vaMAau5E8B68DW77QMXkg==" }, "node_modules/multihashes": { "version": "0.4.21", @@ -15696,9 +15697,9 @@ } }, "multiformats": { - "version": "9.4.6", - "resolved": "https://registry.npmjs.org/multiformats/-/multiformats-9.4.6.tgz", - "integrity": "sha512-ngZRO82P7mPvw/3gu5NQ2QiUJGYTS0LAxvQnEAlWCJakvn7YpK2VAd9JWM5oosYUeqoVbkylH/FsqRc4fc2+ag==" + "version": "9.6.4", + "resolved": "https://registry.npmjs.org/multiformats/-/multiformats-9.6.4.tgz", + "integrity": "sha512-fCCB6XMrr6CqJiHNjfFNGT0v//dxOBMrOMqUIzpPc/mmITweLEyhvMpY9bF+jZ9z3vaMAau5E8B68DW77QMXkg==" }, "multihashes": { "version": "0.4.21", diff --git a/package.json b/package.json index a57e7778..c2aaa39c 100644 --- a/package.json +++ b/package.json @@ -27,6 +27,7 @@ "@fluencelabs/avm-runner-background": "0.1.2", "@fluencelabs/avm-runner-interface": "^0.2.0", "async": "3.2.0", + "browser-or-node": "^2.0.0", "bs58": "4.0.1", "buffer": "^6.0.3", "cids": "0.8.1", @@ -38,11 +39,11 @@ "libp2p-websockets": "^0.16.2", "loglevel": "1.7.0", "multiaddr": "^10.0.1", + "multiformats": "^9.6.4", "peer-id": "=0.15.4", "rxjs": "^7.3.0", "ts-pattern": "^3.3.3", - "uuid": "8.3.0", - "browser-or-node": "^2.0.0" + "uuid": "8.3.0" }, "devDependencies": { "@fluencelabs/aqua": "^0.5.3-258", diff --git a/src/__test__/unit/builtInHandler.spec.ts b/src/__test__/unit/builtInHandler.spec.ts index db06212f..491b94e9 100644 --- a/src/__test__/unit/builtInHandler.spec.ts +++ b/src/__test__/unit/builtInHandler.spec.ts @@ -16,6 +16,9 @@ describe('Tests for default handler', () => { ${'op'} | ${'noop'} | ${[1, 2]} | ${0} | ${{}} ${'op'} | ${'array'} | ${[1, 2, 3]} | ${0} | ${[1, 2, 3]} + + ${'op'} | ${'array_length'} | ${[[1, 2, 3]]} | ${0} | ${3} + ${'op'} | ${'array_length'} | ${[]} | ${1} | ${'array_length accepts exactly one argument, found: 0'} ${'op'} | ${'concat'} | ${[[1, 2], [3, 4], [5, 6]]} | ${0} | ${[1, 2, 3, 4, 5, 6]} ${'op'} | ${'concat'} | ${[[1, 2]]} | ${0} | ${[1, 2]} @@ -33,6 +36,13 @@ describe('Tests for default handler', () => { ${'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"} + + ${'op'} | ${'sha256_string'} | ${["hello, world!"]} | ${0} | ${"QmVQ8pg6L1tpoWYeq6dpoWqnzZoSLCh7E96fCFXKvfKD3u"} + ${'op'} | ${'sha256_string'} | ${["hello, world!", true]} | ${0} | ${"84V7ZxLW7qKsx1Qvbd63BdGaHxUc3TfT2MBPqAXM7Wyu"} + ${'op'} | ${'sha256_string'} | ${[]} | ${1} | ${"sha256_string accepts 1-3 arguments, found: 0"} + + ${'op'} | ${'concat_strings'} | ${[]} | ${0} | ${""} + ${'op'} | ${'concat_strings'} | ${["a", "b", "c"]} | ${0} | ${"abc"} ${'peer'} | ${'timeout'} | ${[200, []]} | ${0} | ${[]}} ${'peer'} | ${'timeout'} | ${[200, ['test']]} | ${0} | ${['test']}} diff --git a/src/internal/builtins/common.ts b/src/internal/builtins/common.ts index 4d021be2..bbb7dee5 100644 --- a/src/internal/builtins/common.ts +++ b/src/internal/builtins/common.ts @@ -16,9 +16,9 @@ import { CallServiceResult } from '@fluencelabs/avm-runner-interface'; import { encode, decode } from 'bs58'; -import { PeerIdB58 } from 'src'; -import { GenericCallServiceHandler, ResultCodes } from '../commonTypes'; -import { KeyPair } from '../KeyPair'; +import { sha256 } from 'multiformats/hashes/sha2'; +import { ResultCodes } from '../commonTypes'; +import Buffer from '../Buffer'; const success = (result: any): CallServiceResult => { return { @@ -44,6 +44,14 @@ export const builtInServices = { return success(req.args); }, + array_length: (req) => { + if (req.args.length !== 1) { + return error('array_length accepts exactly one argument, found: ' + req.args.length); + } else { + return success(req.args[0].length); + } + }, + identity: (req) => { if (req.args.length > 1) { return error(`identity accepts up to 1 arguments, received ${req.args.length} arguments`); @@ -98,6 +106,26 @@ export const builtInServices = { return success(Array.from(decode(req.args[0]))); } }, + + sha256_string: async (req) => { + if (req.args.length < 1 || req.args.length > 3) { + return error('sha256_string accepts 1-3 arguments, found: ' + req.args.length); + } else { + const [input, digestOnly, asBytes] = req.args; + const inBuffer = Buffer.from(input); + const multihash = await sha256.digest(inBuffer); + + const outBytes = digestOnly ? multihash.digest : multihash.bytes; + const res = asBytes ? Array.from(outBytes) : encode(outBytes); + + return success(res); + } + }, + + concat_strings: (req) => { + const res = ''.concat(...req.args); + return success(res); + }, }, peer: {