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.
173 lines
4.4 KiB
173 lines
4.4 KiB
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) {
|
|
let address = tronweb.address.fromPrivateKey(privateKey);
|
|
return {
|
|
'privateKey': privateKey,
|
|
'address': address
|
|
};
|
|
},
|
|
|
|
|
|
initialWallet:function(mnemonic,password){
|
|
|
|
var btcInfo = new Object();
|
|
btcInfo.name = "BTC";
|
|
btcInfo.xname = "Bitcoin";
|
|
btcInfo.iconB=require('@/static/tongyonh/bye.png')
|
|
|
|
var ethInfo = new Object();
|
|
ethInfo.name = "ETH";
|
|
ethInfo.xname = "Ethereum";
|
|
ethInfo.iconB=require('@/static/tongyonh/Frame3299.png')
|
|
|
|
var troInfo = new Object();
|
|
troInfo.name = "TRX";
|
|
troInfo.xname = "Tron";
|
|
troInfo.iconB=require('@/static/tongyonh/tron1.png')
|
|
|
|
|
|
|
|
let btc=this.generateBtc(mnemonic);
|
|
btc.mnemonic=mnemonic;
|
|
btc.password=password;
|
|
btc.coinList=[{
|
|
name:"BTC",
|
|
xname:'Bitcoin',
|
|
icon:require('@/static/tongyonh/bye.png')
|
|
}];
|
|
let eth=this.generateEth(mnemonic);
|
|
eth.password=password;
|
|
eth.mnemonic=mnemonic;
|
|
eth.coinList=[{
|
|
name:"ETH",
|
|
xname:'Ethereum',
|
|
icon:require('@/static/tongyonh/Frame3299.png')
|
|
}];
|
|
let tron=this.generateTron(mnemonic);
|
|
tron.password=password;
|
|
tron.mnemonic=mnemonic;
|
|
tron.coinList=[{
|
|
name:"TRX",
|
|
xname:'TRON',
|
|
icon:require('@/static/tongyonh/tron1.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
|
|
|