-
Notifications
You must be signed in to change notification settings - Fork 10
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
Changes from 1 commit
Commits
Show all changes
6 commits
Select commit
Hold shift + click to select a range
dcc04f2
Dlc close messagetype
dillionverma ecc33de
update test info
dillionverma ac257ce
stub more fields in tests
dillionverma 49b0c3b
add validations and update tests
dillionverma 69b8e3b
update comment
dillionverma ba4b2f1
remove random comment
dillionverma File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
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,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); | ||
}); | ||
}); | ||
}); |
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,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(); | ||
} | ||
} |
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
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