import TokenUtil from './TokenUtil.js' import BtcUtil from './BtcUtil.js' import BtcUtil from './BtcUtil.js' import TronUtil from './TronUtil.js' import EthUtil from './EthUtil.js' let fullWallet = { "BTC": [{ balance: 0, name: "BTC", type:'BTC', coinList: [{ name: "BTC", xname: 'Bitcoin', balance: 0, icon: require('@/static/tongyonh/bye.png') }] }], "ETH": [{ balance: 0, name: "ETH", type:'ETH', coinList: [{ name: "ETH", xname: 'Ethereum', balance: 0, icon: require('@/static/tongyonh/Frame3299.png') }, { name: "USDT", xname: 'Tether USD', balance: 0, contractAddress: '0xdac17f958d2ee523a2206206994597c13d831ec7', icon: require('@/static/tongyonh/img500.png') } ] }], "TRX": [{ balance: 0, name: "TRX", type:'TRX', coinList: [{ name: "TRX", xname: 'TRON', balance: 0, icon: require('@/static/tongyonh/tron1.png') }, { name: "TRC20-USDT", xname: 'Tether USD', balance: 0, contractAddress: 'TR7NHqjeKQxGTCi8q8ZY4pL8otSzgjLj6t', icon: require('@/static/tongyonh/tether_usd.png') } ] }] } let WalletUtil = { //初次创建钱包 initialWallet: function(mnemonic, password) { console.log(password,'password') let walletInfo = fullWallet; let btc = TokenUtil.generateBtc(mnemonic); walletInfo.BTC[0].password = password; walletInfo.BTC[0].mnemonic = mnemonic; walletInfo.BTC[0].privateKey = btc.privateKey; walletInfo.BTC[0].address = btc.address; let eth = TokenUtil.generateEth(mnemonic) walletInfo.ETH[0].password = password; walletInfo.ETH[0].mnemonic = mnemonic; walletInfo.ETH[0].privateKey = eth.privateKey; walletInfo.ETH[0].address = eth.address; let tron = TokenUtil.generateTron(mnemonic); walletInfo.TRX[0].mnemonic = mnemonic; walletInfo.TRX[0].password = password; walletInfo.TRX[0].privateKey = tron.privateKey; walletInfo.TRX[0].address = tron.address; let wallet = walletInfo.BTC[0]; uni.setStorageSync('walletInfo', walletInfo); uni.setStorageSync('wallet', wallet); }, //修改钱包名称 updateWalletName: function(type, address, name) { let walletInfo = uni.getStorageSync('walletInfo'); let walletList = walletInfo[type]; for (var k = 0, length = walletList.length; k < length; k++) { if (walletList[k].address === address) { walletList[k].name = name break; } } uni.setStorageSync('walletInfo', walletInfo); }, //助记词新建 mnemonicEstablishWallet: function(type, mnemonic, password) { let walletInfo = uni.getStorageSync('walletInfo'); console.log(password,'password') let walletdec; switch (type) { case 'BTC': walletdec = TokenUtil.generateBtc(mnemonic); break; case 'ETH': walletdec = TokenUtil.generateTron(mnemonic); break; case 'TRX': walletdec = TokenUtil.generateEth(mnemonic); break; } let wallet = fullWallet[type][0]; wallet.password = password; wallet.privateKey = walletdec.privateKey; wallet.mnemonic = mnemonic; wallet.address = walletdec.address; console.log(wallet) walletInfo[type].push(wallet); console.log(walletInfo, 1111111) uni.setStorageSync('walletInfo', walletInfo); uni.setStorageSync('wallet', wallet); }, //私钥新建 privateKeyEstablishWallet: function(type, privateKey, password) { let walletInfo = uni.getStorageSync('walletInfo'); console.log(type) console.log(password,'password') let walletdec; switch (type) { case 'BTC': walletdec = TokenUtil.importBtcPrivateKey(privateKey); break; case 'ETH': walletdec = TokenUtil.importEthPrivateKey(privateKey); break; case 'TRX': walletdec = TokenUtil.importTronPrivateKey(privateKey); break; } let wallet = fullWallet[type][0]; wallet.password = password; wallet.privateKey = walletdec.privateKey; wallet.address = walletdec.address; walletInfo.push(wallet); uni.setStorageSync('walletInfo', walletInfo); uni.setStorageSync('wallet', wallet); }, //获取钱包列表 getWalletList: function(type) { let walletInfo = uni.getStorageSync('walletInfo'); let walletList = []; if (type) { walletList = walletInfo[type] for (var k = 0, length = walletList.length; k < length; k++) { let address = walletList[k].address; walletList[k].ellipsisAddress = address.substring(0, 6) + '...' + address.substring(25, address.length) } } else { for (let obj in walletInfo) { for (var k = 0, length = walletInfo[obj].length; k < length; k++) { let address = walletInfo[obj][k].address; walletInfo[obj][k].ellipsisAddress = address.substring(0, 6) + '...' + address.substring(25, address.length); walletInfo[obj][k].type = obj; walletList.push(walletInfo[obj][k]); } } } return walletList; }, selectWallet: function(type, address) { uni.setStorageSync('walleti', address); let walletInfo = uni.getStorageSync('walletInfo'); let walletList = walletInfo[type]; console.log() for (var k = 0, length = walletList.length; k < length; k++) { if (walletList[k].address === address) { uni.setStorageSync('wallet', walletList[k]); break; } } this.updateBalance(); }, updateBalance:function() { let wallet = uni.getStorageSync('wallet'); switch (wallet.type) { case 'BTC': BtcUtil.getBalance(wallet.address,function(balance){ wallet.balance=balance; wallet.coinList[0].balance=balance }) break; case 'ETH': EthUtil.getBalance(wallet.address,function(balance){ wallet.balance=balance; wallet.coinList[0].balance=balance for(let i=1;i { wallet.balance=res; wallet.coinList[0].balance=res for(let i=1;i { wallet.coinList[i].balance=res; }) } }).catch(err => { console.log(err) }) break; uni.setStorageSync('wallet', wallet); } } } export default WalletUtil