Skip to content

Commit

Permalink
Fix: OracleAttestationV0Pre167 calculation of deserializedBytes durin…
Browse files Browse the repository at this point in the history
…g deserialization --wip-- [skip ci]
  • Loading branch information
Maxime Suard committed Aug 25, 2023
1 parent 55e7831 commit 567d0e0
Show file tree
Hide file tree
Showing 2 changed files with 26 additions and 4 deletions.
15 changes: 11 additions & 4 deletions packages/messaging/lib/messages/pre-167/OracleAttestation.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,13 +3,14 @@ import assert from 'assert';
import { math, verify } from 'bip-schnorr';

import { MessageType } from '../../MessageType';
import { IDlcMessage } from '../DlcMessage';
import {
deserializeTlv,
ITlv,
serializeTlv,
} from '../../serialize/deserializeTlv';
import { getTlv } from '../../serialize/getTlv';
import { IDlcMessage } from '../DlcMessage';
import { getBigSizeBytesLength } from '../../utils/bigSize';

/**
* In order to make it possible to hold oracles accountable in cases where
Expand Down Expand Up @@ -58,13 +59,19 @@ export class OracleAttestationV0Pre167 implements IDlcMessage {
}

let deserializedBytes =
2 + Number(eventIdLength) + 32 + 2 + numSignatures * 64; // Count 2 bytes for eventIdLength + eventIdLength
// + 32 bytes for oraclePubkey + 2 bytes for numSignatures + 64 bytes per signature
getBigSizeBytesLength(eventIdLength) + // bytes length of eventIdLength
Number(eventIdLength) + // bytes length of eventId
32 + // oraclePubKey is 32 bytes
2 + // numSignatures is a uint16 (2 bytes)
numSignatures * 64; // each signature is 64 bytes

while (deserializedBytes < instance.length) {
const outcomeLen = reader.readBigSize();
const outcomeBuf = reader.readBytes(Number(outcomeLen));
instance.outcomes.push(outcomeBuf.toString());
deserializedBytes += 2 + Number(outcomeLen); // Count 2 bytes for outcomeLen + outcomeLen
deserializedBytes +=
getBigSizeBytesLength(outcomeLen) + // bytes length of outcomeLen
Number(outcomeLen); // bytes length of outcomeBuf
}

while (!reader.eof) {
Expand Down
15 changes: 15 additions & 0 deletions packages/messaging/lib/utils/bigSize.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
// BigSize integers are deserialized using @node-lightning/bufio BufferReader.readBigSize()
// Refer to https://github.com/node-lightning/node-lightning/blob/e9b349b200997b8c5e46751ae6eb13a283323f5b/packages/bufio/lib/BufferReader.ts#L194

/**
* Extract the size of a deserialized bigSize integer
* @param val
* @returns The size in bytes of the value parameter
*/

export const getBigSizeBytesLength = (val: bigint): number => {
if (val < BigInt('0xfd')) return 1;
if (val < BigInt('0x10000')) return 3;
if (val < BigInt('0x100000000')) return 5;
return 9;
};

0 comments on commit 567d0e0

Please sign in to comment.