@node-dlc/bitcoin
Version:
88 lines • 2.8 kB
JavaScript
;
Object.defineProperty(exports, "__esModule", { value: true });
exports.OutPoint = void 0;
const bufio_1 = require("@node-dlc/bufio");
const HashByteOrder_1 = require("./HashByteOrder");
const HashValue_1 = require("./HashValue");
/**
* A tuple defining a transaction output which contains the transaction
* identifier and the output index.
*/
class OutPoint {
/**
* Creates an OutPoint from a byte stream.
* @param reader
*/
static parse(reader) {
const txid = HashValue_1.HashValue.parse(reader);
const outputIndex = reader.readUInt32LE();
return new OutPoint(txid, outputIndex);
}
/**
* Creates an OutPoint instance from an Outpoint serialized
* to the standard string of "txid:vout" where the txid is in RPC
* (reversed) byte order
*/
static fromString(text) {
const parts = /^([0-9a-f]{64,64}):(\d+)$/i.exec(text);
if (!parts) {
throw new Error('invalid argument');
}
const txid = HashValue_1.HashValue.fromRpc(parts[1]);
const voutIdx = parseInt(parts[2]);
if (voutIdx < 0) {
throw new Error('invalid argument');
}
return new OutPoint(txid, voutIdx);
}
/**
* Constructs a new OutPoint
* @param txid TxId as a HashValue or an RPC (big-endian) string
* @param outputIndex
*/
constructor(txid, outputIndex) {
if (txid instanceof HashValue_1.HashValue) {
this.txid = txid;
}
else {
this.txid = HashValue_1.HashValue.fromRpc(txid);
}
this.outputIndex = outputIndex;
}
/**
* Converts the outpoint to a human readable string in the format
* [txid]:[voutidx]
*/
toString() {
return `${this.txid.toString(HashByteOrder_1.HashByteOrder.RPC)}:${this.outputIndex}`;
}
/**
* Converts the outpoint to a JSON object with the txid and index
* tuple
*/
// eslint-disable-next-line @typescript-eslint/explicit-module-boundary-types
toJSON() {
return {
txid: this.txid.toString(HashByteOrder_1.HashByteOrder.RPC),
index: this.outputIndex,
};
}
/**
* Serializes the OutPoint to a buffer where the txid is serialized
* in internal byte order, and the output index is uint32LE
*/
serialize() {
const writer = new bufio_1.BufferWriter(Buffer.alloc(36));
writer.writeBytes(this.txid.serialize());
writer.writeUInt32LE(this.outputIndex);
return writer.toBuffer();
}
/**
* Clones by performing deep copy
*/
clone() {
return new OutPoint(this.txid.clone(), this.outputIndex);
}
}
exports.OutPoint = OutPoint;
//# sourceMappingURL=OutPoint.js.map