Skip to content

Commit

Permalink
feat: modify bitcoin function tests, add extra case
Browse files Browse the repository at this point in the history
  • Loading branch information
Polybius93 committed Jul 11, 2024
1 parent a9ba101 commit d6af425
Show file tree
Hide file tree
Showing 3 changed files with 20 additions and 7 deletions.
3 changes: 3 additions & 0 deletions tests/mocks/constants.ts
Original file line number Diff line number Diff line change
Expand Up @@ -58,6 +58,9 @@ export const TEST_WITHDRAW_PSBT_PARTIALLY_SIGNED_WITHDRAW_PSBT_1 =
export const TEST_DEPOSIT_PSBT_PARTIALLY_SIGNED_DEPOSIT_PSBT_2 =
'70736274ff0100d1020000000266916a1f40ca9c611a60002f81854402ffb424e457c1c23931d06267cf5bc8950000000000f0ffffff8f5413dcad4cbe8876459a1b1fe1817a94206f0d0bbb79765403438c3bb07dbe0100000000ffffffff0370170000000000001600145a81f36535980769bccc23c196337a78458abd1e8096980000000000225120b033a3d9562d22aa4a5476ae9cb6a5858b0959e084b49a3fd92edfba87e8254faa59f42400000000225120676504fcaf89119cc9762c2f867aaa56aa3fffc85158f7dd61da345cdbf4a9be000000000001012b40be402500000000225120676504fcaf89119cc9762c2f867aaa56aa3fffc85158f7dd61da345cdbf4a9be011340fd74c7650cb7edb0eea5fca080ac7dea90ab23ecf616eac1ad4a263d75fcf3940d3c7c09512369cb39a473e4eb1249204df36115095dc2849556686b9087824e011720bb7e175e63064479102ee0b69a719a9f54f8f1b29df17cfaa5437697393e7cfc0001012b404b4c0000000000225120b033a3d9562d22aa4a5476ae9cb6a5858b0959e084b49a3fd92edfba87e8254f4114bb7e175e63064479102ee0b69a719a9f54f8f1b29df17cfaa5437697393e7cfce24db130ab82d20d322c295aa18921a7fe87fb13df3d5b3b2ee0469e2a845114404387a71d8174f9c42ef8ce8cbe0c88c01863fb80ea796955f6678b95a7ae7e07dd1559688bee882feefdda2e98880ea6e6ba886c438bdb2188b41f4f68d09f992215c01fba2e8661f2ed0a747d76578d3a6508a31879569d97da86a0a207c2538ebaa445205216652630455eb8546bbab45e6fdef6498eee683ed339f405d326414b05b192ad20bb7e175e63064479102ee0b69a719a9f54f8f1b29df17cfaa5437697393e7cfcacc00117201fba2e8661f2ed0a747d76578d3a6508a31879569d97da86a0a207c2538ebaa4011820e24db130ab82d20d322c295aa18921a7fe87fb13df3d5b3b2ee0469e2a84511400000000';

