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 | 1x 1x 1x 1x 1x 1x 2x 2x 2x 1x 1x 1x 1x 3x 3x 3x 3x 3x 3x 3x 3x 3x 3x 3x 3x 3x 11x 11x 3x 3x 3x 3x 3x 3x 3x 3x 3x 3x 3x 3x 2x 3x 2x 1x 1x 1x 1x | import { BufferReader, BufferWriter } from '@node-dlc/bufio';
import { MessageType } from '../MessageType';
import { getTlv } from '../serialize/getTlv';
import { IDlcMessage } from './DlcMessage';
import { FundingInputV0, IFundingInputV0JSON } from './FundingInput';
import {
FundingSignaturesV0,
IFundingSignaturesV0JSON,
} from './FundingSignaturesV0';
export abstract class DlcClose {
public static deserialize(buf: Buffer): DlcCloseV0 {
const reader = new BufferReader(buf);
const type = Number(reader.readUInt16BE());
switch (type) {
case 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
}
}
public abstract type: number;
public abstract toJSON(): IDlcCloseV0JSON;
public abstract serialize(): Buffer;
}
/**
* DlcClose message contains information about a node and indicates its
* desire to close an existing contract.
*/
export class DlcCloseV0 extends DlcClose implements IDlcMessage {
public static type = MessageType.DlcCloseV0;
/**
* Deserializes an close_dlc_v0 message
* @param buf
*/
public static deserialize(buf: Buffer): DlcCloseV0 {
const instance = new DlcCloseV0();
const reader = new 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(FundingInputV0.deserialize(getTlv(reader)));
}
instance.fundingSignatures = FundingSignaturesV0.deserialize(
getTlv(reader),
);
return instance;
}
/**
* The type for close_dlc_v0 message. close_dlc_v0 = 52170 // TODO
*/
public type = DlcCloseV0.type;
public contractId: Buffer;
public closeSignature: Buffer;
public offerPayoutSatoshis: bigint;
public acceptPayoutSatoshis: bigint;
public fundInputSerialId: bigint;
public fundingInputs: FundingInputV0[] = [];
public fundingSignatures: FundingSignaturesV0;
/**
* Serializes the close_dlc_v0 message into a Buffer
*/
public serialize(): Buffer {
const writer = new 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
*/
public validate(): void {
// Type is set automatically in class
// Ensure input serial ids are unique
const inputSerialIds = this.fundingInputs.map(
(input: FundingInputV0) => 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: FundingInputV0) => input.validate());
}
/**
* Converts dlc_close_v0 to JSON
*/
public toJSON(): IDlcCloseV0JSON {
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(),
};
}
}
export interface IDlcCloseV0JSON {
type: number;
contractId: string;
closeSignature: string;
offerPayoutSatoshis: number;
acceptPayoutSatoshis: number;
fundInputSerialId: number;
fundingInputs: IFundingInputV0JSON[];
fundingSignatures: IFundingSignaturesV0JSON;
}
|