@hot-labs/near-connect library provides a secure, zero-dependency wallet connector for NEAR blockchain with a unique sandbox-based architecture.
Why NEAR Connect?
Unlike traditional wallet selectors, it offers a dynamic manifest system that allows wallets to be added and updated without requiring developers to update their dependencies.- Secure Execution: Wallet scripts run in isolated sandboxed iframes for maximum security
- Dynamic Wallets: Wallets are loaded from a manifest and can be updated without code changes
- Zero Dependencies: Lightweight library with no external dependencies
- Automatic Detection: Supports both injected wallets (extensions) and manifest-based wallets
Installation
Install the@hot-labs/near-connect and near-api-js packages:
Initializing the Connector
Initialize theNearConnector instance in your application:
Selecting Wallets
Selecting Wallets
Unlike traditional wallet selectors that bundle wallet code, NEAR Connect uses a manifest-based approach:
- Wallet providers register their integration scripts in a public manifest
- The connector dynamically loads wallet scripts when users want to connect
Signing In / Out
Subscribe to thewallet:signIn and wallet:signOut events to update your application’s wallet state:
near-provider.tsx
connector.connect() to open the wallet selection modal and connector.disconnect(wallet) to sign out. In the React provider, these calls also update the local wallet and account state:
near-provider.tsx
Calling Contract Method
To call a contract method, first get the connected wallet instance usingconnector.wallet(), then use the wallet’s signAndSendTransaction method to make a function call:
Calling Read-only Methods
NEAR Connect does not provide a built-in way to call read-only (view) methods. However, you can use thenear-api-js package (or any of your preferred APIs) to create a JSON-RPC provider and call view methods directly:
Get Balance
NEAR Connect does not provide a built-in way to get the account balance. However, you can use thenear-api-js package (or any of your preferred APIs) to create a JSON-RPC provider and query the account balance directly:
Send Multiple Transactions
You can request the user to sign and send multiple transactions in parallel through a single prompt:Sign Messages (NEP-413)
In NEAR, users can sign messages for authentication purposes without needing to send a transaction on-chain:Function-call keys at login
To request a function-call access key during login, usefunction-call-key-plugin. After the user approves the key, the app can sign permitted contract calls locally without opening the wallet for each transaction.
Install the plugin alongside NEAR Connect and near-api-js:
connect():
set_greeting on hello.near-examples.testnet, with zero attached deposit and a gas allowance of 0.25 NEAR. The allowance limits how much the key can spend on gas; the account still pays transaction fees from its balance.
The plugin generates the key pair during sign-in and stores the private key locally after sign-in succeeds. Continue calling wallet.signAndSendTransaction() as shown in Calling Contract Method: the plugin signs eligible calls with the local key and sends calls outside its permissions to the wallet for approval. It also falls back to the wallet if the key’s allowance runs out or the key has been removed on-chain.
In React, create the plugin once and await its registration during initialization.