Skip to content

Commit

Permalink
Merge pull request #871 from near/dev
Browse files Browse the repository at this point in the history
v8.3.0 Release (dev -> main)
  • Loading branch information
kujtimprenkuSQA authored Aug 3, 2023
2 parents e2533dc + b44d2cf commit d3ba11c
Show file tree
Hide file tree
Showing 48 changed files with 427 additions and 47 deletions.
3 changes: 3 additions & 0 deletions .github/workflows/bump-version.yml
Original file line number Diff line number Diff line change
Expand Up @@ -42,6 +42,9 @@ jobs:
if: github.ref == 'refs/heads/main'
run: echo "GIT_TAG=$(git describe --exact-match --tags $(git log -n1 --pretty='%h') 2> /dev/null)" >> $GITHUB_ENV

- name: Run Build All
run: yarn build:all

- name: Deploy
run: yarn nx run-many --target=deploy --all --exclude=$EXAMPLE_APPS --dry-run
env:
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@
<button (click)="signOut()">Log out</button>
<button (click)="switchWallet()">Switch Wallet</button>
<button (click)="onVerifyOwner()">Verify Owner</button>
<button (click)="onSignMessage()">Sign Message</button>
<button *ngIf="accounts.length > 1" (click)="switchAccount()">
Switch Account
</button>
Expand Down
51 changes: 51 additions & 0 deletions examples/angular/src/app/components/content/content.component.ts
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,10 @@ import type {
CodeResult,
} from "near-api-js/lib/providers/provider";
import type { AccountState, Transaction } from "@near-wallet-selector/core";
import {
verifyFullKeyBelongsToUser,
verifySignature,
} from "@near-wallet-selector/core";

import type { Message } from "../../interfaces/message";
import type { Submitted } from "../form/form.component";
Expand Down Expand Up @@ -161,6 +165,53 @@ export class ContentComponent implements OnInit, OnDestroy {
}
}

async onSignMessage() {
const wallet = await this.selector.wallet();
const message = "test message to sign";
const nonce = Buffer.from(Array.from(Array(32).keys()));
const recipient = "guest-book.testnet";

try {
const signedMessage = await wallet.signMessage({
message,
nonce,
recipient,
});
if (signedMessage) {
const verifiedSignature = verifySignature({
message,
nonce,
recipient,
publicKey: signedMessage.publicKey,
signature: signedMessage.signature,
});
const verifiedFullKeyBelongsToUser = await verifyFullKeyBelongsToUser({
publicKey: signedMessage.publicKey,
accountId: signedMessage.accountId,
network: this.selector.options.network,
});

if (verifiedFullKeyBelongsToUser && verifiedSignature) {
alert(
`Successfully verify signed message: '${message}': \n ${JSON.stringify(
signedMessage
)}`
);
} else {
alert(
`Failed to verify signed message '${message}': \n ${JSON.stringify(
signedMessage
)}`
);
}
}
} catch (err) {
const errMsg =
err instanceof Error ? err.message : "Something went wrong";
alert(errMsg);
}
}

subscribeToEvents() {
this.subscription = this.selector.store.observable
.pipe(
Expand Down
54 changes: 53 additions & 1 deletion examples/react/components/Content.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,8 @@ import type {
CodeResult,
} from "near-api-js/lib/providers/provider";
import type { Transaction } from "@near-wallet-selector/core";
import { verifyFullKeyBelongsToUser } from "@near-wallet-selector/core";
import { verifySignature } from "@near-wallet-selector/core";
import BN from "bn.js";

import type { Account, Message } from "../interfaces";
Expand Down Expand Up @@ -81,7 +83,7 @@ const Content: React.FC = () => {
...data,
account_id: accountId,
}));
}, [accountId, selector.options]);
}, [accountId, selector]);

