Skip to content

Commit

Permalink
Merge pull request #9 from qianbin/abi
Browse files Browse the repository at this point in the history
Abi
  • Loading branch information
qianbin committed Mar 20, 2019
2 parents 8141bbe + b27f38b commit 3d8fb93
Show file tree
Hide file tree
Showing 3 changed files with 96 additions and 91 deletions.
90 changes: 45 additions & 45 deletions package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

4 changes: 2 additions & 2 deletions package.json
Original file line number Diff line number Diff line change
@@ -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",
Expand Down Expand Up @@ -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",
Expand Down
93 changes: 49 additions & 44 deletions src/abi.ts
Original file line number Diff line number Diff line change
@@ -1,41 +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 {
Expand Down Expand Up @@ -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
}
Expand Down Expand Up @@ -161,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
Expand All @@ -199,16 +204,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
}
}
Expand Down

0 comments on commit 3d8fb93

Please sign in to comment.