Skip to content
This repository was archived by the owner on Jul 10, 2025. It is now read-only.

Commit 25f63f0

Browse files
authored
Implement additional builtins: array_length, sha256_string, concat_strings (#130)
1 parent d860ea6 commit 25f63f0

File tree

4 files changed

+51
-11
lines changed

4 files changed

+51
-11
lines changed

package-lock.json

Lines changed: 7 additions & 6 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

package.json

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -27,6 +27,7 @@
2727
"@fluencelabs/avm-runner-background": "0.1.2",
2828
"@fluencelabs/avm-runner-interface": "^0.2.0",
2929
"async": "3.2.0",
30+
"browser-or-node": "^2.0.0",
3031
"bs58": "4.0.1",
3132
"buffer": "^6.0.3",
3233
"cids": "0.8.1",
@@ -38,11 +39,11 @@
3839
"libp2p-websockets": "^0.16.2",
3940
"loglevel": "1.7.0",
4041
"multiaddr": "^10.0.1",
42+
"multiformats": "^9.6.4",
4143
"peer-id": "=0.15.4",
4244
"rxjs": "^7.3.0",
4345
"ts-pattern": "^3.3.3",
44-
"uuid": "8.3.0",
45-
"browser-or-node": "^2.0.0"
46+
"uuid": "8.3.0"
4647
},
4748
"devDependencies": {
4849
"@fluencelabs/aqua": "^0.5.3-258",

src/__test__/unit/builtInHandler.spec.ts

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,9 @@ describe('Tests for default handler', () => {
1616
${'op'} | ${'noop'} | ${[1, 2]} | ${0} | ${{}}
1717
1818
${'op'} | ${'array'} | ${[1, 2, 3]} | ${0} | ${[1, 2, 3]}
19+
20+
${'op'} | ${'array_length'} | ${[[1, 2, 3]]} | ${0} | ${3}
21+
${'op'} | ${'array_length'} | ${[]} | ${1} | ${'array_length accepts exactly one argument, found: 0'}
1922
2023
${'op'} | ${'concat'} | ${[[1, 2], [3, 4], [5, 6]]} | ${0} | ${[1, 2, 3, 4, 5, 6]}
2124
${'op'} | ${'concat'} | ${[[1, 2]]} | ${0} | ${[1, 2]}
@@ -33,6 +36,13 @@ describe('Tests for default handler', () => {
3336
3437
${'op'} | ${'bytes_from_b58'} | ${["3yZe7d"]} | ${0} | ${[116, 101, 115, 116]}
3538
${'op'} | ${'bytes_from_b58'} | ${["3yZe7d", 1]} | ${1} | ${"bytes_from_b58 accepts only one string argument"}
39+
40+
${'op'} | ${'sha256_string'} | ${["hello, world!"]} | ${0} | ${"QmVQ8pg6L1tpoWYeq6dpoWqnzZoSLCh7E96fCFXKvfKD3u"}
41+
${'op'} | ${'sha256_string'} | ${["hello, world!", true]} | ${0} | ${"84V7ZxLW7qKsx1Qvbd63BdGaHxUc3TfT2MBPqAXM7Wyu"}
42+
${'op'} | ${'sha256_string'} | ${[]} | ${1} | ${"sha256_string accepts 1-3 arguments, found: 0"}
43+
44+
${'op'} | ${'concat_strings'} | ${[]} | ${0} | ${""}
45+
${'op'} | ${'concat_strings'} | ${["a", "b", "c"]} | ${0} | ${"abc"}
3646
3747
${'peer'} | ${'timeout'} | ${[200, []]} | ${0} | ${[]}}
3848
${'peer'} | ${'timeout'} | ${[200, ['test']]} | ${0} | ${['test']}}

src/internal/builtins/common.ts

Lines changed: 31 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -16,9 +16,9 @@
1616

1717
import { CallServiceResult } from '@fluencelabs/avm-runner-interface';
1818
import { encode, decode } from 'bs58';
19-
import { PeerIdB58 } from 'src';
20-
import { GenericCallServiceHandler, ResultCodes } from '../commonTypes';
21-
import { KeyPair } from '../KeyPair';
19+
import { sha256 } from 'multiformats/hashes/sha2';
20+
import { ResultCodes } from '../commonTypes';
21+
import Buffer from '../Buffer';
2222

2323
const success = (result: any): CallServiceResult => {
2424
return {
@@ -44,6 +44,14 @@ export const builtInServices = {
4444
return success(req.args);
4545
},
4646

47+
array_length: (req) => {
48+
if (req.args.length !== 1) {
49+
return error('array_length accepts exactly one argument, found: ' + req.args.length);
50+
} else {
51+
return success(req.args[0].length);
52+
}
53+
},
54+
4755
identity: (req) => {
4856
if (req.args.length > 1) {
4957
return error(`identity accepts up to 1 arguments, received ${req.args.length} arguments`);
@@ -98,6 +106,26 @@ export const builtInServices = {
98106
return success(Array.from(decode(req.args[0])));
99107
}
100108
},
109+
110+
sha256_string: async (req) => {
111+
if (req.args.length < 1 || req.args.length > 3) {
112+
return error('sha256_string accepts 1-3 arguments, found: ' + req.args.length);
113+
} else {
114+
const [input, digestOnly, asBytes] = req.args;
115+
const inBuffer = Buffer.from(input);
116+
const multihash = await sha256.digest(inBuffer);
117+
118+
const outBytes = digestOnly ? multihash.digest : multihash.bytes;
119+
const res = asBytes ? Array.from(outBytes) : encode(outBytes);
120+
121+
return success(res);
122+
}
123+
},
124+
125+
concat_strings: (req) => {
126+
const res = ''.concat(...req.args);
127+
return success(res);
128+
},
101129
},
102130

103131
peer: {

0 commit comments

Comments
 (0)