<secword_to_keypair> should check secword because secword_to_seed accepts even empty secword

This commit is contained in:
陆柯 2022-08-16 11:59:28 +08:00
parent 741eb70245
commit 1893a0ae9d

29
ticc.js
View File

@ -444,8 +444,12 @@ class TicCrypto {
// 据测试, Purpose和CoinType都可以任意其他值不必要如规范所示' 引号可有可无,导致的密钥不一样; // 据测试, Purpose和CoinType都可以任意其他值不必要如规范所示' 引号可有可无,导致的密钥不一样;
// Account 最大为 0x7FFFFFFF, Change/Index 最大均为 0xFFFFFFFF(=4294967295) // Account 最大为 0x7FFFFFFF, Change/Index 最大均为 0xFFFFFFFF(=4294967295)
// 但可以不断延伸下去:/xxx/xxx/xxx/xxx/... // 但可以不断延伸下去:/xxx/xxx/xxx/xxx/...
coin = coin?.toUpperCase() || my.COIN coin = coin?.toUpperCase?.() || my.COIN
if (!this.is_secword(secword)) {
// 由于 secword_to_seed 可以对一切字符串都正常返回为防止secword为空在这里先做检查。
return null
}
if (tool === 'nacl') { if (tool === 'nacl') {
// 采用自己的算法bip39算法从secword到种子hash后用 nacl.sign.keyPair.fromSeed()方法。 // 采用自己的算法bip39算法从secword到种子hash后用 nacl.sign.keyPair.fromSeed()方法。
hasher = my.HASHER_LIST.includes(hasher) ? hasher : my.HASHER hasher = my.HASHER_LIST.includes(hasher) ? hasher : my.HASHER
@ -482,7 +486,6 @@ class TicCrypto {
pubkey: key.publicKey.toString('hex'), pubkey: key.publicKey.toString('hex'),
} }
} }
return null
} }
/** /**
@ -553,18 +556,18 @@ class TicCrypto {
* @return {Object} * @return {Object}
* @memberof TicCrypto * @memberof TicCrypto
*/ */
static secword_to_account ({ secword, coin = my.COIN, pass, pathRoot, pathIndex, path, tool, hasher } = {}) { static secword_to_account ({ secword, coin = my.COIN, world, pass, pathRoot, pathIndex, path, tool, hasher } = {}) {
// account 比 keypair 多了 address 字段。 // account 比 keypair 多了 address 字段。
coin = coin?.toUpperCase() 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, pathRoot, pathIndex, 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)
kp.address = this.pubkey_to_address({ pubkey: uncompressedPubkey, coin: 'ETH' }) kp.address = this.pubkey_to_address({ pubkey: uncompressedPubkey, coin: 'ETH', world })
} else { } else {
kp.address = this.pubkey_to_address({ pubkey: kp.pubkey, coin }) kp.address = this.pubkey_to_address({ pubkey: kp.pubkey, coin, world })
} }
return Object.assign(kp, { coin, secword }) return Object.assign(kp, { coin, world, secword })
} }
return null return null
} }
@ -578,8 +581,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 = my.COIN, world, pass, pathRoot, pathIndex, path, tool, hasher } = {}) {
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, pathRoot, pathIndex, path, tool, hasher })
if (kp) { if (kp) {
let address let address
@ -809,7 +812,7 @@ class TicCrypto {
* @return {Boolean} * @return {Boolean}
* @memberof TicCrypto * @memberof TicCrypto
*/ */
static is_chain_address ({ address }) { static which_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) {
@ -853,7 +856,7 @@ class TicCrypto {
* @return {*} * @return {*}
* @memberof TicCrypto * @memberof TicCrypto
*/ */
static secword_to_seed ({ secword, pass }) { static secword_to_seed ({ secword, pass } = {}) {
// 遵循bip39的算法。和 ether.HDNode.mnemonic2Seed 结果一样是64字节的种子。 // 遵循bip39的算法。和 ether.HDNode.mnemonic2Seed 结果一样是64字节的种子。
// !!! 警告bip39.mnemonicToSeedSync 也接受不合法的 secword只要是个string或者是 undefined/null/0/''/false这几个的结果都一样 // !!! 警告bip39.mnemonicToSeedSync 也接受不合法的 secword只要是个string或者是 undefined/null/0/''/false这几个的结果都一样
return bip39.mnemonicToSeedSync(secword, pass).toString('hex') // 结果一致与 new BitcoreMnemonic(secword).toSeed(pass).toString('hex') 或 ethers.HDNode.mnemonic2Seed(secword)。 return bip39.mnemonicToSeedSync(secword, pass).toString('hex') // 结果一致与 new BitcoreMnemonic(secword).toSeed(pass).toString('hex') 或 ethers.HDNode.mnemonic2Seed(secword)。
@ -939,9 +942,9 @@ class TicCrypto {
* @return {*} * @return {*}
* @memberof TicCrypto * @memberof TicCrypto
*/ */
static randomize_account ({ lang, wordCount, coin, pass, pathRoot, pathIndex, path, tool, hasher } = {}) { static randomize_account ({ lang, wordCount, coin, world, pass, pathRoot, 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, pass, pathRoot, pathIndex, path, tool, hasher }) return this.secword_to_account({ secword, coin, world, pass, pathRoot, pathIndex, path, tool, hasher })
} }
/** /**