Solana
This example shows how to use the Builder Vault TSM as a simple Solana wallet using the Solana web3.js library.
The example requires that you have access to a Builder Vault instance that is configured to allow signing with EdDSA keys. You can for example do a local deployment of a Builder Vault instance, or use Blockdaemon's hosted sandbox TSM.
The code first tries to read a master key ID from a file. If the file does not exist, a new master key is generated in the Builder Vault, and the new master key ID is saved to the file for later use. The derived public key for the derivation path m/44/501
is then obtained from the Builder Vault and converted to a Solana base-58 account address.
Then, we initialize a web3.js client. This requires a URL to a Solana node. In the example, we use Blockdaemon’s Ubiquity Native API to get access to a Solana node in the Testnet test network:
// Initialize Solana client
const apiKey = process.env.API_KEY
if (!apiKey) {
console.log('API_KEY environment variable not set')
return
}
const solanaNodeUrl = `https://svc.blockdaemon.com/solana/testnet/native?apiKey=${apiKey}`
let connection = new web3.Connection(solanaNodeUrl, "confirmed")
If you use Ubiquity, you need to obtain a Ubiquity API key from Blockdaemon and make sure that it is available as the environment variable API_KEY
, for example by running:
export API_KEY=put_your_ubiquity_api_key_here
Alternatively, you can modify the example so that it connects to a local Solana node that you host yourself or use another third-party Solana API provider instead of Blockdaemon Ubiquity.
Once connected to the Solana network, we use the web3.js client to get the balance of the account defined by the address m/44/501
, as well as the latest block hash.
Then, we generate an unsigned transaction that sends 0.000001 SOL to a destination address. In the example, you can specify a different address or amount.
We create the payload to be signed, sign it using the Builder Vault, and construct the signed transaction. Finally, we use the web3.js client to publish the signed transaction to the Solana network.
The example uses the BIP32 derivation path m/44/501
. See our section about key derivation for more about this. See the section about key import if you want to migrate a key from an external wallet, such as Metamask, to the TSM.
Note
When you run this example the first time, a new random account will be created, and the balance will be 0 SOL. This will cause the program to print out the account address and stop. To actually transfer funds, you will need to first insert some test funds on the account address and then run the program again. Use a Testnet faucet such as https://solfaucet.com
The full code example can be found in our demo repository here.
Updated 15 days ago