Interacting with the Wallet
Wallet interaction is possible only in the browser, because NEAR's Wallet is web-based.
Most frequent action is Sign In. Your user is redirected to the Wallet page to authorize your application. Once the user has Signed In, an access key is saved in browser's LocalStorage. All following actions that require the access key will be allowed. In case a user needs to authorize a transaction that has a deposit attached, your user will be automatically redirected to the Wallet again.
Creating Wallet Connection​
In Wallet connection you use a LocalStorage KeyStore
.
- TestNet
- MainNet
const { connect, keyStores, WalletConnection } = nearAPI;
const connectionConfig = {
networkId: "testnet",
keyStore: new keyStores.BrowserLocalStorageKeyStore(),
nodeUrl: "https://rpc.testnet.near.org",
walletUrl: "https://testnet.mynearwallet.com/",
helperUrl: "https://helper.testnet.near.org",
explorerUrl: "https://testnet.nearblocks.io",
};
// connect to NEAR
const nearConnection = await connect(connectionConfig);
// create wallet connection
// provide a prefix for the key
const walletConnection = new WalletConnection(nearConnection, "example-prefix");
const { connect, keyStores, WalletConnection } = nearAPI;
const connectionConfig = {
networkId: "mainnet",
keyStore: new keyStores.BrowserLocalStorageKeyStore(),
nodeUrl: "https://rpc.mainnet.near.org",
walletUrl: "https://wallet.mainnet.near.org",
helperUrl: "https://helper.mainnet.near.org",
explorerUrl: "https://nearblocks.io",
};
// connect to NEAR
const nearConnection = await connect(connectionConfig);
// create wallet connection
const walletConnection = new WalletConnection(nearConnection);
Module browserConnect
  Â
Class WalletConnection
Ask your user to Sign In​
You first create a WalletConnection, and then call requestSignIn
.
This will redirect the current page to the Wallet authentication page. You can optionally configure success and failure redirect URLs.
When you create a wallet connection you have the option to create a function call access key for a specific contract to be used by your application. This allows the app to automatically sign non-payable methods
for the user without having to sign each transaction manually in the wallet. You can also decide to specify a list of methodNames
that will restrict the key to sign only certain methods on the specified contract. Passing an empty array will allow all methods to be signed.
// const walletConnection = new WalletConnection(nearConnection);
// all parameters are optional
walletConnection.requestSignIn({
contractId: "example-contract.testnet.REPLACE_ME", // optional
methodNames: [], // optional
successUrl: "REPLACE_ME://.com/success", // optional redirect URL on success
failureUrl: "REPLACE_ME://.com/failure", // optional redirect URL on failure
});
Method WalletConnection.requestSignIn
Sign In is not required if you are using an alternative key store to local storage, or you are not signing transactions (meaning - you are only calling read-only view methods on a contract)
Sign Out your user​
// const walletConnection = new WalletConnection(nearConnection);
walletConnection.signOut();
Method WalletConnection.signOut
Check if Signed In​
// const walletConnection = new WalletConnection(nearConnection);
if (walletConnection.isSignedIn()) {
// user is signed in
}
Method WalletConnection.isSignedId
Get Wallet Account​
Get the Account your user has signed in with in the Wallet.
Get Account ID (as string)​
// const walletConnection = new WalletConnection(nearConnection);
const walletAccountId = walletConnection.getAccountId();
Method WalletConnection.getAccountId
Get Account Object​
// const walletConnection = new WalletConnection(nearConnection);
const walletAccountObj = walletConnection.account();
Method WalletConnection.account
  Â
Class ConnectedWalletAccount