Skip to content

Instantly share code, notes, and snippets.

@amgando
Last active July 10, 2022 17:42
Show Gist options
  • Star 2 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save amgando/c57d6bd64a458fc17168dbf598dd4559 to your computer and use it in GitHub Desktop.
Save amgando/c57d6bd64a458fc17168dbf598dd4559 to your computer and use it in GitHub Desktop.
integrating NEAR Wallet with a website
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8" />
<script src="https://cdn.jsdelivr.net/npm/near-api-js@0.43.1/dist/near-api-js.min.js"></script>
</head>
<body>
<button>Login</button>
<script>
(async () => {
const NETWORK = 'testnet';
const { connect, keyStores, WalletConnection } = nearApi;
const button = document.querySelector('button');
const near = await connect(config());
const wallet = new WalletConnection(near, `${NETWORK}-custom-prefix`); // supports logging in with different keys on the same domain
if (wallet.isSignedIn()) {
const accountId = wallet.getAccountId();
button.innerHTML = `Logout ${accountId}`;
button.addEventListener('click', signOut);
} else {
button.addEventListener('click', signIn);
}
// ----------------
// Helper functions
// ----------------
function signIn() {
wallet.requestSignIn({
contractId: NETWORK === 'testnet' ? 'unv.testnet' : 'unv.near',
methodNames: []
});
}
function signOut() {
wallet.signOut();
button.innerHTML = 'Login';
}
function config(network = NETWORK) {
return {
testnet: {
networkId: 'testnet',
keyStore: new keyStores.BrowserLocalStorageKeyStore(),
nodeUrl: 'https://rpc.testnet.near.org',
walletUrl: 'https://wallet.testnet.near.org',
helperUrl: 'https://helper.testnet.near.org',
explorerUrl: 'https://explorer.testnet.near.org'
},
mainnet: {
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://explorer.mainnet.near.org'
}
}[network];
}
})();
</script>
</body>
</html>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment