From d84274177c476b3ea533259ac874f1a530258084 Mon Sep 17 00:00:00 2001 From: chaitika_ Date: Thu, 17 Oct 2024 00:16:51 +0530 Subject: [PATCH] test: parseSilentBlock --- packages/core/src/encoding.ts | 4 +-- packages/core/src/interface.ts | 2 +- packages/core/test/encoding.spec.ts | 21 ++++++++++++- packages/core/test/fixtures/encoding.ts | 41 +++++++++++++++++++++++++ 4 files changed, 64 insertions(+), 4 deletions(-) diff --git a/packages/core/src/encoding.ts b/packages/core/src/encoding.ts index 1c4086d..c09c452 100644 --- a/packages/core/src/encoding.ts +++ b/packages/core/src/encoding.ts @@ -90,13 +90,13 @@ export const parseSilentBlock = (data: Buffer): SilentBlock => { const value = Number(data.readBigUInt64BE(cursor)); cursor += 8; - const pubkey = data.subarray(cursor, cursor + 32).toString('hex'); + const pubKey = data.subarray(cursor, cursor + 32).toString('hex'); cursor += 32; const vout = data.readUint32BE(cursor); cursor += 4; - outputs.push({ value, pubkey, vout }); + outputs.push({ value, pubKey, vout }); } const scanTweak = data.subarray(cursor, cursor + 33).toString('hex'); diff --git a/packages/core/src/interface.ts b/packages/core/src/interface.ts index fe83211..28d1d00 100644 --- a/packages/core/src/interface.ts +++ b/packages/core/src/interface.ts @@ -36,7 +36,7 @@ export type SilentBlock = { txid: string; outputs: { value: number; - pubkey: string; + pubKey: string; vout: number; }[]; scanTweak: string; diff --git a/packages/core/test/encoding.spec.ts b/packages/core/test/encoding.spec.ts index dc95b6c..b91fe71 100644 --- a/packages/core/test/encoding.spec.ts +++ b/packages/core/test/encoding.spec.ts @@ -2,9 +2,10 @@ import { createLabeledSilentPaymentAddress, decodeSilentPaymentAddress, encodeSilentPaymentAddress, + parseSilentBlock, } from '../src'; import { Buffer } from 'buffer'; -import { unlabelled, labelled } from './fixtures/encoding'; +import { unlabelled, labelled, silentBlock } from './fixtures/encoding'; describe('Encoding', () => { describe.each(unlabelled)('Encode/Decode SP', (data) => { @@ -37,4 +38,22 @@ describe('Encoding', () => { ).toBe(address); }, ); + + it.each(silentBlock)('should parse silent block', (blockData) => { + const parsedBlock = parseSilentBlock( + Buffer.from(blockData.encodedBlockHex, 'hex'), + ); + expect(parsedBlock).toStrictEqual({ + type: blockData.type, + transactions: blockData.transactions.map((tx) => ({ + txid: tx.id, + scanTweak: tx.scanTweak, + outputs: tx.outputs.map((output) => ({ + pubKey: output.pubKey, + value: output.value, + vout: output.vout, + })), + })), + }); + }); }); diff --git a/packages/core/test/fixtures/encoding.ts b/packages/core/test/fixtures/encoding.ts index 3cf33bf..55c20a8 100644 --- a/packages/core/test/fixtures/encoding.ts +++ b/packages/core/test/fixtures/encoding.ts @@ -72,3 +72,44 @@ export const labelled = [ 'sp1qqgste7k9hx0qftg6qmwlkqtwuy6cycyavzmzj85c6qdfhjdpdjtdgq7c2zfthc6x3a5yecwc52nxa0kfd20xuz08zyrjpfw4l2j257yq6qgnkdh5', }, ]; + +export const silentBlock = [ + { + type: 0, + transactions: [ + { + id: 'd60cdcc1bdb78d44bf83e2ae0efa94bbb1187ba2397e4a40960d853b78e15b4f', + scanTweak: + '0381a4ef11a26108cf10d8c33305b77aec009beed82a77bb47b716a04ae9ae5bf1', + outputs: [ + { + pubKey: 'ca80a975d09b84b385e39d779293121b9a7c80942d824d2496f098e3f077ced3', + value: 24771038, + vout: 1, + }, + ], + isSpent: false, + }, + { + id: 'cf655a07ed9b672ecf811ce8b3b69257eecaf0518edd025d7014d7c0177050cb', + scanTweak: + '03d87550a4ba8ed7dd37df7739a021d6d212e70e8a8e6e9e70249c13a11fd9f412', + outputs: [ + { + pubKey: '5af9436eb515871413a913f24290ba003eab7a64b9c62cb72051599a383de8ff', + value: 26940, + vout: 0, + }, + { + pubKey: 'e7a3ab33572f47b074878c749a5f9a5d7e026f10b6ffcab1388e9356c07828fa', + value: 4400, + vout: 1, + }, + ], + isSpent: false, + }, + ], + encodedBlockHex: + '0002d60cdcc1bdb78d44bf83e2ae0efa94bbb1187ba2397e4a40960d853b78e15b4f01000000000179f9deca80a975d09b84b385e39d779293121b9a7c80942d824d2496f098e3f077ced3000000010381a4ef11a26108cf10d8c33305b77aec009beed82a77bb47b716a04ae9ae5bf1cf655a07ed9b672ecf811ce8b3b69257eecaf0518edd025d7014d7c0177050cb02000000000000693c5af9436eb515871413a913f24290ba003eab7a64b9c62cb72051599a383de8ff000000000000000000001130e7a3ab33572f47b074878c749a5f9a5d7e026f10b6ffcab1388e9356c07828fa0000000103d87550a4ba8ed7dd37df7739a021d6d212e70e8a8e6e9e70249c13a11fd9f412', + }, +];