-
Notifications
You must be signed in to change notification settings - Fork 13
Bitcoin transaction
Nadimpalli Susruth edited this page Jul 24, 2019
·
1 revision
Doing a Bitcoin/ZCash transaction in mercury is divided into three parts, building an unsigned tx, signing it and submitting it.
// Instantiating a new btc/zec client
client, err := btcclient.New(logger, network)
if err != nil {
// Handle error
}
// `ops` are the OutPoints of the gateway the spender wants to spend
utxos := make(btctypes.UTXOs, len(ops))
var err error
for i, op := range ops {
utxos[i], err = client.UTXO(op)
if err != nil {
// Handle error
}
}
// `recipients` are the recipients of this tx, the change address of this transaction is sent to `refundAddress`.
// And the `txFee` is the fee used when submitting the tx.
tx, err := client.BuildUnsignedTx(utxos, recipients, refundAddress, txFee)
if err != nil {
// Handle error
}
Signing an unsigned tx can be done in two ways. Signing with a local private key
// signing the tx locally
tx.Sign(privateKey)
or getting the hashes signing them somewhere else and injecting the signatures back into the tx.
// retrieving the signature hashes
hashes := tx.SignatureHashes()
// sign hashes to get signatures
if err := tx.InjectSignatures(signatures, serializedPublicKey); err != nil {
// Handle error
}
txHash, err := client.SubmitSignedTx(tx);
if err != nil {
// Handle error
}
// this address should be either recipient.Address or refundAddress
utxo := tx.OutputUTXO(address)