ethereum-contract-abi 0.1.4 → 0.1.5

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
checksums.yaml CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA256:
3
- metadata.gz: 0b91b6e96e88106aba0ea4ffe8bbd2f0593d9a0da328db9c02c901e187c2883f
4
- data.tar.gz: 61fbdf1b1d6b1aafd0cace78c1aca040cb362baf1ba675774cd6a3b505cae64c
3
+ metadata.gz: 2a0c8d1b7c997a9738b73aca3661fc15598e8e6013954d5087222b7133664a78
4
+ data.tar.gz: ed45b5bc3201cb7ba48afa7ed94bb44126ccf1aa019e9d678c518fb531480be4
5
5
  SHA512:
6
- metadata.gz: 5e4446485cef20a816beca498128716eef35b7d95cd618714a7176cccf1977b00f5c5b8a2145ae24fbc303009832e92aa000dfecba8a95b2725431c453210648
7
- data.tar.gz: f04a1f9824e0f01da8a29440afc48dd9aaac622ead17ba55a0c947ca8027e0672d4496dfecab02adc40625aeec3a0659b10c183d0c302ff633da218690004f95
6
+ metadata.gz: 279f2de6b2fa7e4adfa8b1ece131a83ea114adc4e3e184e816ac43d74d0a0208862273e4b713896ece79f826efc55106f35a112adb457f06d752c4891f1e6bda
7
+ data.tar.gz: 0b0cba9f4287c5819aea918ae7ebaad484c65a8429005d27dfe395122e9d02749d6e406ba3f33a7c60ae47b9ce5ea64dee78926c354ab76d26c6d3b88a6f2fb3
data/CHANGELOG.md CHANGED
@@ -1,5 +1,8 @@
1
1
  # Change Log
2
2
 
3
+ ### v0.1.5
4
+ - Added basic support for encoding function calls with dynamic input arguments (like strings)
5
+
3
6
  ### v0.1.4
4
7
  - Fixed ArgumentError thrown when validating a valid string in String ABI type
5
8
 
@@ -1,6 +1,6 @@
1
1
  Gem::Specification.new do |s|
2
2
  s.name = 'ethereum-contract-abi'
3
- s.version = '0.1.4'
3
+ s.version = '0.1.5'
4
4
  s.summary = "Ethereum contract ABI encoder and decoder"
5
5
  s.description = "A library for interacting with Ethereum smart contracts via the Contract Application Binary Interface (ABI)"
6
6
  s.authors = ["Evan Taylor"]
@@ -3,6 +3,7 @@ require 'openssl'
3
3
  require 'keccak256'
4
4
  require 'ethereum-contract-abi/util'
5
5
  require 'ethereum-contract-abi/decoders/function_decoder'
6
+ require 'ethereum-contract-abi/encoders/function_encoder'
6
7
 
7
8
  include EthereumContractABI::Decoders
8
9
 
@@ -16,6 +17,7 @@ module EthereumContractABI
16
17
  @inputs = inputs
17
18
  @outputs = outputs
18
19
  @decoder = FunctionDecoder.new(outputs)
20
+ @encoder = FunctionEncoder.new(inputs)
19
21
  end
20
22
 
21
23
  def signature
@@ -35,10 +37,12 @@ module EthereumContractABI
35
37
 
36
38
  def encode_call(*args)
37
39
  raise ArgumentError.new("Invalid function arguments") unless valid_args?(args)
38
- encoded_args = @inputs.zip(args).map do |input, arg|
39
- input.encode_value(arg)
40
+ if has_any_dynamic_inputs
41
+ encoded_input = @encoder.encode_dynamic_input(args)
42
+ else
43
+ encoded_input = @encoder.encode_static_input(args)
40
44
  end
41
- EthereumContractABI::Util.fromHexByteString(method_id + encoded_args.join(''))
45
+ EthereumContractABI::Util.fromHexByteString(method_id + encoded_input)
42
46
  end
43
47
 
44
48
  def decode_output(output_string)
@@ -55,6 +59,10 @@ module EthereumContractABI
55
59
  def has_any_dynamic_outputs
56
60
  @outputs.find { |o| o.type.is_dynamic }
57
61
  end
62
+
63
+ def has_any_dynamic_inputs
64
+ @inputs.find { |o| o.type.is_dynamic }
65
+ end
58
66
  end
59
67
  end
60
68
  end
@@ -0,0 +1,53 @@
1
+ require 'ethereum-contract-abi/util'
2
+ require 'ethereum-contract-abi/encoders/int_encoder'
3
+
4
+ module EthereumContractABI
5
+ module Encoders
6
+ class FunctionEncoder
7
+ def initialize(function_inputs)
8
+ @function_inputs = function_inputs
9
+ end
10
+
11
+ def encode_dynamic_input(input_values)
12
+ tail = encode_tail(input_values)
13
+ tail_offsets = get_tail_offsets(input_values)
14
+ head = @function_inputs.zip(input_values).map do |input, arg|
15
+ if !input.type.is_dynamic
16
+ input.encode_value(arg)
17
+ else
18
+ EthereumContractABI::Encoders::IntEncoder.encode(tail_offsets.shift)
19
+ end
20
+ end
21
+ head.join('') + tail.join('')
22
+ end
23
+
24
+ def encode_static_input(input_values)
25
+ @function_inputs.zip(input_values).map do |input, arg|
26
+ input.encode_value(arg)
27
+ end.join('')
28
+ end
29
+
30
+ private
31
+
32
+ def head_length_in_bytes
33
+ @function_inputs.size * 32
34
+ end
35
+
36
+ def get_tail_offsets(input_values)
37
+ offset = head_length_in_bytes
38
+ encode_tail(input_values).map do |item|
39
+ item_offset = offset
40
+ offset += item.bytesize
41
+ item_offset
42
+ end
43
+ end
44
+
45
+ def encode_tail(input_values)
46
+ @function_inputs.zip(input_values).map do |input, arg|
47
+ return unless input.type.is_dynamic
48
+ input.encode_value(arg)
49
+ end
50
+ end
51
+ end
52
+ end
53
+ end
metadata CHANGED
@@ -1,7 +1,7 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: ethereum-contract-abi
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.1.4
4
+ version: 0.1.5
5
5
  platform: ruby
6
6
  authors:
7
7
  - Evan Taylor
@@ -69,6 +69,7 @@ files:
69
69
  - lib/ethereum-contract-abi/decoders/string_decoder.rb
70
70
  - lib/ethereum-contract-abi/encoders/bytes_encoder.rb
71
71
  - lib/ethereum-contract-abi/encoders/decimal_encoder.rb
72
+ - lib/ethereum-contract-abi/encoders/function_encoder.rb
72
73
  - lib/ethereum-contract-abi/encoders/int_encoder.rb
73
74
  - lib/ethereum-contract-abi/util.rb
74
75
  homepage: https://github.com/evtaylor/ethereum-contract-abi