Bitcoin

Using Builder Vault TSM as a Bitcoin Wallet

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.

Code Example

The code example includes the following:

📘

Note

When 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.