mirror of
https://github.com/status-im/web3.js.git
synced 2025-02-23 19:48:13 +00:00
added tests for ABI de/encoding
This commit is contained in:
parent
6310f29025
commit
0b260b2bf9
@ -369,6 +369,3 @@ Example
|
||||
myNumber: '62224',
|
||||
mySmallNumber: '16'
|
||||
}
|
||||
|
||||
|
||||
------------------------------------------------------------------------------
|
||||
|
@ -82,7 +82,7 @@ module.exports = {
|
||||
var hash = utils.sha3(data);
|
||||
|
||||
|
||||
var publicKey = secp256k1.recoverPubKey(hash.replace(/^0x/i,''), { r, s }, v - 27);
|
||||
var publicKey = secp256k1.recoverPubKey(hash.replace(/^0x/i,''), { r:r, s:s }, v - 27);
|
||||
console.log(publicKey);
|
||||
return (new Buffer(publicKey.encode('hex', false), 'hex')).slice(1);
|
||||
},
|
||||
|
@ -86,7 +86,7 @@ var _fireError = function (error, emitter, reject, callback) {
|
||||
* @return {String} full function/event name
|
||||
*/
|
||||
var _jsonInterfaceMethodToString = function (json) {
|
||||
if (json.name.indexOf('(') !== -1) {
|
||||
if (_.isObject(json) && json.name && json.name.indexOf('(') !== -1) {
|
||||
return json.name;
|
||||
}
|
||||
|
||||
|
54
test/eth.abi.decodeLog.js
Normal file
54
test/eth.abi.decodeLog.js
Normal file
@ -0,0 +1,54 @@
|
||||
var chai = require('chai');
|
||||
var assert = chai.assert;
|
||||
var Web3 = require('../src/index.js');
|
||||
var web3 = new Web3();
|
||||
|
||||
var tests = [{
|
||||
params: [[{
|
||||
type: 'string',
|
||||
name: 'myString'
|
||||
},{
|
||||
type: 'uint256',
|
||||
name: 'myNumber',
|
||||
indexed: true
|
||||
},{
|
||||
type: 'uint8',
|
||||
name: 'mySmallNumber',
|
||||
indexed: true
|
||||
}], '0x0000000000000000000000000000000000000000000000000000000000000020000000000000000000000000000000000000000000000000000000000000000748656c6c6f252100000000000000000000000000000000000000000000000000',
|
||||
['0x000000000000000000000000000000000000000000000000000000000000f310', '0x0000000000000000000000000000000000000000000000000000000000000010']],
|
||||
result: {
|
||||
'0': 'Hello%!',
|
||||
'1': '62224',
|
||||
'2': '16',
|
||||
myString: 'Hello%!',
|
||||
myNumber: '62224',
|
||||
mySmallNumber: '16',
|
||||
"__length__": 3
|
||||
}
|
||||
},{
|
||||
params: [[{
|
||||
type: 'bytes',
|
||||
name: 'HelloBytes'
|
||||
},{
|
||||
type: 'uint8',
|
||||
name: 'myNumber',
|
||||
indexed: true
|
||||
}], '0x0000000000000000000000000000000000000000000000000000000000000020000000000000000000000000000000000000000000000000000000000000000748656c6c6f252100000000000000000000000000000000000000000000000000',
|
||||
['0x000000000000000000000000000000000000000000000000000000000000f310', '0x0000000000000000000000000000000000000000000000000000000000000010']],
|
||||
result: {
|
||||
'0': '0x48656c6c6f2521',
|
||||
'1': '62224',
|
||||
HelloBytes: '0x48656c6c6f2521',
|
||||
myNumber: '62224',
|
||||
"__length__": 2
|
||||
}
|
||||
}];
|
||||
|
||||
describe('decodeLog', function () {
|
||||
tests.forEach(function (test) {
|
||||
it('should convert correctly', function () {
|
||||
assert.deepEqual(web3.eth.abi.decodeLog.apply(web3.eth.abi, test.params), test.result);
|
||||
});
|
||||
});
|
||||
});
|
20
test/eth.abi.decodeParameter.js
Normal file
20
test/eth.abi.decodeParameter.js
Normal file
@ -0,0 +1,20 @@
|
||||
var chai = require('chai');
|
||||
var assert = chai.assert;
|
||||
var Web3 = require('../src/index.js');
|
||||
var web3 = new Web3();
|
||||
|
||||
var tests = [{
|
||||
params: ['uint256', '0x0000000000000000000000000000000000000000000000000000000000000010'],
|
||||
result: "16"
|
||||
},{
|
||||
params: ['string', '0x0000000000000000000000000000000000000000000000000000000000000020000000000000000000000000000000000000000000000000000000000000000848656c6c6f212521000000000000000000000000000000000000000000000000'],
|
||||
result: "Hello!%!"
|
||||
}];
|
||||
|
||||
describe('decodeParameter', function () {
|
||||
tests.forEach(function (test) {
|
||||
it('should convert correctly', function () {
|
||||
assert.equal(web3.eth.abi.decodeParameter.apply(web3.eth.abi, test.params), test.result);
|
||||
});
|
||||
});
|
||||
});
|
36
test/eth.abi.decodeParameters.js
Normal file
36
test/eth.abi.decodeParameters.js
Normal file
@ -0,0 +1,36 @@
|
||||
var chai = require('chai');
|
||||
var assert = chai.assert;
|
||||
var Web3 = require('../src/index.js');
|
||||
var web3 = new Web3();
|
||||
|
||||
var tests = [{
|
||||
params: [['string', 'uint256'], '0x000000000000000000000000000000000000000000000000000000000000004000000000000000000000000000000000000000000000000000000000000000ea000000000000000000000000000000000000000000000000000000000000000848656c6c6f212521000000000000000000000000000000000000000000000000'],
|
||||
result: {
|
||||
'0': 'Hello!%!',
|
||||
'1': '234',
|
||||
"__length__": 2
|
||||
}
|
||||
},{
|
||||
params: [[{
|
||||
type: 'string',
|
||||
name: 'myString'
|
||||
},{
|
||||
type: 'uint256',
|
||||
name: 'myNumber'
|
||||
}], '0x000000000000000000000000000000000000000000000000000000000000004000000000000000000000000000000000000000000000000000000000000000ea000000000000000000000000000000000000000000000000000000000000000848656c6c6f212521000000000000000000000000000000000000000000000000'],
|
||||
result: {
|
||||
'0': 'Hello!%!',
|
||||
'1': '234',
|
||||
myString: 'Hello!%!',
|
||||
myNumber: '234',
|
||||
"__length__": 2
|
||||
}
|
||||
}];
|
||||
|
||||
describe('decodeParameters', function () {
|
||||
tests.forEach(function (test) {
|
||||
it('should convert correctly', function () {
|
||||
assert.deepEqual(web3.eth.abi.decodeParameters.apply(web3.eth.abi, test.params), test.result);
|
||||
});
|
||||
});
|
||||
});
|
47
test/eth.abi.encodeEventSignature.js
Normal file
47
test/eth.abi.encodeEventSignature.js
Normal file
@ -0,0 +1,47 @@
|
||||
var chai = require('chai');
|
||||
var assert = chai.assert;
|
||||
var Web3 = require('../src/index.js');
|
||||
var web3 = new Web3();
|
||||
|
||||
var tests = [{
|
||||
params: [{
|
||||
name: 'myEvent',
|
||||
type: 'event',
|
||||
inputs: [{
|
||||
type: 'uint256',
|
||||
name: 'myNumber'
|
||||
},{
|
||||
type: 'bytes32',
|
||||
name: 'myBytes'
|
||||
}]
|
||||
}],
|
||||
result: '0xf2eeb729e636a8cb783be044acf6b7b1e2c5863735b60d6daae84c366ee87d97'
|
||||
},{
|
||||
params: [{
|
||||
name: 'SomeEvent',
|
||||
type: 'event',
|
||||
inputs: [{
|
||||
type: 'bytes',
|
||||
name: 'somebytes'
|
||||
},{
|
||||
type: 'byte16',
|
||||
name: 'myBytes'
|
||||
}]
|
||||
}],
|
||||
result: '0xab132b6cdd50f8d4d2ea33c3f140a9b3cf40f451540c69765c4842508bb13838'
|
||||
},{
|
||||
params: [{
|
||||
name: 'AnotherEvent',
|
||||
type: 'event',
|
||||
inputs: []
|
||||
}],
|
||||
result: '0x601d819e31a3cd164f83f7a7cf9cb5042ab1acff87b773c68f63d059c0af2dc0'
|
||||
}];
|
||||
|
||||
describe('encodeEventSignature', function () {
|
||||
tests.forEach(function (test) {
|
||||
it('should convert correctly', function () {
|
||||
assert.equal(web3.eth.abi.encodeEventSignature.apply(web3.eth.abi, test.params), test.result);
|
||||
});
|
||||
});
|
||||
});
|
53
test/eth.abi.encodeFunctionCall.js
Normal file
53
test/eth.abi.encodeFunctionCall.js
Normal file
@ -0,0 +1,53 @@
|
||||
var chai = require('chai');
|
||||
var assert = chai.assert;
|
||||
var Web3 = require('../src/index.js');
|
||||
var web3 = new Web3();
|
||||
|
||||
var tests = [{
|
||||
params: [{
|
||||
name: 'myMethod',
|
||||
type: 'function',
|
||||
inputs: [{
|
||||
type: 'uint256',
|
||||
name: 'myNumber'
|
||||
},{
|
||||
type: 'string',
|
||||
name: 'myString'
|
||||
}]
|
||||
}, ['2345675643', 'Hello!%']],
|
||||
result: '0x24ee0097000000000000000000000000000000000000000000000000000000008bd02b7b0000000000000000000000000000000000000000000000000000000000000040000000000000000000000000000000000000000000000000000000000000000748656c6c6f212500000000000000000000000000000000000000000000000000'
|
||||
},{
|
||||
params: [{
|
||||
name: 'myOtherMethod',
|
||||
type: 'function',
|
||||
inputs: [{
|
||||
type: 'uint8',
|
||||
name: 'myNumberdd'
|
||||
},{
|
||||
type: 'bytes32',
|
||||
name: 'myBytes'
|
||||
}]
|
||||
}, [2323, '0x234567432145678543213456']],
|
||||
result: '0x04a2b93600000000000000000000000000000000000000000000000000000000000009132345674321456785432134560000000000000000000000000000000000000000'
|
||||
},{
|
||||
params: [{
|
||||
name: 'myMethod',
|
||||
type: 'function',
|
||||
inputs: [{
|
||||
type: 'uint16',
|
||||
name: 'myNumber'
|
||||
},{
|
||||
type: 'bytes',
|
||||
name: 'myBytes'
|
||||
}]
|
||||
}, ['2345675643', '0x23456743214567854321ffffdddddd']],
|
||||
result: '0x6bf6602e000000000000000000000000000000000000000000000000000000008bd02b7b0000000000000000000000000000000000000000000000000000000000000040000000000000000000000000000000000000000000000000000000000000000f23456743214567854321ffffdddddd0000000000000000000000000000000000'
|
||||
}];
|
||||
|
||||
describe('encodeFunctionCall', function () {
|
||||
tests.forEach(function (test) {
|
||||
it('should convert correctly', function () {
|
||||
assert.equal(web3.eth.abi.encodeFunctionCall.apply(web3.eth.abi, test.params), test.result);
|
||||
});
|
||||
});
|
||||
});
|
60
test/eth.abi.encodeFunctionSignature.js
Normal file
60
test/eth.abi.encodeFunctionSignature.js
Normal file
@ -0,0 +1,60 @@
|
||||
var chai = require('chai');
|
||||
var assert = chai.assert;
|
||||
var Web3 = require('../src/index.js');
|
||||
var web3 = new Web3();
|
||||
|
||||
var tests = [{
|
||||
params: [{
|
||||
name: 'myMethod',
|
||||
type: 'function',
|
||||
inputs: [{
|
||||
type: 'uint256',
|
||||
name: 'myNumber'
|
||||
},{
|
||||
type: 'string',
|
||||
name: 'myString'
|
||||
}]
|
||||
}],
|
||||
result: '0x24ee0097'
|
||||
},{
|
||||
params: [{
|
||||
name: 'myMethod',
|
||||
type: 'function',
|
||||
inputs: [{
|
||||
type: 'string',
|
||||
name: 'myNumber'
|
||||
},{
|
||||
type: 'bytes8',
|
||||
name: 'myString'
|
||||
}]
|
||||
}],
|
||||
result: '0x27b00c93'
|
||||
},{
|
||||
params: [{
|
||||
name: 'Somthing',
|
||||
type: 'function',
|
||||
inputs: [{
|
||||
type: 'uint16',
|
||||
name: 'myNumber'
|
||||
},{
|
||||
type: 'bytes',
|
||||
name: 'myString'
|
||||
}]
|
||||
}],
|
||||
result: '0x724ff7a1'
|
||||
},{
|
||||
params: [{
|
||||
name: 'something',
|
||||
type: 'function',
|
||||
inputs: []
|
||||
}],
|
||||
result: '0xa7a0d537'
|
||||
}];
|
||||
|
||||
describe('encodeFunctionSignature', function () {
|
||||
tests.forEach(function (test) {
|
||||
it('should convert correctly', function () {
|
||||
assert.equal(web3.eth.abi.encodeFunctionSignature.apply(web3.eth.abi, test.params), test.result);
|
||||
});
|
||||
});
|
||||
});
|
23
test/eth.abi.encodeParameter.js
Normal file
23
test/eth.abi.encodeParameter.js
Normal file
@ -0,0 +1,23 @@
|
||||
var chai = require('chai');
|
||||
var assert = chai.assert;
|
||||
var Web3 = require('../src/index.js');
|
||||
var web3 = new Web3();
|
||||
|
||||
var tests = [{
|
||||
params: ['uint256', '2345675643'],
|
||||
result: '0x000000000000000000000000000000000000000000000000000000008bd02b7b'
|
||||
},{
|
||||
params: ['bytes32', '0xdf3234'],
|
||||
result: '0xdf32340000000000000000000000000000000000000000000000000000000000'
|
||||
},{
|
||||
params: ['bytes32[]', ['0xdf3234', '0xfdfd']],
|
||||
result: '0x00000000000000000000000000000000000000000000000000000000000000200000000000000000000000000000000000000000000000000000000000000002df32340000000000000000000000000000000000000000000000000000000000fdfd000000000000000000000000000000000000000000000000000000000000'
|
||||
}];
|
||||
|
||||
describe('encodeParameter', function () {
|
||||
tests.forEach(function (test) {
|
||||
it('should convert correctly', function () {
|
||||
assert.equal(web3.eth.abi.encodeParameter.apply(web3.eth.abi, test.params), test.result);
|
||||
});
|
||||
});
|
||||
});
|
20
test/eth.abi.encodeParameters.js
Normal file
20
test/eth.abi.encodeParameters.js
Normal file
@ -0,0 +1,20 @@
|
||||
var chai = require('chai');
|
||||
var assert = chai.assert;
|
||||
var Web3 = require('../src/index.js');
|
||||
var web3 = new Web3();
|
||||
|
||||
var tests = [{
|
||||
params: [['uint256','string'], ['2345675643', 'Hello!%']],
|
||||
result: '0x000000000000000000000000000000000000000000000000000000008bd02b7b0000000000000000000000000000000000000000000000000000000000000040000000000000000000000000000000000000000000000000000000000000000748656c6c6f212500000000000000000000000000000000000000000000000000'
|
||||
},{
|
||||
params: [['uint8[]','bytes32'], [['34','434'], '0x324567fff']],
|
||||
result: '0x0000000000000000000000000000000000000000000000000000000000000040324567fff00000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000002000000000000000000000000000000000000000000000000000000000000002200000000000000000000000000000000000000000000000000000000000001b2'
|
||||
}];
|
||||
|
||||
describe('encodeParameters', function () {
|
||||
tests.forEach(function (test) {
|
||||
it('should convert correctly', function () {
|
||||
assert.equal(web3.eth.abi.encodeParameters.apply(web3.eth.abi, test.params), test.result);
|
||||
});
|
||||
});
|
||||
});
|
Loading…
x
Reference in New Issue
Block a user