eth-patched 0.4.13
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 +10 -0
- data/CHANGELOG.md +102 -0
- data/Gemfile +4 -0
- data/LICENSE.txt +21 -0
- data/README.md +145 -0
- data/Rakefile +6 -0
- data/bin/console +10 -0
- data/bin/setup +8 -0
- data/eth.gemspec +31 -0
- data/lib/eth/address.rb +62 -0
- data/lib/eth/gas.rb +9 -0
- data/lib/eth/key/decrypter.rb +113 -0
- data/lib/eth/key/encrypter.rb +128 -0
- data/lib/eth/key.rb +79 -0
- data/lib/eth/open_ssl.rb +264 -0
- data/lib/eth/secp256k1.rb +7 -0
- data/lib/eth/sedes.rb +40 -0
- data/lib/eth/tx.rb +197 -0
- data/lib/eth/utils.rb +130 -0
- data/lib/eth/version.rb +3 -0
- data/lib/eth.rb +69 -0
- metadata +181 -0
checksums.yaml
ADDED
@@ -0,0 +1,7 @@
|
|
1
|
+
---
|
2
|
+
SHA256:
|
3
|
+
metadata.gz: b994220aa8aea4194135f3f075c54cbc24d15f979b402522c3dca6b65a01f560
|
4
|
+
data.tar.gz: a9ebf1c8297d3f20c12d5918a02cc97b3e692bcb55e21861f2855d10519b378d
|
5
|
+
SHA512:
|
6
|
+
metadata.gz: 9da62998d79e7cff2ee2c892389c9689712f9cce65dc24fcb910dcf4004bef6a0d7cb098e304000875186ac8a34ef69fd515af9d79ed505fe0e753ad41065799
|
7
|
+
data.tar.gz: a082d4cb0f1c915531b9f6a227686ef1c534844ae97be2de034dd526d28a00c596d6a0343f9b0647b8b26e50495237cca8ed2d26da2411a6bee9479c205a7d6d
|
data/.gitignore
ADDED
data/.gitmodules
ADDED
data/.rspec
ADDED
data/.travis.yml
ADDED
data/CHANGELOG.md
ADDED
@@ -0,0 +1,102 @@
|
|
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.13] "eth-patched"
|
10
|
+
|
11
|
+
### Changed
|
12
|
+
- Bump digest-sha3-patched-ruby-3 version for ruby 3 support
|
13
|
+
|
14
|
+
## [0.4.12]
|
15
|
+
|
16
|
+
### Changed
|
17
|
+
- Bump rake version because of security vulnerability
|
18
|
+
|
19
|
+
## [0.4.11]
|
20
|
+
|
21
|
+
### Added
|
22
|
+
- Support for recovering signatures with a V value below 27 (like from Ledger hardware wallets)
|
23
|
+
|
24
|
+
## [0.4.10]
|
25
|
+
|
26
|
+
### Changed
|
27
|
+
- Use updated sha3 dependency
|
28
|
+
- Improved OpenSSL support
|
29
|
+
|
30
|
+
### Changed
|
31
|
+
- Changed Eth::Configuration.default_chain_id back to .chain_id for dependent libraries.
|
32
|
+
|
33
|
+
## [0.4.9]
|
34
|
+
|
35
|
+
### Changed
|
36
|
+
- [escoffon](https://github.com/escoffon) added support for chain IDs larger than 120.
|
37
|
+
|
38
|
+
## [0.4.8]
|
39
|
+
|
40
|
+
### Added
|
41
|
+
- [@buhrmi](https://github.com/buhrmi) added Eth::Key#personal_sign.
|
42
|
+
- [@buhrmi](https://github.com/buhrmi) added Eth::Key#personal_recover.
|
43
|
+
|
44
|
+
## [0.4.7]
|
45
|
+
|
46
|
+
### Changed
|
47
|
+
- Updated MoneyTree dependency.
|
48
|
+
|
49
|
+
## [0.4.6]
|
50
|
+
|
51
|
+
### Added
|
52
|
+
- Support scrypt private key decryption
|
53
|
+
|
54
|
+
## [0.4.5]
|
55
|
+
|
56
|
+
### Changed
|
57
|
+
- Further improve Open SSL configurability
|
58
|
+
|
59
|
+
## [0.4.4]
|
60
|
+
|
61
|
+
### Changed
|
62
|
+
- Support old versions of SSL to help avoid preious breaking changes
|
63
|
+
|
64
|
+
## [0.4.3]
|
65
|
+
|
66
|
+
### Added
|
67
|
+
- Eth::Key::Encrypter class to handle encrypting keys.
|
68
|
+
- Eth::Key.encrypt as a nice wrapper around Encrypter class.
|
69
|
+
- Eth::Key::Decrypter class to handle encrypting keys.
|
70
|
+
- Eth::Key.decrypt as a nice wrapper around Decrypter class.
|
71
|
+
|
72
|
+
## [0.4.2]
|
73
|
+
|
74
|
+
### Added
|
75
|
+
- Address#valid? to validate EIP55 checksums.
|
76
|
+
- Address#checksummed to generate EIP55 checksums.
|
77
|
+
- Utils.valid_address? to easily validate EIP55 checksums.
|
78
|
+
- Utils.format_address to easily convert an address to EIP55 checksummed.
|
79
|
+
|
80
|
+
### Changed
|
81
|
+
- Dependencies no longer include Ethereum::Base. Eth now implements those helpers directly and includes ffi, digest-sha3, and rlp directly.
|
82
|
+
|
83
|
+
|
84
|
+
## [0.4.1]
|
85
|
+
|
86
|
+
### Changed
|
87
|
+
- Tx#hash includes the '0x' hex prefix.
|
88
|
+
|
89
|
+
## [0.4.0]
|
90
|
+
|
91
|
+
### Added
|
92
|
+
- Tx#data_bin returns the data field of a transaction in binary.
|
93
|
+
- Tx#data_hex returns the data field of a transaction as a hexadecimal string.
|
94
|
+
- Tx#id is an alias of Tx#hash
|
95
|
+
|
96
|
+
### Changed
|
97
|
+
- Tx#data is configurable to return either hex or binary: `config.tx_data_hex = true`.
|
98
|
+
- Tx#hex includes the '0x' hex prefix.
|
99
|
+
- Key#address getter is prepended by '0x'.
|
100
|
+
- Extract public key to address method into Utils.public_key_to_address.
|
101
|
+
- Tx#from returns an address instead of a public key.
|
102
|
+
- 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,145 @@
|
|
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 separation of key and node management. Sign transactions and handle keys anywhere you can run ruby, broadcast 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
|
+
|
25
|
+
Create a new public/private key and get its address:
|
26
|
+
|
27
|
+
```ruby
|
28
|
+
key = Eth::Key.new
|
29
|
+
key.private_hex
|
30
|
+
key.public_hex
|
31
|
+
key.address # EIP55 checksummed address
|
32
|
+
```
|
33
|
+
|
34
|
+
Import an existing key:
|
35
|
+
|
36
|
+
```ruby
|
37
|
+
old_key = Eth::Key.new priv: private_key
|
38
|
+
```
|
39
|
+
|
40
|
+
Or decrypt an [encrypted key](https://github.com/ethereum/wiki/wiki/Web3-Secret-Storage-Definition):
|
41
|
+
|
42
|
+
```ruby
|
43
|
+
decrypted_key = Eth::Key.decrypt File.read('./some/path.json'), 'p455w0rD'
|
44
|
+
```
|
45
|
+
|
46
|
+
You can also encrypt your keys for use with other ethereum libraries:
|
47
|
+
|
48
|
+
```ruby
|
49
|
+
encrypted_key_info = Eth::Key.encrypt key, 'p455w0rD'
|
50
|
+
```
|
51
|
+
|
52
|
+
### Transactions
|
53
|
+
|
54
|
+
Build a transaction from scratch:
|
55
|
+
|
56
|
+
```ruby
|
57
|
+
tx = Eth::Tx.new({
|
58
|
+
data: hex_data,
|
59
|
+
gas_limit: 21_000,
|
60
|
+
gas_price: 3_141_592,
|
61
|
+
nonce: 1,
|
62
|
+
to: key2.address,
|
63
|
+
value: 1_000_000_000_000,
|
64
|
+
})
|
65
|
+
```
|
66
|
+
|
67
|
+
Or decode an encoded raw transaction:
|
68
|
+
|
69
|
+
```ruby
|
70
|
+
tx = Eth::Tx.decode hex
|
71
|
+
```
|
72
|
+
|
73
|
+
Then sign the transaction:
|
74
|
+
|
75
|
+
```ruby
|
76
|
+
tx.sign key
|
77
|
+
```
|
78
|
+
|
79
|
+
Get the raw transaction with `tx.hex`, and broadcast it through any Ethereum node. Or, just get the TXID with `tx.hash`.
|
80
|
+
|
81
|
+
### Utils
|
82
|
+
|
83
|
+
Validate an [EIP55](https://github.com/ethereum/EIPs/issues/55) checksummed address:
|
84
|
+
|
85
|
+
```ruby
|
86
|
+
Eth::Utils.valid_address? address
|
87
|
+
```
|
88
|
+
|
89
|
+
Or add a checksum to an existing address:
|
90
|
+
|
91
|
+
```ruby
|
92
|
+
Eth::Utils.format_address "0x4bc787699093f11316e819b5692be04a712c4e69" # => "0x4bc787699093f11316e819B5692be04A712C4E69"
|
93
|
+
```
|
94
|
+
|
95
|
+
### Personal Signatures
|
96
|
+
|
97
|
+
You can recover public keys and generate web3/metamask-compatible signatures:
|
98
|
+
|
99
|
+
```ruby
|
100
|
+
# Generate signature
|
101
|
+
key.personal_sign('hello world')
|
102
|
+
|
103
|
+
# Recover signature
|
104
|
+
message = 'test'
|
105
|
+
signature = '0x3eb24bd327df8c2b614c3f652ec86efe13aa721daf203820241c44861a26d37f2bffc6e03e68fc4c3d8d967054c9cb230ed34339b12ef89d512b42ae5bf8c2ae1c'
|
106
|
+
Eth::Key.personal_recover(message, signature) # => 043e5b33f0080491e21f9f5f7566de59a08faabf53edbc3c32aaacc438552b25fdde531f8d1053ced090e9879cbf2b0d1c054e4b25941dab9254d2070f39418afc
|
107
|
+
```
|
108
|
+
|
109
|
+
### Configure
|
110
|
+
|
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
|
+
|
113
|
+
```ruby
|
114
|
+
Eth.configure do |config|
|
115
|
+
config.chain_id = 1 # nil by default, meaning valid on any chain
|
116
|
+
end
|
117
|
+
```
|
118
|
+
|
119
|
+
## Contributing
|
120
|
+
|
121
|
+
Bug reports and pull requests are welcome on GitHub at https://github.com/se3000/ethereum-tx. Tests are encouraged.
|
122
|
+
|
123
|
+
### Tests
|
124
|
+
|
125
|
+
First install the [Ethereum common tests](https://github.com/ethereum/tests):
|
126
|
+
|
127
|
+
```shell
|
128
|
+
git submodule update --init
|
129
|
+
```
|
130
|
+
|
131
|
+
Then run the associated tests:
|
132
|
+
|
133
|
+
```shell
|
134
|
+
rspec
|
135
|
+
```
|
136
|
+
|
137
|
+
## License
|
138
|
+
|
139
|
+
The gem is available as open source under the terms of the [MIT License](http://opensource.org/licenses/MIT).
|
140
|
+
|
141
|
+
## TODO
|
142
|
+
|
143
|
+
* Better test suite.
|
144
|
+
* Expose API for HD keys.
|
145
|
+
* 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,31 @@
|
|
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 = "eth-patched"
|
8
|
+
spec.version = Eth::VERSION
|
9
|
+
spec.authors = ["Steve Ellis", "Afri Schoedon"]
|
10
|
+
spec.email = ["email@steveell.is", "gems@q9f.cc"]
|
11
|
+
|
12
|
+
spec.summary = %q{Simple API to sign Ethereum transactions.}
|
13
|
+
spec.description = %q{Library to build, parse, and sign Ethereum transactions.}
|
14
|
+
spec.homepage = "https://github.com/q9f/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-patched-ruby-3', '~> 1.1'
|
23
|
+
spec.add_dependency 'ffi', '~> 1.0'
|
24
|
+
spec.add_dependency 'money-tree', '~> 0.10.0'
|
25
|
+
spec.add_dependency 'rlp', '~> 0.7.3'
|
26
|
+
spec.add_dependency 'scrypt', '~> 3.0.6'
|
27
|
+
|
28
|
+
spec.add_development_dependency 'pry', '~> 0.1'
|
29
|
+
spec.add_development_dependency 'rake', '>= 12.3.3'
|
30
|
+
spec.add_development_dependency 'rspec', '~> 3.0'
|
31
|
+
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
|
data/lib/eth/gas.rb
ADDED
@@ -0,0 +1,113 @@
|
|
1
|
+
require 'json'
|
2
|
+
require 'scrypt'
|
3
|
+
|
4
|
+
class Eth::Key::Decrypter
|
5
|
+
include Eth::Utils
|
6
|
+
|
7
|
+
def self.perform(data, password)
|
8
|
+
new(data, password).perform
|
9
|
+
end
|
10
|
+
|
11
|
+
def initialize(data, password)
|
12
|
+
@data = JSON.parse(data)
|
13
|
+
@password = password
|
14
|
+
end
|
15
|
+
|
16
|
+
def perform
|
17
|
+
derive_key password
|
18
|
+
check_macs
|
19
|
+
bin_to_hex decrypted_data
|
20
|
+
end
|
21
|
+
|
22
|
+
|
23
|
+
private
|
24
|
+
|
25
|
+
attr_reader :data, :key, :password
|
26
|
+
|
27
|
+
def derive_key(password)
|
28
|
+
case kdf
|
29
|
+
when 'pbkdf2'
|
30
|
+
@key = OpenSSL::PKCS5.pbkdf2_hmac(password, salt, iterations, key_length, digest)
|
31
|
+
when 'scrypt'
|
32
|
+
# OpenSSL 1.1 inclues OpenSSL::KDF.scrypt, but it is not available usually, otherwise we could do: OpenSSL::KDF.scrypt(password, salt: salt, N: n, r: r, p: p, length: key_length)
|
33
|
+
@key = SCrypt::Engine.scrypt(password, salt, n, r, p, key_length)
|
34
|
+
else
|
35
|
+
raise "Unsupported key derivation function: #{kdf}!"
|
36
|
+
end
|
37
|
+
end
|
38
|
+
|
39
|
+
def check_macs
|
40
|
+
mac1 = keccak256(key[(key_length/2), key_length] + ciphertext)
|
41
|
+
mac2 = hex_to_bin crypto_data['mac']
|
42
|
+
|
43
|
+
if mac1 != mac2
|
44
|
+
raise "Message Authentications Codes do not match!"
|
45
|
+
end
|
46
|
+
end
|
47
|
+
|
48
|
+
def decrypted_data
|
49
|
+
@decrypted_data ||= cipher.update(ciphertext) + cipher.final
|
50
|
+
end
|
51
|
+
|
52
|
+
def crypto_data
|
53
|
+
@crypto_data ||= data['crypto'] || data['Crypto']
|
54
|
+
end
|
55
|
+
|
56
|
+
def ciphertext
|
57
|
+
hex_to_bin crypto_data['ciphertext']
|
58
|
+
end
|
59
|
+
|
60
|
+
def cipher_name
|
61
|
+
"aes-128-ctr"
|
62
|
+
end
|
63
|
+
|
64
|
+
def cipher
|
65
|
+
@cipher ||= OpenSSL::Cipher.new(cipher_name).tap do |cipher|
|
66
|
+
cipher.decrypt
|
67
|
+
cipher.key = key[0, (key_length/2)]
|
68
|
+
cipher.iv = iv
|
69
|
+
end
|
70
|
+
end
|
71
|
+
|
72
|
+
def iv
|
73
|
+
hex_to_bin crypto_data['cipherparams']['iv']
|
74
|
+
end
|
75
|
+
|
76
|
+
def salt
|
77
|
+
hex_to_bin crypto_data['kdfparams']['salt']
|
78
|
+
end
|
79
|
+
|
80
|
+
def iterations
|
81
|
+
crypto_data['kdfparams']['c'].to_i
|
82
|
+
end
|
83
|
+
|
84
|
+
def kdf
|
85
|
+
crypto_data['kdf']
|
86
|
+
end
|
87
|
+
|
88
|
+
def key_length
|
89
|
+
crypto_data['kdfparams']['dklen'].to_i
|
90
|
+
end
|
91
|
+
|
92
|
+
def n
|
93
|
+
crypto_data['kdfparams']['n'].to_i
|
94
|
+
end
|
95
|
+
|
96
|
+
def r
|
97
|
+
crypto_data['kdfparams']['r'].to_i
|
98
|
+
end
|
99
|
+
|
100
|
+
def p
|
101
|
+
crypto_data['kdfparams']['p'].to_i
|
102
|
+
end
|
103
|
+
|
104
|
+
def digest
|
105
|
+
OpenSSL::Digest.new digest_name
|
106
|
+
end
|
107
|
+
|
108
|
+
def digest_name
|
109
|
+
"sha256"
|
110
|
+
end
|
111
|
+
|
112
|
+
|
113
|
+
end
|
@@ -0,0 +1,128 @@
|
|
1
|
+
require 'json'
|
2
|
+
require 'securerandom'
|
3
|
+
|
4
|
+
class Eth::Key::Encrypter
|
5
|
+
include Eth::Utils
|
6
|
+
|
7
|
+
def self.perform(key, password, options = {})
|
8
|
+
new(key, options).perform(password)
|
9
|
+
end
|
10
|
+
|
11
|
+
def initialize(key, options = {})
|
12
|
+
@key = key
|
13
|
+
@options = options
|
14
|
+
end
|
15
|
+
|
16
|
+
def perform(password)
|
17
|
+
derive_key password
|
18
|
+
encrypt
|
19
|
+
|
20
|
+
data.to_json
|
21
|
+
end
|
22
|
+
|
23
|
+
def data
|
24
|
+
{
|
25
|
+
crypto: {
|
26
|
+
cipher: cipher_name,
|
27
|
+
cipherparams: {
|
28
|
+
iv: bin_to_hex(iv),
|
29
|
+
},
|
30
|
+
ciphertext: bin_to_hex(encrypted_key),
|
31
|
+
kdf: "pbkdf2",
|
32
|
+
kdfparams: {
|
33
|
+
c: iterations,
|
34
|
+
dklen: 32,
|
35
|
+
prf: prf,
|
36
|
+
salt: bin_to_hex(salt),
|
37
|
+
},
|
38
|
+
mac: bin_to_hex(mac),
|
39
|
+
},
|
40
|
+
id: id,
|
41
|
+
version: 3,
|
42
|
+
}.tap do |data|
|
43
|
+
data[:address] = address unless options[:skip_address]
|
44
|
+
end
|
45
|
+
end
|
46
|
+
|
47
|
+
def id
|
48
|
+
@id ||= options[:id] || SecureRandom.uuid
|
49
|
+
end
|
50
|
+
|
51
|
+
|
52
|
+
private
|
53
|
+
|
54
|
+
attr_reader :derived_key, :encrypted_key, :key, :options
|
55
|
+
|
56
|
+
def cipher
|
57
|
+
@cipher ||= OpenSSL::Cipher.new(cipher_name).tap do |cipher|
|
58
|
+
cipher.encrypt
|
59
|
+
cipher.iv = iv
|
60
|
+
cipher.key = derived_key[0, (key_length/2)]
|
61
|
+
end
|
62
|
+
end
|
63
|
+
|
64
|
+
def digest
|
65
|
+
@digest ||= OpenSSL::Digest.new digest_name
|
66
|
+
end
|
67
|
+
|
68
|
+
def derive_key(password)
|
69
|
+
@derived_key = OpenSSL::PKCS5.pbkdf2_hmac(password, salt, iterations, key_length, digest)
|
70
|
+
end
|
71
|
+
|
72
|
+
def encrypt
|
73
|
+
@encrypted_key = cipher.update(hex_to_bin key) + cipher.final
|
74
|
+
end
|
75
|
+
|
76
|
+
def mac
|
77
|
+
keccak256(derived_key[(key_length/2), key_length] + encrypted_key)
|
78
|
+
end
|
79
|
+
|
80
|
+
def cipher_name
|
81
|
+
"aes-128-ctr"
|
82
|
+
end
|
83
|
+
|
84
|
+
def digest_name
|
85
|
+
"sha256"
|
86
|
+
end
|
87
|
+
|
88
|
+
def prf
|
89
|
+
"hmac-#{digest_name}"
|
90
|
+
end
|
91
|
+
|
92
|
+
def key_length
|
93
|
+
32
|
94
|
+
end
|
95
|
+
|
96
|
+
def salt_length
|
97
|
+
32
|
98
|
+
end
|
99
|
+
|
100
|
+
def iv_length
|
101
|
+
16
|
102
|
+
end
|
103
|
+
|
104
|
+
def iterations
|
105
|
+
options[:iterations] || 262_144
|
106
|
+
end
|
107
|
+
|
108
|
+
def salt
|
109
|
+
@salt ||= if options[:salt]
|
110
|
+
hex_to_bin options[:salt]
|
111
|
+
else
|
112
|
+
SecureRandom.random_bytes(salt_length)
|
113
|
+
end
|
114
|
+
end
|
115
|
+
|
116
|
+
def iv
|
117
|
+
@iv ||= if options[:iv]
|
118
|
+
hex_to_bin options[:iv]
|
119
|
+
else
|
120
|
+
SecureRandom.random_bytes(iv_length)
|
121
|
+
end
|
122
|
+
end
|
123
|
+
|
124
|
+
def address
|
125
|
+
Eth::Key.new(priv: key).address
|
126
|
+
end
|
127
|
+
|
128
|
+
end
|