Skip to content

Commit

Permalink
Merge pull request #193 from zavodil/signMessage
Browse files Browse the repository at this point in the history
feat: Sign message

This PR enables the use of the signMessage method from the wallet-selector. The user can sign a message with any of the connected wallets for further [signature verification](https://docs.near.org/build/web3-apps/backend/) on a third-party authentication server.

- Add `Near.signMessage` function to sign arbitrary message with NEAR account. The message that the user needs to sign contains 4 fields:
  - `message`: The message that the user is signing.
  - `recipient`: The recipient of the message.
  - `nonce`: The challenge that the user is signing.
  - `callbackUrl`: The URL that the wallet will call with the signature.
How to verify the signature: https://docs.near.org/build/web3-apps/backend/#3-verify-the-signature

- Add get/set methods for `window.location.hash`. `Set` method only accepts an empty string as a value.
  • Loading branch information
evgenykuzyakov authored May 28, 2024
2 parents 52bdf78 + 8779f17 commit 257ded3
Show file tree
Hide file tree
Showing 4 changed files with 56 additions and 1 deletion.
8 changes: 8 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,14 @@
## Pending

- Add 2s animation delay to the loading spinner. Makes it visually less jarring when loading is quick.
- Add `Near.signMessage` function to sign arbitrary message with NEAR account. The message that the user needs to sign contains 4 fields:
- `message`: The message that the user is signing.
- `recipient`: The recipient of the message.
- `nonce`: The challenge that the user is signing.
- `callbackUrl`: The URL that the wallet will call with the signature.
How to verify the signature: https://docs.near.org/build/web3-apps/backend/#3-verify-the-signature

- Add get/set methods for `window.location.hash`. `Set` method only accepts an empty string as a value.

## 2.6.1

Expand Down
2 changes: 1 addition & 1 deletion dist/index.js

Large diffs are not rendered by default.

9 changes: 9 additions & 0 deletions src/lib/data/near.js
Original file line number Diff line number Diff line change
Expand Up @@ -284,6 +284,11 @@ function setupContract(near, contractId, options) {
return contract;
}

async function signMessage(near, args) {
const wallet = await (await near.selector).wallet();
return wallet.signMessage(args);
}

async function viewCall(
provider,
blockId,
Expand Down Expand Up @@ -402,6 +407,10 @@ async function _initNear({
blockId: undefined,
};

_near.signMessage = (args) => {
return signMessage(_near, args);
}

_near.viewCall = (contractId, methodName, args, blockHeightOrFinality) => {
const { blockId, finality } = transformBlockId(blockHeightOrFinality);
const nearViewCall = () =>
Expand Down
38 changes: 38 additions & 0 deletions src/lib/vm/vm.js
Original file line number Diff line number Diff line change
Expand Up @@ -1612,6 +1612,31 @@ export default class VM {
};

const Near = {
signMessage: (...args) => {
if (args.length === 1) {
if (isObject(args[0])) {
return this.near.signMessage(args[0]);
} else {
throw new Error(
"Method: Near.signMessage. Required argument: '{message, recipient, nonce, callbackUrl}'"
);
}
} else if (args.length < 3) {
throw new Error(
"Method: Near.signMessage. Required arguments: 'message', 'recipient', 'nonce'. Optional: 'callbackUrl'"
);
}
else {
const [
message,
recipient,
nonce,
callbackUrl
] = args;

return this.near.signMessage({message, recipient, nonce, callbackUrl});
}
},
view: (...args) => {
if (args.length < 2) {
throw new Error(
Expand Down Expand Up @@ -2327,6 +2352,19 @@ export default class VM {
this.state = {
...GlobalInjected,
...this.globalFunctions,
window: {
location: {
get hash() {
return window.location.hash
},
set hash(value) {
if (value !== "") {
throw new Error("Set `window.location.hash` only accepts an empty string as a value.");
}
history.pushState(value, document.title, window.location.pathname + window.location.search);
}
}
},
props: isObject(props) ? Object.assign({}, props) : props,
context,
state,
Expand Down

0 comments on commit 257ded3

Please sign in to comment.