Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Add
snap_getEntropy
JSON-RPC method (#940)
* Add snap_getEntropy JSON-RPC method * Disallow deriving SIP-6 purpose * Update bls-signer example * Fix review comments * Fix bls-signer manifest
- Loading branch information
Showing
13 changed files
with
379 additions
and
9 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
35 changes: 35 additions & 0 deletions
35
packages/rpc-methods/src/restricted/__fixtures__/entropy.ts
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,35 @@ | ||
/** | ||
* SIP-6 test vectors. | ||
*/ | ||
export const ENTROPY_VECTORS = [ | ||
{ | ||
snapId: 'foo', | ||
derivationPath: | ||
"m/1399742832'/1323571613'/1848851859'/458888073'/1339050117'/513522582'/1371866341'/2121938770'/1014285256'", | ||
entropy: | ||
'0x8bbb59ec55a4a8dd5429268e367ebbbe54eee7467c0090ca835c64d45c33a155', | ||
}, | ||
{ | ||
snapId: 'bar', | ||
derivationPath: | ||
"m/1399742832'/767024459'/1206550137'/1427647479'/1048031962'/1656784813'/1860822351'/1362389435'/2133253878'", | ||
entropy: | ||
'0xbdae5c0790d9189d8ae27fd4860b3b57bab420b6594c420ae9ae3a9f87c1ea14', | ||
}, | ||
{ | ||
snapId: 'foo', | ||
salt: 'bar', | ||
derivationPath: | ||
"m/1399742832'/2002032866'/301374032'/1159533269'/453247377'/187127851'/1859522268'/152471137'/187531423'", | ||
entropy: | ||
'0x59cbec1fa877ecb38d88c3a2326b23bff374954b39ad9482c9b082306ac4b3ad', | ||
}, | ||
{ | ||
snapId: 'bar', | ||
salt: 'baz', | ||
derivationPath: | ||
"m/1399742832'/734358031'/701613791'/1618075622'/1535938847'/1610213550'/18831365'/356906080'/2095933563'", | ||
entropy: | ||
'0x814c1f121eb4067d1e1d177246461e8a1cc6a1b1152756737aba7fa9c2161ba2', | ||
}, | ||
]; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1 @@ | ||
export * from './entropy'; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,83 @@ | ||
import { PermissionType } from '@metamask/controllers'; | ||
import { MOCK_SNAP_ID } from '@metamask/snaps-utils/test-utils'; | ||
import { deriveEntropy, getEntropyBuilder } from './getEntropy'; | ||
import { ENTROPY_VECTORS } from './__fixtures__'; | ||
|
||
const TEST_SECRET_RECOVERY_PHRASE = | ||
'test test test test test test test test test test test ball'; | ||
|
||
describe('getEntropyBuilder', () => { | ||
it('has the expected shape', () => { | ||
expect(getEntropyBuilder).toStrictEqual({ | ||
targetKey: 'snap_getEntropy', | ||
specificationBuilder: expect.any(Function), | ||
methodHooks: { | ||
getMnemonic: true, | ||
getUnlockPromise: true, | ||
}, | ||
}); | ||
}); | ||
|
||
it('returns the expected specification', () => { | ||
const methodHooks = { | ||
getMnemonic: jest.fn(), | ||
getUnlockPromise: jest.fn(), | ||
}; | ||
|
||
expect( | ||
getEntropyBuilder.specificationBuilder({ methodHooks }), | ||
).toStrictEqual({ | ||
permissionType: PermissionType.RestrictedMethod, | ||
targetKey: 'snap_getEntropy', | ||
allowedCaveats: null, | ||
methodImplementation: expect.any(Function), | ||
}); | ||
}); | ||
}); | ||
|
||
describe('deriveEntropy', () => { | ||
it.each(ENTROPY_VECTORS)( | ||
'derives entropy from the given parameters', | ||
async () => { | ||
const { snapId, salt, entropy } = ENTROPY_VECTORS[0]; | ||
|
||
expect( | ||
await deriveEntropy(snapId, TEST_SECRET_RECOVERY_PHRASE, salt ?? ''), | ||
).toStrictEqual(entropy); | ||
}, | ||
); | ||
}); | ||
|
||
describe('getEntropyImplementation', () => { | ||
it('returns the expected result', async () => { | ||
const getMnemonic = jest | ||
.fn() | ||
.mockImplementation(() => TEST_SECRET_RECOVERY_PHRASE); | ||
|
||
const getUnlockPromise = jest.fn(); | ||
|
||
const methodHooks = { | ||
getMnemonic, | ||
getUnlockPromise, | ||
}; | ||
|
||
const implementation = getEntropyBuilder.specificationBuilder({ | ||
methodHooks, | ||
}).methodImplementation; | ||
|
||
const result = await implementation({ | ||
method: 'snap_getEntropy', | ||
params: { | ||
version: 1, | ||
salt: 'foo', | ||
}, | ||
context: { | ||
origin: MOCK_SNAP_ID, | ||
}, | ||
}); | ||
|
||
expect(result).toStrictEqual( | ||
'0x6d8e92de419401c7da3cedd5f60ce5635b26059c2a4a8003877fec83653a4921', | ||
); | ||
}); | ||
}); |
Oops, something went wrong.