Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

DLC close messagetype #122

Merged
merged 6 commits into from
Aug 11, 2021
Merged
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
41 changes: 41 additions & 0 deletions packages/messaging/__tests__/messages/DlcCloseV0.spec.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,41 @@
import { expect } from 'chai';
import { DlcCloseV0 } from '../../lib/messages/DlcClose';

describe('DlcCloseV0', () => {
const contractId = Buffer.from(
'c1c79e1e9e2fa2840b2514902ea244f39eb3001a4037a52ea43c797d4f841269',
'hex',
);

describe('serialize', () => {
it('serializes', () => {
const instance = new DlcCloseV0();

instance.contractId = contractId;

// instance.cancelType = 0;

expect(instance.serialize().toString("hex")).to.equal(
"cbcc" +
"c1c79e1e9e2fa2840b2514902ea244f39eb3001a4037a52ea43c797d4f841269"
// "00" // cancel type
); // prettier-ignore
});
});

describe('deserialize', () => {
it('deserializes', () => {
const buf = Buffer.from(
"cbcc" +
"c1c79e1e9e2fa2840b2514902ea244f39eb3001a4037a52ea43c797d4f841269"
// "00" // cancel type
, "hex"
); // prettier-ignore

const instance = DlcCloseV0.deserialize(buf);

expect(instance.contractId).to.deep.equal(contractId);
// expect(instance.cancelType).to.equal(0);
});
});
});
2 changes: 2 additions & 0 deletions packages/messaging/lib/MessageType.ts
Original file line number Diff line number Diff line change
Expand Up @@ -48,6 +48,8 @@ export enum MessageType {

DlcCancelV0 = 52172,

DlcCloseV0 = 11111, // TODO
Copy link
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

There is some ongoing but un-decided discussion here
https://github.com/discreetlogcontracts/dlcspecs/pull/170/files#r674408145


/**
* Dlc Storage Types
*/
Expand Down
86 changes: 86 additions & 0 deletions packages/messaging/lib/messages/DlcClose.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,86 @@
import { BufferReader, BufferWriter } from '@node-lightning/bufio';
import { MessageType } from '../MessageType';
import { getTlv } from '../serialize/getTlv';
import { IDlcMessage } from './DlcMessage';
import { FundingInputV0 } from './FundingInput';

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 serialize(): Buffer;
}

/**
* DlcOffer message contains information about a node and indicates its
* desire to enter into a new contract. This is the first step toward
* creating the funding transaction and CETs.
zpv marked this conversation as resolved.
Show resolved Hide resolved
*/
export class DlcCloseV0 extends DlcClose implements IDlcMessage {
public static type = MessageType.DlcCloseV0;

/**
* Deserializes an offer_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();
const fundingInputsLen = reader.readUInt16BE();
for (let i = 0; i < fundingInputsLen; i++) {
instance.fundingInputs.push(FundingInputV0.deserialize(getTlv(reader)));
}

return instance;
}

/**
* The type for close_dlc_v0 message. close_dlc_v0 = 99999 // TODO
*/
public type = DlcCloseV0.type;

public contractId: Buffer;

public closeSignature: Buffer;

public offerPayoutSatoshis: bigint;

public acceptPayoutSatoshis: bigint;

public fundingInputs: FundingInputV0[] = [];

/**
* 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);
for (const fundingInput of this.fundingInputs) {
writer.writeBytes(fundingInput.serialize());
}

return writer.toBuffer();
}
}