This commit is contained in:
luk 2024-11-03 14:04:26 +08:00
parent 62acb1c453
commit af48ee6f10

62
ticc.js
View File

@ -1362,7 +1362,7 @@ class TicCrypto {
* @memberof TicCrypto * @memberof TicCrypto
*/ */
static b64t_to_hex (b64t) { static b64t_to_hex (b64t) {
if (/^[0-9a-zA-Z\._]+$/.test(b64t)) { if (my.REGEXP_ALPHABET.b64t.test(b64t)) {
return Buffer.from(this.b64t_to_b64(b64t), 'base64').toString('hex') return Buffer.from(this.b64t_to_b64(b64t), 'base64').toString('hex')
} }
return '' return ''
@ -1518,18 +1518,22 @@ class TicCrypto {
// cosh: content hash. 最核心的纯hex的内容地址没有任何额外标记。同一个内容的cosh是唯一的而cid是在cosh基础上有各种不同的编码。cid建议叫做 coid. // cosh: content hash. 最核心的纯hex的内容地址没有任何额外标记。同一个内容的cosh是唯一的而cid是在cosh基础上有各种不同的编码。cid建议叫做 coid.
static cid_to_cosh ({ cid }) { static cid_to_cosh ({ cid }) {
if (/^[Q1]/.test(cid)) { try {
return this.b58_to_hex(cid).slice(4) // 前2字节是 cid0 的字节序数标记 if (/^[Q1]/.test(cid)) {
} else if (/^[bB]/.test(cid)) { return this.b58_to_hex(cid).slice(4) // 前2字节是 cid0 的字节序数标记
return this.b32_to_hex(cid.substr(1)).slice(8) // 前4字节是 cid1 的标记 } else if (/^[bB]/.test(cid)) {
} else if (/^z/.test(cid)) { return this.b32_to_hex(cid.substr(1)).slice(8) // 前4字节是 cid1 的标记
return this.b58_to_hex(cid.substr(1)).slice(8) } else if (/^z/.test(cid)) {
} else if (/^[mMuU]/.test(cid)) { return this.b58_to_hex(cid.substr(1)).slice(8)
return Buffer.from(cid.substr(1), 'base64').toString('hex') } else if (/^[mMuU]/.test(cid)) {
} else if (/^[fF]/) { return Buffer.from(cid.substr(1), 'base64').toString('hex')
return cid.substr(9).toLowerCase() } else if (/^[fF]/) {
} else if (/^9/.test(cid)) { return cid.substr(9).toLowerCase()
return BigInt(cid.slice(1)).toString(16).slice(7) // toString(16) 后,去掉了 01551220... 的打头的 0所以只有7位需要跳过了 } else if (/^9/.test(cid)) {
return BigInt(cid.slice(1)).toString(16).slice(7) // toString(16) 后,去掉了 01551220... 的打头的 0所以只有7位需要跳过了
}
} catch {
return ''
} }
} }
@ -1609,21 +1613,23 @@ class TicCrypto {
static convert_pexid (key) { static convert_pexid (key) {
key = key.toLowerCase() key = key.toLowerCase()
let pextokenCid, pextokenCosh, nftToid let pextokenCid, pextokenCosh, nftToid, tokenURI
if (key.length < 64 && /^bafkrei/.test(key)) { try {
pextokenCid = key if (key.length < 64 && /^bafkrei/.test(key)) {
pextokenCosh = this.cid_to_cosh({ cid: pextokenCid }) pextokenCid = key
nftToid = BigInt('0x' + pextokenCosh).toString() pextokenCosh = this.cid_to_cosh({ cid: pextokenCid })
} else if (key.length > 64 && /^\d+$/.test(key)) { nftToid = BigInt('0x' + pextokenCosh).toString()
nftToid = key } else if (key.length > 64 && /^\d+$/.test(key)) {
pextokenCosh = BigInt(nftToid).toString(16) nftToid = key
pextokenCid = this.cosh_to_cid({ cosh: pextokenCosh }) pextokenCosh = BigInt(nftToid).toString(16)
} else if (/^[0-9a-f]{64}$/.test(key)) { pextokenCid = this.cosh_to_cid({ cosh: pextokenCosh })
pextokenCosh = key } else if (/^[0-9a-f]{64}$/.test(key)) {
pextokenCid = this.cosh_to_cid({ cosh: pextokenCosh }) pextokenCosh = key
nftToid = BigInt('0x' + pextokenCosh).toString() pextokenCid = this.cosh_to_cid({ cosh: pextokenCosh })
} nftToid = BigInt('0x' + pextokenCosh).toString()
const tokenURI = 'https://ipfs.tic.cc/ipfs/f01551220' + pextokenCosh }
tokenURI = pextokenCosh ? `https://ipfs.tic.cc/ipfs/f01551220${pextokenCosh}` : undefined
} catch {}
return { return {
pextokenCid, pextokenCid,
pextokenCosh, pextokenCosh,