完善 isSecword,使用 bip39.mnemonicToEntropy 来精确判断。

This commit is contained in:
陆柯 2020-02-29 07:28:33 +08:00
parent 9454bfd5a0
commit b923fec9cc
2 changed files with 17 additions and 12 deletions

BIN
README.xlsx Normal file

Binary file not shown.

View File

@ -51,7 +51,7 @@ module.exports = {
return false return false
} }
, ,
isSecword(secword){ // 注意 not all 12 words combinations are valid for both bitcore and bip39. Must be generated automatically. 另外实际上bitcore和bip39对12, 15, 18, ... 长度的合法助记词都返回 true。 isSecword(secword){ // 注意 not all 12 words combinations are valid for both bitcore and bip39, because there are checksum in mnemonic. 另外实际上bitcore和bip39对12, 15, 18, ... 长度的合法助记词都返回 true。
//// for bitcore-mnemonic. 注意bitcore-mnemonic 对少于12词的会抛出异常很蠢。 //// for bitcore-mnemonic. 注意bitcore-mnemonic 对少于12词的会抛出异常很蠢。
// if (typeof secword==='string' && 12===secword.split(/ +/).length) // if (typeof secword==='string' && 12===secword.split(/ +/).length)
// return BitcoreMnemonic.isValid(secword) // return BitcoreMnemonic.isValid(secword)
@ -59,9 +59,14 @@ module.exports = {
// return false // return false
//// for bip39. 注意bip39对当前defaultWordlist之外其他语言的合法 mnemonic 也返回 false这一点不如 bitcore-mnemonic. 所以不能直接 bip39.validateMnemonic(secword) //// for bip39. 注意bip39对当前defaultWordlist之外其他语言的合法 mnemonic 也返回 false这一点不如 bitcore-mnemonic. 所以不能直接 bip39.validateMnemonic(secword)
if (typeof secword==='string' && 12===secword.split(/ +/).length) if (typeof secword==='string' && 12===secword.split(/ +/).length) {
try {
bip39.mnemonicToEntropy(secword)
return true return true
else }catch(exception){
return false
}
}
return false return false
} }
, ,
@ -374,7 +379,7 @@ module.exports = {
} }
} }
return address return address
}else if (coin === 'BTC'){ // 对比特币把纯位置转换为大小写敏感能自我验证的bs58check地址先加前缀再加校验共25字节再转base58。得到2634个字符大多数34个。 }else if (coin === 'BTC'){ // 对比特币把纯位置转换为大小写敏感能自我验证的bs58check地址先加前缀1,再加校验4共25字节再转base58。得到2634个字符大多数34个。
let prefix let prefix
switch (net) { switch (net) {
case 'mainnet': prefix='00'; break; // pubkey hash => 1 case 'mainnet': prefix='00'; break; // pubkey hash => 1
@ -461,13 +466,13 @@ module.exports = {
// for bip39 // for bip39
const langMap = { zhcn: 'chinese_simplified', zhtw: 'chinese_traditional', enus: 'english', frfr: 'french', itit: 'italian', jajp: 'japanese', kokr: 'korean', eses: 'spanish' } const langMap = { zhcn: 'chinese_simplified', zhtw: 'chinese_traditional', enus: 'english', frfr: 'french', itit: 'italian', jajp: 'japanese', kokr: 'korean', eses: 'spanish' }
langMap.chinese=langMap.cn=langMap.zhcn langMap.chinese=langMap.cn=langMap.zh=langMap.china=langMap.zhcn
langMap.taiwanese=langMap.tw=langMap.zhtw langMap.taiwanese=langMap.tw=langMap.zhtw
langMap.en=langMap.enus langMap.en=langMap.us=langMap.uk=langMap.enus
langMap.fr=langMap.frfr langMap.fr=langMap.france=langMap.frfr
langMap.it=langMap.itit langMap.it=langMap.italy=langMap.itit
langMap.ko=langMap.kr=langMap.kokr langMap.ko=langMap.kr=langMap.korean=langMap.kokr
langMap.ja=langMap.jp=langMap.jajp langMap.ja=langMap.jp=langMap.japan=langMap.jajp
let language = 'english' let language = 'english'
if (typeof(lang)==='string'){ if (typeof(lang)==='string'){