Resolve "WIP: UTXO Selection and Storage"

This commit is contained in:
Jim Hamill
2025-01-11 08:39:24 +00:00
parent f934216d68
commit 33fd9bac4d
156 changed files with 13304 additions and 4595 deletions
+15
View File
@@ -0,0 +1,15 @@
import { Bytes, Crypto } from '@xo/primitives';
const payload = Bytes.fromHex(
'00112233445566778899aabbccddeeff00112233445566778899aabbccddeeff',
);
console.time('sha256');
for (let i = 0; i < 10_000; i++) {
Crypto.sha256(payload.toBytes());
}
// NOTE: Around 100ms for 10,000 SHA256's on my machine.
// Not too bad, but might still be worth avoiding if trivial.
console.timeEnd('sha256');
+3 -3
View File
@@ -42,18 +42,18 @@ await blockchainLocal.broadcastTransaction(coinbaseTransaction, {
console.log('walletP2PKH unspents', await walletP2PKH.fetchUnspentDirectives());
console.log(
'walletP2PKHReceive unspents',
await walletP2PKHReceive.fetchUnspentDirectives()
await walletP2PKHReceive.fetchUnspentDirectives(),
);
const sweepTx = await walletP2PKH.sweep(
walletP2PKHReceive.getAddress().toLockscriptBytes()
walletP2PKHReceive.getAddress().toLockscriptBytes(),
);
await blockchainLocal.broadcastTransaction(sweepTx);
console.log('walletP2PKH unspents', await walletP2PKH.fetchUnspentDirectives());
console.log(
'walletP2PKHReceive unspents',
await walletP2PKHReceive.fetchUnspentDirectives()
await walletP2PKHReceive.fetchUnspentDirectives(),
);
console.log(blockchainLocal.deriveUtxoSet());
+2 -2
View File
@@ -4,14 +4,14 @@ import { Blockchain, WalletHD } from '@xo/wallet';
const blockchain = new Blockchain(
['bch.imaginary.cash', 'cashnode.bch.ninja', 'electrum.imaginary.cash'],
'Test App',
'1.5'
'1.5',
);
await blockchain.start();
const wallet = WalletHD.fromMnemonic(
'harsh gospel job ordinary garden clay eight cactus replace general prison permit',
blockchain
blockchain,
);
await wallet.scan();
+48
View File
@@ -0,0 +1,48 @@
import { BlockchainElectrum, StorageMemory, WalletHD } from '@xo/wallet';
const ELECTRUM_SERVERS = [
'bch.imaginary.cash',
'cashnode.bch.ninja',
'electrum.imaginary.cash',
];
const blockchainElectrum = new BlockchainElectrum(ELECTRUM_SERVERS);
await blockchainElectrum.start();
const storage = new StorageMemory();
const walletStore = await storage.getChild('wallets');
const walletOneStore = await walletStore.getChild('1');
console.log(walletOneStore);
const cache = new StorageMemory();
const services = {
blockchain: blockchainElectrum,
storage: walletOneStore,
cache: cache,
};
const wallet = await WalletHD.from(
{
mnemonic:
'grape amazing region hand script truck veteran tilt gaze rough despair guard',
},
blockchain,
walletOneStore,
cache,
);
await wallet.start();
console.log(await storage.export());
const txs = await wallet.fetchTransactions();
console.log(txs);
const wallet2 = await WalletHD.fromStorage(services);
await wallet2.start();
console.log(await wallet2.fetchTransactions());
+72
View File
@@ -0,0 +1,72 @@
import { Transaction } from '@xo/primitives';
import { BlockchainLocal, WalletP2PKH } from '@xo/wallet';
const blockchainLocal = new BlockchainLocal();
await blockchainLocal.start();
const services = {
blockchain: blockchainLocal,
};
const walletP2PKH = WalletP2PKH.generateRandom(services);
const walletP2PKHReceive = WalletP2PKH.generateRandom(services);
const coinbaseTransaction = new Transaction({
version: 2,
locktime: 0,
inputs: [
{
outpointIndex: 0,
outpointTransactionHash: new Uint8Array(),
unlockingBytecode: new Uint8Array(),
sequenceNumber: 0,
},
],
outputs: [
{
lockingBytecode: (
await walletP2PKH.getReceivingAddress()
).toLockscriptBytes(),
valueSatoshis: 100_000n,
},
{
lockingBytecode: (
await walletP2PKH.getReceivingAddress()
).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 sendTx = await walletP2PKH.send({
outputs: [
{
lockingBytecode: (
await walletP2PKHReceive.getReceivingAddress()
).toLockscriptBytes(),
valueSatoshis: 150_000n,
},
],
});
console.log(sendTx);
// await blockchainLocal.broadcastTransaction(new Transaction(sendTx));
console.log('walletP2PKH unspents', await walletP2PKH.fetchUnspentDirectives());
console.log(
'walletP2PKHReceive unspents',
await walletP2PKHReceive.fetchUnspentDirectives(),
);
console.log(blockchainLocal.deriveUtxoSet());