Class: ExoCrypto::Bip44WalletProvider
- Inherits:
-
Object
- Object
- ExoCrypto::Bip44WalletProvider
- Defined in:
- lib/exocrypto/bip44_wallet_provider.rb
Overview
Constant Summary collapse
- TESTNET_TICKER =
'TESTNET'
Class Method Summary collapse
- .address_details(sub_wallet, is_testnet, include_private) ⇒ Object
- .btc_address_details_of(seed, path, is_testnet = false) ⇒ Object
- .btc_base58_to_int(base58_val) ⇒ Object
- .btc_decode_base58(base58_val) ⇒ Object
- .btc_encode_base58(hex) ⇒ Object
- .btc_int_to_base58(int_val, leading_zero_bytes = 0) ⇒ Object
- .btc_privkey_of_wif(wif, is_compressed = false) ⇒ Object
- .btc_pubkey_of_privkey(hex, is_compressed = false, is_testnet = false) ⇒ Object
- .btc_pubkey_of_wif(wif, is_compressed = false, is_testnet = false) ⇒ Object
- .btc_pubkey_of_wif_ecdsa(wif, is_compressed = false, is_testnet = false) ⇒ Object
- .btc_wif_of_privkey(hex, is_compressed = false, is_testnet = false) ⇒ Object
- .checksum(hex) ⇒ Object
- .coins ⇒ Object
- .create_subwallet(xpub, sub_path, is_testnet = false) ⇒ Object
- .create_wallet(coin_ticker = 'BTC') ⇒ Object
- .from_absolute_path(path) ⇒ Object
- .int_to_hex(int) ⇒ Object
- .master_details(mnemonic, wallet, is_testnet) ⇒ Object
- .precision_of(ticker) ⇒ Object
- .ripemd160(hex) ⇒ Object
- .seed_of(mnemonic, password = nil) ⇒ Object
- .sha256(hex) ⇒ Object
- .ticker_map ⇒ Object
Class Method Details
.address_details(sub_wallet, is_testnet, include_private) ⇒ Object
60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 |
# File 'lib/exocrypto/bip44_wallet_provider.rb', line 60 def self.address_details(sub_wallet, is_testnet, include_private) if include_private { :bitcoin_address => sub_wallet.bitcoin_address(testnet: is_testnet), :ethereum_address => sub_wallet.ethereum_address, :public_hex => sub_wallet.public_key, :private_hex => sub_wallet.private_key, :private_wif => sub_wallet.wif(testnet: is_testnet) } else { :bitcoin_address => sub_wallet.bitcoin_address(testnet: is_testnet), :ethereum_address => sub_wallet.ethereum_address, :public_hex => sub_wallet.public_key } end end |
.btc_address_details_of(seed, path, is_testnet = false) ⇒ Object
126 127 128 129 130 131 132 133 |
# File 'lib/exocrypto/bip44_wallet_provider.rb', line 126 def self.btc_address_details_of(seed, path, is_testnet=false) wallet = Bip44::Wallet.from_seed(seed, path) details = Bip44WalletProvider.address_details(wallet, is_testnet, true) details[:address] = details[:bitcoin_address] details.delete(:bitcoin_address) details.delete(:ethereum_address) details end |
.btc_base58_to_int(base58_val) ⇒ Object
189 190 191 192 193 194 195 196 197 198 |
# File 'lib/exocrypto/bip44_wallet_provider.rb', line 189 def self.btc_base58_to_int(base58_val) alpha = '123456789ABCDEFGHJKLMNPQRSTUVWXYZabcdefghijkmnopqrstuvwxyz' int_val, base = 0, alpha.size base58_val.reverse.split(//).each_with_index do |char, index| char_index = alpha.index(char) int_val += char_index * base**index end int_val end |
.btc_decode_base58(base58_val) ⇒ Object
200 201 202 203 204 205 206 |
# File 'lib/exocrypto/bip44_wallet_provider.rb', line 200 def self.btc_decode_base58(base58_val) nzeroes = base58_val.chars.find_index { |c| c != '1' } || base58_val.length - 1 prefix = nzeroes < 0 ? '' : '00' * nzeroes decoded = Bip44WalletProvider.int_to_hex(Bip44WalletProvider.btc_base58_to_int(base58_val)) [prefix + decoded].pack('H*') end |
.btc_encode_base58(hex) ⇒ Object
184 185 186 187 |
# File 'lib/exocrypto/bip44_wallet_provider.rb', line 184 def self.btc_encode_base58(hex) leading_zero_bytes = (hex.match(/^([0]+)/) ? $1 : '').size / 2 ('1' * leading_zero_bytes) + Bip44WalletProvider.btc_int_to_base58(hex.to_i(16)) end |
.btc_int_to_base58(int_val, leading_zero_bytes = 0) ⇒ Object
173 174 175 176 177 178 179 180 181 182 |
# File 'lib/exocrypto/bip44_wallet_provider.rb', line 173 def self.btc_int_to_base58(int_val, leading_zero_bytes=0) alpha = '123456789ABCDEFGHJKLMNPQRSTUVWXYZabcdefghijkmnopqrstuvwxyz' base58_val, base = '', alpha.size while int_val > 0 int_val, remainder = int_val.divmod(base) base58_val = alpha[remainder] + base58_val end base58_val end |
.btc_privkey_of_wif(wif, is_compressed = false) ⇒ Object
218 219 220 221 222 223 224 |
# File 'lib/exocrypto/bip44_wallet_provider.rb', line 218 def self.btc_privkey_of_wif(wif, is_compressed=false) result = Bip44WalletProvider.btc_decode_base58(wif)[1..-5].unpack('H*').first if is_compressed result = result[0...-2] end result end |
.btc_pubkey_of_privkey(hex, is_compressed = false, is_testnet = false) ⇒ Object
310 311 312 313 314 |
# File 'lib/exocrypto/bip44_wallet_provider.rb', line 310 def self.btc_pubkey_of_privkey(hex, is_compressed=false, is_testnet=false) priv = Bip44WalletProvider.btc_wif_of_privkey(hex, is_compressed, is_testnet) Bip44WalletProvider.btc_pubkey_of_wif(priv, is_compressed, is_testnet) end |
.btc_pubkey_of_wif(wif, is_compressed = false, is_testnet = false) ⇒ Object
226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 |
# File 'lib/exocrypto/bip44_wallet_provider.rb', line 226 def self.btc_pubkey_of_wif(wif, is_compressed=false, is_testnet=false) priv_key = Bip44WalletProvider.btc_privkey_of_wif(wif, is_compressed) group = OpenSSL::PKey::EC::Group.new('secp256k1') public_key = group.generator.mul(OpenSSL::BN.new(priv_key, 16)) decompressed_public_key_hex = public_key.to_bn.to_s(16) # NOTE: for compression public_key_hex = decompressed_public_key_hex if is_compressed x = decompressed_public_key_hex[2..-1][0..63] y = decompressed_public_key_hex[2..-1][64..-1] leader = y.to_i(16) % 2 == 0 ? '02' : '03' public_key_hex = leader + x end public_key_hash = Bip44WalletProvider.ripemd160(Bip44WalletProvider.sha256(public_key_hex)) network = is_testnet ? '6f' : '00' data = network + public_key_hash public_address = Bip44WalletProvider.btc_encode_base58(data + Bip44WalletProvider.checksum(data)) { :decompressed_public_key => decompressed_public_key_hex, :public_key => public_key_hex, :bitcoin_address => public_address } end |
.btc_pubkey_of_wif_ecdsa(wif, is_compressed = false, is_testnet = false) ⇒ Object
254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 |
# File 'lib/exocrypto/bip44_wallet_provider.rb', line 254 def self.btc_pubkey_of_wif_ecdsa(wif, is_compressed=false, is_testnet=false) priv_key = Bip44WalletProvider.btc_privkey_of_wif(wif, is_compressed) # kpub = kpriv * G curve = ECDSA::Group::Secp256k1 pub_key_point = curve.generator.multiply_by_scalar(priv_key.to_i(16)) # # uncompressed, pub key is in form 0x04 + x + y # compressed, pub key is in form # 0x02 + x if y is even # 0x03 + x if y is odd # Getting encodings right is difficult...pub.x is a Bignum. Bignum + Bignum is # a biggernum, which isn't really what we want. We want string concatenation. To do that, # we translate to hex, concatenate the hex, and pack it back into a string to # concatenate with our leading byte # # pub.x is a Bignum, # so we must concatenate our compression byte with the hex representation of pub_key.x # decompressed_pub_key = "\x04" + [pub_key_point.x.to_s(16)].pack('H*') + [pub_key_point.y.to_s(16)].pack('H*') pub_key = decompressed_pub_key if is_compressed leader = pub_key_point.y % 2 == 0 ? "\x02" : "\x03" pub_key = leader + [pub_key_point.x.to_s(16)].pack('H*') end # ripe160(sha256(pub_key)) pub_key_hash = Digest::RMD160.digest(Digest::SHA256.digest(pub_key)) # # prepend version to our double hashed pub key, append checksum # Bitcoin: 0x00 # Testnet: 0x6f # network = is_testnet ? "\x6f" : "\x00" pub_key_hash_and_version = network + pub_key_hash # add checksum pub_key_hash_and_version_str = pub_key_hash_and_version.unpack('H*').first pub_key_hash_and_version_and_checksum = pub_key_hash_and_version_str + Bip44WalletProvider.checksum( pub_key_hash_and_version_str) # base58 encode version, hash, checksum pub_addr = Bip44WalletProvider.btc_encode_base58(pub_key_hash_and_version_and_checksum) { :decompressed_public_key => decompressed_pub_key.unpack("H*").first, :public_key => pub_key.unpack("H*").first, :bitcoin_address => pub_addr } end |
.btc_wif_of_privkey(hex, is_compressed = false, is_testnet = false) ⇒ Object
208 209 210 211 212 213 214 215 216 |
# File 'lib/exocrypto/bip44_wallet_provider.rb', line 208 def self.btc_wif_of_privkey(hex, is_compressed=false, is_testnet=false) priv_key_version = is_testnet ? 'ef' : '80' data = priv_key_version + hex if is_compressed data += '01' end Bip44WalletProvider.btc_encode_base58(data + Bip44WalletProvider.checksum(data)) end |
.checksum(hex) ⇒ Object
160 161 162 |
# File 'lib/exocrypto/bip44_wallet_provider.rb', line 160 def self.checksum(hex) Bip44WalletProvider.sha256(Bip44WalletProvider.sha256(hex))[0...8] end |
.coins ⇒ Object
51 52 53 |
# File 'lib/exocrypto/bip44_wallet_provider.rb', line 51 def self.coins Bip44WalletProvider.ticker_map.keys.to_set.delete(Bip44WalletProvider::TESTNET_TICKER) end |
.create_subwallet(xpub, sub_path, is_testnet = false) ⇒ Object
105 106 107 108 109 110 111 112 113 114 |
# File 'lib/exocrypto/bip44_wallet_provider.rb', line 105 def self.create_subwallet(xpub, sub_path, is_testnet=false) wallet = Bip44::Wallet.from_xpub(xpub) sub_wallet = wallet.sub_wallet(sub_path) { :obj => sub_wallet, :path => sub_path, :details => Bip44WalletProvider.address_details(sub_wallet, is_testnet, false) } end |
.create_wallet(coin_ticker = 'BTC') ⇒ Object
88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 |
# File 'lib/exocrypto/bip44_wallet_provider.rb', line 88 def self.create_wallet(coin_ticker='BTC') words = BipMnemonic.to_mnemonic(bits: 128) seed = BipMnemonic.to_seed(mnemonic: words) coin = ticker_map[coin_ticker][:slip] path = "m/44'/#{coin}'/0'" wallet = Bip44::Wallet.from_seed(seed, path) testnet = coin_ticker == Bip44WalletProvider::TESTNET_TICKER { :obj => wallet, :seed_hex => seed, :path => path, :details => Bip44WalletProvider.master_details(words, wallet, testnet) } end |
.from_absolute_path(path) ⇒ Object
135 136 137 138 139 140 141 142 143 144 145 |
# File 'lib/exocrypto/bip44_wallet_provider.rb', line 135 def self.from_absolute_path(path) elements = path.split('/') derivation = "m/#{elements.take(4).drop(1).join('/')}" relative = "m/#{elements.drop(4).join('/')}" { :absolute_path => path, :derivation => derivation, :relative_path => relative } end |
.int_to_hex(int) ⇒ Object
164 165 166 167 168 169 170 171 |
# File 'lib/exocrypto/bip44_wallet_provider.rb', line 164 def self.int_to_hex(int) hex = int.to_s(16) # # The hex string must always consist of an even number of characters, # otherwise the pack() parsing will be misaligned. # (hex.length % 2 == 0) ? hex : ('0' + hex) end |
.master_details(mnemonic, wallet, is_testnet) ⇒ Object
78 79 80 81 82 83 84 85 86 |
# File 'lib/exocrypto/bip44_wallet_provider.rb', line 78 def self.master_details(mnemonic, wallet, is_testnet) { :mnemonic => mnemonic, :seed_hex => BipMnemonic.to_seed(mnemonic: mnemonic), :xpub => wallet.xpub(testnet: is_testnet), :xprv => wallet.xprv(testnet: is_testnet), :details => Bip44WalletProvider.address_details(wallet, is_testnet, true) } end |
.precision_of(ticker) ⇒ Object
55 56 57 |
# File 'lib/exocrypto/bip44_wallet_provider.rb', line 55 def self.precision_of(ticker) Bip44WalletProvider.ticker_map[ticker][:precision] end |
.ripemd160(hex) ⇒ Object
156 157 158 |
# File 'lib/exocrypto/bip44_wallet_provider.rb', line 156 def self.ripemd160(hex) Digest::RMD160.hexdigest([hex].pack('H*')) end |
.seed_of(mnemonic, password = nil) ⇒ Object
116 117 118 119 120 121 122 123 124 |
# File 'lib/exocrypto/bip44_wallet_provider.rb', line 116 def self.seed_of(mnemonic, password=nil) #BipMnemonic.to_seed(mnemonic: mnemonic) OpenSSL::PKCS5.pbkdf2_hmac(mnemonic, "mnemonic#{password}", 2048, 64, OpenSSL::Digest::SHA512.new) .unpack('H*').first end |
.sha256(hex) ⇒ Object
see: gobittest.appspot.com/PrivateKey
https://github.com/dougal/base58/blob/master/lib/base58.rb
http://royalforkblog.github.io/2014/07/31/address-gen
152 153 154 |
# File 'lib/exocrypto/bip44_wallet_provider.rb', line 152 def self.sha256(hex) Digest::SHA256.hexdigest([hex].pack('H*')) end |
.ticker_map ⇒ Object
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 |
# File 'lib/exocrypto/bip44_wallet_provider.rb', line 22 def self.ticker_map { 'TESTNET' => { :slip => 1, :precision => 8 }, 'BTC' => { :slip => 0, :precision => 8 }, 'ETH' => { :slip => 60, :precision => 18 }, 'DASH' => { :slip => 5, :precision => 8 }, 'DOGE' => { :slip => 3, :precision => 8 }, 'LTC' => { :slip => 2, :precision => 8 } } end |