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