实现了以太坊地址的生成

This commit is contained in:
陆柯 2019-12-11 16:13:51 +08:00
parent 887035f496
commit 5b4bceb2eb
2 changed files with 44 additions and 21 deletions

View File

@ -3,6 +3,7 @@ const crypto=require('crypto')
const nacl = require('tweetnacl')
const bs58check = require('bs58check')
const uuid = require('uuid')
const keccak = require('keccak')
const Secword = require('bitcore-mnemonic') // https://bitcore.io/api/mnemonic/ https://github.com/bitpay/bitcore-mnemonic
// const bip39 = require('bip39') // https://github.com/bitcoinjs/bip39 // 有更多语言,但不方便选择语言,也不能使用 pass
// const HDKey = require('hdkey') // https://github.com/cryptocoinjs/hdkey // 或者用 bitcore-mnemonic 或者 ethers 里的相同功能
@ -192,6 +193,28 @@ module.exports = {
return null
}
,
secword2account:function(secword, option){ // account 比 keypair 多了 address 字段。
option=option||{}
option.coin=my.COIN_LIST.indexOf(option.coin)>=0?option.coin:my.COIN
let kp=this.secword2keypair(secword, option)
if (kp) {
kp.address=this.seckey2address(kp.seckey, option)
return kp
}
return null
}
,
secword2address:function(secword, option){
option=option||{}
option.coin=my.COIN_LIST.indexOf(option.coin)>=0?option.coin:my.COIN
let address
let kp=this.secword2keypair(secword, option)
if (kp) {
return this.seckey2address(kp.seckey,option)
}
return null
}
,
seckey2pubkey:function(seckey, option){
option=option||{}
option.coin=my.COIN_LIST.indexOf(option.coin)>=0?option.coin:my.COIN
@ -210,24 +233,18 @@ module.exports = {
return null
}
,
secword2account:function(secword, option){ // account 比 keypair 多了 address 字段。
seckey2address: function(seckey, option){
option=option||{}
option.coin=my.COIN_LIST.indexOf(option.coin)>=0?option.coin:my.COIN
let kp=this.secword2keypair(secword, option)
if (kp) {
kp.address=this.pubkey2address(kp.pubkey, option)
return kp
}
return null
}
,
secword2address:function(secword, option){
option=option||{}
option.coin=my.COIN_LIST.indexOf(option.coin)>=0?option.coin:my.COIN
let address
let kp=this.secword2keypair(secword, option)
if (kp) {
return this.pubkey2address(kp.pubkey,option)
if (this.isSeckey(seckey)){
let pubkey
if (option.coin==='ETH'){
pubkey = this.seckey2pubkey(seckey, {compress:'uncompressed'})
return this.pubkey2address(pubkey, option)
}else {
pubkey = this.seckey2pubkey(seckey, {compress:'compressed'})
return this.pubkey2address(pubkey, option)
}
}
return null
}
@ -262,20 +279,25 @@ module.exports = {
let h160 = crypto.createHash('ripemd160').update(h256).digest('hex')
let prefix
if (option.coin==='TIC'){
switch(option.netType){
switch (option.netType){
case 'mainnet': prefix='42'; break; // '42'=>T, '6E'=>m
case 'testnet': prefix='7F'; break; // '7F'=>t
case 'devnet': prefix='5A'; break; // '5A'=>d
default: prefix='42' // 默认暂且为 42为了兼容已经运行的链。
}
}else if(option.coin==='BTC'){
}else if (option.coin==='BTC'){
switch (option.netType) {
case 'mainnet': prefix='00'; break; // 1
case 'testnet': prefix='6f'; break; // m or n
case 'p2sh': prefix='05'; break; // 3
default: prefix='00'
}
}else { // 目前不支持 ETH或其他币种 地址转换因为这会大量增加前端打包的js。
}else if (option.coin==='ETH'){
// 注意必须要用非压缩的64字节的公钥的buffer。
return '0x' + keccak('keccak256').update(Buffer.from(pubkey.slice(2),'hex')).digest('hex').slice(-40)
// 或 const { keccak256 } = require('ethereumjs-util'); keccak256(Buffer.from(pubkey.slice(2),'hex)).toString('hex').slice(40)
// 或 const { Keccak } = require('sha3'); new Keccak('').update(Bufer.from(pubkey.slice(2),'hex')).digest('hex').slice(-40)
}else {
return null
}
var wifAddress=bs58check.encode(Buffer.from(prefix+h160,'hex')) // wallet import format

View File

@ -6,11 +6,12 @@
"bignumber.js": "^6.0.0",
"bitcore-mnemonic": "^1.5.0",
"bs58check": "^2.1.1",
"ethereumjs-util": "^6.2.0",
"sha3": "^2.1.1",
"tweetnacl": "^1.0.0",
"uuid": "^3.3.2"
},
"devDependencies": {
},
"devDependencies": {},
"scripts": {
"setup": "npm install"
},