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:
65
js/src/abi/util/address.js
Normal file
65
js/src/abi/util/address.js
Normal 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;
|
||||
}
|
||||
100
js/src/abi/util/address.spec.js
Normal file
100
js/src/abi/util/address.spec.js
Normal 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
75
js/src/abi/util/pad.js
Normal 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
124
js/src/abi/util/pad.spec.js
Normal 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'));
|
||||
});
|
||||
});
|
||||
});
|
||||
31
js/src/abi/util/signature.js
Normal file
31
js/src/abi/util/signature.js
Normal 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) };
|
||||
}
|
||||
68
js/src/abi/util/signature.spec.js
Normal file
68
js/src/abi/util/signature.spec.js
Normal 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
35
js/src/abi/util/slice.js
Normal 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);
|
||||
}
|
||||
48
js/src/abi/util/slice.spec.js
Normal file
48
js/src/abi/util/slice.spec.js
Normal 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);
|
||||
});
|
||||
});
|
||||
});
|
||||
47
js/src/abi/util/sliceAs.js
Normal file
47
js/src/abi/util/sliceAs.js
Normal 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);
|
||||
}
|
||||
54
js/src/abi/util/sliceAs.spec.js
Normal file
54
js/src/abi/util/sliceAs.spec.js
Normal 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
27
js/src/abi/util/types.js
Normal 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;
|
||||
}
|
||||
62
js/src/abi/util/types.spec.js
Normal file
62
js/src/abi/util/types.spec.js
Normal 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;
|
||||
});
|
||||
});
|
||||
});
|
||||
Reference in New Issue
Block a user