Skip to content

Commit

Permalink
changed prettier settings
Browse files Browse the repository at this point in the history
  • Loading branch information
Dhruv Soni authored and Dhruv Soni committed Jun 11, 2024
1 parent 466381b commit c20c006
Show file tree
Hide file tree
Showing 3 changed files with 79 additions and 79 deletions.
64 changes: 32 additions & 32 deletions packages/encrypted-blockstore/src/encrypt-helpers.ts
Original file line number Diff line number Diff line change
@@ -1,29 +1,29 @@
import { sha256 } from "multiformats/hashes/sha2"
import { CID } from "multiformats"
import { encode, decode, create as mfCreate } from "multiformats/block"
import type { MultihashHasher, ToString } from "multiformats"
import { sha256 } from 'multiformats/hashes/sha2'
import { CID } from 'multiformats'
import { encode, decode, create as mfCreate } from 'multiformats/block'
import type { MultihashHasher, ToString } from 'multiformats'

import type { CarReader } from "@ipld/car"
import * as dagcbor from "@ipld/dag-cbor"
import type { CarReader } from '@ipld/car'
import * as dagcbor from '@ipld/dag-cbor'

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

// @ts-ignorex
import { bf } from "prolly-trees/utils"
import { bf } from 'prolly-trees/utils'
// @ts-ignore
import { nocache as cache } from "prolly-trees/cache"
import { nocache as cache } from 'prolly-trees/cache'
// @ts-ignore
import { create, load } from "prolly-trees/cid-set"
import { create, load } from 'prolly-trees/cid-set'

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

