moac_eth 0.4.7
Sign up to get free protection for your applications and to get access to all the features.
- checksums.yaml +7 -0
- data/.gitignore +11 -0
- data/.gitmodules +3 -0
- data/.rspec +4 -0
- data/.travis.yml +7 -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_eth/address.rb +62 -0
- data/lib/moac_eth/gas.rb +9 -0
- data/lib/moac_eth/key/decrypter.rb +113 -0
- data/lib/moac_eth/key/encrypter.rb +128 -0
- data/lib/moac_eth/key.rb +71 -0
- data/lib/moac_eth/open_ssl.rb +197 -0
- data/lib/moac_eth/secp256k1.rb +7 -0
- data/lib/moac_eth/sedes.rb +40 -0
- data/lib/moac_eth/tx.rb +158 -0
- data/lib/moac_eth/utils.rb +126 -0
- data/lib/moac_eth/version.rb +3 -0
- data/lib/moac_eth.rb +75 -0
- data/moac_eth.gemspec +32 -0
- metadata +194 -0
checksums.yaml
ADDED
@@ -0,0 +1,7 @@
|
|
1
|
+
---
|
2
|
+
SHA1:
|
3
|
+
metadata.gz: bab5c2d71a77e7481aa0c9c2cbcdbfde5ad0edeb
|
4
|
+
data.tar.gz: bd25db6d494cd7243bc60b0c1b5c1dcbeca63eb9
|
5
|
+
SHA512:
|
6
|
+
metadata.gz: 501708ba5f48c90ee0b0cd57a2a20f71af371ac23c2bfc55155a3ad965eea9f2d5e6067aed9b4be219736a14acec6f6fa4b425ec1ac52e0168f7ac87512762d0
|
7
|
+
data.tar.gz: 0af8b7b39adff33acb31a8db2f9b14035a98e69739adc4dbe9bed7ce0235574d7129d73cb802c313fdc20598363f25dff18b23bab578911f4bde3ff6c5d4cfd3
|
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
|
+
# MoacEth [![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
|
+
Fork from [ruby-eth](https://github.com/se3000/ruby-eth)
|
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_eth'
|
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 = MoacEth::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 = MoacEth::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 = MoacEth::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 = MoacEth::Key.encrypt key, 'p455w0rD'
|
52
|
+
```
|
53
|
+
|
54
|
+
### Transactions
|
55
|
+
|
56
|
+
Build a transaction from scratch:
|
57
|
+
|
58
|
+
```ruby
|
59
|
+
tx = MoacEth::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 = MoacEth::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
|
+
MoacEth::Utils.valid_address? address
|
89
|
+
```
|
90
|
+
|
91
|
+
Or add a checksum to an existing address:
|
92
|
+
|
93
|
+
```ruby
|
94
|
+
MoacEth::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
|
+
MoacEth.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
@@ -0,0 +1,62 @@
|
|
1
|
+
module MoacEth
|
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/moac_eth/gas.rb
ADDED
@@ -0,0 +1,113 @@
|
|
1
|
+
require 'json'
|
2
|
+
require 'scrypt'
|
3
|
+
|
4
|
+
class MoacEth::Key::Decrypter
|
5
|
+
include MoacEth::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 MoacEth::Key::Encrypter
|
5
|
+
include MoacEth::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
|
+
MoacEth::Key.new(priv: key).address
|
126
|
+
end
|
127
|
+
|
128
|
+
end
|
data/lib/moac_eth/key.rb
ADDED
@@ -0,0 +1,71 @@
|
|
1
|
+
module MoacEth
|
2
|
+
class Key
|
3
|
+
autoload :Decrypter, 'moac_eth/key/decrypter'
|
4
|
+
autoload :Encrypter, 'moac_eth/key/encrypter'
|
5
|
+
|
6
|
+
attr_reader :private_key, :public_key
|
7
|
+
|
8
|
+
def self.encrypt(key, password)
|
9
|
+
key = new(priv: key) unless key.is_a?(Key)
|
10
|
+
|
11
|
+
Encrypter.perform key.private_hex, password
|
12
|
+
end
|
13
|
+
|
14
|
+
def self.decrypt(data, password)
|
15
|
+
priv = Decrypter.perform data, password
|
16
|
+
new priv: priv
|
17
|
+
end
|
18
|
+
|
19
|
+
|
20
|
+
def initialize(priv: nil)
|
21
|
+
@private_key = MoneyTree::PrivateKey.new key: priv
|
22
|
+
@public_key = MoneyTree::PublicKey.new private_key, compressed: false
|
23
|
+
end
|
24
|
+
|
25
|
+
def private_hex
|
26
|
+
private_key.to_hex
|
27
|
+
end
|
28
|
+
|
29
|
+
def public_bytes
|
30
|
+
public_key.to_bytes
|
31
|
+
end
|
32
|
+
|
33
|
+
def public_hex
|
34
|
+
public_key.to_hex
|
35
|
+
end
|
36
|
+
|
37
|
+
def address
|
38
|
+
Utils.public_key_to_address public_hex
|
39
|
+
end
|
40
|
+
alias_method :to_address, :address
|
41
|
+
|
42
|
+
def sign(message)
|
43
|
+
sign_hash message_hash(message)
|
44
|
+
end
|
45
|
+
|
46
|
+
def sign_hash(hash)
|
47
|
+
loop do
|
48
|
+
signature = OpenSsl.sign_compact hash, private_hex, public_hex
|
49
|
+
return signature if valid_s? signature
|
50
|
+
end
|
51
|
+
end
|
52
|
+
|
53
|
+
def verify_signature(message, signature)
|
54
|
+
hash = message_hash(message)
|
55
|
+
public_hex == OpenSsl.recover_compact(hash, signature)
|
56
|
+
end
|
57
|
+
|
58
|
+
|
59
|
+
private
|
60
|
+
|
61
|
+
def message_hash(message)
|
62
|
+
Utils.keccak256 message
|
63
|
+
end
|
64
|
+
|
65
|
+
def valid_s?(signature)
|
66
|
+
s_value = Utils.v_r_s_for(signature).last
|
67
|
+
s_value <= Secp256k1::N/2 && s_value != 0
|
68
|
+
end
|
69
|
+
|
70
|
+
end
|
71
|
+
end
|