Skip to content
This repository was archived by the owner on Jul 10, 2025. It is now read-only.
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
13 changes: 7 additions & 6 deletions package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

5 changes: 3 additions & 2 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -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",
Expand All @@ -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",
Expand Down
10 changes: 10 additions & 0 deletions src/__test__/unit/builtInHandler.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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]}
Expand All @@ -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']}}
Expand Down
34 changes: 31 additions & 3 deletions src/internal/builtins/common.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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 {
Expand All @@ -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`);
Expand Down Expand Up @@ -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: {
Expand Down