This repository has been archived by the owner on Nov 12, 2023. It is now read-only.
generated from TBD54566975/tbd-project-template
-
Notifications
You must be signed in to change notification settings - Fork 2
/
seed_app.html
84 lines (71 loc) · 2.52 KB
/
seed_app.html
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
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
<!DOCTYPE html>
<html>
<head>
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<title>OMG Web5.js App</title>
<link href="https://cdn.jsdelivr.net/gh/TBD54566975/web5-omg@main/styles.css" rel="stylesheet">
<link href="https://fonts.googleapis.com/css2?family=Press+Start+2P&display=swap" rel="stylesheet">
</head>
<body>
<h>{TBD logo}</h>
<script type="module">
import { Web5 } from 'https://cdn.jsdelivr.net/npm/@tbd54566975/[email protected]/dist/browser.mjs';
const web5 = new Web5();
/* boilerplate start - required for every web5 app */
let myDid;
if (localStorage.getItem('myDid')) {
myDid = JSON.parse(localStorage.getItem('myDid'));
console.log("Loading previous did from localStorage");
} else {
console.log("Creating new did and storing in local storage");
myDid = await web5.did.create('ion', {
services: [{
'id': 'dwn',
'type': 'DecentralizedWebNode',
'serviceEndpoint': {
'nodes': []
}
}]
});
localStorage.setItem('myDid', JSON.stringify(myDid));
}
web5.did.register({
connected: true,
did: myDid.id,
endpoint: 'app://dwn',
keys: myDid.keys[0].keypair
});
function base64UrlToString(encodedData) {
return web5.dwn.SDK.Encoder.bytesToString(web5.dwn.SDK.Encoder.base64UrlToBytes(encodedData));
}
function base64UrlToObject(encodedData) {
return web5.dwn.SDK.Encoder.base64UrlToObject(encodedData);
}
/* boilerplate end */
/* Below are examples of operations with the DWN: writing and reading records. */
// Example: Write a JSON record to the in-memory DWN.
let response = await web5.dwn.records.write(myDid.id, {
author: myDid.id,
data: { random: 'stuff' },
message: {
schema: 'foo/baz',
dataFormat: 'application/json'
}
});
console.log('WRITE RESPONSE:', response.status);
// Example: Query for the JSON that was just created.
response = await web5.dwn.records.query(myDid.id, {
author: myDid.id,
message: {
filter: {
schema: 'foo/baz'
}
}
});
console.log('QUERY RESPONSE:', response.entries[0]);
// this gets the response (actually the first entry in the response) as an object:
let responseObj = base64UrlToObject(response.entries[0].encodedData);
console.log('RESPONSE DATA:', responseObj);
</script>
</body>
</html>