-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathhd_wallet.rb
74 lines (56 loc) · 2.77 KB
/
hd_wallet.rb
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
require 'money-tree'
require 'digest'
#
# Every key in the HD wallet is derived from this root seed
# If you have a backup of this root seed, you can re-create the entire wallet.
# The root seed is mostly represented by a “mnemonic word sequence”
# to make it easier for people to store.
#
# Use can generate master private key by using libbitcoin explore:
# - Set configuration file path for libbitcoin explore
# export BX_CONFIG="/usr/local/etc/libbitcoin/testnet.cfg" to .bash_profile or .zshrc
# - $ bx seed | bx hd-new
# - @master_node = MoneyTree::Node.from_bip32(master_private_key)
# Or getting seed EC-HEX 64 hexadecimal digits
seed = Digest::SHA256.digest('happy sad love password haha hihi').unpack("H*")[0]
puts seed
# Import the seed for creating a new master node.
@master = MoneyTree::Master.new seed_hex: seed
p @master.to_serialized_address(:private) # DEPRECATED.
p @master.to_bip32(:private) # new
# xprv9s21ZrQH143K37WQTsCNcUjyLv4Qmz8G8Q4R3817euzJ7iEuiA8HUpYut6VtCKEQpiTywzhEKnJbhSdE9sUfbvqqDdgtNBC7PGMQAxf524p
# generate the m/0
@node = @master.node_for_path "m/0"
# generate the m/0 extended public key
p @node.to_serialized_address # old
p @node.to_bip32 # new
# generate the m/0 extended private key
p @node.to_serialized_address :private
# xprv9tvWjMnzKSw4RLufcHyQqjpsLXm8thZ33WthMFrabLcWZnmHC3xGokAEzgX8dTSKgX5KtJJsKwL8R96dBcMm2JsrnJjLnJ5YVmiAvDKR98J
# show the private key of m/0 as a WIF
p @node.private_key.to_wif
# Ky1jAmxVjXSXiHSdbE2NWKxzwCrQpZCZmcFv3c4kM3Xi1ad9Rd4G
# show the bitcoin address of M/0
p @node.to_address
# 1NsLs3trs7Y5aHjuLysXCVSovx7EUs8j6g
# generate m/0/1'/2
@node = @master.node_for_path "m/0/1'/2"
p @node.private_key.to_wif
p @node.to_address
# L4rxY47cbMM7exZ3adKXB4STPoLKMktmQZRwM84dU45bBiAvUXzM
# 1P2ioDZfrAeLB7CdkN9ho7pFBJQmESJkBC
# Bitcoin addresses can be generated by a public wallet key.
# but private keys for spending bitcoins can only be generated using a private wallet key.
@node = @master.node_for_path("M/0/3") # or "m/0/3.pub" or "M/0/3.pub"...these are equivalent
p @node.to_bip32
# xpub6BXtqq512LSJtV843RSHoxkJevNk8AwFZQR4WLvr751vSneePouH4Gt2bPPGU6QdzvqYPKc8ed2KW6KWk8aaZhZ28R8ydUpFWW7HrminPwK
@node.to_serialized_address(:private)
# raises MoneyTree::Node::PrivatePublicMismatch error
# Now the “extended public key” can be installed on a web server
# and can generate bitcoin addresses as much as we want.
# The web server will not have any private keys that would be vulnerable to theft.
# Without HD wallets, the only way to do this is generating a plenty of addresses on a secure server
# and then load them on the web server.
# BIP0044 Multi-Account Hierarchy for Deterministic Wallets
# https://github.com/bitcoin/bips/blob/master/bip-0044.mediawiki#path-levels
# m / 44' / coin_type' / account' / change / address_index