-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathdemo-get.js
55 lines (46 loc) · 1.65 KB
/
demo-get.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
import Hypercore from "hypercore";
import Hyperbee from "hyperbee";
import { getPublicKeyAsync } from "@noble/ed25519";
import { keyFromPem } from "./src/signAttestation.js";
import { getInfo } from "./src/otsTimestamp.js";
import { dbGet, dbIsEncrypted, dbRawValue } from "./src/dbGet.js";
// Set up Hypercore and Hyperbee
const core = new Hypercore("./demo.hypercore");
await core.ready();
const db = new Hyperbee(core, {
keyEncoding: "utf-8", // for demo
valueEncoding: "binary",
});
// Attestation data
const waczCID = "bafybeifgkpgb7yqgjnovszaio7tzetmdfmigylr24hg6a76wnjxcnhkx54";
const attribute = "description";
// XXX: just a demo key
const sigPubKey = await getPublicKeyAsync(await keyFromPem("./demokey.pem"));
// Decode and print value
const result = await dbGet(db, waczCID, attribute, sigPubKey);
console.log(result);
// TODO
// console.log('timestamp verified?', verifyTimestamp(resultObj));
console.log("\nExtra timestamp info:");
getInfo(result.timestamp.ots.proof);
// Upgrade and check
// console.log("Upgrading...");
// await dbUpgrade(db, waczCID, attribute, sigPubKey);
// getInfo(result.timestamp.ots.proof);
// Encrypted value
//
// 32 byte DEMO encryption key that is reused in demo.js
const key = Buffer.from(
"QHle+CRiaq8iv1fP9xopZGbO6F7F8926TpSOrReQJ1Q=",
"base64"
);
console.log(
"Is 'secret-stuff' encrypted?",
await dbIsEncrypted(db, waczCID, "secret-stuff")
);
console.log("Retrieve 'secret-stuff' without encryption key:");
console.log(await dbRawValue(db, waczCID, "secret-stuff"));
console.log("Retrieve 'secret-stuff' WITH encryption key:");
console.log(
(await dbGet(db, waczCID, "secret-stuff", sigPubKey, key, true)).value
);