@bsv/wallet-toolbox
Version:
BRC100 conforming wallet, wallet storage and wallet signer components
101 lines • 3.83 kB
JavaScript
;
Object.defineProperty(exports, "__esModule", { value: true });
exports.MockChainStorage = void 0;
const MockChainMigrations_1 = require("./MockChainMigrations");
class MockChainStorage {
constructor(knex) {
this.knex = knex;
}
async migrate() {
const migrationSource = new MockChainMigrations_1.MockChainMigrations();
await this.knex.migrate.latest({
migrationSource,
tableName: 'knex_migrations_mockchain'
});
}
async insertTransaction(txid, rawTx) {
await this.knex('mockchain_transactions').insert({
txid,
rawTx: Buffer.from(rawTx),
blockHeight: null,
blockIndex: null
});
}
async getTransaction(txid) {
return this.knex('mockchain_transactions').where({ txid }).first();
}
async getUnminedTransactions() {
return this.knex('mockchain_transactions').whereNull('blockHeight');
}
async setTransactionBlock(txid, height, index) {
await this.knex('mockchain_transactions').where({ txid }).update({ blockHeight: height, blockIndex: index });
}
async insertUtxo(txid, vout, lockingScript, satoshis, scriptHash, isCoinbase = false, blockHeight = null) {
await this.knex('mockchain_utxos').insert({
txid,
vout,
lockingScript: Buffer.from(lockingScript),
satoshis,
scriptHash,
spentByTxid: null,
isCoinbase,
blockHeight
});
}
async getUtxo(txid, vout) {
return this.knex('mockchain_utxos').where({ txid, vout }).first();
}
async getUtxosByScriptHash(scriptHash) {
return this.knex('mockchain_utxos').where({ scriptHash });
}
async markUtxoSpent(txid, vout, spentByTxid) {
await this.knex('mockchain_utxos').where({ txid, vout }).update({ spentByTxid });
}
async insertBlockHeader(header) {
await this.knex('mockchain_block_headers').insert(header);
}
async getBlockHeaderByHeight(height) {
const row = await this.knex('mockchain_block_headers').where({ height }).first();
return row ? this.rowToBlockHeader(row) : undefined;
}
async getBlockHeaderByHash(hash) {
const row = await this.knex('mockchain_block_headers').where({ hash }).first();
return row ? this.rowToBlockHeader(row) : undefined;
}
async getChainTip() {
const row = await this.knex('mockchain_block_headers').orderBy('height', 'desc').first();
return row ? this.rowToBlockHeader(row) : undefined;
}
async getTransactionsInBlock(height) {
return this.knex('mockchain_transactions').where({ blockHeight: height }).orderBy('blockIndex', 'asc');
}
async deleteBlockHeader(height) {
await this.knex('mockchain_block_headers').where({ height }).delete();
}
async deleteTransaction(txid) {
await this.knex('mockchain_transactions').where({ txid }).delete();
}
async deleteUtxosByTxid(txid) {
await this.knex('mockchain_utxos').where({ txid }).delete();
}
async setUtxoBlockHeight(txid, blockHeight) {
await this.knex('mockchain_utxos').where({ txid }).update({ blockHeight });
}
async unspendUtxo(txid, vout) {
await this.knex('mockchain_utxos').where({ txid, vout }).update({ spentByTxid: null });
}
rowToBlockHeader(row) {
return {
height: row.height,
hash: row.hash,
previousHash: row.previousHash,
merkleRoot: row.merkleRoot,
version: row.version,
time: row.time,
bits: row.bits,
nonce: row.nonce
};
}
}
exports.MockChainStorage = MockChainStorage;
//# sourceMappingURL=MockChainStorage.js.map