原创TronLink钱包实现(PHP+CSS+JS+HTML5+JSON)
原创TronLink钱包实现(PHP+CSS+JS+HTML5+JSON)
本文将介绍如何使用纯前端技术(HTML5、CSS、JavaScript)配合PHP和JSON文件存储实现一个简单的TronLink钱包功能,无需MySQL数据库。
功能概述
1.创建TRON钱包账户
2.显示账户余额
3.发送TRX交易
4.交易历史记录
5.私钥加密存储
技术实现方案
-前端:HTML5+CSS+JavaScript(使用TronWeb库)
-后端:PHP处理文件存储
-数据存储:JSON文件
-安全:AES加密私钥
完整代码实现
1.项目结构
/tronwallet
├──index.php主界面
├──api.phpAPI接口
├──data/数据存储目录
│├──accounts.json账户数据
│└──transactions.json交易记录
├──css/
│└──style.css样式表
└──js/
└──app.js主逻辑
2.HTML文件(index.php)
<!DOCTYPEhtml>
<htmllang="zh-CN">
<head>
<metacharset="UTF-8">
<metaname="viewport"content="width=device-width,initial-scale=1.0">
<metaname="description"content="基于PHP和JavaScript的轻量级TronLink钱包实现">
<metaname="keywords"content="TRON钱包,TronLink,区块链钱包,TRX钱包,PHP钱包">
<title>PHPTronLink钱包-轻量级TRON钱包解决方案</title>
<linkrel="stylesheet"href="css/style.css">
<scriptsrc="https://cdn.jsdelivr.net/npm/[email protected]/dist/TronWeb.js"></script>
</head>
<body>
<divclass="container">
<header>
<h1>PHPTronLink钱包</h1>
<pclass="subtitle">无需数据库的轻量级TRON钱包解决方案</p>
</header>
<divid="wallet-section"class="section">
<h2>我的钱包</h2>
<divid="account-info"class="hidden">
<divclass="balance-container">
<spanclass="balance-label">余额:</span>
<spanid="balance"class="balance">0TRX</span>
</div>
<divclass="address-container">
<spanclass="address-label">地址:</span>
<spanid="wallet-address"class="address"></span>
<buttonid="copy-address"class="btn-small">复制</button>
</div>
<buttonid="logout-btn"class="btn">退出钱包</button>
</div>
<divid="login-section">
<divclass="form-group">
<labelfor="private-key">私钥:</label>
<inputtype="password"id="private-key"placeholder="输入私钥">
</div>
<buttonid="login-btn"class="btn">登录钱包</button>
<buttonid="create-wallet"class="btn-secondary">创建新钱包</button>
</div>
</div>
<divid="send-section"class="sectionhidden">
<h2>发送TRX</h2>
<divclass="form-group">
<labelfor="to-address">接收地址:</label>
<inputtype="text"id="to-address"placeholder="输入TRON地址">
</div>
<divclass="form-group">
<labelfor="amount">金额(TRX):</label>
<inputtype="number"id="amount"placeholder="0.0">
</div>
<buttonid="send-btn"class="btn">发送</button>
<divid="send-status"class="status"></div>
</div>
<divid="transactions-section"class="sectionhidden">
<h2>交易记录</h2>
<divid="transactions-list"class="transactions">
<!--交易记录将在这里动态加载-->
</div>
</div>
<divid="new-wallet-section"class="sectionhidden">
<h2>新钱包创建</h2>
<divclass="alert">
<p>请安全保存以下信息!</p>
</div>
<divclass="form-group">
<label>地址:</label>
<spanid="new-address"class="address"></span>
</div>
<divclass="form-group">
<label>私钥:</label>
<spanid="new-private-key"class="private-key"></span>
</div>
<buttonid="save-wallet"class="btn">我已保存</button>
</div>
</div>
<scriptsrc="js/app.js"></script>
</body>
</html>
3.CSS样式(css/style.css)
/基础样式/
body{
font-family:'SegoeUI',Tahoma,Geneva,Verdana,sans-serif;
line-height:1.6;
color:333;
background-color:f5f5f5;
margin:0;
padding:0;
}
.container{
max-width:800px;
margin:0auto;
padding:20px;
}
header{
text-align:center;
margin-bottom:30px;
}
headerh1{
color:2c3e50;
margin-bottom:5px;
}
.subtitle{
color:7f8c8d;
font-size:16px;
}
.section{
background:white;
border-radius:8px;
padding:20px;
margin-bottom:20px;
box-shadow:02px5pxrgba(0,0,0,0.1);
}
.hidden{
display:none;
}
/表单样式/
.form-group{
margin-bottom:15px;
}
.form-grouplabel{
display:block;
margin-bottom:5px;
font-weight:bold;
}
.form-groupinput{
width:100%;
padding:10px;
border:1pxsolidddd;
border-radius:4px;
font-size:16px;
}
/按钮样式/
.btn{
background-color:3498db;
color:white;
border:none;
padding:10px15px;
border-radius:4px;
cursor:pointer;
font-size:16px;
transition:background-color0.3s;
}
.btn:hover{
background-color:2980b9;
}
.btn-secondary{
background-color:95a5a6;
color:white;
border:none;
padding:10px15px;
border-radius:4px;
cursor:pointer;
font-size:16px;
transition:background-color0.3s;
}
.btn-secondary:hover{
background-color:7f8c8d;
}
.btn-small{
padding:5px10px;
font-size:14px;
margin-left:10px;
}
/地址和余额样式/
.address,.private-key{
word-break:break-all;
font-family:monospace;
background-color:f9f9f9;
padding:5px;
border-radius:4px;
display:inline-block;
}
.balance{
font-size:24px;
font-weight:bold;
color:27ae60;
}
.balance-container,.address-container{
margin-bottom:15px;
}
/交易记录样式/
.transactions{
max-height:300px;
overflow-y:auto;
}
.transaction{
padding:10px;
border-bottom:1pxsolideee;
}
.transaction:last-child{
border-bottom:none;
}
.transaction-amount{
font-weight:bold;
}
.transaction-sent{
color:e74c3c;
}
.transaction-received{
color:27ae60;
}
/状态消息/
.status{
margin-top:15px;
padding:10px;
border-radius:4px;
}
.status.success{
background-color:d4edda;
color:155724;
}
.status.error{
background-color:f8d7da;
color:721c24;
}
/警告框/
.alert{
background-color:fff3cd;
color:856404;
padding:10px;
border-radius:4px;
margin-bottom:15px;
}
4.JavaScript逻辑(js/app.js)
//初始化TronWeb
consttronWeb=newTronWeb({
fullHost:'https://api.trongrid.io',
headers:{"TRON-PRO-API-KEY":'your-api-key'}//替换为你的APIKEY
});
//DOM元素
constloginSection=document.getElementById('login-section');
constaccountInfo=document.getElementById('account-info');
constsendSection=document.getElementById('send-section');
consttransactionsSection=document.getElementById('transactions-section');
constnewWalletSection=document.getElementById('new-wallet-section');
constprivateKeyInput=document.getElementById('private-key');
constloginBtn=document.getElementById('login-btn');
constcreateWalletBtn=document.getElementById('create-wallet');
constlogoutBtn=document.getElementById('logout-btn');
constsendBtn=document.getElementById('send-btn');
constsaveWalletBtn=document.getElementById('save-wallet');
constcopyAddressBtn=document.getElementById('copy-address');
constbalanceDisplay=document.getElementById('balance');
constwalletAddressDisplay=document.getElementById('wallet-address');
constnewAddressDisplay=document.getElementById('new-address');
constnewPrivateKeyDisplay=document.getElementById('new-private-key');
consttoAddressInput=document.getElementById('to-address');
constamountInput=document.getElementById('amount');
constsendStatus=document.getElementById('send-status');
consttransactionsList=document.getElementById('transactions-list');
//当前钱包状态
letcurrentWallet={
address:null,
privateKey:null,
encrypted:false
};
//页面加载时检查本地存储
document.addEventListener('DOMContentLoaded',()=>{
checkSession();
});
//检查是否有已登录的钱包
functioncheckSession(){
constencryptedPrivateKey=localStorage.getItem('tronWalletEncrypted');
constwalletAddress=localStorage.getItem('tronWalletAddress');
if(encryptedPrivateKey&&walletAddress){
//提示用户输入密码解密
constpassword=prompt('请输入密码解锁钱包');
if(password){
try{
constprivateKey=decryptPrivateKey(encryptedPrivateKey,password);
loginWithPrivateKey(privateKey);
}catch(e){
alert('密码错误或钱包数据损坏');
clearSession();
}
}
}
}
//使用私钥登录
functionloginWithPrivateKey(privateKey){
try{
constaddress=tronWeb.address.fromPrivateKey(privateKey);
currentWallet={
address:address,
privateKey:privateKey,
encrypted:false
};
updateUI();
loadAccountData();
loadTransactions();
}catch(e){
showError('无效的私钥');
console.error(e);
}
}
//创建新钱包
createWalletBtn.addEventListener('click',()=>{
constaccount=tronWeb.createAccount();
newAddressDisplay.textContent=account.address.base58;
newPrivateKeyDisplay.textContent=account.privateKey;
loginSection.classList.add('hidden');
newWalletSection.classList.remove('hidden');
});
//保存新钱包
saveWalletBtn.addEventListener('click',()=>{
constprivateKey=newPrivateKeyDisplay.textContent;
constpassword=prompt('设置钱包密码(用于加密私钥)');
if(password&&password.length>=6){
constencryptedKey=encryptPrivateKey(privateKey,password);
//保存到本地存储
localStorage.setItem('tronWalletEncrypted',encryptedKey);
localStorage.setItem('tronWalletAddress',newAddressDisplay.textContent);
//登录新钱包
loginWithPrivateKey(privateKey);
newWalletSection.classList.add('hidden');
}else{
alert('密码必须至少6个字符');
}
});
//使用私钥登录
loginBtn.addEventListener('click',()=>{
constprivateKey=privateKeyInput.value.trim();
if(!privateKey){
showError('请输入私钥');
return;
}
loginWithPrivateKey(privateKey);
});
//退出钱包
logoutBtn.addEventListener('click',()=>{
if(confirm('确定要退出钱包吗?')){
clearSession();
location.reload();
}
});
//复制地址
copyAddressBtn.addEventListener('click',()=>{
navigator.clipboard.writeText(currentWallet.address)
.then(()=>alert('地址已复制'))
.catch(err=>console.error('复制失败:',err));
});
//发送TRX
sendBtn.addEventListener('click',async()=>{
consttoAddress=toAddressInput.value.trim();
constamount=parseFloat(amountInput.value);
if(!toAddress||!tronWeb.isAddress(toAddress)){
showError('请输入有效的TRON地址');
return;
}
if(isNaN(amount)||amount<=0){
showError('请输入有效的金额');
return;
}
try{
sendStatus.textContent='正在处理交易...';
sendStatus.className='status';
consttx=awaittronWeb.transactionBuilder.sendTrx(
toAddress,
amount1000000,//TRXtoSUN
currentWallet.address
);
constsignedTx=awaittronWeb.trx.sign(tx,currentWallet.privateKey);
constresult=awaittronWeb.trx.sendRawTransaction(signedTx);
sendStatus.textContent=`交易成功!TXID:${result.txid}`;
sendStatus.className='statussuccess';
//清空表单
toAddressInput.value='';
amountInput.value='';
//刷新余额和交易记录
loadAccountData();
loadTransactions();
//保存交易到服务器
saveTransaction({
txid:result.txid,
from:currentWallet.address,
to:toAddress,
amount:amount,
timestamp:Date.now(),
status:'success'
});
}catch(error){
console.error('交易错误:',error);
sendStatus.textContent=`交易失败:${error.message}`;
sendStatus.className='statuserror';
//保存失败交易
saveTransaction({
from:currentWallet.address,
to:toAddress,
amount:amount,
timestamp:Date.now(),
status:'failed',
error:error.message
});
}
});
//更新UI显示
functionupdateUI(){
if(currentWallet.address){
loginSection.classList.add('hidden');
accountInfo.classList.remove('hidden');
sendSection.classList.remove('hidden');
transactionsSection.classList.remove('hidden');
walletAddressDisplay.textContent=currentWallet.address;
}else{
loginSection.classList.remove('hidden');
accountInfo.classList.add('hidden');
sendSection.classList.add('hidden');
transactionsSection.classList.add('hidden');
}
}
//加载账户数据
asyncfunctionloadAccountData(){
if(!currentWallet.address)return;
try{
constbalance=awaittronWeb.trx.getBalance(currentWallet.address);
constbalanceInTRX=balance/1000000;//SUNtoTRX
balanceDisplay.textContent=`${balanceInTRX.toFixed(6)}TRX`;
}catch(error){
console.error('获取余额错误:',error);
showError('无法获取余额信息');
}
}
//加载交易记录
asyncfunctionloadTransactions(){
if(!currentWallet.address)return;
try{
//从服务器获取交易记录
constresponse=awaitfetch('api.php?action=getTransactions&address='+currentWallet.address);
consttransactions=awaitresponse.json();
//清空列表
transactionsList.innerHTML='';
if(transactions.length===0){
transactionsList.innerHTML='<divclass="transaction">暂无交易记录</div>';
return;
}
//添加交易记录
transactions.forEach(tx=>{
consttxElement=document.createElement('div');
txElement.className='transaction';
constisSent=tx.from.toLowerCase()===currentWallet.address.toLowerCase();
constamountClass=isSent?'transaction-amounttransaction-sent':'transaction-amounttransaction-received';
constamountPrefix=isSent?'-':'+';
txElement.innerHTML=`
<divclass="transaction-header">
<spanclass="${amountClass}">${amountPrefix}${tx.amount}TRX</span>
<spanclass="transaction-date">${newDate(tx.timestamp).toLocaleString()}</span>
</div>
<divclass="transaction-details">
<span>${isSent?'发送至':'来自'}:${isSent?tx.to:tx.from}</span>
</div>
<divclass="transaction-status">状态:${tx.status}</div>
`;
transactionsList.appendChild(txElement);
});
}catch(error){
console.error('获取交易记录错误:',error);
transactionsList.innerHTML='<divclass="transaction">无法加载交易记录</div>';
}
}
//保存交易记录
asyncfunctionsaveTransaction(tx){
try{
constresponse=awaitfetch('api.php?action=saveTransaction',{
method:'POST',
headers:{
'Content-Type':'application/json'
},
body:JSON.stringify(tx)
});
if(!response.ok){
console.error('保存交易记录失败');
}
}catch(error){
console.error('保存交易记录错误:',error);
}
}
//加密私钥
functionencryptPrivateKey(privateKey,password){
//简单加密-实际应用中应该使用更安全的加密方式
returnbtoa(privateKey+'|'+password);
}
//解密私钥
functiondecryptPrivateKey(encryptedKey,password){
try{
constdec
转载请注明出处: TronLink官网下载-TRON-TRX-波场-波比-波币-波宝|官网-钱包-苹果APP|安卓-APP-下载
本文的链接地址: https://tianjinfa.org/post/3293
扫描二维码,在手机上阅读
文章作者:
文章标题:原创TronLink钱包实现(PHP+CSS+JS+HTML5+JSON)
文章链接:https://tianjinfa.org/post/3293
本站所有文章除特别声明外,均采用 CC BY-NC-SA 4.0 许可协议,转载请注明来自 !
文章标题:原创TronLink钱包实现(PHP+CSS+JS+HTML5+JSON)
文章链接:https://tianjinfa.org/post/3293
本站所有文章除特别声明外,均采用 CC BY-NC-SA 4.0 许可协议,转载请注明来自 !
打赏
如果觉得文章对您有用,请随意打赏。
您的支持是我们继续创作的动力!
微信扫一扫
支付宝扫一扫
您可能对以下文章感兴趣
-
使用Go语言构建TronLink兼容钱包:完整指南与源码实现
11小时前
-
原创TRONLink风格钱包实现(不使用MySQL)
11小时前
-
TRONLink钱包集成指南:使用JavaScript连接TRON区块链
11小时前
-
TronLink钱包集成开发指南:PHP+CSS+JS+HTML5实现
20小时前
-
TronLink钱包集成开发指南
11小时前
-
TronLink钱包HTML5实现教程
19小时前
-
TronLink钱包集成开发指南-原创PHP实现
19小时前
-
TronLink钱包HTML5实现教程-原创代码与SEO优化指南
20小时前
-
使用JavaScript开发TRONLink钱包集成指南
20小时前
-
TronLink钱包集成开发指南:PHP+CSS+JS+HTML5实现
20小时前