XO Integration Part II

This commit is contained in:
Jim Hamill
2025-05-08 08:05:46 +00:00
parent 476a535768
commit 1870877df5
13 changed files with 164 additions and 21 deletions
@@ -9,6 +9,7 @@ import {
import {
Address,
BlockHeader,
ExtMap,
Outpoint,
Transaction,
@@ -30,6 +31,7 @@ export class BlockchainInMemory extends BaseBlockchain {
return blockchain as InstanceType<T>;
}
private blockHeaders: Array<BlockHeader>;
private transactions: BlockchainTransactions;
private addressSubscriptions: Map<string, Set<BlockchainCallback>>;
private transactionSubscriptions: Map<string, Set<BlockchainCallback>>;
@@ -40,6 +42,7 @@ export class BlockchainInMemory extends BaseBlockchain {
this.transactions = new ExtMap<BlockchainTransaction>();
this.addressSubscriptions = new Map();
this.transactionSubscriptions = new Map();
this.blockHeaders = [];
}
async start() {}
@@ -89,6 +92,17 @@ export class BlockchainInMemory extends BaseBlockchain {
});
}
async fetchBlockHeader(blockHeight: number) {
const blockHeader = this.blockHeaders[blockHeight];
if (!blockHeader) {
throw new Error(`No block available for height ${blockHeight}`);
}
// Return the block header.
return blockHeader;
}
async broadcastTransaction(
tx: Transaction | Uint8Array | string,
opts: { skipValidation: boolean } = { skipValidation: false },
@@ -326,6 +340,14 @@ export class BlockchainInMemory extends BaseBlockchain {
}
async mineBlock(outputs: Array<Output>): Promise<void> {
// Create a block header from the current timestamp.
const newBlockHeader = BlockHeader.generateRandom(
Math.floor(Date.now() / 1000),
);
// Add it to our list of block headers.
this.blockHeaders.push(newBlockHeader);
// Create a transaction that pays to our subscribed address.
const transaction = Transaction.fromRaw({
version: 2,