const getMessages = useCallback(() => {
const { network } = selector.options;
Expand All @@ -101,6 +103,7 @@ const Content: React.FC = () => {
useEffect(() => {
// TODO: don't just fetch once; subscribe!
getMessages().then(setMessages);
// eslint-disable-next-line react-hooks/exhaustive-deps
}, []);

useEffect(() => {
Expand Down Expand Up @@ -256,6 +259,54 @@ const Content: React.FC = () => {
[addMessages, getMessages]
);

const handleSignMessage = async () => {
const wallet = await selector.wallet();

const message = "test message to sign";
const nonce = Buffer.from(Array.from(Array(32).keys()));
const recipient = "guest-book.testnet";

try {
const signedMessage = await wallet.signMessage({
message,
nonce,
recipient,
});
if (signedMessage) {
const verifiedSignature = verifySignature({
message,
nonce,
recipient,
publicKey: signedMessage.publicKey,
signature: signedMessage.signature,
});
const verifiedFullKeyBelongsToUser = await verifyFullKeyBelongsToUser({
publicKey: signedMessage.publicKey,
accountId: signedMessage.accountId,
network: selector.options.network,
});

if (verifiedFullKeyBelongsToUser && verifiedSignature) {
alert(
`Successfully verify signed message: '${message}': \n ${JSON.stringify(
signedMessage
)}`
);
} else {
alert(
`Failed to verify signed message '${message}': \n ${JSON.stringify(
signedMessage
)}`
);
}
}
} catch (err) {
const errMsg =
err instanceof Error ? err.message : "Something went wrong";
alert(errMsg);
}
};

if (loading) {
return null;
}
Expand All @@ -277,6 +328,7 @@ const Content: React.FC = () => {
<button onClick={handleSignOut}>Log out</button>
<button onClick={handleSwitchWallet}>Switch Wallet</button>
<button onClick={handleVerifyOwner}>Verify Owner</button>
<button onClick={handleSignMessage}>Sign Message</button>
{accounts.length > 1 && (
<button onClick={handleSwitchAccount}>Switch Account</button>
)}
Expand Down
5 changes: 3 additions & 2 deletions package.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "near-wallet-selector",
"version": "8.2.1",
"version": "8.3.0",
"description": "NEAR Wallet Selector makes it easy for users to interact with your dApp by providing an abstraction over various wallets within the NEAR ecosystem",
"keywords": [
"near",
Expand Down Expand Up @@ -95,6 +95,7 @@
"better-sqlite3": "^8.4.0",
"big.js": "^6.1.1",
"bn.js": "^5.2.0",
"borsh": "^0.7.0",
"bs58": "^5.0.0",
"buffer": "^6.0.3",
"copy-to-clipboard": "^3.3.3",
Expand All @@ -115,7 +116,7 @@
"rxjs": "^7.8.1",
"stream-browserify": "^3.0.0",
"stream-http": "^3.2.0",
"tslib": "^2.6.0",
"tslib": "^2.6.1",
"tweetnacl": "^1.0.3",
"tweetnacl-util": "^0.15.1",
"url": "^0.11.0",
Expand Down
2 changes: 1 addition & 1 deletion packages/account-export/package.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "@near-wallet-selector/account-export",
"version": "8.2.1",
"version": "8.3.0",
"description": "This is the Export Selector UI package for NEAR Wallet Selector.",
"keywords": [
"near",
Expand Down
2 changes: 1 addition & 1 deletion packages/coin98-wallet/package.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "@near-wallet-selector/coin98-wallet",
"version": "8.2.1",
"version": "8.3.0",
"description": "Coin 98 wallet package for NEAR Wallet Selector.",
"keywords": [
"near",
Expand Down
33 changes: 33 additions & 0 deletions packages/core/docs/api/wallet.md
Original file line number Diff line number Diff line change
Expand Up @@ -293,3 +293,36 @@ Signs one or more transactions before sending to the network. The user must be s
});
})();
```

### `.signMessage(params)`

**Parameters**
- `params` (`object`)
- `message` (`string`): The message that wants to be transmitted.
- `recipient` (`string`): The recipient to whom the message is destined (e.g. "alice.near" or "myapp.com").
- `nonce` (`Buffer`): A nonce that uniquely identifies this instance of the message, denoted as a 32 bytes array (a fixed `Buffer` in JS/TS).
- `callbackUrl` (`string?`): Optional, applicable to browser wallets (e.g. MyNearWallet). The URL to call after the signing process. Defaults to `window.location.href`.
- `state` (`string?`): Optional, applicable to browser wallets (e.g. MyNearWallet). A state for authentication purposes.

**Returns**
- `Promise<void | SignedMessage>`: Browser wallets won't return the signing outcome as they may need to redirect for signing. For MyNearWallet the outcome is passed to the callback url.

**Description**

Allows users to sign a message for a specific recipient using their NEAR account, based on the [NEP413](https://github.com/near/NEPs/blob/master/neps/nep-0413.md).

**Example**

```ts
// MyNearWallet
(async () => {
const wallet = await selector.wallet("my-near-wallet");
const message = "test message for verification";
let nonceArray: Uint8Array = new Uint8Array(32);
nonceArray = crypto.getRandomValues(nonceArray);
const nonce = Buffer.from(nonceArray);
const recipient = "myapp.com";

await wallet.signMessage({ message, recipient, nonce });
})();
```
11 changes: 11 additions & 0 deletions packages/core/docs/guides/custom-wallets.md
Original file line number Diff line number Diff line change
Expand Up @@ -68,6 +68,12 @@ const MyWallet: WalletBehaviourFactory<BrowserWallet> = ({
signedTxs.map((signedTx) => provider.sendTransaction(signedTx))
);
},

async signMessage({ message, nonce, recipient, callbackUrl, state }) {
// A standardized Wallet API method, namely `signMessage`,
// that allows users to sign a message for a specific receiver using their NEAR account
return await wallet.signMessage({ message, nonce, recipient, callbackUrl, state });
},
};
};

Expand Down Expand Up @@ -143,3 +149,8 @@ Where you might have to construct NEAR Transactions and send them yourself, you
This method is similar to `signAndSendTransaction` but instead sends a batch of Transactions.

> Note: Exactly how this method should behave when transactions fail is still under review with no clear "right" way to do it. NEAR Wallet (website) seems to ignore any transactions that fail and continue executing the rest. Our approach attempts to execute the transactions in a series and bail if any fail (we will look to improve this in the future by implementing a retry feature).
### `signMessage`

This method allows users to sign a message for a specific recipient using their NEAR account.
Returns the `SignedMessage` based on the [NEP413](https://github.com/near/NEPs/blob/master/neps/nep-0413.md).
2 changes: 1 addition & 1 deletion packages/core/package.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "@near-wallet-selector/core",
"version": "8.2.1",
"version": "8.3.0",
"description": "This is the core package for NEAR Wallet Selector.",
"keywords": [
"near",
Expand Down
4 changes: 4 additions & 0 deletions packages/core/src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -68,6 +68,8 @@ export type {
DeleteAccountAction,
AddKeyPermission,
AccountImportData,
SignedMessage,
SignMessageParams,
} from "./lib/wallet";

export type { FinalExecutionOutcome } from "near-api-js/lib/providers";
Expand All @@ -76,6 +78,8 @@ export {
waitFor,
getActiveAccount,
isCurrentBrowserSupported,
verifyFullKeyBelongsToUser,
verifySignature,
} from "./lib/helpers";

export { translate, allowOnlyLanguage } from "./lib/translate/translate";
1 change: 1 addition & 0 deletions packages/core/src/lib/helpers/index.ts
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
export * from "./waitFor";
export * from "./getActiveAccount";
export * from "./detect-browser";
export * from "./verify-signature/verify-signature";
43 changes: 43 additions & 0 deletions packages/core/src/lib/helpers/verify-signature/payload.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,43 @@
import type { SignMessageParams } from "../../wallet";

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",
},
],
],
},
],
]);
Loading

0 comments on commit d3ba11c

Please sign in to comment.