From cff94cfa837b1d78ffd01d389981ec299166ef63 Mon Sep 17 00:00:00 2001 From: qianbin Date: Wed, 20 Mar 2019 10:31:17 +0800 Subject: [PATCH 1/2] fix(abi): skip keyed decoded field when name is empty --- src/abi.ts | 19 +++++++++++-------- 1 file changed, 11 insertions(+), 8 deletions(-) diff --git a/src/abi.ts b/src/abi.ts index fc4d740..e136484 100644 --- a/src/abi.ts +++ b/src/abi.ts @@ -1,5 +1,6 @@ import { AbiCoder, formatSignature } from 'ethers/utils/abi-coder' import { keccak256 } from './cry' + const coder = (() => { const c = new AbiCoder((type, value) => { if ((type.match(/^u?int/) && !Array.isArray(value) && typeof value !== 'object') || @@ -81,7 +82,9 @@ export namespace abi { const decoded: Decoded = {} types.forEach((t, i) => { decoded[i] = result[i] - decoded[t.name] = result[i] + if (t.name) { + decoded[t.name] = result[i] + } }) return decoded } @@ -199,16 +202,16 @@ export namespace abi { const decoded: Decoded = {} this.definition.inputs.forEach((t, i) => { if (t.indexed) { - if (isDynamicType(t.type)) { - decoded[i] = decoded[t.name] = topics.shift() - } else { - decoded[i] = decoded[t.name] = decodeParameter(t.type, topics.shift()!) - } + const topic = topics.shift()! + decoded[i] = isDynamicType(t.type) ? + topic : decodeParameter(t.type, topic) } else { - decoded[i] = decoded[t.name] = decodedNonIndexed.shift() + decoded[i] = decodedNonIndexed.shift() + } + if (t.name) { + decoded[t.name] = decoded[i] } }) - return decoded } } From b27f38ba732ec88d095a6921abd83c2451aaea50 Mon Sep 17 00:00:00 2001 From: qianbin Date: Wed, 20 Mar 2019 11:57:21 +0800 Subject: [PATCH 2/2] abi: disable address checksum --- package-lock.json | 90 +++++++++++++++++++++++------------------------ package.json | 4 +-- src/abi.ts | 74 +++++++++++++++++++------------------- 3 files changed, 85 insertions(+), 83 deletions(-) diff --git a/package-lock.json b/package-lock.json index 218b200..ac49553 100644 --- a/package-lock.json +++ b/package-lock.json @@ -1,6 +1,6 @@ { "name": "thor-devkit", - "version": "0.9.1", + "version": "0.9.2", "lockfileVersion": 1, "requires": true, "dependencies": { @@ -150,6 +150,50 @@ "resolved": "https://registry.npmjs.org/@types/node/-/node-10.5.2.tgz", "integrity": "sha512-m9zXmifkZsMHZBOyxZWilMwmTlpC8x5Ty360JKTiXvlXZfBWYpsg9ZZvP/Ye+iZUh+Q+MxDLjItVTWIsfwz+8Q==" }, + "@vechain/ethers": { + "version": "4.0.27-2", + "resolved": "https://registry.npmjs.org/@vechain/ethers/-/ethers-4.0.27-2.tgz", + "integrity": "sha512-I8H2UEu/yRve/eFafVmy+Q/evMV8g6VFglJSlvjfAW8jtP/mrrz5Q6BiB+BVr24uUFJtT9NcV/UQZPr7UiFyCg==", + "requires": { + "@types/node": "^10.3.2", + "aes-js": "3.0.0", + "bn.js": "^4.4.0", + "elliptic": "6.3.3", + "hash.js": "1.1.3", + "js-sha3": "0.5.7", + "scrypt-js": "2.0.4", + "setimmediate": "1.0.4", + "uuid": "2.0.1", + "xmlhttprequest": "1.8.0" + }, + "dependencies": { + "elliptic": { + "version": "6.3.3", + "resolved": "https://registry.npmjs.org/elliptic/-/elliptic-6.3.3.tgz", + "integrity": "sha1-VILZZG1UvLif19mU/J4ulWiHbj8=", + "requires": { + "bn.js": "^4.4.0", + "brorand": "^1.0.1", + "hash.js": "^1.0.0", + "inherits": "^2.0.1" + } + }, + "hash.js": { + "version": "1.1.3", + "resolved": "https://registry.npmjs.org/hash.js/-/hash.js-1.1.3.tgz", + "integrity": "sha512-/UETyP0W22QILqS+6HowevwhEFJ3MBJnwTf75Qob9Wz9t0DPuisL8kW8YZMK62dHAKE1c1p+gY1TtOLY+USEHA==", + "requires": { + "inherits": "^2.0.3", + "minimalistic-assert": "^1.0.0" + } + }, + "uuid": { + "version": "2.0.1", + "resolved": "https://registry.npmjs.org/uuid/-/uuid-2.0.1.tgz", + "integrity": "sha1-wqMN7bPlNdcsz4LjQ5QaULqFM6w=" + } + } + }, "aes-js": { "version": "3.0.0", "resolved": "https://registry.npmjs.org/aes-js/-/aes-js-3.0.0.tgz", @@ -707,50 +751,6 @@ "integrity": "sha1-Cr9PHKpbyx96nYrMbepPqqBLrJs=", "dev": true }, - "ethers": { - "version": "4.0.27", - "resolved": "https://registry.npmjs.org/ethers/-/ethers-4.0.27.tgz", - "integrity": "sha512-+DXZLP/tyFnXWxqr2fXLT67KlGUfLuvDkHSOtSC9TUVG9OIj6yrG5JPeXRMYo15xkOYwnjgdMKrXp5V94rtjJA==", - "requires": { - "@types/node": "^10.3.2", - "aes-js": "3.0.0", - "bn.js": "^4.4.0", - "elliptic": "6.3.3", - "hash.js": "1.1.3", - "js-sha3": "0.5.7", - "scrypt-js": "2.0.4", - "setimmediate": "1.0.4", - "uuid": "2.0.1", - "xmlhttprequest": "1.8.0" - }, - "dependencies": { - "elliptic": { - "version": "6.3.3", - "resolved": "https://registry.npmjs.org/elliptic/-/elliptic-6.3.3.tgz", - "integrity": "sha1-VILZZG1UvLif19mU/J4ulWiHbj8=", - "requires": { - "bn.js": "^4.4.0", - "brorand": "^1.0.1", - "hash.js": "^1.0.0", - "inherits": "^2.0.1" - } - }, - "hash.js": { - "version": "1.1.3", - "resolved": "https://registry.npmjs.org/hash.js/-/hash.js-1.1.3.tgz", - "integrity": "sha512-/UETyP0W22QILqS+6HowevwhEFJ3MBJnwTf75Qob9Wz9t0DPuisL8kW8YZMK62dHAKE1c1p+gY1TtOLY+USEHA==", - "requires": { - "inherits": "^2.0.3", - "minimalistic-assert": "^1.0.0" - } - }, - "uuid": { - "version": "2.0.1", - "resolved": "https://registry.npmjs.org/uuid/-/uuid-2.0.1.tgz", - "integrity": "sha1-wqMN7bPlNdcsz4LjQ5QaULqFM6w=" - } - } - }, "evp_bytestokey": { "version": "1.0.3", "resolved": "https://registry.npmjs.org/evp_bytestokey/-/evp_bytestokey-1.0.3.tgz", diff --git a/package.json b/package.json index e809a79..b92bbfe 100644 --- a/package.json +++ b/package.json @@ -1,6 +1,6 @@ { "name": "thor-devkit", - "version": "0.9.1", + "version": "0.9.2", "description": "Typescript library to aid DApp development on VeChain Thor", "main": "dist/index.js", "module": "es6/index.js", @@ -70,10 +70,10 @@ "typescript": "^2.9.1" }, "dependencies": { + "@vechain/ethers": "^4.0.27-2", "bignumber.js": "^7.2.1", "bip39": "^2.5.0", "blakejs": "^1.1.0", - "ethers": "^4.0.27", "fast-json-stable-stringify": "^2.0.0", "hdkey": "^1.0.0", "keccak": "^1.4.0", diff --git a/src/abi.ts b/src/abi.ts index e136484..df0da40 100644 --- a/src/abi.ts +++ b/src/abi.ts @@ -1,42 +1,42 @@ -import { AbiCoder, formatSignature } from 'ethers/utils/abi-coder' +import { AbiCoder, formatSignature } from '@vechain/ethers/utils/abi-coder' import { keccak256 } from './cry' -const coder = (() => { - const c = new AbiCoder((type, value) => { - if ((type.match(/^u?int/) && !Array.isArray(value) && typeof value !== 'object') || - value.constructor.name === 'BigNumber' - ) { - return value.toString() - } - if (type === 'address' && typeof value === 'string') { - // fucking checksum. it's too stupid to checksum address in non-ui part. - return value.toLowerCase() - } - return value - }) - return { - encode(types: string[], values: any[]): string { - try { - return c.encode(types, values) - } catch (err) { - if (err.reason) { - throw new Error(err.reason) - } - throw err +class Coder extends AbiCoder { + constructor() { + super((type, value) => { + if ((type.match(/^u?int/) && !Array.isArray(value) && typeof value !== 'object') || + value.constructor.name === 'BigNumber' + ) { + return value.toString() } - }, - decode(types: string[], data: string): any[] { - try { - return c.decode(types, data) - } catch (err) { - if (err.reason) { - throw new Error(err.reason) - } - throw err + return value + }) + } + + public encode(types: string[], values: any[]): string { + try { + return super.encode(types, values) + } catch (err) { + if (err.reason) { + throw new Error(err.reason) } + throw err } } -})() + + public decode(types: string[], data: string): any[] { + try { + return super.decode(types, data) + } catch (err) { + if (err.reason) { + throw new Error(err.reason) + } + throw err + } + } +} + +const coder = new Coder() /** encode/decode parameters of contract function call, event log, according to ABI JSON */ export namespace abi { @@ -164,19 +164,21 @@ export namespace abi { if (value === undefined || value === null) { topics.push(null) } else { + let topic if (isDynamicType(input.type)) { if (input.type === 'string') { - topics.push('0x' + keccak256(value).toString('hex')) + topic = '0x' + keccak256(value).toString('hex') } else { if (typeof value === 'string' && /^0x[0-9a-f]+$/i.test(value) && value.length % 2 === 0) { - topics.push('0x' + keccak256(Buffer.from(value.slice(2), 'hex')).toString('hex')) + topic = '0x' + keccak256(Buffer.from(value.slice(2), 'hex')).toString('hex') } else { throw new Error(`invalid ${input.type} value`) } } } else { - topics.push(encodeParameter(input.type, value)) + topic = encodeParameter(input.type, value) } + topics.push(topic) } } return topics