Files
xo-stack/examples/node/genesisTest.js
T
2024-09-12 21:33:11 +10:00

55 lines
1.4 KiB
JavaScript

import { Transaction } from '@xo/primitives';
import { BlockchainLocal, WalletP2PKH } from '@xo/wallet';
const blockchainLocal = new BlockchainLocal();
await blockchainLocal.start();
const walletP2PKH = WalletP2PKH.generateRandom(blockchainLocal);
const walletP2PKHReceive = WalletP2PKH.generateRandom(blockchainLocal);
const coinbaseTransaction = new Transaction({
version: 2,
locktime: 0,
inputs: [
{
outpointIndex: 0,
outpointTransactionHash: new Uint8Array(),
unlockingBytecode: new Uint8Array(),
sequenceNumber: 0,
},
],
outputs: [
{
lockingBytecode: walletP2PKH.getAddress().toLockscriptBytes(),
valueSatoshis: 100_000n,
},
{
lockingBytecode: walletP2PKH.getAddress().toLockscriptBytes(),
valueSatoshis: 150_000n,
},
],
});
await blockchainLocal.broadcastTransaction(coinbaseTransaction, {
skipValidation: true,
});
console.log('walletP2PKH unspents', await walletP2PKH.fetchUnspentDirectives());
console.log(
'walletP2PKHReceive unspents',
await walletP2PKHReceive.fetchUnspentDirectives()
);
const sweepTx = await walletP2PKH.sweep(
walletP2PKHReceive.getAddress().toLockscriptBytes()
);
await blockchainLocal.broadcastTransaction(sweepTx);
console.log('walletP2PKH unspents', await walletP2PKH.fetchUnspentDirectives());
console.log(
'walletP2PKHReceive unspents',
await walletP2PKHReceive.fetchUnspentDirectives()
);
console.log(blockchainLocal.deriveUtxoSet());