|
| 1 | +import { SnapCaveatType } from '../caveats'; |
| 2 | +import { |
| 3 | + getBip32EntropyBuilder, |
| 4 | + getBip32EntropyCaveatSpecifications, |
| 5 | + getBip32EntropyImplementation, |
| 6 | + validateCaveatPaths, |
| 7 | + validatePath, |
| 8 | +} from './getBip32Entropy'; |
| 9 | + |
| 10 | +const TEST_SECRET_RECOVERY_PHRASE = |
| 11 | + 'test test test test test test test test test test test ball'; |
| 12 | + |
| 13 | +describe('validatePath', () => { |
| 14 | + it.each([true, false, null, undefined, 'foo', [], new (class {})()])( |
| 15 | + 'throws if the value is not a plain object', |
| 16 | + (value) => { |
| 17 | + expect(() => validatePath(value)).toThrow('Expected a plain object.'); |
| 18 | + }, |
| 19 | + ); |
| 20 | + |
| 21 | + it.each([{}, { path: [] }, { path: 'foo' }])( |
| 22 | + 'throws if the path is invalid or empty', |
| 23 | + () => { |
| 24 | + expect(() => validatePath({})).toThrow( |
| 25 | + 'Invalid "path" parameter. The path must be a non-empty BIP-32 derivation path array.', |
| 26 | + ); |
| 27 | + }, |
| 28 | + ); |
| 29 | + |
| 30 | + it('throws if the path does not start with "m"', () => { |
| 31 | + expect(() => validatePath({ path: ["44'", "60'"] })).toThrow( |
| 32 | + 'Invalid "path" parameter. The path must start with "m".', |
| 33 | + ); |
| 34 | + }); |
| 35 | + |
| 36 | + it.each([ |
| 37 | + { path: ['m', 'foo'] }, |
| 38 | + { path: ['m', '0', 'bar'] }, |
| 39 | + { path: ['m', 0] }, |
| 40 | + ])('throws if the path is invalid', (value) => { |
| 41 | + expect(() => validatePath(value)).toThrow( |
| 42 | + 'Invalid "path" parameter. The path must be a valid BIP-32 derivation path array.', |
| 43 | + ); |
| 44 | + }); |
| 45 | + |
| 46 | + it.each([{ path: ['m'] }, { path: ['m', "44'"] }])( |
| 47 | + 'throws if the path has a length of less than three', |
| 48 | + (value) => { |
| 49 | + expect(() => validatePath(value)).toThrow( |
| 50 | + 'Invalid "path" parameter. Paths must have a length of at least three.', |
| 51 | + ); |
| 52 | + }, |
| 53 | + ); |
| 54 | + |
| 55 | + it('throws if the curve is invalid', () => { |
| 56 | + expect(() => |
| 57 | + validatePath({ path: ['m', "44'", "60'"], curve: 'foo' }), |
| 58 | + ).toThrow( |
| 59 | + 'Invalid "curve" parameter. The curve must be "secp256k1" or "ed25519".', |
| 60 | + ); |
| 61 | + }); |
| 62 | + |
| 63 | + it('throws if the curve is ed25519 and the path has an unhardened index', () => { |
| 64 | + expect(() => |
| 65 | + validatePath({ path: ['m', "44'", "60'", '1'], curve: 'ed25519' }), |
| 66 | + ).toThrow( |
| 67 | + 'Invalid "path" parameter. Ed25519 does not support unhardened paths.', |
| 68 | + ); |
| 69 | + }); |
| 70 | + |
| 71 | + it('does not throw if the path is valid', () => { |
| 72 | + expect(() => |
| 73 | + validatePath({ path: ['m', "44'", "60'"], curve: 'secp256k1' }), |
| 74 | + ).not.toThrow(); |
| 75 | + |
| 76 | + expect(() => |
| 77 | + validatePath({ path: ['m', "44'", "60'"], curve: 'ed25519' }), |
| 78 | + ).not.toThrow(); |
| 79 | + }); |
| 80 | +}); |
| 81 | + |
| 82 | +describe('validateCaveatPaths', () => { |
| 83 | + it.each([[], null, undefined, 'foo'])( |
| 84 | + 'throws if the value is not an array or empty', |
| 85 | + (value) => { |
| 86 | + expect(() => |
| 87 | + validateCaveatPaths({ |
| 88 | + type: SnapCaveatType.PermittedDerivationPaths, |
| 89 | + value, |
| 90 | + }), |
| 91 | + ).toThrow('Expected non-empty array of paths.'); |
| 92 | + }, |
| 93 | + ); |
| 94 | + |
| 95 | + it('throws if any of the paths is invalid', () => { |
| 96 | + expect(() => |
| 97 | + validateCaveatPaths({ |
| 98 | + type: SnapCaveatType.PermittedDerivationPaths, |
| 99 | + value: [{ path: ['foo'], curve: 'secp256k1' }], |
| 100 | + }), |
| 101 | + ).toThrow('Invalid "path" parameter. The path must start with "m".'); |
| 102 | + }); |
| 103 | +}); |
| 104 | + |
| 105 | +describe('specificationBuilder', () => { |
| 106 | + const methodHooks = { |
| 107 | + getMnemonic: jest.fn(), |
| 108 | + getUnlockPromise: jest.fn(), |
| 109 | + }; |
| 110 | + |
| 111 | + const specification = getBip32EntropyBuilder.specificationBuilder({ |
| 112 | + methodHooks, |
| 113 | + }); |
| 114 | + |
| 115 | + describe('validator', () => { |
| 116 | + it('throws if the caveat is not a single "permittedDerivationPaths"', () => { |
| 117 | + expect(() => |
| 118 | + // @ts-expect-error Missing required permission types. |
| 119 | + specification.validator({}), |
| 120 | + ).toThrow('Expected a single "permittedDerivationPaths" caveat.'); |
| 121 | + |
| 122 | + expect(() => |
| 123 | + // @ts-expect-error Missing other required permission types. |
| 124 | + specification.validator({ |
| 125 | + caveats: [{ type: 'foo', value: 'bar' }], |
| 126 | + }), |
| 127 | + ).toThrow('Expected a single "permittedDerivationPaths" caveat.'); |
| 128 | + |
| 129 | + expect(() => |
| 130 | + // @ts-expect-error Missing other required permission types. |
| 131 | + specification.validator({ |
| 132 | + caveats: [ |
| 133 | + { type: 'permittedDerivationPaths', value: [] }, |
| 134 | + { type: 'permittedDerivationPaths', value: [] }, |
| 135 | + ], |
| 136 | + }), |
| 137 | + ).toThrow('Expected a single "permittedDerivationPaths" caveat.'); |
| 138 | + }); |
| 139 | + }); |
| 140 | +}); |
| 141 | + |
| 142 | +describe('getBip32EntropyCaveatSpecifications', () => { |
| 143 | + describe('decorator', () => { |
| 144 | + const params = { path: ['m', "44'", "60'"], curve: 'secp256k1' }; |
| 145 | + |
| 146 | + it('returns the result of the method implementation', async () => { |
| 147 | + const fn = jest.fn().mockImplementation(() => 'foo'); |
| 148 | + |
| 149 | + expect( |
| 150 | + await getBip32EntropyCaveatSpecifications[ |
| 151 | + SnapCaveatType.PermittedDerivationPaths |
| 152 | + ].decorator(fn, { |
| 153 | + type: SnapCaveatType.PermittedDerivationPaths, |
| 154 | + value: [params], |
| 155 | + // @ts-expect-error Missing other required properties. |
| 156 | + })({ params }), |
| 157 | + ).toBe('foo'); |
| 158 | + }); |
| 159 | + |
| 160 | + it('throws if the path is invalid', async () => { |
| 161 | + const fn = jest.fn().mockImplementation(() => 'foo'); |
| 162 | + |
| 163 | + await expect( |
| 164 | + getBip32EntropyCaveatSpecifications[ |
| 165 | + SnapCaveatType.PermittedDerivationPaths |
| 166 | + ].decorator(fn, { |
| 167 | + type: SnapCaveatType.PermittedDerivationPaths, |
| 168 | + value: [params], |
| 169 | + // @ts-expect-error Missing other required properties. |
| 170 | + })({ params: { ...params, path: [] } }), |
| 171 | + ).rejects.toThrow( |
| 172 | + 'Invalid "path" parameter. The path must be a non-empty BIP-32 derivation path array.', |
| 173 | + ); |
| 174 | + }); |
| 175 | + |
| 176 | + it('throws if the path is not specified in the caveats', async () => { |
| 177 | + const fn = jest.fn().mockImplementation(() => 'foo'); |
| 178 | + |
| 179 | + await expect( |
| 180 | + getBip32EntropyCaveatSpecifications[ |
| 181 | + SnapCaveatType.PermittedDerivationPaths |
| 182 | + ].decorator(fn, { |
| 183 | + type: SnapCaveatType.PermittedDerivationPaths, |
| 184 | + value: [params], |
| 185 | + // @ts-expect-error Missing other required properties. |
| 186 | + })({ params: { ...params, path: ['m', "44'", "0'"] } }), |
| 187 | + ).rejects.toThrow( |
| 188 | + 'The requested path is not permitted. Allowed paths must be specified in the snap manifest.', |
| 189 | + ); |
| 190 | + }); |
| 191 | + }); |
| 192 | + |
| 193 | + describe('validator', () => { |
| 194 | + it('throws if the caveat values are invalid', () => { |
| 195 | + expect(() => |
| 196 | + getBip32EntropyCaveatSpecifications[ |
| 197 | + SnapCaveatType.PermittedDerivationPaths |
| 198 | + ].validator?.({ |
| 199 | + type: SnapCaveatType.PermittedDerivationPaths, |
| 200 | + value: [{ path: ['foo'], curve: 'secp256k1' }], |
| 201 | + }), |
| 202 | + ).toThrow('Invalid "path" parameter. The path must start with "m".'); |
| 203 | + }); |
| 204 | + }); |
| 205 | +}); |
| 206 | + |
| 207 | +describe('getBip32EntropyImplementation', () => { |
| 208 | + describe('getBip32Entropy', () => { |
| 209 | + it('derives the entropy from the path', async () => { |
| 210 | + const getUnlockPromise = jest.fn().mockResolvedValue(undefined); |
| 211 | + const getMnemonic = jest |
| 212 | + .fn() |
| 213 | + .mockResolvedValue(TEST_SECRET_RECOVERY_PHRASE); |
| 214 | + |
| 215 | + expect( |
| 216 | + // @ts-expect-error Missing other required properties. |
| 217 | + await getBip32EntropyImplementation({ getUnlockPromise, getMnemonic })({ |
| 218 | + params: { path: ['m', "44'", "60'"], curve: 'secp256k1' }, |
| 219 | + }), |
| 220 | + ).toMatchInlineSnapshot(` |
| 221 | + Object { |
| 222 | + "chainCode": "c4d424c253ca0eab92de6d8c819a37889e15a11bbf1cb6a48ffca2faef1f4d4d", |
| 223 | + "curve": "secp256k1", |
| 224 | + "depth": 2, |
| 225 | + "index": 2147483708, |
| 226 | + "parentFingerprint": 2557986109, |
| 227 | + "privateKey": "ca8d3571710e2b08628926f0ec14983aded0fd039518c59522c004e0e7eb4f5a", |
| 228 | + "publicKey": "041e31e8432aab932fe18b5f9798b7252394ff0b943920b40c50a79301062df5ece2b884a45c456241e35000137e6dbd92c9119ccd5f46cc92ba9568ca661b994b", |
| 229 | + } |
| 230 | + `); |
| 231 | + }); |
| 232 | + }); |
| 233 | +}); |
0 commit comments