Skip to content

Commit

Permalink
test: add tests
Browse files Browse the repository at this point in the history
  • Loading branch information
emjshrx committed May 10, 2024
1 parent 7f6b201 commit a15e515
Show file tree
Hide file tree
Showing 4 changed files with 86 additions and 0 deletions.
19 changes: 19 additions & 0 deletions packages/core/test/fixtures/transaction.ts
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,9 @@ export const validTransactions = [
script: '76a914c42e7ef92fdb603af844d064faad95db9bcdfd3d88ac',
},
],
inPubkeys: [
'0279be667ef9dcbbac55a06295ce870b07029bfcdb2dce28d959f2815b16f81798',
],
},
},
{
Expand Down Expand Up @@ -54,6 +57,10 @@ export const validTransactions = [
script: 'a9147ccb85f0ab2d599bc17246c98babd5a20b1cdc7687',
},
],
inPubkeys: [
'0279be667ef9dcbbac55a06295ce870b07029bfcdb2dce28d959f2815b16f81798',
null,
],
},
},
{
Expand Down Expand Up @@ -247,6 +254,9 @@ export const validTransactions = [
script: '4effffffff01',
},
],
inPubkeys: [
'02d5ede09a8ae667d0f855ef90325e27f6ce35bbe60a1e6e87af7f5b3c652140fd',
],
},
},
{
Expand Down Expand Up @@ -368,6 +378,9 @@ export const validTransactions = [
script: '76a914851a33a5ef0d4279bd5854949174e2c65b1d450088ac',
},
],
inPubkeys: [
'038de63cf582d058a399a176825c045672d5ff8ea25b64d28d4375dcdb14c02b2b',
],
},
},
{
Expand Down Expand Up @@ -571,6 +584,9 @@ export const validTransactions = [
script: '76a914851a33a5ef0d4279bd5854949174e2c65b1d450088ac',
},
],
inPubkeys: [
'038de63cf582d058a399a176825c045672d5ff8ea25b64d28d4375dcdb14c02b2b',
],
},
},
{
Expand All @@ -597,6 +613,9 @@ export const validTransactions = [
script: '76a914851a33a5ef0d4279bd5854949174e2c65b1d450088ac',
},
],
inPubkeys: [
'038de63cf582d058a399a176825c045672d5ff8ea25b64d28d4375dcdb14c02b2b',
],
},
},
{
Expand Down
26 changes: 26 additions & 0 deletions packages/core/test/fixtures/utility.ts
Original file line number Diff line number Diff line change
Expand Up @@ -114,3 +114,29 @@ export const createTaggedHashData = [
'de9cd3236391a236466fbc0d3c79e62503cde0270c6df0c4a7d3d10ca94404e4',
},
];

export const publicKeysTestData = [
{
publickKey: '',
valid: false,
},
{
publickKey: 'random string',
valid: false,
},
{
publickKey:
'02d5ede09a8ae667d0f855ef90325e27f6ce35bbe60a1e6e87af7f5b3c652140fd',
valid: true,
},
{
publickKey:
'3045022100d269531f120f377ed2f94f42bef893ff2fe6544ac97fb477fa291bc6cfb7647e02200983f6a5bbd4ce6cf97f571995634805a7324cc5d8353ed954fa62477b0fcd0901',
valid: false,
},
{
publickKey:
'21038de63cf582d058a399a176825c045672d5ff8ea25b64d28d4375dcdb14c02b2bac',
valid: false,
},
];
30 changes: 30 additions & 0 deletions packages/core/test/transaction.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -48,4 +48,34 @@ describe('Transaction', () => {
expect(transaction.locktime).toBe(tx.raw.locktime);
},
);

const complexTransactions = [
'P2PK',
'P2SH P2PK',
'P2SH P2PKH',
'P2WSH P2PK',
'P2WSH P2PKH',
'P2SH P2WSH P2PKH',
'P2SH P2WSH P2PK',
'Standard transaction (14:2)',
'Multisig',
'P2WSH Multisig',
'P2SH P2WSH Multisig',
'Coinbase transaction w/ witness',
'P2SH Multisig',
'Coinbase transaction',
];

it.each(
validTransactions.filter(
(tx) => !complexTransactions.includes(tx.description),
),
)('gets the Public Key from Inputs of a $description', (tx) => {
const transaction = Transaction.fromHex(tx.hex);
const receivedInPubkeys = tx.raw.ins.map((_, index) => {
const PK = transaction.getPublicKeyFromInput(index);
return PK ? PK?.toString('hex') : PK;
});
expect(receivedInPubkeys).toStrictEqual(tx.raw.inPubkeys);
});
});
11 changes: 11 additions & 0 deletions packages/core/test/utility.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,11 +3,13 @@ import {
createTaggedHash,
createInputHash,
PrivateKey,
isPubKey,
} from '../src';
import {
createTaggedHashData,
createInputHashData,
inputPrivateKeys,
publicKeysTestData,
} from './fixtures/utility';

describe('Utility', () => {
Expand Down Expand Up @@ -38,4 +40,13 @@ describe('Utility', () => {
expect(inputHash.toString('hex')).toBe(expected);
},
);

it.each(publicKeysTestData)(
'should test for public key validity',
({ publickKey, valid }) => {
expect(isPubKey(Buffer.from(publickKey, 'hex'))).toStrictEqual(
valid,
);
},
);
});

0 comments on commit a15e515

Please sign in to comment.