Код: Выделить всё
// Handle connect button click
connectButton.addEventListener('click', async () => {
try {
// Initialize WalletConnect
const walletConnectProvider = new WalletConnect.default({
bridge: "https://bridge.walletconnect.org", // Optional: WalletConnect bridge URL
});
// Enable the provider to establish the connection
await walletConnectProvider.enable();
// Initialize Web3 with WalletConnect provider
provider = new Web3(walletConnectProvider);
// Get the accounts
accounts = await provider.eth.getAccounts();
const address = accounts[0];
walletAddressElement.innerText = "Connected: " + address;
// Toggle button visibility
connectButton.style.display = 'none';
disconnectButton.style.display = 'inline-block';
} catch (error) {
console.error("Error during connection:", error);
alert("Connection failed. Please try again.");
}
});
document.addEventListener('deviceready', function () {
console.log("Device is ready!");
const connectButton = document.getElementById('connectButton');
const walletAddressElement = document.getElementById('walletAddress');
function openMetaMask() {
const metamaskUrl = "https://metamask.app.link/wc"; // Opens MetaMask Mobile
console.log("Opening MetaMask:", metamaskUrl);
window.open(metamaskUrl, '_system'); // Opens MetaMask app
}
async function connectWallet() {
try {
if (!window.ethereum) {
alert("MetaMask is not installed. Please install it first.");
return;
}
const accounts = await ethereum.request({ method: 'eth_requestAccounts' });
const account = accounts[0];
console.log("Connected account:", account);
walletAddressElement.innerText = "Wallet: " + account;
// Now, request a signature
const message = "Sign this message to authenticate with My Cordova App";
const signature = await ethereum.request({
method: 'personal_sign',
params: [message, account]
});
console.log("Signature:", signature);
alert("Signed successfully! Returning to app...");
} catch (error) {
console.error("Error:", error);
alert("Connection or signing failed.");
}
}
connectButton.addEventListener('click', function () {
openMetaMask();
setTimeout(connectWallet, 5000); // Wait for MetaMask to open before requesting connection
});
< /code>
Есть ли у вас какой -либо другой подход, я использую только CDN и хочу быть таким, есть ли альтернатива без использования API Infura или повторного идентификатора проекта? Единственное, что мне нужно, - это подключиться, нет ничего другого, без транзакций, ничто не только подписать одобрение, а затем вернуться к приложению.
Подробнее здесь: https://stackoverflow.com/questions/794 ... ing-javasc
Мобильная версия