ethereum_bip44 0.2.0 → 0.2.1
Sign up to get free protection for your applications and to get access to all the features.
- checksums.yaml +4 -4
- data/README.md +1 -1
- data/lib/ethereum_bip44.rb +1 -35
- data/lib/ethereum_bip44/version.rb +1 -1
- data/lib/ethereum_bip44/wallet.rb +62 -0
- metadata +2 -1
checksums.yaml
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
---
|
2
2
|
SHA1:
|
3
|
-
metadata.gz:
|
4
|
-
data.tar.gz:
|
3
|
+
metadata.gz: 2842344fa400cbf84fec2dd4f6e543116b417d78
|
4
|
+
data.tar.gz: 5f57d180b1188a630c749971856454a7f4552f77
|
5
5
|
SHA512:
|
6
|
-
metadata.gz:
|
7
|
-
data.tar.gz:
|
6
|
+
metadata.gz: b5c67c76fba449e85fb5ca20dc7ea08145c774c427ed612eb4668571eff0f98a6ad042484c2c5899d845685d78c9fbcadb944ffeee5936506bc4a789a7d5ce4f
|
7
|
+
data.tar.gz: c53d65b4474fe22dcac2f178a40aa1fec532250b1b6ce6f8cfb32eafcb12de1f57b888fffac05c5387db77aacc840e688af2501c89996dbc22027b54e7e5e18d
|
data/README.md
CHANGED
data/lib/ethereum_bip44.rb
CHANGED
@@ -5,41 +5,7 @@ require 'rlp'
|
|
5
5
|
require 'money-tree'
|
6
6
|
require 'bip_mnemonic'
|
7
7
|
require 'ethereum_bip44/utils'
|
8
|
+
require 'ethereum_bip44/wallet'
|
8
9
|
|
9
10
|
module EthereumBip44
|
10
|
-
|
11
|
-
# "44'", # bip 44
|
12
|
-
# "60'", # coin
|
13
|
-
# "0'", # wallet
|
14
|
-
# "0" # 0 - public, 1 = private
|
15
|
-
# "0" # index
|
16
|
-
class Wallet
|
17
|
-
|
18
|
-
def self.create_from_mnemonic(mnemonic, wallet_index)
|
19
|
-
seed = BipMnemonic.to_seed(mnemonic: mnemonic) # 2. 该助记词使用 PBKDF2 转化为种子(参见 BIP39)
|
20
|
-
EthereumBip44::Wallet.new(seed, wallet_index)
|
21
|
-
end
|
22
|
-
|
23
|
-
def initialize(seed, wallet_index)
|
24
|
-
master = MoneyTree::Master.new(seed_hex: seed) # 3. 种子用于使用 HMAC-SHA512 生成根私钥(参见 BIP32)
|
25
|
-
@wallet_node = master.node_for_path("m/44'/60'/#{wallet_index}'") # 4. 从该根私钥,导出子私钥(参见 BIP32),其中节点布局由BIP44设置
|
26
|
-
end
|
27
|
-
|
28
|
-
def get_address(index)
|
29
|
-
node = @wallet_node.node_for_path("M/0/#{index}")
|
30
|
-
|
31
|
-
# from bitcoin public key to ethereum public key
|
32
|
-
group = ECDSA::Group::Secp256k1
|
33
|
-
public_key = ECDSA::Format::PointOctetString.decode(node.public_key.to_bytes, group) # a point
|
34
|
-
ethereum_public = public_key.x.to_s(16) + public_key.y.to_s(16)
|
35
|
-
|
36
|
-
# from ethereum public key to ethereum address
|
37
|
-
bytes = EthereumBip44::Utils.hex_to_bin(ethereum_public)
|
38
|
-
address_bytes = Digest::SHA3.new(256).digest(bytes)[-20..-1]
|
39
|
-
address = EthereumBip44::Utils.bin_to_hex(address_bytes)
|
40
|
-
EthereumBip44::Utils.prefix_hex(address)
|
41
|
-
end
|
42
|
-
|
43
|
-
end
|
44
|
-
|
45
11
|
end
|
@@ -0,0 +1,62 @@
|
|
1
|
+
module EthereumBip44
|
2
|
+
|
3
|
+
# "44'", # bip 44
|
4
|
+
# "60'", # coin, 0': bitcoin, 60': ethereum
|
5
|
+
# "0'", # wallet
|
6
|
+
# "0" # 0 - public, 1 = private
|
7
|
+
# "0" # index
|
8
|
+
class Wallet
|
9
|
+
|
10
|
+
def self.from_seed(seed, path)
|
11
|
+
master = MoneyTree::Master.new(seed_hex: seed) # 3. 种子用于使用 HMAC-SHA512 生成根私钥(参见 BIP32)
|
12
|
+
wallet_node = master.node_for_path(path) # 4. 从该根私钥,导出子私钥(参见 BIP32),其中节点布局由BIP44设置
|
13
|
+
Wallet.new(wallet_node)
|
14
|
+
end
|
15
|
+
|
16
|
+
def self.from_mnemonic(mnemonic, path)
|
17
|
+
seed = BipMnemonic.to_seed(mnemonic: mnemonic) # 2. 该助记词使用 PBKDF2 转化为种子(参见 BIP39)
|
18
|
+
Wallet.from_seed(seed, path)
|
19
|
+
end
|
20
|
+
|
21
|
+
def self.from_xpub(xpub)
|
22
|
+
wallet_node = MoneyTree::Node.from_bip32(xpub)
|
23
|
+
Wallet.new(wallet_node)
|
24
|
+
end
|
25
|
+
|
26
|
+
def xpub
|
27
|
+
@wallet_node.to_bip32
|
28
|
+
end
|
29
|
+
|
30
|
+
def xprv
|
31
|
+
@wallet_node.to_bip32(:private)
|
32
|
+
end
|
33
|
+
|
34
|
+
def get_bitcoin_address(index)
|
35
|
+
node = @wallet_node.node_for_path("M/0/#{index}")
|
36
|
+
node.to_address
|
37
|
+
end
|
38
|
+
|
39
|
+
def get_ethereum_address(index)
|
40
|
+
node = @wallet_node.node_for_path("M/0/#{index}")
|
41
|
+
|
42
|
+
# from bitcoin public key to ethereum public key
|
43
|
+
group = ECDSA::Group::Secp256k1
|
44
|
+
public_key = ECDSA::Format::PointOctetString.decode(node.public_key.to_bytes, group) # a point
|
45
|
+
ethereum_public = public_key.x.to_s(16) + public_key.y.to_s(16)
|
46
|
+
|
47
|
+
# from ethereum public key to ethereum address
|
48
|
+
bytes = EthereumBip44::Utils.hex_to_bin(ethereum_public)
|
49
|
+
address_bytes = Digest::SHA3.new(256).digest(bytes)[-20..-1]
|
50
|
+
address = EthereumBip44::Utils.bin_to_hex(address_bytes)
|
51
|
+
EthereumBip44::Utils.prefix_hex(address)
|
52
|
+
end
|
53
|
+
|
54
|
+
private
|
55
|
+
|
56
|
+
def initialize(wallet_node)
|
57
|
+
@wallet_node = wallet_node
|
58
|
+
end
|
59
|
+
|
60
|
+
end
|
61
|
+
|
62
|
+
end
|
metadata
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
--- !ruby/object:Gem::Specification
|
2
2
|
name: ethereum_bip44
|
3
3
|
version: !ruby/object:Gem::Version
|
4
|
-
version: 0.2.
|
4
|
+
version: 0.2.1
|
5
5
|
platform: ruby
|
6
6
|
authors:
|
7
7
|
- wuminzhe
|
@@ -150,6 +150,7 @@ files:
|
|
150
150
|
- lib/ethereum_bip44.rb
|
151
151
|
- lib/ethereum_bip44/utils.rb
|
152
152
|
- lib/ethereum_bip44/version.rb
|
153
|
+
- lib/ethereum_bip44/wallet.rb
|
153
154
|
homepage: https://github.com/wuminzhe/ethereum-bip44.rb
|
154
155
|
licenses:
|
155
156
|
- MIT
|