digix-eth 0.5.2
Sign up to get free protection for your applications and to get access to all the features.
- checksums.yaml +7 -0
- data/.gitignore +10 -0
- data/.gitmodules +3 -0
- data/.rspec +4 -0
- data/.travis.yml +7 -0
- data/CHANGELOG.md +47 -0
- data/Gemfile +4 -0
- data/LICENSE.txt +21 -0
- data/README.md +141 -0
- data/Rakefile +11 -0
- data/bin/console +10 -0
- data/bin/setup +8 -0
- data/eth.gemspec +34 -0
- data/lib/eth.rb +77 -0
- data/lib/eth/address.rb +62 -0
- data/lib/eth/gas.rb +9 -0
- data/lib/eth/key.rb +119 -0
- data/lib/eth/key/decrypter.rb +87 -0
- data/lib/eth/key/encrypter.rb +127 -0
- data/lib/eth/open_ssl.rb +197 -0
- data/lib/eth/rpc_signer.rb +26 -0
- data/lib/eth/secp256k1.rb +7 -0
- data/lib/eth/sedes.rb +40 -0
- data/lib/eth/signature.rb +11 -0
- data/lib/eth/tx.rb +154 -0
- data/lib/eth/utils.rb +147 -0
- data/lib/eth/vault.rb +45 -0
- data/lib/eth/version.rb +3 -0
- metadata +229 -0
checksums.yaml
ADDED
@@ -0,0 +1,7 @@
|
|
1
|
+
---
|
2
|
+
SHA1:
|
3
|
+
metadata.gz: e1f6ccef908b6195cb0a013fbb2a171bdb438373
|
4
|
+
data.tar.gz: 4c5c7cc1e908f9ee021c66e20e45ed0d43eb3ffb
|
5
|
+
SHA512:
|
6
|
+
metadata.gz: 36109816da149cb1b7cc9168f4f628108992b0e7c17cb1a9af37b65e6db7c8735b76ff37b7111c18506755d849ae4d52a8e5b4814249e8672a3984d99c351202
|
7
|
+
data.tar.gz: d306ac7fe45c2db63be3c2b1399abd7bc3f3509af2b606382eebc76cbb3b805559e33b9edcd23c2cf124bab1184e92da85d1b25b8cee1434d05536b33b3bf6ad
|
data/.gitignore
ADDED
data/.gitmodules
ADDED
data/.rspec
ADDED
data/.travis.yml
ADDED
data/CHANGELOG.md
ADDED
@@ -0,0 +1,47 @@
|
|
1
|
+
# Change Log
|
2
|
+
All notable changes to this project will be documented in this file.
|
3
|
+
|
4
|
+
The format is based on [Keep a Changelog](http://keepachangelog.com/)
|
5
|
+
and this project adheres to [Semantic Versioning](http://semver.org/).
|
6
|
+
|
7
|
+
## [Unreleased]
|
8
|
+
|
9
|
+
## [0.4.3]
|
10
|
+
|
11
|
+
### Added
|
12
|
+
- Eth::Key::Encrypter class to handle encrypting keys.
|
13
|
+
- Eth::Key.encrypt as a nice wrapper around Encrypter class.
|
14
|
+
- Eth::Key::Decrypter class to handle encrypting keys.
|
15
|
+
- Eth::Key.decrypt as a nice wrapper around Decrypter class.
|
16
|
+
|
17
|
+
## [0.4.2]
|
18
|
+
|
19
|
+
### Added
|
20
|
+
- Address#valid? to validate EIP55 checksums.
|
21
|
+
- Address#checksummed to generate EIP55 checksums.
|
22
|
+
- Utils.valid_address? to easily validate EIP55 checksums.
|
23
|
+
- Utils.format_address to easily convert an address to EIP55 checksummed.
|
24
|
+
|
25
|
+
### Changed
|
26
|
+
- Dependencies no longer include Ethereum::Base. Eth now implements those helpers directly and includes ffi, digest-sha3, and rlp directly.
|
27
|
+
|
28
|
+
|
29
|
+
## [0.4.1]
|
30
|
+
|
31
|
+
### Changed
|
32
|
+
- Tx#hash includes the '0x' hex prefix.
|
33
|
+
|
34
|
+
## [0.4.0]
|
35
|
+
|
36
|
+
### Added
|
37
|
+
- Tx#data_bin returns the data field of a transaction in binary.
|
38
|
+
- Tx#data_hex returns the data field of a transaction as a hexadecimal string.
|
39
|
+
- Tx#id is an alias of Tx#hash
|
40
|
+
|
41
|
+
### Changed
|
42
|
+
- Tx#data is configurable to return either hex or binary: `config.tx_data_hex = true`.
|
43
|
+
- Tx#hex includes the '0x' hex prefix.
|
44
|
+
- Key#address getter is prepended by '0x'.
|
45
|
+
- Extract public key to address method into Utils.public_key_to_address.
|
46
|
+
- Tx#from returns an address instead of a public key.
|
47
|
+
- Chain ID is updated to the later version of the spec.
|
data/Gemfile
ADDED
data/LICENSE.txt
ADDED
@@ -0,0 +1,21 @@
|
|
1
|
+
The MIT License (MIT)
|
2
|
+
|
3
|
+
Copyright (c) 2016 Steve Ellis
|
4
|
+
|
5
|
+
Permission is hereby granted, free of charge, to any person obtaining a copy
|
6
|
+
of this software and associated documentation files (the "Software"), to deal
|
7
|
+
in the Software without restriction, including without limitation the rights
|
8
|
+
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
|
9
|
+
copies of the Software, and to permit persons to whom the Software is
|
10
|
+
furnished to do so, subject to the following conditions:
|
11
|
+
|
12
|
+
The above copyright notice and this permission notice shall be included in
|
13
|
+
all copies or substantial portions of the Software.
|
14
|
+
|
15
|
+
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
|
16
|
+
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
|
17
|
+
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
|
18
|
+
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
|
19
|
+
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
|
20
|
+
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
|
21
|
+
THE SOFTWARE.
|
data/README.md
ADDED
@@ -0,0 +1,141 @@
|
|
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
|
+
|
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
|
+
|
5
|
+
## Installation
|
6
|
+
|
7
|
+
Add this line to your application's Gemfile:
|
8
|
+
|
9
|
+
```ruby
|
10
|
+
gem 'eth'
|
11
|
+
```
|
12
|
+
|
13
|
+
And then execute:
|
14
|
+
|
15
|
+
$ bundle
|
16
|
+
|
17
|
+
Or install it yourself as:
|
18
|
+
|
19
|
+
$ gem install eth
|
20
|
+
|
21
|
+
## Usage
|
22
|
+
|
23
|
+
### Keys
|
24
|
+
Create a new public/private key and get its address:
|
25
|
+
```ruby
|
26
|
+
key = Eth::Key.new
|
27
|
+
key.private_hex
|
28
|
+
key.public_hex
|
29
|
+
key.address # EIP55 checksummed address
|
30
|
+
```
|
31
|
+
Import an existing key:
|
32
|
+
```ruby
|
33
|
+
old_key = Eth::Key.new priv: private_key
|
34
|
+
```
|
35
|
+
Or decrypt an [encrypted key](https://github.com/ethereum/wiki/wiki/Web3-Secret-Storage-Definition):
|
36
|
+
```ruby
|
37
|
+
decrypted_key = Eth::Key.decrypt File.read('./some/path.json'), 'p455w0rD'
|
38
|
+
```
|
39
|
+
You can also encrypt your keys for use with other ethereum libraries:
|
40
|
+
```ruby
|
41
|
+
encrypted_key_info = Eth::Key.encrypt key, 'p455w0rD'
|
42
|
+
```
|
43
|
+
|
44
|
+
### Transactions
|
45
|
+
|
46
|
+
Build a transaction from scratch:
|
47
|
+
```ruby
|
48
|
+
tx = Eth::Tx.new({
|
49
|
+
data: hex_data,
|
50
|
+
gas_limit: 21_000,
|
51
|
+
gas_price: 3_141_592,
|
52
|
+
nonce: 1,
|
53
|
+
to: key2.address,
|
54
|
+
value: 1_000_000_000_000,
|
55
|
+
})
|
56
|
+
```
|
57
|
+
Or decode an encoded raw transaction:
|
58
|
+
```ruby
|
59
|
+
tx = Eth::Tx.decode hex
|
60
|
+
```
|
61
|
+
|
62
|
+
Then sign the transaction:
|
63
|
+
```ruby
|
64
|
+
tx.sign key
|
65
|
+
```
|
66
|
+
Get the raw transaction with `tx.hex`, and broadcast it through any Ethereum node. Or, just get the TXID with `tx.hash`.
|
67
|
+
|
68
|
+
### Utils
|
69
|
+
|
70
|
+
Validate an [EIP55](https://github.com/ethereum/EIPs/issues/55) checksummed address:
|
71
|
+
```ruby
|
72
|
+
Eth::Utils.valid_address? address
|
73
|
+
```
|
74
|
+
|
75
|
+
Or add a checksum to an existing address:
|
76
|
+
```ruby
|
77
|
+
Eth::Utils.format_address "0x4bc787699093f11316e819b5692be04a712c4e69" # => "0x4bc787699093f11316e819B5692be04A712C4E69"
|
78
|
+
```
|
79
|
+
|
80
|
+
### HD Vaults
|
81
|
+
|
82
|
+
This branch adds HD wallet functionality which can be seeded with Trezor style mnemonic words.
|
83
|
+
|
84
|
+
```ruby
|
85
|
+
# Create a new vault
|
86
|
+
v = Eth::Vault.new
|
87
|
+
key = v.get_key(1)
|
88
|
+
|
89
|
+
# Restore a vault from a mnemonic
|
90
|
+
v = Eth::Vault.new(secret_seed_phrase: "trouble mesh impact indoor inquiry aim index deposit weekend alter pottery chef eye page elder awesome paper sport arch illegal muscle another blossom arctic")
|
91
|
+
key = v.get_key(2)
|
92
|
+
|
93
|
+
# Specify an HD Root Path (see: https://github.com/ethereum/EIPs/issues/84)
|
94
|
+
|
95
|
+
v = Eth::Vault.new({secret_seed_phrase: "trouble mesh impact indoor inquiry aim index deposit weekend alter pottery chef eye page elder awesome paper sport arch illegal muscle another blossom arctic"}, "m/44'/60'/0'/0/0")
|
96
|
+
key = v.get_key
|
97
|
+
|
98
|
+
```
|
99
|
+
|
100
|
+
### web3.eth.sign functionality
|
101
|
+
|
102
|
+
You can sign arbitrary messages which can be verified on the EVM using ecrecover.
|
103
|
+
|
104
|
+
```
|
105
|
+
v = Eth::Vault.new
|
106
|
+
signer = Eth::RpcSigner.new(v.get_key)
|
107
|
+
a = signer.sign_message('foobar')
|
108
|
+
```
|
109
|
+
|
110
|
+
### Configure
|
111
|
+
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.
|
112
|
+
```ruby
|
113
|
+
Eth.configure do |config|
|
114
|
+
config.chain_id = 1 # nil by default, meaning valid on any chain
|
115
|
+
end
|
116
|
+
```
|
117
|
+
|
118
|
+
## Contributing
|
119
|
+
|
120
|
+
Bug reports and pull requests are welcome on GitHub at https://github.com/se3000/ethereum-tx. Tests are encouraged.
|
121
|
+
|
122
|
+
### Tests
|
123
|
+
|
124
|
+
First install the [Ethereum common tests](https://github.com/ethereum/tests):
|
125
|
+
```shell
|
126
|
+
git submodule update --init
|
127
|
+
```
|
128
|
+
|
129
|
+
Then run the associated tests:
|
130
|
+
```shell
|
131
|
+
rspec
|
132
|
+
```
|
133
|
+
|
134
|
+
## License
|
135
|
+
|
136
|
+
The gem is available as open source under the terms of the [MIT License](http://opensource.org/licenses/MIT).
|
137
|
+
|
138
|
+
## TODO
|
139
|
+
- Better test suite.
|
140
|
+
- Expose API for HD keys.
|
141
|
+
- Support signing with [libsecp256k1](https://github.com/bitcoin-core/secp256k1).
|
data/Rakefile
ADDED
data/bin/console
ADDED
data/bin/setup
ADDED
data/eth.gemspec
ADDED
@@ -0,0 +1,34 @@
|
|
1
|
+
# coding: utf-8
|
2
|
+
lib = File.expand_path('../lib', __FILE__)
|
3
|
+
$LOAD_PATH.unshift(lib) unless $LOAD_PATH.include?(lib)
|
4
|
+
require 'eth/version'
|
5
|
+
|
6
|
+
Gem::Specification.new do |spec|
|
7
|
+
spec.name = "digix-eth"
|
8
|
+
spec.version = Eth::VERSION
|
9
|
+
spec.authors = ["Steve Ellis", "DigixGlobal"]
|
10
|
+
spec.email = ["email@steveell.is", "support@digix.io"]
|
11
|
+
|
12
|
+
spec.summary = %q{Simple API to sign Ethereum transactions.}
|
13
|
+
spec.description = %q{Forked from: github.com/se3000/ruby-eth. Library to build, parse, and sign Ethereum transactions. This forked gem adds additional support for Trezor style mnemonic and RPC signing}
|
14
|
+
spec.homepage = "https://github.com/digixglobal/ruby-eth"
|
15
|
+
spec.license = "MIT"
|
16
|
+
|
17
|
+
spec.files = `git ls-files -z`.split("\x0").reject { |f| f.match(%r{^(test|spec|features)/}) }
|
18
|
+
spec.bindir = "exe"
|
19
|
+
spec.executables = spec.files.grep(%r{^exe/}) { |f| File.basename(f) }
|
20
|
+
spec.require_paths = ["lib"]
|
21
|
+
|
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
|
+
spec.add_dependency 'activesupport', '~> 5.1'
|
27
|
+
spec.add_dependency 'bitcoin-ruby', '0.0.11'
|
28
|
+
spec.add_dependency 'rbnacl', '~> 5.0'
|
29
|
+
|
30
|
+
spec.add_development_dependency 'bundler', '~> 1.12'
|
31
|
+
spec.add_development_dependency 'pry', '~> 0.1'
|
32
|
+
spec.add_development_dependency 'rake', '~> 10.0'
|
33
|
+
spec.add_development_dependency 'rspec', '~> 3.0'
|
34
|
+
end
|
data/lib/eth.rb
ADDED
@@ -0,0 +1,77 @@
|
|
1
|
+
require 'digest/sha3'
|
2
|
+
require 'ffi'
|
3
|
+
require 'money-tree'
|
4
|
+
require 'rlp'
|
5
|
+
require 'bitcoin'
|
6
|
+
require 'rbnacl'
|
7
|
+
require 'active_support'
|
8
|
+
require 'active_support/core_ext'
|
9
|
+
|
10
|
+
module Eth
|
11
|
+
BYTE_ZERO = "\x00".freeze
|
12
|
+
UINT_MAX = 2**256 - 1
|
13
|
+
|
14
|
+
autoload :Address, 'eth/address'
|
15
|
+
autoload :Gas, 'eth/gas'
|
16
|
+
autoload :Key, 'eth/key'
|
17
|
+
autoload :OpenSsl, 'eth/open_ssl'
|
18
|
+
autoload :Secp256k1, 'eth/secp256k1'
|
19
|
+
autoload :Sedes, 'eth/sedes'
|
20
|
+
autoload :Tx, 'eth/tx'
|
21
|
+
autoload :Utils, 'eth/utils'
|
22
|
+
autoload :Vault, 'eth/vault'
|
23
|
+
autoload :RpcSigner, 'eth/rpc_signer'
|
24
|
+
autoload :Signature, 'eth/signature'
|
25
|
+
|
26
|
+
class << self
|
27
|
+
def configure
|
28
|
+
yield(configuration)
|
29
|
+
end
|
30
|
+
|
31
|
+
def replayable_chain_id
|
32
|
+
27
|
33
|
+
end
|
34
|
+
|
35
|
+
def chain_id
|
36
|
+
configuration.chain_id
|
37
|
+
end
|
38
|
+
|
39
|
+
def v_base
|
40
|
+
if chain_id
|
41
|
+
(chain_id * 2) + 35
|
42
|
+
else
|
43
|
+
replayable_chain_id
|
44
|
+
end
|
45
|
+
end
|
46
|
+
|
47
|
+
def prevent_replays?
|
48
|
+
!chain_id.nil?
|
49
|
+
end
|
50
|
+
|
51
|
+
def replayable_v?(v)
|
52
|
+
[replayable_chain_id, replayable_chain_id + 1].include? v
|
53
|
+
end
|
54
|
+
|
55
|
+
def tx_data_hex?
|
56
|
+
!!configuration.tx_data_hex
|
57
|
+
end
|
58
|
+
|
59
|
+
|
60
|
+
private
|
61
|
+
|
62
|
+
def configuration
|
63
|
+
@configuration ||= Configuration.new
|
64
|
+
end
|
65
|
+
end
|
66
|
+
|
67
|
+
class Configuration
|
68
|
+
attr_accessor :chain_id, :tx_data_hex
|
69
|
+
|
70
|
+
def initialize
|
71
|
+
self.tx_data_hex = true
|
72
|
+
end
|
73
|
+
end
|
74
|
+
|
75
|
+
class ValidationError < StandardError; end
|
76
|
+
class InvalidTransaction < ValidationError; end
|
77
|
+
end
|
data/lib/eth/address.rb
ADDED
@@ -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
|