"use strict";
Object.defineProperty(exports, "__esModule", { value: true });
exports.TxOut = void 0;
const bufio_1 = require("@node-dlc/bufio");
const Script_1 = require("./Script");
const Value_1 = require("./Value");
class TxOut {
    /**
     * Parses a TxOut from a stream and returns a new instance of TxOut
     * @param reader
     */
    static parse(reader) {
        const value = Value_1.Value.fromSats(reader.readBigUInt64LE());
        const scriptPubKey = Script_1.Script.parse(reader);
        return new TxOut(value, scriptPubKey);
    }
    /**
     * Parses a hex string serialization of a transaction output. 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 TxOut.parse(reader);
    }
    /**
     * Constructs a new TxOut from the supplied arguments
     * @param value
     * @param scriptPubKey
     */
    constructor(value, scriptPubKey) {
        this.value = value;
        this.scriptPubKey = scriptPubKey;
    }
    /**
     * Returns the TxOut as a string
     */
    toString() {
        return `value="${this.value.sats}", scriptPubKey="${this.scriptPubKey}"`;
    }
    /**
     * Returns the TxOut as a JSON object
     */
    toJSON() {
        return {
            value: this.value.sats.toString(),
            scriptPubKey: this.scriptPubKey.toJSON(),
        };
    }
    /**
     * Returns the serialization of the transaction output into a buffer
     */
    serialize() {
        const writer = new bufio_1.BufferWriter();
        writer.writeUInt64LE(this.value.sats);
        writer.writeBytes(this.scriptPubKey.serialize());
        return writer.toBuffer();
    }
    /**
     * Clone via deep copy
     */
    clone() {
        return new TxOut(this.value.clone(), this.scriptPubKey.clone());
    }
}
exports.TxOut = TxOut;
//# sourceMappingURL=TxOut.js.map