From 34cf26e1684d73371e2da7d8c49f379bbcdb561e Mon Sep 17 00:00:00 2001 From: "luk.lu" Date: Wed, 3 Aug 2022 16:30:09 +0800 Subject: [PATCH] =?UTF-8?q?=E6=8A=8A=E5=A4=A7=E5=A4=9A=E6=95=B0ticc?= =?UTF-8?q?=E6=96=B9=E6=B3=95=E7=9A=84=E5=8F=82=E6=95=B0=E7=B1=BB=E5=9E=8B?= =?UTF-8?q?=E7=BB=9F=E4=B8=80=E4=B8=BA=20{=20...=20}=20=E5=AF=B9=E8=B1=A1?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- ticc.js | 31 ++++++++++++++----------------- 1 file changed, 14 insertions(+), 17 deletions(-) diff --git a/ticc.js b/ticc.js index beffbd5..8305305 100644 --- a/ticc.js +++ b/ticc.js @@ -142,7 +142,7 @@ class TicCrypto { * @return {Boolean} * @memberof TicCrypto */ - static is_seckey (seckey) { + static is_seckey ({ seckey } = {}) { // 比特币、以太坊的私钥:64 hex // nacl.sign 的私钥 128 hex, nacl.box 的私钥 64 hex return /^([a-fA-F0-9]{128}|[a-fA-F0-9]{64})$/.test(seckey) @@ -171,8 +171,8 @@ class TicCrypto { * @return {Boolean} * @memberof TicCrypto */ - static is_signature (signature) { - return /^[a-fA-F0-9]{128,144}$/.test(signature) && signature.length % 2 === 0 // 128 for nacl, 140/142/144 for crypto and eccrypto in der format. + static is_signature ({ sig } = {}) { + return /^[a-fA-F0-9]{128,144}$/.test(sig) && sig.length % 2 === 0 // 128 for nacl, 140/142/144 for crypto and eccrypto in der format. } /** @@ -299,7 +299,7 @@ class TicCrypto { */ static async sign_easy ({ data, seckey, tool = 'crypto', hasher }) { // data can be string or buffer or object, results are the same - if (this.is_hashable({ data }) && this.is_seckey(seckey)) { + if (this.is_hashable({ data }) && this.is_seckey({ seckey })) { if (tool === 'nacl' && seckey.length === 128) { // 使用nacl的签名算法。注意,nacl.sign需要的seckey是64字节=128字符。 let hashBuf = this.hash_easy(data, { output: 'buf' }) // 哈希必须输出为 buffer @@ -336,7 +336,7 @@ class TicCrypto { */ static async verify_easy ({ data, signature, pubkey, tool = 'crypto', hasher }) { // data could be anything, but converts to string or remains be Buffer/TypedArray/DataView - if (this.is_hashable({ data }) && this.is_signature(signature) && this.is_pubkey({ pubkey })) { + if (this.is_hashable({ data }) && this.is_signature({ sig: signature }) && this.is_pubkey({ pubkey })) { if ('nacl' === tool && signature.length === 128) { let bufHash = this.hash_easy(data, { output: 'buf' }) let bufSignature = Buffer.from(signature, 'hex') @@ -599,7 +599,7 @@ class TicCrypto { * @memberof TicCrypto */ static seckey_to_pubkey ({ seckey, curve, compress } = {}) { - if (this.is_seckey(seckey) && seckey.length === 64) { + if (this.is_seckey({ seckey }) && seckey.length === 64) { // 只能用于32字节的私钥(BTC, ETH)。也就是不能用于 TIC 的私钥。 curve = my.CURVE_LIST.includes(curve) ? curve : my.CURVE // 默认为 secp256k1 // return new crypto.createECDH(curve).setPrivateKey(seckey,'hex').getPublicKey('hex', compress===false?'uncompressed':'compressed') // ecdh.getPublicKey(不加参数) 默认为 'compressed'。用 HBuilderX 2.6.4 打包成ios或安卓 app 后 setPrivateKey() 报错:TypeError: null is not an object (evaluating 'this.rand.getBytes') @@ -613,7 +613,7 @@ class TicCrypto { // return ecc.getPublicCompressed(this.hex_to_buf(seckey)).toString('hex') // } // 注意,Buffer.from(nacl.box.keyPair.fromSecretKey(Buffer.from(seckey,'hex')).publicKey).toString('hex') 得到的公钥与上面的不同 - } else if (this.is_seckey(seckey) && seckey.length === 128) { + } else if (this.is_seckey({ seckey }) && seckey.length === 128) { // 用于64字节=128 hex的 TIC 私钥 let keypair = nacl.sign.keyPair.fromSecretKey(Buffer.from(seckey, 'hex')) return Buffer.from(keypair.publicKey).toString('hex') // 测试过 不能直接keypair.publicKey.toString('hex'),不是buffer类型 @@ -632,7 +632,7 @@ class TicCrypto { */ static seckey_to_address ({ seckey, coin, world } = {}) { coin = coin?.toUpperCase() || my.COIN - if (this.is_seckey(seckey)) { + if (this.is_seckey({ seckey })) { /** @type {*} */ let pubkey if (coin === 'ETH') { @@ -1080,7 +1080,7 @@ class TicCrypto { */ static hash_to_sig_distance ({ hash, sig } = {}) { // hash为64hex字符,sig为128hex字符。返回用hex表达的距离。 - if (this.is_signature(sig) && this.is_hash({ hash })) { + if (this.is_signature({ sig: sig }) && this.is_hash({ hash })) { var hashSig = this.hash_easy(sig) // 把签名也转成32字节的哈希,同样长度方便比较 return new BigInt(hash, 16) .subtract(new BigInt(hashSig, 16)) @@ -1103,7 +1103,7 @@ class TicCrypto { static compare_signatures ({ hash, sig1, sig2 } = {}) { // 返回距离hash更近的sig if (this.is_hash({ hash })) { - if (this.is_signature(sig2) && this.is_signature(sig1)) { + if (this.is_signature({ sig: sig2 }) && this.is_signature({ sig: sig1 })) { var dis1 = this.hash_to_sig_distance({ hash, sig: sig1 }) var dis2 = this.hash_to_sig_distance({ hash, sig: sig2 }) if (dis1 < dis2) { @@ -1114,10 +1114,10 @@ class TicCrypto { // 如果极其巧合的距离相等,也可能是一个在左、一个在右,那就按 signature 本身的字符串排序来比较。 return sig1 < sig2 ? sig1 : sig1 === sig2 ? sig1 : sig2 } - } else if (this.is_signature(sig2)) { + } else if (this.is_signature({ sig: sig2 })) { // 允许其中一个signature是非法的,例如undefined return sig2 - } else if (this.is_signature(sig1)) { + } else if (this.is_signature({ sig: sig1 })) { return sig1 } } @@ -1136,7 +1136,7 @@ class TicCrypto { static sort_sig_list ({ hash, sigList } = {}) { if (Array.isArray(sigList) && this.is_hash({ hash })) { sigList.sort(function (sig1, sig2) { - if (this.is_signature(sig1) && this.is_signature(sig2)) { + if (this.is_signature({ sig: sig1 }) && this.is_signature({ sig: sig2 })) { var winner = this.compare_signatures({ hash, sig1, sig2 }) if (sig1 === sig2) return 0 else if (winner === sig1) return -1 @@ -1494,6 +1494,7 @@ class TicCrypto { } if (cidVersion === 1) { let fullHex = `01${multicodec[cidCodec]}${multialgo[cidAlgo]}${Number(cosh.length / 2).toString(16)}${cosh}` + console.log(fullHex) if (cidBase === 'b32') { return ( multibase[cidBase] + @@ -1506,10 +1507,6 @@ class TicCrypto { } } } - - static string_to_raw_cid (str) { - return this.cosh_to_cid({ cosh: this.hash_easy(str), cidVersion: 1, cidCodec: 'raw' }) - } } // 必须单独写 module.exports,不要和类定义写在一起,否则会导致 jsdoc 解析不到类内文档。