8000 Bitcoin transaction · renproject/mercury Wiki · GitHub
[go: up one dir, main page]
More Web Proxy on the site http://driver.im/
Skip to content

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.

Building an unsigned tx:

// 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:

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
}

Submitting a signed tx:

txHash, err := client.SubmitSignedTx(tx);
if err != nil {
    // Handle error
}

Retrieving UTXOs that can be used in future transactions

// this address should be either recipient.Address or refundAddress
utxo := tx.OutputUTXO(address)
Clone this wiki locally
0