-
Notifications
You must be signed in to change notification settings - Fork 277
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat: add example for createDescriptorWalletWithWalletPassphrase
Issue: BTC-0
- Loading branch information
1 parent
8c7e4dc
commit 40465df
Showing
3 changed files
with
84 additions
and
2 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,79 @@ | ||
/** | ||
* Create a multi-sig wallet at BitGo. | ||
* This makes use of the convenience function generateWallet | ||
* | ||
* This tool will help you see how to use the BitGo API to easily create a wallet. | ||
* In this form, it creates 2 keys on the host which runs this example. | ||
* It is HIGHLY RECOMMENDED that you GENERATE THE KEYS ON SEPARATE MACHINES for real money wallets! | ||
* | ||
* To perform more advanced features, such as encrypting keys yourself, please look at createWalletAdvanced.js | ||
* | ||
* Copyright 2022, BitGo, Inc. All Rights Reserved. | ||
*/ | ||
|
||
/** | ||
* Add Low Fee webhook to a wallet. | ||
* | ||
* Copyright 2022 BitGo, Inc. All Rights Reserved. | ||
*/ | ||
|
||
import { BitGoAPI } from '@bitgo/sdk-api'; | ||
import { Tbtc } from '@bitgo/sdk-coin-btc'; // Replace with your given coin (e.g. Ltc, Tltc) | ||
import { AbstractUtxoCoin, descriptor } from '@bitgo/abstract-utxo'; | ||
require('dotenv').config({ path: '../../.env' }); | ||
|
||
const bitgo = new BitGoAPI({ | ||
accessToken: process.env.TESTNET_ACCESS_TOKEN, | ||
env: 'test', // Change this to env: 'production' when you are ready for production | ||
}); | ||
|
||
// Set the coin name to match the blockchain and network | ||
// btc = bitcoin, tbtc = testnet bitcoin | ||
const coin = 'tbtc'; | ||
bitgo.register(coin, Tbtc.createInstance); | ||
|
||
// TODO: set a label for your new wallet here | ||
const label = 'Example Descriptor Wallet'; | ||
|
||
// TODO: set your passphrase for your new wallet here | ||
const passphrase = 'test_wallet_passphrase'; | ||
|
||
// TODO: set your enterprise ID for your new wallet here | ||
const enterprise = 'your_enterprise_id'; | ||
|
||
async function main() { | ||
console.log( | ||
// this wrapper creates three keys: userKey, backupKey, and bitgoKey | ||
// at the moment, this is a somewhat artificial requirement from wallet platform | ||
// in the future, we will allow for more flexible key generation | ||
await descriptor.createWallet.createDescriptorWalletWithWalletPassphrase( | ||
bitgo, | ||
bitgo.coin(coin) as AbstractUtxoCoin, | ||
{ | ||
label, | ||
walletPassphrase: passphrase, | ||
enterprise, | ||
descriptorsFromKeys(userKey, cosigners) { | ||
// userKey is backed up at BitGo with the wallet passphrase | ||
// cosigners are backup key and BitGo key | ||
const xpubs = [userKey, ...cosigners].map((key) => key.neutered().toBase58()); | ||
|
||
return [ | ||
// here is a single-sig descriptor for the user key | ||
descriptor.createNamedDescriptorWithSignature('SingleSigWpkh', `wpkh(${xpubs[0]}/*)`, userKey), | ||
// here is a 2of3 multisig descriptor for the backup key and BitGo key | ||
descriptor.createNamedDescriptorWithSignature( | ||
'MultiSigWsh', | ||
`wsh(multi(2,${xpubs.map((xpub) => `${xpub}/*`).join(',')})`, | ||
userKey | ||
), | ||
// equivalent to the above, but returns two descriptors (external and internal) | ||
...descriptor.createWallet.DefaultWsh2Of3(userKey, cosigners), | ||
]; | ||
}, | ||
} | ||
) | ||
); | ||
} | ||
|
||
main().catch((e) => console.log(e)); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,6 +1,6 @@ | ||
export { Miniscript, Descriptor } from '@bitgo/wasm-miniscript'; | ||
export { assertDescriptorWalletAddress } from './assertDescriptorWalletAddress'; | ||
export { NamedDescriptor } from './NamedDescriptor'; | ||
export { NamedDescriptor, createNamedDescriptorWithSignature, hasValidSignature } from './NamedDescriptor'; | ||
export { isDescriptorWallet, getDescriptorMapFromWallet } from './descriptorWallet'; | ||
export { getPolicyForEnv } from './validatePolicy'; | ||
export * as createWallet from './createWallet'; |