Skip to content

Commit

Permalink
add fast-check tests
Browse files Browse the repository at this point in the history
  • Loading branch information
mozrt2 committed Jan 21, 2024
1 parent 558b5c5 commit 28866be
Show file tree
Hide file tree
Showing 9 changed files with 105 additions and 3 deletions.
6 changes: 6 additions & 0 deletions .projen/deps.json

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

3 changes: 2 additions & 1 deletion .projenrc.ts
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,8 @@ const project = new typescript.TypeScriptProject({
'[email protected]',
],
devDeps: [
'istanbul-badges-readme',
'[email protected]',
'[email protected]',
],
tsconfig: {
exclude: [
Expand Down
3 changes: 2 additions & 1 deletion package.json

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

19 changes: 19 additions & 0 deletions test/extractViewingPrivateKeyNode.test.ts
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
import * as fc from 'fast-check';
import { HDKey } from 'viem/accounts';
import { extractViewingPrivateKeyNode } from '../src/extractViewingPrivateKeyNode';

Expand Down Expand Up @@ -57,4 +58,22 @@ describe('extractPrivateViewingKeyNode', () => {
'Hex private viewing key is not valid.',
);
});

it('should handle a variety of valid private viewing keys without crashing', () => {
fc.assert(
fc.property(fc.hexaString({ minLength: 64, maxLength: 64 }).map(s => `0x${s}` as `0x${string}`), (randomPrivateViewingKey) => {
const result = extractViewingPrivateKeyNode(randomPrivateViewingKey, 0);
expect(result).toBeInstanceOf(HDKey);
}),
);
});

it('should throw an error for a variety of invalid private viewing keys', () => {
fc.assert(
fc.property(fc.string({ minLength: 64, maxLength: 64 }).map(s => `0x${s}` as `0x${string}`), (randomPrivateViewingKey) => {
fc.pre(!/^0x[0-9a-fA-F]{64}$/.test(randomPrivateViewingKey));
expect(() => extractViewingPrivateKeyNode(randomPrivateViewingKey)).toThrow('Hex private viewing key is not valid.');
}),
);
});
});
15 changes: 15 additions & 0 deletions test/generateEphemeralPrivateKey.test.ts
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
import * as fc from 'fast-check';
import { extractViewingPrivateKeyNode } from '../src/extractViewingPrivateKeyNode';
import { generateEphemeralPrivateKey } from '../src/generateEphemeralPrivateKey';

Expand Down Expand Up @@ -42,4 +43,18 @@ describe('generateEphemeralPrivateKey', () => {
}),
).toThrow('coinType or chainId must be defined.');
});

it('should handle a variety of valid private viewing keys and nonces without crashing', () => {
fc.assert(
fc.property(fc.hexaString({ minLength: 64, maxLength: 64 }).map(s => `0x${s}` as `0x${string}`), fc.nat(), (randomPrivateViewingKey, nonce) => {
const viewingPrivateKeyNode = extractViewingPrivateKeyNode(randomPrivateViewingKey);
const { ephemeralPrivateKey } = generateEphemeralPrivateKey({
viewingPrivateKeyNode,
nonce,
chainId: 10,
});
expect(ephemeralPrivateKey).toMatch(/^0x[0-9a-fA-F]{64}$/);
}),
);
});
});
9 changes: 9 additions & 0 deletions test/generateKeysFromSignature.test.ts
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
import * as fc from 'fast-check';
import { generateKeysFromSignature } from '../src/generateKeysFromSignature';

describe('generateKeysFromSignature', () => {
Expand All @@ -23,4 +24,12 @@ describe('generateKeysFromSignature', () => {
const signature = 'a'.repeat(132) as `0x${string}`;
expect(() => generateKeysFromSignature(signature)).toThrow('Signature is not valid.');
});

it('should handle a variety of inputs without crashing or errors', () => {
fc.assert(
fc.property(fc.hexaString({ minLength: 130, maxLength: 130 }).map(s => `0x${s}` as `0x${string}`), (signature) => {
generateKeysFromSignature(signature);
}),
);
});
});
21 changes: 21 additions & 0 deletions test/generateStealthAddresses.test.ts
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
import * as fc from 'fast-check';
import { privateKeyToAccount } from 'viem/accounts';
import { generateStealthAddresses } from '../src/generateStealthAddresses';

Expand All @@ -24,4 +25,24 @@ describe('generateStealthAddresses', () => {
],
});
});

it('should handle a variety of valid private keys without crashing', () => {
fc.assert(
fc.property(
fc.array(fc.hexaString({ minLength: 64, maxLength: 64 }).map(s => `0x${s}` as `0x${string}`), { minLength: 1, maxLength: 10 }),
fc.hexaString({ minLength: 64, maxLength: 64 }).map(s => `0x${s}` as `0x${string}`),
(privateKeys, ephemeralPrivateKey) => {
const spendingPublicKeys = privateKeys.map(privateKey => privateKeyToAccount(privateKey).publicKey);
const result = generateStealthAddresses({
spendingPublicKeys,
ephemeralPrivateKey,
});
for (const stealthAddress of result.stealthAddresses) {
expect(stealthAddress).toMatch(/^0x[0-9a-fA-F]{40}$/);
expect(stealthAddress).toHaveLength(42);
}
},
),
);
});
});
23 changes: 23 additions & 0 deletions test/predictStealthSafeAddress.test.ts
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
import * as fc from 'fast-check';
import { predictStealthSafeAddress } from '../src/predictStealthSafeAddress';

describe('predictStealthSafeAddress', () => {
Expand Down Expand Up @@ -39,4 +40,26 @@ describe('predictStealthSafeAddress', () => {
}),
).rejects.toThrow('No safe contracts found for this configuration.');
});

it('should handle a variety of valid inputs without crashing', () => {
fc.assert(
fc.asyncProperty(
fc.constantFrom(1, 5, 10, 8453, 42161),
fc.array(fc.hexaString({ minLength: 40, maxLength: 40 }).map(s => `0x${s}`), { minLength: 1, maxLength: 10 }),
async (chainId, stealthAddresses) => {
const threshold = Math.floor(Math.random() * stealthAddresses.length) + 1;
const result = await predictStealthSafeAddress({
chainId,
threshold,
stealthAddresses,
});
expect(result).toHaveProperty('stealthSafeAddress');
expect(result.stealthSafeAddress).toMatch(/^0x[a-fA-F0-9]{40}$/);
},
),
).catch((error) => {
throw error;
});
});

});
9 changes: 8 additions & 1 deletion yarn.lock

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

0 comments on commit 28866be

Please sign in to comment.