-
Notifications
You must be signed in to change notification settings - Fork 11
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat: add batch funding group tlv (#207)
- Loading branch information
1 parent
bb6b4ab
commit 73d5226
Showing
8 changed files
with
271 additions
and
2 deletions.
There are no files selected for viewing
52 changes: 52 additions & 0 deletions
52
packages/messaging/__tests__/messages/BatchFundingGroup.spec.ts
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,52 @@ | ||
import { Value } from '@node-dlc/bitcoin'; | ||
import { expect } from 'chai'; | ||
|
||
import { BatchFundingGroup } from '../../lib'; | ||
|
||
describe('BatchFundingGroup TLV', () => { | ||
it('should serialize and deserialize without contract ids', () => { | ||
const batchFundingGroup = new BatchFundingGroup(); | ||
|
||
const eventIds = ['event1', 'event2', 'event3']; | ||
|
||
batchFundingGroup.eventIds = eventIds; | ||
batchFundingGroup.allocatedCollateral = Value.fromBitcoin(0.5); | ||
|
||
const deserialized = BatchFundingGroup.deserialize( | ||
batchFundingGroup.serialize(), | ||
); | ||
|
||
expect(deserialized.eventIds).to.deep.equal(eventIds); | ||
expect(batchFundingGroup.serialize()).to.deep.equal( | ||
deserialized.serialize(), | ||
); | ||
}); | ||
|
||
it('should serialize and deserialize with contract ids', () => { | ||
const batchFundingGroup = new BatchFundingGroup(); | ||
|
||
const eventIds = ['event1', 'event2', 'event3']; | ||
|
||
batchFundingGroup.eventIds = eventIds; | ||
batchFundingGroup.allocatedCollateral = Value.fromBitcoin(0.5); | ||
batchFundingGroup.tempContractIds = [ | ||
Buffer.from('tempContractId1'), | ||
Buffer.from('tempContractId2'), | ||
Buffer.from('tempContractId3'), | ||
]; | ||
batchFundingGroup.contractIds = [ | ||
Buffer.from('contractId1'), | ||
Buffer.from('contractId2'), | ||
Buffer.from('contractId3'), | ||
]; | ||
|
||
const deserialized = BatchFundingGroup.deserialize( | ||
batchFundingGroup.serialize(), | ||
); | ||
|
||
expect(deserialized.eventIds).to.deep.equal(eventIds); | ||
expect(batchFundingGroup.serialize()).to.deep.equal( | ||
deserialized.serialize(), | ||
); | ||
}); | ||
}); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,131 @@ | ||
import { Value } from '@node-dlc/bitcoin'; | ||
import { BufferReader, BufferWriter } from '@node-lightning/bufio'; | ||
|
||
import { MessageType } from '../MessageType'; | ||
import { IDlcMessage } from './DlcMessage'; | ||
|
||
/** | ||
* The BatchFundingGroup TLV contains information about the intent to | ||
* enter multiple DLCs simulatenously within one batch dlc funding | ||
* transaction in the contract negotiation stage of the peer protocol | ||
* | ||
* This is the first step toward creating a batch dlc funding transaction | ||
* | ||
* A DlcOffer or DlcAccept can contain one or multiple BatchFundingInfo | ||
* TLVs to specify one or more groupings. This allows specification of | ||
* collateral put towards different types of contracts, such as options | ||
* contracts, futures contracts, or other investment types. | ||
* | ||
* Attributes: | ||
* - tempContractIds: Temporary identifiers for contracts proposed in DlcOffers. | ||
* - contractIds: Identifiers for contracts that have been accepted and are | ||
* part of the funding transaction. These are derived from DlcOffers and DlcAccepts. | ||
* - allocatedCollateral: The amount of collateral allocated to the contracts | ||
* within this group. This is specified early in the negotiation process. | ||
* - eventIds: Oracle event identifiers for the contracts in this group. These | ||
* are also specified early in the negotiation process. | ||
* | ||
* Note: During the early stages of the negotiation protocol, only allocatedCollateral | ||
* and eventIds are specified. tempContractIds and contractIds are added to the | ||
* DlcAccept upon creation. | ||
*/ | ||
export class BatchFundingGroup implements IDlcMessage { | ||
public static type = MessageType.BatchFundingGroup; | ||
|
||
/** | ||
* Deserializes a batch_contract_info message | ||
* @param buf | ||
*/ | ||
public static deserialize(buf: Buffer): BatchFundingGroup { | ||
const instance = new BatchFundingGroup(); | ||
const reader = new BufferReader(buf); | ||
|
||
reader.readBigSize(); // read type | ||
const tempContractIdsCount = reader.readBigSize(); | ||
for (let i = 0; i < Number(tempContractIdsCount); i++) { | ||
const length = reader.readBigSize(); | ||
instance.tempContractIds.push(reader.readBytes(Number(length))); | ||
} | ||
|
||
const contractIdsCount = reader.readBigSize(); | ||
for (let i = 0; i < Number(contractIdsCount); i++) { | ||
const length = reader.readBigSize(); | ||
instance.contractIds.push(reader.readBytes(Number(length))); | ||
} | ||
|
||
instance.allocatedCollateral = Value.fromSats(reader.readUInt64BE()); | ||
|
||
const eventIdsCount = reader.readBigSize(); | ||
for (let i = 0; i < Number(eventIdsCount); i++) { | ||
const length = reader.readBigSize(); | ||
instance.eventIds.push(reader.readBytes(Number(length)).toString()); | ||
} | ||
|
||
return instance; | ||
} | ||
|
||
/** | ||
* The type for batch_contract_info message. | ||
*/ | ||
public type = BatchFundingGroup.type; | ||
|
||
public tempContractIds: Buffer[] = []; | ||
|
||
public contractIds: Buffer[] = []; | ||
|
||
public allocatedCollateral: Value; | ||
|
||
public eventIds: string[] = []; | ||
|
||
/** | ||
* Converts batch_funding_info to JSON | ||
*/ | ||
public toJSON(): IBatchFundingGroupJSON { | ||
return { | ||
type: this.type, | ||
tempContractIds: this.tempContractIds.map((id) => id.toString('hex')), | ||
contractIds: this.contractIds.map((id) => id.toString('hex')), | ||
totalCollateral: Number(this.allocatedCollateral.sats), | ||
eventIds: this.eventIds, | ||
}; | ||
} | ||
|
||
/** | ||
* Serializes the batch_funding_info message into a Buffer | ||
*/ | ||
public serialize(): Buffer { | ||
const writer = new BufferWriter(); | ||
writer.writeBigSize(this.type); | ||
|
||
writer.writeBigSize(this.tempContractIds.length); | ||
this.tempContractIds.forEach((id) => { | ||
writer.writeBigSize(id.length); | ||
writer.writeBytes(id); | ||
}); | ||
|
||
writer.writeBigSize(this.contractIds.length); | ||
this.contractIds.forEach((id) => { | ||
writer.writeBigSize(id.length); | ||
writer.writeBytes(id); | ||
}); | ||
|
||
writer.writeUInt64BE(this.allocatedCollateral.sats); | ||
|
||
writer.writeBigSize(this.eventIds.length); | ||
this.eventIds.forEach((id) => { | ||
const idBuffer = Buffer.from(id); | ||
writer.writeBigSize(idBuffer.length); | ||
writer.writeBytes(idBuffer); | ||
}); | ||
|
||
return writer.toBuffer(); | ||
} | ||
} | ||
|
||
export interface IBatchFundingGroupJSON { | ||
type: number; | ||
tempContractIds: string[]; | ||
contractIds: string[]; | ||
totalCollateral: number; | ||
eventIds: string[]; | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.