bitcooo
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
 
 
 
 

531 lines
16 KiB

import TokenUtil from './TokenUtil.js'
import BtcUtil from './BtcUtil.js'
import TronUtil from './TronUtil.js'
import EthUtil from './EthUtil.js'
import api from './api.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-ERC20",
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: "USDT-TRC20",
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;
walletInfo.BTC[0].ellipsisAddress = walletInfo.BTC[0].address.substring(0, 6) + '...' + walletInfo.BTC[
0].address.substring(walletInfo.BTC[0].address.length - 6,
walletInfo.BTC[0].address.length)
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);
this.updateBalance();
},
//修改钱包名称
updateWalletName: function(type, address, name) {
let walletInfo = uni.getStorageSync('walletInfo');
let walletList = walletInfo[type];
let wallet = uni.getStorageSync('wallet');
for (var k = 0, length = walletList.length; k < length; k++) {
if (walletList[k].address === address) {
walletList[k].name = name
if (wallet.address == walletList[k].address) {
wallet.name = name
uni.setStorageSync('wallet', wallet);
}
break;
}
}
uni.setStorageSync('walletInfo', walletInfo);
},
//助记词新建
mnemonicEstablishWallet: function(type, mnemonic, password) {
let walletInfo = uni.getStorageSync('walletInfo');
console.log(password, 'password', type)
let walletdec;
switch (type) {
case 'BTC':
walletdec = TokenUtil.generateBtc(mnemonic);
if (uni.getStorageSync('walletInfo')) {
uni.setStorageSync('walleti', walletdec.address)
}
break;
case 'ETH':
walletdec = TokenUtil.generateEth(mnemonic);
if (uni.getStorageSync('walletInfo')) {
uni.setStorageSync('walleti', walletdec.address)
}
break;
case 'TRX':
walletdec = TokenUtil.generateTron(mnemonic);
if (uni.getStorageSync('walletInfo')) {
uni.setStorageSync('walleti', walletdec.address)
}
break;
}
let wallet = fullWallet[type][0];
wallet.password = password;
wallet.privateKey = walletdec.privateKey;
wallet.mnemonic = mnemonic;
wallet.address = walletdec.address;
wallet.ellipsisAddress = wallet.address.substring(0, 6) + '...' + wallet.address.substring(
wallet.address.length - 6,
wallet.address.length);
console.log(wallet)
walletInfo[type].push(wallet);
console.log(walletInfo, 1111111)
uni.setStorageSync('walletInfo', walletInfo);
uni.setStorageSync('wallet', wallet);
uni.setStorageSync('walleti', wallet.address);
},
//私钥新建
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;
wallet.ellipsisAddress = wallet.address.substring(0, 6) + '...' + wallet.address.substring(
wallet.address.length - 6,
wallet.address.length);
walletInfo[type].push(wallet);
console.log(walletInfo, '当前私钥建的')
uni.setStorageSync('walletInfo', walletInfo);
uni.setStorageSync('wallet', wallet);
uni.setStorageSync('walleti', wallet.address);
},
//获取钱包列表
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(address
.length - 6,
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(
address.length - 6,
address.length);
walletInfo[obj][k].type = obj;
walletList.push(walletInfo[obj][k]);
}
}
}
return walletList;
},
selectWallet: function(type, address) {
console.log('选择钱包')
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) {
walletList[k].ellipsisAddress = address.substring(0, 6) + '...' + address.substring(address
.length - 6,
address.length)
uni.setStorageSync('wallet', walletList[k]);
break;
}
}
},
updateBalance: async function(success) {
uni.setStorageSync('isWallet', true);
const wallet = uni.getStorageSync('wallet');
let params = {
coinName: wallet.type
}
const mainCoinP = await api.getCoinRate(params);
params.coinName = 'USDT';
const usdtU = await api.getCoinRate(params);
wallet.totalBalancePrice = 0
wallet.totalBalanceCnyPrice = 0
console.log(usdtU, 8888)
switch (wallet.type) {
case 'BTC':
console.log("进来BTC")
wallet.balance = await BtcUtil.getBalance(wallet.address)
break;
case 'ETH':
console.log("进来ETH", wallet.balance)
wallet.balance = await EthUtil.getBalance(wallet.address);
wallet.balance = wallet.balance > 0 ? wallet.balance.toFixed(6) : wallet.balance
console.log("进来ETH", wallet.balance)
for (let i = 1; i < wallet.coinList.length; i++) {
wallet.coinList[i].balance = await EthUtil.getTokenBalance(wallet.address, wallet.coinList[
i].contractAddress);
console.log("进来ETH", wallet.coinList[i].balance)
}
wallet.totalBalancePrice = wallet.coinList[1].balancePrice ? wallet.coinList[1].balancePrice :
0;
wallet.totalBalanceCnyPrice = wallet.coinList[1].balanceCnyPrice ? wallet.coinList[1]
.balanceCnyPrice : 0;
wallet.coinList[1].balancePrice = (wallet
.coinList[1].balance * usdtU.data.priceInUsd).toFixed(2);
wallet.coinList[1].balanceCnyPrice = (wallet
.coinList[1].balance * usdtU.data.priceCny).toFixed(2);
if(wallet.coinList[1].name==='USDT')
{
wallet.coinList[1].name='USDT-ERC20'
}
break;
case 'TRX':
// console.log("进来TRX")
// wallet.balance = await TronUtil.getTronBalance(wallet.address);
// console.log(wallet.balance)
params.address = wallet.address;
params.chainName = wallet.type;
const tronCoin = await api.getBalance(params);
var newList = [];
newList.push(wallet.coinList[0])
newList.push(wallet.coinList[1])
newList[0].tokenType = "trc10"
newList[0].contractAddress='_';
newList[1].balance = 0;
newList[1].balanceCnyPrice = 0;
newList[1].balancePrice = 0;
for (let i = 0; i < tronCoin.data.length; i++) {
if (tronCoin.data[i].tokenAbbr === 'trx') {
wallet.balance =(Number(tronCoin.data[i].balance) / Math.pow(10, tronCoin
.data[i].tokenDecimal));
}else
if (tronCoin.data[i].tokenAbbr === 'USDT') {
if(newList[1].name==='USDT')
{
newList[1].name='USDT-TRC20'
}
newList[1].balance = (Number(tronCoin.data[i].balance) / Math.pow(10, tronCoin
.data[i].tokenDecimal));
newList[1].tokenType = "trc20"
newList[1].balanceCnyPrice = (newList[1].balance * usdtU.data.priceCny).toFixed(2);
newList[1].balancePrice = (newList[1].balance * usdtU.data.priceInUsd).toFixed(2);
wallet.totalBalancePrice = newList[1].balancePrice
wallet.totalBalanceCnyPrice = newList[1].balanceCnyPrice
} else {
console.log(Number(tronCoin.data[i].balance) / Math.pow(10, tronCoin.data[i]
.tokenDecimal))
var coin = {
name: tronCoin.data[i].tokenAbbr,
xname: tronCoin.data[i].tokenName,
balance: (Number(tronCoin.data[i].balance) / Math.pow(10, tronCoin.data[i]
.tokenDecimal)),
icon: tronCoin.data[i].tokenLogo,
contractAddress: tronCoin.data[i].tokenId,
tokenType: tronCoin.data[i].tokenType
}
newList.push(coin)
}
}
wallet.coinList = newList;
break;
}
wallet.ellipsisAddress = wallet.address.substring(0, 6) + '...' + wallet.address.substring(
wallet.address.length - 6,
wallet.address.length);
wallet.balancePrice = (wallet.balance * mainCoinP.data.priceInUsd).toFixed(2);
wallet.balanceCnyPrice = (wallet.balance * mainCoinP.data.priceCny).toFixed(2);
wallet.totalBalancePrice = (Number(wallet.totalBalancePrice) + Number(wallet.balancePrice)).toFixed(2);
wallet.totalBalanceCnyPrice = (Number(wallet.totalBalanceCnyPrice) + Number(wallet.balanceCnyPrice))
.toFixed(2);
console.log("wallet.balancePrice", Number(wallet.totalBalancePrice))
console.log("wallet.balanceCnyPrice", wallet.totalBalancePrice)
wallet.coinList[0].balance = wallet.balance
wallet.coinList[0].balancePrice = wallet.balancePrice
wallet.coinList[0].balanceCnyPrice = wallet.balanceCnyPrice
uni.setStorageSync('wallet', wallet);
const walletInfo = uni.getStorageSync('walletInfo');
for (var k = 0, length = walletInfo[wallet.type].length; k < length; k++) {
if (walletInfo[wallet.type][k].address === wallet.address) {
walletInfo[wallet.type][k] = wallet;
break;
}
}
console.log(walletInfo,123456)
uni.setStorageSync('walletInfo', walletInfo);
uni.setStorageSync('isWallet', false);
return wallet;
},
// 封装地址方法
updateAddress: function(type, value, bigAddress, addressName) {
let that = this
switch (type) {
case 'BTC':
break;
case 'ETH':
break;
case 'TRX':
if (value == 'ETH') {
const gai = uni.getStorageSync('ETHAddressInfo')
const gai2 = uni.getStorageSync('TRXAddressInfo')
const index = uni.getStorageSync('editIndex')
let eth = new Object;
eth.address = bigAddress;
eth.name = addressName
eth.coinList = [{
name: "ETH",
name2: "ETH",
xname: 'Ethereum',
icon: require('@/static/tongyonh/Frame3299.png')
}];
gai2.splice(index, 1)
uni.setStorageSync('TRXAddressInfo', gai2)
gai.push(eth);
uni.setStorageSync('ETHAddressInfo', gai)
uni.showToast({
title: 'Successfully',
icon: 'success',
duration: 1500
})
uni.navigateTo({
url: '../address/index'
})
}
break;
}
},
// 指纹识别
finger() {
// #ifdef APP-PLUS
if (!plus.fingerprint.isSupport()) {
this.result = '此设备不支持指纹识别';
this.disabled = true;
} else if (!plus.fingerprint.isKeyguardSecure()) {
this.result = '此设备未设置密码锁屏,无法使用指纹识别';
this.disabled = true;
} else if (!plus.fingerprint.isEnrolledFingerprints()) {
this.result = '此设备未录入指纹,请到设置中开启';
this.disabled = true;
} else {
this.result = '此设备支持指纹识别';
this.disabled = false;
}
// #endif
// #ifdef MP-WEIXIN
this.disabled = false;
this.result = '请在微信真机中使用,模拟器不支持';
// #endif
// #ifndef APP-PLUS || MP-WEIXIN
this.result = '此平台不支持指纹识别';
// #endif
},
fingerprint: function(sett) {
let bltype = true;
// #ifdef APP-PLUS
if (!plus.fingerprint.isSupport()) {
uni.showToast({
title: 'This device does not support fingerprint identification',
icon: 'none',
duration: 1500
})
// this.disabled = true;
} else if (!plus.fingerprint.isKeyguardSecure()) {
uni.showToast({
title: 'This device is not equipped with a password lock screen and cannot use fingerprint identification',
icon: 'none',
duration: 1500
})
} else if (!plus.fingerprint.isEnrolledFingerprints()) {
uni.showToast({
title: 'There is no fingerprint entered in this device. Please turn it on in the setting',
icon: 'none',
duration: 1500
})
// this.disabled = true;
} else {
// this.result = '此设备支持指纹识别';
// // this.disabled = false;
// #ifdef MP-WEIXIN
// this.disabled = false;
uni.showToast({
title: 'Please use it in wechat real machine. The simulator does not support it',
icon: 'none',
duration: 1500
})
// #endif
// #ifndef APP-PLUS || MP-WEIXIN
uni.showToast({
title: 'Fingerprint identification is not supported on this platform',
icon: 'none',
duration: 1500,
})
// #endif
let that = this;
let forSett = true;
// #ifdef APP-PLUS
plus.fingerprint.authenticate(function() {
plus.nativeUI.closeWaiting(); //兼容Android平台关闭等待框
// uni.showToast({
// title: 'Fingerprint identification succeeded',
// icon: 'none',
// duration: 1500,
// })
// 存储设置里开启指纹后的状态,隐藏使用密码
console.log(forSett, 12121)
if (sett) {
uni.setStorageSync('fingerPass', true)
console.log(forSett, 12121)
}
//plus.nativeUI.alert('Fingerprint identification succeeded');
}, function(e) {
switch (e.code) {
case e.AUTHENTICATE_MISMATCH:
plus.nativeUI.toast('Fingerprint matching failed, please re-enter');
break;
case e.AUTHENTICATE_OVERLIMIT:
plus.nativeUI.closeWaiting(); //兼容Android平台关闭等待框
plus.nativeUI.alert(
'The number of fingerprint identification failures exceeds the limit. Please use other methods for authentication'
);
break;
case e.CANCEL:
plus.nativeUI.toast('Recognition has been cancelled');
break;
default:
plus.nativeUI.closeWaiting(); //兼容Android平台关闭等待框
plus.nativeUI.alert('Fingerprint identification failed, please try again');
break;
}
});
// Android平台手动弹出等待提示框
if ('Android' == plus.os.name) {
this.show = true;
/* plus.nativeUI.showWaiting('指纹识别中...').onclose = function() {
plus.fingerprint.cancel();
} */
}
// #endif
// #ifdef MP-WEIXIN
wx.startSoterAuthentication({
requestAuthModes: ['fingerPrint'],
challenge: '123456',
authContent: '请用指纹解锁',
success(res) {
uni.showToast({
title: '识别成功',
mask: false,
duration: 1500
});
}
})
// #endif
}
// #endif
},
printCancel: function() {
plus.fingerprint.cancel();
// this.result="停止指纹识别"
},
}
export default WalletUtil