From 6ff8a84ed8fb062dd7e08149d7b5a2a7d5cd7e36 Mon Sep 17 00:00:00 2001 From: Derrick Hammer Date: Fri, 17 Nov 2023 08:04:01 -0500 Subject: [PATCH] feat: add encrypted_cid.ts --- src/encrypted_cid.ts | 67 ++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 67 insertions(+) create mode 100644 src/encrypted_cid.ts diff --git a/src/encrypted_cid.ts b/src/encrypted_cid.ts new file mode 100644 index 0000000..d37f630 --- /dev/null +++ b/src/encrypted_cid.ts @@ -0,0 +1,67 @@ +import Multibase from "#multibase.js"; +import { Multihash } from "#multihash.js"; +import CID from "#cid.js"; +import { CID_TYPES } from "#constants.js"; +import { decodeEndian, encodeEndian } from "#util.js"; + +export default class EncryptedCID extends Multibase { + encryptedBlobHash: Multihash; + originalCID: CID; + encryptionAlgorithm: number; + padding: number; + chunkSizeAsPowerOf2: number; + encryptionKey: Uint8Array; + + constructor( + encryptedBlobHash: Multihash, + originalCID: CID, + encryptionKey: Uint8Array, + padding: number, + chunkSizeAsPowerOf2: number, + encryptionAlgorithm: number, + ) { + super(); + this.encryptedBlobHash = encryptedBlobHash; + this.originalCID = originalCID; + this.encryptionKey = encryptionKey; + this.padding = padding; + this.chunkSizeAsPowerOf2 = chunkSizeAsPowerOf2; + this.encryptionAlgorithm = encryptionAlgorithm; + } + + static decode(cid: string): EncryptedCID { + return EncryptedCID.fromBytes(Multibase.decodeString(cid)); + } + + static fromBytes(bytes: Uint8Array): EncryptedCID { + if (bytes[0] !== CID_TYPES.ENCRYPTED_DYNAMIC) { + throw new Error(`Invalid CID type (${bytes[0]})`); + } + + return new EncryptedCID( + new Multihash(bytes.slice(3, 36)), + CID.fromBytes(bytes.slice(72)), + bytes.slice(36, 68), + decodeEndian(bytes.slice(68, 72)), + bytes[2], + bytes[1], + ); + } + + get chunkSize(): number { + return Math.pow(2, this.chunkSizeAsPowerOf2); + } + + toBytes(): Uint8Array { + const data = [ + CID_TYPES.ENCRYPTED_STATIC, + this.encryptionAlgorithm, + this.chunkSizeAsPowerOf2, + ...this.encryptedBlobHash.fullBytes, + ...this.encryptionKey, + ...encodeEndian(this.padding, 4), + ...this.originalCID.toBytes(), + ]; + return new Uint8Array(data); + } +}