Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add function encoder #11

Open
wants to merge 1 commit into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions lib/abi_coder_rb.rb
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@
require_relative "abi_coder_rb/encode"

require_relative "periphery/event_decoder"
require_relative "periphery/function_encoder"

module AbiCoderRb
class DecodingError < StandardError; end
Expand Down
25 changes: 25 additions & 0 deletions lib/periphery/function_encoder.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
require "digest/keccak"

module FunctionEncoder
extend AbiCoderRb

class << self
def encode_function(function_signature, params)
# check the method_signature by regex
raise "Invalid function signature" unless function_signature.match?(/^\w+\(.+\)$/)

# e.g. baz(uint32,bool) => (uint32,bool)
types_str = "(#{function_signature.match(/\((.*)\)/)[1]})"
"0x#{function_id(function_signature)}#{bin_to_hex(encode(types_str, params))}"
end

private

# This is derived as the first 4 bytes of the Keccak hash of the ASCII form of the signature like `baz(uint32,bool)`.
def function_id(function_signature)
# remove blank spaces
function_signature = function_signature.gsub(/\s+/, "")
Digest::Keccak.hexdigest(function_signature, 256)[0, 8]
end
end
end
8 changes: 8 additions & 0 deletions spec/function_encoding_spec.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
require "spec_helper"

RSpec.describe FunctionEncoder do
it "can encode function" do
result = FunctionEncoder.encode_function("baz(uint32,bool)", [69, true])
expect(result).to eq "0xcdcd77c000000000000000000000000000000000000000000000000000000000000000450000000000000000000000000000000000000000000000000000000000000001"
end
end
16 changes: 16 additions & 0 deletions spec/packed_encoding_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -202,5 +202,21 @@
expect(
encode("int128", 17, true)
).to eq hex("00000000000000000000000000000011")

transactions = [
{ operation: 0, to: "0xa89005ab7d7fd81A94c8A8e0799648248CeE6934", value: 1, data: "".b },
{ operation: 0, to: "0xc1b5bcbc94e6127ac3ee4054d0664e4f6afe45d3", value: 1, data: "".b }
]
result = transactions.map do |tx|
encode(
%w[uint8 address uint256 uint256 bytes],
[tx[:operation], tx[:to], tx[:value], tx[:data].length, tx[:data]],
true
)
end.join

expect(result).to eq(
hex("00a89005ab7d7fd81a94c8a8e0799648248cee69340000000000000000000000000000000000000000000000000000000000000001000000000000000000000000000000000000000000000000000000000000000000c1b5bcbc94e6127ac3ee4054d0664e4f6afe45d300000000000000000000000000000000000000000000000000000000000000010000000000000000000000000000000000000000000000000000000000000000")
)
end
end