Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat: Add support for signMessage in Sender #997

Merged
merged 5 commits into from
Nov 24, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
16 changes: 15 additions & 1 deletion packages/sender/src/lib/injected-sender.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,11 @@

// Empty string if we haven't signed in before.
import type { Account, providers } from "near-api-js";
import type { AccountImportData } from "@near-wallet-selector/core";
import type {
AccountImportData,
SignedMessage,
SignMessageParams,
} from "@near-wallet-selector/core";

export interface AccessKey {
publicKey: {
Expand Down Expand Up @@ -104,6 +108,15 @@ interface SignAndSendTransactionsResponse {
type: "sender-wallet-extensionResult";
}

interface SignMessageResponse {
actionType: "DAPP/POPUP_RESPONSE";
method: "signMessage";
notificationId: number;
error?: string;
response?: SignedMessage;
type: "sender-wallet-extensionResult";
}

interface Transaction {
receiverId: string;
actions: Array<Action>;
Expand Down Expand Up @@ -150,4 +163,5 @@ export interface InjectedSender {
params: RequestSignTransactionsParams
) => Promise<SignAndSendTransactionsResponse>;
batchImport: (params: batchImportParams) => Promise<unknown>;
signMessage: (params: SignMessageParams) => Promise<SignMessageResponse>;
}
29 changes: 29 additions & 0 deletions packages/sender/src/lib/sender.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -67,6 +67,14 @@ const mockSenderOnWindow = () => {
),
})
),
signMessage: jest.fn().mockResolvedValue({
error: undefined,
response: {
accountId,
publicKey: "ed25519:test",
signature: "testSignature",
},
}),
batchImport: jest.fn(),
};

Expand Down Expand Up @@ -170,3 +178,24 @@ describe("importAccountsInSecureContext", () => {
}
});
});

describe("signMessage", () => {
it("sign message", async () => {
const { wallet, injectedSender } = await createSenderWallet();

const message = {
message: "test message",
nonce: Buffer.from("30990309-30990309-390A303-292090"),
recipient: "test.app",
};

const result = await wallet.signMessage!(message);

expect(injectedSender?.signMessage).toHaveBeenCalled();
expect(result).toEqual({
accountId,
publicKey: "ed25519:test",
signature: "testSignature",
});
});
});
14 changes: 14 additions & 0 deletions packages/sender/src/lib/sender.ts
Original file line number Diff line number Diff line change
Expand Up @@ -252,6 +252,20 @@ const Sender: WalletBehaviourFactory<InjectedWallet> = async ({
};
},

async signMessage(message) {
return _state.wallet.signMessage(message).then((res) => {
if (res.error) {
throw new Error(res.error);
}

if (!res?.response) {
throw new Error("Invalid response");
}

return res.response;
});
},

async signAndSendTransaction({ signerId, receiverId, actions }) {
logger.log("signAndSendTransaction", { signerId, receiverId, actions });

Expand Down
Loading