mirror of
https://github.com/fluencelabs/fluence-js.git
synced 2024-12-05 02:10:18 +00:00
Implement additional builtins: array_length, sha256_string, concat_strings (#130)
This commit is contained in:
parent
d860ea6dec
commit
25f63f0b0d
13
package-lock.json
generated
13
package-lock.json
generated
@ -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",
|
||||
|
@ -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",
|
||||
|
@ -17,6 +17,9 @@ describe('Tests for default handler', () => {
|
||||
|
||||
${'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]}
|
||||
${'op'} | ${'concat'} | ${[]} | ${0} | ${[]}
|
||||
@ -34,6 +37,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']}}
|
||||
${'peer'} | ${'timeout'} | ${[]} | ${1} | ${'timeout accepts exactly two arguments: timeout duration in ms and a message string'}}
|
||||
|
@ -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: {
|
||||
|
Loading…
Reference in New Issue
Block a user