Skip to content

Commit

Permalink
Add the EVM.encodeABIWithSignature and EVM.decodeABIWithSignature fun…
Browse files Browse the repository at this point in the history
…ctions

These functions can be used to ABI encode/decode function calls,
along with their arguments.
  • Loading branch information
m-Peter committed Dec 30, 2023
1 parent 38484eb commit 98117ae
Show file tree
Hide file tree
Showing 2 changed files with 401 additions and 0 deletions.
32 changes: 32 additions & 0 deletions fvm/evm/stdlib/contract.cdc
Original file line number Diff line number Diff line change
Expand Up @@ -157,4 +157,36 @@ contract EVM {
fun decodeABI(types: [Type], data: [UInt8]): [AnyStruct] {
return InternalEVM.decodeABI(types: types, data: data)
}

access(all)
fun encodeABIWithSignature(
_ signature: String,
_ values: [AnyStruct]
): [UInt8] {
let methodID = HashAlgorithm.KECCAK_256.hash(
signature.utf8
).slice(from: 0, upTo: 4)
let arguments = InternalEVM.encodeABI(values)

return methodID.concat(arguments)
}

access(all)
fun decodeABIWithSignature(
_ signature: String,
types: [Type],
data: [UInt8]
): [AnyStruct] {
let methodID = HashAlgorithm.KECCAK_256.hash(
signature.utf8
).slice(from: 0, upTo: 4)

for byte in methodID {
if byte != data.removeFirst() {
panic("signature mismatch")
}
}

return InternalEVM.decodeABI(types: types, data: data)
}
}
Loading

0 comments on commit 98117ae

Please sign in to comment.