oleganza / btcruby

Comprehensive Bitcoin and Open Assets toolkit for Ruby
MIT License
72 stars 28 forks source link

Support for BIP49 (segwit) address? #24

Open ecdsa521 opened 6 years ago

ecdsa521 commented 6 years ago

Hello

Is it possible to generate segwit style addresses as defined in BIP49? It should be compatible with BIP44 and only require change to derivation path, but I am not able to generate proper segwit addresses. It also seems impossible to generate addresses using custom derivation path without private key part?

k=BTC::Keychain.new :xpub => "xpub6CUGRUonZSQ4TWtTMmzXdrXDtypWKiKrhko4egpiMZbpiaQL2jkwSB1icqYh2cfDfVxdx4df189oLKnC5fSwqPfgyP3hooxujYzAu3fDVmz"
k.derived_key("m/49'/0'/0'/1'").address

The result is BTC::BTCError: Not possible to derive a hardened keychain without a private key (index: 49).

If I do initialize it with xprv instead, for example

k=BTC::Keychain.new :xprv => "xprv9s21ZrQH143K4KqQx9Zrf1eN8EaPQVFxM2Ast8mdHn7GKiDWzNEyNdduJhWXToy8MpkGcKjxeFWd8oBSvsz4PCYamxR7TX49pSpp3bmHVAY"
k.derived_key("m/49'/0'/0'/1'").address
# => #<BTC::PublicKeyAddress:1AgPR8sjddSPY4ZQsZet5JxjEJTLrSMqVK>

It does generate address, but not segwit style.

Can you please tell me if I am doing something wrong? Ideally I want to generate segwit style addresses with only xpub.

iamajvillalobos commented 6 years ago

I think you're using it incorrectly. I tried to use it like this and it works perfectly. You don't need to supply the full path but only the index.

wallet = BTC::Keychain.new(xpub: "xpub6EEtgK5WdTq4FK4BwU38C8g2FKAANdMoxCoKXwkjFSm3p2qCqbGQPehPBKi5PdeGM3JqZR4y5mFsAexSCCS8vogQpv9CV6vFPeznEUTjvMX")
wallet.derived_key(0).address.to_s  # 151BbmtNVLduGhumeq1vuxFvp5PejuQdtC

Which is the same when I use tool like https://coinomi.com/recovery-phrase-tool.html screenshot-2018-3-28 bip39 - mnemonic code

ecdsa521 commented 6 years ago

@iamarmanjon this is old style address, not Segwit.

Segwit addresses are either old style, embedded in P2SH, starting with "3", or new style (bech32) starting with "bc1"

sha-0 commented 6 years ago

Any plans to add SegWit (BIP49/BIP84) support? @oleganza