Skip to content

Commit

Permalink
test: parseSilentBlock
Browse files Browse the repository at this point in the history
  • Loading branch information
chaitika committed Oct 24, 2024
1 parent b565fc5 commit d842741
Show file tree
Hide file tree
Showing 4 changed files with 64 additions and 4 deletions.
4 changes: 2 additions & 2 deletions packages/core/src/encoding.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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');
Expand Down
2 changes: 1 addition & 1 deletion packages/core/src/interface.ts
Original file line number Diff line number Diff line change
Expand Up @@ -36,7 +36,7 @@ export type SilentBlock = {
txid: string;
outputs: {
value: number;
pubkey: string;
pubKey: string;
vout: number;
}[];
scanTweak: string;
Expand Down
21 changes: 20 additions & 1 deletion packages/core/test/encoding.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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) => {
Expand Down Expand Up @@ -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,
})),
})),
});
});
});
41 changes: 41 additions & 0 deletions packages/core/test/fixtures/encoding.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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',
},
];

0 comments on commit d842741

Please sign in to comment.