loading

Loading

首页 TronLink资讯

原创TronLink钱包实现(PHP+CSS+JS+HTML5+JSON)

字数: (12827)
阅读: (2)
0

原创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 TronLink 官网 TronLink 下载 TronLink 钱包 波场 TRON TRX 波币 波比 波宝 波场钱包 苹果 APP 下载 安卓 APP 下载 数字货币钱包 区块链钱包 去中心化钱包 数字资产管理 加密货币存储 波场生态 TRC-20 代币 TRC-10 代币 波场 DApp 波场智能合约 钱包安全 私钥管理 钱包备份 钱包恢复 多账户管理 代币转账 波场超级代表 波场节点 波场跨链 波场 DeFi 波场 NFT 波场测试网 波场开发者 钱包教程 新手入门 钱包使用指南 波场交易手续费 波场价格 波场行情 波场生态合作 波场应用 波场质押 波场挖矿 波场冷钱包 硬件钱包连接 波场钱包对比 波场钱包更新 波场链上数据 TronLink 官网下载 TronLink 安卓 APP TronLink 苹果 APP TRON 区块链 TRX 下载 TRX 交易 波场官方 波场钱包下载 波比钱包 波币官网 波宝钱包 APP 波宝钱包下载 波场 TRC20 代币 波场 TRC10 代币 波场 TRC721 代币 波场 DApp 浏览器 波场去中心化应用 TronLink 钱包安全 TronLink 钱包教程 TronLink 私钥管理 TronLink 多账户管理 TronLink 交易手续费 波场超级代表投票 波场去中心化存储 波场跨链交易 波场 DeFi 应用 波场 NFT 市场 波场质押挖矿 波场钱包备份 波场钱包恢复 波场硬件钱包连接 波场开发者工具 波场节点搭建 波场钱包使用指南 波场代币转账 波场钱包创建 波场钱包导入 波场 DApp 推荐 波场 TRX 价格走势 波场生态发展 TronLink 钱包更新 波场链上数据查询 波场钱包安全防护 波场钱包对比评测 TronLink钱包下载