Skip to content

Commit

Permalink
Change: added type assertion when deserializing --wip-- [skip ci]
Browse files Browse the repository at this point in the history
  • Loading branch information
maxpolizzo committed May 17, 2023
1 parent 4c683bb commit 4b7cb03
Show file tree
Hide file tree
Showing 49 changed files with 302 additions and 63 deletions.
5 changes: 4 additions & 1 deletion packages/messaging/lib/messages/AddressCache.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
import { BufferReader, BufferWriter } from '@node-lightning/bufio';
import { BitcoinNetwork } from 'bitcoin-networks';
import { address } from 'bitcoinjs-lib';
import assert from 'assert';

import { MessageType } from '../MessageType';

Expand All @@ -26,7 +27,9 @@ export class AddressCache {
const instance = new AddressCache();
if (reader instanceof Buffer) reader = new BufferReader(reader);

reader.readUInt16BE(); // read type
const type = reader.readUInt16BE();
assert(type === this.type, `Expected AddressCache, got type ${type}`);

reader.readBigSize(); // num_cache_spks

while (!reader.eof) {
Expand Down
14 changes: 10 additions & 4 deletions packages/messaging/lib/messages/ContractDescriptor.ts
Original file line number Diff line number Diff line change
Expand Up @@ -78,11 +78,12 @@ export class EnumeratedContractDescriptor

const instance = new EnumeratedContractDescriptor();

const type = reader.readBigSize(); // read type
const type = Number(reader.readBigSize());
assert(
Number(type) === this.type,
`Expected Enumerated Contract Descriptor, got type ${this.type}`,
type === this.type,
`Expected ContractDescriptorType.Enumerated, got type ${type}`,
);

const numOutcomes = reader.readBigSize(); // num_outcomes

for (let i = 0; i < numOutcomes; i++) {
Expand Down Expand Up @@ -177,7 +178,12 @@ export class NumericContractDescriptor
if (reader instanceof Buffer) reader = new BufferReader(reader);
const instance = new NumericContractDescriptor();

reader.readBigSize(); // read type
const type = Number(reader.readBigSize());
assert(
type === this.type,
`Expected ContractDescriptorType.Numeric, got type ${type}`,
);

instance.numDigits = reader.readUInt16BE(); // num_digits
instance.payoutFunction = PayoutFunction.deserialize(reader);
instance.roundingIntervals = RoundingIntervals.deserialize(reader);
Expand Down
15 changes: 13 additions & 2 deletions packages/messaging/lib/messages/ContractInfo.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
import { BufferReader, BufferWriter } from '@node-lightning/bufio';
import assert from 'assert';

import { MessageType } from '../MessageType';
import {
Expand Down Expand Up @@ -89,7 +90,12 @@ export class SingleContractInfo extends ContractInfo implements IDlcMessage {

const instance = new SingleContractInfo();

reader.readBigSize(); // read type
const type = Number(reader.readBigSize());
assert(
type === this.type,
`Expected ContractInfoType.Single, got type ${type}`,
);

instance.totalCollateral = reader.readUInt64BE();
instance.contractDescriptor = ContractDescriptor.deserialize(reader);
instance.oracleInfo = OracleInfo.deserialize(reader);
Expand Down Expand Up @@ -239,7 +245,12 @@ export class DisjointContractInfo implements IDlcMessage {

const instance = new DisjointContractInfo();

reader.readBigSize(); // read type
const type = Number(reader.readBigSize());
assert(
type === this.type,
`Expected ContractInfoType.Disjoint, got type ${type}`,
);

instance.totalCollateral = reader.readUInt64BE();

const numDisjointEvents = reader.readBigSize(); // read num_disjoint_events
Expand Down
5 changes: 4 additions & 1 deletion packages/messaging/lib/messages/DlcAccept.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ import { hash160, sigToDER } from '@node-lightning/crypto';
import { BitcoinNetwork } from 'bitcoin-networks';
import { address } from 'bitcoinjs-lib';
import secp256k1 from 'secp256k1';
import assert from 'assert';

import { MessageType } from '../MessageType';
import { deserializeTlv, ITlv, serializeTlv } from "../serialize/deserializeTlv";
Expand Down Expand Up @@ -63,7 +64,9 @@ export class DlcAcceptV0 extends DlcAccept implements IDlcMessage {
const instance = new DlcAcceptV0();
if (reader instanceof Buffer) reader = new BufferReader(reader);

reader.readUInt16BE(); // read type
const type = reader.readUInt16BE();
assert(type === this.type, `Expected DlcAcceptV0, got type ${type}`);

instance.protocolVersion = reader.readUInt32BE();
console.log('test1');
instance.temporaryContractId = reader.readBytes(32);
Expand Down
5 changes: 4 additions & 1 deletion packages/messaging/lib/messages/DlcCancel.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
import { BufferReader, BufferWriter } from '@node-lightning/bufio';
import assert from 'assert';

import { MessageType } from '../MessageType';
import { IDlcMessage } from './DlcMessage';
Expand Down Expand Up @@ -40,7 +41,9 @@ export class DlcCancelV0 extends DlcCancel implements IDlcMessage {
const instance = new DlcCancelV0();
if (reader instanceof Buffer) reader = new BufferReader(reader);

reader.readUInt16BE(); // read type
const type = reader.readUInt16BE();
assert(type === this.type, `Expected DlcCancelV0, got type ${type}`);

instance.contractId = reader.readBytes(32);
instance.cancelType = reader.readUInt8();

Expand Down
5 changes: 4 additions & 1 deletion packages/messaging/lib/messages/DlcClose.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
import { BufferReader, BufferWriter } from '@node-lightning/bufio';
import assert from 'assert';

import { MessageType } from '../MessageType';
import { IDlcMessage } from './DlcMessage';
Expand Down Expand Up @@ -45,7 +46,9 @@ export class DlcCloseV0 extends DlcClose implements IDlcMessage {
const instance = new DlcCloseV0();
if (reader instanceof Buffer) reader = new BufferReader(reader);

reader.readUInt16BE(); // read type
const type = reader.readUInt16BE();
assert(type === this.type, `Expected DlcCloseV0, got type ${type}`);

instance.contractId = reader.readBytes(32);
instance.closeSignature = reader.readBytes(64);
instance.offerPayoutSatoshis = reader.readUInt64BE();
Expand Down
5 changes: 4 additions & 1 deletion packages/messaging/lib/messages/DlcIds.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
import { BufferReader, BufferWriter } from '@node-lightning/bufio';
import assert from 'assert';

import { MessageType } from '../MessageType';
import { IDlcMessage } from './DlcMessage';
Expand Down Expand Up @@ -38,7 +39,9 @@ export class DlcIdsV0 extends DlcIds implements IDlcMessage {
const instance = new DlcIdsV0();
if (reader instanceof Buffer) reader = new BufferReader(reader);

reader.readUInt16BE(); // read type
const type = reader.readUInt16BE();
assert(type === this.type, `Expected DlcIdsV0, got type ${type}`);

const idsLen = reader.readBigSize(); // ids length

for (let i = 0; i < idsLen; i++) {
Expand Down
4 changes: 3 additions & 1 deletion packages/messaging/lib/messages/DlcInfo.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
import { BufferReader, BufferWriter } from '@node-lightning/bufio';
import assert from 'assert';

import { MessageType } from '../MessageType';
import { IDlcMessage } from './DlcMessage';
Expand Down Expand Up @@ -38,7 +39,8 @@ export class DlcInfoV0 extends DlcInfo implements IDlcMessage {
const instance = new DlcInfoV0();
if (reader instanceof Buffer) reader = new BufferReader(reader);

reader.readUInt16BE(); // read type
const type = reader.readUInt16BE();
assert(type === this.type, `Expected DlcInfoV0, got type ${type}`);

instance.numDlcOffers = reader.readUInt32BE();
instance.numDlcAccepts = reader.readUInt32BE();
Expand Down
5 changes: 4 additions & 1 deletion packages/messaging/lib/messages/DlcOffer.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ import { hash160 } from '@node-lightning/crypto';
import { BitcoinNetwork } from 'bitcoin-networks';
import { address } from 'bitcoinjs-lib';
import secp256k1 from 'secp256k1';
import assert from 'assert';

import { MessageType } from '../MessageType';
import {
Expand Down Expand Up @@ -64,7 +65,9 @@ export class DlcOfferV0 extends DlcOffer implements IDlcMessage {
const instance = new DlcOfferV0();
if (reader instanceof Buffer) reader = new BufferReader(reader);

reader.readUInt16BE(); // read type
const type = reader.readUInt16BE();
assert(type === this.type, `Expected DlcOfferV0, got type ${type}`);

instance.protocolVersion = reader.readUInt32BE();
instance.contractFlags = reader.readUInt8();
instance.chainHash = reader.readBytes(32);
Expand Down
5 changes: 4 additions & 1 deletion packages/messaging/lib/messages/DlcSign.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
import { BufferReader, BufferWriter } from '@node-lightning/bufio';
import { sigToDER } from '@node-lightning/crypto';
import assert from 'assert';

import { MessageType } from '../MessageType';
import { deserializeTlv, ITlv, serializeTlv } from "../serialize/deserializeTlv";
Expand Down Expand Up @@ -51,7 +52,9 @@ export class DlcSignV0 extends DlcSign implements IDlcMessage {
const instance = new DlcSignV0();
if (reader instanceof Buffer) reader = new BufferReader(reader);

reader.readUInt16BE(); // read type
const type = reader.readUInt16BE();
assert(type === this.type, `Expected DlcSignV0, got type ${type}`);

instance.protocolVersion = reader.readUInt32BE();
instance.contractId = reader.readBytes(32);
instance.cetSignatures = CetAdaptorSignatures.deserialize(reader);
Expand Down
4 changes: 3 additions & 1 deletion packages/messaging/lib/messages/DlcTransactions.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ import {
BufferWriter,
StreamReader,
} from '@node-lightning/bufio';
import assert from 'assert';

import { MessageType } from '../MessageType';
import { IDlcMessage } from './DlcMessage';
Expand Down Expand Up @@ -47,7 +48,8 @@ export class DlcTransactionsV0 extends DlcTransactions implements IDlcMessage {
const instance = new DlcTransactionsV0();
if (reader instanceof Buffer) reader = new BufferReader(reader);

reader.readUInt16BE(); // read type
const type = reader.readUInt16BE();
assert(type === this.type, `Expected DlcTransactionsV0, got type ${type}`);

instance.contractId = reader.readBytes(32);

Expand Down
9 changes: 7 additions & 2 deletions packages/messaging/lib/messages/NegotiationFields.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
import { BufferReader, BufferWriter } from '@node-lightning/bufio';
import assert from 'assert';

import { IDlcMessage } from './DlcMessage';
import {
Expand Down Expand Up @@ -80,7 +81,9 @@ export class SingleNegotiationFields

const instance = new SingleNegotiationFields();

reader.readBigSize(); // read type
const type = Number(reader.readBigSize());
assert(type === this.type, `Expected NegotiationFieldsType.Single, got type ${type}`);

instance.roundingIntervals = RoundingIntervals.deserialize(reader);

return instance;
Expand Down Expand Up @@ -148,7 +151,9 @@ export class DisjointNegotiationFields

const instance = new DisjointNegotiationFields();

reader.readBigSize(); // read type
const type = Number(reader.readBigSize());
assert(type === this.type, `Expected NegotiationFieldsType.Disjoint, got type ${type}`);

const numDisjointEvents = reader.readBigSize(); // num_disjoint_events

for (let i = 0; i < numDisjointEvents; i++) {
Expand Down
4 changes: 3 additions & 1 deletion packages/messaging/lib/messages/NodeAnnouncementMessage.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ import { BufferReader, BufferWriter } from '@node-lightning/bufio';
import { BitField } from '@node-lightning/core';
import * as crypto from '@node-lightning/crypto';
import { NodeFeatureFlags } from '@node-lightning/wire';
import assert from 'assert';

import { Address } from '../domain/Address';
import { MessageType } from '../MessageType';
Expand All @@ -21,7 +22,8 @@ export class NodeAnnouncementMessage implements IWireMessage {
const instance = new NodeAnnouncementMessage();
if (reader instanceof Buffer) reader = new BufferReader(reader);

reader.readUInt16BE(); // read off type
const type = reader.readUInt16BE();
assert(type === this.type, `Expected NodeAnnouncement, got type ${type}`);

instance.signature = reader.readBytes(64);

Expand Down
4 changes: 3 additions & 1 deletion packages/messaging/lib/messages/OracleIdentifier.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
import { BufferReader, BufferWriter } from '@node-lightning/bufio';
import assert from 'assert';

import { MessageType } from '../MessageType';
import { IDlcMessage } from './DlcMessage';
Expand All @@ -14,7 +15,8 @@ export class OracleIdentifierV0 implements IDlcMessage {
const instance = new OracleIdentifierV0();
if (reader instanceof Buffer) reader = new BufferReader(reader);

reader.readBigSize(); // read type
const type = Number(reader.readBigSize());
assert(type === this.type, `Expected OracleIdentifierV0, got type ${type}`);

const oracleNameLength = reader.readBigSize();
const oracleNameBuf = reader.readBytes(Number(oracleNameLength));
Expand Down
15 changes: 13 additions & 2 deletions packages/messaging/lib/messages/OracleInfo.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
import { BufferReader, BufferWriter } from '@node-lightning/bufio';
import assert from 'assert';

import { getTlv } from '../serialize/getTlv';
import { IDlcMessage } from './DlcMessage';
Expand Down Expand Up @@ -68,7 +69,12 @@ export class SingleOracleInfo extends OracleInfo implements IDlcMessage {

const instance = new SingleOracleInfo();

reader.readBigSize(); // read type
const type = Number(reader.readBigSize());
assert(
type === this.type,
`Expected OracleInfoType.Single, got type ${type}`,
);

instance.announcement = OracleAnnouncementV0Pre167.deserialize(getTlv(reader));

return instance;
Expand Down Expand Up @@ -136,7 +142,12 @@ export class MultiOracleInfo extends OracleInfo implements IDlcMessage {

const instance = new MultiOracleInfo();

reader.readBigSize(); // read type
const type = Number(reader.readBigSize());
assert(
type === this.type,
`Expected OracleInfoType.Multi, got type ${type}`,
);

instance.threshold = reader.readUInt16BE();
const numOracles = reader.readBigSize();
for (let i = 0; i < numOracles; i++) {
Expand Down
5 changes: 4 additions & 1 deletion packages/messaging/lib/messages/OrderAccept.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
import { BufferReader, BufferWriter } from '@node-lightning/bufio';
import assert from 'assert';

import { MessageType } from '../MessageType';
import { IDlcMessage } from './DlcMessage';
Expand Down Expand Up @@ -48,7 +49,9 @@ export class OrderAcceptV0 extends OrderAccept implements IDlcMessage {
const instance = new OrderAcceptV0();
if (reader instanceof Buffer) reader = new BufferReader(reader);

reader.readUInt16BE(); // read type
const type = reader.readUInt16BE();
assert(type === this.type, `Expected OrderAcceptV0, got type ${type}`);

instance.protocolVersion = reader.readUInt32BE();
instance.tempOrderId = reader.readBytes(32);
const hasNegotiationFields = reader.readUInt8() === 1;
Expand Down
4 changes: 3 additions & 1 deletion packages/messaging/lib/messages/OrderIrcInfo.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
import { BufferReader, BufferWriter } from '@node-lightning/bufio';
import assert from 'assert';

import { MessageType } from '../MessageType';
import { IDlcMessage } from './DlcMessage';
Expand Down Expand Up @@ -42,7 +43,8 @@ export class OrderIrcInfoV0 extends OrderIrcInfo implements IDlcMessage {
const instance = new OrderIrcInfoV0();
if (reader instanceof Buffer) reader = new BufferReader(reader);

reader.readBigSize(); // read type
const type = Number(reader.readBigSize());
assert(type === this.type, `Expected OrderIrcInfoV0, got type ${type}`);

const nickLength = reader.readBigSize();
const nickBuf = reader.readBytes(Number(nickLength));
Expand Down
4 changes: 3 additions & 1 deletion packages/messaging/lib/messages/OrderMetadata.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
import { BufferReader, BufferWriter } from '@node-lightning/bufio';
import assert from 'assert';

import { MessageType } from '../MessageType';
import { IDlcMessage } from './DlcMessage';
Expand Down Expand Up @@ -42,7 +43,8 @@ export class OrderMetadataV0 extends OrderMetadata implements IDlcMessage {
const instance = new OrderMetadataV0();
if (reader instanceof Buffer) reader = new BufferReader(reader);

reader.readBigSize(); // read type
const type = Number(reader.readBigSize());
assert(type === this.type, `Expected OrderMetadataV0, got type ${type}`);

const offerIdLength = reader.readBigSize();
const offerIdBuf = reader.readBytes(Number(offerIdLength));
Expand Down
5 changes: 4 additions & 1 deletion packages/messaging/lib/messages/OrderOffer.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
import { BufferReader, BufferWriter } from '@node-lightning/bufio';
import assert from 'assert';

import { IOrderMetadataJSON } from '..';
import { MessageType } from '../MessageType';
Expand Down Expand Up @@ -64,7 +65,9 @@ export class OrderOfferV0 extends OrderOffer implements IDlcMessage {
const instance = new OrderOfferV0();
if (reader instanceof Buffer) reader = new BufferReader(reader);

reader.readUInt16BE(); // read type
const type = reader.readUInt16BE();
assert(type === this.type, `Expected OrderOfferV0, got type ${type}`);

instance.protocolVersion = reader.readUInt32BE();
instance.contractFlags = reader.readUInt8();
instance.chainHash = reader.readBytes(32);
Expand Down
Loading

0 comments on commit 4b7cb03

Please sign in to comment.