-
Notifications
You must be signed in to change notification settings - Fork 392
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
* fix: log * fix: ext dapp account bug (cherry picked from commit d8f050db1b3457ca0e273430abe880be0c1753c2) * fix: lint * fix: network change save to db
- Loading branch information
1 parent
3cd6641
commit 7ef94b3
Showing
10 changed files
with
268 additions
and
39 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,75 @@ | ||
<!DOCTYPE html> | ||
<html> | ||
<head> | ||
<title>ETH DApp Demo</title> | ||
</head> | ||
<body> | ||
<div> | ||
<input type="checkbox" id="autoSign" checked> | ||
<label for="autoSign">连接后自动签名</label> | ||
</div> | ||
<button id="connectBtn">Connect Wallet</button> | ||
<button id="signBtn">Sign Message</button> | ||
<p id="status"></p> | ||
|
||
<script> | ||
let account = null; | ||
const connectBtn = document.getElementById('connectBtn'); | ||
const signBtn = document.getElementById('signBtn'); | ||
const statusText = document.getElementById('status'); | ||
const autoSignCheckbox = document.getElementById('autoSign'); | ||
|
||
// 页面加载时从 localStorage 读取状态 | ||
const savedAutoSign = localStorage.getItem('autoSign'); | ||
if (savedAutoSign !== null) { | ||
autoSignCheckbox.checked = savedAutoSign === 'true'; | ||
} | ||
|
||
// 监听复选框变化,保存到 localStorage | ||
autoSignCheckbox.addEventListener('change', (event) => { | ||
localStorage.setItem('autoSign', event.target.checked); | ||
}); | ||
|
||
async function connectWallet() { | ||
try { | ||
// 检查是否安装了 MetaMask | ||
if (typeof window.ethereum === 'undefined') { | ||
throw new Error('请安装 MetaMask!'); | ||
} | ||
|
||
// 请求连接钱包 | ||
const accounts = await window.ethereum.request({ | ||
method: 'eth_requestAccounts', | ||
}); | ||
|
||
account = accounts[0]; | ||
statusText.textContent = `已连接: ${account}`; | ||
|
||
// 根据勾选状态决定是否自动签名 | ||
if (autoSignCheckbox.checked) { | ||
await requestSign(); | ||
} | ||
} catch (err) { | ||
statusText.textContent = `错误: ${err.message}`; | ||
} | ||
} | ||
|
||
async function requestSign() { | ||
try { | ||
const message = '请签名以验证您的身份776655'; | ||
const signature = await window.ethereum.request({ | ||
method: 'personal_sign', | ||
params: [message, account], | ||
}); | ||
|
||
statusText.textContent += `\n签名成功: ${signature}`; | ||
} catch (err) { | ||
statusText.textContent += `\n签名失败: ${err.message}`; | ||
} | ||
} | ||
|
||
connectBtn.addEventListener('click', connectWallet); | ||
signBtn.addEventListener('click', requestSign); | ||
</script> | ||
</body> | ||
</html> |
Oops, something went wrong.