Suite of AT Protocol TypeScript libraries built on web standards
21
fork

Configure Feed

Select the types of activity you want to include in your feed.

at main 89 lines 2.3 kB view raw
1import { Secp256k1Keypair } from "@atp/crypto"; 2import { blocksToCarFile, Repo, verifyProofs, WriteOpAction } from "../mod.ts"; 3import { MemoryBlockstore } from "../storage/index.ts"; 4import { assertRejects } from "@std/assert"; 5import { assertEquals } from "@std/assert/equals"; 6 7// @NOTE this test uses a fully deterministic tree structure 8Deno.test("includes all relevant blocks for proof in commit data", async () => { 9 const did = "did:example:alice"; 10 const collection = "com.atproto.test"; 11 const record = { 12 test: 123, 13 }; 14 15 const blockstore = new MemoryBlockstore(); 16 const keypair = Secp256k1Keypair.create(); 17 let repo = await Repo.create(blockstore, did, keypair); 18 19 const keys: string[] = []; 20 for (let i = 0; i < 50; i++) { 21 const rkey = `key-${i}`; 22 keys.push(rkey); 23 repo = await repo.applyWrites( 24 [ 25 { 26 action: WriteOpAction.Create, 27 collection, 28 rkey, 29 record, 30 }, 31 ], 32 keypair, 33 ); 34 } 35 36 // this test demonstrates the test case: 37 // specifically in the case of deleting the first key, there is a "rearranged block" that is necessary 38 // in the proof path but _is not_ in newBlocks (as it already existed in the repository) 39 { 40 const commit = await repo.formatCommit( 41 { 42 action: WriteOpAction.Delete, 43 collection, 44 rkey: keys[0], 45 }, 46 keypair, 47 ); 48 const car = await blocksToCarFile(commit.cid, commit.newBlocks); 49 const proofAttempt = verifyProofs( 50 car, 51 [ 52 { 53 collection, 54 rkey: keys[0], 55 cid: null, 56 }, 57 ], 58 did, 59 keypair.did(), 60 ); 61 await assertRejects(() => proofAttempt, "block not found"); 62 } 63 64 for (const rkey of keys) { 65 const commit = await repo.formatCommit( 66 { 67 action: WriteOpAction.Delete, 68 collection, 69 rkey, 70 }, 71 keypair, 72 ); 73 const car = await blocksToCarFile(commit.cid, commit.relevantBlocks); 74 const proofRes = await verifyProofs( 75 car, 76 [ 77 { 78 collection, 79 rkey: rkey, 80 cid: null, 81 }, 82 ], 83 did, 84 keypair.did(), 85 ); 86 assertEquals(proofRes.unverified.length, 0); 87 repo = await repo.applyCommit(commit); 88 } 89});