Fixing decoding API with signatures in names (#4125)

This commit is contained in:
Tomasz Drwięga 2017-01-11 12:17:04 +01:00 committed by Gav Wood
parent 21006da125
commit 26500af8c0
5 changed files with 96 additions and 25 deletions

View File

@ -23,12 +23,12 @@ import { eventSignature } from '../../util/signature';
export default class Event { export default class Event {
constructor (abi) { constructor (abi) {
this._name = abi.name;
this._inputs = EventParam.toEventParams(abi.inputs || []); this._inputs = EventParam.toEventParams(abi.inputs || []);
this._anonymous = !!abi.anonymous; 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._id = id;
this._name = name;
this._signature = signature; this._signature = signature;
} }

View File

@ -22,14 +22,14 @@ import { methodSignature } from '../util/signature';
export default class Func { export default class Func {
constructor (abi) { constructor (abi) {
this._abi = abi; this._abi = abi;
this._name = abi.name;
this._constant = !!abi.constant; this._constant = !!abi.constant;
this._payable = abi.payable; this._payable = abi.payable;
this._inputs = Param.toParams(abi.inputs || []); this._inputs = Param.toParams(abi.inputs || []);
this._outputs = Param.toParams(abi.outputs || []); 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._id = id;
this._name = name;
this._signature = signature; this._signature = signature;
} }

View File

@ -35,6 +35,18 @@ describe('abi/spec/Function', () => {
}); });
describe('constructor', () => { 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', () => { it('stores the parameters as received', () => {
expect(func.name).to.equal('test'); expect(func.name).to.equal('test');
expect(func.constant).to.be.false; expect(func.constant).to.be.false;

View File

@ -17,15 +17,31 @@
import { keccak_256 } from 'js-sha3'; // eslint-disable-line camelcase import { keccak_256 } from 'js-sha3'; // eslint-disable-line camelcase
import { fromParamType } from '../spec/paramType/format'; 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 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) { export function methodSignature (methodName, params) {
const { id, signature } = eventSignature(name, 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
};
} }

View File

@ -19,50 +19,93 @@ import { eventSignature, methodSignature } from './signature';
describe('abi/util/signature', () => { describe('abi/util/signature', () => {
describe('eventSignature', () => { describe('eventSignature', () => {
it('encodes signature baz() correctly', () => { it('encodes signature baz() correctly', () => {
expect(eventSignature('baz', [])) expect(eventSignature('baz', [])).to.deep.equal({
.to.deep.equal({ id: 'baz()', signature: 'a7916fac4f538170f7cd12c148552e2cba9fcd72329a2dd5b07a6fa906488ddf' }); id: 'baz()',
name: 'baz',
signature: 'a7916fac4f538170f7cd12c148552e2cba9fcd72329a2dd5b07a6fa906488ddf'
});
}); });
it('encodes signature baz(uint32) correctly', () => { it('encodes signature baz(uint32) correctly', () => {
expect(eventSignature('baz', [{ type: 'uint', length: 32 }])) expect(eventSignature('baz', [{ type: 'uint', length: 32 }])).to.deep.equal({
.to.deep.equal({ id: 'baz(uint32)', signature: '7d68785e8fc871be024b75964bd86d093511d4bc2dc7cf7bea32c48a0efaecb1' }); id: 'baz(uint32)',
name: 'baz',
signature: '7d68785e8fc871be024b75964bd86d093511d4bc2dc7cf7bea32c48a0efaecb1'
});
}); });
it('encodes signature baz(uint32, bool) correctly', () => { it('encodes signature baz(uint32, bool) correctly', () => {
expect(eventSignature('baz', [{ type: 'uint', length: 32 }, { type: 'bool' }])) expect(eventSignature('baz', [{ type: 'uint', length: 32 }, { type: 'bool' }])).to.deep.equal({
.to.deep.equal({ id: 'baz(uint32,bool)', signature: 'cdcd77c0992ec5bbfc459984220f8c45084cc24d9b6efed1fae540db8de801d2' }); id: 'baz(uint32,bool)',
name: 'baz',
signature: 'cdcd77c0992ec5bbfc459984220f8c45084cc24d9b6efed1fae540db8de801d2'
});
}); });
it('encodes no-name signature correctly as ()', () => { it('encodes no-name signature correctly as ()', () => {
expect(eventSignature(undefined, [])) expect(eventSignature(undefined, [])).to.deep.equal({
.to.deep.equal({ id: '()', signature: '861731d50c3880a2ca1994d5ec287b94b2f4bd832a67d3e41c08177bdd5674fe' }); id: '()',
name: undefined,
signature: '861731d50c3880a2ca1994d5ec287b94b2f4bd832a67d3e41c08177bdd5674fe'
});
}); });
it('encodes no-params signature correctly as ()', () => { it('encodes no-params signature correctly as ()', () => {
expect(eventSignature(undefined, undefined)) expect(eventSignature(undefined, undefined)).to.deep.equal({
.to.deep.equal({ id: '()', signature: '861731d50c3880a2ca1994d5ec287b94b2f4bd832a67d3e41c08177bdd5674fe' }); id: '()',
name: undefined,
signature: '861731d50c3880a2ca1994d5ec287b94b2f4bd832a67d3e41c08177bdd5674fe'
});
}); });
}); });
describe('methodSignature', () => { describe('methodSignature', () => {
it('encodes signature baz() correctly', () => { 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', () => { 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', () => { 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 ()', () => { 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 ()', () => { 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'
});
}); });
}); });
}); });