oleganza / CoreBitcoin

Awesome Bitcoin toolkit for ObjC and Swift
Do What The F*ck You Want To Public License
719 stars 255 forks source link

Watch only wallet with BTCKeychain? #106

Closed Fonta1n3 closed 6 years ago

Fonta1n3 commented 6 years ago

I want to only store the extended public key in the wallet for future address generation. Is it possible using this library to generate unlimited child addresses from the extended public key string only?

I know how to do it with the whole KeyChain but not just the extended public key.

Thanks in advanced!

oleganza commented 6 years ago

Yes, you can instantiate BTCKeychain with an xpub:

// Initializes with a base58-encoded extended public or private key.
// Inherits the network from the formatted key.
- (id) initWithExtendedKey:(NSString*)extendedKey;
Fonta1n3 commented 6 years ago

Thanks very much! For anyone using swift heres the code i used:

var data = BigUInt(userRandomness).serialize()

let mnemonic = BTCMnemonic.init(
                entropy: data,
                password: "",
                wordListType: BTCMnemonicWordListType.english
               )

let keychain = mnemonic?.keychain
let xpub = keychain?.extendedPublicKey
let childKeychain = BTCKeychain.init(extendedKey: xpub)
let newAddress = childKeychain?.key.address.string
oleganza commented 6 years ago

keychain.extendedPublicKey already returns a string, so no need to call .description (which is only for debugging and does not guarantee the format). I have edited your snippet with this fix.

Same for address.description — it should be address.string.