Skip to content
Closed
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
44 changes: 44 additions & 0 deletions src/example-sign-transaction.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,44 @@
/**
* Example: Signing a Bitcoin transaction using bitcoinjs-lib
*
* This demo creates a dummy transaction, signs it using a random private key,
* and prints both the raw transaction hex and the public address.
*/

import * as bitcoin from 'bitcoinjs-lib';
import { ECPairFactory } from 'ecpair';
import * as tinysecp from 'tiny-secp256k1';

const ECPair = ECPairFactory(tinysecp);

export function signExampleTx() {
const keyPair = ECPair.makeRandom();
const { address } = bitcoin.payments.p2wpkh({ pubkey: keyPair.publicKey });

// Create a fake transaction
const psbt = new bitcoin.Psbt({ network: bitcoin.networks.testnet });
psbt.addInput({
hash: 'ffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffff',
index: 0,
witnessUtxo: {
script: bitcoin.payments.p2wpkh({ pubkey: keyPair.publicKey }).output,
value: 10_000,
},
});
psbt.addOutput({
address: address,
value: 9_000,
});

// Sign & finalize
psbt.signAllInputs(keyPair);
psbt.finalizeAllInputs();

const txHex = psbt.extractTransaction().toHex();
console.log('Signed transaction (hex):', txHex);
console.log('Sender address:', address);
return txHex;
}

// Uncomment below for direct testing
// signExampleTx();