Skip to content

Commit

Permalink
Add support for ed25519Bip32 to the BIP-32 example snap (#2428)
Browse files Browse the repository at this point in the history
Add support for the `ed25519Bip32` curve to the BIP-32 example Snap for
E2E testing purposes.

---------

Co-authored-by: Frederik Bolding <[email protected]>
  • Loading branch information
bowensanders and FrederikBolding authored May 27, 2024
1 parent 00a9587 commit 1d0be14
Show file tree
Hide file tree
Showing 6 changed files with 52 additions and 7 deletions.
10 changes: 9 additions & 1 deletion packages/examples/packages/bip32/snap.manifest.json
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@
"url": "https://github.com/MetaMask/snaps.git"
},
"source": {
"shasum": "GVN0FD0c4xnr+osY4rjRvF/KSGBgVC2fccbLwzGoFnk=",
"shasum": "VTYKPC0W5V/eIRyWm3BWx596kB2yHcAtduJKwfZM9mg=",
"location": {
"npm": {
"filePath": "dist/bundle.js",
Expand Down Expand Up @@ -38,6 +38,14 @@
"0'"
],
"curve": "ed25519"
},
{
"path": [
"m",
"44'",
"0'"
],
"curve": "ed25519Bip32"
}
],
"snap_getBip32PublicKey": [
Expand Down
32 changes: 32 additions & 0 deletions packages/examples/packages/bip32/src/index.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -160,6 +160,38 @@ describe('onRpcRequest', () => {
);
});

it('signs a message for the given BIP-32 path using ed25519Bip32', async () => {
const { request } = await installSnap();

const response = request({
method: 'signMessage',
params: {
path: ['m', "44'", "0'"],
curve: 'ed25519Bip32',
message: 'Hello, world!',
},
});

const ui = await response.getInterface();
expect(ui).toRender(
panel([
heading('Signature request'),
text(
`Do you want to ed25519Bip32 sign "Hello, world!" with the following public key?`,
),
copyable(
'0x2c3ac523b470dead7981df46c93d894ed4381e94c23aa1ec3806a320ff8ceb42',
),
]),
);

await ui.ok();

expect(await response).toRespondWith(
'0x5c01bf4c314a74d3feb27b261637837740e167cf3261fe0bc89dcdbf97888276be030a84298087c031141c1093647768de5b35c1154ec485cb08373a72574902',
);
});

it('throws an error when rejecting the signature request', async () => {
const { request } = await installSnap();

Expand Down
10 changes: 7 additions & 3 deletions packages/examples/packages/bip32/src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -54,7 +54,11 @@ export const onRpcRequest: OnRpcRequestHandler = async ({ request }) => {
const node = await getPrivateNode({ ...params, curve });

assert(node.privateKey);
assert(curve === 'ed25519' || curve === 'secp256k1');
assert(
curve === 'ed25519' ||
curve === 'ed25519Bip32' ||
curve === 'secp256k1',
);

const approved = await snap.request({
method: 'snap_dialog',
Expand All @@ -74,10 +78,10 @@ export const onRpcRequest: OnRpcRequestHandler = async ({ request }) => {
throw new UserRejectedRequestError();
}

if (curve === 'ed25519') {
if (curve === 'ed25519' || curve === 'ed25519Bip32') {
const signed = await signEd25519(
stringToBytes(message),
remove0x(node.privateKey),
remove0x(node.privateKey).slice(0, 64),
);
return bytesToHex(signed);
}
Expand Down
4 changes: 2 additions & 2 deletions packages/examples/packages/bip32/src/types.ts
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@ export type GetBip32PublicKeyParams = {
/**
* The curve used to derive the account.
*/
curve: 'secp256k1' | 'ed25519';
curve: 'secp256k1' | 'ed25519' | 'ed25519Bip32';

/**
* Whether to return the public key in compressed form.
Expand Down Expand Up @@ -46,5 +46,5 @@ export type SignMessageParams = {
/**
* The curve used to derive the account.
*/
curve: 'secp256k1' | 'ed25519';
curve: 'secp256k1' | 'ed25519' | 'ed25519Bip32';
};
1 change: 1 addition & 0 deletions packages/test-snaps/src/features/snaps/bip32/BIP32.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@ export const BIP32: FunctionComponent = () => {
<PublicKey />
<SignMessage curve="secp256k1" />
<SignMessage curve="ed25519" />
<SignMessage curve="ed25519Bip32" />
</Snap>
);
};
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@ import { getSnapId } from '../../../../utils';
import { BIP_32_PORT, BIP_32_SNAP_ID } from '../constants';

export type SignMessageProps = {
curve: 'secp256k1' | 'ed25519';
curve: 'secp256k1' | 'ed25519' | 'ed25519Bip32';
};

export const SignMessage: FunctionComponent<SignMessageProps> = ({ curve }) => {
Expand Down

0 comments on commit 1d0be14

Please sign in to comment.