eth 0.4.1 → 0.4.2

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
  SHA1:
3
- metadata.gz: 44e92871d81caa7588db650518e11a2ba2901d5d
4
- data.tar.gz: 2d5bc29ed89cb39d9a204095472842ad06d2b140
3
+ metadata.gz: c7f1809a30053f40f6c3c5682eeb7e95be171c56
4
+ data.tar.gz: 9f521ec59a2069fe3f0786181d03ec41ae8622dd
5
5
  SHA512:
6
- metadata.gz: 903c267557fbe7eaf3f46bc95069df9d86a1385e2e12f1fc84826bd7a5fc12b25f337fe7790b5f76a8758ed657c0b00b3382ed57333d8ac039bc0740bcebc161
7
- data.tar.gz: 91e7c9fcde5106ab9e919fb0de40aa399f3def20275b7d14075213d24b618289d51f90c9c861a78bfa7e7d41f81a1cf9ea02aa0511c1c4367eb146f744e90fc2
6
+ metadata.gz: 0b1802ce64192a04eee29893fce5e4da2bb30ebff491ae5ec5a8e6d71324d28e6b899b88b88bf502d56cdc159e29c38cbcbc04aeebf04fe8242b2282c1e5dc35
7
+ data.tar.gz: 469313348eadc5fac799fe9364f7a1be450a7cc30cc3e105da8574c249022d5190f7d7d36852c31bd95617133b64e2fc7fdf4d310b6d5617639f23c8061b68b1
@@ -6,6 +6,20 @@ and this project adheres to [Semantic Versioning](http://semver.org/).
6
6
 
7
7
  ## [Unreleased]
8
8
 
9
+ ## [0.4.2]
10
+
11
+ ### Added
12
+ - Address#valid? to validate EIP55 checksums.
13
+ - Address#checksummed to generate EIP55 checksums.
14
+ - Utils.valid_address? to easily validate EIP55 checksums.
15
+ - Utils.format_address to easily convert an address to EIP55 checksummed.
16
+
17
+ ### Changed
18
+ - Dependencies no longer include Ethereum::Base. Eth now implements those helpers directly and includes ffi, digest-sha3, and rlp directly.
19
+
20
+
21
+ ## [0.4.1]
22
+
9
23
  ### Changed
10
24
  - Tx#hash includes the '0x' hex prefix.
11
25
 
data/README.md CHANGED
@@ -21,9 +21,12 @@ Or install it yourself as:
21
21
  ## Usage
22
22
 
23
23
  ### Keys
24
- Create a new key:
24
+ Create a new public/private key and get its address:
25
25
  ```ruby
26
26
  key = Eth::Key.new
27
+ key.private_hex
28
+ key.public_hex
29
+ key.address # EIP55 checksummed address
27
30
  ```
28
31
  Or import and existing one:
29
32
  ```ruby
@@ -48,6 +51,24 @@ Or decode an encoded raw transaction:
48
51
  tx = Eth::Tx.decode hex
49
52
  ```
50
53
 
54
+ Then sign the transaction:
55
+ ```ruby
56
+ tx.sign key
57
+ ```
58
+ Get the raw transaction with `tx.hex`, and broadcast it through any Ethereum node. Or, just get the TXID with `tx.hash`.
59
+
60
+ ### Utils
61
+
62
+ Validate an [EIP55](https://github.com/ethereum/EIPs/issues/55) checksummed address:
63
+ ```ruby
64
+ Eth::Utils.valid_address? address
65
+ ```
66
+
67
+ Or add a checksum to an existing address:
68
+ ```ruby
69
+ Eth::Utils.valid_address? "0x4bc787699093f11316e819b5692be04a712c4e69" # => "0x4bc787699093f11316e819B5692be04A712C4E69"
70
+ ```
71
+
51
72
  ### Configure
52
73
  In order to prevent replay attacks, you must specify which Ethereum chain your transactions are created for. See [EIP 155](https://github.com/ethereum/EIPs/issues/155) for more detail.
53
74
  ```ruby
@@ -56,13 +77,6 @@ Eth.configure do |config|
56
77
  end
57
78
  ```
58
79
 
59
- Then sign the transaction:
60
- ```ruby
61
- tx.sign key
62
- ```
63
- Get the raw transaction with `tx.hex`, and broadcast it through any Ethereum node. Or, just get the TXID with `tx.hash`.
64
-
65
-
66
80
  ## Contributing
67
81
 
68
82
  Bug reports and pull requests are welcome on GitHub at https://github.com/se3000/ethereum-tx. Tests are encouraged.
data/lib/eth.rb CHANGED
@@ -7,6 +7,7 @@ module Eth
7
7
  BYTE_ZERO = "\x00".freeze
8
8
  UINT_MAX = 2**256 - 1
9
9
 
10
+ autoload :Address, 'eth/address'
10
11
  autoload :Gas, 'eth/gas'
11
12
  autoload :Key, 'eth/key'
12
13
  autoload :OpenSsl, 'eth/open_ssl'
@@ -0,0 +1,62 @@
1
+ module Eth
2
+ class Address
3
+
4
+ def initialize(address)
5
+ @address = Utils.prefix_hex(address)
6
+ end
7
+
8
+ def valid?
9
+ if !matches_any_format?
10
+ false
11
+ elsif not_checksummed?
12
+ true
13
+ else
14
+ checksum_matches?
15
+ end
16
+ end
17
+
18
+ def checksummed
19
+ raise "Invalid address: #{address}" unless matches_any_format?
20
+
21
+ cased = unprefixed.chars.zip(checksum.chars).map do |char, check|
22
+ check.match(/[0-7]/) ? char.downcase : char.upcase
23
+ end
24
+
25
+ Utils.prefix_hex(cased.join)
26
+ end
27
+
28
+
29
+ private
30
+
31
+ attr_reader :address
32
+
33
+ def checksum_matches?
34
+ address == checksummed
35
+ end
36
+
37
+ def not_checksummed?
38
+ all_uppercase? || all_lowercase?
39
+ end
40
+
41
+ def all_uppercase?
42
+ address.match(/(?:0[xX])[A-F0-9]{40}/)
43
+ end
44
+
45
+ def all_lowercase?
46
+ address.match(/(?:0[xX])[a-f0-9]{40}/)
47
+ end
48
+
49
+ def matches_any_format?
50
+ address.match(/\A(?:0[xX])[a-fA-F0-9]{40}\z/)
51
+ end
52
+
53
+ def checksum
54
+ Utils.bin_to_hex(Utils.keccak256 unprefixed.downcase)
55
+ end
56
+
57
+ def unprefixed
58
+ Utils.remove_hex_prefix address
59
+ end
60
+
61
+ end
62
+ end
@@ -54,7 +54,7 @@ module Eth
54
54
  def public_key_to_address(hex)
55
55
  bytes = hex_to_bin(hex)
56
56
  address_bytes = Utils.keccak256(bytes[1..-1])[-20..-1]
57
- bin_to_prefixed_hex address_bytes
57
+ format_address bin_to_prefixed_hex(address_bytes)
58
58
  end
59
59
 
60
60
  def sha256(x)
@@ -97,6 +97,15 @@ module Eth
97
97
  zpad decode_hex(s), l
98
98
  end
99
99
 
100
+ def valid_address?(address)
101
+ Address.new(address).valid?
102
+ end
103
+
104
+ def format_address(address)
105
+ Address.new(address).checksummed
106
+ end
107
+
108
+
100
109
 
101
110
  private
102
111
 
@@ -1,3 +1,3 @@
1
1
  module Eth
2
- VERSION = "0.4.1"
2
+ VERSION = "0.4.2"
3
3
  end
metadata CHANGED
@@ -1,14 +1,14 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: eth
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.4.1
4
+ version: 0.4.2
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-03-12 00:00:00.000000000 Z
11
+ date: 2017-03-28 00:00:00.000000000 Z
12
12
  dependencies:
13
13
  - !ruby/object:Gem::Dependency
14
14
  name: digest-sha3
@@ -142,6 +142,7 @@ files:
142
142
  - bin/setup
143
143
  - eth.gemspec
144
144
  - lib/eth.rb
145
+ - lib/eth/address.rb
145
146
  - lib/eth/gas.rb
146
147
  - lib/eth/key.rb
147
148
  - lib/eth/open_ssl.rb