function makeEncDec(crypto: any, randomBytes: (size: number) => Uint8Array) {
const codec = makeCodec(crypto, randomBytes)
Expand All @@ -35,7 +35,7 @@ function makeEncDec(crypto: any, randomBytes: (size: number) => Uint8Array) {
key,
cache,
chunker,
root,
root
}: {
get: (cid: AnyLink) => Promise<AnyBlock | undefined>
key: ArrayBuffer
Expand All @@ -49,14 +49,14 @@ function makeEncDec(crypto: any, randomBytes: (size: number) => Uint8Array) {
let eroot
for (const cid of cids) {
const unencrypted = await get(cid)
if (!unencrypted) throw new Error("missing cid: " + cid.toString())
if (!unencrypted) throw new Error('missing cid: ' + cid.toString())
const encrypted = await codec.encrypt({ ...unencrypted, key })
const block = await encode({ ...encrypted, codec, hasher })
yield block
set.add(block.cid.toString())
if (unencrypted.cid.equals(root)) eroot = block.cid
}
if (!eroot) throw new Error("cids does not include root")
if (!eroot) throw new Error('cids does not include root')
const list = [...set].map((s) => CID.parse(s))
let last
// eslint-disable-next-line @typescript-eslint/no-unsafe-call
Expand All @@ -66,14 +66,14 @@ function makeEncDec(crypto: any, randomBytes: (size: number) => Uint8Array) {
cache,
chunker,
hasher,
codec: dagcbor,
codec: dagcbor
})) {
// eslint-disable-next-line @typescript-eslint/no-unsafe-member-access
const block = (await node.block) as AnyBlock
yield block
last = block
}
if (!last) throw new Error("missing last block")
if (!last) throw new Error('missing last block')
const head = [eroot, last.cid]
const block = await encode({ value: head, codec: dagcbor, hasher })
yield block
Expand All @@ -85,7 +85,7 @@ function makeEncDec(crypto: any, randomBytes: (size: number) => Uint8Array) {
key,
cache,
chunker,
hasher,
hasher
}: {
root: AnyLink
get: (cid: AnyLink) => Promise<AnyBlock | undefined>
Expand All @@ -107,33 +107,33 @@ function makeEncDec(crypto: any, randomBytes: (size: number) => Uint8Array) {
return decoded
})
const decodedRoot = await getWithDecode(root)
if (!decodedRoot) throw new Error("missing root")
if (!decodedRoot.bytes) throw new Error("missing bytes")
if (!decodedRoot) throw new Error('missing root')
if (!decodedRoot.bytes) throw new Error('missing bytes')
const {
value: [eroot, tree],
value: [eroot, tree]
} = decodedRoot as { value: [AnyLink, AnyLink] }
const rootBlock = (await get(eroot)) as AnyDecodedBlock
if (!rootBlock) throw new Error("missing root block")
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,
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 unwrap = async (eblock: AnyDecodedBlock | undefined) => {
if (!eblock) throw new Error("missing block")
if (!eblock) throw new Error('missing block')
if (!eblock.value) {
eblock = (await decode({
...eblock,
codec,
hasher,
hasher
})) as AnyDecodedBlock
}
const { bytes, cid } = await codec
Expand Down Expand Up @@ -178,7 +178,7 @@ export async function encryptedEncodeCarFile(
for (const { cid } of t.entries()) {
cidsToEncrypt.push(cid)
const g = await t.get(cid)
if (!g) throw new Error("missing cid block")
if (!g) throw new Error('missing cid block')
}
let last: AnyBlock | null = null
const { encrypt } = makeEncDec(crypto.crypto, crypto.randomBytes)
Expand All @@ -192,12 +192,12 @@ export async function encryptedEncodeCarFile(
chunker,
// eslint-disable-next-line @typescript-eslint/no-unsafe-assignment
cache,
root: rootCid,
root: rootCid
}) as AsyncGenerator<AnyBlock, void, unknown>) {
await encryptedBlocks.put(block.cid, block.bytes)
last = block
}
if (!last) throw new Error("no blocks encrypted")
if (!last) throw new Error('no blocks encrypted')
const encryptedCar = await encodeCarFile([last.cid], encryptedBlocks)
return encryptedCar
}
Expand Down Expand Up @@ -236,11 +236,11 @@ async function decodeCarBlocks(
// eslint-disable-next-line @typescript-eslint/no-unsafe-assignment
chunker,
// eslint-disable-next-line @typescript-eslint/no-unsafe-assignment
cache,
cache
})) {
await decryptedBlocks.put(block.cid, block.bytes)
last = block
}
if (!last) throw new Error("no blocks decrypted")
if (!last) throw new Error('no blocks decrypted')
return { blocks: decryptedBlocks, root: last.cid }
}
22 changes: 11 additions & 11 deletions packages/encrypted-blockstore/src/loader-helpers.ts
Original file line number Diff line number Diff line change
@@ -1,12 +1,12 @@
import { CID } from "multiformats"
import { Block, encode, decode } from "multiformats/block"
import { sha256 as hasher } from "multiformats/hashes/sha2"
import * as raw from "multiformats/codecs/raw"
import * as CBW from "@ipld/car/buffer-writer"
import * as codec from "@ipld/dag-cbor"
import { CarReader } from "@ipld/car"
import { CID } from 'multiformats'
import { Block, encode, decode } from 'multiformats/block'
import { sha256 as hasher } from 'multiformats/hashes/sha2'
import * as raw from 'multiformats/codecs/raw'
import * as CBW from '@ipld/car/buffer-writer'
import * as codec from '@ipld/dag-cbor'
import { CarReader } from '@ipld/car'

import { AnyBlock, AnyLink, CarHeader, CarMakeable } from "./types"
import { AnyBlock, AnyLink, CarHeader, CarMakeable } from './types'

export async function encodeCarFile(
roots: AnyLink[],
Expand Down Expand Up @@ -42,17 +42,17 @@ export async function encodeCarHeader(fp: CarHeader) {
return (await encode({
value: { fp },
hasher,
codec,
codec
})) as AnyBlock
}

export async function parseCarFile(reader: CarReader): Promise<CarHeader> {
const roots = await reader.getRoots()
const header = await reader.get(roots[0])
if (!header) throw new Error("missing header block")
if (!header) throw new Error('missing header block')
const { value } = await decode({ bytes: header.bytes, hasher, codec })
// @ts-ignore
if (value && value.fp === undefined) throw new Error("missing fp")
if (value && value.fp === undefined) throw new Error('missing fp')
const { fp } = value as { fp: CarHeader }
return fp
}
Loading

0 comments on commit c20c006

Please sign in to comment.