@node-dlc/messaging
Version:
DLC Messaging Protocol
157 lines • 5.92 kB
JavaScript
;
Object.defineProperty(exports, "__esModule", { value: true });
exports.DlcSignContainer = exports.DlcSignV0 = exports.DlcSign = void 0;
const bufio_1 = require("@node-dlc/bufio");
const MessageType_1 = require("../MessageType");
const deserializeTlv_1 = require("../serialize/deserializeTlv");
const getTlv_1 = require("../serialize/getTlv");
const BatchFundingGroup_1 = require("./BatchFundingGroup");
const CetAdaptorSignaturesV0_1 = require("./CetAdaptorSignaturesV0");
const FundingSignaturesV0_1 = require("./FundingSignaturesV0");
class DlcSign {
static deserialize(buf) {
const reader = new bufio_1.BufferReader(buf);
const type = Number(reader.readUInt16BE());
switch (type) {
case MessageType_1.MessageType.DlcSignV0:
return DlcSignV0.deserialize(buf);
default:
throw new Error(`Dlc Sign message type must be DlcSignV0`);
}
}
}
exports.DlcSign = DlcSign;
/**
* DlcSign gives all of the initiator's signatures, which allows the
* receiver to broadcast the funding transaction with both parties being
* fully committed to all closing transactions.
*/
class DlcSignV0 extends DlcSign {
constructor() {
super(...arguments);
/**
* The type for sign_dlc_v0 message. sign_dlc_v0 = 42782
*/
this.type = DlcSignV0.type;
}
/**
* Deserializes an sign_dlc_v0 message
* @param buf
*/
static deserialize(buf) {
const instance = new DlcSignV0();
const reader = new bufio_1.BufferReader(buf);
reader.readUInt16BE(); // read type
instance.contractId = reader.readBytes(32);
instance.cetSignatures = CetAdaptorSignaturesV0_1.CetAdaptorSignaturesV0.deserialize((0, getTlv_1.getTlv)(reader));
instance.refundSignature = reader.readBytes(64);
instance.fundingSignatures = FundingSignaturesV0_1.FundingSignaturesV0.deserialize((0, getTlv_1.getTlv)(reader));
while (!reader.eof) {
const buf = (0, getTlv_1.getTlv)(reader);
const tlvReader = new bufio_1.BufferReader(buf);
const { type } = (0, deserializeTlv_1.deserializeTlv)(tlvReader);
switch (Number(type)) {
case MessageType_1.MessageType.BatchFundingGroup:
if (!instance.batchFundingGroups) {
instance.batchFundingGroups = [];
}
instance.batchFundingGroups.push(BatchFundingGroup_1.BatchFundingGroup.deserialize(buf));
break;
default:
break;
}
}
return instance;
}
/**
* Converts sign_dlc_v0 to JSON
*/
toJSON() {
const tlvs = [];
if (this.batchFundingGroups) {
this.batchFundingGroups.forEach((group) => {
tlvs.push(group.serialize());
});
}
return {
type: this.type,
contractId: this.contractId.toString('hex'),
cetSignatures: this.cetSignatures.toJSON(),
refundSignature: this.refundSignature.toString('hex'),
fundingSignatures: this.fundingSignatures.toJSON(),
tlvs,
};
}
/**
* Serializes the sign_dlc_v0 message into a Buffer
*/
serialize() {
const writer = new bufio_1.BufferWriter();
writer.writeUInt16BE(this.type);
writer.writeBytes(this.contractId);
writer.writeBytes(this.cetSignatures.serialize());
writer.writeBytes(this.refundSignature);
writer.writeBytes(this.fundingSignatures.serialize());
if (this.batchFundingGroups)
this.batchFundingGroups.forEach((fundingInfo) => writer.writeBytes(fundingInfo.serialize()));
return writer.toBuffer();
}
}
exports.DlcSignV0 = DlcSignV0;
DlcSignV0.type = MessageType_1.MessageType.DlcSignV0;
class DlcSignContainer {
constructor() {
this.signs = [];
}
/**
* Adds a DlcSign to the container.
* @param sign The DlcSign to add.
*/
addSign(sign) {
this.signs.push(sign);
}
/**
* Returns all DlcSigns in the container.
* @returns An array of DlcSign instances.
*/
getSigns() {
return this.signs;
}
/**
* Serializes all DlcSigns in the container to a Buffer.
* @returns A Buffer containing the serialized DlcSigns.
*/
serialize() {
const writer = new bufio_1.BufferWriter();
// Write the number of signs in the container first.
writer.writeBigSize(this.signs.length);
// Serialize each sign and write it.
this.signs.forEach((sign) => {
const serializedSign = sign.serialize();
// Optionally, write the length of the serialized sign for easier deserialization.
writer.writeBigSize(serializedSign.length);
writer.writeBytes(serializedSign);
});
return writer.toBuffer();
}
/**
* Deserializes a Buffer into a DlcSignContainer with DlcSigns.
* @param buf The Buffer to deserialize.
* @returns A DlcSignContainer instance.
*/
static deserialize(buf) {
const reader = new bufio_1.BufferReader(buf);
const container = new DlcSignContainer();
const signsCount = reader.readBigSize();
for (let i = 0; i < signsCount; i++) {
// Optionally, read the length of the serialized sign if it was written during serialization.
const signLength = reader.readBigSize();
const signBuf = reader.readBytes(Number(signLength));
const sign = DlcSign.deserialize(signBuf); // Adjust based on actual implementation.
container.addSign(sign);
}
return container;
}
}
exports.DlcSignContainer = DlcSignContainer;
//# sourceMappingURL=DlcSign.js.map