rename pathRoot to pathSeed

This commit is contained in:
陆柯 2022-09-17 15:10:16 +08:00
parent dc834c8157
commit 93eb774457

30
ticc.js
View File

@ -431,7 +431,7 @@ class TicCrypto {
* @return {Object} {pubkey, prikey,} * @return {Object} {pubkey, prikey,}
* @memberof TicCrypto * @memberof TicCrypto
*/ */
static secword_to_keypair ({ secword, coin, pass, pathRoot, pathIndex, path, tool, hasher = my.HASHER } = {}) { static secword_to_keypair ({ secword, coin, pass, pathSeed, pathIndex, path, tool, hasher = my.HASHER } = {}) {
// coin 币种; // coin 币种;
// passphase 密码,默认为空; // passphase 密码,默认为空;
// path 规范为 m/Purpose'/CoinType'/Account'/Change/Index (https://learnblockchain.cn/2018/09/28/hdwallet/), 其中 // path 规范为 m/Purpose'/CoinType'/Account'/Change/Index (https://learnblockchain.cn/2018/09/28/hdwallet/), 其中
@ -466,11 +466,11 @@ class TicCrypto {
// let hdmaster=new BitcoreMnemonic(secword).toHDPrivateKey(pass) // 和 ethers.HDNode.fromMnemonic(secword)的公私钥一样。而 ethers.HDNode.fromMnemonic(secword).derivePath("m/44'/60'/0'/0/0")的公私钥===ethers.Wallet.fromMnemonic(secword [,"m/44'/60'/0'/0/0"]) // let hdmaster=new BitcoreMnemonic(secword).toHDPrivateKey(pass) // 和 ethers.HDNode.fromMnemonic(secword)的公私钥一样。而 ethers.HDNode.fromMnemonic(secword).derivePath("m/44'/60'/0'/0/0")的公私钥===ethers.Wallet.fromMnemonic(secword [,"m/44'/60'/0'/0/0"])
let key = hdmaster let key = hdmaster
if (path) { if (path) {
// 指定了path 例如 "m/0/2147483647'/1" 则用 path 例如 不存在 pathRoot 时获取的是根路径 "m/44'/0'/0'/0/0" 或 "m/44'/60'/0'/0/0" // 指定了path 例如 "m/0/2147483647'/1" 则用 path 例如 不存在 pathSeed 时获取的是根路径 "m/44'/0'/0'/0/0" 或 "m/44'/60'/0'/0/0"
key = hdmaster.derive(path) key = hdmaster.derive(path)
} else if (pathRoot) { } else if (pathSeed) {
// 指定了 pathRoot 则调用 root_to_path() 来获取路径 // 指定了 pathSeed 则调用 root_to_path() 来获取路径
path = this.root_to_path({ pathRoot, pathIndex, coin }) path = this.root_to_path({ pathSeed, pathIndex, coin })
key = hdmaster.derive(path) key = hdmaster.derive(path)
} else { } else {
// 没有指定 path 或 pathRoot则返回主钥 // 没有指定 path 或 pathRoot则返回主钥
@ -489,19 +489,19 @@ class TicCrypto {
* 从种子到路径 * 从种子到路径
* *
* @static * @static
* @param {*} pathRoot * @param {*} pathSeed
* @param {string} option [{ coin = my.COIN }={ }] * @param {string} option [{ coin = my.COIN }={ }]
* @return {String} path * @return {String} path
* @memberof TicCrypto * @memberof TicCrypto
*/ */
static root_to_path ({ pathRoot, pathIndex, coin = my.COIN } = {}) { static root_to_path ({ pathSeed, pathIndex, coin = my.COIN } = {}) {
// 路径规范 BIP44: m/Purpose'/Coin'/Account'/Change/Index, // 路径规范 BIP44: m/Purpose'/Coin'/Account'/Change/Index,
// 但实际上 Purpose, Coin 都可任意定;' 可有可无; // 但实际上 Purpose, Coin 都可任意定;' 可有可无;
// 后面还可继续延伸 /xxx/xxx/xxx/...... // 后面还可继续延伸 /xxx/xxx/xxx/......
// 每个数字最大到 parseInt("0x7FFFFFFF", 16)=parseInt(0x7FFFFFFF)=2147483647更大就报错。 // 每个数字最大到 parseInt("0x7FFFFFFF", 16)=parseInt(0x7FFFFFFF)=2147483647更大就报错。
let path let path
if (pathRoot) { if (pathSeed) {
let pathHash = this.hash_easy(pathRoot, { hasher: 'md5' }) let pathHash = this.hash_easy(pathSeed, { hasher: 'md5' })
let part0 = parseInt(pathHash.slice(0, 6), 16) let part0 = parseInt(pathHash.slice(0, 6), 16)
let part1 = parseInt(pathHash.slice(6, 12), 16) let part1 = parseInt(pathHash.slice(6, 12), 16)
let part2 = parseInt(pathHash.slice(12, 18), 16) let part2 = parseInt(pathHash.slice(12, 18), 16)
@ -554,10 +554,10 @@ class TicCrypto {
* @memberof TicCrypto * @memberof TicCrypto
* 只要提供了 path pathRoot就创建 bip39 账户如果都不存在那就创建主账户 * 只要提供了 path pathRoot就创建 bip39 账户如果都不存在那就创建主账户
*/ */
static secword_to_account ({ secword, coin, world, pass, pathRoot, pathIndex, path, tool, hasher } = {}) { static secword_to_account ({ secword, coin, world, pass, pathSeed, pathIndex, 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, pathRoot, pathIndex, path, tool, hasher }) let kp = this.secword_to_keypair({ secword, coin, pass, pathSeed, pathIndex, path, tool, hasher })
if (kp) { if (kp) {
if (coin === 'ETH') { if (coin === 'ETH') {
world = world || 'mainnet' world = world || 'mainnet'
@ -584,8 +584,8 @@ class TicCrypto {
* @return {String} address * @return {String} address
* @memberof TicCrypto * @memberof TicCrypto
*/ */
static secword_to_address ({ secword, coin, world, pass, pathRoot, pathIndex, path, tool, hasher } = {}) { static secword_to_address ({ secword, coin, world, pass, pathSeed, pathIndex, path, tool, hasher } = {}) {
const account = this.secword_to_account({ secword, coin, world, pass, pathRoot, pathIndex, path, tool, hasher }) const account = this.secword_to_account({ secword, coin, world, pass, pathSeed, pathIndex, path, tool, hasher })
return account?.address return account?.address
} }
@ -935,9 +935,9 @@ class TicCrypto {
* @return {*} * @return {*}
* @memberof TicCrypto * @memberof TicCrypto
*/ */
static randomize_account ({ lang, wordCount, coin, world, pass, pathRoot, pathIndex, path, tool, hasher } = {}) { static randomize_account ({ lang, wordCount, coin, world, pass, pathSeed, pathIndex, path, tool, hasher } = {}) {
let secword = this.randomize_secword({ lang, wordCount }) let secword = this.randomize_secword({ lang, wordCount })
return this.secword_to_account({ secword, coin, world, pass, pathRoot, pathIndex, path, tool, hasher }) return this.secword_to_account({ secword, coin, world, pass, pathSeed, pathIndex, path, tool, hasher })
} }
/** /**