diff --git a/index.js b/index.js index db200e9..6d6d192 100644 --- a/index.js +++ b/index.js @@ -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 diff --git a/package.json b/package.json index d97c09a..a244b50 100644 --- a/package.json +++ b/package.json @@ -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" },