Skip to content
This repository has been archived by the owner on Sep 3, 2019. It is now read-only.

converts bytes[N] with hex value #86

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
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
1 change: 1 addition & 0 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,7 @@
"ethjs-abi": "^0.2.1",
"lodash": "^4.17.10",
"utf8": "^3.0.0",
"web3-eth-abi": "^1.0.0-beta.36",
"web3-utils": "^1.0.0-beta.36"
},
"devDependencies": {
Expand Down
7 changes: 6 additions & 1 deletion src/formatters/encoder.js
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
const _ = require('lodash');
const Web3Utils = require('web3-utils');
const Web3ETHAbi = require('web3-eth-abi');
const BigNumber = require('bignumber.js');
const bs58 = require('bs58');

Expand Down Expand Up @@ -236,7 +237,11 @@ class Encoder {
hex = this.uintToHex(value);
}
} else if (type.match(Constants.REGEX_BYTES)) { // fixed bytes, ie. bytes32
hex = this.stringToHex(value, Constants.MAX_HEX_CHARS_PER_BYTE);
if (Web3Utils.isHexStrict(value)) {
hex = Web3ETHAbi.encodeParameter(type, value).substr(2);
} else {
hex = this.stringToHex(value, Constants.MAX_HEX_CHARS_PER_BYTE);
}
} else if (type.match(Constants.REGEX_STATIC_BYTES_ARRAY)) { // fixed bytes array, ie. bytes32[10]
const arrCapacity = _.toNumber(type.match(Constants.REGEX_NUMBER)[1]);
if (value instanceof Array) {
Expand Down
71 changes: 71 additions & 0 deletions src/formatters/tests/encoder.tests.js
Original file line number Diff line number Diff line change
Expand Up @@ -844,6 +844,50 @@ describe('Encoder', () => {
.toLowerCase());
});

it('converts bytes types with hex value', () => {
let methodObj = {
constant: true,
inputs: [
{
name: '',
type: 'bytes32',
},
],
name: 'didWithdraw',
outputs: [],
payable: false,
stateMutability: 'view',
type: 'function',
};
let args = ['0x68656c6c6f'];
let dataHex = Encoder.constructData([methodObj], 'didWithdraw', args);

let funcHash = Encoder.objToHash(methodObj, true);
let param = '68656c6c6f000000000000000000000000000000000000000000000000000000';
assert.equal(dataHex, funcHash.concat(param));

methodObj = {
constant: true,
inputs: [
{
name: '',
type: 'bytes8',
},
],
name: 'didWithdraw',
outputs: [],
payable: false,
stateMutability: 'view',
type: 'function',
};
args = ['0x68656c6c6f'];
dataHex = Encoder.constructData([methodObj], 'didWithdraw', args);

funcHash = Encoder.objToHash(methodObj, true);
param = '68656c6c6f000000000000000000000000000000000000000000000000000000';
assert.equal(dataHex, funcHash.concat(param));
});

it('converts bytes types', () => {
let methodObj = {
constant: true,
Expand Down Expand Up @@ -973,6 +1017,33 @@ describe('Encoder', () => {
assert.equal(dataHex, funcHash);
});

it('converts bytes32 types', () => {
const methodObj = {
constant: true,
inputs: [
{
name: 'v',
type: 'uint8',
}, {
name: 'r',
type: 'bytes32',
}, {
name: 's',
type: 'bytes32',
},
],
name: 'verify',
outputs: [],
payable: false,
stateMutability: 'view',
type: 'function',
};
const args = ['0x1c', '0x9e72ad2e50c8cff774ce8984e9cbd47d3edb658f72a4938e8e2e475390a8e57e', '0x3685b2ee64860d800428efa29b4da13453455159a9cd2448c7ce15844f6d564d'];
const dataHex = Encoder.constructData([methodObj], 'verify', args);

assert.equal('e2454522000000000000000000000000000000000000000000000000000000000000001c9e72ad2e50c8cff774ce8984e9cbd47d3edb658f72a4938e8e2e475390a8e57e3685b2ee64860d800428efa29b4da13453455159a9cd2448c7ce15844f6d564d', dataHex);
});

it('throws if abi is undefined', () => {
assert.throws(() => Encoder.constructData(undefined, 'test', ['qKjn4fStBaAtwGiwueJf9qFxgpbAvf1xAy', 'Hello World',
['a', 'b', 'c'], 'c350', 'c738']), Error);
Expand Down