> ## Documentation Index
> Fetch the complete documentation index at: https://docs.auth.near.org/llms.txt
> Use this file to discover all available pages before exploring further.

# Sign messages (NEP-413)

> Ask a user to sign an off-chain NEP-413 message to prove account ownership, without broadcasting a transaction of their own.

Besides transactions and delegate actions, your app can ask a user to sign an off-chain **[NEP-413](https://github.com/near/NEPs/blob/master/neps/nep-0413.md) message**, the same NEAR standard that wallets use for "sign in with NEAR" flows. The user approves the message through Auth0 just as they would a transaction, and the [Multi-Party Computation network](https://docs.auth.near.org/protocol/concepts/mpc.md) returns a signature over the SHA-256 hash of the encoded message instead of over a transaction hash. The signature comes from a `sign` call to the [NEAR Auth contract](https://docs.auth.near.org/protocol/contracts/fast-auth.md), and that call is the only transaction your app sends (see [Security notes](#security-notes)).

Use message signing when you need to **prove a user controls a NEAR account** without that account sending a transaction. For example, use it to log the user in to your backend, link an account, or authorize an off-chain action.

Message signing works with the [Browser SDK](https://docs.auth.near.org/sdks/browser-sdk.md) and the [JavaScript provider](https://docs.auth.near.org/sdks/javascript-provider.md). The React SDK and the React Native provider don't support it yet.

> [!TIP]
>
> This guide assumes you already have a Browser SDK `FastAuthSigner`. If you don't, see [Get a signer](https://docs.auth.near.org/home/guides/sign-transactions.md#get-a-signer).

***

## Sign a message (Browser SDK)

Get the `nonce` from your backend (see [Security notes](#security-notes)). The Browser SDK exposes the same request and retrieve steps as signing a transaction, and you still submit the `sign` call yourself. There's no signed transaction to broadcast afterward, because the call's **return value is the final signature**.

```ts theme={"theme":{"light":"github-light","dark":"github-dark"}}
import { FastAuthSignature } from "@fast-auth-near/browser-sdk";

// 1. Ask the user to approve the message in a popup. With `redirectUri`, run
//    the remaining steps on your callback route instead.
await signer.requestMessageSignature({
  message: "Log in to MyApp",
  recipient: "myapp.com",
  nonce, // 32-byte Uint8Array your backend issued for this session
});

// 2. Read the approved request and build the `sign` action.
const { signatureRequest } = await signer.getSignatureRequest();
const signAction = await signer.createSignAction(signatureRequest, {
  gas: 300000000000000n,
  deposit: 1n,
});

// 3. Add `signAction` to a transaction from any funded account (your
//    backend or relayer) to the NEAR Auth contract. Read `successValue`
//    from the outcome's `status.SuccessValue`. It holds the MPC signature
//    over the SHA-256 hash of the payload, or is empty if the guard
//    rejected the JWT.
if (!successValue) throw new Error("NEAR Auth did not sign the message");
const signature = FastAuthSignature.fromBase64(successValue)
  .recover("ed25519");

// 4. Send NEP-413's SignedMessage to your backend.
const publicKey = await signer.getPublicKey("ed25519");
const signedMessage = {
  accountId, // e.g. the implicit account: hex of publicKey's 32 bytes
  publicKey: publicKey.toString(),
  signature: signature.toString("base64"),
};
```

`requestMessageSignature` rejects with `NEP-413 nonce must be 32 bytes, got …` before opening Auth0 if the nonce isn't 32 bytes. If MPC signing fails, `SuccessValue` decodes to `null` and `recover` throws a `TypeError`.

Keep messages under about 1 KB. The approved payload travels inside the signing JWT as a list of decimal byte values, and the [Auth0 Guard](https://docs.auth.near.org/protocol/contracts/auth0-guard.md) rejects JWTs over 7168 bytes (7 KB). Auth0 still lets the user approve a larger message, but the `sign` call then returns no signature.

***

## Verify the signature on your backend

Your backend can't trust what the client sends, so it must independently verify the `accountId`, `publicKey`, and `signature` of NEP-413's `SignedMessage`. Accept only a nonce your backend issued to the session making the request, and accept it only once.

**1. Re-encode the message**

Encode the message with `encodeSignMessage` (exported by `@fast-auth-near/browser-sdk` and `@fast-auth-near/javascript-provider`) from values your backend already holds: your own `recipient`, the `nonce` it issued to this session, and the `message` and `callbackUrl` it expects. Don't take these values from the client. Then hash the result with SHA-256.

In a Node.js backend, load the encoder with `require()`, because the packages' ES module builds don't load in plain Node.js. In an ES module, create `require` with `createRequire(import.meta.url)` from `node:module`.

**2. Verify the signature**

Verify the `signature` against that hash with Ed25519, using the `publicKey` the client sent in the same `SignedMessage`.

**3. Verify account ownership**

Confirm the `publicKey` is a **full-access key** of `accountId`, as NEP-413 requires. A `view_access_key` RPC query must return the key with `FullAccess` permission.

New NEAR Auth users often sign with an implicit account (a 64-character hex `accountId`) that isn't funded yet, so it doesn't exist on-chain and `view_access_key` fails. **Only while the account doesn't exist**, accept an `accountId` that is the hex encoding of the ed25519 public key's 32 bytes. Treat the account as nonexistent only when a `view_account` query returns an `UNKNOWN_ACCOUNT` error, not when the query fails for any other reason. Once the account exists, keys can be added to or removed from it like any other account, so require the `view_access_key` check.

***

## Security notes

> [card] The signature is public
>
> The `sign` call is an on-chain transaction. Its arguments carry the message, recipient, and nonce, and its result is the signature. Anyone watching the chain can see the signature before your client sends it, so never treat it as a bearer credential on its own.

> [card] Issue the nonce from your backend
>
> Generate 32 random bytes server-side, bind them to the user's session, and accept each nonce once. A signature then counts only for the session that requested it, even if someone copies it from the chain.

> [card] Check the recipient server-side
>
> Re-encode the message with your own `recipient`, never one sent by the client. A message signed for another app then fails verification, so it can't be replayed against yours.

> [card] Signing can authorize actions
>
> A NEP-413 signature proves key ownership, but whoever receives it decides what it allows, and some contracts accept one as permission to act for the user. NEAR Intents (`intents.near`), for example, can move a user's funds when it receives their signed message. The consent screen shows your app's name, the recipient, any callback URL, and the message as plain text, with a note that signing can authorize actions, including moving funds. It doesn't interpret what the message authorizes, so only ask users to sign what you would accept being used as authorization.

> [card] Check the callback URL
>
> `callbackUrl` is only signed data. NEAR Auth never calls it. If your app uses it, verify it matches what you expect.

***

## Next steps

> [card] [Sign transactions](https://docs.auth.near.org/home/guides/sign-transactions.md)
>
> Request MPC signatures for on-chain transactions and gasless delegate actions.

> [card] [How MPC signing works](https://docs.auth.near.org/protocol/concepts/mpc.md)
>
> How distributed key shares produce a signature without ever assembling a private key.

> [card] [Core types](https://docs.auth.near.org/sdks/core.md)
>
> `SignMessageParams`, `encodeSignMessage`, and the rest of the shared types.

> [card] [Authenticate users](https://docs.auth.near.org/home/guides/authenticate-users.md)
>
> Log users in and manage sessions before requesting signatures.


This documentation is built and hosted on [Mintlify](https://mintlify.com), a developer documentation platform.