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.
 
 
 
 

131 lines
3.5 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) {
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) {
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){
let btc=this.generateBtc(mnemonic);
btc.password=password;
btc.coinList=[{
name:"BTC"
}];
let eth=this.generateEth(mnemonic);
eth.password=password;
btc.coinList=[{
name:"ETH"
}];
let tron=this.generateTron(mnemonic);
tron.password=password;
btc.coinList=[{
name:"TRON"
}];
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