Skip to content

Commit

Permalink
Add nep-413 signMessage in welldone-wallet
Browse files Browse the repository at this point in the history
  • Loading branch information
sunny0529 committed Sep 21, 2023
1 parent 5de4d28 commit a753aec
Show file tree
Hide file tree
Showing 2 changed files with 107 additions and 4 deletions.
58 changes: 58 additions & 0 deletions packages/welldone-wallet/src/lib/nep413.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,58 @@
import type { SignMessageParams } from "@near-wallet-selector/core";
import { serialize } from "borsh";

export class Payload {
tag: number;
message: string;
nonce: Buffer;
recipient: string;
callbackUrl?: string;

constructor(data: SignMessageParams) {
// The tag's value is a hardcoded value as per
// defined in the NEP [NEP413](https://github.com/near/NEPs/blob/master/neps/nep-0413.md)
this.tag = 2147484061;
this.message = data.message;
this.nonce = data.nonce;
this.recipient = data.recipient;
if (data.callbackUrl) {
this.callbackUrl = data.callbackUrl;
}
}
}

export const payloadSchema = new Map([
[
Payload,
{
kind: "struct",
fields: [
["tag", "u32"],
["message", "string"],
["nonce", [32]],
["recipient", "string"],
[
"callbackUrl",
{
kind: "option",
type: "string",
},
],
],
},
],
]);

export const isNep413Message = (message: any): boolean => {

Check failure on line 46 in packages/welldone-wallet/src/lib/nep413.ts

View workflow job for this annotation

GitHub Actions / Test Suite

Unexpected any. Specify a different type
if ("message" in message && "recipient" in message && "nonce" in message) {
return true;
}
return false;
};

export const serializeNep413 = (
signMessageParams: SignMessageParams
): Buffer => {
const payload = new Payload({ ...signMessageParams });
return Buffer.from(serialize(payloadSchema, payload));
};
53 changes: 49 additions & 4 deletions packages/welldone-wallet/src/lib/welldone.ts
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@ import type {
Optional,
Transaction,
Account,
SignMessageParams,
} from "@near-wallet-selector/core";
import { isCurrentBrowserSupported, waitFor } from "@near-wallet-selector/core";
import type {
Expand All @@ -20,6 +21,7 @@ import type {
import icon from "./icon";
import { signTransactions } from "@near-wallet-selector/wallet-utils";
import isMobile from "is-mobile";
import { isNep413Message, serializeNep413 } from "./nep413";

export const STORAGE_ACCOUNT = "account";

Expand Down Expand Up @@ -152,20 +154,54 @@ const WelldoneWallet: WalletBehaviourFactory<InjectedWallet> = async ({

return nearAPI.utils.PublicKey.from(account.publicKey!);
},
signMessage: async (message, accountId) => {
signMessage: async (
message: Uint8Array | SignMessageParams,
accountId?: string
// eslint-disable-next-line @typescript-eslint/no-explicit-any
): Promise<any> => {
if (!_state.wallet) {
throw new Error("Wallet is not installed");
}

const accounts = getAccounts();
const account = accounts.find((a) => a.accountId === accountId);
const account = accountId
? accounts.find((a) => a.accountId === accountId)
: accounts[0];

if (!account) {
throw new Error("Failed to find account for signing");
}

if (isNep413Message(message)) {
const serializedTx = serializeNep413(message as SignMessageParams);
const signed = await _state.wallet.request("near", {
method: "dapp:signMessage",
params: ["0x" + serializedTx.toString("hex")],
});

return (message as SignMessageParams).state
? {
accountId: accountId || accounts[0].accountId,
publicKey: signed[0].publicKey,
signature: Buffer.from(
signed[0].signature.substr(2),
"hex"
).toString("base64"),
state: (message as SignMessageParams).state,
}
: {
accountId: accountId || accounts[0].accountId,
publicKey: signed[0].publicKey,
signature: Buffer.from(
signed[0].signature.substr(2),
"hex"
).toString("base64"),
};
}

try {
const tx = nearAPI.transactions.Transaction.decode(
Buffer.from(message)
Buffer.from(message as Uint8Array)
);
const serializedTx = Buffer.from(tx.encode()).toString("hex");
const signed = await _state.wallet.request("near", {
Expand All @@ -178,7 +214,7 @@ const WelldoneWallet: WalletBehaviourFactory<InjectedWallet> = async ({
publicKey: nearAPI.utils.PublicKey.from(signed[0].publicKey),
};
} catch (err) {
const decoded = new TextDecoder("utf-8").decode(message);
const decoded = new TextDecoder("utf-8").decode(message as Uint8Array);
const signed = await _state.wallet.request("near", {
method: "dapp:signMessage",
params: [decoded],
Expand Down Expand Up @@ -295,6 +331,15 @@ const WelldoneWallet: WalletBehaviourFactory<InjectedWallet> = async ({
};
},

// eslint-disable-next-line @typescript-eslint/no-explicit-any
async signMessage(
message: Uint8Array | SignMessageParams,
accountId?: string
) {
// eslint-disable-next-line @typescript-eslint/no-explicit-any
return (await signer.signMessage(message as any, accountId)) as any;
},

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

Expand Down

0 comments on commit a753aec

Please sign in to comment.