@node-dlc/messaging
Version:
DLC Messaging Protocol
105 lines • 3.9 kB
JavaScript
;
Object.defineProperty(exports, "__esModule", { value: true });
exports.FundingInputV0 = exports.FundingInput = void 0;
const bitcoin_1 = require("@node-dlc/bitcoin");
const bufio_1 = require("@node-dlc/bufio");
const MessageType_1 = require("../MessageType");
class FundingInput {
static deserialize(buf) {
const reader = new bufio_1.BufferReader(buf);
const type = Number(reader.readBigSize());
switch (type) {
case MessageType_1.MessageType.FundingInputV0:
return FundingInputV0.deserialize(buf);
default:
throw new Error(`FundingInput function TLV type must be FundingInputV0`);
}
}
}
exports.FundingInput = FundingInput;
/**
* FundingInput V0 contains information about a specific input to be used
* in a funding transaction, as well as its corresponding on-chain UTXO.
*/
class FundingInputV0 extends FundingInput {
constructor() {
super(...arguments);
/**
* The type for funding_input_v0 message. funding_input_v0 = 42772
*/
this.type = FundingInputV0.type;
}
/**
* Deserializes an funding_input_v0 message
* @param buf
*/
static deserialize(buf) {
const instance = new FundingInputV0();
const reader = new bufio_1.BufferReader(buf);
reader.readBigSize(); // read type
instance.length = reader.readBigSize();
instance.inputSerialId = reader.readUInt64BE();
const prevTxLen = reader.readUInt16BE();
instance.prevTx = bitcoin_1.Tx.decode(bufio_1.StreamReader.fromBuffer(reader.readBytes(prevTxLen)));
instance.prevTxVout = reader.readUInt32BE();
instance.sequence = new bitcoin_1.Sequence(reader.readUInt32LE());
instance.maxWitnessLen = reader.readUInt16BE();
const redeemScriptLen = reader.readUInt16BE();
instance.redeemScript = reader.readBytes(redeemScriptLen);
return instance;
}
scriptSigLength() {
if (this.redeemScript.length > 0) {
return 1 + this.redeemScript.length;
}
else {
return 0;
}
}
/**
* Validates correctness of all fields
* @throws Will throw an error if validation fails
*/
validate() {
// 1. Type is set automatically in class
// 2. Ensure inputs are segwit
if (!this.prevTx.isSegWit)
throw new Error('fundingInput must be segwit');
}
/**
* Converts funding_input_v0 to JSON
*/
toJSON() {
return {
type: this.type,
inputSerialId: Number(this.inputSerialId),
prevTx: this.prevTx.serialize().toString('hex'),
prevTxVout: this.prevTxVout,
sequence: this.sequence.value,
maxWitnessLen: this.maxWitnessLen,
redeemScript: this.redeemScript.toString('hex'),
};
}
/**
* Serializes the funding_input_v0 message into a Buffer
*/
serialize() {
const writer = new bufio_1.BufferWriter();
writer.writeBigSize(this.type);
const dataWriter = new bufio_1.BufferWriter();
dataWriter.writeUInt64BE(this.inputSerialId);
dataWriter.writeUInt16BE(this.prevTx.serialize().length);
dataWriter.writeBytes(this.prevTx.serialize());
dataWriter.writeUInt32BE(this.prevTxVout);
dataWriter.writeUInt32BE(this.sequence.value);
dataWriter.writeUInt16BE(this.maxWitnessLen);
dataWriter.writeUInt16BE(this.redeemScript.length);
dataWriter.writeBytes(this.redeemScript);
writer.writeBigSize(dataWriter.size);
writer.writeBytes(dataWriter.toBuffer());
return writer.toBuffer();
}
}
exports.FundingInputV0 = FundingInputV0;
FundingInputV0.type = MessageType_1.MessageType.FundingInputV0;
//# sourceMappingURL=FundingInput.js.map