Skip to content

internet-court/internet-court-skill / vendored/near/near-api-js

near-api-js

Verified

Guide for developing with near-api-js v7 - the JavaScript/TypeScript library for NEAR blockchain interaction. Use when: (1) building apps that interact with NEAR blockchain, (2) creating/signing transactions, (3) calling smart contracts, (4) managing accounts and keys, (5) working with NEAR RPC API, (6) handling FT/NFT tokens on NEAR, (7) using NEAR cryptographic operations (KeyPair, signing), (8) converting between NEAR units (yocto, gas), (9) gasless/meta transactions with relayers, (10) NEP-413 message signing for authentication, (11) storage deposit management for FT contracts. Triggers on any NEAR blockchain development tasks.

FileKindSize
LICENSEother329 B
SKILL.mdskill md16.0 KB
references/api_patterns.mdmarkdown10.9 KB
references/contracts.mdmarkdown4.3 KB
references/key_management.mdmarkdown6.4 KB
references/meta_transactions.mdmarkdown5.8 KB
references/nep413.mdmarkdown2.7 KB
references/tokens_guide.mdmarkdown6.1 KB

references/nep413.md

View on GitHub
# NEP-413 Message Signing & Verification

NEP-413 message signing/verification.

## Table of Contents

1. [NEP-413 Message Verification](#nep-413-message-verification)
2. [NEP-413 Message Signing](#nep-413-message-signing)

---

## NEP-413 Message Verification

### verifyMessage()

```typescript
import { verifyMessage } from "near-api-js/nep413";
import { JsonRpcProvider } from "near-api-js";

const provider = new JsonRpcProvider({
  url: "https://test.rpc.fastnear.com",
});

// The challenge given to the user to sign
const MESSAGE = "log me in";
const APP = "http://localhost:3000";
const CHALLENGE = Buffer.from(Array.from(Array(32).keys()));

// Example object that could have been returned by a wallet after logging in
const walletReturn = {
  signature:
    "IfModLa3g3czlyPhkg/LSkTFSy7XCGreStZJTDIO1m3viEnYFLdXfpz1gYUVKYv3W2vwcV77TmGEzc9y0Nz+AA==",
  accountId: "alice.near",
  publicKey: "ed25519:AtH7GEjv2qmBVoT8qoRhWXizXM5CC12DC6tiqY9iNoRm",
};

// Throws an error if verification fails
await verifyMessage({
  signerAccountId: walletReturn.accountId,
  signerPublicKey: walletReturn.publicKey,
  signature: new Uint8Array(Buffer.from(walletReturn.signature, "base64")),
  payload: {
    message: MESSAGE,
    recipient: APP,
    nonce: CHALLENGE,
  },
  provider,
});
```

---

## NEP-413 Message Signing

### signMessage()

```typescript
import { signMessage } from "near-api-js/nep413";
import { Account, KeyPairSigner, KeyPair } from "near-api-js";

const account = new Account("bob.near", provider, new KeyPairSigner(keyPair));

const signedMessage = await signMessage({
  signerAccount: account,
  payload: {
    message: "Hello, world!",
    recipient: "alice.near",
    nonce: new Uint8Array(new Array(32)), // random 32 bytes
  },
});

// signedMessage contains the signature that can be passed to verifyMessage
```

### Authentication Flow

```typescript
// 1. Server generates nonce
const nonce = crypto.getRandomValues(new Uint8Array(32));

// 2. Client signs with signMessage
const signedMessage = await signMessage({
  signerAccount: account,
  payload: {
    message: `Sign in to MyApp`,
    recipient: "myapp.com",
    nonce,
  },
});

// 3. Send to server for verification
await fetch("/api/auth/verify", {
  method: "POST",
  body: JSON.stringify({
    accountId: account.accountId,
    publicKey: (await account.signer.getPublicKey()).toString(),
    signature: Array.from(signedMessage.signature),
    nonce: Array.from(nonce),
  }),
});

// 4. Server verifies
await verifyMessage({
  signerAccountId: body.accountId,
  signerPublicKey: body.publicKey,
  signature: new Uint8Array(body.signature),
  payload: {
    message: "Sign in to MyApp",
    recipient: "myapp.com",
    nonce: Buffer.from(body.nonce),
  },
  provider,
});
```
receiving-code-reviewobra/superpowersUse when receiving code review feedback, before implementing suggestions, especially if feedback seems unclear or technically questionable - requires technical rigor and verification, not performative agreement or blind implementationStars: 322kUpdated Claude CodeCodexVerifiedInstall
requesting-code-reviewobra/superpowersUse when completing tasks, implementing major features, or before merging to verify work meets requirementsStars: 322kUpdated Claude CodeCodexVerifiedInstall
using-git-worktreesobra/superpowersUse when starting feature work that needs isolation from current workspace or before executing implementation plans - ensures an isolated workspace exists via native tools or git worktree fallbackStars: 322kUpdated Claude CodeCodexVerifiedInstall
verification-before-completionobra/superpowersUse when about to claim work is complete, fixed, or passing, before committing or creating PRs - requires running verification commands and confirming output before making any success claims; evidence before assertions alwaysStars: 322kUpdated Claude CodeCodexVerifiedInstall