把大多数ticc方法的参数类型统一为 { ... } 对象

This commit is contained in:
陆柯 2022-08-03 16:30:09 +08:00
parent 0cb2547d17
commit 34cf26e168

31
ticc.js
View File

@ -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 解析不到类内文档。