@fireproof/core 0.7.0 → 0.7.1

Sign up to get free protection for your applications and to get access to all the features.
@@ -14,7 +14,7 @@ import { nocache as cache } from 'prolly-trees/cache';
14
14
  import { Buffer } from 'buffer';
15
15
  import { rawSha1 as sha1sync } from '../sha1.js';
16
16
  // @ts-ignore
17
- import * as codec from 'encrypted-block';
17
+ import * as codec from '../encrypted-block.js';
18
18
  import { blocksToCarBlock, blocksToEncryptedCarBlock, blocksFromEncryptedCarBlock } from '../valet.js';
19
19
  const chunker = bf(30);
20
20
  const blockOpts = { cache, chunker, codec: dagcbor, hasher: sha256, compare };
@@ -232,6 +232,7 @@ export class Base {
232
232
  const got = await reader.get(cid);
233
233
  let useCodec = codec;
234
234
  if (cid.toString().indexOf('bafy') === 0) {
235
+ // @ts-ignore
235
236
  useCodec = dagcbor; // todo this is a dirty check
236
237
  }
237
238
  const decoded = await Block.decode({
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@fireproof/core",
3
- "version": "0.7.0",
3
+ "version": "0.7.1",
4
4
  "description": "Live data for React, accelerated by proofs, powered by IPFS",
5
5
  "main": "dist/src/fireproof.js",
6
6
  "module": "dist/src/fireproof.mjs",
@@ -45,9 +45,9 @@
45
45
  "charwise": "^3.0.1",
46
46
  "cross-fetch": "^3.1.6",
47
47
  "crypto-browserify": "^3.12.0",
48
- "encrypted-block": "jchris/encrypted-block#update",
49
48
  "idb": "^7.1.1",
50
49
  "ipld-hashmap": "^2.1.18",
50
+ "js-crypto-aes": "^1.0.4",
51
51
  "multiformats": "^11.0.1",
52
52
  "prolly-trees": "1.0.4",
53
53
  "randombytes": "^2.1.0",
package/src/crypto.js CHANGED
@@ -1,5 +1,5 @@
1
1
  // @ts-nocheck
2
- import * as codec from 'encrypted-block'
2
+ import * as codec from './encrypted-block.js'
3
3
  import {
4
4
  create,
5
5
  load
@@ -0,0 +1,57 @@
1
+ // from https://github.com/mikeal/encrypted-block
2
+ import randomBytes from 'randombytes'
3
+ import aes from 'js-crypto-aes'
4
+ import { CID } from 'multiformats'
5
+
6
+ const enc32 = value => {
7
+ value = +value
8
+ const buff = new Uint8Array(4)
9
+ buff[3] = (value >>> 24)
10
+ buff[2] = (value >>> 16)
11
+ buff[1] = (value >>> 8)
12
+ buff[0] = (value & 0xff)
13
+ return buff
14
+ }
15
+
16
+ const readUInt32LE = (buffer) => {
17
+ const offset = buffer.byteLength - 4
18
+ return ((buffer[offset]) |
19
+ (buffer[offset + 1] << 8) |
20
+ (buffer[offset + 2] << 16)) +
21
+ (buffer[offset + 3] * 0x1000000)
22
+ }
23
+
24
+ const encode = ({ iv, bytes }) => concat([iv, bytes])
25
+ const decode = bytes => {
26
+ const iv = bytes.subarray(0, 12)
27
+ bytes = bytes.slice(12)
28
+ return { iv, bytes }
29
+ }
30
+
31
+ const code = 0x300000 + 1337
32
+
33
+ const concat = buffers => Uint8Array.from(buffers.map(b => [...b]).flat())
34
+
35
+ const decrypt = async ({ key, value }) => {
36
+ let { bytes, iv } = value
37
+ bytes = await aes.decrypt(bytes, key, { name: 'AES-GCM', iv, tagLength: 16 })
38
+ const len = readUInt32LE(bytes.subarray(0, 4))
39
+ const cid = CID.decode(bytes.subarray(4, 4 + len))
40
+ bytes = bytes.subarray(4 + len)
41
+ return { cid, bytes }
42
+ }
43
+ const encrypt = async ({ key, cid, bytes }) => {
44
+ const len = enc32(cid.bytes.byteLength)
45
+ const iv = randomBytes(12)
46
+ const msg = concat([len, cid.bytes, bytes])
47
+ bytes = await aes.encrypt(msg, key, { name: 'AES-GCM', iv, tagLength: 16 })
48
+ return { value: { bytes, iv } }
49
+ }
50
+
51
+ const crypto = key => {
52
+ return { encrypt: opts => encrypt({ key, ...opts }), decrypt: opts => decrypt({ key, ...opts }) }
53
+ }
54
+
55
+ const name = 'mikeal@encrypted-block:aes-gcm'
56
+
57
+ export { encode, decode, code, name, encrypt, decrypt, crypto }
@@ -14,7 +14,7 @@ import { nocache as cache } from 'prolly-trees/cache'
14
14
  import { Buffer } from 'buffer'
15
15
  import { rawSha1 as sha1sync } from '../sha1.js'
16
16
  // @ts-ignore
17
- import * as codec from 'encrypted-block'
17
+ import * as codec from '../encrypted-block.js'
18
18
  import { blocksToCarBlock, blocksToEncryptedCarBlock, blocksFromEncryptedCarBlock } from '../valet.js'
19
19
 
20
20
  const chunker = bf(30)
@@ -247,6 +247,7 @@ export class Base {
247
247
  const got = await reader.get(cid)
248
248
  let useCodec = codec
249
249
  if (cid.toString().indexOf('bafy') === 0) {
250
+ // @ts-ignore
250
251
  useCodec = dagcbor // todo this is a dirty check
251
252
  }
252
253
  const decoded = await Block.decode({