Bitcoin
This example shows how to use the Builder Vault TSM as a simple Bitcoin wallet using the btcd library.
The example requires that you have access to a Builder Vault instance that is configured to allow signing with ECDSA keys. You can for example do a local deployment of a Builder Vault instance, or use Blockdaemon's hosted sandbox TSM.
The code example includes the following:
- The first time the wallet is started, it uses the Builder Vault to generate a master key. The master key ID is stored to file. Subsequently, the key ID is loaded from file.
- The wallet instructs the Builder Vault to use the derived address corresponding to the chain path
m/42/5
. This path is somewhat arbitrary, but illustrates how to use the Builder Vault with keys derived from a master key. See our section about key derivation for more about key derivation. - The wallet uses the btcd library and Blockdaemon's API Suite to access the Bitcoin network. This is used to read the current balance for the Bitcoin address, select UTXOs for new transactions, and to submit the signed transactions.
- A transaction is build based on input submitted on the command line and data obtained from the Bitcoin blockchain. Then the Builder Vault instance is used to sign the transaction, and finally the signed transaction is submitted to the blockchain.
NoteWhen you run this example the first time, a new random wallet address will be created, and the balance will be 0 BTC. This will cause the program to print out the wallet address and stop. To actually transfer funds, you will need to first insert some test funds on the wallet address and then run the program again.
In this example we use ECDSA signatures. Builder Vault also supports BIP340 (Schnorr signatures over the secp256k1 curve). The example can easily be adapted to use BIP40 signatures.
Blockchain Handler
We use Blockdaemon’s BD Transact APIs to interact with the Bitcoin network. Native APIs do not allow listing UTXOs by default, so some advanced handler is needed. We use Blockdaemon’s BD Transact APIs to access the Bitcoin test network:
apiKey := strings.TrimSpace(os.Getenv("API_KEY"))
ubiquityUniversalURL := "https://svc.blockdaemon.com/universal/v1/bitcoin/testnet/"
req, err := http.NewRequest(http.MethodGet, blockchain.ubiquityUniversalURL+path, nil)
if err != nil {
panic(err)
}
req.Header.Set("Content-Type", "application/json")
req.Header.Set("Authorization", "Bearer "+blockchain.apiKey)
response, err := http.DefaultClient.Do(req)
if err != nil {
panic(err)
}
responseBody, err := io.ReadAll(response.Body)
if err != nil {
panic(err)
}
Alternatively, you can modify the example to connect to a local Bitcoin node you host or use another third-party Bitcoin API provider instead of Blockdaemon’s BD Transact APIs. They will need to provide a listing of UTXOs, which is disabled by default in some instances.
Running the example
The example will transfer 0.00001 BTC to a default destination address by default. If you want a different address or amount, you can provide these as parameters:
go run example.go --satoshi=10000 --dstAddress=mgp2FpQvn3EQkFpLSMpNKypiVBnaJvnqJN
Code Example
You can find the final code example in our demo repository (Go). It also contains a Java example.
Updated 19 days ago