export const TEST_DEPOSIT_PSBT_PARTIALLY_SIGNED_DEPOSIT_PSBT_3 =
'70736274ff0100fd17010200000004591440ba8921026ba9c35a08fe1be0b192fa59c328919070216ecc396529adaf0200000000f0ffffffaea70896bc92b479b8dd98d2e706b9c6492cd6f567caf34713c71b10cafd746a0200000000ffffffffbb6f00e7148900daf36a20f08152aebce2ed41085e8bbfc28c8aff796dec4f4f0200000000ffffffffcb6450850a4596027f12d43ce19b624185196fd1831abfda0afffced3e9c0d700100000000ffffffff03001bb700000000001600145a81f36535980769bccc23c196337a78458abd1e402f585402000000225120bca90746e285c10235453757249fcac29178e418bef5492b5add21475f76232a3e539f2601000000160014add70fb03578d3ac85aab80897395bb046223c92000000000001011f10c7202901000000160014add70fb03578d3ac85aab80897395bb046223c92220202f4d8696f9b275f4e10af63f03bcb7fbba7b1ed44dd9b12a973c8d20212beb8d14830450221009a699d65d75cf43a4b9ef89f3717352cb4af3981848e9a406ada7a1a727fc98502207b063ca138901ce774e005ef543cd225c976fb8d1dbfbe5a63d85b96dfaba356010001011f10c7202901000000160014add70fb03578d3ac85aab80897395bb046223c92220202f4d8696f9b275f4e10af63f03bcb7fbba7b1ed44dd9b12a973c8d20212beb8d1483045022100cfd104f2751a744510cc5e07dbb1c48c597db3c69aa48e52de7be379a00db0d40220360c0e608c016a19a4bd19c9235b01f18c6e7998f439475cc8637fdd04d7cecf010001011f10c7202901000000160014add70fb03578d3ac85aab80897395bb046223c92220202f4d8696f9b275f4e10af63f03bcb7fbba7b1ed44dd9b12a973c8d20212beb8d1483045022100de829b10d533c05b3ff7d65caa3503e7d3c16ef5d1a83224c21de63c5f88531d02207d13d47f640aa4f85511293d363af5e7158c328b312e175f9e3e0290eaa54618010001012b404b4c0000000000225120bca90746e285c10235453757249fcac29178e418bef5492b5add21475f76232a4114bb7e175e63064479102ee0b69a719a9f54f8f1b29df17cfaa5437697393e7cfce24db130ab82d20d322c295aa18921a7fe87fb13df3d5b3b2ee0469e2a845114406d997b5d10bccd43e8c210c1b21685f866e14d3648954767cd8656311e3cff3b30c3581d4eafd4468a376af3ab8f305118fad37083fca5da48d3ab9953bfa3602215c1d6d94d6dd718c7ad15270bc3e3ed3058d4c2253942ff1c1cbea1b1105fd6cc9145205216652630455eb8546bbab45e6fdef6498eee683ed339f405d326414b05b192ad20bb7e175e63064479102ee0b69a719a9f54f8f1b29df17cfaa5437697393e7cfcacc0011720d6d94d6dd718c7ad15270bc3e3ed3058d4c2253942ff1c1cbea1b1105fd6cc91011820e24db130ab82d20d322c295aa18921a7fe87fb13df3d5b3b2ee0469e2a84511400000000';

export const TEST_WITHDRAW_PSBT_PARTIALLY_SIGNED_WITHDRAW_PSBT_2 =
'70736274ff0100a80200000001b2097c129d74fc6e80d673ba3a83e4858c046444993991cad52f66d28ff65b970000000000f0ffffff034c1d0000000000001600145a81f36535980769bccc23c196337a78458abd1e404b4c0000000000225120b033a3d9562d22aa4a5476ae9cb6a5858b0959e084b49a3fd92edfba87e8254f822c4c0000000000225120676504fcaf89119cc9762c2f867aaa56aa3fffc85158f7dd61da345cdbf4a9be000000000001012b8096980000000000225120b033a3d9562d22aa4a5476ae9cb6a5858b0959e084b49a3fd92edfba87e8254f4114bb7e175e63064479102ee0b69a719a9f54f8f1b29df17cfaa5437697393e7cfce24db130ab82d20d322c295aa18921a7fe87fb13df3d5b3b2ee0469e2a84511440c06edfdd88bc66ffec4c8ff30fd3f6b38e25ebd85c09635a7b1594f66af614ba320c4d98e1c6017f38c8d752227ca24a5cd178cb462a4444c465fc773738e4b52215c01fba2e8661f2ed0a747d76578d3a6508a31879569d97da86a0a207c2538ebaa445205216652630455eb8546bbab45e6fdef6498eee683ed339f405d326414b05b192ad20bb7e175e63064479102ee0b69a719a9f54f8f1b29df17cfaa5437697393e7cfcacc00117201fba2e8661f2ed0a747d76578d3a6508a31879569d97da86a0a207c2538ebaa4011820e24db130ab82d20d322c295aa18921a7fe87fb13df3d5b3b2ee0469e2a84511400000000';

