u
This commit is contained in:
parent
5bfd536c03
commit
f73f4beb23
50
index.js
50
index.js
@ -386,7 +386,7 @@ class TicCrypto {
|
||||
* @memberof TicCrypto
|
||||
*/
|
||||
static entropy_to_secword ({ entropy } = {}) {
|
||||
// entropy could be hex string or buffer. 位数可为 128/160/192/224/256 位,即 16, 20, 24, 28, 32 字节,最后可生成 12, 15, 18, 21, 24 个单词的助记词。
|
||||
// entropy could be hex string or buffer. 位数可为 128|160|192|224|256 位,即 16|20|24|28|32 字节,最后可生成 12|15|18|21|24 个单词的助记词。
|
||||
return bip39.entropyToMnemonic(entropy) // results are the same for the same entropy.
|
||||
}
|
||||
|
||||
@ -399,7 +399,7 @@ class TicCrypto {
|
||||
* @memberof TicCrypto
|
||||
*/
|
||||
static secword_to_entropy ({ secword } = {}) {
|
||||
// secword could be of length 12, 15, 18, 21, 24,which outputs hex of length 32, 40, 48, 56, 64.
|
||||
// secword could be of length 12|15|18|21|24,which outputs hex of length 32|40|48|56|64.
|
||||
return bip39.mnemonicToEntropy(secword) // results are the same for the same secword.
|
||||
}
|
||||
|
||||
@ -412,7 +412,7 @@ class TicCrypto {
|
||||
* @return {Object} {pubkey, seckey,}
|
||||
* @memberof TicCrypto
|
||||
*/
|
||||
static secword_to_keypair ({ secword, coin, pass, pathSeed, path, tool, hasher } = {}) {
|
||||
static secword_to_keypair ({ secword, coin, pass, pathRoot, path, tool, hasher } = {}) {
|
||||
// coin 币种;
|
||||
// passphase 密码,默认为空;
|
||||
// path==='master' 生成 HD master key,不定义则默认为相应币种的第一对公私钥。
|
||||
@ -449,8 +449,8 @@ class TicCrypto {
|
||||
if (path === 'master') {
|
||||
key = hdmaster
|
||||
} else {
|
||||
// 指定了路径 path 例如 "m/0/2147483647'/1" 则用 path;没有指定路径 则调用 seed_to_path() 来获取路径, 例如 不存在 pathSeed 时获取的是根路径 "m/44'/0'/0'/0/0" 或 "m/44'/60'/0'/0/0"
|
||||
path = path || this.seed_to_path({ seed: pathSeed, coin })
|
||||
// 指定了路径 path 例如 "m/0/2147483647'/1" 则用 path;没有指定路径 则调用 root_to_path() 来获取路径, 例如 不存在 pathRoot 时获取的是根路径 "m/44'/0'/0'/0/0" 或 "m/44'/60'/0'/0/0"
|
||||
path = path || this.root_to_path({ pathRoot, coin })
|
||||
key = hdmaster.derive(path)
|
||||
}
|
||||
return {
|
||||
@ -468,30 +468,30 @@ class TicCrypto {
|
||||
* 从种子到路径
|
||||
*
|
||||
* @static
|
||||
* @param {*} seed
|
||||
* @param {string} option [{ coin = my.COIN }={ coin: my.COIN }]
|
||||
* @param {*} pathRoot
|
||||
* @param {string} option [{ coin = my.COIN }={ }]
|
||||
* @return {String} path
|
||||
* @memberof TicCrypto
|
||||
*/
|
||||
static seed_to_path ({ seed, coin = my.COIN } = {}) {
|
||||
static root_to_path ({ pathRoot, coin } = {}) {
|
||||
// 路径规范 BIP44: m/Purpose'/Coin'/Account'/Change/Index,
|
||||
// 但实际上 Purpose, Coin 都可任意定;' 可有可无;
|
||||
// Account/Change/Index 最大到 parseInt(0x7FFFFFFF, 16)
|
||||
// 后面还可继续延伸 /xxx/xxx/xxx/......
|
||||
let path
|
||||
if (seed) {
|
||||
let hash = this.hash(seed, { hasher: 'md5' })
|
||||
let part0 = parseInt(hash.slice(0, 6), 16)
|
||||
let part1 = parseInt(hash.slice(6, 12), 16)
|
||||
let part2 = parseInt(hash.slice(12, 18), 16)
|
||||
let part3 = parseInt(hash.slice(18, 24), 16)
|
||||
let part4 = parseInt(hash.slice(24, 30), 16)
|
||||
let part5 = parseInt(hash.slice(30, 32), 16)
|
||||
if (pathRoot) {
|
||||
let pathHash = this.hash(pathRoot, { hasher: 'md5' })
|
||||
let part0 = parseInt(pathHash.slice(0, 6), 16)
|
||||
let part1 = parseInt(pathHash.slice(6, 12), 16)
|
||||
let part2 = parseInt(pathHash.slice(12, 18), 16)
|
||||
let part3 = parseInt(pathHash.slice(18, 24), 16)
|
||||
let part4 = parseInt(pathHash.slice(24, 30), 16)
|
||||
let part5 = parseInt(pathHash.slice(30, 32), 16)
|
||||
path = `${part0}'/${part1}/${part2}/${part3}/${part4}/${part5}`
|
||||
} else {
|
||||
path = "0'/0/0"
|
||||
}
|
||||
coin = coin.toUpperCase() || my.COIN
|
||||
coin = coin?.toUpperCase() || my.COIN
|
||||
if (coin === 'BTC') {
|
||||
return `m/44'/0'/${path}`
|
||||
} else if (coin === 'ETH') {
|
||||
@ -528,10 +528,10 @@ class TicCrypto {
|
||||
* @return {Object}
|
||||
* @memberof TicCrypto
|
||||
*/
|
||||
static secword_to_account ({ secword, coin, pass, pathSeed, path, tool, hasher } = {}) {
|
||||
static secword_to_account ({ secword, coin, pass, pathRoot, path, tool, hasher } = {}) {
|
||||
// account 比 keypair 多了 address 字段。
|
||||
coin = coin?.toUpperCase() || my.COIN
|
||||
let kp = this.secword_to_keypair({ secword, coin, pass, pathSeed, path, tool, hasher })
|
||||
let kp = this.secword_to_keypair({ secword, coin, pass, pathRoot, path, tool, hasher })
|
||||
if (kp) {
|
||||
if (coin === 'ETH') {
|
||||
let uncompressedPubkey = this.decompress_pubkey(kp.pubkey)
|
||||
@ -553,9 +553,9 @@ class TicCrypto {
|
||||
* @return {String} address
|
||||
* @memberof TicCrypto
|
||||
*/
|
||||
static secword_to_address ({ secword, coin, world, pass, pathSeed, path, tool, hasher } = {}) {
|
||||
static secword_to_address ({ secword, coin, world, pass, pathRoot, path, tool, hasher } = {}) {
|
||||
coin = coin?.toUpperCase() || my.COIN
|
||||
let kp = this.secword_to_keypair({ secword, coin, pass, pathSeed, path, tool, hasher })
|
||||
let kp = this.secword_to_keypair({ secword, coin, pass, pathRoot, path, tool, hasher })
|
||||
if (kp) {
|
||||
let address
|
||||
if (coin === 'ETH') {
|
||||
@ -721,7 +721,7 @@ class TicCrypto {
|
||||
address = bs58check.encode(Buffer.from(prefix + position, 'hex')) // wallet import format
|
||||
return address
|
||||
} else {
|
||||
// 默认为 TIC。把纯位置转换为大小写敏感能自我验证的 b64t 地址。
|
||||
// 默认为 TIC 系列。把纯位置转换为大小写敏感能自我验证的 b64t 地址。
|
||||
let prefix
|
||||
switch (world) {
|
||||
// Base64: https://baike.baidu.com/item/base64
|
||||
@ -784,7 +784,7 @@ class TicCrypto {
|
||||
* @return {Boolean}
|
||||
* @memberof TicCrypto
|
||||
*/
|
||||
static is_chain_address ({address}) {
|
||||
static is_chain_address ({ address }) {
|
||||
if (/^(0x)?[\da-fA-F]{40}$/.test(address)) {
|
||||
return 'ETH'
|
||||
} else if (/^[123456789ABCDEFGHJKLMNPQRSTUVWXYZabcdefghijkmnopqrstuvwxyz]{26,34}$/.test(address) && address.length !== 32) {
|
||||
@ -937,9 +937,9 @@ class TicCrypto {
|
||||
* @return {*}
|
||||
* @memberof TicCrypto
|
||||
*/
|
||||
static randomize_account ({ lang, wordCount, coin, pass, pathSeed, path, tool, hasher } = {}) {
|
||||
static randomize_account ({ lang, wordCount, coin, pass, pathRoot, path, tool, hasher } = {}) {
|
||||
let secword = this.randomize_secword({ lang, wordCount })
|
||||
return this.secword_to_account({ secword, coin, pass, pathSeed, path, tool, hasher })
|
||||
return this.secword_to_account({ secword, coin, pass, pathRoot, path, tool, hasher })
|
||||
}
|
||||
|
||||
/**
|
||||
|
Loading…
Reference in New Issue
Block a user