ignore *nogit* and *nosf*; 似乎 hash_to_sig_distance 用的还是之前的 BigNumber,现在是失效的,重新改写。

This commit is contained in:
Luk 2024-09-22 15:50:33 +08:00
parent ec367b9bbd
commit 43b9bd6898
3 changed files with 26 additions and 8 deletions

6
.gitignore vendored
View File

@ -13,9 +13,13 @@
?*.gitignore.*
?*.gitignore.*/
*.gitomit
*.gitomit/
*.gitomit.*
*.gitomit/
*.gitomit.*/
*.nogit
*.nogit.*
*.nogit/
*.nogit.*/
# 保留
!.gitignore
!.gitignore.*

View File

@ -17,6 +17,10 @@
*.sfomit.*
*.sfomit/
*.sfomit.*/
*.nosf
*.nosf.*
*.nosf/
*.nosf.*/
.DS_Store
*/.DS_Store

24
ticc.js
View File

@ -1,5 +1,5 @@
// const BigNumber=require('bignumber.js') // 处理整数 https://github.com/MikeMcl/bignumber.js
const BigInt = require('big-integer') // 处理整数 https://github.com/peterolson/BigInteger.js
// const BigNumber=require('bignumber.js') // 处理整数 https://github.com/MikeMcl/bignumber.js // size: 360K
const BigInt = require('big-integer') // 处理整数 https://github.com/peterolson/BigInteger.js // size: 188K. ethers.js 24M.
const crypto = require('crypto')
const nacl = require('tweetnacl')
const bs58check = require('bs58check')
@ -193,6 +193,7 @@ class TicCrypto {
* @param {option} [{ hasher = my.HASHER, salt, input = my.INPUT, output = my.OUTPUT }={}]
* @return {String}
* @memberof TicCrypto
* 返回结果不包含 0x
*/
static hash_easy (data, { hasher = my.HASHER, salt, input = my.INPUT, output = my.OUTPUT } = {}) {
// data can be anything, but converts to string or remains be Buffer/TypedArray/DataView
@ -264,9 +265,11 @@ class TicCrypto {
// data 应当是 encrypt 输出的数据类型
if (mode === 'ecrsa') {
if (key?.prikey && key?.pubkey) {
return ecrsa.decryptMessage(data, key?.prikey, key?.pubkey)
return ecrsa.decryptMessage(data, key.prikey, key.pubkey)
} else if (key?.receiverPrikey && key?.senderPubkey) {
return ecrsa.decryptMessage(data, key.receiverPrikey, key.senderPubkey)
} else {
return ecrsa.decryptMessage(data, key?.receiverPrikey, key?.senderPubkey)
return null
}
} else if (mode === 'ecc') {
try {
@ -533,6 +536,8 @@ class TicCrypto {
return `m/44'/60'/${path}`
} else if (coin === 'TIC') {
return `m/44'/60000'/${path}`
} else if (coin === 'MATIC') { // Polygon 测试网 (Mumbai): 80001
return `m/44'/137'/${path}`
} else if (/[A-Z]{3}/.test(coin)) {
return `m/44'/60${this.alpha_to_digit(coin)}'/${path}`
} else {
@ -1097,7 +1102,7 @@ class TicCrypto {
// hash为64hex字符sig为128hex字符。返回用hex表达的距离。
if (this.is_signature({ sig: sig }) && this.is_hash({ hash })) {
var hashSig = this.hash_easy(sig) // 把签名也转成32字节的哈希同样长度方便比较
return (BigInt('0x' + hash) - BigInt('0x' + hashSig)).toString(16).replace(/^-/, '')
return (new BigInt(hash, 16) - new BigInt(hashSig, 16)).toString(16).replace(/^-/, '') // if using bignumber.js: (BigInt('0x' + hash) - BigInt('0x' + hashSig)).toString(16)
}
return null
}
@ -1455,15 +1460,18 @@ class TicCrypto {
return '04' + this.padStart(x.toString(16), 64, '0') + this.padStart(y.toString(16), 64, '0')
}
// cosh: content hash. 最核心的纯hex的内容地址没有任何额外标记。同一个内容的cosh是唯一的而cid是在cosh基础上有各种不同的编码。cid建议叫做 coid.
static cid_to_cosh ({ cid }) {
if (/^[Q|1]/.test(cid)) {
return this.b58_to_hex(cid).slice(4)
return this.b58_to_hex(cid).slice(4) // 前2字节是 cid0 的字节序数标记
} else if (/^[b|B]/.test(cid)) {
return this.b32_to_hex(cid.substr(1)).slice(8)
return this.b32_to_hex(cid.substr(1)).slice(8) // 前4字节是 cid1 的标记
} else if (/^z/.test(cid)) {
return this.b58_to_hex(cid.substr(1)).slice(8)
} else if (/^[m|M|u|U]/.test(cid)) {
return Buffer.from(cid.substr(1), 'base64').toString('hex')
} else if (/^9/.test(cid)) {
return new BigInt(cid.substr(1)).toString(16).slice(7) // BigInt toString(16) 后,去掉了 01551220... 的打头的 0所以只有7位需要跳过了
}
}
@ -1522,6 +1530,8 @@ class TicCrypto {
converted = Buffer.from(fullHex, 'hex').toString('base64')
} else if (cidBase === 'b64') {
converted = Buffer.from(fullHex, 'hex').toString('base64').replace(/=/g, '')
} else if (cidBase === 'b10') {
converted = new BigInt(fullHex, 16).toString()
}
return multibase[cidBase] + converted
}