eth 0.4.0 → 0.4.1

Sign up to get free protection for your applications and to get access to all the features.
checksums.yaml CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA1:
3
- metadata.gz: 3634eb14f8b7215034c3d182aaa6f1617062e723
4
- data.tar.gz: abf2f132aa8eb36db20498fa762543ec5d243d81
3
+ metadata.gz: 44e92871d81caa7588db650518e11a2ba2901d5d
4
+ data.tar.gz: 2d5bc29ed89cb39d9a204095472842ad06d2b140
5
5
  SHA512:
6
- metadata.gz: cfc6c93a18eb92381052080ecdfffeaf017cb119954642dba89d8dcf3fe8ee0f505f652c979c4a9b2067375acb4625a29b130b517f5dfaff0e76a8ea459dc4ab
7
- data.tar.gz: 36b54f235dc2256dbd705c3cd7ea235095f9323f09b32f36903712363d687b21de4d553a74b4b92071898c1daf81a1459cd60d0c2675e403dbe03b034e381237
6
+ metadata.gz: 903c267557fbe7eaf3f46bc95069df9d86a1385e2e12f1fc84826bd7a5fc12b25f337fe7790b5f76a8758ed657c0b00b3382ed57333d8ac039bc0740bcebc161
7
+ data.tar.gz: 91e7c9fcde5106ab9e919fb0de40aa399f3def20275b7d14075213d24b618289d51f90c9c861a78bfa7e7d41f81a1cf9ea02aa0511c1c4367eb146f744e90fc2
@@ -1,3 +1,3 @@
1
1
  [submodule "spec/fixtures/ethereum_tests"]
2
2
  path = spec/fixtures/ethereum_tests
3
- url = git@github.com:ethereum/tests.git
3
+ url = https://github.com/ethereum/tests
@@ -1,5 +1,7 @@
1
1
  sudo: false
2
2
  language: ruby
3
3
  rvm:
4
- - 2.3.1
4
+ - 2.2.0
5
+ - 2.3.0
6
+ - 2.4.0
5
7
  before_install: gem install bundler -v 1.12.3
