Initial new UI source code import (#2607)

* address -> name mappings

* expanding, loading all coin details

* send use only actual BasicCoin tokens registered (any reg)

* sending token & accounts

* form styling updates

* send form layout in place

* coin send working as expected

* api subscriptions on multiple addresses

* bring in events

* simplify

* basic events display in-place, functionally complete

* basic functionality in-place

* fix horrible event address issue

* rwork display of events slightly

* test TLA availability

* table for owner -> tokens

* fix signature lookup address

* fix signature lookup address

* basic overview styling

* txhash links

* page layout adjustments

* background import

* adjust colors

* no global registration, simplify color selection

* updated styling

* connection dialog for "busy connecting"

* initial token connection - WIP

* init token updates take place

* basic test for manual token

* rework connection display

* allow updates of the secure token

* first stab at making the build build

* update runner tags

* fix linting issues

* skip tests requiring network (should be e2e, TODO)

* re-enable javascript tag/runner

* release push does the trick

* push to any branch, CI name

* javscript-test runner as well

* swap dependencies build requires test

* revert stages swap

* retrieve images associated with tokens

* remove js build deps order

* null image when hash = 0x0

* 6x64 images (hashes for registries)

* don't pass tokens as prop to IdentityIcon

* check images against content hash pictures

* cleanup signer after connection changes

* fix naming typo

* display unknownImages for balances (not available as content hash)

* unknownImage for transfer dialog

* basic githubhint layout

* single input for commit/filename

* ethcore_hashContent call

* lookup hash

* registration in place

* fixes

* events is using a proper table

* pass value through as-is

* stop wrongly using main app IdentityIcon

* NEVER export class instance functions

* alignment back to normal

* typo  in definition

* set & get images working (mostly)

* show content retrieval info

* set exitcode via ||

* use javascript:latest images

* disable npm progress bar

* rename phase I

* rename phase II

* only send build output to GitHub on major branches

* also run the build step as part of the test (until comprehensive)

* ci-specific build (no webpack progress)

* allow for account creation via recovery phrase

* display account uuid (where available), closes #2546

* connection dialog now shows up in dapps as well, closes #2538

* token images show up as expected

* IdentityName component added and deployed

* fix padding tests

* adjust tests to map to stricter 0x-prefixed hex

* render names via common component for the address -> name

* split lint into seperate script (early exit)

* test phases changed to lint, test & pack

* pack part of test phase

* remove files marked for deletion (cleanup)

* Signer cleanups, start moving in the direction of the rest

* add personal signer methods

* basic signer request subscription

* don't poll blockNumber when not connected

* missing return, creating massive ws queue backlogs

* ΞTH -> ETH

* fix failing tests

* registry uses setAddress to actually set addresses now

* bytes mapping operates on lowerCase hex strings

* sha3 ids for each application

* add dappreg to list of contracts

* adjust alignment of queries

* show gas estimation log

* abi with payable for register function

* add key as required

* image retrieval from dappreg

* use proper Image urls

* embed and link apps from Parity, retrieved via /api/apps

* filter apps that has been replaced

* proxy entry for parity-utils

* add basiccoin abi

* add support for fallback abi type

* capture constructor paramaters

* merge master into js

* move images to assets/images/

* add font assets

* import fonts as part of build

* don't inline woff files

* Revert "merge master into js"

This reverts commit cfcfa81bd26f1b3cbc748d3afa1eb5c670b363fe.

* remove unused npm packages

* information on gas estimates (like almost everywhere else)

* don't pass gas & gasPrice to estimation

* display account passwordhint when available

* signer subscriptions based on polling & function trapping

* pending requests retrieved via jsapi

* update signer middleware

* remove all web3 instances

* remove web3 package

* last web3 dependencies removed

* no need to toChecksumAddress - api takes care of it

* expand description for personal_confirmRequest

* Signer conversion from web3 -> parity.js completed

* explicit in no return

* green circle background

* remove generated background

* convert /api/* paths to localhost:8080/api/* paths (hard-coded, temporary)

* change dapps to load from localhost:8080/ui/*

* remove dangling web3 files

* update manager test for signer

* /api/ping -> /

* additional token images

* additional token images

* add missing styles.css for 8180 error pages

* cater for txhash returning null/empty object

* adjust output directories

* Release merge with origin with ours strategy

* additional token images

* cater for development server

* s/localhost/127.0.0.1/ (cater for origin)

* Fix address selection for contract deployment

* Adjust z-index for error overlay

* better text on unique background pattern

* fix signer rejections

* Don't allow gavcoin transfer with no balance

* fix txhash rendering in signer

* remove unnecessary ParityBackground

* script to update js-precompiled

* Redirect from :8080 to :8180

* Remove extra return

* Dapp logo images
This commit is contained in:
Jaco Greeff
2016-10-18 11:52:56 +02:00
committed by Gav Wood
parent 6c7af57529
commit 1e6a2cb378
969 changed files with 57315 additions and 0 deletions

32
js/src/abi/README.md Normal file
View File

@@ -0,0 +1,32 @@
# ethabi-js
A very early, very POC-type port of [https://github.com/ethcore/ethabi](https://github.com/ethcore/ethabi) to JavaScript
[![Build Status](https://travis-ci.org/jacogr/ethabi-js.svg?branch=master)](https://travis-ci.org/jacogr/ethabi-js)
[![Coverage Status](https://coveralls.io/repos/github/jacogr/ethabi-js/badge.svg?branch=master)](https://coveralls.io/github/jacogr/ethabi-js?branch=master)
[![Dependency Status](https://david-dm.org/jacogr/ethabi-js.svg)](https://david-dm.org/jacogr/ethabi-js)
[![devDependency Status](https://david-dm.org/jacogr/ethabi-js/dev-status.svg)](https://david-dm.org/jacogr/ethabi-js#info=devDependencies)
## contributing
Clone the repo and install dependencies via `npm install`. Tests can be executed via
- `npm run testOnce` (100% covered unit tests)
## installation
Install the package with `npm install --save ethabi-js` from the [npm registry ethabi-js](https://www.npmjs.com/package/ethabi-js)
## implementation
### approach
- this version tries to stay as close to the original Rust version in intent, function names & purpose
- it is a basic port of the Rust version, relying on effectively the same test-suite (expanded where deemed appropriate)
- it is meant as a library to be used in other projects, i.e. [ethapi-js](https://www.npmjs.com/package/ethapi-js)
### differences to original Rust version
- internally the library operates on string binary representations as opposed to Vector bytes, lengths are therefore 64 bytes as opposed to 32 bytes
- function names are adapted from the Rust standard snake_case to the JavaScript standard camelCase
- due to the initial library focus, the cli component (as implemented by the original) is not supported nor mplemented

20
js/src/abi/abi.js Normal file
View File

@@ -0,0 +1,20 @@
// Copyright 2015, 2016 Ethcore (UK) Ltd.
// This file is part of Parity.
// Parity is free software: you can redistribute it and/or modify
// it under the terms of the GNU General Public License as published by
// the Free Software Foundation, either version 3 of the License, or
// (at your option) any later version.
// Parity is distributed in the hope that it will be useful,
// but WITHOUT ANY WARRANTY; without even the implied warranty of
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
// GNU General Public License for more details.
// You should have received a copy of the GNU General Public License
// along with Parity. If not, see <http://www.gnu.org/licenses/>.
import Interface from './spec/interface';
export default class Abi extends Interface {
}

View File

@@ -0,0 +1,30 @@
// Copyright 2015, 2016 Ethcore (UK) Ltd.
// This file is part of Parity.
// Parity is free software: you can redistribute it and/or modify
// it under the terms of the GNU General Public License as published by
// the Free Software Foundation, either version 3 of the License, or
// (at your option) any later version.
// Parity is distributed in the hope that it will be useful,
// but WITHOUT ANY WARRANTY; without even the implied warranty of
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
// GNU General Public License for more details.
// You should have received a copy of the GNU General Public License
// along with Parity. If not, see <http://www.gnu.org/licenses/>.
export default class BytesTaken {
constructor (bytes, newOffset) {
this._bytes = bytes;
this._newOffset = newOffset;
}
get bytes () {
return this._bytes;
}
get newOffset () {
return this._newOffset;
}
}

View File

@@ -0,0 +1,29 @@
// Copyright 2015, 2016 Ethcore (UK) Ltd.
// This file is part of Parity.
// Parity is free software: you can redistribute it and/or modify
// it under the terms of the GNU General Public License as published by
// the Free Software Foundation, either version 3 of the License, or
// (at your option) any later version.
// Parity is distributed in the hope that it will be useful,
// but WITHOUT ANY WARRANTY; without even the implied warranty of
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
// GNU General Public License for more details.
// You should have received a copy of the GNU General Public License
// along with Parity. If not, see <http://www.gnu.org/licenses/>.
import BytesTaken from './bytesTaken';
describe('abi/decoder/BytesTaken', () => {
describe('constructor', () => {
it('sets the bytes of the object', () => {
expect((new BytesTaken(1, 2)).bytes).to.equal(1);
});
it('sets the newOffset of the object', () => {
expect((new BytesTaken(3, 4)).newOffset).to.equal(4);
});
});
});

View File

@@ -0,0 +1,30 @@
// Copyright 2015, 2016 Ethcore (UK) Ltd.
// This file is part of Parity.
// Parity is free software: you can redistribute it and/or modify
// it under the terms of the GNU General Public License as published by
// the Free Software Foundation, either version 3 of the License, or
// (at your option) any later version.
// Parity is distributed in the hope that it will be useful,
// but WITHOUT ANY WARRANTY; without even the implied warranty of
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
// GNU General Public License for more details.
// You should have received a copy of the GNU General Public License
// along with Parity. If not, see <http://www.gnu.org/licenses/>.
export default class DecodeResult {
constructor (token, newOffset) {
this._token = token;
this._newOffset = newOffset;
}
get token () {
return this._token;
}
get newOffset () {
return this._newOffset;
}
}

View File

@@ -0,0 +1,29 @@
// Copyright 2015, 2016 Ethcore (UK) Ltd.
// This file is part of Parity.
// Parity is free software: you can redistribute it and/or modify
// it under the terms of the GNU General Public License as published by
// the Free Software Foundation, either version 3 of the License, or
// (at your option) any later version.
// Parity is distributed in the hope that it will be useful,
// but WITHOUT ANY WARRANTY; without even the implied warranty of
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
// GNU General Public License for more details.
// You should have received a copy of the GNU General Public License
// along with Parity. If not, see <http://www.gnu.org/licenses/>.
import DecodeResult from './decodeResult';
describe('abi/decoder/DecodeResult', () => {
describe('constructor', () => {
it('sets the token of the object', () => {
expect((new DecodeResult('token', 2)).token).to.equal('token');
});
it('sets the newOffset of the object', () => {
expect((new DecodeResult('baz', 4)).newOffset).to.equal(4);
});
});
});

View File

@@ -0,0 +1,145 @@
// Copyright 2015, 2016 Ethcore (UK) Ltd.
// This file is part of Parity.
// Parity is free software: you can redistribute it and/or modify
// it under the terms of the GNU General Public License as published by
// the Free Software Foundation, either version 3 of the License, or
// (at your option) any later version.
// Parity is distributed in the hope that it will be useful,
// but WITHOUT ANY WARRANTY; without even the implied warranty of
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
// GNU General Public License for more details.
// You should have received a copy of the GNU General Public License
// along with Parity. If not, see <http://www.gnu.org/licenses/>.
import utf8 from 'utf8';
import Token from '../token/token';
import BytesTaken from './bytesTaken';
import DecodeResult from './decodeResult';
import ParamType from '../spec/paramType/paramType';
import { sliceData } from '../util/slice';
import { asAddress, asBool, asI32, asU32 } from '../util/sliceAs';
import { isArray, isInstanceOf } from '../util/types';
const NULL = '0000000000000000000000000000000000000000000000000000000000000000';
export default class Decoder {
static decode (params, data) {
if (!isArray(params)) {
throw new Error('Parameters should be array of ParamType');
}
const slices = sliceData(data);
let offset = 0;
return params.map((param) => {
const result = Decoder.decodeParam(param, slices, offset);
offset = result.newOffset;
return result.token;
});
}
static peek (slices, position) {
if (!slices || !slices[position]) {
return NULL;
}
return slices[position];
}
static takeBytes (slices, position, length) {
const slicesLength = Math.floor((length + 31) / 32);
let bytesStr = '';
for (let idx = 0; idx < slicesLength; idx++) {
bytesStr = `${bytesStr}${Decoder.peek(slices, position + idx)}`;
}
const bytes = (bytesStr.substr(0, length * 2).match(/.{1,2}/g) || []).map((code) => parseInt(code, 16));
return new BytesTaken(bytes, position + slicesLength);
}
static decodeParam (param, slices, offset) {
if (!isInstanceOf(param, ParamType)) {
throw new Error('param should be instanceof ParamType');
}
const tokens = [];
let taken;
let lengthOffset;
let length;
let newOffset;
switch (param.type) {
case 'address':
return new DecodeResult(new Token(param.type, asAddress(Decoder.peek(slices, offset))), offset + 1);
case 'bool':
return new DecodeResult(new Token(param.type, asBool(Decoder.peek(slices, offset))), offset + 1);
case 'int':
return new DecodeResult(new Token(param.type, asI32(Decoder.peek(slices, offset))), offset + 1);
case 'uint':
return new DecodeResult(new Token(param.type, asU32(Decoder.peek(slices, offset))), offset + 1);
case 'fixedBytes':
taken = Decoder.takeBytes(slices, offset, param.length);
return new DecodeResult(new Token(param.type, taken.bytes), taken.newOffset);
case 'bytes':
lengthOffset = asU32(Decoder.peek(slices, offset)).div(32).toNumber();
length = asU32(Decoder.peek(slices, lengthOffset)).toNumber();
taken = Decoder.takeBytes(slices, lengthOffset + 1, length);
return new DecodeResult(new Token(param.type, taken.bytes), offset + 1);
case 'string':
if (param.indexed) {
taken = Decoder.takeBytes(slices, offset, 32);
return new DecodeResult(new Token('fixedBytes', taken.bytes), offset + 1);
}
lengthOffset = asU32(Decoder.peek(slices, offset)).div(32).toNumber();
length = asU32(Decoder.peek(slices, lengthOffset)).toNumber();
taken = Decoder.takeBytes(slices, lengthOffset + 1, length);
const str = taken.bytes.map((code) => String.fromCharCode(code)).join('');
return new DecodeResult(new Token(param.type, utf8.decode(str)), offset + 1);
case 'array':
lengthOffset = asU32(Decoder.peek(slices, offset)).div(32).toNumber();
length = asU32(Decoder.peek(slices, lengthOffset)).toNumber();
newOffset = lengthOffset + 1;
for (let idx = 0; idx < length; idx++) {
const result = Decoder.decodeParam(param.subtype, slices, newOffset);
newOffset = result.newOffset;
tokens.push(result.token);
}
return new DecodeResult(new Token(param.type, tokens), offset + 1);
case 'fixedArray':
newOffset = offset;
for (let idx = 0; idx < param.length; idx++) {
const result = Decoder.decodeParam(param.subtype, slices, newOffset);
newOffset = result.newOffset;
tokens.push(result.token);
}
return new DecodeResult(new Token(param.type, tokens), newOffset);
default:
throw new Error(`Invalid param type ${param.type} in decodeParam`);
}
}
}

View File

@@ -0,0 +1,310 @@
// Copyright 2015, 2016 Ethcore (UK) Ltd.
// This file is part of Parity.
// Parity is free software: you can redistribute it and/or modify
// it under the terms of the GNU General Public License as published by
// the Free Software Foundation, either version 3 of the License, or
// (at your option) any later version.
// Parity is distributed in the hope that it will be useful,
// but WITHOUT ANY WARRANTY; without even the implied warranty of
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
// GNU General Public License for more details.
// You should have received a copy of the GNU General Public License
// along with Parity. If not, see <http://www.gnu.org/licenses/>.
import BigNumber from 'bignumber.js';
import Decoder from './decoder';
import ParamType from '../spec/paramType';
import Token from '../token';
import { padU32 } from '../util/pad';
describe('abi/decoder/Decoder', () => {
const stringToBytes = function (str) {
return str.match(/.{1,2}/g).map((code) => parseInt(code, 16));
};
const address1 = '0000000000000000000000001111111111111111111111111111111111111111';
const address2 = '0000000000000000000000002222222222222222222222222222222222222222';
const address3 = '0000000000000000000000003333333333333333333333333333333333333333';
const address4 = '0000000000000000000000004444444444444444444444444444444444444444';
const bool1 = '0000000000000000000000000000000000000000000000000000000000000001';
const bytes1 = '1234000000000000000000000000000000000000000000000000000000000000';
const bytes2 = '1000000000000000000000000000000000000000000000000000000000000000';
const bytes3 = '10000000000000000000000000000000000000000000000000000000000002';
const bytes4 = '0010000000000000000000000000000000000000000000000000000000000002';
const int1 = '0111111111111111111111111111111111111111111111111111111111111111';
const intn = 'ffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffff85';
const string1 = '6761766f66796f726b0000000000000000000000000000000000000000000000';
const tokenAddress1 = new Token('address', `0x${address1.slice(-40)}`);
const tokenAddress2 = new Token('address', `0x${address2.slice(-40)}`);
const tokenAddress3 = new Token('address', `0x${address3.slice(-40)}`);
const tokenAddress4 = new Token('address', `0x${address4.slice(-40)}`);
const tokenBool1 = new Token('bool', true);
const tokenFixedBytes1 = new Token('fixedBytes', [0x12, 0x34]);
const tokenBytes1 = new Token('bytes', [0x12, 0x34]);
const tokenBytes2 = new Token('bytes', stringToBytes(bytes2).concat(stringToBytes(bytes2)));
const tokenBytes3 = new Token('bytes', stringToBytes(bytes3));
const tokenBytes4 = new Token('bytes', stringToBytes(bytes4));
const tokenInt1 = new Token('int', new BigNumber(int1, 16));
const tokenIntn = new Token('int', new BigNumber(-123));
const tokenUint1 = new Token('uint', new BigNumber(int1, 16));
const tokenUintn = new Token('uint', new BigNumber(intn, 16));
const tokenString1 = new Token('string', 'gavofyork');
const slices = [ address1, address2, address3, address4 ];
describe('peek', () => {
it('returns the slice at the correct position', () => {
expect(Decoder.peek(slices, 1)).to.equal(slices[1]);
});
it('returns empty on invalid slices', () => {
expect(Decoder.peek(null, 4)).to.equal('0000000000000000000000000000000000000000000000000000000000000000');
});
});
describe('takeBytes', () => {
it('returns a single slice', () => {
expect(Decoder.takeBytes(slices, 0, 32).bytes).to.deep.equal(stringToBytes(slices[0]));
});
it('returns a single partial slice', () => {
expect(Decoder.takeBytes(slices, 0, 20).bytes).to.deep.equal(stringToBytes(slices[0].substr(0, 40)));
});
it('returns multiple slices', () => {
expect(Decoder.takeBytes(slices, 0, 64).bytes).to.deep.equal(stringToBytes(`${slices[0]}${slices[1]}`));
});
it('returns a single offset slice', () => {
expect(Decoder.takeBytes(slices, 1, 32).bytes).to.deep.equal(stringToBytes(slices[1]));
});
it('returns multiple offset slices', () => {
expect(Decoder.takeBytes(slices, 1, 64).bytes).to.deep.equal(stringToBytes(`${slices[1]}${slices[2]}`));
});
it('returns the requires length from slices', () => {
expect(
Decoder.takeBytes(slices, 1, 75).bytes
).to.deep.equal(stringToBytes(`${slices[1]}${slices[2]}${slices[3]}`.substr(0, 150)));
});
});
describe('decodeParam', () => {
it('throws an error on non ParamType param', () => {
expect(() => Decoder.decodeParam({})).to.throw(/ParamType/);
});
it('throws an error on invalid param type', () => {
const pt = new ParamType('address');
pt._type = 'noMatch';
expect(() => Decoder.decodeParam(pt)).to.throw(/noMatch/);
});
it('decodes an address', () => {
expect(
Decoder.decodeParam(new ParamType('address'), [address1], 0).token
).to.deep.equal(tokenAddress1);
});
it('decodes a bool', () => {
expect(
Decoder.decodeParam(new ParamType('bool'), [bool1], 0).token
).to.deep.equal(tokenBool1);
});
it('decodes an int', () => {
expect(
Decoder.decodeParam(new ParamType('int'), [int1], 0).token
).to.deep.equal(tokenInt1);
});
it('decodes a negative int', () => {
expect(
Decoder.decodeParam(new ParamType('int'), [intn], 0).token
).to.deep.equal(tokenIntn);
});
it('decodes an uint', () => {
expect(
Decoder.decodeParam(new ParamType('uint'), [int1], 0).token
).to.deep.equal(tokenUint1);
});
it('decodes an uint (negative as int)', () => {
expect(
Decoder.decodeParam(new ParamType('uint'), [intn], 0).token
).to.deep.equal(tokenUintn);
});
it('decodes fixedBytes', () => {
expect(
Decoder.decodeParam(new ParamType('fixedBytes', null, 2), [bytes1], 0).token
).to.deep.equal(tokenFixedBytes1);
});
it('decodes bytes', () => {
expect(
Decoder.decodeParam(new ParamType('bytes'), [padU32(0x20), padU32(2), bytes1], 0).token
).to.deep.equal(tokenBytes1);
});
it('decodes string', () => {
expect(
Decoder.decodeParam(new ParamType('string'), [padU32(0x20), padU32(9), string1], 0).token
).to.deep.equal(tokenString1);
});
it('decodes string (indexed)', () => {
expect(
Decoder.decodeParam(new ParamType('string', null, 0, true), [bytes1], 0)
).to.deep.equal(Decoder.decodeParam(new ParamType('fixedBytes', null, 32, true), [bytes1], 0));
});
});
describe('decode', () => {
it('throws an error on invalid params', () => {
expect(() => Decoder.decode(null, '123')).to.throw(/array/);
});
describe('address', () => {
it('decodes an address', () => {
expect(
Decoder.decode(
[new ParamType('address')],
`${address1}`
)
).to.deep.equal([tokenAddress1]);
});
it('decodes 2 addresses', () => {
expect(
Decoder.decode(
[new ParamType('address'), new ParamType('address')],
`${address1}${address2}`
)
).to.deep.equal([tokenAddress1, tokenAddress2]);
});
it('decodes a fixedArray of addresses', () => {
expect(
Decoder.decode(
[new ParamType('fixedArray', new ParamType('address'), 2)],
`${address1}${address2}`
)
).to.deep.equal([new Token('fixedArray', [tokenAddress1, tokenAddress2])]);
});
it('decodes a dynamic array of addresses', () => {
expect(
Decoder.decode(
[new ParamType('array', new ParamType('address'))],
`${padU32(0x20)}${padU32(2)}${address1}${address2}`
)
).to.deep.equal([new Token('array', [tokenAddress1, tokenAddress2])]);
});
it('decodes a dynamic array of fixed arrays', () => {
expect(
Decoder.decode(
[new ParamType('array', new ParamType('fixedArray', new ParamType('address'), 2))],
`${padU32(0x20)}${padU32(2)}${address1}${address2}${address3}${address4}`
)
).to.deep.equal([
new Token('array', [
new Token('fixedArray', [tokenAddress1, tokenAddress2]),
new Token('fixedArray', [tokenAddress3, tokenAddress4])
])
]);
});
});
describe('int', () => {
it('decodes an int', () => {
expect(
Decoder.decode(
[new ParamType('int')],
`${int1}`
)
).to.deep.equal([tokenInt1]);
});
});
describe('uint', () => {
it('decodes an uint', () => {
expect(
Decoder.decode(
[new ParamType('uint')],
`${int1}`
)
).to.deep.equal([tokenUint1]);
});
});
describe('fixedBytes', () => {
it('decodes fixedBytes', () => {
expect(
Decoder.decode(
[new ParamType('fixedBytes', null, 2)],
`${bytes1}`
)
).to.deep.equal([tokenFixedBytes1]);
});
});
describe('bytes', () => {
it('decodes bytes', () => {
expect(
Decoder.decode(
[new ParamType('bytes')],
`${padU32(0x20)}${padU32(2)}${bytes1}`
)
).to.deep.equal([tokenBytes1]);
});
it('decodes bytes sequence', () => {
expect(
Decoder.decode(
[new ParamType('bytes')],
`${padU32(0x20)}${padU32(0x40)}${bytes2}${bytes2}`
)
).to.deep.equal([tokenBytes2]);
});
it('decodes bytes seuence (2)', () => {
expect(
Decoder.decode(
[new ParamType('bytes'), new ParamType('bytes')],
`${padU32(0x40)}${padU32(0x80)}${padU32(0x1f)}${bytes3}00${padU32(0x20)}${bytes4}`
)
).to.deep.equal([tokenBytes3, tokenBytes4]);
});
});
describe('bool', () => {
it('decodes a single bool', () => {
expect(
Decoder.decode(
[new ParamType('bool')],
bool1
)
).to.deep.equal([tokenBool1]);
});
});
describe('string', () => {
it('decodes a string', () => {
expect(
Decoder.decode(
[new ParamType('string')],
`${padU32(0x20)}${padU32(9)}${string1}`
)
).to.deep.equal([tokenString1]);
});
});
});
});

View File

@@ -0,0 +1,17 @@
// Copyright 2015, 2016 Ethcore (UK) Ltd.
// This file is part of Parity.
// Parity is free software: you can redistribute it and/or modify
// it under the terms of the GNU General Public License as published by
// the Free Software Foundation, either version 3 of the License, or
// (at your option) any later version.
// Parity is distributed in the hope that it will be useful,
// but WITHOUT ANY WARRANTY; without even the implied warranty of
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
// GNU General Public License for more details.
// You should have received a copy of the GNU General Public License
// along with Parity. If not, see <http://www.gnu.org/licenses/>.
export default from './decoder';

View File

@@ -0,0 +1,72 @@
// Copyright 2015, 2016 Ethcore (UK) Ltd.
// This file is part of Parity.
// Parity is free software: you can redistribute it and/or modify
// it under the terms of the GNU General Public License as published by
// the Free Software Foundation, either version 3 of the License, or
// (at your option) any later version.
// Parity is distributed in the hope that it will be useful,
// but WITHOUT ANY WARRANTY; without even the implied warranty of
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
// GNU General Public License for more details.
// You should have received a copy of the GNU General Public License
// along with Parity. If not, see <http://www.gnu.org/licenses/>.
import { padAddress, padBool, padBytes, padFixedBytes, padU32, padString } from '../util/pad';
import Mediate from './mediate';
import Token from '../token/token';
import { isArray, isInstanceOf } from '../util/types';
export default class Encoder {
static encode (tokens) {
if (!isArray(tokens)) {
throw new Error('tokens should be array of Token');
}
const mediates = tokens.map((token) => Encoder.encodeToken(token));
const inits = mediates
.map((mediate, idx) => mediate.init(Mediate.offsetFor(mediates, idx)))
.join('');
const closings = mediates
.map((mediate, idx) => mediate.closing(Mediate.offsetFor(mediates, idx)))
.join('');
return `${inits}${closings}`;
}
static encodeToken (token) {
if (!isInstanceOf(token, Token)) {
throw new Error('token should be instanceof Token');
}
switch (token.type) {
case 'address':
return new Mediate('raw', padAddress(token.value));
case 'int':
case 'uint':
return new Mediate('raw', padU32(token.value));
case 'bool':
return new Mediate('raw', padBool(token.value));
case 'fixedBytes':
return new Mediate('raw', padFixedBytes(token.value));
case 'bytes':
return new Mediate('prefixed', padBytes(token.value));
case 'string':
return new Mediate('prefixed', padString(token.value));
case 'fixedArray':
case 'array':
return new Mediate(token.type, token.value.map((token) => Encoder.encodeToken(token)));
default:
throw new Error(`Invalid token type ${token.type} in encodeToken`);
}
}
}

View File

@@ -0,0 +1,290 @@
// Copyright 2015, 2016 Ethcore (UK) Ltd.
// This file is part of Parity.
// Parity is free software: you can redistribute it and/or modify
// it under the terms of the GNU General Public License as published by
// the Free Software Foundation, either version 3 of the License, or
// (at your option) any later version.
// Parity is distributed in the hope that it will be useful,
// but WITHOUT ANY WARRANTY; without even the implied warranty of
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
// GNU General Public License for more details.
// You should have received a copy of the GNU General Public License
// along with Parity. If not, see <http://www.gnu.org/licenses/>.
import Encoder from './encoder';
import Token from '../token';
import { padAddress, padFixedBytes, padU32 } from '../util/pad';
describe('abi/encoder/Encoder', () => {
describe('encodeToken', () => {
it('requires token as Token', () => {
expect(() => Encoder.encodeToken()).to.throw(/Token/);
});
it('encodes address tokens in Mediate(raw)', () => {
const mediate = Encoder.encodeToken(new Token('address', '123'));
expect(mediate.type).to.equal('raw');
expect(mediate.value).to.be.ok;
});
it('encodes bool tokens in Mediate(raw)', () => {
const mediatet = Encoder.encodeToken(new Token('bool', true));
const mediatef = Encoder.encodeToken(new Token('bool', false));
expect(mediatet.type).to.equal('raw');
expect(mediatet.value).to.be.ok;
expect(mediatef.type).to.equal('raw');
expect(mediatef.value).to.be.ok;
});
it('encodes int tokens in Mediate(raw)', () => {
const mediate = Encoder.encodeToken(new Token('int', '123'));
expect(mediate.type).to.equal('raw');
expect(mediate.value).to.be.ok;
});
it('encodes uint tokens in Mediate(raw)', () => {
const mediate = Encoder.encodeToken(new Token('uint', '123'));
expect(mediate.type).to.equal('raw');
expect(mediate.value).to.be.ok;
});
it('encodes fixedBytes tokens in Mediate(raw)', () => {
const mediate = Encoder.encodeToken(new Token('fixedBytes', '123'));
expect(mediate.type).to.equal('raw');
expect(mediate.value).to.be.ok;
});
it('encodes bytes tokens in Mediate(prefixed)', () => {
const mediate = Encoder.encodeToken(new Token('bytes', '123'));
expect(mediate.type).to.equal('prefixed');
expect(mediate.value).to.be.ok;
});
it('encodes string tokens in Mediate(prefixed)', () => {
const mediate = Encoder.encodeToken(new Token('string', '123'));
expect(mediate.type).to.equal('prefixed');
expect(mediate.value).to.be.ok;
});
it('encodes fixedArray tokens in Mediate(fixedArray)', () => {
const mediate = Encoder.encodeToken(new Token('fixedArray', [new Token('uint', '123')]));
expect(mediate.type).to.equal('fixedArray');
expect(mediate.value).to.be.ok;
});
it('encodes array tokens in Mediate(array)', () => {
const mediate = Encoder.encodeToken(new Token('array', [new Token('uint', '123')]));
expect(mediate.type).to.equal('array');
expect(mediate.value).to.be.ok;
});
it('throws an Error on invalid tokens', () => {
const token = new Token('address');
token._type = 'noMatch';
expect(() => Encoder.encodeToken(token)).to.throw(/noMatch/);
});
});
describe('encode', () => {
it('requires tokens array', () => {
expect(() => Encoder.encode()).to.throw(/array/);
});
describe('addresses', () => {
const address1 = '1111111111111111111111111111111111111111';
const address2 = '2222222222222222222222222222222222222222';
const address3 = '3333333333333333333333333333333333333333';
const address4 = '4444444444444444444444444444444444444444';
const encAddress1 = padAddress(address1);
const encAddress2 = padAddress(address2);
const encAddress3 = padAddress(address3);
const encAddress4 = padAddress(address4);
const tokenAddress1 = new Token('address', address1);
const tokenAddress2 = new Token('address', address2);
const tokenAddress3 = new Token('address', address3);
const tokenAddress4 = new Token('address', address4);
it('encodes an address', () => {
const token = tokenAddress1;
expect(Encoder.encode([token])).to.equal(encAddress1);
});
it('encodes an array of addresses', () => {
const expected = `${padU32(0x20)}${padU32(2)}${encAddress1}${encAddress2}`;
const token = new Token('array', [tokenAddress1, tokenAddress2]);
expect(Encoder.encode([token])).to.equal(expected);
});
it('encodes an fixedArray of addresses', () => {
const expected = `${encAddress1}${encAddress2}`;
const token = new Token('fixedArray', [tokenAddress1, tokenAddress2]);
expect(Encoder.encode([token])).to.equal(expected);
});
it('encodes two addresses', () => {
const expected = `${encAddress1}${encAddress2}`;
const tokens = [tokenAddress1, tokenAddress2];
expect(Encoder.encode(tokens)).to.equal(expected);
});
it('encodes fixed array of dynamic array addresses', () => {
const tokens1 = new Token('array', [tokenAddress1, tokenAddress2]);
const tokens2 = new Token('array', [tokenAddress3, tokenAddress4]);
const fixed = new Token('fixedArray', [tokens1, tokens2]);
const expected = `${padU32(0x40)}${padU32(0xa0)}${padU32(2)}${encAddress1}${encAddress2}${padU32(2)}${encAddress3}${encAddress4}`;
expect(Encoder.encode([fixed])).to.equal(expected);
});
it('encodes dynamic array of fixed array addresses', () => {
const tokens1 = new Token('fixedArray', [tokenAddress1, tokenAddress2]);
const tokens2 = new Token('fixedArray', [tokenAddress3, tokenAddress4]);
const dynamic = new Token('array', [tokens1, tokens2]);
const expected = `${padU32(0x20)}${padU32(2)}${encAddress1}${encAddress2}${encAddress3}${encAddress4}`;
expect(Encoder.encode([dynamic])).to.equal(expected);
});
it('encodes dynamic array of dynamic array addresses', () => {
const tokens1 = new Token('array', [tokenAddress1]);
const tokens2 = new Token('array', [tokenAddress2]);
const dynamic = new Token('array', [tokens1, tokens2]);
const expected = `${padU32(0x20)}${padU32(2)}${padU32(0x80)}${padU32(0xc0)}${padU32(1)}${encAddress1}${padU32(1)}${encAddress2}`;
expect(Encoder.encode([dynamic])).to.equal(expected);
});
it('encodes dynamic array of dynamic array addresses (2)', () => {
const tokens1 = new Token('array', [tokenAddress1, tokenAddress2]);
const tokens2 = new Token('array', [tokenAddress3, tokenAddress4]);
const dynamic = new Token('array', [tokens1, tokens2]);
const expected = `${padU32(0x20)}${padU32(2)}${padU32(0x80)}${padU32(0xe0)}${padU32(2)}${encAddress1}${encAddress2}${padU32(2)}${encAddress3}${encAddress4}`;
expect(Encoder.encode([dynamic])).to.equal(expected);
});
it('encodes fixed array of fixed array addresses', () => {
const tokens1 = new Token('fixedArray', [tokenAddress1, tokenAddress2]);
const tokens2 = new Token('fixedArray', [tokenAddress3, tokenAddress4]);
const dynamic = new Token('fixedArray', [tokens1, tokens2]);
const expected = `${encAddress1}${encAddress2}${encAddress3}${encAddress4}`;
expect(Encoder.encode([dynamic])).to.equal(expected);
});
});
describe('bytes', () => {
const bytes1 = '0x1234';
const bytes2 = '0x10000000000000000000000000000000000000000000000000000000000002';
const bytes3 = '0x1000000000000000000000000000000000000000000000000000000000000000';
it('encodes fixed bytes', () => {
const token = new Token('fixedBytes', bytes1);
expect(Encoder.encode([token])).to.equal(padFixedBytes(bytes1));
});
it('encodes bytes', () => {
const token = new Token('bytes', bytes1);
expect(Encoder.encode([token])).to.equal(`${padU32(0x20)}${padU32(2)}${padFixedBytes(bytes1)}`);
});
it('encodes bytes (short of boundary)', () => {
const token = new Token('bytes', bytes2);
expect(Encoder.encode([token])).to.equal(`${padU32(0x20)}${padU32(0x1f)}${padFixedBytes(bytes2)}`);
});
it('encodes bytes (two blocks)', () => {
const input = `${bytes3}${bytes3.slice(-64)}`;
const token = new Token('bytes', input);
expect(Encoder.encode([token])).to.equal(`${padU32(0x20)}${padU32(0x40)}${padFixedBytes(input)}`);
});
it('encodes two consecutive bytes', () => {
const in1 = '0x10000000000000000000000000000000000000000000000000000000000002';
const in2 = '0x0010000000000000000000000000000000000000000000000000000000000002';
const tokens = [new Token('bytes', in1), new Token('bytes', in2)];
expect(Encoder.encode(tokens)).to.equal(`${padU32(0x40)}${padU32(0x80)}${padU32(0x1f)}${padFixedBytes(in1)}${padU32(0x20)}${padFixedBytes(in2)}`);
});
});
describe('string', () => {
it('encodes a string', () => {
const string = 'gavofyork';
const stringEnc = padFixedBytes('0x6761766f66796f726b');
const token = new Token('string', string);
expect(Encoder.encode([token])).to.equal(`${padU32(0x20)}${padU32(string.length.toString(16))}${stringEnc}`);
});
});
describe('uint', () => {
it('encodes a uint', () => {
const token = new Token('uint', 4);
expect(Encoder.encode([token])).to.equal(padU32(4));
});
});
describe('int', () => {
it('encodes a int', () => {
const token = new Token('int', 4);
expect(Encoder.encode([token])).to.equal(padU32(4));
});
});
describe('bool', () => {
it('encodes a bool (true)', () => {
const token = new Token('bool', true);
expect(Encoder.encode([token])).to.equal(padU32(1));
});
it('encodes a bool (false)', () => {
const token = new Token('bool', false);
expect(Encoder.encode([token])).to.equal(padU32(0));
});
});
describe('comprehensive test', () => {
it('encodes a complex sequence', () => {
const bytes = '0x131a3afc00d1b1e3461b955e53fc866dcf303b3eb9f4c16f89e388930f48134b131a3afc00d1b1e3461b955e53fc866dcf303b3eb9f4c16f89e388930f48134b';
const tokens = [new Token('int', 5), new Token('bytes', bytes), new Token('int', 3), new Token('bytes', bytes)];
expect(Encoder.encode(tokens)).to.equal(`${padU32(5)}${padU32(0x80)}${padU32(3)}${padU32(0xe0)}${padU32(0x40)}${bytes.substr(2)}${padU32(0x40)}${bytes.substr(2)}`);
});
it('encodes a complex sequence (nested)', () => {
const array = [new Token('int', 5), new Token('int', 6), new Token('int', 7)];
const tokens = [new Token('int', 1), new Token('string', 'gavofyork'), new Token('int', 2), new Token('int', 3), new Token('int', 4), new Token('array', array)];
const stringEnc = padFixedBytes('0x6761766f66796f726b');
expect(Encoder.encode(tokens)).to.equal(`${padU32(1)}${padU32(0xc0)}${padU32(2)}${padU32(3)}${padU32(4)}${padU32(0x100)}${padU32(9)}${stringEnc}${padU32(3)}${padU32(5)}${padU32(6)}${padU32(7)}`);
});
});
});
});

View File

@@ -0,0 +1,17 @@
// Copyright 2015, 2016 Ethcore (UK) Ltd.
// This file is part of Parity.
// Parity is free software: you can redistribute it and/or modify
// it under the terms of the GNU General Public License as published by
// the Free Software Foundation, either version 3 of the License, or
// (at your option) any later version.
// Parity is distributed in the hope that it will be useful,
// but WITHOUT ANY WARRANTY; without even the implied warranty of
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
// GNU General Public License for more details.
// You should have received a copy of the GNU General Public License
// along with Parity. If not, see <http://www.gnu.org/licenses/>.
export default from './encoder';

View File

@@ -0,0 +1,142 @@
// Copyright 2015, 2016 Ethcore (UK) Ltd.
// This file is part of Parity.
// Parity is free software: you can redistribute it and/or modify
// it under the terms of the GNU General Public License as published by
// the Free Software Foundation, either version 3 of the License, or
// (at your option) any later version.
// Parity is distributed in the hope that it will be useful,
// but WITHOUT ANY WARRANTY; without even the implied warranty of
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
// GNU General Public License for more details.
// You should have received a copy of the GNU General Public License
// along with Parity. If not, see <http://www.gnu.org/licenses/>.
const TYPES = ['raw', 'prefixed', 'fixedArray', 'array'];
import { padU32 } from '../util/pad';
export default class Mediate {
constructor (type, value) {
Mediate.validateType(type);
this._type = type;
this._value = value;
}
initLength () {
switch (this._type) {
case 'raw':
return this._value.length / 2;
case 'array':
case 'prefixed':
return 32;
case 'fixedArray':
return this._value
.reduce((total, mediate) => {
return total + mediate.initLength();
}, 0);
}
}
closingLength () {
switch (this._type) {
case 'raw':
return 0;
case 'prefixed':
return this._value.length / 2;
case 'array':
return this._value
.reduce((total, mediate) => {
return total + mediate.initLength();
}, 32);
case 'fixedArray':
return this._value
.reduce((total, mediate) => {
return total + mediate.initLength() + mediate.closingLength();
}, 0);
}
}
init (suffixOffset) {
switch (this._type) {
case 'raw':
return this._value;
case 'fixedArray':
return this._value
.map((mediate, idx) => mediate.init(Mediate.offsetFor(this._value, idx)).toString(16))
.join('');
case 'prefixed':
case 'array':
return padU32(suffixOffset);
}
}
closing (offset) {
switch (this._type) {
case 'raw':
return '';
case 'prefixed':
return this._value;
case 'fixedArray':
return this._value
.map((mediate, idx) => mediate.closing(Mediate.offsetFor(this._value, idx)).toString(16))
.join('');
case 'array':
const prefix = padU32(this._value.length);
const inits = this._value
.map((mediate, idx) => mediate.init(offset + Mediate.offsetFor(this._value, idx) + 32).toString(16))
.join('');
const closings = this._value
.map((mediate, idx) => mediate.closing(offset + Mediate.offsetFor(this._value, idx)).toString(16))
.join('');
return `${prefix}${inits}${closings}`;
}
}
get type () {
return this._type;
}
get value () {
return this._value;
}
static offsetFor (mediates, position) {
if (position < 0 || position >= mediates.length) {
throw new Error(`Invalid position ${position} specified for Mediate.offsetFor`);
}
const initLength = mediates
.reduce((total, mediate) => {
return total + mediate.initLength();
}, 0);
return mediates
.slice(0, position)
.reduce((total, mediate) => {
return total + mediate.closingLength();
}, initLength);
}
static validateType (type) {
if (TYPES.filter((_type) => type === _type).length) {
return true;
}
throw new Error(`Invalid type ${type} received for Mediate.validateType`);
}
}

View File

@@ -0,0 +1,105 @@
// Copyright 2015, 2016 Ethcore (UK) Ltd.
// This file is part of Parity.
// Parity is free software: you can redistribute it and/or modify
// it under the terms of the GNU General Public License as published by
// the Free Software Foundation, either version 3 of the License, or
// (at your option) any later version.
// Parity is distributed in the hope that it will be useful,
// but WITHOUT ANY WARRANTY; without even the implied warranty of
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
// GNU General Public License for more details.
// You should have received a copy of the GNU General Public License
// along with Parity. If not, see <http://www.gnu.org/licenses/>.
import Mediate from './mediate';
describe('abi/encoder/Mediate', () => {
const LONG15 = '1234567890abcdef000000000000000000000000000000000000000000000000';
const DOUBLE15 = `${LONG15}${LONG15}`;
const ARRAY = [new Mediate('raw', DOUBLE15), new Mediate('raw', LONG15)];
describe('validateType', () => {
it('validates raw', () => {
expect(Mediate.validateType('raw')).to.be.true;
});
it('validates prefixed', () => {
expect(Mediate.validateType('prefixed')).to.be.true;
});
it('validates fixedArray', () => {
expect(Mediate.validateType('fixedArray')).to.be.true;
});
it('validates array', () => {
expect(Mediate.validateType('array')).to.be.true;
});
it('throws an error on invalid types', () => {
expect(() => Mediate.validateType('noMatch')).to.throw(/noMatch/);
});
});
describe('offsetFor', () => {
it('thows an error when offset < 0', () => {
expect(() => Mediate.offsetFor([1], -1)).to.throw(/Invalid position/);
});
it('throws an error when offset >= length', () => {
expect(() => Mediate.offsetFor([1], 1)).to.throw(/Invalid position/);
});
});
describe('constructor', () => {
it('throws an error on invalid types', () => {
expect(() => new Mediate('noMatch', '1')).to.throw(/noMatch/);
});
it('sets the type of the object', () => {
expect((new Mediate('raw', '1')).type).to.equal('raw');
});
it('sets the value of the object', () => {
expect((new Mediate('raw', '1')).value).to.equal('1');
});
});
describe('initLength', () => {
it('returns correct variable byte length for raw', () => {
expect(new Mediate('raw', DOUBLE15).initLength()).to.equal(64);
});
it('returns correct fixed byte length for array', () => {
expect(new Mediate('array', [1, 2, 3, 4]).initLength()).to.equal(32);
});
it('returns correct fixed byte length for prefixed', () => {
expect(new Mediate('prefixed', 0).initLength()).to.equal(32);
});
it('returns correct variable byte length for fixedArray', () => {
expect(new Mediate('fixedArray', ARRAY).initLength()).to.equal(96);
});
});
describe('closingLength', () => {
it('returns 0 byte length for raw', () => {
expect(new Mediate('raw', DOUBLE15).closingLength()).to.equal(0);
});
it('returns prefix + size for prefixed', () => {
expect(new Mediate('prefixed', DOUBLE15).closingLength()).to.equal(64);
});
it('returns prefix + size for array', () => {
expect(new Mediate('array', ARRAY).closingLength()).to.equal(128);
});
it('returns total length for fixedArray', () => {
expect(new Mediate('fixedArray', ARRAY).closingLength()).to.equal(96);
});
});
});

17
js/src/abi/index.js Normal file
View File

@@ -0,0 +1,17 @@
// Copyright 2015, 2016 Ethcore (UK) Ltd.
// This file is part of Parity.
// Parity is free software: you can redistribute it and/or modify
// it under the terms of the GNU General Public License as published by
// the Free Software Foundation, either version 3 of the License, or
// (at your option) any later version.
// Parity is distributed in the hope that it will be useful,
// but WITHOUT ANY WARRANTY; without even the implied warranty of
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
// GNU General Public License for more details.
// You should have received a copy of the GNU General Public License
// along with Parity. If not, see <http://www.gnu.org/licenses/>.
export default from './abi';

View File

@@ -0,0 +1,36 @@
// Copyright 2015, 2016 Ethcore (UK) Ltd.
// This file is part of Parity.
// Parity is free software: you can redistribute it and/or modify
// it under the terms of the GNU General Public License as published by
// the Free Software Foundation, either version 3 of the License, or
// (at your option) any later version.
// Parity is distributed in the hope that it will be useful,
// but WITHOUT ANY WARRANTY; without even the implied warranty of
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
// GNU General Public License for more details.
// You should have received a copy of the GNU General Public License
// along with Parity. If not, see <http://www.gnu.org/licenses/>.
import Encoder from '../encoder/encoder';
import Param from './param';
export default class Constructor {
constructor (abi) {
this._inputs = Param.toParams(abi.inputs || []);
}
get inputs () {
return this._inputs;
}
inputParamTypes () {
return this._inputs.map((input) => input.kind);
}
encodeCall (tokens) {
return Encoder.encode(tokens);
}
}

View File

@@ -0,0 +1,52 @@
// Copyright 2015, 2016 Ethcore (UK) Ltd.
// This file is part of Parity.
// Parity is free software: you can redistribute it and/or modify
// it under the terms of the GNU General Public License as published by
// the Free Software Foundation, either version 3 of the License, or
// (at your option) any later version.
// Parity is distributed in the hope that it will be useful,
// but WITHOUT ANY WARRANTY; without even the implied warranty of
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
// GNU General Public License for more details.
// You should have received a copy of the GNU General Public License
// along with Parity. If not, see <http://www.gnu.org/licenses/>.
import Constructor from './constructor';
import Param from './param';
import Token from '../token';
describe('abi/spec/Constructor', () => {
const inputsArr = [{ name: 'boolin', type: 'bool' }, { name: 'stringin', type: 'string' }];
const bool = new Param('boolin', 'bool');
const string = new Param('stringin', 'string');
const inputs = [bool, string];
const cr = new Constructor({ inputs: inputsArr });
describe('constructor', () => {
it('stores the inputs as received', () => {
expect(cr.inputs).to.deep.equal(inputs);
});
it('matches empty inputs with []', () => {
expect(new Constructor({}).inputs).to.deep.equal([]);
});
});
describe('inputParamTypes', () => {
it('retrieves the input types as received', () => {
expect(cr.inputParamTypes()).to.deep.equal([bool.kind, string.kind]);
});
});
describe('encodeCall', () => {
it('encodes correctly', () => {
const result = cr.encodeCall([new Token('bool', true), new Token('string', 'jacogr')]);
expect(result).to.equal('0000000000000000000000000000000000000000000000000000000000000001000000000000000000000000000000000000000000000000000000000000004000000000000000000000000000000000000000000000000000000000000000066a61636f67720000000000000000000000000000000000000000000000000000');
});
});
});

View File

@@ -0,0 +1,30 @@
// Copyright 2015, 2016 Ethcore (UK) Ltd.
// This file is part of Parity.
// Parity is free software: you can redistribute it and/or modify
// it under the terms of the GNU General Public License as published by
// the Free Software Foundation, either version 3 of the License, or
// (at your option) any later version.
// Parity is distributed in the hope that it will be useful,
// but WITHOUT ANY WARRANTY; without even the implied warranty of
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
// GNU General Public License for more details.
// You should have received a copy of the GNU General Public License
// along with Parity. If not, see <http://www.gnu.org/licenses/>.
export default class DecodedLog {
constructor (params, address) {
this._params = params;
this._address = address;
}
get address () {
return this._address;
}
get params () {
return this._params;
}
}

View File

@@ -0,0 +1,28 @@
// Copyright 2015, 2016 Ethcore (UK) Ltd.
// This file is part of Parity.
// Parity is free software: you can redistribute it and/or modify
// it under the terms of the GNU General Public License as published by
// the Free Software Foundation, either version 3 of the License, or
// (at your option) any later version.
// Parity is distributed in the hope that it will be useful,
// but WITHOUT ANY WARRANTY; without even the implied warranty of
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
// GNU General Public License for more details.
// You should have received a copy of the GNU General Public License
// along with Parity. If not, see <http://www.gnu.org/licenses/>.
import DecodedLog from './decodedLog';
const log = new DecodedLog('someParams', 'someAddress');
describe('abi/spec/event/DecodedLog', () => {
describe('constructor', () => {
it('sets internal state', () => {
expect(log.params).to.equal('someParams');
expect(log.address).to.equal('someAddress');
});
});
});

View File

@@ -0,0 +1,45 @@
// Copyright 2015, 2016 Ethcore (UK) Ltd.
// This file is part of Parity.
// Parity is free software: you can redistribute it and/or modify
// it under the terms of the GNU General Public License as published by
// the Free Software Foundation, either version 3 of the License, or
// (at your option) any later version.
// Parity is distributed in the hope that it will be useful,
// but WITHOUT ANY WARRANTY; without even the implied warranty of
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
// GNU General Public License for more details.
// You should have received a copy of the GNU General Public License
// along with Parity. If not, see <http://www.gnu.org/licenses/>.
import ParamType from '../paramType/paramType';
import Token from '../../token/token';
import { isInstanceOf } from '../../util/types';
export default class DecodedLogParam {
constructor (name, kind, token) {
if (!isInstanceOf(kind, ParamType)) {
throw new Error('kind not instanceof ParamType');
} else if (!isInstanceOf(token, Token)) {
throw new Error('token not instanceof Token');
}
this._name = name;
this._kind = kind;
this._token = token;
}
get name () {
return this._name;
}
get kind () {
return this._kind;
}
get token () {
return this._token;
}
}

View File

@@ -0,0 +1,42 @@
// Copyright 2015, 2016 Ethcore (UK) Ltd.
// This file is part of Parity.
// Parity is free software: you can redistribute it and/or modify
// it under the terms of the GNU General Public License as published by
// the Free Software Foundation, either version 3 of the License, or
// (at your option) any later version.
// Parity is distributed in the hope that it will be useful,
// but WITHOUT ANY WARRANTY; without even the implied warranty of
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
// GNU General Public License for more details.
// You should have received a copy of the GNU General Public License
// along with Parity. If not, see <http://www.gnu.org/licenses/>.
import DecodedLogParam from './decodedLogParam';
import ParamType from '../paramType';
import Token from '../../token';
describe('abi/spec/event/DecodedLogParam', () => {
describe('constructor', () => {
const pt = new ParamType('bool');
const tk = new Token('bool');
it('disallows kind not instanceof ParamType', () => {
expect(() => new DecodedLogParam('test', 'param')).to.throw(/ParamType/);
});
it('disallows token not instanceof Token', () => {
expect(() => new DecodedLogParam('test', pt, 'token')).to.throw(/Token/);
});
it('stores all parameters received', () => {
const log = new DecodedLogParam('test', pt, tk);
expect(log.name).to.equal('test');
expect(log.kind).to.equal(pt);
expect(log.token).to.equal(tk);
});
});
});

View File

@@ -0,0 +1,113 @@
// Copyright 2015, 2016 Ethcore (UK) Ltd.
// This file is part of Parity.
// Parity is free software: you can redistribute it and/or modify
// it under the terms of the GNU General Public License as published by
// the Free Software Foundation, either version 3 of the License, or
// (at your option) any later version.
// Parity is distributed in the hope that it will be useful,
// but WITHOUT ANY WARRANTY; without even the implied warranty of
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
// GNU General Public License for more details.
// You should have received a copy of the GNU General Public License
// along with Parity. If not, see <http://www.gnu.org/licenses/>.
import Decoder from '../../decoder/decoder';
import DecodedLog from './decodedLog';
import DecodedLogParam from './decodedLogParam';
import EventParam from './eventParam';
import { asAddress } from '../../util/sliceAs';
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());
this._id = id;
this._signature = signature;
}
get name () {
return this._name;
}
get id () {
return this._id;
}
get inputs () {
return this._inputs;
}
get anonymous () {
return this._anonymous;
}
get signature () {
return this._signature;
}
inputParamTypes () {
return this._inputs.map((input) => input.kind);
}
inputParamNames () {
return this._inputs.map((input) => input.name);
}
indexedParams (indexed) {
return this._inputs.filter((input) => input.indexed === indexed);
}
decodeLog (topics, data) {
const topicParams = this.indexedParams(true);
const dataParams = this.indexedParams(false);
let address;
let toSkip;
if (!this.anonymous) {
address = asAddress(topics[0]);
toSkip = 1;
} else {
toSkip = 0;
}
const topicTypes = topicParams.map((param) => param.kind);
const flatTopics = topics
.filter((topic, idx) => idx >= toSkip)
.map((topic) => {
return (topic.substr(0, 2) === '0x')
? topic.substr(2)
: topic;
}).join('');
const topicTokens = Decoder.decode(topicTypes, flatTopics);
if (topicTokens.length !== (topics.length - toSkip)) {
throw new Error('Invalid topic data');
}
const dataTypes = dataParams.map((param) => param.kind);
const dataTokens = Decoder.decode(dataTypes, data);
const namedTokens = {};
topicParams.forEach((param, idx) => {
namedTokens[param.name] = topicTokens[idx];
});
dataParams.forEach((param, idx) => {
namedTokens[param.name] = dataTokens[idx];
});
const inputParamTypes = this.inputParamTypes();
const decodedParams = this.inputParamNames()
.map((name, idx) => new DecodedLogParam(name, inputParamTypes[idx], namedTokens[name]));
return new DecodedLog(decodedParams, address);
}
}

View File

@@ -0,0 +1,111 @@
// Copyright 2015, 2016 Ethcore (UK) Ltd.
// This file is part of Parity.
// Parity is free software: you can redistribute it and/or modify
// it under the terms of the GNU General Public License as published by
// the Free Software Foundation, either version 3 of the License, or
// (at your option) any later version.
// Parity is distributed in the hope that it will be useful,
// but WITHOUT ANY WARRANTY; without even the implied warranty of
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
// GNU General Public License for more details.
// You should have received a copy of the GNU General Public License
// along with Parity. If not, see <http://www.gnu.org/licenses/>.
import BigNumber from 'bignumber.js';
import Event from './event';
import EventParam from './eventParam';
import DecodedLogParam from './decodedLogParam';
import ParamType from '../paramType';
import Token from '../../token';
describe('abi/spec/event/Event', () => {
const inputArr = [{ name: 'a', type: 'bool' }, { name: 'b', type: 'uint', indexed: true }];
const inputs = [new EventParam('a', 'bool', false), new EventParam('b', 'uint', true)];
const event = new Event({ name: 'test', inputs: inputArr, anonymous: true });
describe('constructor', () => {
it('stores the parameters as received', () => {
expect(event.name).to.equal('test');
expect(event.inputs).to.deep.equal(inputs);
expect(event.anonymous).to.be.true;
});
it('matches empty inputs with []', () => {
expect(new Event({ name: 'test' }).inputs).to.deep.equal([]);
});
it('sets the event signature', () => {
expect(new Event({ name: 'baz' }).signature)
.to.equal('a7916fac4f538170f7cd12c148552e2cba9fcd72329a2dd5b07a6fa906488ddf');
});
});
describe('inputParamTypes', () => {
it('returns all the types', () => {
expect(event.inputParamTypes()).to.deep.equal([new ParamType('bool'), new ParamType('uint', null, 256, true)]);
});
});
describe('inputParamNames', () => {
it('returns all the names', () => {
expect(event.inputParamNames()).to.deep.equal(['a', 'b']);
});
});
describe('indexedParams', () => {
it('returns all indexed parameters (indexed)', () => {
expect(event.indexedParams(true)).to.deep.equal([inputs[1]]);
});
it('returns all indexed parameters (non-indexed)', () => {
expect(event.indexedParams(false)).to.deep.equal([inputs[0]]);
});
});
describe('decodeLog', () => {
it('decodes an event', () => {
const event = new Event({
name: 'foo',
inputs: [
{ name: 'a', type: 'int' },
{ name: 'b', type: 'int', indexed: true },
{ name: 'c', type: 'address' },
{ name: 'd', type: 'address', indexed: true }
]
});
const decoded = event.decodeLog([
'0000000000000000000000004444444444444444444444444444444444444444',
'0000000000000000000000000000000000000000000000000000000000000002',
'0000000000000000000000001111111111111111111111111111111111111111' ],
'00000000000000000000000000000000000000000000000000000000000000030000000000000000000000002222222222222222222222222222222222222222');
expect(decoded.address).to.equal('0x4444444444444444444444444444444444444444');
expect(decoded.params).to.deep.equal([
new DecodedLogParam('a', new ParamType('int', null, 256), new Token('int', new BigNumber(3))),
new DecodedLogParam('b', new ParamType('int', null, 256, true), new Token('int', new BigNumber(2))),
new DecodedLogParam('c', new ParamType('address'), new Token('address', '0x2222222222222222222222222222222222222222')),
new DecodedLogParam('d', new ParamType('address', null, 0, true), new Token('address', '0x1111111111111111111111111111111111111111'))
]);
});
it('decodes an anonymous event', () => {
const event = new Event({ name: 'foo', inputs: [{ name: 'a', type: 'int' }], anonymous: true });
const decoded = event.decodeLog([], '0000000000000000000000000000000000000000000000000000000000000003');
expect(decoded.address).to.not.be.ok;
expect(decoded.params).to.deep.equal([
new DecodedLogParam('a', new ParamType('int', null, 256), new Token('int', new BigNumber(3)))
]);
});
it('throws on invalid topics', () => {
const event = new Event({ name: 'foo', inputs: [{ name: 'a', type: 'int' }], anonymous: true });
expect(() => event.decodeLog(['0000000000000000000000004444444444444444444444444444444444444444'], '0000000000000000000000000000000000000000000000000000000000000003')).to.throw(/Invalid/);
});
});
});

View File

@@ -0,0 +1,41 @@
// Copyright 2015, 2016 Ethcore (UK) Ltd.
// This file is part of Parity.
// Parity is free software: you can redistribute it and/or modify
// it under the terms of the GNU General Public License as published by
// the Free Software Foundation, either version 3 of the License, or
// (at your option) any later version.
// Parity is distributed in the hope that it will be useful,
// but WITHOUT ANY WARRANTY; without even the implied warranty of
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
// GNU General Public License for more details.
// You should have received a copy of the GNU General Public License
// along with Parity. If not, see <http://www.gnu.org/licenses/>.
import { toParamType } from '../paramType/format';
export default class EventParam {
constructor (name, type, indexed = false) {
this._name = name;
this._indexed = indexed;
this._kind = toParamType(type, indexed);
}
get name () {
return this._name;
}
get kind () {
return this._kind;
}
get indexed () {
return this._indexed;
}
static toEventParams (params) {
return params.map((param) => new EventParam(param.name, param.type, param.indexed));
}
}

View File

@@ -0,0 +1,43 @@
// Copyright 2015, 2016 Ethcore (UK) Ltd.
// This file is part of Parity.
// Parity is free software: you can redistribute it and/or modify
// it under the terms of the GNU General Public License as published by
// the Free Software Foundation, either version 3 of the License, or
// (at your option) any later version.
// Parity is distributed in the hope that it will be useful,
// but WITHOUT ANY WARRANTY; without even the implied warranty of
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
// GNU General Public License for more details.
// You should have received a copy of the GNU General Public License
// along with Parity. If not, see <http://www.gnu.org/licenses/>.
import EventParam from './eventParam';
describe('abi/spec/event/EventParam', () => {
describe('constructor', () => {
it('sets the properties', () => {
const param = new EventParam('foo', 'uint', true);
expect(param.name).to.equal('foo');
expect(param.kind.type).to.equal('uint');
expect(param.indexed).to.be.true;
});
it('uses defaults for indexed', () => {
expect(new EventParam('foo', 'uint').indexed).to.be.false;
});
});
describe('toEventParams', () => {
it('maps an array of params', () => {
const params = EventParam.toEventParams([{ name: 'foo', type: 'uint' }]);
expect(params.length).to.equal(1);
expect(params[0].indexed).to.be.false;
expect(params[0].name).to.equal('foo');
expect(params[0].kind.type).to.equal('uint');
});
});
});

View File

@@ -0,0 +1,17 @@
// Copyright 2015, 2016 Ethcore (UK) Ltd.
// This file is part of Parity.
// Parity is free software: you can redistribute it and/or modify
// it under the terms of the GNU General Public License as published by
// the Free Software Foundation, either version 3 of the License, or
// (at your option) any later version.
// Parity is distributed in the hope that it will be useful,
// but WITHOUT ANY WARRANTY; without even the implied warranty of
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
// GNU General Public License for more details.
// You should have received a copy of the GNU General Public License
// along with Parity. If not, see <http://www.gnu.org/licenses/>.
export default from './event';

View File

@@ -0,0 +1,87 @@
// Copyright 2015, 2016 Ethcore (UK) Ltd.
// This file is part of Parity.
// Parity is free software: you can redistribute it and/or modify
// it under the terms of the GNU General Public License as published by
// the Free Software Foundation, either version 3 of the License, or
// (at your option) any later version.
// Parity is distributed in the hope that it will be useful,
// but WITHOUT ANY WARRANTY; without even the implied warranty of
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
// GNU General Public License for more details.
// You should have received a copy of the GNU General Public License
// along with Parity. If not, see <http://www.gnu.org/licenses/>.
import Decoder from '../decoder/decoder';
import Encoder from '../encoder/encoder';
import Param from './param';
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());
this._id = id;
this._signature = signature;
}
get abi () {
return this._abi;
}
get constant () {
return this._constant;
}
get name () {
return this._name;
}
get id () {
return this._id;
}
get payable () {
return this._payable;
}
get inputs () {
return this._inputs;
}
get outputs () {
return this._outputs;
}
get signature () {
return this._signature;
}
inputParamTypes () {
return this._inputs.map((input) => input.kind);
}
outputParamTypes () {
return this._outputs.map((output) => output.kind);
}
encodeCall (tokens) {
return `${this._signature}${Encoder.encode(tokens)}`;
}
decodeInput (data) {
return Decoder.decode(this.inputParamTypes(), data);
}
decodeOutput (data) {
return Decoder.decode(this.outputParamTypes(), data);
}
}

View File

@@ -0,0 +1,89 @@
// Copyright 2015, 2016 Ethcore (UK) Ltd.
// This file is part of Parity.
// Parity is free software: you can redistribute it and/or modify
// it under the terms of the GNU General Public License as published by
// the Free Software Foundation, either version 3 of the License, or
// (at your option) any later version.
// Parity is distributed in the hope that it will be useful,
// but WITHOUT ANY WARRANTY; without even the implied warranty of
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
// GNU General Public License for more details.
// You should have received a copy of the GNU General Public License
// along with Parity. If not, see <http://www.gnu.org/licenses/>.
import Func from './function';
import Param from './param';
import Token from '../token';
describe('abi/spec/Function', () => {
const inputsArr = [{ name: 'boolin', type: 'bool' }, { name: 'stringin', type: 'string' }];
const outputsArr = [{ name: 'output', type: 'uint' }];
const uint = new Param('output', 'uint');
const bool = new Param('boolin', 'bool');
const string = new Param('stringin', 'string');
const inputs = [bool, string];
const outputs = [uint];
const func = new Func({
name: 'test',
inputs: inputsArr,
outputs: outputsArr
});
describe('constructor', () => {
it('stores the parameters as received', () => {
expect(func.name).to.equal('test');
expect(func.constant).to.be.false;
expect(func.inputs).to.deep.equal(inputs);
expect(func.outputs).to.deep.equal(outputs);
});
it('matches empty inputs with []', () => {
expect(new Func({ name: 'test', outputs: outputsArr }).inputs).to.deep.equal([]);
});
it('matches empty outputs with []', () => {
expect(new Func({ name: 'test', inputs: inputsArr }).outputs).to.deep.equal([]);
});
it('sets the method signature', () => {
expect(new Func({ name: 'baz' }).signature).to.equal('a7916fac');
});
it('allows constant functions', () => {
expect(new Func({ name: 'baz', constant: true }).constant).to.be.true;
});
});
describe('inputParamTypes', () => {
it('retrieves the input types as received', () => {
expect(func.inputParamTypes()).to.deep.equal([bool.kind, string.kind]);
});
});
describe('outputParamTypes', () => {
it('retrieves the output types as received', () => {
expect(func.outputParamTypes()).to.deep.equal([uint.kind]);
});
});
describe('encodeCall', () => {
it('encodes the call correctly', () => {
const result = func.encodeCall([new Token('bool', true), new Token('string', 'jacogr')]);
expect(result).to.equal('023562050000000000000000000000000000000000000000000000000000000000000001000000000000000000000000000000000000000000000000000000000000004000000000000000000000000000000000000000000000000000000000000000066a61636f67720000000000000000000000000000000000000000000000000000');
});
});
describe('decodeOutput', () => {
it('decodes the result correctly', () => {
const result = func.decodeOutput('1111111111111111111111111111111111111111111111111111111111111111');
expect(result[0].value.toString(16)).to.equal('1111111111111111111111111111111111111111111111111111111111111111');
});
});
});

17
js/src/abi/spec/index.js Normal file
View File

@@ -0,0 +1,17 @@
// Copyright 2015, 2016 Ethcore (UK) Ltd.
// This file is part of Parity.
// Parity is free software: you can redistribute it and/or modify
// it under the terms of the GNU General Public License as published by
// the Free Software Foundation, either version 3 of the License, or
// (at your option) any later version.
// Parity is distributed in the hope that it will be useful,
// but WITHOUT ANY WARRANTY; without even the implied warranty of
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
// GNU General Public License for more details.
// You should have received a copy of the GNU General Public License
// along with Parity. If not, see <http://www.gnu.org/licenses/>.
export default from './interface';

View File

@@ -0,0 +1,73 @@
// Copyright 2015, 2016 Ethcore (UK) Ltd.
// This file is part of Parity.
// Parity is free software: you can redistribute it and/or modify
// it under the terms of the GNU General Public License as published by
// the Free Software Foundation, either version 3 of the License, or
// (at your option) any later version.
// Parity is distributed in the hope that it will be useful,
// but WITHOUT ANY WARRANTY; without even the implied warranty of
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
// GNU General Public License for more details.
// You should have received a copy of the GNU General Public License
// along with Parity. If not, see <http://www.gnu.org/licenses/>.
import Constructor from './constructor';
import Event from './event/event';
import Func from './function';
import Token from '../token';
export default class Interface {
constructor (abi) {
this._interface = Interface.parseABI(abi);
}
get interface () {
return this._interface;
}
get constructors () {
return this._interface.filter((item) => item instanceof Constructor);
}
get events () {
return this._interface.filter((item) => item instanceof Event);
}
get functions () {
return this._interface.filter((item) => item instanceof Func);
}
encodeTokens (paramTypes, values) {
const createToken = function (paramType, value) {
if (paramType.subtype) {
return new Token(paramType.type, value.map((entry) => createToken(paramType.subtype, entry)));
}
return new Token(paramType.type, value);
};
return paramTypes.map((paramType, idx) => createToken(paramType, values[idx]));
}
static parseABI (abi) {
return abi.map((item) => {
switch (item.type) {
case 'constructor':
return new Constructor(item);
case 'event':
return new Event(item);
case 'function':
case 'fallback':
return new Func(item);
default:
throw new Error(`Unknown ABI type ${item.type}`);
}
});
}
}

View File

@@ -0,0 +1,126 @@
// Copyright 2015, 2016 Ethcore (UK) Ltd.
// This file is part of Parity.
// Parity is free software: you can redistribute it and/or modify
// it under the terms of the GNU General Public License as published by
// the Free Software Foundation, either version 3 of the License, or
// (at your option) any later version.
// Parity is distributed in the hope that it will be useful,
// but WITHOUT ANY WARRANTY; without even the implied warranty of
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
// GNU General Public License for more details.
// You should have received a copy of the GNU General Public License
// along with Parity. If not, see <http://www.gnu.org/licenses/>.
import Interface from './interface';
import ParamType from './paramType';
import Token from '../token';
describe('abi/spec/Interface', () => {
const construct = {
type: 'constructor',
inputs: []
};
const event = {
type: 'event',
name: 'Event2',
anonymous: false,
inputs: [{ name: 'a', type: 'uint256', indexed: true }, { name: 'b', type: 'bytes32', indexed: false }]
};
const func = {
type: 'function',
name: 'foo',
inputs: [{ name: 'a', type: 'uint256' }],
outputs: []
};
describe('parseABI', () => {
it('throws on invalid types', () => {
expect(() => Interface.parseABI([{ type: 'noMatch' }])).to.throw(/noMatch/);
});
it('creates constructors', () => {
expect(Interface.parseABI([ construct ])).to.deep.equal([{ _inputs: [] }]);
});
it('creates events', () => {
expect(Interface.parseABI([ event ])[0].name).to.equal('Event2');
});
it('creates functions', () => {
expect(Interface.parseABI([ func ])[0].name).to.equal('foo');
});
it('parse complex interfaces', () => {
expect(Interface.parseABI([ construct, event, func ]).length).to.equal(3);
});
});
describe('constructor', () => {
const int = new Interface([ construct, event, func ]);
it('contains the full interface', () => {
expect(int.interface.length).to.equal(3);
});
it('contains the constructors', () => {
expect(int.constructors.length).to.equal(1);
});
it('contains the events', () => {
expect(int.events.length).to.equal(1);
});
it('contains the functions', () => {
expect(int.functions.length).to.equal(1);
});
});
describe('encodeTokens', () => {
const int = new Interface([ construct, event, func ]);
it('encodes simple types', () => {
expect(
int.encodeTokens(
[new ParamType('bool'), new ParamType('string'), new ParamType('int'), new ParamType('uint')],
[true, 'gavofyork', -123, 123]
)
).to.deep.equal([
new Token('bool', true), new Token('string', 'gavofyork'), new Token('int', -123), new Token('uint', 123)
]);
});
it('encodes array', () => {
expect(
int.encodeTokens(
[new ParamType('array', new ParamType('bool'))],
[[true, false, true]]
)
).to.deep.equal([
new Token('array', [
new Token('bool', true), new Token('bool', false), new Token('bool', true)
])
]);
});
it('encodes simple with array of array', () => {
expect(
int.encodeTokens(
[
new ParamType('bool'),
new ParamType('fixedArray', new ParamType('array', new ParamType('uint')), 2)
],
[true, [[0, 1], [2, 3]]]
)
).to.deep.equal([
new Token('bool', true),
new Token('fixedArray', [
new Token('array', [new Token('uint', 0), new Token('uint', 1)]),
new Token('array', [new Token('uint', 2), new Token('uint', 3)])
])
]);
});
});
});

36
js/src/abi/spec/param.js Normal file
View File

@@ -0,0 +1,36 @@
// Copyright 2015, 2016 Ethcore (UK) Ltd.
// This file is part of Parity.
// Parity is free software: you can redistribute it and/or modify
// it under the terms of the GNU General Public License as published by
// the Free Software Foundation, either version 3 of the License, or
// (at your option) any later version.
// Parity is distributed in the hope that it will be useful,
// but WITHOUT ANY WARRANTY; without even the implied warranty of
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
// GNU General Public License for more details.
// You should have received a copy of the GNU General Public License
// along with Parity. If not, see <http://www.gnu.org/licenses/>.
import { toParamType } from './paramType/format';
export default class Param {
constructor (name, type) {
this._name = name;
this._kind = toParamType(type);
}
get name () {
return this._name;
}
get kind () {
return this._kind;
}
static toParams (params) {
return params.map((param) => new Param(param.name, param.type));
}
}

View File

@@ -0,0 +1,38 @@
// Copyright 2015, 2016 Ethcore (UK) Ltd.
// This file is part of Parity.
// Parity is free software: you can redistribute it and/or modify
// it under the terms of the GNU General Public License as published by
// the Free Software Foundation, either version 3 of the License, or
// (at your option) any later version.
// Parity is distributed in the hope that it will be useful,
// but WITHOUT ANY WARRANTY; without even the implied warranty of
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
// GNU General Public License for more details.
// You should have received a copy of the GNU General Public License
// along with Parity. If not, see <http://www.gnu.org/licenses/>.
import Param from './param';
describe('abi/spec/Param', () => {
describe('constructor', () => {
const param = new Param('foo', 'uint');
it('sets the properties', () => {
expect(param.name).to.equal('foo');
expect(param.kind.type).to.equal('uint');
});
});
describe('toParams', () => {
it('maps an array of params', () => {
const params = Param.toParams([{ name: 'foo', type: 'uint' }]);
expect(params.length).to.equal(1);
expect(params[0].name).to.equal('foo');
expect(params[0].kind.type).to.equal('uint');
});
});
});

View File

@@ -0,0 +1,80 @@
// Copyright 2015, 2016 Ethcore (UK) Ltd.
// This file is part of Parity.
// Parity is free software: you can redistribute it and/or modify
// it under the terms of the GNU General Public License as published by
// the Free Software Foundation, either version 3 of the License, or
// (at your option) any later version.
// Parity is distributed in the hope that it will be useful,
// but WITHOUT ANY WARRANTY; without even the implied warranty of
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
// GNU General Public License for more details.
// You should have received a copy of the GNU General Public License
// along with Parity. If not, see <http://www.gnu.org/licenses/>.
import ParamType from './paramType';
export function toParamType (type, indexed) {
if (type[type.length - 1] === ']') {
const last = type.lastIndexOf('[');
const length = type.substr(last + 1, type.length - last - 2);
const subtype = toParamType(type.substr(0, last));
if (length.length === 0) {
return new ParamType('array', subtype, 0, indexed);
}
return new ParamType('fixedArray', subtype, parseInt(length, 10), indexed);
}
switch (type) {
case 'address':
case 'bool':
case 'bytes':
case 'string':
return new ParamType(type, null, 0, indexed);
case 'int':
case 'uint':
return new ParamType(type, null, 256, indexed);
default:
if (type.indexOf('uint') === 0) {
return new ParamType('uint', null, parseInt(type.substr(4), 10), indexed);
} else if (type.indexOf('int') === 0) {
return new ParamType('int', null, parseInt(type.substr(3), 10), indexed);
} else if (type.indexOf('bytes') === 0) {
return new ParamType('fixedBytes', null, parseInt(type.substr(5), 10), indexed);
}
throw new Error(`Cannot convert ${type} to valid ParamType`);
}
}
export function fromParamType (paramType) {
switch (paramType.type) {
case 'address':
case 'bool':
case 'bytes':
case 'string':
return paramType.type;
case 'int':
case 'uint':
return `${paramType.type}${paramType.length}`;
case 'fixedBytes':
return `bytes${paramType.length}`;
case 'fixedArray':
return `${fromParamType(paramType.subtype)}[${paramType.length}]`;
case 'array':
return `${fromParamType(paramType.subtype)}[]`;
default:
throw new Error(`Cannot convert from ParamType ${paramType.type}`);
}
}

View File

@@ -0,0 +1,228 @@
// Copyright 2015, 2016 Ethcore (UK) Ltd.
// This file is part of Parity.
// Parity is free software: you can redistribute it and/or modify
// it under the terms of the GNU General Public License as published by
// the Free Software Foundation, either version 3 of the License, or
// (at your option) any later version.
// Parity is distributed in the hope that it will be useful,
// but WITHOUT ANY WARRANTY; without even the implied warranty of
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
// GNU General Public License for more details.
// You should have received a copy of the GNU General Public License
// along with Parity. If not, see <http://www.gnu.org/licenses/>.
import ParamType from './paramType';
import { fromParamType, toParamType } from './format';
describe('abi/spec/paramType/format', () => {
describe('fromParamType', () => {
it('errors on invalid types', () => {
expect(() => fromParamType({ type: 'noMatch' })).to.throw(/noMatch/);
});
describe('simple types', () => {
it('converts address to address', () => {
const pt = new ParamType('address');
expect(fromParamType(pt)).to.equal('address');
});
it('converts bool to bool', () => {
const pt = new ParamType('bool');
expect(fromParamType(pt)).to.equal('bool');
});
it('converts bytes to bytes', () => {
const pt = new ParamType('bytes');
expect(fromParamType(pt)).to.equal('bytes');
});
it('converts string to string', () => {
const pt = new ParamType('string');
expect(fromParamType(pt)).to.equal('string');
});
});
describe('length types', () => {
it('converts int32 to int32', () => {
const pt = new ParamType('int', null, 32);
expect(fromParamType(pt)).to.equal('int32');
});
it('converts uint64 to int64', () => {
const pt = new ParamType('uint', null, 64);
expect(fromParamType(pt)).to.equal('uint64');
});
it('converts fixedBytes8 to bytes8', () => {
const pt = new ParamType('fixedBytes', null, 8);
expect(fromParamType(pt)).to.equal('bytes8');
});
});
describe('arrays', () => {
it('converts string[2] to string[2]', () => {
const pt = new ParamType('fixedArray', new ParamType('string'), 2);
expect(fromParamType(pt)).to.equal('string[2]');
});
it('converts bool[] to bool[]', () => {
const pt = new ParamType('array', new ParamType('bool'));
expect(fromParamType(pt)).to.equal('bool[]');
});
it('converts bool[][2] to bool[][2]', () => {
const pt = new ParamType('fixedArray', new ParamType('array', new ParamType('bool')), 2);
expect(fromParamType(pt)).to.equal('bool[][2]');
});
it('converts bool[2][] to bool[2][]', () => {
const pt = new ParamType('array', new ParamType('fixedArray', new ParamType('bool'), 2));
expect(fromParamType(pt)).to.equal('bool[2][]');
});
});
});
describe('toParamType', () => {
it('errors on invalid types', () => {
expect(() => toParamType('noMatch')).to.throw(/noMatch/);
});
describe('simple mapping', () => {
it('converts address to address', () => {
const pt = toParamType('address');
expect(pt.type).to.equal('address');
});
it('converts bool to bool', () => {
const pt = toParamType('bool');
expect(pt.type).to.equal('bool');
});
it('converts bytes to bytes', () => {
const pt = toParamType('bytes');
expect(pt.type).to.equal('bytes');
});
it('converts string to string', () => {
const pt = toParamType('string');
expect(pt.type).to.equal('string');
});
});
describe('number', () => {
it('converts int to int256', () => {
const pt = toParamType('int');
expect(pt.type).to.equal('int');
expect(pt.length).to.equal(256);
});
it('converts uint to uint256', () => {
const pt = toParamType('uint');
expect(pt.type).to.equal('uint');
expect(pt.length).to.equal(256);
});
});
describe('sized types', () => {
it('converts int32 to int32', () => {
const pt = toParamType('int32');
expect(pt.type).to.equal('int');
expect(pt.length).to.equal(32);
});
it('converts uint16 to uint16', () => {
const pt = toParamType('uint32');
expect(pt.type).to.equal('uint');
expect(pt.length).to.equal(32);
});
it('converts bytes8 to fixedBytes8', () => {
const pt = toParamType('bytes8');
expect(pt.type).to.equal('fixedBytes');
expect(pt.length).to.equal(8);
});
});
describe('arrays', () => {
describe('fixed arrays', () => {
it('creates fixed array', () => {
const pt = toParamType('bytes[8]');
expect(pt.type).to.equal('fixedArray');
expect(pt.subtype.type).to.equal('bytes');
expect(pt.length).to.equal(8);
});
it('creates fixed arrays of fixed arrays', () => {
const pt = toParamType('bytes[45][3]');
expect(pt.type).to.equal('fixedArray');
expect(pt.length).to.equal(3);
expect(pt.subtype.type).to.equal('fixedArray');
expect(pt.subtype.length).to.equal(45);
expect(pt.subtype.subtype.type).to.equal('bytes');
});
});
describe('dynamic arrays', () => {
it('creates a dynamic array', () => {
const pt = toParamType('bytes[]');
expect(pt.type).to.equal('array');
expect(pt.subtype.type).to.equal('bytes');
});
it('creates a dynamic array of dynamic arrays', () => {
const pt = toParamType('bool[][]');
expect(pt.type).to.equal('array');
expect(pt.subtype.type).to.equal('array');
expect(pt.subtype.subtype.type).to.equal('bool');
});
});
describe('mixed arrays', () => {
it('creates a fixed dynamic array', () => {
const pt = toParamType('bool[][3]');
expect(pt.type).to.equal('fixedArray');
expect(pt.length).to.equal(3);
expect(pt.subtype.type).to.equal('array');
expect(pt.subtype.subtype.type).to.equal('bool');
});
it('creates a dynamic fixed array', () => {
const pt = toParamType('bool[3][]');
expect(pt.type).to.equal('array');
expect(pt.subtype.type).to.equal('fixedArray');
expect(pt.subtype.length).to.equal(3);
expect(pt.subtype.subtype.type).to.equal('bool');
});
});
});
});
});

View File

@@ -0,0 +1,17 @@
// Copyright 2015, 2016 Ethcore (UK) Ltd.
// This file is part of Parity.
// Parity is free software: you can redistribute it and/or modify
// it under the terms of the GNU General Public License as published by
// the Free Software Foundation, either version 3 of the License, or
// (at your option) any later version.
// Parity is distributed in the hope that it will be useful,
// but WITHOUT ANY WARRANTY; without even the implied warranty of
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
// GNU General Public License for more details.
// You should have received a copy of the GNU General Public License
// along with Parity. If not, see <http://www.gnu.org/licenses/>.
export default from './paramType';

View File

@@ -0,0 +1,52 @@
// Copyright 2015, 2016 Ethcore (UK) Ltd.
// This file is part of Parity.
// Parity is free software: you can redistribute it and/or modify
// it under the terms of the GNU General Public License as published by
// the Free Software Foundation, either version 3 of the License, or
// (at your option) any later version.
// Parity is distributed in the hope that it will be useful,
// but WITHOUT ANY WARRANTY; without even the implied warranty of
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
// GNU General Public License for more details.
// You should have received a copy of the GNU General Public License
// along with Parity. If not, see <http://www.gnu.org/licenses/>.
import TYPES from './types';
export default class ParamType {
constructor (type, subtype = null, length = 0, indexed = false) {
ParamType.validateType(type);
this._type = type;
this._subtype = subtype;
this._length = length;
this._indexed = indexed;
}
get type () {
return this._type;
}
get subtype () {
return this._subtype;
}
get length () {
return this._length;
}
get indexed () {
return this._indexed;
}
static validateType (type) {
if (TYPES.filter((_type) => type === _type).length) {
return true;
}
throw new Error(`Invalid type ${type} received for ParamType`);
}
}

View File

@@ -0,0 +1,87 @@
// Copyright 2015, 2016 Ethcore (UK) Ltd.
// This file is part of Parity.
// Parity is free software: you can redistribute it and/or modify
// it under the terms of the GNU General Public License as published by
// the Free Software Foundation, either version 3 of the License, or
// (at your option) any later version.
// Parity is distributed in the hope that it will be useful,
// but WITHOUT ANY WARRANTY; without even the implied warranty of
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
// GNU General Public License for more details.
// You should have received a copy of the GNU General Public License
// along with Parity. If not, see <http://www.gnu.org/licenses/>.
import ParamType from './paramType';
describe('abi/spec/paramType/ParamType', () => {
describe('validateType', () => {
it('validates address', () => {
expect(ParamType.validateType('address')).to.be.true;
});
it('validates fixedArray', () => {
expect(ParamType.validateType('fixedArray')).to.be.true;
});
it('validates array', () => {
expect(ParamType.validateType('array')).to.be.true;
});
it('validates fixedBytes', () => {
expect(ParamType.validateType('fixedBytes')).to.be.true;
});
it('validates bytes', () => {
expect(ParamType.validateType('bytes')).to.be.true;
});
it('validates bool', () => {
expect(ParamType.validateType('bool')).to.be.true;
});
it('validates int', () => {
expect(ParamType.validateType('int')).to.be.true;
});
it('validates uint', () => {
expect(ParamType.validateType('uint')).to.be.true;
});
it('validates string', () => {
expect(ParamType.validateType('string')).to.be.true;
});
it('throws an error on invalid types', () => {
expect(() => ParamType.validateType('noMatch')).to.throw(/noMatch/);
});
});
describe('constructor', () => {
it('throws an error on invalid types', () => {
expect(() => new ParamType('noMatch')).to.throw(/noMatch/);
});
it('sets the type of the object', () => {
expect((new ParamType('bool', null, 1)).type).to.equal('bool');
});
it('sets the subtype of the object', () => {
expect((new ParamType('array', 'bool', 1)).subtype).to.equal('bool');
});
it('sets the length of the object', () => {
expect((new ParamType('array', 'bool', 1)).length).to.equal(1);
});
it('sets the index of the object', () => {
expect((new ParamType('array', 'bool', 1, true)).indexed).to.be.true;
});
it('sets default values where none supplied', () => {
expect(Object.values(new ParamType('string'))).to.deep.equal(['string', null, 0, false]);
});
});
});

View File

@@ -0,0 +1,19 @@
// Copyright 2015, 2016 Ethcore (UK) Ltd.
// This file is part of Parity.
// Parity is free software: you can redistribute it and/or modify
// it under the terms of the GNU General Public License as published by
// the Free Software Foundation, either version 3 of the License, or
// (at your option) any later version.
// Parity is distributed in the hope that it will be useful,
// but WITHOUT ANY WARRANTY; without even the implied warranty of
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
// GNU General Public License for more details.
// You should have received a copy of the GNU General Public License
// along with Parity. If not, see <http://www.gnu.org/licenses/>.
const TYPES = ['address', 'bytes', 'int', 'uint', 'bool', 'string', 'array', 'fixedBytes', 'fixedArray'];
export default TYPES;

17
js/src/abi/token/index.js Normal file
View File

@@ -0,0 +1,17 @@
// Copyright 2015, 2016 Ethcore (UK) Ltd.
// This file is part of Parity.
// Parity is free software: you can redistribute it and/or modify
// it under the terms of the GNU General Public License as published by
// the Free Software Foundation, either version 3 of the License, or
// (at your option) any later version.
// Parity is distributed in the hope that it will be useful,
// but WITHOUT ANY WARRANTY; without even the implied warranty of
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
// GNU General Public License for more details.
// You should have received a copy of the GNU General Public License
// along with Parity. If not, see <http://www.gnu.org/licenses/>.
export default from './token';

42
js/src/abi/token/token.js Normal file
View File

@@ -0,0 +1,42 @@
// Copyright 2015, 2016 Ethcore (UK) Ltd.
// This file is part of Parity.
// Parity is free software: you can redistribute it and/or modify
// it under the terms of the GNU General Public License as published by
// the Free Software Foundation, either version 3 of the License, or
// (at your option) any later version.
// Parity is distributed in the hope that it will be useful,
// but WITHOUT ANY WARRANTY; without even the implied warranty of
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
// GNU General Public License for more details.
// You should have received a copy of the GNU General Public License
// along with Parity. If not, see <http://www.gnu.org/licenses/>.
import TYPES from '../spec/paramType/types';
export default class Token {
constructor (type, value) {
Token.validateType(type);
this._type = type;
this._value = value;
}
get type () {
return this._type;
}
get value () {
return this._value;
}
static validateType (type) {
if (TYPES.filter((_type) => type === _type).length) {
return true;
}
throw new Error(`Invalid type ${type} received for Token`);
}
}

View File

@@ -0,0 +1,75 @@
// Copyright 2015, 2016 Ethcore (UK) Ltd.
// This file is part of Parity.
// Parity is free software: you can redistribute it and/or modify
// it under the terms of the GNU General Public License as published by
// the Free Software Foundation, either version 3 of the License, or
// (at your option) any later version.
// Parity is distributed in the hope that it will be useful,
// but WITHOUT ANY WARRANTY; without even the implied warranty of
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
// GNU General Public License for more details.
// You should have received a copy of the GNU General Public License
// along with Parity. If not, see <http://www.gnu.org/licenses/>.
import Token from './token';
describe('abi/token/token', () => {
describe('validateType', () => {
it('validates address', () => {
expect(Token.validateType('address')).to.be.true;
});
it('validates fixedArray', () => {
expect(Token.validateType('fixedArray')).to.be.true;
});
it('validates array', () => {
expect(Token.validateType('array')).to.be.true;
});
it('validates fixedBytes', () => {
expect(Token.validateType('fixedBytes')).to.be.true;
});
it('validates bytes', () => {
expect(Token.validateType('bytes')).to.be.true;
});
it('validates bool', () => {
expect(Token.validateType('bool')).to.be.true;
});
it('validates int', () => {
expect(Token.validateType('int')).to.be.true;
});
it('validates uint', () => {
expect(Token.validateType('uint')).to.be.true;
});
it('validates string', () => {
expect(Token.validateType('string')).to.be.true;
});
it('throws an error on invalid types', () => {
expect(() => Token.validateType('noMatch')).to.throw(/noMatch/);
});
});
describe('constructor', () => {
it('throws an error on invalid types', () => {
expect(() => new Token('noMatch', '1')).to.throw(/noMatch/);
});
it('sets the type of the object', () => {
expect((new Token('bool', '1')).type).to.equal('bool');
});
it('sets the value of the object', () => {
expect((new Token('bool', '1')).value).to.equal('1');
});
});
});

View File

@@ -0,0 +1,65 @@
// Copyright 2015, 2016 Ethcore (UK) Ltd.
// This file is part of Parity.
// Parity is free software: you can redistribute it and/or modify
// it under the terms of the GNU General Public License as published by
// the Free Software Foundation, either version 3 of the License, or
// (at your option) any later version.
// Parity is distributed in the hope that it will be useful,
// but WITHOUT ANY WARRANTY; without even the implied warranty of
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
// GNU General Public License for more details.
// You should have received a copy of the GNU General Public License
// along with Parity. If not, see <http://www.gnu.org/licenses/>.
import { keccak_256 } from 'js-sha3'; // eslint-disable-line camelcase
export function isChecksumValid (_address) {
const address = _address.replace('0x', '');
const hash = keccak_256(address.toLowerCase(address));
for (let n = 0; n < 40; n++) {
const hashval = parseInt(hash[n], 16);
const isLower = address[n].toUpperCase() !== address[n];
const isUpper = address[n].toLowerCase() !== address[n];
if ((hashval > 7 && isLower) || (hashval <= 7 && isUpper)) {
return false;
}
}
return true;
}
export function isAddress (address) {
if (address && address.length === 42) {
if (!/^(0x)?[0-9a-f]{40}$/i.test(address)) {
return false;
} else if (/^(0x)?[0-9a-f]{40}$/.test(address) || /^(0x)?[0-9A-F]{40}$/.test(address)) {
return true;
}
return isChecksumValid(address);
}
return false;
}
export function toChecksumAddress (_address) {
const address = (_address || '').toLowerCase();
if (!isAddress(address)) {
return '';
}
const hash = keccak_256(address.slice(-40));
let result = '0x';
for (let n = 0; n < 40; n++) {
result = `${result}${parseInt(hash[n], 16) > 7 ? address[n + 2].toUpperCase() : address[n + 2]}`;
}
return result;
}

View File

@@ -0,0 +1,100 @@
// Copyright 2015, 2016 Ethcore (UK) Ltd.
// This file is part of Parity.
// Parity is free software: you can redistribute it and/or modify
// it under the terms of the GNU General Public License as published by
// the Free Software Foundation, either version 3 of the License, or
// (at your option) any later version.
// Parity is distributed in the hope that it will be useful,
// but WITHOUT ANY WARRANTY; without even the implied warranty of
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
// GNU General Public License for more details.
// You should have received a copy of the GNU General Public License
// along with Parity. If not, see <http://www.gnu.org/licenses/>.
import { isChecksumValid, isAddress, toChecksumAddress } from './address';
describe('abi/util/address', () => {
const value = '63Cf90D3f0410092FC0fca41846f596223979195';
const address = `0x${value}`;
const lowercase = `0x${value.toLowerCase()}`;
const uppercase = `0x${value.toUpperCase()}`;
const invalid = '0x' + value.split('').map((char) => {
if (char >= 'a' && char <= 'f') {
return char.toUpperCase();
} else if (char >= 'A' && char <= 'F') {
return char.toLowerCase();
}
return char;
}).join('');
const invalidhex = '0x01234567890123456789012345678901234567gh';
describe('isChecksumValid', () => {
it('returns false when fully lowercase', () => {
expect(isChecksumValid(lowercase)).to.be.false;
});
it('returns false when fully uppercase', () => {
expect(isChecksumValid(uppercase)).to.be.false;
});
it('returns false on a mixed-case address', () => {
expect(isChecksumValid(invalid)).to.be.false;
});
it('returns true on a checksummed address', () => {
expect(isChecksumValid(address)).to.be.true;
});
});
describe('isAddress', () => {
it('returns true when fully lowercase', () => {
expect(isAddress(lowercase)).to.be.true;
});
it('returns true when fully uppercase', () => {
expect(isAddress(uppercase)).to.be.true;
});
it('returns true when checksummed', () => {
expect(isAddress(address)).to.be.true;
});
it('returns false when invalid checksum', () => {
expect(isAddress(invalid)).to.be.false;
});
it('returns false on valid length, non-hex', () => {
expect(isAddress(invalidhex)).to.be.false;
});
});
describe('toChecksumAddress', () => {
it('returns empty when no address specified', () => {
expect(toChecksumAddress()).to.equal('');
});
it('returns empty on invalid address structure', () => {
expect(toChecksumAddress('0xnotaddress')).to.equal('');
});
it('returns formatted address on checksum input', () => {
expect(toChecksumAddress(address)).to.equal(address);
});
it('returns formatted address on lowercase input', () => {
expect(toChecksumAddress(lowercase)).to.equal(address);
});
it('returns formatted address on uppercase input', () => {
expect(toChecksumAddress(uppercase)).to.equal(address);
});
it('returns formatted address on mixed input', () => {
expect(toChecksumAddress(invalid)).to.equal(address);
});
});
});

75
js/src/abi/util/pad.js Normal file
View File

@@ -0,0 +1,75 @@
// Copyright 2015, 2016 Ethcore (UK) Ltd.
// This file is part of Parity.
// Parity is free software: you can redistribute it and/or modify
// it under the terms of the GNU General Public License as published by
// the Free Software Foundation, either version 3 of the License, or
// (at your option) any later version.
// Parity is distributed in the hope that it will be useful,
// but WITHOUT ANY WARRANTY; without even the implied warranty of
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
// GNU General Public License for more details.
// You should have received a copy of the GNU General Public License
// along with Parity. If not, see <http://www.gnu.org/licenses/>.
import BigNumber from 'bignumber.js';
import utf8 from 'utf8';
import { isArray } from './types';
const ZERO_64 = '0000000000000000000000000000000000000000000000000000000000000000';
export function padAddress (_input) {
const input = _input.substr(0, 2) === '0x' ? _input.substr(2) : _input;
return `${ZERO_64}${input}`.slice(-64);
}
export function padBool (input) {
return `${ZERO_64}${input ? '1' : '0'}`.slice(-64);
}
export function padU32 (input) {
let bn = new BigNumber(input);
if (bn.lessThan(0)) {
bn = new BigNumber('ffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffff', 16)
.plus(bn).plus(1);
}
return `${ZERO_64}${bn.toString(16)}`.slice(-64);
}
function stringToBytes (input) {
if (isArray(input)) {
return input;
} else if (input.substr(0, 2) === '0x') {
return input.substr(2).toLowerCase().match(/.{1,2}/g).map((value) => parseInt(value, 16));
} else {
return input.split('').map((char) => char.charCodeAt(0));
}
}
export function padBytes (_input) {
const input = stringToBytes(_input);
return `${padU32(input.length)}${padFixedBytes(input)}`;
}
export function padFixedBytes (_input) {
const input = stringToBytes(_input);
const sinput = input.map((code) => `0${code.toString(16)}`.slice(-2)).join('');
const max = Math.floor((sinput.length + 63) / 64) * 64;
return `${sinput}${ZERO_64}`.substr(0, max);
}
export function padString (input) {
const array = utf8.encode(input)
.split('')
.map((char) => char.charCodeAt(0));
return padBytes(array);
}

124
js/src/abi/util/pad.spec.js Normal file
View File

@@ -0,0 +1,124 @@
// Copyright 2015, 2016 Ethcore (UK) Ltd.
// This file is part of Parity.
// Parity is free software: you can redistribute it and/or modify
// it under the terms of the GNU General Public License as published by
// the Free Software Foundation, either version 3 of the License, or
// (at your option) any later version.
// Parity is distributed in the hope that it will be useful,
// but WITHOUT ANY WARRANTY; without even the implied warranty of
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
// GNU General Public License for more details.
// You should have received a copy of the GNU General Public License
// along with Parity. If not, see <http://www.gnu.org/licenses/>.
import BigNumber from 'bignumber.js';
import { padAddress, padBool, padBytes, padFixedBytes, padString, padU32 } from './pad';
describe('abi/util/pad', () => {
const SHORT15 = '1234567890abcdef';
const BYTES15 = [0x12, 0x34, 0x56, 0x78, 0x90, 0xab, 0xcd, 0xef];
const LONG15 = `${SHORT15}000000000000000000000000000000000000000000000000`;
const PAD123 = '0000000000000000000000000000000000000000000000000000000000000123';
describe('padAddress', () => {
it('pads to 64 characters', () => {
expect(padAddress('123')).to.equal(PAD123);
});
it('strips leading 0x when passed in', () => {
expect(padFixedBytes(`0x${PAD123}`)).to.equal(PAD123);
});
});
describe('padBool', () => {
const TRUE = '0000000000000000000000000000000000000000000000000000000000000001';
const FALSE = '0000000000000000000000000000000000000000000000000000000000000000';
it('pads true to 64 characters', () => {
expect(padBool(true)).to.equal(TRUE);
});
it('pads false to 64 characters', () => {
expect(padBool(false)).to.equal(FALSE);
});
});
describe('padU32', () => {
it('left pads length < 64 bytes to 64 bytes', () => {
expect(padU32(1)).to.equal('0000000000000000000000000000000000000000000000000000000000000001');
});
it('pads hex representation', () => {
expect(padU32(0x123)).to.equal(PAD123);
});
it('pads decimal representation', () => {
expect(padU32(291)).to.equal(PAD123);
});
it('pads string representation', () => {
expect(padU32('0x123')).to.equal(PAD123);
});
it('pads BigNumber representation', () => {
expect(padU32(new BigNumber(0x123))).to.equal(PAD123);
});
it('converts negative numbers to 2s complement', () => {
expect(padU32(-123)).to.equal('ffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffff85');
});
});
describe('padFixedBytes', () => {
it('right pads length < 64 bytes to 64 bytes (string)', () => {
expect(padFixedBytes(`0x${SHORT15}`)).to.equal(LONG15);
});
it('right pads length < 64 bytes to 64 bytes (array)', () => {
expect(padFixedBytes(BYTES15)).to.equal(LONG15);
});
it('right pads length > 64 bytes (64 byte multiples)', () => {
expect(padFixedBytes(`0x${LONG15}${SHORT15}`)).to.equal(`${LONG15}${LONG15}`);
});
it('strips leading 0x when passed in', () => {
expect(padFixedBytes(`0x${SHORT15}`)).to.equal(LONG15);
});
});
describe('padBytes', () => {
it('right pads length < 64, adding the length (string)', () => {
const result = padBytes(`0x${SHORT15}`);
expect(result.length).to.equal(128);
expect(result).to.equal(`${padU32(8)}${LONG15}`);
});
it('right pads length < 64, adding the length (array)', () => {
const result = padBytes(BYTES15);
expect(result.length).to.equal(128);
expect(result).to.equal(`${padU32(8)}${LONG15}`);
});
it('right pads length > 64, adding the length', () => {
const result = padBytes(`0x${LONG15}${SHORT15}`);
expect(result.length).to.equal(192);
expect(result).to.equal(`${padU32(0x28)}${LONG15}${LONG15}`);
});
});
describe('padString', () => {
it('correctly converts & pads strings', () => {
const result = padString('gavofyork');
expect(result.length).to.equal(128);
expect(result).to.equal(padBytes('0x6761766f66796f726b'));
});
});
});

View File

@@ -0,0 +1,31 @@
// Copyright 2015, 2016 Ethcore (UK) Ltd.
// This file is part of Parity.
// Parity is free software: you can redistribute it and/or modify
// it under the terms of the GNU General Public License as published by
// the Free Software Foundation, either version 3 of the License, or
// (at your option) any later version.
// Parity is distributed in the hope that it will be useful,
// but WITHOUT ANY WARRANTY; without even the implied warranty of
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
// GNU General Public License for more details.
// You should have received a copy of the GNU General Public License
// along with Parity. If not, see <http://www.gnu.org/licenses/>.
import { keccak_256 } from 'js-sha3'; // eslint-disable-line camelcase
import { fromParamType } from '../spec/paramType/format';
export function eventSignature (name, params) {
const types = (params || []).map(fromParamType).join(',');
const id = `${name || ''}(${types})`;
return { id, signature: keccak_256(id) };
}
export function methodSignature (name, params) {
const { id, signature } = eventSignature(name, params);
return { id, signature: signature.substr(0, 8) };
}

View File

@@ -0,0 +1,68 @@
// Copyright 2015, 2016 Ethcore (UK) Ltd.
// This file is part of Parity.
// Parity is free software: you can redistribute it and/or modify
// it under the terms of the GNU General Public License as published by
// the Free Software Foundation, either version 3 of the License, or
// (at your option) any later version.
// Parity is distributed in the hope that it will be useful,
// but WITHOUT ANY WARRANTY; without even the implied warranty of
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
// GNU General Public License for more details.
// You should have received a copy of the GNU General Public License
// along with Parity. If not, see <http://www.gnu.org/licenses/>.
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' });
});
it('encodes signature baz(uint32) correctly', () => {
expect(eventSignature('baz', [{ type: 'uint', length: 32 }]))
.to.deep.equal({ id: 'baz(uint32)', 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' });
});
it('encodes no-name signature correctly as ()', () => {
expect(eventSignature(undefined, []))
.to.deep.equal({ id: '()', signature: '861731d50c3880a2ca1994d5ec287b94b2f4bd832a67d3e41c08177bdd5674fe' });
});
it('encodes no-params signature correctly as ()', () => {
expect(eventSignature(undefined, undefined))
.to.deep.equal({ id: '()', signature: '861731d50c3880a2ca1994d5ec287b94b2f4bd832a67d3e41c08177bdd5674fe' });
});
});
describe('methodSignature', () => {
it('encodes signature baz() correctly', () => {
expect(methodSignature('baz', [])).to.deep.equal({ id: 'baz()', signature: 'a7916fac' });
});
it('encodes signature baz(uint32) correctly', () => {
expect(methodSignature('baz', [{ type: 'uint', length: 32 }])).to.deep.equal({ id: 'baz(uint32)', 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' });
});
it('encodes no-name signature correctly as ()', () => {
expect(methodSignature(undefined, [])).to.deep.equal({ id: '()', signature: '861731d5' });
});
it('encodes no-params signature correctly as ()', () => {
expect(methodSignature(undefined, undefined)).to.deep.equal({ id: '()', signature: '861731d5' });
});
});
});

35
js/src/abi/util/slice.js Normal file
View File

@@ -0,0 +1,35 @@
// Copyright 2015, 2016 Ethcore (UK) Ltd.
// This file is part of Parity.
// Parity is free software: you can redistribute it and/or modify
// it under the terms of the GNU General Public License as published by
// the Free Software Foundation, either version 3 of the License, or
// (at your option) any later version.
// Parity is distributed in the hope that it will be useful,
// but WITHOUT ANY WARRANTY; without even the implied warranty of
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
// GNU General Public License for more details.
// You should have received a copy of the GNU General Public License
// along with Parity. If not, see <http://www.gnu.org/licenses/>.
import { padAddress } from './pad';
export function sliceData (_data) {
if (!_data || !_data.length) {
return [];
}
let data = (_data.substr(0, 2) === '0x') ? _data.substr(2) : _data;
if (!data.length) {
data = padAddress('');
}
if (data.length % 64) {
throw new Error(`Invalid data length (not mod 64) passed to sliceData, ${data}, % 64 == ${data.length % 64}`);
}
return data.match(/.{1,64}/g);
}

View File

@@ -0,0 +1,48 @@
// Copyright 2015, 2016 Ethcore (UK) Ltd.
// This file is part of Parity.
// Parity is free software: you can redistribute it and/or modify
// it under the terms of the GNU General Public License as published by
// the Free Software Foundation, either version 3 of the License, or
// (at your option) any later version.
// Parity is distributed in the hope that it will be useful,
// but WITHOUT ANY WARRANTY; without even the implied warranty of
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
// GNU General Public License for more details.
// You should have received a copy of the GNU General Public License
// along with Parity. If not, see <http://www.gnu.org/licenses/>.
import { sliceData } from './slice';
describe('abi/util/slice', () => {
describe('sliceData', () => {
const slice1 = '131a3afc00d1b1e3461b955e53fc866dcf303b3eb9f4c16f89e388930f48134b';
const slice2 = '2124768576358735263578356373526387638357635873563586353756358763';
it('throws an error on mod 64 != 0', () => {
expect(() => sliceData('123')).to.throw(/sliceData/);
});
it('returns an empty array when length === 0', () => {
expect(sliceData('')).to.deep.equal([]);
});
it('returns an array with the slices otherwise', () => {
const sliced = sliceData(`${slice1}${slice2}`);
expect(sliced.length).to.equal(2);
expect(sliced[0]).to.equal(slice1);
expect(sliced[1]).to.equal(slice2);
});
it('removes leading 0x when passed in', () => {
const sliced = sliceData(`0x${slice1}${slice2}`);
expect(sliced.length).to.equal(2);
expect(sliced[0]).to.equal(slice1);
expect(sliced[1]).to.equal(slice2);
});
});
});

View File

@@ -0,0 +1,47 @@
// Copyright 2015, 2016 Ethcore (UK) Ltd.
// This file is part of Parity.
// Parity is free software: you can redistribute it and/or modify
// it under the terms of the GNU General Public License as published by
// the Free Software Foundation, either version 3 of the License, or
// (at your option) any later version.
// Parity is distributed in the hope that it will be useful,
// but WITHOUT ANY WARRANTY; without even the implied warranty of
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
// GNU General Public License for more details.
// You should have received a copy of the GNU General Public License
// along with Parity. If not, see <http://www.gnu.org/licenses/>.
import BigNumber from 'bignumber.js';
import { toChecksumAddress } from './address';
export function asU32 (slice) {
// TODO: validation
return new BigNumber(slice, 16);
}
export function asI32 (slice) {
if (new BigNumber(slice.substr(0, 1), 16).toString(2)[0] === '1') {
return new BigNumber(slice, 16)
.minus(new BigNumber('ffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffff', 16))
.minus(1);
}
return new BigNumber(slice, 16);
}
export function asAddress (slice) {
// TODO: address validation?
return toChecksumAddress(`0x${slice.slice(-40)}`);
}
export function asBool (slice) {
// TODO: everything else should be 0
return new BigNumber(slice[63]).eq(1);
}

View File

@@ -0,0 +1,54 @@
// Copyright 2015, 2016 Ethcore (UK) Ltd.
// This file is part of Parity.
// Parity is free software: you can redistribute it and/or modify
// it under the terms of the GNU General Public License as published by
// the Free Software Foundation, either version 3 of the License, or
// (at your option) any later version.
// Parity is distributed in the hope that it will be useful,
// but WITHOUT ANY WARRANTY; without even the implied warranty of
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
// GNU General Public License for more details.
// You should have received a copy of the GNU General Public License
// along with Parity. If not, see <http://www.gnu.org/licenses/>.
import { asAddress, asBool, asI32, asU32 } from './sliceAs';
describe('abi/util/sliceAs', () => {
const MAX_INT = 'ffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffff';
describe('asAddress', () => {
it('correctly returns the last 0x40 characters', () => {
const address = '1111111111222222222233333333334444444444';
expect(asAddress(`000000000000000000000000${address}`)).to.equal(`0x${address}`);
});
});
describe('asBool', () => {
it('correctly returns true', () => {
expect(asBool('0000000000000000000000000000000000000000000000000000000000000001')).to.be.true;
});
it('correctly returns false', () => {
expect(asBool('0000000000000000000000000000000000000000000000000000000000000000')).to.be.false;
});
});
describe('asI32', () => {
it('correctly decodes positive numbers', () => {
expect(asI32('000000000000000000000000000000000000000000000000000000000000007b').toString()).to.equal('123');
});
it('correctly decodes negative numbers', () => {
expect(asI32('ffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffff85').toString()).to.equal('-123');
});
});
describe('asU32', () => {
it('returns a maxium U32', () => {
expect(asU32(MAX_INT).toString(16)).to.equal(MAX_INT);
});
});
});

27
js/src/abi/util/types.js Normal file
View File

@@ -0,0 +1,27 @@
// Copyright 2015, 2016 Ethcore (UK) Ltd.
// This file is part of Parity.
// Parity is free software: you can redistribute it and/or modify
// it under the terms of the GNU General Public License as published by
// the Free Software Foundation, either version 3 of the License, or
// (at your option) any later version.
// Parity is distributed in the hope that it will be useful,
// but WITHOUT ANY WARRANTY; without even the implied warranty of
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
// GNU General Public License for more details.
// You should have received a copy of the GNU General Public License
// along with Parity. If not, see <http://www.gnu.org/licenses/>.
export function isArray (test) {
return Object.prototype.toString.call(test) === '[object Array]';
}
export function isString (test) {
return Object.prototype.toString.call(test) === '[object String]';
}
export function isInstanceOf (test, clazz) {
return test instanceof clazz;
}

View File

@@ -0,0 +1,62 @@
// Copyright 2015, 2016 Ethcore (UK) Ltd.
// This file is part of Parity.
// Parity is free software: you can redistribute it and/or modify
// it under the terms of the GNU General Public License as published by
// the Free Software Foundation, either version 3 of the License, or
// (at your option) any later version.
// Parity is distributed in the hope that it will be useful,
// but WITHOUT ANY WARRANTY; without even the implied warranty of
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
// GNU General Public License for more details.
// You should have received a copy of the GNU General Public License
// along with Parity. If not, see <http://www.gnu.org/licenses/>.
import { isArray, isString, isInstanceOf } from './types';
import Token from '../token';
describe('abi/util/types', () => {
describe('isArray', () => {
it('correctly identifies empty arrays as Array', () => {
expect(isArray([])).to.be.true;
});
it('correctly identifies non-empty arrays as Array', () => {
expect(isArray([1, 2, 3])).to.be.true;
});
it('correctly identifies strings as non-Array', () => {
expect(isArray('not an array')).to.be.false;
});
it('correctly identifies objects as non-Array', () => {
expect(isArray({})).to.be.false;
});
});
describe('isString', () => {
it('correctly identifies empty string as string', () => {
expect(isString('')).to.be.true;
});
it('correctly identifies string as string', () => {
expect(isString('123')).to.be.true;
});
});
describe('isInstanceOf', () => {
it('correctly identifies build-in instanceof', () => {
expect(isInstanceOf(new String('123'), String)).to.be.true; // eslint-disable-line no-new-wrappers
});
it('correctly identifies own instanceof', () => {
expect(isInstanceOf(new Token('int', 123), Token)).to.be.true;
});
it('correctly reports false for own', () => {
expect(isInstanceOf({ type: 'int' }, Token)).to.be.false;
});
});
});