@node-dlc/bitcoin
Version:
79 lines • 2.66 kB
JavaScript
;
Object.defineProperty(exports, "__esModule", { value: true });
exports.TxIn = void 0;
const bufio_1 = require("@node-dlc/bufio");
const OutPoint_1 = require("./OutPoint");
const Script_1 = require("./Script");
const Sequence_1 = require("./Sequence");
class TxIn {
/**
* Parses a TxIn from a a stream reader.
* @param stream
*/
static parse(reader) {
const outpoint = OutPoint_1.OutPoint.parse(reader);
const scriptSig = Script_1.Script.parse(reader);
const sequence = new Sequence_1.Sequence(reader.readUInt32LE());
return new TxIn(outpoint, scriptSig, sequence);
}
/**
* Parses a hex string serialization of a transaction input. This
* is a helper function instead of having to do `StreamReader.fromHex`
* on a string directly.
* @param hex
*/
static fromHex(hex) {
const reader = bufio_1.StreamReader.fromHex(hex);
return this.parse(reader);
}
/**
* Constructs a new transaction input from the values
* @param outpoint
* @param scriptSig
* @param sequence
* @param witness
*/
constructor(outpoint, scriptSig = new Script_1.Script(), sequence = new Sequence_1.Sequence(), witness = []) {
this.outpoint = outpoint;
this.scriptSig = scriptSig;
this.sequence = sequence;
this.witness = witness;
}
/**
* Creates a string of the transaction input that includes all of the
* properties.
*/
toString() {
return `prev=${this.outpoint.toString()}, scriptSig=${this.scriptSig.toString()}, sequence=${this.sequence.toString()}`; // prettier-ignore
}
/**
* Creates a JSON object of the transaction input that includes all
* of the properties.
*/
// eslint-disable-next-line @typescript-eslint/explicit-module-boundary-types
toJSON() {
return {
outpoint: this.outpoint.toJSON(),
scriptSig: this.scriptSig.toJSON(),
sequence: this.sequence.toJSON(),
};
}
/**
* Returns the byte serialization of the transaction input
*/
serialize() {
const writer = new bufio_1.BufferWriter();
writer.writeBytes(this.outpoint.serialize());
writer.writeBytes(this.scriptSig.serialize());
writer.writeBytes(this.sequence.serialize());
return writer.toBuffer();
}
/**
* Clone via deep copy
*/
clone() {
return new TxIn(this.outpoint.clone(), this.scriptSig.clone(), this.sequence.clone(), this.witness.map((p) => p.clone()));
}
}
exports.TxIn = TxIn;
//# sourceMappingURL=TxIn.js.map