@atproto/repo 0.0.1 → 0.1.0

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
Files changed (63) hide show
  1. package/bench/mst.bench.ts +7 -4
  2. package/bench/repo.bench.ts +25 -16
  3. package/dist/block-map.d.ts +25 -0
  4. package/dist/data-diff.d.ts +36 -0
  5. package/dist/error.d.ts +20 -0
  6. package/dist/index.d.ts +3 -1
  7. package/dist/index.js +11605 -10399
  8. package/dist/index.js.map +4 -4
  9. package/dist/mst/diff.d.ts +4 -33
  10. package/dist/mst/mst.d.ts +68 -25
  11. package/dist/mst/util.d.ts +13 -5
  12. package/dist/parse.d.ts +16 -0
  13. package/dist/readable-repo.d.ts +22 -0
  14. package/dist/repo.d.ts +14 -30
  15. package/dist/storage/index.d.ts +4 -0
  16. package/dist/storage/memory-blockstore.d.ts +28 -0
  17. package/dist/storage/readable-blockstore.d.ts +24 -0
  18. package/dist/storage/repo-storage.d.ts +18 -0
  19. package/dist/storage/sync-storage.d.ts +15 -0
  20. package/dist/storage/types.d.ts +3 -0
  21. package/dist/sync/consumer.d.ts +18 -0
  22. package/dist/sync/index.d.ts +2 -0
  23. package/dist/sync/provider.d.ts +9 -0
  24. package/dist/types.d.ts +124 -317
  25. package/dist/util.d.ts +31 -12
  26. package/dist/verify.d.ts +26 -4
  27. package/package.json +4 -2
  28. package/src/block-map.ts +95 -0
  29. package/src/cid-set.ts +1 -2
  30. package/src/data-diff.ts +121 -0
  31. package/src/error.ts +31 -0
  32. package/src/index.ts +3 -1
  33. package/src/mst/diff.ts +120 -90
  34. package/src/mst/mst.ts +185 -184
  35. package/src/mst/util.ts +54 -31
  36. package/src/parse.ts +44 -0
  37. package/src/readable-repo.ts +75 -0
  38. package/src/repo.ts +119 -249
  39. package/src/storage/index.ts +4 -0
  40. package/src/storage/memory-blockstore.ts +114 -0
  41. package/src/storage/readable-blockstore.ts +56 -0
  42. package/src/storage/repo-storage.ts +42 -0
  43. package/src/storage/sync-storage.ts +35 -0
  44. package/src/storage/types.ts +3 -0
  45. package/src/sync/consumer.ts +137 -0
  46. package/src/sync/index.ts +2 -0
  47. package/src/sync/provider.ts +91 -0
  48. package/src/types.ts +101 -62
  49. package/src/util.ts +237 -56
  50. package/src/verify.ts +207 -42
  51. package/tests/_util.ts +132 -97
  52. package/tests/mst.test.ts +269 -122
  53. package/tests/repo.test.ts +48 -50
  54. package/tests/sync/checkout.test.ts +57 -0
  55. package/tests/sync/diff.test.ts +87 -0
  56. package/tests/sync/narrow.test.ts +145 -0
  57. package/tsconfig.build.tsbuildinfo +1 -1
  58. package/tsconfig.json +2 -1
  59. package/src/blockstore/index.ts +0 -2
  60. package/src/blockstore/ipld-store.ts +0 -103
  61. package/src/blockstore/memory-blockstore.ts +0 -49
  62. package/src/sync.ts +0 -38
  63. package/tests/sync.test.ts +0 -129
package/dist/types.d.ts CHANGED
@@ -1,355 +1,159 @@
1
1
  import { z } from 'zod';
2
2
  import { BlockWriter } from '@ipld/car/writer';
3
3
  import { CID } from 'multiformats';
