recover-ord-pass

This commit is contained in:
recovery committed 2026-10-05 03:54:15 +00:00
1 parent aa39db3879
commit cdd5723977
15 files changed
+486 -76

No files matched your search

+23 -3
View File
@@ -1,3 +1,4 @@
import { outputRanges, satAt, offsetOf } from './sats.js';
import { Tx } from '@cmdcode/tapscript';
import { readEnvelope } from './inscription.js';
@@ -6,12 +7,22 @@ import { readEnvelope } from './inscription.js';
export class MetadataIndex {
constructor(chain) {
this.events = [];
this.utxos = new Map();
const visit = (raw, height, block) => {
const tx = Tx.decode(raw);
if (tx.vin[0]?.witness?.length !== 3) return;
const item = readEnvelope(tx.vin[0].witness);
const txid = Tx.util.getTxid(tx), coinbase = tx.vin[0].txid === '0'.repeat(64);
const coins = coinbase ? [] : tx.vin.map(i => this.utxos.get(`${i.txid}:${i.vout}`));
const ranges = outputRanges(tx, coins, height, coinbase);
tx.vin.forEach(i => this.utxos.delete(`${i.txid}:${i.vout}`));
tx.vout.forEach((out, vout) => this.utxos.set(`${txid}:${vout}`, { txid, vout, value: Number(out.value), ranges: ranges[vout] }));
const witness = tx.vin.find(i => i.witness?.length === 3)?.witness;
if (!witness) return;
const item = readEnvelope(witness);
const pointer = BigInt(item.pointer ?? 0), total = tx.vout.reduce((n, o) => n + Number(o.value), 0);
const sat = satAt(ranges.flat(), pointer < BigInt(total) ? Number(pointer) : 0);
const metadata = item.metadata;
this.events.push({ id: `${Tx.util.getTxid(tx)}i0`, contentType: item.contentType,
this.events.push({ id: `${Tx.util.getTxid(tx)}i0`, contentType: item.contentType, delegate: item.delegate, pointer: item.pointer, sat,
reinscriptionPosition: this.events.filter(e => e.sat === sat).length,
status: height === null ? 'pending' : 'confirmed', height, block,
metadata: metadata ?? null, hasMetadata: metadata !== undefined, parent: item.parent,
protocol: metadata && !Array.isArray(metadata) && typeof metadata === 'object' &&
@@ -19,6 +30,15 @@ export class MetadataIndex {
};
for (const block of chain.blocks) for (const raw of block.raws) visit(raw, block.height, block.hash);
for (const raw of chain.mempool) visit(raw, null, null);
for (const event of this.events) {
event.location = null;
for (const coin of this.utxos.values()) {
const offset = offsetOf(coin.ranges, event.sat);
if (offset !== null) { event.location = { txid: coin.txid, vout: coin.vout, offset }; break; }
}
event.delegatedBy = this.events.filter(e => e.delegate === event.id).map(e => e.id);
event.inscriptions = this.events.filter(e => e.sat === event.sat).map(e => e.id);
}
}
forProtocol(name) { return this.events.filter(e => e.protocol === name); }
get protocols() { return [...new Set(this.events.filter(e => e.protocol !== null).map(e => e.protocol))].sort(); }