Skip to content

Commit

Permalink
fix: dialog approval should halt signature return (#6)
Browse files Browse the repository at this point in the history
* fix: dialog approval should halt signature return

* fix lints
  • Loading branch information
vlopes11 authored Nov 17, 2023
1 parent 518a3f4 commit ecc22a3
Show file tree
Hide file tree
Showing 4 changed files with 67 additions and 8 deletions.
28 changes: 28 additions & 0 deletions packages/site/src/components/Sovereign.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@ type SovereignState = {
keyId: number;
nonce: number;
message?: string;
pk?: string;
tx?: string;
sequencer?: string;
status?: string;
Expand Down Expand Up @@ -86,6 +87,12 @@ export const Sovereign = () => {
<div>
<SignButton
onClick={async () => {
setState({
...state,
pk: '',
tx: '',
});

try {
const { keyId, nonce, message } = state;
const path = ['m', "44'", "1551'", `${keyId}'`];
Expand All @@ -97,6 +104,22 @@ export const Sovereign = () => {
},
};

const pkRequest = {
method: 'wallet_invokeSnap',
params: {
snapId: defaultSnapOrigin,
request: {
method: 'getPublicKey',
params: {
path,
compressed: true,
},
},
},
};

const pk = await window.ethereum.request<string>(pkRequest);

const request = {
method: 'wallet_invokeSnap',
params: {
Expand All @@ -111,6 +134,7 @@ export const Sovereign = () => {
const response = await window.ethereum.request<string>(request);
setState({
...state,
pk: pk ?? '',
tx: response ?? '',
});
} catch (er) {
Expand All @@ -122,6 +146,10 @@ export const Sovereign = () => {
}}
/>
</div>
<div>Public Key:</div>
<div>
<input type="text" value={state.pk} readOnly />
</div>
<div>Transaction:</div>
<div>
<input type="text" value={state.tx} readOnly />
Expand Down
2 changes: 1 addition & 1 deletion packages/snap/snap.manifest.json
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@
"url": "https://github.com/Sovereign-Labs/sov-snap.git"
},
"source": {
"shasum": "dHbeVd+8Pvoip3rOqq31v+STlhExNRpJAhMukHTom3g=",
"shasum": "ZcjIMDNneWe6FWkEl1qAXeyFYYTAwtJ+GuCnp6JKoMk=",
"location": {
"npm": {
"filePath": "dist/bundle.js",
Expand Down
9 changes: 7 additions & 2 deletions packages/snap/src/index.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -22,14 +22,19 @@ describe('onRpcRequest', () => {
it('returns a public key', async () => {
const { request, close } = await installSnap();

const response = await request({
const response = request({
method: 'getPublicKey',
params: {
path: ['m', "44'", "1551'"],
},
});

expect(response).toRespondWith(
const ui = await response.getInterface();
expect(ui.type).toBe('confirmation');

await ui.ok();

expect(await response).toRespondWith(
'0x00ff3c690d2a58db6d7f97e9ed0aa3455dd54a21246cf71492f36d60bb7c0a659f',
);

Expand Down
36 changes: 31 additions & 5 deletions packages/snap/src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -16,18 +16,41 @@ const wasm = new SovWasm();
*
* @param args - The request handler args as object.
* invoked the snap.
* @param args.origin - The origin of the request.
* @param args.request - A validated JSON-RPC request object.
* @returns The result of `snap_dialog`.
* @throws If the request method is not valid for this snap.
*/
export const onRpcRequest: OnRpcRequestHandler = async ({ request }) => {
export const onRpcRequest: OnRpcRequestHandler = async ({
origin,
request,
}) => {
switch (request.method) {
// the return is a plain hex string
// https://docs.metamask.io/snaps/reference/rpc-api/#returns-5
case 'getPublicKey': {
const { path, compressed } = request.params as GetBip32PublicKeyParams;

return snap.request({
// eslint-disable-next-line @typescript-eslint/await-thenable
const approved = await snap.request({
method: 'snap_dialog',
params: {
type: DialogType.Confirmation,
content: panel([
heading('Public key request'),
text(`The origin`),
copyable(origin),
text(`is requesting your public key.`),
]),
},
});

if (!approved) {
throw providerErrors.userRejectedRequest();
}

// eslint-disable-next-line @typescript-eslint/await-thenable
return await snap.request({
method: 'snap_getBip32PublicKey',
params: {
path,
Expand Down Expand Up @@ -55,17 +78,20 @@ export const onRpcRequest: OnRpcRequestHandler = async ({ request }) => {
const node = await SLIP10Node.fromJSON(entropy);
assert(node.privateKey);

const approved = snap.request({
// eslint-disable-next-line @typescript-eslint/await-thenable
const approved = await snap.request({
method: 'snap_dialog',
params: {
type: DialogType.Confirmation,
content: panel([
heading('Signature request'),
text(`Do you want to sign`),
text(`The origin`),
copyable(origin),
text(`is requesting a signature for the message`),
copyable(transaction.message),
text(`with nonce`),
copyable(transaction.nonce.toString()),
text(`and the following public key?`),
text(`and the following public key`),
copyable(add0x(node.publicKey)),
]),
},
Expand Down

0 comments on commit ecc22a3

Please sign in to comment.