改进 isSecword 防止数量不对导致 bitcore-mnemonics 异常。改进 isAddress 把 prefix 也纳入 TIC地址的校验。

This commit is contained in:
陆柯 2020-02-23 10:46:19 +08:00
parent bdec8f6acb
commit 20169a56ee
3 changed files with 12 additions and 7 deletions

1
.gitignore vendored
View File

@ -5,3 +5,4 @@ package-lock.json
.svn
~*
.gitattributes
dump.rdb

BIN
dump.rdb

Binary file not shown.

View File

@ -67,7 +67,10 @@ module.exports = {
}
,
isSecword(secword){
return Secword.isValid(secword)
if (typeof secword==='string' && 12===secword.split(/ +/))
return Secword.isValid(secword)
else
return false
}
,
isSeckey(seckey){
@ -357,7 +360,7 @@ module.exports = {
case 'devnet': prefix='74'; break; // Base58: 0x90 => d, Base 64: d=0x1d=0b00011101 => 0b 011101xx = 0x74~77
default: prefix='4c'
}
let checksum = this.hash(this.hash(position)).slice(0,6) // 添加 checksum 使得能够检测大小写错误。注意,不包含 prefix这样更纯粹、专注。
let checksum = this.hash(this.hash(prefix+position)).slice(0,6) // 添加 checksum 使得能够检测大小写错误。[todo] 校验码里要不要包含 prefix?
// address = this.hex2eip55(prefix + position + checksum) // 前缀1节位置20节校验3节共24节=48字符能够完全转化为8个色彩再转eip55。
address = this.hex2b64u(prefix + position + checksum) // 实际采用 b64u (named by luk.lu as base 64 for url), 共 32字符。
return address
@ -384,16 +387,17 @@ module.exports = {
}
,
isAddress(address){
if (/^0x[\da-fA-F]{40}$/.test(address)){
if (/^(0x)?[\da-fA-F]{40}$/.test(address)){
return 'ETH'
}else if (/^[123456789ABCDEFGHJKLMNPQRSTUVWXYZabcdefghijkmnopqrstuvwxyz]{26,34}$/.test(address) // 格式合法
&& this.b58c2hex(address)){ // 内容合法
}else if (/^[123456789ABCDEFGHJKLMNPQRSTUVWXYZabcdefghijkmnopqrstuvwxyz]{26,34}$/.test(address)){ // 格式合法
let prefixedPosition = this.b58c2hex(address)
if (prefixedPosition && prefixedPosition.length===42) // 内容合法
return 'BTC'
}else if (/^[Ttd][0-9a-zA-Z\-_]{31}$/.test(address)){ // 格式合法
let b64 = address.replace('-', '+').replace('_', '/')
let hex = Buffer.from(b64, 'base64').toString('hex')
let [all, prefix, position, checksum] = hex.match(/^([\da-fA-F]{2})([\da-fA-F]{40})([\da-fA-F]{6})$/)
return this.hash(this.hash(position)).slice(0,6) === checksum
let [all, prefix, position, checksum] = hex.match(/^([\da-fA-F]{2})([\da-fA-F]{40})([\da-fA-F]{6})$/) // 内容合法
return this.hash(this.hash(prefix+position)).slice(0,6) === checksum // [todo] 校验码里要不要包含 prefix?
}
return null
}