Fixing decoding API with signatures in names (#4125)
This commit is contained in:
parent
21006da125
commit
26500af8c0
@ -23,12 +23,12 @@ import { eventSignature } from '../../util/signature';
|
||||
|
||||
export default class Event {
|
||||
constructor (abi) {
|
||||
this._name = abi.name;
|
||||
this._inputs = EventParam.toEventParams(abi.inputs || []);
|
||||
this._anonymous = !!abi.anonymous;
|
||||
|
||||
const { id, signature } = eventSignature(this._name, this.inputParamTypes());
|
||||
const { id, name, signature } = eventSignature(abi.name, this.inputParamTypes());
|
||||
this._id = id;
|
||||
this._name = name;
|
||||
this._signature = signature;
|
||||
}
|
||||
|
||||
|
@ -22,14 +22,14 @@ import { methodSignature } from '../util/signature';
|
||||
export default class Func {
|
||||
constructor (abi) {
|
||||
this._abi = abi;
|
||||
this._name = abi.name;
|
||||
this._constant = !!abi.constant;
|
||||
this._payable = abi.payable;
|
||||
this._inputs = Param.toParams(abi.inputs || []);
|
||||
this._outputs = Param.toParams(abi.outputs || []);
|
||||
|
||||
const { id, signature } = methodSignature(this._name, this.inputParamTypes());
|
||||
const { id, name, signature } = methodSignature(abi.name, this.inputParamTypes());
|
||||
this._id = id;
|
||||
this._name = name;
|
||||
this._signature = signature;
|
||||
}
|
||||
|
||||
|
@ -35,6 +35,18 @@ describe('abi/spec/Function', () => {
|
||||
});
|
||||
|
||||
describe('constructor', () => {
|
||||
it('returns signature correctly if name already contains it', () => {
|
||||
const func = new Func({
|
||||
name: 'test(bool,string)',
|
||||
inputs: inputsArr,
|
||||
outputs: outputsArr
|
||||
});
|
||||
|
||||
expect(func.name).to.equal('test');
|
||||
expect(func.id).to.equal('test(bool,string)');
|
||||
expect(func.signature).to.equal('02356205');
|
||||
});
|
||||
|
||||
it('stores the parameters as received', () => {
|
||||
expect(func.name).to.equal('test');
|
||||
expect(func.constant).to.be.false;
|
||||
|
@ -17,15 +17,31 @@
|
||||
import { keccak_256 } from 'js-sha3'; // eslint-disable-line camelcase
|
||||
import { fromParamType } from '../spec/paramType/format';
|
||||
|
||||
export function eventSignature (name, params) {
|
||||
export function eventSignature (eventName, params) {
|
||||
const { strName, name } = parseName(eventName);
|
||||
const types = (params || []).map(fromParamType).join(',');
|
||||
const id = `${name || ''}(${types})`;
|
||||
const id = `${strName}(${types})`;
|
||||
|
||||
return { id, signature: keccak_256(id) };
|
||||
return { id, name, signature: keccak_256(id) };
|
||||
}
|
||||
|
||||
export function methodSignature (name, params) {
|
||||
const { id, signature } = eventSignature(name, params);
|
||||
export function methodSignature (methodName, params) {
|
||||
const { id, name, signature } = eventSignature(methodName, params);
|
||||
|
||||
return { id, signature: signature.substr(0, 8) };
|
||||
return { id, name, signature: signature.substr(0, 8) };
|
||||
}
|
||||
|
||||
function parseName (name) {
|
||||
const strName = `${name || ''}`;
|
||||
const idx = strName.indexOf('(');
|
||||
|
||||
if (idx === -1) {
|
||||
return { strName, name };
|
||||
}
|
||||
|
||||
const trimmedName = strName.slice(0, idx);
|
||||
return {
|
||||
strName: trimmedName,
|
||||
name: trimmedName
|
||||
};
|
||||
}
|
||||
|
@ -19,50 +19,93 @@ import { eventSignature, methodSignature } from './signature';
|
||||
describe('abi/util/signature', () => {
|
||||
describe('eventSignature', () => {
|
||||
it('encodes signature baz() correctly', () => {
|
||||
expect(eventSignature('baz', []))
|
||||
.to.deep.equal({ id: 'baz()', signature: 'a7916fac4f538170f7cd12c148552e2cba9fcd72329a2dd5b07a6fa906488ddf' });
|
||||
expect(eventSignature('baz', [])).to.deep.equal({
|
||||
id: 'baz()',
|
||||
name: 'baz',
|
||||
signature: 'a7916fac4f538170f7cd12c148552e2cba9fcd72329a2dd5b07a6fa906488ddf'
|
||||
});
|
||||
});
|
||||
|
||||
it('encodes signature baz(uint32) correctly', () => {
|
||||
expect(eventSignature('baz', [{ type: 'uint', length: 32 }]))
|
||||
.to.deep.equal({ id: 'baz(uint32)', signature: '7d68785e8fc871be024b75964bd86d093511d4bc2dc7cf7bea32c48a0efaecb1' });
|
||||
expect(eventSignature('baz', [{ type: 'uint', length: 32 }])).to.deep.equal({
|
||||
id: 'baz(uint32)',
|
||||
name: 'baz',
|
||||
signature: '7d68785e8fc871be024b75964bd86d093511d4bc2dc7cf7bea32c48a0efaecb1'
|
||||
});
|
||||
});
|
||||
|
||||
it('encodes signature baz(uint32, bool) correctly', () => {
|
||||
expect(eventSignature('baz', [{ type: 'uint', length: 32 }, { type: 'bool' }]))
|
||||
.to.deep.equal({ id: 'baz(uint32,bool)', signature: 'cdcd77c0992ec5bbfc459984220f8c45084cc24d9b6efed1fae540db8de801d2' });
|
||||
expect(eventSignature('baz', [{ type: 'uint', length: 32 }, { type: 'bool' }])).to.deep.equal({
|
||||
id: 'baz(uint32,bool)',
|
||||
name: 'baz',
|
||||
signature: 'cdcd77c0992ec5bbfc459984220f8c45084cc24d9b6efed1fae540db8de801d2'
|
||||
});
|
||||
});
|
||||
|
||||
it('encodes no-name signature correctly as ()', () => {
|
||||
expect(eventSignature(undefined, []))
|
||||
.to.deep.equal({ id: '()', signature: '861731d50c3880a2ca1994d5ec287b94b2f4bd832a67d3e41c08177bdd5674fe' });
|
||||
expect(eventSignature(undefined, [])).to.deep.equal({
|
||||
id: '()',
|
||||
name: undefined,
|
||||
signature: '861731d50c3880a2ca1994d5ec287b94b2f4bd832a67d3e41c08177bdd5674fe'
|
||||
});
|
||||
});
|
||||
|
||||
it('encodes no-params signature correctly as ()', () => {
|
||||
expect(eventSignature(undefined, undefined))
|
||||
.to.deep.equal({ id: '()', signature: '861731d50c3880a2ca1994d5ec287b94b2f4bd832a67d3e41c08177bdd5674fe' });
|
||||
expect(eventSignature(undefined, undefined)).to.deep.equal({
|
||||
id: '()',
|
||||
name: undefined,
|
||||
signature: '861731d50c3880a2ca1994d5ec287b94b2f4bd832a67d3e41c08177bdd5674fe'
|
||||
});
|
||||
});
|
||||
});
|
||||
|
||||
describe('methodSignature', () => {
|
||||
it('encodes signature baz() correctly', () => {
|
||||
expect(methodSignature('baz', [])).to.deep.equal({ id: 'baz()', signature: 'a7916fac' });
|
||||
expect(methodSignature('baz', [])).to.deep.equal({
|
||||
id: 'baz()',
|
||||
name: 'baz',
|
||||
signature: 'a7916fac'
|
||||
});
|
||||
});
|
||||
|
||||
it('encodes signature baz(uint32) correctly', () => {
|
||||
expect(methodSignature('baz', [{ type: 'uint', length: 32 }])).to.deep.equal({ id: 'baz(uint32)', signature: '7d68785e' });
|
||||
expect(methodSignature('baz', [{ type: 'uint', length: 32 }])).to.deep.equal({
|
||||
id: 'baz(uint32)',
|
||||
name: 'baz',
|
||||
signature: '7d68785e'
|
||||
});
|
||||
});
|
||||
|
||||
it('encodes signature baz(uint32, bool) correctly', () => {
|
||||
expect(methodSignature('baz', [{ type: 'uint', length: 32 }, { type: 'bool' }])).to.deep.equal({ id: 'baz(uint32,bool)', signature: 'cdcd77c0' });
|
||||
expect(methodSignature('baz', [{ type: 'uint', length: 32 }, { type: 'bool' }])).to.deep.equal({
|
||||
id: 'baz(uint32,bool)',
|
||||
name: 'baz',
|
||||
signature: 'cdcd77c0'
|
||||
});
|
||||
});
|
||||
|
||||
it('encodes signature in name correctly', () => {
|
||||
expect(methodSignature('baz(uint32,bool)', [{ type: 'uint', length: 32 }, { type: 'bool' }])).to.deep.equal({
|
||||
id: 'baz(uint32,bool)',
|
||||
name: 'baz',
|
||||
signature: 'cdcd77c0'
|
||||
});
|
||||
});
|
||||
|
||||
it('encodes no-name signature correctly as ()', () => {
|
||||
expect(methodSignature(undefined, [])).to.deep.equal({ id: '()', signature: '861731d5' });
|
||||
expect(methodSignature(undefined, [])).to.deep.equal({
|
||||
id: '()',
|
||||
name: undefined,
|
||||
signature: '861731d5'
|
||||
});
|
||||
});
|
||||
|
||||
it('encodes no-params signature correctly as ()', () => {
|
||||
expect(methodSignature(undefined, undefined)).to.deep.equal({ id: '()', signature: '861731d5' });
|
||||
expect(methodSignature(undefined, undefined)).to.deep.equal({
|
||||
id: '()',
|
||||
name: undefined,
|
||||
signature: '861731d5'
|
||||
});
|
||||
});
|
||||
});
|
||||
});
|
||||
|
Loading…
Reference in New Issue
Block a user