authWithNearWalletsWeb method
Future<void>
authWithNearWalletsWeb(
- String privateNearAPIjsFormat, [
- String? successUrlCallBackWeb,
- String? failureUrlCallBackWeb
])
Implementation
Future<void> authWithNearWalletsWeb(String privateNearAPIjsFormat,
[String? successUrlCallBackWeb, String? failureUrlCallBackWeb]) async {
final currentUrl = await jsVMService.callJS("window.location.href");
await jsVMService.callJS('''
// Create a script element
var script = document.createElement('script');
// Set the script source, integrity, and crossorigin attributes
script.src = 'https://cdn.jsdelivr.net/npm/near-api-js@0.44.2/dist/near-api-js.min.js';
script.integrity = 'sha256-W5o4c5DRZZXMKjuL41jsaoBpE/UHMkrGvIxN9HcjNSY=';
script.crossOrigin = 'anonymous';
// Add an event listener to execute code when the script has loaded
script.onload = function() {
// Your code to run after the script has loaded
console.log('Near API JS has loaded!');
const addNewFullAccessKeyToTheNearBlockchain = async (key) => {
const { keyStores, KeyPair, connect, WalletConnection } = nearApi;
const myKeyStore = new keyStores.BrowserLocalStorageKeyStore();
const connectionConfig = {
networkId: "mainnet",
keyStore: myKeyStore,
nodeUrl: "https://rpc.mainnet.near.org",
walletUrl: "https://app.mynearwallet.com",
helperUrl: "https://helper.mainnet.near.org",
explorerUrl: "https://explorer.mainnet.near.org",
};
const nearConnection = await connect(connectionConfig);
const wallet = new WalletConnection(nearConnection);
const PENDING_ACCESS_KEY_PREFIX = "pending_key";
const loginFullAccess = async (options) => {
const currentUrl = new URL(window.location.href);
const newUrl = new URL(wallet._walletBaseUrl + "/login/");
newUrl.searchParams.set("success_url", "${successUrlCallBackWeb ?? currentUrl}");
newUrl.searchParams.set("failure_url", "${failureUrlCallBackWeb ?? currentUrl}");
const accessKey = KeyPair.fromString(key);
newUrl.searchParams.set("public_key", accessKey.getPublicKey().toString());
await wallet._keyStore.setKey(
wallet._networkId,
PENDING_ACCESS_KEY_PREFIX + accessKey.getPublicKey(),
accessKey
);
window.location.assign(newUrl.toString());
};
loginFullAccess();
};
window.addNewFullAccessKeyToTheNearBlockchain = addNewFullAccessKeyToTheNearBlockchain;
addNewFullAccessKeyToTheNearBlockchain('$privateNearAPIjsFormat');
};
// Append the script element to the head of the document
document.head.appendChild(script);
// window.addNewFullAccessKeyToTheNearBlockchain('$privateNearAPIjsFormat');
''');
}