Class: EthereumBip44::Wallet

Inherits:
Object
  • Object
show all
Defined in:
lib/ethereum_bip44/wallet.rb

Overview

“44’”, # bip 44 “60’”, # coin, 0’: bitcoin, 60’: ethereum “0’”, # wallet “0” # 0 - public, 1 = private “0” # index

Class Method Summary collapse

Instance Method Summary collapse

Class Method Details

.from_mnemonic(mnemonic, path) ⇒ Object



16
17
18
19
# File 'lib/ethereum_bip44/wallet.rb', line 16

def self.from_mnemonic(mnemonic, path)
  seed = BipMnemonic.to_seed(mnemonic: mnemonic) # 2. 该助记词使用 PBKDF2 转化为种子(参见 BIP39)
  Wallet.from_seed(seed, path)
end

.from_seed(seed, path) ⇒ Object



10
11
12
13
14
# File 'lib/ethereum_bip44/wallet.rb', line 10

def self.from_seed(seed, path)
  master = MoneyTree::Master.new(seed_hex: seed) # 3. 种子用于使用 HMAC-SHA512 生成根私钥(参见 BIP32)
  wallet_node = master.node_for_path(path) # 4. 从该根私钥,导出子私钥(参见 BIP32),其中节点布局由BIP44设置
  Wallet.new(wallet_node)
end

.from_xpub(xpub) ⇒ Object



21
22
23
24
# File 'lib/ethereum_bip44/wallet.rb', line 21

def self.from_xpub(xpub)
  wallet_node = MoneyTree::Node.from_bip32(xpub)
  Wallet.new(wallet_node)
end

Instance Method Details

#get_bitcoin_address(index) ⇒ Object



34
35
36
37
# File 'lib/ethereum_bip44/wallet.rb', line 34

def get_bitcoin_address(index)
  node = @wallet_node.node_for_path("M/0/#{index}")
  node.to_address
end

#get_ethereum_address(index) ⇒ Object



39
40
41
42
43
44
45
46
47
48
49
50
51
52
# File 'lib/ethereum_bip44/wallet.rb', line 39

def get_ethereum_address(index)
  node = @wallet_node.node_for_path("M/0/#{index}")

  # from bitcoin public key to ethereum public key
  group = ECDSA::Group::Secp256k1
  public_key = ECDSA::Format::PointOctetString.decode(node.public_key.to_bytes, group) # a point
  ethereum_public = public_key.x.to_s(16) + public_key.y.to_s(16)

  # from ethereum public key to ethereum address
  bytes = EthereumBip44::Utils.hex_to_bin(ethereum_public)
  address_bytes = Digest::SHA3.new(256).digest(bytes)[-20..-1]
  address = EthereumBip44::Utils.bin_to_hex(address_bytes)
  EthereumBip44::Utils.prefix_hex(address)
end

#xprvObject



30
31
32
# File 'lib/ethereum_bip44/wallet.rb', line 30

def xprv
  @wallet_node.to_bip32(:private)
end

#xpubObject



26
27
28
# File 'lib/ethereum_bip44/wallet.rb', line 26

def xpub
  @wallet_node.to_bip32
end