Skip to content

Commit

Permalink
feat: modify sign transaction test to use funding transaction variabl…
Browse files Browse the repository at this point in the history
…e and to not use deployed environment
  • Loading branch information
Polybius93 committed Aug 29, 2024
1 parent 549c5b2 commit d77ef00
Show file tree
Hide file tree
Showing 2 changed files with 46 additions and 41 deletions.
39 changes: 39 additions & 0 deletions tests/mocks/bitcoin-transaction.test.constants.ts
Original file line number Diff line number Diff line change
Expand Up @@ -921,6 +921,45 @@ export const TEST_WITHDRAW_PSBT_PARTIALLY_SIGNED_WITHDRAW_PSBT_2 =
// }
// }

export const TEST_FUNDING_PSBT_PARTIALLY_SIGNED_WITHDRAW_PSBT_1 =
'70736274ff0100a80200000001dcd01b1b19b0f64371bcf4dad0483f963cec83e635b1a650e3bba6dfb91137420000000000f0ffffff0340420f0000000000225120dcdc93b920e39fe9c6efd225dca8cd4fe7bbf873dc8effeb5286159857d7bb1c1027000000000000160014622c23eebbf46df254d7da8e1c4d95d4f5c7d69fcc74e60500000000225120676504fcaf89119cc9762c2f867aaa56aa3fffc85158f7dd61da345cdbf4a9be000000000001012b00e1f50500000000225120676504fcaf89119cc9762c2f867aaa56aa3fffc85158f7dd61da345cdbf4a9be011720bb7e175e63064479102ee0b69a719a9f54f8f1b29df17cfaa5437697393e7cfc00000000';
// Readable format:
// {
// global: { txVersion: 2 },
// inputs: [
// {
// tapInternalKey: <Buffer bb 7e 17 5e 63 06 44 79 10 2e e0 b6 9a 71 9a 9f 54 f8 f1 b2 9d f1 7c fa a5 43 76 97 39 3e 7c fc>,
// txid: [Uint8Array],
// index: 0,
// witnessUtxo: [Object],
// sequence: 4294967280
// }
// ],
// outputs: [
// { amount: 1000000n, script: [Uint8Array] },
// { amount: 10000n, script: [Uint8Array] },
// { amount: 98989260n, script: [Uint8Array] }
// ],
// opts: {
// createTx: true,
// bip69: false,
// changeAddress: 'bcrt1pvajsfl903ygeejtk9shcv742264rll7g29v00htpmg69ekl54xlq8eddah',
// feePerByte: 4n,
// network: {
// messagePrefix: '\x18Bitcoin Signed Message:\n',
// bech32: 'bcrt',
// bip32: [Object],
// pubKeyHash: 111,
// scriptHash: 196,
// wif: 239
// },
// dust: 546n,
// version: 2,
// lockTime: 0,
// PSBTVersion: 0
// }
// }

// This is a testnet funding transaction with valid inputs and outputs
export const TEST_TESTNET_FUNDING_TRANSACTION_1: BitcoinTransaction = {
txid: '4cf5c2954c84bf5225d98ef014aa97bbfa0f05d56b5749782fcd8af8b9d505a5',
Expand Down
48 changes: 7 additions & 41 deletions tests/unit/sign-transactions.test.ts
Original file line number Diff line number Diff line change
@@ -1,25 +1,21 @@
import { Transaction, p2wpkh } from '@scure/btc-signer';
import { hexToBytes } from '@noble/hashes/utils';
import { Transaction } from '@scure/btc-signer';
import { regtest } from 'bitcoinjs-lib/src/networks.js';

import { PrivateKeyDLCHandler } from '../../src/index.js';
import { shiftValue } from '../../src/utilities/index.js';
import {
TEST_BITCOIN_BLOCKCHAIN_FEE_RECOMMENDATION_API,
TEST_REGTEST_BITCOIN_BLOCKCHAIN_API,
} from '../mocks/api.test.constants.js';
import { TEST_TESTNET_ATTESTOR_EXTENDED_GROUP_PUBLIC_KEY_1 } from '../mocks/attestor.test.constants.js';
import { TEST_FUNDING_PSBT_PARTIALLY_SIGNED_WITHDRAW_PSBT_1 } from '../mocks/bitcoin-transaction.test.constants.js';
import {
TEST_BITCOIN_AMOUNT,
TEST_BITCOIN_EXTENDED_PRIVATE_KEY,
TEST_BITCOIN_WALLET_ACCOUNT_INDEX,
TEST_FUNDING_PAYMENT_TYPE,
} from '../mocks/bitcoin.test.constants.js';
import { TEST_VAULT_1 } from '../mocks/ethereum-vault.test.constants.js';

describe('Create and Sign Vault related Transactions', () => {
let dlcHandler: PrivateKeyDLCHandler;
let fundingTransaction: Transaction;
let signedFundingTransaction: Transaction;

it('should initialize a Private Key DLC Handler', async () => {
dlcHandler = new PrivateKeyDLCHandler(
Expand All @@ -32,41 +28,11 @@ describe('Create and Sign Vault related Transactions', () => {
);
});

it('should create a funding transaction', async () => {
fundingTransaction = await dlcHandler.createFundingPSBT(
TEST_VAULT_1,
BigInt(shiftValue(TEST_BITCOIN_AMOUNT)),
TEST_TESTNET_ATTESTOR_EXTENDED_GROUP_PUBLIC_KEY_1,
2
);

const vaultAmount = TEST_VAULT_1.valueLocked.toBigInt();
const feeAmount = vaultAmount / TEST_VAULT_1.btcMintFeeBasisPoints.toBigInt();

const feeRecipientScript = p2wpkh(
Buffer.from(TEST_VAULT_1.btcFeeRecipient, 'hex'),
regtest
).script;
const multisigScript = dlcHandler.payment?.multisigPayment.script;

const outputs = Array.from({ length: fundingTransaction.outputsLength }, (_, index) =>
fundingTransaction.getOutput(index)
);

const multisigOutput = outputs.find(
output => output.script?.toString() === multisigScript?.toString()
);
const feeOutput = outputs.find(
output => output.script?.toString() === feeRecipientScript.toString()
);

expect(fundingTransaction).toBeDefined();
expect(multisigOutput?.amount === vaultAmount).toBeTruthy();
expect(feeOutput?.amount === feeAmount).toBeTruthy();
});

it('should sign a funding transaction', async () => {
signedFundingTransaction = dlcHandler.signPSBT(fundingTransaction, 'funding');
const signedFundingTransaction = dlcHandler.signPSBT(
Transaction.fromPSBT(hexToBytes(TEST_FUNDING_PSBT_PARTIALLY_SIGNED_WITHDRAW_PSBT_1)),
'funding'
);

expect(signedFundingTransaction.isFinal).toBeTruthy();
});
Expand Down

0 comments on commit d77ef00

Please sign in to comment.