secjs-hashcode

Encoding for Contract Application Binary Interface (ABI), including function name and input paras, based on the rule https://solidity.readthedocs.io/en/develop/abi-spec.html

Usage no npm install needed!

<script type="module">
  import secjsHashcode from 'https://cdn.skypack.dev/secjs-hashcode';
</script>

README

secjs-hashcode

Encoding for Contract Application Binary Interface (ABI), including function name and input parameter values, based on the rule https://solidity.readthedocs.io/en/develop/abi-spec.html For further information in detail. Please refer to test cases and introduction in test/test.js.

JavaScript Style Guide

[JavaScript Style Guide]


new HashCode(sFnName, aParaValue)

new HashCode(sFnName, aParaValue) Constructs an Encoder.

Param Type Description
sFnName String The function name with the parenthesised list of parameter types. Parameter types are split by a single comma - no spaces are used. e.g. "sam(bytes,bool,uint[])"
aParaValue Array.<Dict> The array of input Parameter Values with its type. e.g. [{value: 'dave', type: 'bytes'}, {value: true, type: 'bool'}, {value: [1, 2, 3], type: 'uint[]'}]

Example

const HashCode = require('../src/index')

const sFnName = 'sam(bytes,bool,uint[])'
const aParaValue = [{value: 'dave', type: 'bytes'}, {value: true, type: 'bool'}, {value: [1, 2, 3], type: 'uint[]'}]
const oHashCode = new HashCode(sFnName, aParaValue)

HashCode.getFnName() ⇒ String

Returns the current Function Name information.

Example

const sFnName = oHashCode.getFnName()

HashCode.getParaValue() ⇒ Array.<Dict>

Returns the current input parameter values information.

Example

const aParaValue = oHashCode.getParaValue()

HashCode.setFnName(sFnName)

Set the Function Name.

Example

const sFnName = 'sam(bytes,bool,uint[])'
oHashCode.setFnName(sFnName)

HashCode.setParaValue(aParaValue)

Set the input parameter values.

Example

const aParaValue = [{value: 'dave', type: 'bytes'}, {value: true, type: 'bool'}, {value: [1, 2, 3], type: 'uint[]'}]
oHashCode.setParaValue(aParaValue)

HashCode.runHashCode()

Run the encoding based on current function name information and input parameter values

Example

oHashCode.runHashCode(aParaValue)

HashCode.getHashCode() ⇒ String

Returns the endcoded Bytecode String.

Example

const sByteCode = oHashCode1.getHashCode()
/*
An example for the format of result:
"8be6524600000000000000000000000000000000000000000000000000000000000001230000000000000000000000000000000000000000000000000000000000000080313233343536373839300000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000e0000000000000000000000000000000000000000000000000000000000000000200000000000000000000000000000000000000000000000000000000000004560000000000000000000000000000000000000000000000000000000000000789000000000000000000000000000000000000000000000000000000000000000d48656c6c6f2c20776f726c642100000000000000000000000000000000000000"
*/

HashCode.getHashCodeFragments() ⇒ Array.<String>

Returns the array of encoded result of each element to have a better look and check.

Example

const aHashCodeFragments = oHashCode1.getHashCodeFragments()
/*
An example for the format of result:
["8be65246",
"0000000000000000000000000000000000000000000000000000000000000123",
"0000000000000000000000000000000000000000000000000000000000000080",
"3132333435363738393000000000000000000000000000000000000000000000",
"00000000000000000000000000000000000000000000000000000000000000e0",
"0000000000000000000000000000000000000000000000000000000000000002",
"0000000000000000000000000000000000000000000000000000000000000456",
"0000000000000000000000000000000000000000000000000000000000000789",
"000000000000000000000000000000000000000000000000000000000000000d",
"48656c6c6f2c20776f726c642100000000000000000000000000000000000000"]
*/

HashCode.getHashCodeFragments() ⇒ Array.<String>

Show the encoded result of each element row by row in console to have a better look and check.

Example

oHashCode1.showHashCodeFragments()
/*
An example for the console output:
8be65246
0000000000000000000000000000000000000000000000000000000000000123
0000000000000000000000000000000000000000000000000000000000000080
3132333435363738393000000000000000000000000000000000000000000000
00000000000000000000000000000000000000000000000000000000000000e0
0000000000000000000000000000000000000000000000000000000000000002
0000000000000000000000000000000000000000000000000000000000000456
0000000000000000000000000000000000000000000000000000000000000789
000000000000000000000000000000000000000000000000000000000000000d
48656c6c6f2c20776f726c642100000000000000000000000000000000000000
*/