Skip to content

Commit

Permalink
Add signInMessage to near-snap and nightly wallet.
Browse files Browse the repository at this point in the history
  • Loading branch information
kujtimprenkuSQA committed Sep 18, 2023
1 parent efb665d commit bc7a0c5
Show file tree
Hide file tree
Showing 2 changed files with 90 additions and 8 deletions.
47 changes: 46 additions & 1 deletion packages/near-snap/src/lib/selector.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,10 @@ import type {
WalletBehaviourFactory,
} from "@near-wallet-selector/core";
import { NearSnap, NearSnapAccount } from "@near-snap/sdk";
import {
verifyFullKeyBelongsToUser,
verifySignature,
} from "@near-wallet-selector/core";

export const initNearSnap: WalletBehaviourFactory<InjectedWallet> = async (
config
Expand Down Expand Up @@ -66,14 +70,55 @@ export const initNearSnap: WalletBehaviourFactory<InjectedWallet> = async (
return await account.signMessage({ message, nonce, recipient });
},

async signInMessage({ message, nonce, recipient }) {
let snapAccount: NearSnapAccount | null = null;
if (account == null) {
snapAccount = await NearSnapAccount.connect({
contractId: undefined,
methods: [],
network,
snap,
});
}

const currentAccount = account || snapAccount;

const response = await currentAccount!.signMessage({
message,
nonce,
recipient,
});

const verifiedSignature = verifySignature({
message,
nonce,
recipient,
publicKey: response.publicKey,
signature: response.signature,
});
const verifiedFullKeyBelongsToUser = await verifyFullKeyBelongsToUser({
publicKey: response.publicKey,
accountId: response.accountId,
network: options.network,
});

if (verifiedSignature && verifiedFullKeyBelongsToUser) {
return response;
} else {
throw new Error(`Failed to verify the message`);
}
},

async verifyOwner() {
throw Error("NearSnap:verifyOwner is not released yet");
},

async signAndSendTransactions({ transactions }) {
logger.log("NearSnap:signAndSendTransactions", { transactions });

if (account == null) {
const { contract } = store.getState();

if (account == null || !contract) {
throw new Error("Wallet not signed in");
}

Expand Down
51 changes: 44 additions & 7 deletions packages/nightly/src/lib/nightly.ts
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,11 @@ import type {
WalletEvents,
Account,
} from "@near-wallet-selector/core";
import { waitFor } from "@near-wallet-selector/core";
import {
verifyFullKeyBelongsToUser,
verifySignature,
waitFor,
} from "@near-wallet-selector/core";
import { signTransactions } from "@near-wallet-selector/wallet-utils";
import { isMobile } from "is-mobile";
import type { Signer } from "near-api-js";
Expand Down Expand Up @@ -147,12 +151,6 @@ const Nightly: WalletBehaviourFactory<InjectedWallet> = async ({

return {
async signIn() {
const existingAccounts = getAccounts();

if (existingAccounts.length) {
return existingAccounts;
}

await _state.wallet.connect((newAcc) => {
if (!newAcc) {
emitter.emit("signedOut", null);
Expand Down Expand Up @@ -207,6 +205,45 @@ const Nightly: WalletBehaviourFactory<InjectedWallet> = async ({
return signature;
},

async signInMessage({ message, nonce, recipient, state }) {
logger.log("Nightly:signInMessage", {
message,
nonce,
recipient,
state,
});

if (!_state.wallet.isConnected) {
await _state.wallet.connect();
}

const response = await _state.wallet.signMessage({
message,
nonce,
recipient,
state,
});

const verifiedSignature = verifySignature({
message,
nonce,
recipient,
publicKey: response.publicKey,
signature: response.signature,
});
const verifiedFullKeyBelongsToUser = await verifyFullKeyBelongsToUser({
publicKey: response.publicKey,
accountId: response.accountId,
network: options.network,
});

if (verifiedSignature && verifiedFullKeyBelongsToUser) {
return response;
} else {
throw new Error(`Failed to verify the message`);
}
},

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

Expand Down

0 comments on commit bc7a0c5

Please sign in to comment.