@node-dlc/messaging
Version:
DLC Messaging Protocol
105 lines • 4.2 kB
JavaScript
;
Object.defineProperty(exports, "__esModule", { value: true });
exports.DlcCloseV0 = exports.DlcClose = void 0;
const bufio_1 = require("@node-dlc/bufio");
const MessageType_1 = require("../MessageType");
const getTlv_1 = require("../serialize/getTlv");
const FundingInput_1 = require("./FundingInput");
const FundingSignaturesV0_1 = require("./FundingSignaturesV0");
class DlcClose {
static deserialize(buf) {
const reader = new bufio_1.BufferReader(buf);
const type = Number(reader.readUInt16BE());
switch (type) {
case MessageType_1.MessageType.DlcCloseV0:
return DlcCloseV0.deserialize(buf);
default:
throw new Error(`DLC Close message type must be DlcCloseV0`); // This is a temporary measure while protocol is being developed
}
}
}
exports.DlcClose = DlcClose;
/**
* DlcClose message contains information about a node and indicates its
* desire to close an existing contract.
*/
class DlcCloseV0 extends DlcClose {
constructor() {
super(...arguments);
/**
* The type for close_dlc_v0 message. close_dlc_v0 = 52170 // TODO
*/
this.type = DlcCloseV0.type;
this.fundingInputs = [];
}
/**
* Deserializes an close_dlc_v0 message
* @param buf
*/
static deserialize(buf) {
const instance = new DlcCloseV0();
const reader = new bufio_1.BufferReader(buf);
reader.readUInt16BE(); // read type
instance.contractId = reader.readBytes(32);
instance.closeSignature = reader.readBytes(64);
instance.offerPayoutSatoshis = reader.readUInt64BE();
instance.acceptPayoutSatoshis = reader.readUInt64BE();
instance.fundInputSerialId = reader.readUInt64BE();
const fundingInputsLen = reader.readUInt16BE();
for (let i = 0; i < fundingInputsLen; i++) {
instance.fundingInputs.push(FundingInput_1.FundingInputV0.deserialize((0, getTlv_1.getTlv)(reader)));
}
instance.fundingSignatures = FundingSignaturesV0_1.FundingSignaturesV0.deserialize((0, getTlv_1.getTlv)(reader));
return instance;
}
/**
* Serializes the close_dlc_v0 message into a Buffer
*/
serialize() {
const writer = new bufio_1.BufferWriter();
writer.writeUInt16BE(this.type);
writer.writeBytes(this.contractId);
writer.writeBytes(this.closeSignature);
writer.writeUInt64BE(this.offerPayoutSatoshis);
writer.writeUInt64BE(this.acceptPayoutSatoshis);
writer.writeUInt64BE(this.fundInputSerialId);
writer.writeUInt16BE(this.fundingInputs.length);
for (const fundingInput of this.fundingInputs) {
writer.writeBytes(fundingInput.serialize());
}
writer.writeBytes(this.fundingSignatures.serialize());
return writer.toBuffer();
}
/**
* Validates correctness of all fields
* @throws Will throw an error if validation fails
*/
validate() {
// Type is set automatically in class
// Ensure input serial ids are unique
const inputSerialIds = this.fundingInputs.map((input) => input.inputSerialId);
if (new Set(inputSerialIds).size !== inputSerialIds.length) {
throw new Error('inputSerialIds must be unique');
}
// Ensure funding inputs are segwit
this.fundingInputs.forEach((input) => input.validate());
}
/**
* Converts dlc_close_v0 to JSON
*/
toJSON() {
return {
type: this.type,
contractId: this.contractId.toString('hex'),
closeSignature: this.closeSignature.toString('hex'),
offerPayoutSatoshis: Number(this.offerPayoutSatoshis),
acceptPayoutSatoshis: Number(this.acceptPayoutSatoshis),
fundInputSerialId: Number(this.fundInputSerialId),
fundingInputs: this.fundingInputs.map((input) => input.toJSON()),
fundingSignatures: this.fundingSignatures.toJSON(),
};
}
}
exports.DlcCloseV0 = DlcCloseV0;
DlcCloseV0.type = MessageType_1.MessageType.DlcCloseV0;
//# sourceMappingURL=DlcClose.js.map