mirror of
https://github.com/fluencelabs/fluence-js.git
synced 2025-06-27 23:01:33 +00:00
Implement additional builtins: array_length, sha256_string, concat_strings (#130)
This commit is contained in:
@ -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: {
|
||||
|
Reference in New Issue
Block a user