Skip to content

Commit

Permalink
refactor e2e test
Browse files Browse the repository at this point in the history
  • Loading branch information
notTanveer committed Oct 16, 2024
1 parent fa9a95a commit 562c18b
Show file tree
Hide file tree
Showing 4 changed files with 32 additions and 61 deletions.
32 changes: 7 additions & 25 deletions e2e/helpers/wallet.helper.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,3 @@
import { generateMnemonic, mnemonicToSeedSync } from 'bip39';
import { BIP32Interface, fromSeed } from 'bip32';
import * as ecc from 'tiny-secp256k1';
import {
Expand All @@ -10,6 +9,8 @@ import {
Transaction,
} from 'bitcoinjs-lib';
import { btcToSats } from '@e2e/helpers/common.helper';
import { randomBytes } from 'crypto';
import { toXOnly } from 'bitcoinjs-lib/src/psbt/bip371';

initEccLib(ecc);

Expand All @@ -20,33 +21,29 @@ export type UTXO = {
rawTx: string;
};
export class WalletHelper {
private mnemonic: string;
private seed: Buffer;
private root: BIP32Interface;

constructor() {
this.mnemonic = generateMnemonic();
this.seed = mnemonicToSeedSync(this.mnemonic);
this.root = fromSeed(this.seed, networks.regtest);
this.root = fromSeed(randomBytes(64), networks.regtest);
}

generateAddresses(count: number, type: 'p2wpkh' | 'p2tr'): Payment[] {
const outputs: Payment[] = [];
for (let i = 0; i < count; i++) {
const path = `m/84'/0'/0'/0/${i}`;
const child = this.root.derivePath(path);
let output;
let output: Payment;

switch (type) {
case 'p2wpkh':
output = payments.p2wpkh({
pubkey: Buffer.from(child.publicKey),
pubkey: child.publicKey,
network: networks.regtest,
});
break;
case 'p2tr':
output = payments.p2tr({
internalPubkey: Buffer.from(toXOnly(child.publicKey)),
internalPubkey: toXOnly(child.publicKey),
network: networks.regtest,
});
break;
Expand Down Expand Up @@ -98,8 +95,7 @@ export class WalletHelper {

// Sign the inputs with the corresponding private keys
utxos.forEach((utxo, index) => {
const child = this.root.derivePath(`m/84'/0'/0'/0/${index}`);
const keyPair = child;
const keyPair = this.root.derivePath(`m/84'/0'/0'/0/${index}`);
psbt.signInput(index, keyPair);
});

Expand All @@ -108,17 +104,3 @@ export class WalletHelper {
return psbt.extractTransaction(true);
}
}

const toXOnly = (pubKey) => {
if (pubKey.length === 32) {
return pubKey;
}

if (pubKey.length === 33 && (pubKey[0] === 0x02 || pubKey[0] === 0x03)) {
return pubKey.slice(1, 33);
}

throw new Error(
'Invalid public key format. Expected compressed public key or x-only key.',
);
};
50 changes: 25 additions & 25 deletions e2e/indexer.e2e-spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -16,8 +16,8 @@ describe('Silent Pay Indexer Tests', () => {
let p2wkhOutputs: Payment[];
let taprootOutput: Payment;
let utxos: UTXO[];
let transactions: any[];
let transaction: any;
let blockHash: string;

beforeAll(async () => {
walletHelper = new WalletHelper();
Expand Down Expand Up @@ -61,16 +61,17 @@ describe('Silent Pay Indexer Tests', () => {
}
}
transaction = walletHelper.craftTransaction(
utxos.slice(0, 6),
utxos,
taprootOutput, // Send 5.99 BTC to taproot address with .01 BTC fee
);

await bitcoinRPCUtil.sendRawTransaction(transaction.toHex());
const blockHash = (
await bitcoinRPCUtil.mineToAddress(1, initialAddress)
)[0];
blockHash = (await bitcoinRPCUtil.mineToAddress(1, initialAddress))[0];

await new Promise((resolve) => setTimeout(resolve, 15000));
});

it('should ensure that the correct silent block is fetched', async () => {
const response = await apiHelper.get(
`/silent-block/hash/${blockHash}`,
{
Expand All @@ -79,29 +80,28 @@ describe('Silent Pay Indexer Tests', () => {
);

const decodedBlock = parseSilentBlock(response.data);
transactions = decodedBlock.transactions;
});

it('should ensure that the correct silent block is fetched', async () => {
expect(transactions).toHaveLength(1);
const foundTx = transactions[0];

expect(foundTx.outputs.length).toBe(1);
const output = foundTx.outputs[0];
expect(output).toBeDefined();
expect(output.value).toEqual(btcToSats(5.999));

const taprootPubKeyBuffer = Buffer.from(taprootOutput.pubkey).toString(
'hex',
);

expect(output.pubkey).toEqual(taprootPubKeyBuffer);

const scantweak = generateScanTweak(
const expectedScanTweak = generateScanTweak(
transaction,
p2wkhOutputs,
indexerService,
);
expect(foundTx.scanTweak).toEqual(scantweak);
expect(decodedBlock).toMatchObject({
type: 0,
transactions: [
{
txid: transaction.getId(),
outputs: [
{
value: btcToSats(5.999),
pubkey: Buffer.from(taprootOutput.pubkey).toString(
'hex',
),
vout: 0,
},
],
scanTweak: expectedScanTweak,
},
],
});
});
});
10 changes: 0 additions & 10 deletions package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

1 change: 0 additions & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -58,7 +58,6 @@
"@typescript-eslint/eslint-plugin": "^5.0.0",
"@typescript-eslint/parser": "^5.0.0",
"bip32": "^2.0.0",
"bip39": "^3.1.0",
"bitcoinjs-lib": "^6.1.6-rc.0",
"class-transformer": "^0.5.1",
"class-validator": "^0.14.1",
Expand Down

0 comments on commit 562c18b

Please sign in to comment.