Skip to content

Commit

Permalink
feat: Add sign message support (#958)
Browse files Browse the repository at this point in the history
* feat(my-near-wallet): Add sign message support

* Fixes after review
  • Loading branch information
Strnadj authored Oct 12, 2023
1 parent d0000d2 commit 52fa86e
Show file tree
Hide file tree
Showing 2 changed files with 55 additions and 0 deletions.
28 changes: 28 additions & 0 deletions packages/my-near-wallet/src/lib/my-near-wallet.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -132,3 +132,31 @@ describe("buildImportAccountsUrl", () => {
);
});
});

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

const replace = window.location.replace;

Object.defineProperty(window, "location", {
value: { replace: jest.fn() },
});

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

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

expect(result).toBe(undefined);
expect(window.location.replace).toHaveBeenCalledWith(
"https://testnet.mynearwallet.com/sign-message?message=test+message&nonce=30990309-30990309-390A303-292090&recipient=test.app&callbackUrl=https%3A%2F%2Ftest.app"
);

window.location.replace = replace;
});
});
27 changes: 27 additions & 0 deletions packages/my-near-wallet/src/lib/my-near-wallet.ts
Original file line number Diff line number Diff line change
Expand Up @@ -160,6 +160,33 @@ const MyNearWallet: WalletBehaviourFactory<
throw new Error(`Method not supported by ${metadata.name}`);
},

async signMessage({ message, nonce, recipient, callbackUrl, state }) {
logger.log("sign message", { message });

const locationUrl =
typeof window !== "undefined" ? window.location.href : "";

const url = callbackUrl || locationUrl;

if (!url) {
throw new Error(`The callbackUrl is missing for ${metadata.name}`);
}

const href = new URL(params.walletUrl);
href.pathname = "sign-message";
href.searchParams.append("message", message);
href.searchParams.append("nonce", nonce.toString());
href.searchParams.append("recipient", recipient);
href.searchParams.append("callbackUrl", url);
if (state) {
href.searchParams.append("state", state);
}

window.location.replace(href.toString());

return;
},

async signAndSendTransaction({
signerId,
receiverId,
Expand Down

0 comments on commit 52fa86e

Please sign in to comment.