Skip to main content

Signing transactions

Advanced
Bitcoin
Tutorial

Overview

Before a transaction can be sent to the Bitcoin network, it must be signed using the Threshold ECDSA API method sign_with_ecdsa and an ECDSA public key, which can be returned by calling the API method ecdsa_public_key.

Signing transactions

A code example using these API methods can be found below:

  public func ecdsa_public_key(key_name : Text, derivation_path : [Blob]) : async Blob {
// Retrieve the public key of this canister at derivation path
// from the ECDSA API.
let res = await ecdsa_canister_actor.ecdsa_public_key({
canister_id = null;
derivation_path;
key_id = {
curve = #secp256k1;
name = key_name;
};
});

res.public_key
};

public func sign_with_ecdsa(key_name : Text, derivation_path : [Blob], message_hash : Blob) : async Blob {
ExperimentalCycles.add(SIGN_WITH_ECDSA_COST_CYCLES);
let res = await ecdsa_canister_actor.sign_with_ecdsa({
message_hash;
derivation_path;
key_id = {
curve = #secp256k1;
name = key_name;
};
});

res.signature
};

A full example of using the Threshold ECDSA API can be found in the basic Bitcoin example.

Next steps

Now that your transaction is signed, it can be submitted to Bitcoin to be executed.

Learn how to submit Bitcoin transactions.