Skip to content

Commit

Permalink
feat: add batch funding group tlv to DlcSign msg (#215)
Browse files Browse the repository at this point in the history
  • Loading branch information
matthewjablack authored May 2, 2024
1 parent c9acd91 commit 6bb8221
Show file tree
Hide file tree
Showing 3 changed files with 87 additions and 1 deletion.
24 changes: 24 additions & 0 deletions packages/messaging/__tests__/messages/DlcAcceptV0.spec.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
import { BitcoinNetworks } from 'bitcoin-networks';
import { expect } from 'chai';

import { BatchFundingGroup } from '../../lib';
import { CetAdaptorSignaturesV0 } from '../../lib/messages/CetAdaptorSignaturesV0';
import {
DlcAccept,
Expand Down Expand Up @@ -309,4 +310,27 @@ describe('DlcAccept', () => {
expect(container.serialize()).to.deep.equal(instance.serialize());
});
});

describe('TLVs', () => {
it('should serialize and deserialize batch funding groups', () => {
const dlcAccept = DlcAcceptV0.deserialize(dlcAcceptHex);

const batchFundingGroup = BatchFundingGroup.deserialize(
BatchFundingGroup.deserialize(
Buffer.from(
'fdff967900000000000005f5e100051561746f6d69632d656e67696e652d74726164652d311561746f6d69632d656e67696e652d74726164652d321561746f6d69632d656e67696e652d74726164652d331561746f6d69632d656e67696e652d74726164652d341561746f6d69632d656e67696e652d74726164652d35',
'hex',
),
).serialize(),
);

dlcAccept.batchFundingGroups = [batchFundingGroup];

const instance = DlcAcceptV0.deserialize(dlcAccept.serialize());

expect(instance.batchFundingGroups[0].serialize()).to.deep.equal(
batchFundingGroup.serialize(),
);
});
});
});
28 changes: 27 additions & 1 deletion packages/messaging/__tests__/messages/DlcSignV0.spec.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
import { expect } from 'chai';

import { MessageType } from '../../lib';
import { BatchFundingGroup, MessageType } from '../../lib';
import { CetAdaptorSignaturesV0 } from '../../lib/messages/CetAdaptorSignaturesV0';
import {
DlcSign,
Expand Down Expand Up @@ -138,4 +138,30 @@ describe('DlcSign', () => {
expect(container.serialize()).to.deep.equal(instance.serialize());
});
});

describe('TLV', () => {
it('should serialize and deserialize batch funding groups', () => {
const dlcSign = DlcSignV0.deserialize(dlcSignHex);

const batchFundingGroup = BatchFundingGroup.deserialize(
BatchFundingGroup.deserialize(
Buffer.from(
'fdff967900000000000005f5e100051561746f6d69632d656e67696e652d74726164652d311561746f6d69632d656e67696e652d74726164652d321561746f6d69632d656e67696e652d74726164652d331561746f6d69632d656e67696e652d74726164652d341561746f6d69632d656e67696e652d74726164652d35',
'hex',
),
).serialize(),
);

batchFundingGroup.contractIds = [contractId, contractId2];
batchFundingGroup.tempContractIds = [contractId, contractId2];

dlcSign.batchFundingGroups = [batchFundingGroup];

const instance = DlcSignV0.deserialize(dlcSign.serialize());

expect(instance.batchFundingGroups[0].serialize()).to.deep.equal(
batchFundingGroup.serialize(),
);
});
});
});
36 changes: 36 additions & 0 deletions packages/messaging/lib/messages/DlcSign.ts
Original file line number Diff line number Diff line change
@@ -1,7 +1,9 @@
import { BufferReader, BufferWriter } from '@node-lightning/bufio';

import { MessageType } from '../MessageType';
import { deserializeTlv } from '../serialize/deserializeTlv';
import { getTlv } from '../serialize/getTlv';
import { BatchFundingGroup, IBatchFundingGroupJSON } from './BatchFundingGroup';
import {
CetAdaptorSignaturesV0,
ICetAdaptorSignaturesV0JSON,
Expand Down Expand Up @@ -57,6 +59,23 @@ export class DlcSignV0 extends DlcSign implements IDlcMessage {
getTlv(reader),
);

while (!reader.eof) {
const buf = getTlv(reader);
const tlvReader = new BufferReader(buf);
const { type } = deserializeTlv(tlvReader);

switch (Number(type)) {
case MessageType.BatchFundingGroup:
if (!instance.batchFundingGroups) {
instance.batchFundingGroups = [];
}
instance.batchFundingGroups.push(BatchFundingGroup.deserialize(buf));
break;
default:
break;
}
}

return instance;
}

Expand All @@ -73,16 +92,27 @@ export class DlcSignV0 extends DlcSign implements IDlcMessage {

public fundingSignatures: FundingSignaturesV0;

public batchFundingGroups?: BatchFundingGroup[];

/**
* Converts sign_dlc_v0 to JSON
*/
public toJSON(): IDlcSignV0JSON {
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,
};
}

Expand All @@ -97,6 +127,11 @@ export class DlcSignV0 extends DlcSign implements IDlcMessage {
writer.writeBytes(this.refundSignature);
writer.writeBytes(this.fundingSignatures.serialize());

if (this.batchFundingGroups)
this.batchFundingGroups.forEach((fundingInfo) =>
writer.writeBytes(fundingInfo.serialize()),
);

return writer.toBuffer();
}
}
Expand All @@ -107,6 +142,7 @@ export interface IDlcSignV0JSON {
cetSignatures: ICetAdaptorSignaturesV0JSON;
refundSignature: string;
fundingSignatures: IFundingSignaturesV0JSON;
tlvs: IBatchFundingGroupJSON[];
}

export class DlcSignContainer {
Expand Down

0 comments on commit 6bb8221

Please sign in to comment.