Skip to content

Commit

Permalink
feat: add parseSilentBlock
Browse files Browse the repository at this point in the history
  • Loading branch information
chaitika committed Oct 24, 2024
1 parent 29058be commit b565fc5
Show file tree
Hide file tree
Showing 2 changed files with 58 additions and 1 deletion.
46 changes: 45 additions & 1 deletion packages/core/src/encoding.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,13 @@ import secp256k1 from 'secp256k1';
import { Buffer } from 'buffer';
import { Network } from 'bitcoinjs-lib';
import { bitcoin } from 'bitcoinjs-lib/src/networks';
import { createTaggedHash, serialiseUint32 } from './utility';
import {
createTaggedHash,
encodingLength,
readVarInt,
serialiseUint32,
} from './utility';
import { SilentBlock } from './interface';

export const encodeSilentPaymentAddress = (
scanPubKey: Uint8Array,
Expand Down Expand Up @@ -63,3 +69,41 @@ export const createLabeledSilentPaymentAddress = (
const hrpFromNetwork = (network: Network): string => {
return network.bech32 === 'bc' ? 'sp' : 'tsp';
};

export const parseSilentBlock = (data: Buffer): SilentBlock => {
const type = data.readUInt8(0); // read 8 bits from 0th index // unsigned integer - UInt
const transactions = [];

let cursor = 1;
const count = readVarInt(data, cursor);
cursor += encodingLength(count);

for (let i = 0; i < count; i++) {
const txid = data.subarray(cursor, cursor + 32).toString('hex');
cursor += 32;

const outputs = [];
const outputCount = readVarInt(data, cursor);
cursor += encodingLength(outputCount);

for (let j = 0; j < outputCount; j++) {
const value = Number(data.readBigUInt64BE(cursor));
cursor += 8;

const pubkey = data.subarray(cursor, cursor + 32).toString('hex');
cursor += 32;

const vout = data.readUint32BE(cursor);
cursor += 4;

outputs.push({ value, pubkey, vout });
}

const scanTweak = data.subarray(cursor, cursor + 33).toString('hex');
cursor += 33;

transactions.push({ txid, outputs, scanTweak });
}

return { type, transactions };
};
13 changes: 13 additions & 0 deletions packages/core/src/interface.ts
Original file line number Diff line number Diff line change
Expand Up @@ -29,3 +29,16 @@ export type Input = {
sequence: number;
witness: Buffer[];
};

export type SilentBlock = {
type: number;
transactions: {
txid: string;
outputs: {
value: number;
pubkey: string;
vout: number;
}[];
scanTweak: string;
}[];
};

0 comments on commit b565fc5

Please sign in to comment.