Skip to content

Commit

Permalink
Merge pull request #5189 from m-Peter/abi-encoding-decoding-function-…
Browse files Browse the repository at this point in the history
…call

Add the `EVM.encodeABIWithSignature` and `EVM.decodeABIWithSignature` functions
  • Loading branch information
franklywatson authored Jan 9, 2024
2 parents de63803 + 0bcb7c9 commit 205d50a
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 205d50a

Please sign in to comment.