<secword_to_keypair> should check secword because secword_to_seed accepts even empty secword
This commit is contained in:
parent
741eb70245
commit
1893a0ae9d
29
ticc.js
29
ticc.js
@ -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 })
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
|
Loading…
Reference in New Issue
Block a user