Skip to content

Commit

Permalink
feat: specify permissions in approval
Browse files Browse the repository at this point in the history
  • Loading branch information
jurevans committed Oct 29, 2024
1 parent 98caa16 commit dfb6ebc
Show file tree
Hide file tree
Showing 7 changed files with 40 additions and 24 deletions.
22 changes: 14 additions & 8 deletions apps/extension/src/Approvals/ApproveConnection.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ import { ConnectInterfaceResponseMsg } from "background/approvals";
import { useQuery } from "hooks";
import { useRequester } from "hooks/useRequester";
import { Ports } from "router";
import { AllowedPermissions } from "storage";
import { closeCurrentTab } from "utils";

export const ApproveConnection: React.FC = () => {
Expand All @@ -12,15 +13,13 @@ export const ApproveConnection: React.FC = () => {
const interfaceOrigin = params.get("interfaceOrigin");
const chainId = params.get("chainId")!;

const handleResponse = async (allowConnection: boolean): Promise<void> => {
const handleResponse = async (
permissions: AllowedPermissions
): Promise<void> => {
if (interfaceOrigin) {
await requester.sendMessage(
Ports.Background,
new ConnectInterfaceResponseMsg(
interfaceOrigin,
chainId,
allowConnection
)
new ConnectInterfaceResponseMsg(interfaceOrigin, chainId, permissions)
);
await closeCurrentTab();
}
Expand All @@ -35,12 +34,19 @@ export const ApproveConnection: React.FC = () => {
signing for <strong>{chainId}</strong>?
</Alert>
<Stack gap={2}>
<ActionButton onClick={() => handleResponse(true)}>
<ActionButton
onClick={() =>
// NOTE: In the future we may want the user to have
// granular control over what access the extension may
// have for any particular domain
handleResponse(["accounts", "proofGenKeys", "signing"])
}
>
Approve
</ActionButton>
<ActionButton
outlineColor="yellow"
onClick={() => handleResponse(false)}
onClick={() => handleResponse([])}
>
Reject
</ActionButton>
Expand Down
2 changes: 1 addition & 1 deletion apps/extension/src/background/approvals/handler.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -84,7 +84,7 @@ describe("approvals handler", () => {
const connectInterfaceResponseMsg = new ConnectInterfaceResponseMsg(
"",
chainId,
true
["accounts", "signing"]
);
handler(env, connectInterfaceResponseMsg);
expect(service.approveConnectionResponse).toBeCalled();
Expand Down
4 changes: 2 additions & 2 deletions apps/extension/src/background/approvals/handler.ts
Original file line number Diff line number Diff line change
Expand Up @@ -135,13 +135,13 @@ const handleConnectInterfaceResponseMsg: (
) => InternalHandler<ConnectInterfaceResponseMsg> = (service) => {
return async (
{ senderTabId: popupTabId },
{ interfaceOrigin, chainId, allowConnection }
{ interfaceOrigin, chainId, permissions }
) => {
return await service.approveConnectionResponse(
popupTabId,
interfaceOrigin,
chainId,
allowConnection
permissions
);
};
};
Expand Down
15 changes: 12 additions & 3 deletions apps/extension/src/background/approvals/messages.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -63,20 +63,29 @@ describe("approvals messages", () => {
});

test("valid ConnectInterfaceResponseMsg", () => {
const msg = new ConnectInterfaceResponseMsg("interface", "chainId", true);
const msg = new ConnectInterfaceResponseMsg("interface", "chainId", [
"accounts",
"signing",
]);

expect(msg.type()).toBe(MessageType.ConnectInterfaceResponse);
expect(msg.route()).toBe(ROUTE);
expect(msg.validate()).toBeUndefined();
});

test("invalid ConnectInterfaceResponseMsg", () => {
const msg = new ConnectInterfaceResponseMsg("interface", "chainId", true);
const msg = new ConnectInterfaceResponseMsg("interface", "chainId", [
"accounts",
"signing",
]);
(msg as any).interfaceOrigin = undefined;

expect(() => msg.validate()).toThrow();

const msg2 = new ConnectInterfaceResponseMsg("interface", "chainId", true);
const msg2 = new ConnectInterfaceResponseMsg("interface", "chainId", [
"accounts",
"signing",
]);
(msg2 as any).allowConnection = undefined;

expect(() => msg2.validate()).toThrow();
Expand Down
5 changes: 3 additions & 2 deletions apps/extension/src/background/approvals/messages.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ import { ROUTE } from "./constants";

import { TxDetails } from "@namada/types";
import { ResponseSign } from "@zondax/ledger-namada";
import { AllowedPermissions } from "storage";
import { validateProps } from "utils";

export enum MessageType {
Expand Down Expand Up @@ -148,13 +149,13 @@ export class ConnectInterfaceResponseMsg extends Message<void> {
constructor(
public readonly interfaceOrigin: string,
public readonly chainId: string,
public readonly allowConnection: boolean
public readonly permissions: AllowedPermissions
) {
super();
}

validate(): void {
validateProps(this, ["interfaceOrigin", "chainId", "allowConnection"]);
validateProps(this, ["interfaceOrigin", "chainId", "permissions"]);
}

route(): string {
Expand Down
8 changes: 4 additions & 4 deletions apps/extension/src/background/approvals/service.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -323,7 +323,7 @@ describe("approvals service", () => {
popupTabId,
interfaceOrigin,
chainId,
true
["accounts", "signing"]
);

expect(service["resolverMap"][popupTabId].resolve).toHaveBeenCalled();
Expand All @@ -343,7 +343,7 @@ describe("approvals service", () => {
popupTabId,
interfaceOrigin,
chainId,
true
["accounts", "signing"]
)
).rejects.toBeDefined();
});
Expand All @@ -363,7 +363,7 @@ describe("approvals service", () => {
popupTabId,
interfaceOrigin,
chainId,
false
[]
);

expect(service["resolverMap"][popupTabId].reject).toHaveBeenCalled();
Expand Down Expand Up @@ -449,7 +449,7 @@ describe("approvals service", () => {
popupTabId,
interfaceOrigin,
chainId,
false
[]
);

expect(service["resolverMap"][popupTabId].reject).toHaveBeenCalled();
Expand Down
8 changes: 4 additions & 4 deletions apps/extension/src/background/approvals/service.ts
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@ import { PermissionsService } from "background/permissions";
import { SdkService } from "background/sdk";
import { VaultService } from "background/vault";
import { ExtensionBroadcaster } from "extension";
import { LocalStorage } from "storage";
import { AllowedPermissions, LocalStorage } from "storage";
import { fromEncodedTx } from "utils";
import { EncodedTxData, PendingTx } from "./types";

Expand Down Expand Up @@ -224,16 +224,16 @@ export class ApprovalsService {
popupTabId: number,
interfaceOrigin: string,
chainId: string,
allowConnection: boolean
permissions: AllowedPermissions
): Promise<void> {
const resolvers = this.getResolver(popupTabId);

if (allowConnection) {
if (permissions.length) {
try {
await this.permissionsService.enablePermissions(
interfaceOrigin,
chainId,
["accounts", "proofGenKeys", "signing"]
permissions
);
// Enable signing for this chain
await this.chainService.updateChain(chainId);
Expand Down

0 comments on commit dfb6ebc

Please sign in to comment.