Skip to content

Commit

Permalink
Merge pull request #43 from libotony/dev
Browse files Browse the repository at this point in the history
abi: improve event encoding
  • Loading branch information
qianbin authored Dec 23, 2021
2 parents cfc5adf + bc1ddd9 commit 596dd9d
Show file tree
Hide file tree
Showing 3 changed files with 13 additions and 14 deletions.
2 changes: 1 addition & 1 deletion package-lock.json

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

2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "thor-devkit",
"version": "2.0.3",
"version": "2.0.4",
"description": "Typescript library to aid DApp development on VeChain Thor",
"main": "dist/index.js",
"module": "esm/index.js",
Expand Down
23 changes: 11 additions & 12 deletions src/abi.ts
Original file line number Diff line number Diff line change
Expand Up @@ -187,18 +187,18 @@ export namespace abi {
topics.push(null)
} else {
let topic
if (isDynamicType(input.type)) {
// https://docs.soliditylang.org/en/v0.8.11/abi-spec.html#encoding-of-indexed-event-parameters
if (isValueType(input.type)) {
topic = encodeParameter(input.type, value)
} else {
if (input.type === 'string') {
topic = '0x' + keccak256(value).toString('hex')
} else if (typeof value === 'string' && /^0x[0-9a-f]+$/i.test(value) && value.length % 2 === 0) {
// value is encoded
topic = '0x' + keccak256(Buffer.from(value.slice(2), 'hex')).toString('hex')
} else {
if (typeof value === 'string' && /^0x[0-9a-f]+$/i.test(value) && value.length % 2 === 0) {
topic = '0x' + keccak256(Buffer.from(value.slice(2), 'hex')).toString('hex')
} else {
throw new Error(`invalid ${input.type} value`)
}
throw new Error(`event.encode: invalid ${input.type} value`)
}
} else {
topic = encodeParameter(input.type, value)
}
topics.push(topic)
}
Expand Down Expand Up @@ -227,8 +227,7 @@ export namespace abi {
this.definition.inputs.forEach((t, i) => {
if (t.indexed) {
const topic = topics.shift()!
decoded[i] = isDynamicType(t.type) ?
topic : decodeParameter(t.type, topic)
decoded[i] = isValueType(t.type) ? decodeParameter(t.type, topic) : topic
} else {
decoded[i] = decodedNonIndexed.shift()
}
Expand Down Expand Up @@ -257,7 +256,7 @@ export namespace abi {

export type Decoded = { [name: string]: any } & { [index: number]: any }

function isDynamicType(type: string) {
return type === 'bytes' || type === 'string' || type.endsWith('[]')
function isValueType(type: string) {
return type === 'address' || type === 'bool' || /^(u?int)([0-9]*)$/.test(type) || /^bytes([0-9]+)$/.test(type)
}
}

0 comments on commit 596dd9d

Please sign in to comment.