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

12
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,6 +1518,7 @@ class TicCrypto {
// cosh: content hash. 最核心的纯hex的内容地址没有任何额外标记。同一个内容的cosh是唯一的而cid是在cosh基础上有各种不同的编码。cid建议叫做 coid.
static cid_to_cosh ({ cid }) {
try {
if (/^[Q1]/.test(cid)) {
return this.b58_to_hex(cid).slice(4) // 前2字节是 cid0 的字节序数标记
} else if (/^[bB]/.test(cid)) {
@ -1531,6 +1532,9 @@ class TicCrypto {
} else if (/^9/.test(cid)) {
return BigInt(cid.slice(1)).toString(16).slice(7) // toString(16) 后,去掉了 01551220... 的打头的 0所以只有7位需要跳过了
}
} catch {
return ''
}
}
static cosh_to_cid ({ cosh, cidBase = 'b32', cidVersion = 1, cidCodec = 'raw', cidAlgo = 'sha256' }) {
@ -1609,7 +1613,8 @@ class TicCrypto {
static convert_pexid (key) {
key = key.toLowerCase()
let pextokenCid, pextokenCosh, nftToid
let pextokenCid, pextokenCosh, nftToid, tokenURI
try {
if (key.length < 64 && /^bafkrei/.test(key)) {
pextokenCid = key
pextokenCosh = this.cid_to_cosh({ cid: pextokenCid })
@ -1623,7 +1628,8 @@ class TicCrypto {
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 {
pextokenCid,
pextokenCosh,