Skip to content

Commit

Permalink
remove file from pr
Browse files Browse the repository at this point in the history
  • Loading branch information
jchris committed Jun 11, 2024
1 parent c20c006 commit 8e5a3f4
Showing 1 changed file with 15 additions and 51 deletions.
66 changes: 15 additions & 51 deletions packages/encrypted-blockstore/src/encrypt-helpers.ts
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@ import * as dagcbor from '@ipld/dag-cbor'

import { MemoryBlockstore } from '@web3-storage/pail/block'

// @ts-ignorex
// @ts-ignore
import { bf } from 'prolly-trees/utils'
// @ts-ignore
import { nocache as cache } from 'prolly-trees/cache'
Expand All @@ -17,13 +17,7 @@ import { create, load } from 'prolly-trees/cid-set'

import { encodeCarFile } from './loader-helpers'
import { makeCodec } from './encrypt-codec.js'
import type {
AnyBlock,
CarMakeable,
AnyLink,
AnyDecodedBlock,
CryptoOpts
} from './types'
import type { AnyBlock, CarMakeable, AnyLink, AnyDecodedBlock, CryptoOpts } from './types'

function makeEncDec(crypto: any, randomBytes: (size: number) => Uint8Array) {
const codec = makeCodec(crypto, randomBytes)
Expand Down Expand Up @@ -57,17 +51,10 @@ function makeEncDec(crypto: any, randomBytes: (size: number) => Uint8Array) {
if (unencrypted.cid.equals(root)) eroot = block.cid
}
if (!eroot) throw new Error('cids does not include root')
const list = [...set].map((s) => CID.parse(s))
const list = [...set].map(s => CID.parse(s))
let last
// eslint-disable-next-line @typescript-eslint/no-unsafe-call
for await (const node of create({
list,
get,
cache,
chunker,
hasher,
codec: dagcbor
})) {
for await (const node of create({ list, get, cache, chunker, hasher, codec: dagcbor })) {
// eslint-disable-next-line @typescript-eslint/no-unsafe-member-access
const block = (await node.block) as AnyBlock
yield block
Expand Down Expand Up @@ -95,13 +82,13 @@ function makeEncDec(crypto: any, randomBytes: (size: number) => Uint8Array) {
hasher: MultihashHasher<number>
}): AsyncGenerator<AnyBlock, void, undefined> {
const getWithDecode = async (cid: AnyLink) =>
get(cid).then(async (block) => {
get(cid).then(async block => {
if (!block) return
const decoded = await decode({ ...block, codec: dagcbor, hasher })
return decoded
})
const getWithDecrypt = async (cid: AnyLink) =>
get(cid).then(async (block) => {
get(cid).then(async block => {
if (!block) return
const decoded = await decode({ ...block, codec, hasher })
return decoded
Expand All @@ -115,39 +102,23 @@ function makeEncDec(crypto: any, randomBytes: (size: number) => Uint8Array) {
const rootBlock = (await get(eroot)) as AnyDecodedBlock
if (!rootBlock) throw new Error('missing root block')
// eslint-disable-next-line @typescript-eslint/no-unsafe-assignment, @typescript-eslint/no-unsafe-call
const cidset = await load({
cid: tree,
get: getWithDecode,
cache,
chunker,
codec,
hasher
})
const cidset = await load({ cid: tree, get: getWithDecode, cache, chunker, codec, hasher })
// eslint-disable-next-line @typescript-eslint/no-unsafe-member-access, @typescript-eslint/no-unsafe-call
const { result: nodes } = (await cidset.getAllEntries()) as {
result: { cid: CID }[]
}
const { result: nodes } = (await cidset.getAllEntries()) as { result: { cid: CID }[] }
const unwrap = async (eblock: AnyDecodedBlock | undefined) => {
if (!eblock) throw new Error('missing block')
if (!eblock.value) {
eblock = (await decode({
...eblock,
codec,
hasher
})) as AnyDecodedBlock
eblock = (await decode({ ...eblock, codec, hasher })) as AnyDecodedBlock
}
const { bytes, cid } = await codec
.decrypt({ ...eblock, key })
.catch((e) => {
throw e
})
const { bytes, cid } = await codec.decrypt({ ...eblock, key }).catch(e => {
throw e
})
const block = await mfCreate({ cid, bytes, hasher, codec })
return block
}
const promises = []
for (const { cid } of nodes) {
if (!rootBlock.cid.equals(cid))
promises.push(getWithDecrypt(cid).then(unwrap))
if (!rootBlock.cid.equals(cid)) promises.push(getWithDecrypt(cid).then(unwrap))
}
yield* promises
yield unwrap(rootBlock)
Expand Down Expand Up @@ -202,11 +173,7 @@ export async function encryptedEncodeCarFile(
return encryptedCar
}

export async function decodeEncryptedCar(
crypto: CryptoOpts,
key: string,
reader: CarReader
) {
export async function decodeEncryptedCar(crypto: CryptoOpts, key: string, reader: CarReader) {
const roots = await reader.getRoots()
const root = roots[0]
return await decodeCarBlocks(crypto, root, reader.get.bind(reader), key)
Expand All @@ -218,10 +185,7 @@ async function decodeCarBlocks(
keyMaterial: string
): Promise<{ blocks: MemoryBlockstore; root: AnyLink }> {
const decryptionKeyUint8 = hexStringToUint8Array(keyMaterial)
const decryptionKey = decryptionKeyUint8.buffer.slice(
0,
decryptionKeyUint8.byteLength
)
const decryptionKey = decryptionKeyUint8.buffer.slice(0, decryptionKeyUint8.byteLength)

const decryptedBlocks = new MemoryBlockstore()
let last: AnyBlock | null = null
Expand Down

0 comments on commit 8e5a3f4

Please sign in to comment.