Creating a Simple Transaction
Understanding and Creating Transactions
Creating and Signing a Transaction
import { Transaction, PrivateKey, PublicKey, P2PKH, ARC } from '@bsv/sdk'
const privKey = PrivateKey.fromWif('...') // Your P2PKH private key
const changePrivKey = PrivateKey.fromWif('...') // Change private key (never re-use addresses)
const recipientAddress = '1Fd5F7XR8LYHPmshLNs8cXSuVAAQzGp7Hc' // Address of the recipient
const tx = new Transaction()
// Add the input
tx.addInput({
sourceTransaction: Transaction.fromHex('...'), // The source transaction where the output you are spending was created,
sourceOutputIndex: 0, // The output index in the source transaction
unlockingScriptTemplate: new P2PKH().unlock(privKey), // The script template you are using to unlock the output, in this case P2PKH
})
// Pay an output to a recipient using the P2PKH locking template
tx.addOutput({
lockingScript: new P2PKH().lock(recipientAddress),
satoshis: 2500
})
// Send remainder back the change
tx.addOutput({
lockingScript: new P2PKH().lock(changePrivKey.toPublicKey().toHash()),
change: true
})
// Now we can compute the fee and sign the transaction
await tx.fee()
await tx.sign()
// Finally, we broadcast it with ARC.
// get your api key from https://console.taal.com
const apiKey = 'mainnet_xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx' // replace
await tx.broadcast(new ARC('https://api.taal.com/arc', apiKey))Handling Hex Locking Scripts
Configuring the ARC with http client
fetch
https
axios
other libraries
Last updated
Was this helpful?