4
- import { DataDiff } from './mst';
5
- declare const repoMeta: z.ZodObject<{
4
+ import BlockMap from './block-map';
5
+ import { RepoRecord } from '@atproto/lexicon';
6
+ declare const unsignedCommit: z.ZodObject<{
6
7
  did: z.ZodString;
7
8
  version: z.ZodNumber;
8
- datastore: z.ZodString;
9
- }, "strip", z.ZodTypeAny, {
10
- version: number;
11
- did: string;
12
- datastore: string;
13
- }, {
14
- version: number;
15
- did: string;
16
- datastore: string;
17
- }>;
18
- export declare type RepoMeta = z.infer<typeof repoMeta>;
19
- declare const repoRoot: z.ZodObject<{
20
- meta: z.ZodEffects<z.ZodEffects<z.ZodAny, any, any>, CID, any>;
21
9
  prev: z.ZodNullable<z.ZodEffects<z.ZodEffects<z.ZodAny, any, any>, CID, any>>;
22
- auth_token: z.ZodNullable<z.ZodEffects<z.ZodEffects<z.ZodAny, any, any>, CID, any>>;
23
10
  data: z.ZodEffects<z.ZodEffects<z.ZodAny, any, any>, CID, any>;
24
11
  }, "strip", z.ZodTypeAny, {
25
12
  data: CID;
26
- meta: CID;
13
+ did: string;
14
+ version: number;
27
15
  prev: CID | null;
28
- auth_token: CID | null;
29
16
  }, {
30
17
  data?: any;
31
- meta?: any;
32
18
  prev?: any;
33
- auth_token?: any;
19
+ did: string;
20
+ version: number;
34
21
  }>;
35
- export declare type RepoRoot = z.infer<typeof repoRoot>;
22
+ export declare type UnsignedCommit = z.infer<typeof unsignedCommit> & {
23
+ sig?: never;
24
+ };
36
25
  declare const commit: z.ZodObject<{
37
- root: z.ZodEffects<z.ZodEffects<z.ZodAny, any, any>, CID, any>;
26
+ did: z.ZodString;
27
+ version: z.ZodNumber;
28
+ prev: z.ZodNullable<z.ZodEffects<z.ZodEffects<z.ZodAny, any, any>, CID, any>>;
29
+ data: z.ZodEffects<z.ZodEffects<z.ZodAny, any, any>, CID, any>;
38
30
  sig: z.ZodType<Uint8Array, z.ZodTypeDef, Uint8Array>;
39
31
  }, "strip", z.ZodTypeAny, {
40
- root: CID;
32
+ data: CID;
33
+ did: string;
34
+ version: number;
35
+ prev: CID | null;
41
36
  sig: Uint8Array;
42
37
  }, {
43
- root?: any;
38
+ data?: any;
39
+ prev?: any;
40
+ did: string;
41
+ version: number;
44
42
  sig: Uint8Array;
45
43
  }>;
46
44
  export declare type Commit = z.infer<typeof commit>;
47
- export declare const cidCreateOp: z.ZodObject<{
48
- action: z.ZodLiteral<"create">;
49
- collection: z.ZodString;
50
- rkey: z.ZodString;
51
- cid: z.ZodEffects<z.ZodEffects<z.ZodAny, any, any>, CID, any>;
52
- }, "strip", z.ZodTypeAny, {
53
- collection: string;
54
- action: "create";
55
- rkey: string;
56
- cid: CID;
57
- }, {
58
- cid?: any;
59
- collection: string;
60
- action: "create";
61
- rkey: string;
62
- }>;
63
- export declare type CidCreateOp = z.infer<typeof cidCreateOp>;
64
- export declare const cidUpdateOp: z.ZodObject<{
65
- action: z.ZodLiteral<"update">;
66
- collection: z.ZodString;
67
- rkey: z.ZodString;
68
- cid: z.ZodEffects<z.ZodEffects<z.ZodAny, any, any>, CID, any>;
69
- }, "strip", z.ZodTypeAny, {
70
- collection: string;
71
- action: "update";
72
- rkey: string;
73
- cid: CID;
74
- }, {
75
- cid?: any;
76
- collection: string;
77
- action: "update";
78
- rkey: string;
79
- }>;
80
- export declare type CidUpdateOp = z.infer<typeof cidUpdateOp>;
81
- export declare const deleteOp: z.ZodObject<{
82
- action: z.ZodLiteral<"delete">;
83
- collection: z.ZodString;
84
- rkey: z.ZodString;
85
- }, "strip", z.ZodTypeAny, {
86
- collection: string;
87
- action: "delete";
88
- rkey: string;
89
- }, {
90
- collection: string;
91
- action: "delete";
92
- rkey: string;
93
- }>;
94
- export declare type DeleteOp = z.infer<typeof deleteOp>;
95
- export declare const cidWriteOp: z.ZodUnion<[z.ZodObject<{
96
- action: z.ZodLiteral<"create">;
97
- collection: z.ZodString;
98
- rkey: z.ZodString;
99
- cid: z.ZodEffects<z.ZodEffects<z.ZodAny, any, any>, CID, any>;
100
- }, "strip", z.ZodTypeAny, {
101
- collection: string;
102
- action: "create";
103
- rkey: string;
104
- cid: CID;
105
- }, {
106
- cid?: any;
107
- collection: string;
108
- action: "create";
109
- rkey: string;
110
- }>, z.ZodObject<{
111
- action: z.ZodLiteral<"update">;
112
- collection: z.ZodString;
113
- rkey: z.ZodString;
114
- cid: z.ZodEffects<z.ZodEffects<z.ZodAny, any, any>, CID, any>;
115
- }, "strip", z.ZodTypeAny, {
116
- collection: string;
117
- action: "update";
118
- rkey: string;
119
- cid: CID;
120
- }, {
121
- cid?: any;
122
- collection: string;
123
- action: "update";
124
- rkey: string;
125
- }>, z.ZodObject<{
126
- action: z.ZodLiteral<"delete">;
127
- collection: z.ZodString;
128
- rkey: z.ZodString;
129
- }, "strip", z.ZodTypeAny, {
130
- collection: string;
131
- action: "delete";
132
- rkey: string;
133
- }, {
134
- collection: string;
135
- action: "delete";
136
- rkey: string;
137
- }>]>;
138
- export declare type CidWriteOp = z.infer<typeof cidWriteOp>;
139
- export declare const recordCreateOp: z.ZodObject<{
140
- action: z.ZodLiteral<"create">;
141
- collection: z.ZodString;
142
- rkey: z.ZodString;
143
- value: z.ZodAny;
144
- }, "strip", z.ZodTypeAny, {
145
- value?: any;
146
- collection: string;
147
- action: "create";
148
- rkey: string;
149
- }, {
150
- value?: any;
151
- collection: string;
152
- action: "create";
153
- rkey: string;
154
- }>;
155
- export declare type RecordCreateOp = z.infer<typeof recordCreateOp>;
156
- export declare const recordUpdateOp: z.ZodObject<{
157
- action: z.ZodLiteral<"update">;
158
- collection: z.ZodString;
159
- rkey: z.ZodString;
160
- value: z.ZodAny;
161
- }, "strip", z.ZodTypeAny, {
162
- value?: any;
163
- collection: string;
164
- action: "update";
165
- rkey: string;
166
- }, {
167
- value?: any;
168
- collection: string;
169
- action: "update";
170
- rkey: string;
171
- }>;
172
- export declare type RecordUpdateOp = z.infer<typeof recordUpdateOp>;
173
- export declare const recordWriteOp: z.ZodUnion<[z.ZodObject<{
174
- action: z.ZodLiteral<"create">;
175
- collection: z.ZodString;
176
- rkey: z.ZodString;
177
- value: z.ZodAny;
178
- }, "strip", z.ZodTypeAny, {
179
- value?: any;
180
- collection: string;
181
- action: "create";
182
- rkey: string;
183
- }, {
184
- value?: any;
185
- collection: string;
186
- action: "create";
187
- rkey: string;
188
- }>, z.ZodObject<{
189
- action: z.ZodLiteral<"update">;
190
- collection: z.ZodString;
191
- rkey: z.ZodString;
192
- value: z.ZodAny;
193
- }, "strip", z.ZodTypeAny, {
194
- value?: any;
195
- collection: string;
196
- action: "update";
197
- rkey: string;
198
- }, {
199
- value?: any;
200
- collection: string;
201
- action: "update";
202
- rkey: string;
203
- }>, z.ZodObject<{
204
- action: z.ZodLiteral<"delete">;
205
- collection: z.ZodString;
206
- rkey: z.ZodString;
207
- }, "strip", z.ZodTypeAny, {
208
- collection: string;
209
- action: "delete";
210
- rkey: string;
211
- }, {
212
- collection: string;
213
- action: "delete";
214
- rkey: string;
215
- }>]>;
216
- export declare type RecordWriteOp = z.infer<typeof recordWriteOp>;
217
- export declare const def: {
218
- repoMeta: z.ZodObject<{
45
+ export declare const schema: {
46
+ commit: z.ZodObject<{
219
47
  did: z.ZodString;
220
48
  version: z.ZodNumber;
221
- datastore: z.ZodString;
222
- }, "strip", z.ZodTypeAny, {
223
- version: number;
224
- did: string;
225
- datastore: string;
226
- }, {
227
- version: number;
228
- did: string;
229
- datastore: string;
230
- }>;
231
- repoRoot: z.ZodObject<{
232
- meta: z.ZodEffects<z.ZodEffects<z.ZodAny, any, any>, CID, any>;
233
49
  prev: z.ZodNullable<z.ZodEffects<z.ZodEffects<z.ZodAny, any, any>, CID, any>>;
234
- auth_token: z.ZodNullable<z.ZodEffects<z.ZodEffects<z.ZodAny, any, any>, CID, any>>;
235
50
  data: z.ZodEffects<z.ZodEffects<z.ZodAny, any, any>, CID, any>;
51
+ sig: z.ZodType<Uint8Array, z.ZodTypeDef, Uint8Array>;
236
52
  }, "strip", z.ZodTypeAny, {
237
53
  data: CID;
238
- meta: CID;
54
+ did: string;
55
+ version: number;
239
56
  prev: CID | null;
240
- auth_token: CID | null;
57
+ sig: Uint8Array;
241
58
  }, {
242
59
  data?: any;
243
- meta?: any;
244
60
  prev?: any;
245
- auth_token?: any;
246
- }>;
247
- commit: z.ZodObject<{
248
- root: z.ZodEffects<z.ZodEffects<z.ZodAny, any, any>, CID, any>;
249
- sig: z.ZodType<Uint8Array, z.ZodTypeDef, Uint8Array>;
250
- }, "strip", z.ZodTypeAny, {
251
- root: CID;
252
- sig: Uint8Array;
253
- }, {
254
- root?: any;
61
+ did: string;
62
+ version: number;
255
63
  sig: Uint8Array;
256
64
  }>;
257
- cidWriteOp: z.ZodUnion<[z.ZodObject<{
258
- action: z.ZodLiteral<"create">;
259
- collection: z.ZodString;
260
- rkey: z.ZodString;
261
- cid: z.ZodEffects<z.ZodEffects<z.ZodAny, any, any>, CID, any>;
262
- }, "strip", z.ZodTypeAny, {
263
- collection: string;
264
- action: "create";
265
- rkey: string;
266
- cid: CID;
267
- }, {
268
- cid?: any;
269
- collection: string;
270
- action: "create";
271
- rkey: string;
272
- }>, z.ZodObject<{
273
- action: z.ZodLiteral<"update">;
274
- collection: z.ZodString;
275
- rkey: z.ZodString;
276
- cid: z.ZodEffects<z.ZodEffects<z.ZodAny, any, any>, CID, any>;
277
- }, "strip", z.ZodTypeAny, {
278
- collection: string;
279
- action: "update";
280
- rkey: string;
281
- cid: CID;
282
- }, {
283
- cid?: any;
284
- collection: string;
285
- action: "update";
286
- rkey: string;
287
- }>, z.ZodObject<{
288
- action: z.ZodLiteral<"delete">;
289
- collection: z.ZodString;
290
- rkey: z.ZodString;
291
- }, "strip", z.ZodTypeAny, {
292
- collection: string;
293
- action: "delete";
294
- rkey: string;
295
- }, {
296
- collection: string;
297
- action: "delete";
298
- rkey: string;
299
- }>]>;
300
- recordWriteOp: z.ZodUnion<[z.ZodObject<{
301
- action: z.ZodLiteral<"create">;
302
- collection: z.ZodString;
303
- rkey: z.ZodString;
304
- value: z.ZodAny;
305
- }, "strip", z.ZodTypeAny, {
306
- value?: any;
307
- collection: string;
308
- action: "create";
309
- rkey: string;
310
- }, {
311
- value?: any;
312
- collection: string;
313
- action: "create";
314
- rkey: string;
315
- }>, z.ZodObject<{
316
- action: z.ZodLiteral<"update">;
317
- collection: z.ZodString;
318
- rkey: z.ZodString;
319
- value: z.ZodAny;
320
- }, "strip", z.ZodTypeAny, {
321
- value?: any;
322
- collection: string;
323
- action: "update";
324
- rkey: string;
325
- }, {
326
- value?: any;
327
- collection: string;
328
- action: "update";
329
- rkey: string;
330
- }>, z.ZodObject<{
331
- action: z.ZodLiteral<"delete">;
332
- collection: z.ZodString;
333
- rkey: z.ZodString;
334
- }, "strip", z.ZodTypeAny, {
335
- collection: string;
336
- action: "delete";
337
- rkey: string;
338
- }, {
339
- collection: string;
340
- action: "delete";
341
- rkey: string;
342
- }>]>;
343
- string: z.ZodString;
344
65
  cid: z.ZodEffects<z.ZodEffects<z.ZodAny, any, any>, CID, any>;
345
- strToCid: z.ZodEffects<z.ZodEffects<z.ZodString, string, string>, CID, string>;
346
66
  bytes: z.ZodType<Uint8Array, z.ZodTypeDef, Uint8Array>;
347
- strToInt: z.ZodEffects<z.ZodEffects<z.ZodString, string, string>, number, string>;
348
- strToBool: z.ZodEffects<z.ZodString, boolean, string>;
67
+ string: z.ZodString;
68
+ array: z.ZodArray<z.ZodUnknown, "many">;
69
+ map: z.ZodRecord<z.ZodString, z.ZodUnknown>;
70
+ unknown: z.ZodUnknown;
349
71
  };
350
- export interface CarStreamable {
351
- writeToCarStream(car: BlockWriter): Promise<void>;
72
+ export declare const def: {
73
+ commit: {
74
+ name: string;
75
+ schema: z.ZodObject<{
76
+ did: z.ZodString;
77
+ version: z.ZodNumber;
78
+ prev: z.ZodNullable<z.ZodEffects<z.ZodEffects<z.ZodAny, any, any>, CID, any>>;
79
+ data: z.ZodEffects<z.ZodEffects<z.ZodAny, any, any>, CID, any>;
80
+ sig: z.ZodType<Uint8Array, z.ZodTypeDef, Uint8Array>;
81
+ }, "strip", z.ZodTypeAny, {
82
+ data: CID;
83
+ did: string;
84
+ version: number;
85
+ prev: CID | null;
86
+ sig: Uint8Array;
87
+ }, {
88
+ data?: any;
89
+ prev?: any;
90
+ did: string;
91
+ version: number;
92
+ sig: Uint8Array;
93
+ }>;
94
+ };
95
+ cid: import("@atproto/common-web/src/check").Def<CID>;
96
+ bytes: import("@atproto/common-web/src/check").Def<Uint8Array>;
97
+ string: import("@atproto/common-web/src/check").Def<string>;
98
+ map: import("@atproto/common-web/src/check").Def<Record<string, unknown>>;
99
+ unknown: import("@atproto/common-web/src/check").Def<unknown>;
100
+ };
101
+ export declare enum WriteOpAction {
102
+ Create = "create",
103
+ Update = "update",
104
+ Delete = "delete"
352
105
  }
106
+ export declare type RecordCreateOp = {
107
+ action: WriteOpAction.Create;
108
+ collection: string;
109
+ rkey: string;
110
+ record: RepoRecord;
111
+ };
112
+ export declare type RecordUpdateOp = {
113
+ action: WriteOpAction.Update;
114
+ collection: string;
115
+ rkey: string;
116
+ record: RepoRecord;
117
+ };
118
+ export declare type RecordDeleteOp = {
119
+ action: WriteOpAction.Delete;
120
+ collection: string;
121
+ rkey: string;
122
+ };
123
+ export declare type RecordWriteOp = RecordCreateOp | RecordUpdateOp | RecordDeleteOp;
124
+ export declare type RecordCreateDescript = RecordCreateOp & {
125
+ cid: CID;
126
+ };
127
+ export declare type RecordUpdateDescript = RecordUpdateOp & {
128
+ prev: CID;
129
+ cid: CID;
130
+ };
131
+ export declare type RecordDeleteDescript = RecordDeleteOp & {
132
+ cid: CID;
133
+ };
134
+ export declare type RecordWriteDescript = RecordCreateDescript | RecordUpdateDescript | RecordDeleteDescript;
135
+ export declare type WriteLog = RecordWriteDescript[][];
136
+ export declare type CommitBlockData = {
137
+ commit: CID;
138
+ blocks: BlockMap;
139
+ };
140
+ export declare type CommitData = CommitBlockData & {
141
+ prev: CID | null;
142
+ };
143
+ export declare type RepoUpdate = CommitData & {
144
+ ops: RecordWriteOp[];
145
+ };
146
+ export declare type CollectionContents = Record<string, RepoRecord>;
147
+ export declare type RepoContents = Record<string, CollectionContents>;
148
+ export declare type RecordPath = {
149
+ collection: string;
150
+ rkey: string;
151
+ };
152
+ export declare type RecordClaim = {
153
+ collection: string;
154
+ rkey: string;
155
+ record: RepoRecord | null;
156
+ };
353
157
  export declare type DataValue = {
354
158
  key: string;
355
159
  value: CID;
@@ -359,10 +163,13 @@ export interface DataStore {
359
163
  update(key: string, value: CID): Promise<DataStore>;
360
164
  delete(key: string): Promise<DataStore>;
361
165
  get(key: string): Promise<CID | null>;
362
- list(count: number, after?: string, before?: string): Promise<DataValue[]>;
166
+ list(count?: number, after?: string, before?: string): Promise<DataValue[]>;
363
167
  listWithPrefix(prefix: string, count?: number): Promise<DataValue[]>;
364
- diff(other: DataStore): Promise<DataDiff>;
365
- stage(): Promise<CID>;
168
+ getUnstoredBlocks(): Promise<{
169
+ root: CID;
170
+ blocks: BlockMap;
171
+ }>;
366
172
  writeToCarStream(car: BlockWriter): Promise<void>;
173
+ cidsForPath(key: string): Promise<CID[]>;
367
174
  }
368
175
  export {};
package/dist/util.d.ts CHANGED
@@ -1,13 +1,32 @@
1
1
  import { CID } from 'multiformats/cid';
2
- import * as auth from '@atproto/auth';
3
- import { DataDiff } from './mst';
4
- import { IpldStore } from './blockstore';
5
- import { DataStore, RecordWriteOp } from './types';
6
- export declare const ucanForOperation: (prevData: DataStore, newData: DataStore, rootDid: string, authStore: auth.AuthStore) => Promise<string>;
7
- export declare const getCommitPath: (blockstore: IpldStore, earliest: CID | null, latest: CID) => Promise<CID[] | null>;
8
- export declare const getWriteOpLog: (blockstore: IpldStore, earliest: CID | null, latest: CID) => Promise<RecordWriteOp[][]>;
9
- export declare const diffToWriteOps: (blockstore: IpldStore, diff: DataDiff) => Promise<RecordWriteOp[]>;
10
- export declare const parseRecordKey: (key: string) => {
11
- collection: string;
12
- rkey: string;
13
- };
2
+ import { BlockWriter } from '@ipld/car/writer';
3
+ import { Block as CarBlock } from '@ipld/car/api';
4
+ import { LexValue, RepoRecord } from '@atproto/lexicon';
5
+ import DataDiff from './data-diff';
6
+ import { RepoStorage } from './storage';
7
+ import { Commit, RecordPath, RecordWriteDescript, UnsignedCommit, WriteLog } from './types';
8
+ import BlockMap from './block-map';
9
+ import { Keypair } from '@atproto/crypto';
10
+ export declare function verifyIncomingCarBlocks(car: AsyncIterable<CarBlock>): AsyncIterable<CarBlock>;
11
+ export declare const writeCar: (root: CID | null, fn: (car: BlockWriter) => Promise<void>) => Promise<Uint8Array>;
12
+ export declare const blocksToCar: (root: CID | null, blocks: BlockMap) => Promise<Uint8Array>;
13
+ export declare const readCar: (bytes: Uint8Array) => Promise<{
14
+ roots: CID[];
15
+ blocks: BlockMap;
16
+ }>;
17
+ export declare const readCarWithRoot: (bytes: Uint8Array) => Promise<{
18
+ root: CID;
19
+ blocks: BlockMap;
20
+ }>;
21
+ export declare const getWriteLog: (storage: RepoStorage, latest: CID, earliest: CID | null) => Promise<WriteLog>;
22
+ export declare const diffToWriteDescripts: (diff: DataDiff, blocks: BlockMap) => Promise<RecordWriteDescript[]>;
23
+ export declare const collapseWriteLog: (log: WriteLog) => RecordWriteDescript[];
24
+ export declare const collapseDiffs: (diffs: DataDiff[]) => DataDiff;
25
+ export declare const parseDataKey: (key: string) => RecordPath;
26
+ export declare const formatDataKey: (collection: string, rkey: string) => string;
27
+ export declare const metaEqual: (a: Commit, b: Commit) => boolean;
28
+ export declare const signCommit: (unsigned: UnsignedCommit, keypair: Keypair) => Promise<Commit>;
29
+ export declare const verifyCommitSig: (commit: Commit, didKey: string) => Promise<boolean>;
30
+ export declare const cborToLex: (val: Uint8Array) => LexValue;
31
+ export declare const cborToLexRecord: (val: Uint8Array) => RepoRecord;
32
+ export declare const cidForRecord: (val: LexValue) => Promise<CID>;
package/dist/verify.d.ts CHANGED
@@ -1,5 +1,27 @@
1
1
  import { CID } from 'multiformats/cid';
2
- import * as auth from '@atproto/auth';
3
- import { IpldStore } from './blockstore';
4
- import { DataDiff } from './mst';
5
- export declare const verifyUpdates: (blockstore: IpldStore, earliest: CID | null, latest: CID, verifier: auth.Verifier) => Promise<DataDiff>;
2
+ import { ReadableBlockstore, RepoStorage } from './storage';
3
+ import DataDiff from './data-diff';
4
+ import ReadableRepo from './readable-repo';
5
+ import CidSet from './cid-set';
6
+ import { RecordClaim, RepoContents } from './types';
7
+ export declare type VerifiedCheckout = {
8
+ contents: RepoContents;
9
+ newCids: CidSet;
10
+ };
11
+ export declare const verifyCheckout: (storage: ReadableBlockstore, head: CID, did: string, signingKey: string) => Promise<VerifiedCheckout>;
12
+ export declare type VerifiedUpdate = {
13
+ commit: CID;
14
+ prev: CID | null;
15
+ diff: DataDiff;
16
+ newCids: CidSet;
17
+ };
18
+ export declare const verifyFullHistory: (storage: RepoStorage, head: CID, did: string, signingKey: string) => Promise<VerifiedUpdate[]>;
19
+ export declare const verifyUpdates: (repo: ReadableRepo, updateStorage: RepoStorage, updateRoot: CID, did: string, signingKey: string) => Promise<VerifiedUpdate[]>;
20
+ export declare const verifyCommitPath: (baseRepo: ReadableRepo, storage: ReadableBlockstore, commitPath: CID[], did: string, signingKey: string) => Promise<VerifiedUpdate[]>;
21
+ export declare const verifyProofs: (proofs: Uint8Array, claims: RecordClaim[], did: string, didKey: string) => Promise<{
22
+ verified: RecordClaim[];
23
+ unverified: RecordClaim[];
24
+ }>;
25
+ export declare const verifyRecords: (proofs: Uint8Array, did: string, signingKey: string) => Promise<RecordClaim[]>;
26
+ export declare class RepoVerificationError extends Error {
27
+ }
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@atproto/repo",
3
- "version": "0.0.1",
3
+ "version": "0.1.0",
4
4
  "main": "dist/index.js",
5
5
  "license": "MIT",
6
6
  "scripts": {
@@ -22,8 +22,10 @@
22
22
  "postpublish": "npm run update-main-to-src"
23
23
  },
24
24
  "dependencies": {
25
- "@atproto/auth": "*",
26
25
  "@atproto/common": "*",
26
+ "@atproto/crypto": "*",
27
+ "@atproto/did-resolver": "*",
28
+ "@atproto/lexicon": "*",
27
29
  "@atproto/nsid": "*",
28
30
  "@ipld/car": "^3.2.3",
29
31
  "@ipld/dag-cbor": "^7.0.0",
@@ -0,0 +1,95 @@
1
+ import { lexToIpld, LexValue } from '@atproto/lexicon'
2
+ import { dataToCborBlock } from '@atproto/common'
3
+ import { CID } from 'multiformats/cid'
4
+ import * as uint8arrays from 'uint8arrays'
5
+
6
+ export class BlockMap {
7
+ private map: Map<string, Uint8Array> = new Map()
8
+
9
+ async add(value: LexValue): Promise<CID> {
10
+ const block = await dataToCborBlock(lexToIpld(value))
11
+ this.set(block.cid, block.bytes)
12
+ return block.cid
13
+ }
14
+
15
+ set(cid: CID, bytes: Uint8Array) {
16
+ this.map.set(cid.toString(), bytes)
17
+ }
18
+
19
+ get(cid: CID): Uint8Array | undefined {
20
+ return this.map.get(cid.toString())
21
+ }
22
+
23
+ getMany(cids: CID[]): { blocks: BlockMap; missing: CID[] } {
24
+ const missing: CID[] = []
25
+ const blocks = new BlockMap()
26
+ for (const cid of cids) {
27
+ const got = this.map.get(cid.toString())
28
+ if (got) {
29
+ blocks.set(cid, got)
30
+ } else {
31
+ missing.push(cid)
32
+ }
33
+ }
34
+ return { blocks, missing }
35
+ }
36
+
37
+ has(cid: CID): boolean {
38
+ return this.map.has(cid.toString())
39
+ }
40
+
41
+ clear(): void {
42
+ this.map.clear()
43
+ }
44
+
45
+ forEach(cb: (bytes: Uint8Array, cid: CID) => void): void {
46
+ this.map.forEach((val, key) => cb(val, CID.parse(key)))
47
+ }
48
+
49
+ entries(): Entry[] {
50
+ const entries: Entry[] = []
51
+ this.forEach((bytes, cid) => {
52
+ entries.push({ cid, bytes })
53
+ })
54
+ return entries
55
+ }
56
+
57
+ addMap(toAdd: BlockMap) {
58
+ toAdd.forEach((bytes, cid) => {
59
+ this.set(cid, bytes)
60
+ })
61
+ }
62
+
63
+ get size(): number {
64
+ return this.map.size
65
+ }
66
+
67
+ get byteSize(): number {
68
+ let size = 0
69
+ this.forEach((bytes) => {
70
+ size += bytes.length
71
+ })
72
+ return size
73
+ }
74
+
75
+ equals(other: BlockMap): boolean {
76
+ if (this.size !== other.size) {
77
+ return false
78
+ }
79
+ for (const entry of this.entries()) {
80
+ const otherBytes = other.get(entry.cid)
81
+ if (!otherBytes) return false
82
+ if (!uint8arrays.equals(entry.bytes, otherBytes)) {
83
+ return false
84
+ }
85
+ }
86
+ return true
87
+ }
88
+ }
89
+
90
+ type Entry = {
91
+ cid: CID
92
+ bytes: Uint8Array
93
+ }
94
+
95
+ export default BlockMap
package/src/cid-set.ts CHANGED
@@ -42,8 +42,7 @@ export class CidSet {
42
42
  }
43
43
 
44
44
  toList(): CID[] {
45
- const arr = [...this.set]
46
- return arr.map((c) => CID.parse(c))
45
+ return [...this.set].map((c) => CID.parse(c))
47
46
  }
48
47
  }
49
48