This commit is contained in:
陆柯 2022-07-03 23:01:29 +08:00
parent 5bfd536c03
commit f73f4beb23

View File

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