example: watch only wallet

This commit is contained in:
Mark Tyneway 2019-02-03 10:00:15 -08:00 committed by Matthew Zipkin
parent 375965c8a6
commit de8b2f7ba2
No known key found for this signature in database
GPG Key ID: E7E2984B6289C93A
2 changed files with 64 additions and 0 deletions

View File

@ -0,0 +1,62 @@
const {HDPrivateKey,Network} = require('bcoin');
const {Mnemonic} = require('bcoin/lib/hd');
const {WalletClient} = require('bclient');
(async () => {
// use well know test passphrase
const phrase = [
'abandon',
'abandon',
'abandon',
'abandon',
'abandon',
'abandon',
'abandon',
'abandon',
'abandon',
'abandon',
'abandon',
'about',
].join(' ');
const network = Network.get('main');
const mnemonic = Mnemonic.fromPhrase(phrase);
// m'
const priv = HDPrivateKey.fromMnemonic(mnemonic);
// m'/44'
const bip44Key = priv.derive(44, true);
// m'/44'/0'
const bitcoinKey = bip44Key.derive(0, true);
// m'/44'/0'/0'
const accountKey = bitcoinKey.derive(0, true);
// account extended public key
// https://github.com/bitcoin/bips/blob/master/bip-0032.mediawiki#Serialization_format
const xpub = accountKey.xpubkey(network.type);
// recommended to use hardware wallet to derive keys
// see github.com/bcoin-org/bledger
const client = new WalletClient({
network: network.type,
port: network.walletPort,
});
// create watch only wallet
// the wallet will generate lookahead
// addresses from the account extended public key
// and can find spendable coins in the blockchain state
const response = await client.createWallet('mywallet', {
accountKey: xpub,
watchOnly: true,
});
})();

View File

@ -25,6 +25,7 @@ Welcome to the bcoin docs!
- [Miner with WorkerPool][example-miner-configs]
- [Create and Sign TX][example-tx-create-sign]
- [Get Transaction from Chain][example-tx-from-chain]
- [Create Watch Only Wallet][example-watch-only-wallet]
[getting-started]: Beginner's-Guide.md
@ -49,3 +50,4 @@ Welcome to the bcoin docs!
[example-simple-fullnode]: Examples/fullnode.js
[example-tx-create-sign]: Examples/create-sign-tx.js
[example-tx-from-chain]: Examples/get-tx-from-chain.js
[example-watch-only-wallet]: Examples/watch-only-wallet.js