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.
 
 
 
 

257 lines
6.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');
const WAValidator = require('wallet-address-validator');
const sha256= require('js-sha256').sha256;
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': wallet.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.balance = 0;
btc.coinList = [{
name: "BTC",
name2: "BTC",
xname: 'Bitcoin',
balance: 0,
icon: require('@/static/tongyonh/bye.png')
}];
let eth = this.generateEth(mnemonic);
eth.password = password;
eth.balance = 0;
eth.mnemonic = mnemonic;
eth.coinList = [{
name: "ETH",
name2: "ETH",
xname: 'Ethereum',
balance: 0,
icon: require('@/static/tongyonh/Frame3299.png')
},
{
name: "USDT",
name2: "ERC20-USDT",
xname: 'Tether USD',
balance: 0,
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.balance = 0;
tron.mnemonic = mnemonic;
tron.coinList = [{
name: "TRX",
name2: "TRX",
xname: 'TRON',
balance: 0,
icon: require('@/static/tongyonh/tron1.png')
},
{
name: "USDT",
name2: "TRC20-USDT",
xname: 'Tether USD',
balance: 0,
contractAddress: 'TR7NHqjeKQxGTCi8q8ZY4pL8otSzgjLj6t',
icon: require('@/static/tongyonh/tether_usd.png')
},
];
let wallrtInfo = {
"BTC": [
btc
],
"ETH": [
eth
],
"TRON": [
tron
]
}
return wallrtInfo;
},
validateBtc: function(address) {
let valid = WAValidator.validate(address, 'BTC');
console.log(valid, 'BTC')
return valid;
},
validateEth: function(address) {
let valid = WAValidator.validate(address, 'ETH');
console.log(valid, 'eth')
return valid;
},
validateTrx: function(base58Str) {
if (typeof(base58Str) !== 'string')
return false;
if (base58Str.length !== 34)
return false;
let address = this.decode58(base58Str);
if (address.length !== 25)
return false;
if (address[0] !== 0x41)
return false;
// const checkSum = address.slice(21);
// address = address.slice(0, 21);
// const hash0 = sha256.sha256(address);
// const hash1 = sha256.sha256(hash0);
// const checkSum1 = hash1.slice(0, 4);
// console.log(hash1)
// console.log(address,1111)
// console.log(checkSum1)
// console.log(checkSum)
// if (checkSum[0] == checkSum1[0] && checkSum[1] == checkSum1[1] && checkSum[2] ==
// checkSum1[2] && checkSum[3] == checkSum1[3]
// ) {
return true
// }
// return false;
},
decode58: function (string) {
var ALPHABET = '123456789ABCDEFGHJKLMNPQRSTUVWXYZabcdefghijkmnopqrstuvwxyz';
var ALPHABET_MAP = {};
var BASE = 58;
for (var i = 0; i < ALPHABET.length; i++) {
ALPHABET_MAP[ALPHABET.charAt(i)] = i;
}
if (string.length === 0) return [];
var i,
j,
bytes = [0];
for (i = 0; i < string.length; i++) {
var c = string[i];
// c是不是ALPHABET_MAP的key
if (!(c in ALPHABET_MAP)) throw new Error('Non-base58 character');
for (j = 0; j < bytes.length; j++) bytes[j] *= BASE;
bytes[0] += ALPHABET_MAP[c];
var carry = 0;
for (j = 0; j < bytes.length; ++j) {
bytes[j] += carry;
carry = bytes[j] >> 8;
// 0xff --> 11111111
bytes[j] &= 0xff;
}
while (carry) {
bytes.push(carry & 0xff);
carry >>= 8;
}
}
// deal with leading zeros
for (i = 0; string[i] === '1' && i < string.length - 1; i++) bytes.push(0);
return bytes.reverse();
}
}
export default token