Skip to content

Commit

Permalink
Add more tests
Browse files Browse the repository at this point in the history
  • Loading branch information
Mrtenz committed May 15, 2024
1 parent 58668f9 commit e5aab88
Show file tree
Hide file tree
Showing 5 changed files with 48 additions and 4 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -101,6 +101,28 @@ describe('getBip32PublicKeyImplementation', () => {
);
});

it('derives the ed25519Bip32 public key from the path', async () => {
const getUnlockPromise = jest.fn().mockResolvedValue(undefined);
const getMnemonic = jest
.fn()
.mockResolvedValue(TEST_SECRET_RECOVERY_PHRASE_BYTES);

expect(
await getBip32PublicKeyImplementation({
getUnlockPromise,
getMnemonic,
// @ts-expect-error Missing other required properties.
})({
params: {
path: ['m', "44'", "1'", "0'", "0'", "1'"],
curve: 'ed25519Bip32',
},
}),
).toMatchInlineSnapshot(
`"0xd91d18b4540a2f30341e8463d5f9b25b14fae9a236dcbea338b668a318bb0867"`,
);
});

it('derives the compressed public key from the path', async () => {
const getUnlockPromise = jest.fn().mockResolvedValue(undefined);
const getMnemonic = jest
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -13,11 +13,12 @@ import type {
import {
bip32entropy,
Bip32PathStruct,
CurveStruct,
SnapCaveatType,
} from '@metamask/snaps-utils';
import type { NonEmptyArray } from '@metamask/utils';
import { assertStruct } from '@metamask/utils';
import { boolean, enums, object, optional } from 'superstruct';
import { boolean, object, optional } from 'superstruct';

import type { MethodHooksObject } from '../utils';
import { getNode } from '../utils';
Expand Down Expand Up @@ -53,7 +54,7 @@ type GetBip32PublicKeySpecification = ValidPermissionSpecification<{
export const Bip32PublicKeyArgsStruct = bip32entropy(
object({
path: Bip32PathStruct,
curve: enums(['ed25519', 'secp256k1']),
curve: CurveStruct,
compressed: optional(boolean()),
}),
);
Expand Down
2 changes: 1 addition & 1 deletion packages/snaps-utils/coverage.json
Original file line number Diff line number Diff line change
Expand Up @@ -2,5 +2,5 @@
"branches": 96.7,
"functions": 98.72,
"lines": 98.81,
"statements": 94.78
"statements": 94.79
}
14 changes: 14 additions & 0 deletions packages/snaps-utils/src/manifest/validation.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@ import {
Bip32EntropyStruct,
Bip32PathStruct,
createSnapManifest,
CurveStruct,
EmptyObjectStruct,
isSnapManifest,
SnapIdsStruct,
Expand Down Expand Up @@ -82,6 +83,19 @@ describe('Bip32PathStruct', () => {
);
});

describe('CurveStruct', () => {
it.each(['secp256k1', 'ed25519', 'ed25519Bip32'])('validates %p', (curve) => {
expect(is(curve, CurveStruct)).toBe(true);
});

it.each([1, '', 'asd', {}, null, undefined])(
'does not validate %p',
(curve) => {
expect(is(curve, CurveStruct)).toBe(false);
},
);
});

describe('Bip32EntropyStruct', () => {
it('works with ed25519', () => {
expect(
Expand Down
9 changes: 8 additions & 1 deletion packages/snaps-utils/src/manifest/validation.ts
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
import type { SupportedCurve } from '@metamask/key-tree';
import { isValidBIP32PathSegment } from '@metamask/key-tree';
import type { EmptyObject, InitialPermissions } from '@metamask/snaps-sdk';
import {
Expand Down Expand Up @@ -107,11 +108,17 @@ export const bip32entropy = <
return true;
});

export const CurveStruct: Describe<SupportedCurve> = enums([
'ed25519',
'secp256k1',
'ed25519Bip32',
]);

// Used outside @metamask/snap-utils
export const Bip32EntropyStruct = bip32entropy(
type({
path: Bip32PathStruct,
curve: enums(['ed25519', 'secp256k1', 'ed25519Bip32']),
curve: CurveStruct,
}),
);

Expand Down

0 comments on commit e5aab88

Please sign in to comment.