@near-wallet-selector/core
This is the core package for NEAR Wallet Selector.
Installation and Usage
The easiest way to use this package is to install it from the NPM registry:
yarn add @near-wallet-selector/core
npm install @near-wallet-selector/core
Then use it in your dApp:
import NearWalletSelector from "@near-wallet-selector/core";
import { setupNearWallet } from "@near-wallet-selector/near-wallet";
const selector = await NearWalletSelector.init({
network: "testnet",
contractId: "guest-book.testnet",
wallets: [setupNearWallet()],
});
API Reference
.init(options)
Parameters
options
(object
)
network
(string | object
): Network ID or object matching that of your dApp configuration . Network ID can be either mainnet
, testnet
or betanet
.
networkId
(string
): Custom network ID (e.g. localnet
).nodeUrl
(string
): Custom URL for RPC requests.helperUrl
(string
): Custom URL for creating accounts.explorerUrl
(string
): Custom URL for
contractId
(string
): Account ID of the Smart Contract used for sign in and transactions.wallets
(Array<string | WalletModule>
): List of wallets you want to support in your dApp.methodNames
(Array<string>?
): Specify limited access to particular methods on the Smart Contract.ui
: (object?
)
theme
(string?
): Specify light/dark theme for UI. Defaults to the browser configuration when omitted or set to 'auto'. This can be either light
, dark
or auto
.description
(string?
): Define a custom description in the UI.
Returns
Promise<NearWalletSelector>
Description
Initialises the selector using the configured options before rendering the UI. If a user has previously signed in, this method will also initialise the selected wallet, ready to handle signing.
Example
await NearWalletSelector.init({
network: "testnet",
contractId: "guest-book.testnet",
wallets: [],
});
.show()
Parameters
Returns
Description
Opens the modal for users to sign in to their preferred wallet. You can also use this method to switch wallets.
Example
selector.show();
.hide()
Parameters
Returns
Description
Closes the modal.
Example
selector.hide();
.signIn(params)
Parameters
params
(object
)
walletId
(string
): ID of the wallet (see example for specific values).accountId
(string?
): Required for hardware wallets (e.g. Ledger Wallet). This is the account ID related to the public key found at derivationPath
.derviationPath
(string?
): Required for hardware wallets (e.g. Ledger Wallet). This is the path to the public key on your device.
Returns
Description
Programmatically sign in to a specific wallet without presenting the UI. Hardware wallets (e.g. Ledger Wallet) require accountId
and derivationPath
to validate access key permissions.
Example
await selector.signIn({
walletId: "near-wallet",
});
await selector.signIn({
walletId: "sender",
});
await selector.signIn({
walletId: "math-wallet",
});
await selector.signIn({
walletId: "ledger",
accountId: "account-id.testnet",
derviationPath: "44'/397'/0'/0'/1'",
});
await selector.signIn({
walletId: "wallet-connect",
});
.signOut()
Parameters
Returns
Description
Signs out of the selected wallet.
Example
await selector.signOut();
.isSignedIn()
Parameters
Returns
Description
Determines whether the user is signed in.
Example
const signedIn = await selector.isSignedIn();
console.log(signedIn)
.getAccounts()
Parameters
Returns
Promise<Array<object>>
accountId
: An account id for each account associated with the selected wallet.
Description
Retrieves account information when the user is signed in. Returns an empty array when the user is signed out. This method can be useful for wallets that support accounts at once such as WalletConnect. In this case, you can use an accountId
returned as the signerId
for signAndSendTransaction
.
Example
const accounts = await selector.getAccounts();
console.log(accounts);
.on(event, callback)
Parameters
event
(string
): Name of the event. This can be either signIn
or signOut
.callback
(() => void
): Handler to be triggered when the event
fires.
Returns
object
remove
(() => void
): Removes the event handler.
Description
Attach an event handler to important events.
Example
const subscription = selector.on("signIn", () => {
console.log("User signed in!");
});
subscription.remove();
.off(event, callback)
Parameters
event
(string
): Name of the event. This can be either signIn
or signOut
.callback
(() => void
): Original handler passed to .on(event, callback)
.
Returns
Description
Removes the event handler attached to the given event
.
Example
const handleSignIn = () => {
console.log("User signed in!");
}
selector.on("signIn", handleSignIn);
selector.off("signIn", handleSignIn);
.getContractId()
Parameters
Returns
Description
Retrieves account ID of the configured Smart Contract.
Example
const contractId = selector.getContractId();
console.log(contractId);
.signAndSendTransaction(params)
Parameters
params
(object
)
signerId
(string?
): Account ID used to sign the transaction. Defaults to the first account.receiverId
(string?
): Account ID to receive the transaction. Defaults to contractId
defined in .init
.actions
(Array<Action>
)
type
(string
): Action type. See below for available values.params
(object?
): Parameters for the Action (if applicable).
Returns
Promise<void | object>
: Browser wallets won't return the transaction outcome as they may need to redirect for signing. More details on this can be found here.
Description
Signs one or more actions before sending to the network. The user must be signed in to call this method as there's at least charges for gas spent.
Note: Sender only supports "FunctionCall"
action types right now. If you wish to use other NEAR Actions in your dApp, it's recommended to remove this wallet in your configuration.
Below are the 8 supported NEAR Actions:
export interface CreateAccountAction {
type: "CreateAccount";
}
export interface DeployContractAction {
type: "DeployContract";
params: {
code: Uint8Array;
};
}
export interface FunctionCallAction {
type: "FunctionCall";
params: {
methodName: string;
args: object;
gas: string;
deposit: string;
};
}
export interface TransferAction {
type: "Transfer";
params: {
deposit: string;
};
}
export interface StakeAction {
type: "Stake";
params: {
stake: string;
publicKey: string;
};
}
export interface AddKeyAction {
type: "AddKey";
params: {
publicKey: string;
accessKey: {
nonce?: number;
permission:
| "FullAccess"
| {
receiverId: string;
allowance?: string;
methodNames?: Array<string>;
};
};
};
}
export interface DeleteKeyAction {
type: "DeleteKey";
params: {
publicKey: string;
};
}
export interface DeleteAccountAction {
type: "DeleteAccount";
params: {
beneficiaryId: string;
};
}
Example
await selector.signAndSendTransaction({
actions: [{
type: "FunctionCall",
params: {
methodName: "addMessage",
args: { text: "Hello World!" },
gas: "30000000000000",
deposit: "10000000000000000000000",
}
}]
});
.signAndSendTransactions(params)
Parameters
params
(object
)
transactions
(Array<Transaction>
)
signerId
(string?
): Account ID used to sign the transaction. Defaults to the first account.receiverId
(string
): Account ID to receive the transaction.actions
(Array<Action>
)
type
(string
): Action type. See above for available values.params
(object?
): Parameters for the Action (if applicable).
Returns
Promise<void | Array<object>>
: Browser wallets won't return the transaction outcomes as they may need to redirect for signing. More details on this can be found here.
Description
Signs one or more transactions before sending to the network. The user must be signed in to call this method as there's at least charges for gas spent.
Note: Sender only supports "FunctionCall"
action types right now. If you wish to use other NEAR Actions in your dApp, it's recommended to remove this wallet in your configuration.
Example
await selector.signAndSendTransactions({
transactions: [{
receiverId: "guest-book.testnet",
actions: [{
type: "FunctionCall",
params: {
methodName: "addMessage",
args: { text: "Hello World!" },
gas: "30000000000000",
deposit: "10000000000000000000000",
}
}]
}]
});
License
This repository is distributed under the terms of both the MIT license and the Apache License (Version 2.0).