const tronweb = require('tronweb') const bip39 = require('bip39'); const bip32 = require('bip32'); const util = require('ethereumjs-util') const ethers = require('ethers') const bitcoin = require('bitcoinjs-lib') let Tx = require('ethereumjs-tx'); let token = { // 获取助记词 generateMnemonic: function() { return bip39.generateMnemonic(); }, //获取Child getPrivateKey: function(mnemonic, hdpath) { if(mnemonic.split(" ").length%12>0){ throw new Error("mnemonic error"); } let seed = bip39.mnemonicToSeedSync(mnemonic); const node = bip32.fromSeed(seed); const child = node.derivePath(hdpath); let privateKey = util.bufferToHex(child.privateKey); return privateKey; }, //根据助记词生成以太坊ETH的钱包信息 generateEth: function(mnemonic) { let privateKey = this.getPrivateKey(mnemonic, "m/44'/60'/0'/0/0"); let wallet = new ethers.Wallet(privateKey); let address = wallet.address; return { 'privateKey': privateKey, 'address': address }; }, //根据助记词生成波场TRON的钱包信息 generateTron: function(mnemonic) { let privateKey = this.getPrivateKey(mnemonic, "m/44'/195'/0'/0/0"); privateKey = privateKey.replace('0x', ''); let address = tronweb.address.fromPrivateKey(privateKey); return { 'privateKey': privateKey, 'address': address }; }, //根据助记词生成比特币BTC钱包信息 generateBtc: function(mnemonic) { if(mnemonic.split(" ").length%12>0){ throw new Error("mnemonic error"); } let seed = bip39.mnemonicToSeedSync(mnemonic); const node = bip32.fromSeed(seed); const keyPair = node.derivePath("m/44'/195'/0'/0/0"); const privateKey = keyPair.toWIF(); console.log("BTC私钥:", privateKey) let address = bitcoin.payments.p2sh({redeem: bitcoin.payments.p2wpkh({ pubkey: keyPair.publicKey })}); //bitcoin.payments.p2pkh({ pubkey: child.publicKey }) //console.log(address.fromScriptHash({pubkey: keyPair.publicKey})) return { 'privateKey': privateKey, 'address': address.address }; }, //导入比特币BTC私钥 importBtcPrivateKey: function(privateKey) { let keyPair = new bitcoin.ECPair.fromWIF(privateKey); let address = bitcoin.payments.p2sh({redeem: bitcoin.payments.p2wpkh({ pubkey: keyPair.publicKey })}); //console.log(address.fromScriptHash({pubkey: keyPair.publicKey})) return { 'privateKey': privateKey, 'address': address.address }; }, //导入以太坊ETH私钥 importEthPrivateKey: function(privateKey) { let wallet = new ethers.Wallet(privateKey); let address = wallet.address; return { 'privateKey': privateKey, 'address': address }; }, //导入波场TRON私钥 importTronPrivateKey: function(privateKey) { privateKey = privateKey.replace('0x', ''); let address = tronweb.address.fromPrivateKey(privateKey); return { 'privateKey': privateKey, 'address': address }; }, initialWallet:function(mnemonic,password){ let btc=this.generateBtc(mnemonic); btc.mnemonic=mnemonic; btc.password=password; btc.coinList=[{ name:"BTC", name2:"BTC", xname:'Bitcoin', icon:require('@/static/tongyonh/bye.png') }]; let eth=this.generateEth(mnemonic); eth.password=password; eth.mnemonic=mnemonic; eth.coinList=[{ name:"ETH", name2:"ETH", xname:'Ethereum', icon:require('@/static/tongyonh/Frame3299.png') }, { name:"USDT", name2:"ERC20-USDT", xname:'Tether USD', contractAddress:'0xdac17f958d2ee523a2206206994597c13d831ec7', icon:require('@/static/tongyonh/img500.png') } ]; console.log(eth.coinList,4444) console.log(eth,2224) let tron=this.generateTron(mnemonic); tron.password=password; tron.mnemonic=mnemonic; tron.coinList=[ { name:"TRX", name2:"TRX", xname:'TRON', icon:require('@/static/tongyonh/tron1.png') }, { name:"USDT", name2:"TRC20-USDT", xname:'Tether USD', contractAddress:'TR7NHqjeKQxGTCi8q8ZY4pL8otSzgjLj6t', icon:require('@/static/tongyonh/tether_usd.png') }, ]; let wallrtInfo={ "BTC":[ btc ], "ETH":[ eth ], "TRON":[ tron ] } return wallrtInfo; }, creatingWallets: function() { let mnemonic = this.generateMnemonic(); let eth = this.generateEth(mnemonic); let tron = this.generateTron(mnemonic); let btc = this.generateBtc(mnemonic); // let privateKey = this.getPrivateKey(mnemonic); return { 'eth': eth, 'tron': tron, 'btc': btc, 'mnemonic': mnemonic, // 'privateKey': privateKey }; }, } export default token