Press n or j to go to the next uncovered block, b, p or k for the previous block.
| 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 | 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 2x 2x 2x 2x 2x 2x 2x 2x 2x 2x 2x 2x 2x 2x 2x 4x 4x 4x 4x 2x 5x 5x 5x 5x 1x 4x 4x 4x 4x 4x 4x 4x 4x 4x 4x 4x 4x 4x 26x 26x 26x 4x 4x 1x 3x 1x 2x 3x 2x 1x 1x 3x 3x 3x 3x 9x 9x 9x 9x 9x 9x 9x 9x 9x 9x 4x 9x 9x 9x | import { BufferReader, BufferWriter } from '@node-dlc/bufio';
import { MessageType, PROTOCOL_VERSION } from '../MessageType';
import { deserializeTlv } from '../serialize/deserializeTlv';
import { getTlv } from '../serialize/getTlv';
import { bigIntToNumber, toBigInt } from '../util';
import { IDlcMessage } from './DlcMessage';
import { FundingInput, IFundingInputJSON } from './FundingInput';
import { FundingSignatures, IFundingSignaturesJSON } from './FundingSignatures';
import { ScriptWitnessV0 } from './ScriptWitnessV0';
/**
* DlcClose message contains information about a node and indicates its
* desire to close an existing contract.
* Updated to follow DlcOffer architectural patterns.
*/
export class DlcClose implements IDlcMessage {
public static type = MessageType.DlcClose;
/**
* Creates a DlcClose from JSON data (e.g., from test vectors)
* Handles both our internal format and external test vector formats
* @param json JSON object representing a DLC close message
*/
// eslint-disable-next-line @typescript-eslint/explicit-module-boundary-types, @typescript-eslint/no-explicit-any
public static fromJSON(json: any): DlcClose {
const instance = new DlcClose();
// Basic fields with field name variations
instance.protocolVersion =
json.protocolVersion || json.protocol_version || PROTOCOL_VERSION;
// Basic fields with field name variations
instance.contractId = Buffer.from(
json.contractId || json.contract_id,
'hex',
);
instance.closeSignature = Buffer.from(
json.closeSignature || json.close_signature,
'hex',
);
// Use toBigInt helper to handle BigInt values from json-bigint
instance.offerPayoutSatoshis = toBigInt(
json.offerPayoutSatoshis || json.offer_payout_satoshis,
);
instance.acceptPayoutSatoshis = toBigInt(
json.acceptPayoutSatoshis || json.accept_payout_satoshis,
);
instance.fundInputSerialId = toBigInt(
json.fundInputSerialId || json.fund_input_serial_id,
);
// Use FundingInput.fromJSON() for each funding input - proper delegation
instance.fundingInputs = (json.fundingInputs || json.funding_inputs || [])
// eslint-disable-next-line @typescript-eslint/no-explicit-any
.map((inputJson: any) => FundingInput.fromJSON(inputJson));
// Create FundingSignatures manually since it doesn't have fromJSON
Eif (json.fundingSignatures || json.funding_signatures) {
instance.fundingSignatures = new FundingSignatures();
const sigData = json.fundingSignatures || json.funding_signatures;
// Handle different possible structures
Eif (sigData.fundingSignatures) {
// Standard format
instance.fundingSignatures.witnessElements = sigData.fundingSignatures.map(
// eslint-disable-next-line @typescript-eslint/no-explicit-any
(sig: any) =>
// eslint-disable-next-line @typescript-eslint/no-explicit-any
sig.witnessElements?.map((elem: any) => {
const witness = new ScriptWitnessV0();
witness.length =
elem.length || Buffer.from(elem.witness || '', 'hex').length;
witness.witness = Buffer.from(elem.witness || '', 'hex');
return witness;
}) || [],
);
} else if (Array.isArray(sigData)) {
// Array format
instance.fundingSignatures.witnessElements = sigData.map(
// eslint-disable-next-line @typescript-eslint/no-explicit-any
(sig: any) =>
// eslint-disable-next-line @typescript-eslint/no-explicit-any
sig.witnessElements?.map((elem: any) => {
const witness = new ScriptWitnessV0();
witness.length =
elem.length || Buffer.from(elem.witness || '', 'hex').length;
witness.witness = Buffer.from(elem.witness || '', 'hex');
return witness;
}) || [],
);
}
}
return instance;
}
/**
* Deserializes a close_dlc message with backward compatibility
* Detects old format (without protocol_version) vs new format (with protocol_version)
* @param buf
*/
public static deserialize(buf: Buffer): DlcClose {
const instance = new DlcClose();
const reader = new BufferReader(buf);
const type = reader.readUInt16BE(); // read type
// Validate type matches expected DlcClose type
if (type !== MessageType.DlcClose) {
throw new Error(
`Invalid message type. Expected ${MessageType.DlcClose}, got ${type}`,
);
}
// Read protocol version
instance.protocolVersion = reader.readUInt32BE();
instance.contractId = reader.readBytes(32);
instance.closeSignature = reader.readBytes(64);
instance.offerPayoutSatoshis = reader.readUInt64BE();
instance.acceptPayoutSatoshis = reader.readUInt64BE();
instance.fundInputSerialId = reader.readUInt64BE();
// Changed from u16 to bigsize for consistency with DlcOffer
const fundingInputsLen = Number(reader.readBigSize());
for (let i = 0; i < fundingInputsLen; i++) {
// FundingInput body is serialized directly without TLV wrapper in rust-dlc format
const fundingInput = FundingInput.deserializeBody(
Buffer.from(reader.buffer.subarray(reader.position)),
);
instance.fundingInputs.push(fundingInput);
// Skip past the FundingInput we just read
const fundingInputLength = fundingInput.serializeBody().length;
reader.position += fundingInputLength;
}
// Handle FundingSignatures - deserialize raw data (no TLV wrapper) like DlcSign
instance.fundingSignatures = FundingSignatures.deserialize(
Buffer.from(reader.buffer.subarray(reader.position)),
);
// Skip past the funding signatures we just read
const fundingLength = instance.fundingSignatures.serialize().length;
reader.position += fundingLength;
// Parse any additional TLV stream (for future extensibility)
while (!reader.eof) {
const buf = getTlv(reader);
const tlvReader = new BufferReader(buf);
const { type } = deserializeTlv(tlvReader);
// Store unknown TLVs for future compatibility
if (!instance.unknownTlvs) {
instance.unknownTlvs = [];
}
instance.unknownTlvs.push({ type: Number(type), data: buf });
}
return instance;
}
/**
* The type for close_dlc message. close_dlc = 52170
*/
public type = DlcClose.type;
// New fields as per dlcspecs PR #163
public protocolVersion: number = PROTOCOL_VERSION; // Default to current protocol version
public contractId: Buffer;
public closeSignature: Buffer;
public offerPayoutSatoshis: bigint;
public acceptPayoutSatoshis: bigint;
public fundInputSerialId: bigint;
public fundingInputs: FundingInput[] = [];
public fundingSignatures: FundingSignatures;
// Store unknown TLVs for forward compatibility
public unknownTlvs?: Array<{ type: number; data: Buffer }>;
/**
* Validates correctness of all fields
* @throws Will throw an error if validation fails
*/
public validate(): void {
// Type is set automatically in class
// protocol_version validation
Iif (this.protocolVersion !== PROTOCOL_VERSION) {
throw new Error(
`Unsupported protocol version: ${this.protocolVersion}, expected: ${PROTOCOL_VERSION}`,
);
}
// contractId validation
if (!this.contractId || this.contractId.length !== 32) {
throw new Error('contractId must be 32 bytes');
}
// closeSignature validation
if (!this.closeSignature || this.closeSignature.length !== 64) {
throw new Error('closeSignature must be 64 bytes');
}
// Ensure input serial ids are unique
const inputSerialIds = this.fundingInputs.map(
(input: FundingInput) => 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: FundingInput) => input.validate());
// Note: FundingSignatures doesn't have a validate method, so we skip validation
}
/**
* Converts dlc_close to JSON (canonical rust-dlc format)
*/
public toJSON(): IDlcCloseJSON {
// Include unknown TLVs for debugging
const tlvs = [];
Iif (this.unknownTlvs) {
this.unknownTlvs.forEach((tlv) =>
tlvs.push({ type: tlv.type, data: tlv.data.toString('hex') }),
);
}
// Return canonical rust-dlc format
return {
protocolVersion: this.protocolVersion,
contractId: this.contractId.toString('hex'),
closeSignature: this.closeSignature.toString('hex'),
offerPayoutSatoshis: bigIntToNumber(this.offerPayoutSatoshis),
acceptPayoutSatoshis: bigIntToNumber(this.acceptPayoutSatoshis),
fundInputSerialId: bigIntToNumber(this.fundInputSerialId),
fundingInputs: this.fundingInputs.map((input) => input.toJSON()),
fundingSignatures: this.fundingSignatures.toJSON(),
}; // Allow different field names from interface
}
/**
* Serializes the close_dlc message into a Buffer
* Updated serialization format to match DlcOffer patterns
*/
public serialize(): Buffer {
const writer = new BufferWriter();
writer.writeUInt16BE(this.type);
// New fields as per dlcspecs PR #163
writer.writeUInt32BE(this.protocolVersion);
writer.writeBytes(this.contractId);
writer.writeBytes(this.closeSignature);
writer.writeUInt64BE(this.offerPayoutSatoshis);
writer.writeUInt64BE(this.acceptPayoutSatoshis);
writer.writeUInt64BE(this.fundInputSerialId);
// Changed from u16 to bigsize for consistency with DlcOffer
writer.writeBigSize(this.fundingInputs.length);
for (const fundingInput of this.fundingInputs) {
// Use serializeBody() to match rust-dlc behavior - funding inputs in vec are serialized without TLV wrapper
writer.writeBytes(fundingInput.serializeBody());
}
// Serialize FundingSignatures directly (no TLV wrapper) like DlcSign
writer.writeBytes(this.fundingSignatures.serialize());
// Write unknown TLVs for forward compatibility
Iif (this.unknownTlvs) {
this.unknownTlvs.forEach((tlv) => {
writer.writeBytes(tlv.data);
});
}
return writer.toBuffer();
}
}
export interface IDlcCloseJSON {
type?: number; // Made optional for rust-dlc compatibility
protocolVersion: number;
contractId: string;
closeSignature: string;
offerPayoutSatoshis: number;
acceptPayoutSatoshis: number;
fundInputSerialId: number;
fundingInputs: IFundingInputJSON[];
fundingSignatures: IFundingSignaturesJSON;
serialized?: string; // Made optional - hex serialization for compatibility testing
// eslint-disable-next-line @typescript-eslint/no-explicit-any
tlvs?: any[]; // Made optional - for unknown TLVs
}
|