[go: up one dir, main page]

Polkadot

This example shows how to use the Builder Vault TSM as a simple Polkadot wallet using the substrate library. However, this is not completely up to date, so the example must specify the newest structures for it to work.

The example requires that you have access to a Builder Vault instance. You can, for example, do a local deployment of a Builder Vault instance or use Blockdaemon's hosted sandbox. The Builder Vault instance must be configured to use the Ed25519 signing scheme. Builder Vault also supports the two other signature types accepted by Polkadot (ECDSA and sr25519), - using one of these instead of Ed25519 only requires minor changes to the example.

The code goes through the following steps:

  • Create a transfer based on the input
  • Create a wallet. The ID is loaded from a file if a key has been generated. Otherwise, a new key is generated, and the ID is saved. The wallet creates a derived key with a derivation path m/42/5 for the wallet. This utilizes the Builder Vault TSM
  • Setup a blockchain handler to handle requests to the blockchain - see below
  • Check the balance to see that enough funds are available
  • Build a transaction based on the transfer and sign it using the Builder Vault TSM
  • Send the transaction
📘

Note

When you run this example the first time, a new random account will be created, and the balance will be 0 DOT. 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.

The example uses the derivation path m/42/5 . See our section about key derivation for more. See the section about key import to migrate a key from an external wallet, such as Metamask, to the TSM. EdDSA does not have a defined non-hardened derivation, so the account key will probably need to be transferred for the process to work.

Blockchain Handler

For interaction with the Polkadot network we are using sidecar on the Blockdaemon’s BD Transact APIs. This is used to access the Westend test network:

apiKey := strings.TrimSpace(os.Getenv("API_KEY"))
ubiquityNativeURL := "https://svc.blockdaemon.com/native/v1/polkadot/westend/"
req, err := http.NewRequest(http.MethodGet, blockchain.sidecarURL+url, nil)
	if err != nil {
		log.Fatalln("HTTPGet: could not create request:", err)
	}
	req.Header.Set("Content-Type", "application/json")

	if blockchain.apiKey != "" {
		req.Header.Set("Authorization", "Bearer "+blockchain.apiKey)
	}
	res, err := http.DefaultClient.Do(req)
	if err != nil {
		log.Fatalln("HTTPGet: error making http request:", err)
	}

Alternatively, you can modify the example to connect to a local Polkadot node you host yourself or use another third-party Polkadot API provider instead of Blockdaemon’s BD Transact APIs.

Running the example

The example will transfer 0.01 WND 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 --planck=10000000000 --dstAddress=5E5QrYML2MyZhNg1U72y2YqFC18NSuehU73mCxhducVFaq4R

Code Example

You can find the final code example in our demo repository.