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 | 1x 1x 1x 1x 1x 1x 1x 1x 4x 4x 4x 3x 1x 1x 1x 8x 8x 8x 8x 8x 8x 8x 8x 1x 1x 1x 1x 1x 1x 1x 1x 8x 15x 1x 1x 1x 10x 10x 10x 10x 10x 10x 10x 1x 1x 10x 1x 2x 4x 3x 3x 3x 6x 6x 6x 3x 1x 1x 1x 1x 2x 2x 2x 2x 1x | import { BufferReader, BufferWriter } from '@node-dlc/bufio';
import { MessageType } from '../MessageType';
import { deserializeTlv } from '../serialize/deserializeTlv';
import { getTlv } from '../serialize/getTlv';
import { BatchFundingGroup, IBatchFundingGroupJSON } from './BatchFundingGroup';
import {
CetAdaptorSignaturesV0,
ICetAdaptorSignaturesV0JSON,
} from './CetAdaptorSignaturesV0';
import { IDlcMessage } from './DlcMessage';
import {
FundingSignaturesV0,
IFundingSignaturesV0JSON,
} from './FundingSignaturesV0';
export abstract class DlcSign {
public static deserialize(buf: Buffer): DlcSignV0 {
const reader = new BufferReader(buf);
const type = Number(reader.readUInt16BE());
switch (type) {
case MessageType.DlcSignV0:
return DlcSignV0.deserialize(buf);
default:
throw new Error(`Dlc Sign message type must be DlcSignV0`);
}
}
public abstract type: number;
public abstract toJSON(): IDlcSignV0JSON;
public abstract serialize(): Buffer;
}
/**
* 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.
*/
export class DlcSignV0 extends DlcSign implements IDlcMessage {
public static type = MessageType.DlcSignV0;
/**
* Deserializes an sign_dlc_v0 message
* @param buf
*/
public static deserialize(buf: Buffer): DlcSignV0 {
const instance = new DlcSignV0();
const reader = new BufferReader(buf);
reader.readUInt16BE(); // read type
instance.contractId = reader.readBytes(32);
instance.cetSignatures = CetAdaptorSignaturesV0.deserialize(getTlv(reader));
instance.refundSignature = reader.readBytes(64);
instance.fundingSignatures = FundingSignaturesV0.deserialize(
getTlv(reader),
);
while (!reader.eof) {
const buf = getTlv(reader);
const tlvReader = new BufferReader(buf);
const { type } = deserializeTlv(tlvReader);
switch (Number(type)) {
case MessageType.BatchFundingGroup:
Eif (!instance.batchFundingGroups) {
instance.batchFundingGroups = [];
}
instance.batchFundingGroups.push(BatchFundingGroup.deserialize(buf));
break;
default:
break;
}
}
return instance;
}
/**
* The type for sign_dlc_v0 message. sign_dlc_v0 = 42782
*/
public type = DlcSignV0.type;
public contractId: Buffer;
public cetSignatures: CetAdaptorSignaturesV0;
public refundSignature: Buffer;
public fundingSignatures: FundingSignaturesV0;
public batchFundingGroups?: BatchFundingGroup[];
/**
* Converts sign_dlc_v0 to JSON
*/
public toJSON(): IDlcSignV0JSON {
const tlvs = [];
Iif (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
*/
public serialize(): Buffer {
const writer = new 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();
}
}
export interface IDlcSignV0JSON {
type: number;
contractId: string;
cetSignatures: ICetAdaptorSignaturesV0JSON;
refundSignature: string;
fundingSignatures: IFundingSignaturesV0JSON;
tlvs: IBatchFundingGroupJSON[];
}
export class DlcSignContainer {
private signs: DlcSign[] = [];
/**
* Adds a DlcSign to the container.
* @param sign The DlcSign to add.
*/
public addSign(sign: DlcSign): void {
this.signs.push(sign);
}
/**
* Returns all DlcSigns in the container.
* @returns An array of DlcSign instances.
*/
public getSigns(): DlcSign[] {
return this.signs;
}
/**
* Serializes all DlcSigns in the container to a Buffer.
* @returns A Buffer containing the serialized DlcSigns.
*/
public serialize(): Buffer {
const writer = new 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.
*/
public static deserialize(buf: Buffer): DlcSignContainer {
const reader = new 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;
}
}
|