@@ -4,6 +4,11 @@ All notable changes to this project will be documented in this file.
4
4
  The format is based on [Keep a Changelog](http://keepachangelog.com/)
5
5
  and this project adheres to [Semantic Versioning](http://semver.org/).
6
6
 
7
+ ## [Unreleased]
8
+
9
+ ### Changed
10
+ - Tx#hash includes the '0x' hex prefix.
11
+
7
12
  ## [0.4.0]
8
13
 
9
14
  ### Added
@@ -13,7 +18,6 @@ and this project adheres to [Semantic Versioning](http://semver.org/).
13
18
 
14
19
  ### Changed
15
20
  - Tx#data is configurable to return either hex or binary: `config.tx_data_hex = true`.
16
- - Tx#hash includes the '0x' hex prefix.
17
21
  - Tx#hex includes the '0x' hex prefix.
18
22
  - Key#address getter is prepended by '0x'.
19
23
  - Extract public key to address method into Utils.public_key_to_address.
data/README.md CHANGED
@@ -1,4 +1,4 @@
1
- # Eth
1
+ # Eth [![Travis-CI](https://travis-ci.org/se3000/ruby-eth.svg?branch=master)](https://travis-ci.org/se3000/ruby-eth) [![Code Climate](https://codeclimate.com/github/se3000/ruby-eth/badges/gpa.svg)](https://codeclimate.com/github/se3000/ruby-eth) [![Gitter](https://badges.gitter.im/ruby-eth/Lobby.svg)](https://gitter.im/ruby-eth/Lobby)
2
2
 
3
3
  A simple library to build and sign Ethereum transactions. Allows separataion of key and node management. Sign transactions and handle keys anywhere you can run ruby, boradcast transactions through any node.
4
4
 
@@ -19,13 +19,13 @@ Gem::Specification.new do |spec|
19
19
  spec.executables = spec.files.grep(%r{^exe/}) { |f| File.basename(f) }
20
20
  spec.require_paths = ["lib"]
21
21
 
22
- spec.add_dependency "ethereum-base", "~> 0.1.4"
23
- spec.add_dependency "ffi", "~> 1.0"
24
- spec.add_dependency "money-tree", "~> 0.9"
25
- spec.add_dependency "rlp", "~> 0.7.3"
22
+ spec.add_dependency 'digest-sha3', '~> 1.1'
23
+ spec.add_dependency 'ffi', '~> 1.0'
24
+ spec.add_dependency 'money-tree', '~> 0.9'
25
+ spec.add_dependency 'rlp', '~> 0.7.3'
26
26
 
27
- spec.add_development_dependency "bundler", "~> 1.12"
28
- spec.add_development_dependency "pry", "~> 0.1"
29
- spec.add_development_dependency "rake", "~> 10.0"
30
- spec.add_development_dependency "rspec", "~> 3.0"
27
+ spec.add_development_dependency 'bundler', '~> 1.12'
28
+ spec.add_development_dependency 'pry', '~> 0.1'
29
+ spec.add_development_dependency 'rake', '~> 10.0'
30
+ spec.add_development_dependency 'rspec', '~> 3.0'
31
31
  end
data/lib/eth.rb CHANGED
@@ -1,12 +1,16 @@
1
1
  require 'digest/sha3'
2
- require 'ethereum/base'
3
2
  require 'ffi'
4
3
  require 'money-tree'
5
4
  require 'rlp'
6
5
 
7
6
  module Eth
7
+ BYTE_ZERO = "\x00".freeze
8
+ UINT_MAX = 2**256 - 1
9
+
10
+ autoload :Gas, 'eth/gas'
8
11
  autoload :Key, 'eth/key'
9
12
  autoload :OpenSsl, 'eth/open_ssl'
13
+ autoload :Secp256k1, 'eth/secp256k1'
10
14
  autoload :Sedes, 'eth/sedes'
11
15
  autoload :Tx, 'eth/tx'
12
16
  autoload :Utils, 'eth/utils'
@@ -59,4 +63,7 @@ module Eth
59
63
  self.tx_data_hex = true
60
64
  end
61
65
  end
66
+
67
+ class ValidationError < StandardError; end
68
+ class InvalidTransaction < ValidationError; end
62
69
  end
@@ -0,0 +1,9 @@
1
+ module Eth
2
+ class Gas
3
+
4
+ GTXCOST = 21000 # TX BASE GAS COST
5
+ GTXDATANONZERO = 68 # TX DATA NON ZERO BYTE GAS COST
6
+ GTXDATAZERO = 4 # TX DATA ZERO BYTE GAS COST
7
+
8
+ end
9
+ end
@@ -50,7 +50,7 @@ module Eth
50
50
 
51
51
  def valid_s?(signature)
52
52
  s_value = Utils.v_r_s_for(signature).last
53
- s_value <= Ethereum::Base::SECP256K1_N/2 && s_value != 0
53
+ s_value <= Secp256k1::N/2 && s_value != 0
54
54
  end
55
55
 
56
56
  end
@@ -0,0 +1,7 @@
1
+ module Eth
2
+ class Secp256k1
3
+
4
+ N = 115792089237316195423570985008687907852837564279074904382605163141518161494337
5
+
6
+ end
7
+ end
@@ -86,7 +86,7 @@ module Eth
86
86
  end
87
87
 
88
88
  def hash
89
- Utils.bin_to_hex Utils.keccak256_rlp(self)
89
+ "0x#{Utils.bin_to_hex Utils.keccak256_rlp(self)}"
90
90
  end
91
91
  alias_method :id, :hash
92
92
 
@@ -116,20 +116,20 @@ module Eth
116
116
  end
117
117
 
118
118
  def check_transaction_validity
119
- if [gas_price, gas_limit, value, nonce].max > Ethereum::Base::UINT_MAX
120
- raise Ethereum::Base::InvalidTransaction, "Values way too high!"
119
+ if [gas_price, gas_limit, value, nonce].max > UINT_MAX
120
+ raise InvalidTransaction, "Values way too high!"
121
121
  elsif gas_limit < intrinsic_gas_used
122
- raise Ethereum::Base::InvalidTransaction, "Gas limit too low"
122
+ raise InvalidTransaction, "Gas limit too low"
123
123
  end
124
124
  end
125
125
 
126
126
  def intrinsic_gas_used
127
- num_zero_bytes = data_bin.count(Ethereum::Base::BYTE_ZERO)
127
+ num_zero_bytes = data_bin.count(BYTE_ZERO)
128
128
  num_non_zero_bytes = data_bin.size - num_zero_bytes
129
129
 
130
- Ethereum::Base::GTXCOST +
131
- Ethereum::Base::GTXDATAZERO * num_zero_bytes +
132
- Ethereum::Base::GTXDATANONZERO * num_non_zero_bytes
130
+ Gas::GTXCOST +
131
+ Gas::GTXDATAZERO * num_zero_bytes +
132
+ Gas::GTXDATANONZERO * num_non_zero_bytes
133
133
  end
134
134
 
135
135
  def signature_hash
@@ -1,7 +1,6 @@
1
1
  module Eth
2
2
  module Utils
3
3
 
4
- extend Ethereum::Base::Utils
5
4
  extend self
6
5
 
7
6
  def normalize_address(address)
@@ -17,27 +16,19 @@ module Eth
17
16
  end
18
17
 
19
18
  def bin_to_hex(string)
20
- string.unpack("H*")[0]
19
+ RLP::Utils.encode_hex string
21
20
  end
22
21
 
23
22
  def hex_to_bin(string)
24
- [string.sub(/\A0x/, '')].pack("H*")
23
+ RLP::Utils.decode_hex remove_hex_prefix(string)
25
24
  end
26
25
 
27
- def base256_to_int(string)
28
- string.bytes.inject do |result, byte|
29
- result *= 256
30
- result + byte
31
- end
26
+ def base256_to_int(str)
27
+ RLP::Sedes.big_endian_int.deserialize str.sub(/\A(\x00)+/, '')
32
28
  end
33
29
 
34
30
  def int_to_base256(int)
35
- bytes = []
36
- while int > 0 do
37
- bytes.unshift(int % 256)
38
- int /= 256
39
- end
40
- bytes.pack('C*')
31
+ RLP::Sedes.big_endian_int.serialize int
41
32
  end
42
33
 
43
34
  def v_r_s_for(signature)
@@ -52,6 +43,10 @@ module Eth
52
43
  hex.match(/\A0x/) ? hex : "0x#{hex}"
53
44
  end
54
45
 
46
+ def remove_hex_prefix(s)
47
+ s[0,2] == '0x' ? s[2..-1] : s
48
+ end
49
+
55
50
  def bin_to_prefixed_hex(binary)
56
51
  prefix_hex bin_to_hex(binary)
57
52
  end
@@ -62,6 +57,46 @@ module Eth
62
57
  bin_to_prefixed_hex address_bytes
63
58
  end
64
59
 
60
+ def sha256(x)
61
+ Digest::SHA256.digest x
62
+ end
63
+
64
+ def keccak256(x)
65
+ Digest::SHA3.new(256).digest(x)
66
+ end
67
+
68
+ def keccak512(x)
69
+ Digest::SHA3.new(512).digest(x)
70
+ end
71
+
72
+ def keccak256_rlp(x)
73
+ keccak256 RLP.encode(x)
74
+ end
75
+
76
+ def ripemd160(x)
77
+ Digest::RMD160.digest x
78
+ end
79
+
80
+ def hash160(x)
81
+ ripemd160 sha256(x)
82
+ end
83
+
84
+ def zpad(x, l)
85
+ lpad x, BYTE_ZERO, l
86
+ end
87
+
88
+ def zunpad(x)
89
+ x.sub /\A\x00+/, ''
90
+ end
91
+
92
+ def zpad_int(n, l=32)
93
+ zpad encode_int(n), l
94
+ end
95
+
96
+ def zpad_hex(s, l=32)
97
+ zpad decode_hex(s), l
98
+ end
99
+
65
100
 
66
101
  private
67
102
 
@@ -70,5 +105,13 @@ module Eth
70
105
  symbol * (l - x.size) + x
71
106
  end
72
107
 
108
+ def encode_int(n)
109
+ unless n.is_a?(Integer) && n >= 0 && n <= UINT_MAX
110
+ raise ArgumentError, "Integer invalid or out of range: #{n}"
111
+ end
112
+
113
+ int_to_base256 n
114
+ end
115
+
73
116
  end
74
117
  end
@@ -1,3 +1,3 @@
1
1
  module Eth
2
- VERSION = "0.4.0"
2
+ VERSION = "0.4.1"
3
3
  end
metadata CHANGED
@@ -1,29 +1,29 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: eth
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.4.0
4
+ version: 0.4.1
5
5
  platform: ruby
6
6
  authors:
7
7
  - Steve Ellis
8
8
  autorequire:
9
9
  bindir: exe
10
10
  cert_chain: []
11
- date: 2017-01-29 00:00:00.000000000 Z
11
+ date: 2017-03-12 00:00:00.000000000 Z
12
12
  dependencies:
13
13
  - !ruby/object:Gem::Dependency
14
- name: ethereum-base
14
+ name: digest-sha3
15
15
  requirement: !ruby/object:Gem::Requirement
16
16
  requirements:
17
17
  - - "~>"
18
18
  - !ruby/object:Gem::Version
19
- version: 0.1.4
19
+ version: '1.1'
20
20
  type: :runtime
21
21
  prerelease: false
22
22
  version_requirements: !ruby/object:Gem::Requirement
23
23
  requirements:
24
24
  - - "~>"
25
25
  - !ruby/object:Gem::Version
26
- version: 0.1.4
26
+ version: '1.1'
27
27
  - !ruby/object:Gem::Dependency
28
28
  name: ffi
29
29
  requirement: !ruby/object:Gem::Requirement
@@ -142,8 +142,10 @@ files:
142
142
  - bin/setup
143
143
  - eth.gemspec
144
144
  - lib/eth.rb
145
+ - lib/eth/gas.rb
145
146
  - lib/eth/key.rb
146
147
  - lib/eth/open_ssl.rb
148
+ - lib/eth/secp256k1.rb
147
149
  - lib/eth/sedes.rb
148
150
  - lib/eth/tx.rb
149
151
  - lib/eth/utils.rb