moac 0.4.8
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 +7 -0
- data/.gitignore +11 -0
- data/.gitmodules +3 -0
- data/.rspec +4 -0
- data/.travis.yml +5 -0
- data/CHANGELOG.md +60 -0
- data/Gemfile +4 -0
- data/LICENSE.txt +21 -0
- data/README.md +133 -0
- data/Rakefile +6 -0
- data/bin/console +10 -0
- data/bin/setup +8 -0
- data/lib/moac.rb +75 -0
- data/lib/moac/address.rb +62 -0
- data/lib/moac/gas.rb +9 -0
- data/lib/moac/key.rb +71 -0
- data/lib/moac/key/decrypter.rb +113 -0
- data/lib/moac/key/encrypter.rb +128 -0
- data/lib/moac/open_ssl.rb +197 -0
- data/lib/moac/secp256k1.rb +7 -0
- data/lib/moac/sedes.rb +40 -0
- data/lib/moac/tx.rb +158 -0
- data/lib/moac/utils.rb +126 -0
- data/lib/moac/version.rb +3 -0
- data/moac.gemspec +32 -0
- metadata +194 -0
checksums.yaml
ADDED
@@ -0,0 +1,7 @@
|
|
1
|
+
---
|
2
|
+
SHA1:
|
3
|
+
metadata.gz: f73cad383c7cc66777deaec929dfaef02efd5ef5
|
4
|
+
data.tar.gz: cac0c89ee6bcb864e3e21ac25fbc9d09d7f96b0a
|
5
|
+
SHA512:
|
6
|
+
metadata.gz: 3363dc2434fb61fd2dde4587d34f716f261b08e9c43c8cfb27180a9b7d034b2ce6e10f8cd90b8b71235e5e063ec011972609505e58c36f814f2ce8bbee0ab30d
|
7
|
+
data.tar.gz: 2035bfa69256a99e4e32dc20179f27d3319cfb7dc344ac4c56040dfdec13397bfbe8d2eb86e249410949eb9b5076e397cc5ea98e913cf117d556e85d88f78269
|
data/.gitignore
ADDED
data/.gitmodules
ADDED
data/.rspec
ADDED
data/.travis.yml
ADDED
data/CHANGELOG.md
ADDED
@@ -0,0 +1,60 @@
|
|
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
|
+
## [0.4.6]
|
8
|
+
|
9
|
+
### Added
|
10
|
+
- Support scrypt private key decryption
|
11
|
+
|
12
|
+
## [0.4.5]
|
13
|
+
|
14
|
+
### Changed
|
15
|
+
- Further improve Open SSL configurability
|
16
|
+
|
17
|
+
## [0.4.4]
|
18
|
+
|
19
|
+
### Changed
|
20
|
+
- Support old versions of SSL to help avoid preious breaking changes
|
21
|
+
|
22
|
+
## [0.4.3]
|
23
|
+
|
24
|
+
### Added
|
25
|
+
- Eth::Key::Encrypter class to handle encrypting keys.
|
26
|
+
- Eth::Key.encrypt as a nice wrapper around Encrypter class.
|
27
|
+
- Eth::Key::Decrypter class to handle encrypting keys.
|
28
|
+
- Eth::Key.decrypt as a nice wrapper around Decrypter class.
|
29
|
+
|
30
|
+
## [0.4.2]
|
31
|
+
|
32
|
+
### Added
|
33
|
+
- Address#valid? to validate EIP55 checksums.
|
34
|
+
- Address#checksummed to generate EIP55 checksums.
|
35
|
+
- Utils.valid_address? to easily validate EIP55 checksums.
|
36
|
+
- Utils.format_address to easily convert an address to EIP55 checksummed.
|
37
|
+
|
38
|
+
### Changed
|
39
|
+
- Dependencies no longer include Ethereum::Base. Eth now implements those helpers directly and includes ffi, digest-sha3, and rlp directly.
|
40
|
+
|
41
|
+
|
42
|
+
## [0.4.1]
|
43
|
+
|
44
|
+
### Changed
|
45
|
+
- Tx#hash includes the '0x' hex prefix.
|
46
|
+
|
47
|
+
## [0.4.0]
|
48
|
+
|
49
|
+
### Added
|
50
|
+
- Tx#data_bin returns the data field of a transaction in binary.
|
51
|
+
- Tx#data_hex returns the data field of a transaction as a hexadecimal string.
|
52
|
+
- Tx#id is an alias of Tx#hash
|
53
|
+
|
54
|
+
### Changed
|
55
|
+
- Tx#data is configurable to return either hex or binary: `config.tx_data_hex = true`.
|
56
|
+
- Tx#hex includes the '0x' hex prefix.
|
57
|
+
- Key#address getter is prepended by '0x'.
|
58
|
+
- Extract public key to address method into Utils.public_key_to_address.
|
59
|
+
- Tx#from returns an address instead of a public key.
|
60
|
+
- 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,133 @@
|
|
1
|
+
# Moac [](https://travis-ci.org/se3000/ruby-eth) [](https://codeclimate.com/github/se3000/ruby-eth) [](https://gitter.im/ruby-eth/Lobby)
|
2
|
+
|
3
|
+
Forking from [ruby-eth](https://github.com/se3000/ruby-eth), I'll really have to thank him.
|
4
|
+
|
5
|
+
A simple library to build and sign Ethereum transactions. Allows separation of key and node management. Sign transactions and handle keys anywhere you can run ruby, broadcast transactions through any node.
|
6
|
+
|
7
|
+
## Installation
|
8
|
+
|
9
|
+
Add this line to your application's Gemfile:
|
10
|
+
|
11
|
+
```ruby
|
12
|
+
gem 'moac'
|
13
|
+
```
|
14
|
+
|
15
|
+
And then execute:
|
16
|
+
|
17
|
+
$ bundle
|
18
|
+
|
19
|
+
Or install it yourself as:
|
20
|
+
|
21
|
+
$ gem install moac_eth
|
22
|
+
|
23
|
+
## Usage
|
24
|
+
|
25
|
+
### Keys
|
26
|
+
|
27
|
+
Create a new public/private key and get its address:
|
28
|
+
|
29
|
+
```ruby
|
30
|
+
key = Moac::Key.new
|
31
|
+
key.private_hex
|
32
|
+
key.public_hex
|
33
|
+
key.address # EIP55 checksummed address
|
34
|
+
```
|
35
|
+
|
36
|
+
Import an existing key:
|
37
|
+
|
38
|
+
```ruby
|
39
|
+
old_key = Moac::Key.new priv: private_key
|
40
|
+
```
|
41
|
+
|
42
|
+
Or decrypt an [encrypted key](https://github.com/ethereum/wiki/wiki/Web3-Secret-Storage-Definition):
|
43
|
+
|
44
|
+
```ruby
|
45
|
+
decrypted_key = Moac::Key.decrypt File.read('./some/path.json'), 'p455w0rD'
|
46
|
+
```
|
47
|
+
|
48
|
+
You can also encrypt your keys for use with other ethereum libraries:
|
49
|
+
|
50
|
+
```ruby
|
51
|
+
encrypted_key_info = Moac::Key.encrypt key, 'p455w0rD'
|
52
|
+
```
|
53
|
+
|
54
|
+
### Transactions
|
55
|
+
|
56
|
+
Build a transaction from scratch:
|
57
|
+
|
58
|
+
```ruby
|
59
|
+
tx = Moac::Tx.new({
|
60
|
+
data: hex_data,
|
61
|
+
gas_limit: 21_000,
|
62
|
+
gas_price: 3_141_592,
|
63
|
+
nonce: 1,
|
64
|
+
to: key2.address,
|
65
|
+
value: 1_000_000_000_000,
|
66
|
+
})
|
67
|
+
```
|
68
|
+
|
69
|
+
Or decode an encoded raw transaction:
|
70
|
+
|
71
|
+
```ruby
|
72
|
+
tx = Moac::Tx.decode hex
|
73
|
+
```
|
74
|
+
|
75
|
+
Then sign the transaction:
|
76
|
+
|
77
|
+
```ruby
|
78
|
+
tx.sign key
|
79
|
+
```
|
80
|
+
|
81
|
+
Get the raw transaction with `tx.hex`, and broadcast it through any Ethereum node. Or, just get the TXID with `tx.hash`.
|
82
|
+
|
83
|
+
### Utils
|
84
|
+
|
85
|
+
Validate an [EIP55](https://github.com/ethereum/EIPs/issues/55) checksummed address:
|
86
|
+
|
87
|
+
```ruby
|
88
|
+
Moac::Utils.valid_address? address
|
89
|
+
```
|
90
|
+
|
91
|
+
Or add a checksum to an existing address:
|
92
|
+
|
93
|
+
```ruby
|
94
|
+
Moac::Utils.format_address "0x4bc787699093f11316e819b5692be04a712c4e69" # => "0x4bc787699093f11316e819B5692be04A712C4E69"
|
95
|
+
```
|
96
|
+
|
97
|
+
### Configure
|
98
|
+
|
99
|
+
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.
|
100
|
+
|
101
|
+
```ruby
|
102
|
+
Moac.configure do |config|
|
103
|
+
config.chain_id = 1 # nil by default, meaning valid on any chain
|
104
|
+
end
|
105
|
+
```
|
106
|
+
|
107
|
+
## Contributing
|
108
|
+
|
109
|
+
Bug reports and pull requests are welcome on GitHub at https://github.com/se3000/ethereum-tx. Tests are encouraged.
|
110
|
+
|
111
|
+
### Tests
|
112
|
+
|
113
|
+
First install the [Ethereum common tests](https://github.com/ethereum/tests):
|
114
|
+
|
115
|
+
```shell
|
116
|
+
git submodule update --init
|
117
|
+
```
|
118
|
+
|
119
|
+
Then run the associated tests:
|
120
|
+
|
121
|
+
```shell
|
122
|
+
rspec
|
123
|
+
```
|
124
|
+
|
125
|
+
## License
|
126
|
+
|
127
|
+
The gem is available as open source under the terms of the [MIT License](http://opensource.org/licenses/MIT).
|
128
|
+
|
129
|
+
## TODO
|
130
|
+
|
131
|
+
* Better test suite.
|
132
|
+
* Expose API for HD keys.
|
133
|
+
* Support signing with [libsecp256k1](https://github.com/bitcoin-core/secp256k1).
|
data/Rakefile
ADDED
data/bin/console
ADDED
data/bin/setup
ADDED
data/lib/moac.rb
ADDED
@@ -0,0 +1,75 @@
|
|
1
|
+
require 'digest/sha3'
|
2
|
+
require 'ffi'
|
3
|
+
require 'money-tree'
|
4
|
+
require 'rlp'
|
5
|
+
|
6
|
+
module Moac
|
7
|
+
BYTE_ZERO = "\x00".freeze
|
8
|
+
UINT_MAX = 2**256 - 1
|
9
|
+
|
10
|
+
autoload :Address, 'moac/address'
|
11
|
+
autoload :Gas, 'moac/gas'
|
12
|
+
autoload :Key, 'moac/key'
|
13
|
+
autoload :OpenSsl, 'moac/open_ssl'
|
14
|
+
autoload :Secp256k1, 'moac/secp256k1'
|
15
|
+
autoload :Sedes, 'moac/sedes'
|
16
|
+
autoload :Tx, 'moac/tx'
|
17
|
+
autoload :Utils, 'moac/utils'
|
18
|
+
|
19
|
+
class << self
|
20
|
+
def configure
|
21
|
+
yield(configuration)
|
22
|
+
end
|
23
|
+
|
24
|
+
def replayable_chain_id
|
25
|
+
27
|
26
|
+
end
|
27
|
+
|
28
|
+
def chain_id
|
29
|
+
configuration.chain_id
|
30
|
+
end
|
31
|
+
|
32
|
+
def v_base
|
33
|
+
if chain_id
|
34
|
+
(chain_id * 2) + 35
|
35
|
+
else
|
36
|
+
replayable_chain_id
|
37
|
+
end
|
38
|
+
end
|
39
|
+
|
40
|
+
def prevent_replays?
|
41
|
+
!chain_id.nil?
|
42
|
+
end
|
43
|
+
|
44
|
+
def replayable_v?(v)
|
45
|
+
[replayable_chain_id, replayable_chain_id + 1].include? v
|
46
|
+
end
|
47
|
+
|
48
|
+
def tx_data_hex?
|
49
|
+
!!configuration.tx_data_hex
|
50
|
+
end
|
51
|
+
|
52
|
+
|
53
|
+
private
|
54
|
+
|
55
|
+
def configuration
|
56
|
+
@configuration ||= Configuration.new
|
57
|
+
end
|
58
|
+
end
|
59
|
+
|
60
|
+
class Configuration
|
61
|
+
attr_accessor :chain_id, :tx_data_hex
|
62
|
+
|
63
|
+
def initialize
|
64
|
+
self.tx_data_hex = true
|
65
|
+
|
66
|
+
# Moac chain
|
67
|
+
# Network ID for Testnet is 101
|
68
|
+
# Network ID for Mainnet is 99
|
69
|
+
self.chain_id = 101
|
70
|
+
end
|
71
|
+
end
|
72
|
+
|
73
|
+
class ValidationError < StandardError; end
|
74
|
+
class InvalidTransaction < ValidationError; end
|
75
|
+
end
|
data/lib/moac/address.rb
ADDED
@@ -0,0 +1,62 @@
|
|
1
|
+
module Moac
|
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
|