From 450c6df357b623c0cce760cdbd1a31d53696b3aa Mon Sep 17 00:00:00 2001 From: Luk Date: Mon, 16 Mar 2026 14:17:15 +0800 Subject: [PATCH] add `content_to_cid` --- package.json | 1 + ticc.js | 7 +++++++ 2 files changed, 8 insertions(+) diff --git a/package.json b/package.json index bd14d3e..7cf3381 100644 --- a/package.json +++ b/package.json @@ -12,6 +12,7 @@ "eccrypto-js": "^5.4.0", "ethereum-rsa": "^1.0.5", "hdkey": "^2.0.1", + "ipfs-only-hash": "^4.0.0", "js-crypto-key-utils": "^1.0.4", "keccak": "^3.0.2", "secp256k1": "^4.0.3", diff --git a/ticc.js b/ticc.js index 3c334b6..841e910 100644 --- a/ticc.js +++ b/ticc.js @@ -16,6 +16,7 @@ const hdkey = require('hdkey') // https://github.com/cryptocoinjs/hdkey // 或 const secp256k1 = require('secp256k1') const base32encode = require('base32-encode') const base32decode = require('base32-decode') +const ipfsOnlyHash = require('ipfs-only-hash') const { TronWeb } = require('tronweb') // 全部以hex为默认输入输出格式,方便人的阅读,以及方便函数之间统一接口 @@ -1527,6 +1528,12 @@ class TicCrypto { return '04' + this.padStart(x.toString(16), 64, '0') + this.padStart(y.toString(16), 64, '0') } + static async content_to_cid ({ content, ...option }) { + // {content: 'hello world', cidVersion: 0} + // content can be a string, a file, a stream + return await ipfsOnlyHash.of(content, option) + } + // cahex: content address hex. 最核心的纯hex的内容地址,没有任何额外标记。同一个内容的 cahex 是唯一的,而 cid 是在 cahex 基础上有各种不同的编码,转成字符串形式。cid建议叫做 caid. static cid_to_cahex ({ cid }) { try {