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 1
/
Copy pathseed_app_keychain.html
173 lines (156 loc) · 5.07 KB
/
seed_app_keychain.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
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
<!DOCTYPE html>
<!-- this example will store did in keychain, and use session storage to avoid needing to login each time -->
<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>
<form
id="loginForm"
action="/fake-submit"
method="post"
target="hiddenIframe"
>
<label for="username">Username:</label>
<input
type="text"
id="username"
name="username"
value="myDid"
autocomplete="username"
/>
<br />
<label for="password">Password (myDid JSON):</label>
<input
type="password"
id="password"
name="password"
autocomplete="current-password"
/>
<br />
<button type="button" id="generateMyDid">Generate new myDid</button>
<br />
<input type="submit" value="Login" />
</form>
<iframe
name="hiddenIframe"
id="hiddenIframe"
style="display: none;"
></iframe>
<script type="module">
import { Web5 } from "https://cdn.jsdelivr.net/npm/@tbd54566975/[email protected]/dist/browser.mjs";
const web5 = new Web5();
const loginForm = document.getElementById("loginForm");
const passwordField = document.getElementById("password");
const generateMyDidButton = document.getElementById("generateMyDid");
loginForm.addEventListener("submit", async (e) => {
e.preventDefault();
const myDidJson = JSON.parse(passwordField.value);
sessionStorage.setItem("myDid", JSON.stringify(myDidJson));
// Reload the page to trigger password manager save
window.location.href = window.location.href.split("?")[0]; // Remove query string if any
initializeWeb5(myDidJson);
});
generateMyDidButton.addEventListener("click", async () => {
const newMyDid = await createMyDid();
passwordField.value = JSON.stringify(newMyDid);
});
async function createMyDid() {
const myDid = await web5.did.create("ion", {
services: [
{
id: "dwn",
type: "DecentralizedWebNode",
serviceEndpoint: {
nodes: []
}
}
]
});
return myDid;
}
// Check if myDid is stored in sessionStorage and initializeWeb5 if exists
if (sessionStorage.getItem("myDid")) {
const myDidJson = JSON.parse(sessionStorage.getItem("myDid"));
document.getElementById("loginForm").style.display = "none";
initializeWeb5(myDidJson);
}
async function initializeWeb5(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. */
// Write a plain text record to the in-memory DWN.
let response = await web5.dwn.records.write(myDid.id, {
author: myDid.id,
data: "Hello, world!",
message: {
schema: "foo/bar",
dataFormat: "text/plain"
}
});
console.log("WRITE RESPONSE:", response.status);
// Query for the record that was just created.
response = await web5.dwn.records.query(myDid.id, {
author: myDid.id,
message: {
filter: {
schema: "foo/bar"
}
}
});
console.log("QUERY RESPONSE:", response.entries[0]);
console.log(
"RESPONSE DATA:",
base64UrlToString(response.entries[0].encodedData)
);
// Write a JSON record to the in-memory DWN.
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);
// Query for the record 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]);
console.log(
"RESPONSE DATA:",
base64UrlToObject(response.entries[0].encodedData)
);
}
</script>
</body>
</html>