Expand Down
18 changes: 14 additions & 4 deletions tests/unit/bitcoin-functions.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -15,12 +15,13 @@ import {
TEST_ALICE_TAPROOT_PUBLIC_KEY_2,
TEST_DEPOSIT_PSBT_PARTIALLY_SIGNED_DEPOSIT_PSBT_1,
TEST_DEPOSIT_PSBT_PARTIALLY_SIGNED_DEPOSIT_PSBT_2,
TEST_DEPOSIT_PSBT_PARTIALLY_SIGNED_DEPOSIT_PSBT_3,
TEST_WITHDRAW_PSBT_PARTIALLY_SIGNED_WITHDRAW_PSBT_1,
} from '../mocks/constants';

describe('Bitcoin Functions', () => {
describe('getInputIndicesByScript', () => {
it('correctly retrieves the input indices by script', () => {
it('correctly retrieves one input index by script', () => {
const transaction = Transaction.fromPSBT(
hexToBytes(TEST_DEPOSIT_PSBT_PARTIALLY_SIGNED_DEPOSIT_PSBT_1)
);
Expand All @@ -29,7 +30,16 @@ describe('Bitcoin Functions', () => {
expect(inputIndices).toEqual([0]);
});

it('correctly retrieve an empty array when the script is not found', () => {
it('correctly retrieves multiple input indices by script', () => {
const transaction = Transaction.fromPSBT(
hexToBytes(TEST_DEPOSIT_PSBT_PARTIALLY_SIGNED_DEPOSIT_PSBT_3)
);
const aliceScript = hexToBytes(TEST_ALICE_NATIVE_SEGWIT_PAYMENT_SCRIPT_1);
const inputIndices = getInputIndicesByScript(aliceScript, transaction);
expect(inputIndices).toEqual([0, 1, 2]);
});

it('correctly retrieve an empty array when no inputs found with given script', () => {
const transaction = Transaction.fromPSBT(
hexToBytes(TEST_WITHDRAW_PSBT_PARTIALLY_SIGNED_WITHDRAW_PSBT_1)
);
Expand All @@ -54,7 +64,7 @@ describe('Bitcoin Functions', () => {
expect(transaction.getInput(1).finalScriptWitness).toBeUndefined();
});

it('does not finalize inputs given a transaction and a native segwit payment script', () => {
it('does not finalize inputs given a transaction and a native segwit payment script that is not included in this transaction', () => {
const transaction = Transaction.fromPSBT(
hexToBytes(TEST_DEPOSIT_PSBT_PARTIALLY_SIGNED_DEPOSIT_PSBT_1)
);
Expand Down Expand Up @@ -85,7 +95,7 @@ describe('Bitcoin Functions', () => {
expect(transaction.getInput(1).finalScriptWitness).toBeUndefined();
});

it('does not finalize inputs given a transaction and a taproot payment script', () => {
it('does not finalize inputs given a transaction and a taproot payment script that is not included in this transaction', () => {
const transaction = Transaction.fromPSBT(
hexToBytes(TEST_DEPOSIT_PSBT_PARTIALLY_SIGNED_DEPOSIT_PSBT_2)
);
Expand Down
6 changes: 3 additions & 3 deletions tests/unit/utility.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -31,9 +31,9 @@ describe('Utility Functions', () => {
describe('customShiftValue', () => {
it('correctly shifts the value based on the provided parameters', () => {
const value = 1;
const shift = 8;
const shift = 12;
const shiftedValue = customShiftValue(value, shift, false);
expect(shiftedValue).toBe(100000000);
expect(shiftedValue).toBe(1000000000000);
});

it('correctly unshifts the value based on the provided parameters', () => {
Expand Down Expand Up @@ -133,7 +133,7 @@ describe('Utility Functions', () => {

describe('delay', () => {
it('delays execution for the specified number of milliseconds', () => {
const delayTime = 1000;
const delayTime = 10;
const startTime = Date.now();
delay(delayTime).then(() => {
const endTime = Date.now();
Expand Down

0 comments on commit d6af425

Please sign in to comment.