-
Notifications
You must be signed in to change notification settings - Fork 0
/
index.html
158 lines (150 loc) · 6.53 KB
/
index.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
<html>
<head>
<link rel="stylesheet"
href="//cdnjs.cloudflare.com/ajax/libs/highlight.js/9.12.0/styles/default.min.css">
<title>Chlu Reputation Viewer</title>
</head>
<body>
<a id="download" style="display:none">Download your Decentralized Reputation</a>
<div id="content">
Loading...
</div>
<script>
function show(txt) {
console.log(txt);
const button = document.getElementById('download')
if (txt instanceof Error) {
document.getElementById('content').innerHTML = 'Error: ' + txt.message;
button.style.display = 'none';
} else if (typeof txt === 'string') {
document.getElementById('content').innerHTML = txt;
button.style.display = 'none';
} else if (typeof txt === 'object') {
const string = JSON.stringify(txt, undefined, 2)
document.getElementById('content').innerHTML = '<pre><code>' + string + '</code></pre>'
hljs.highlightBlock(document.querySelector('#content pre code'));
button.download = 'my_decentralized_reputation.json'
button.href = "data:text/json;charset=utf-8," + encodeURIComponent(string);
button.style.display = 'block';
} else {
button.style.display = 'none';
}
}
function getAnchor() {
return window.location.hash.substr(1);
}
</script>
<!-- IPFS Stuff -->
<script src="https://unpkg.com/cids/dist/index.min.js"></script>
<script src="https://unpkg.com/[email protected]/dist/index.min.js"></script>
<script src="https://unpkg.com/[email protected]/dist/orbitdb.min.js"></script>
<!-- End IPFS Stuff -->
<script src="//cdnjs.cloudflare.com/ajax/libs/highlight.js/9.12.0/highlight.min.js"></script>
<script>
async function main() {
hljs.initHighlightingOnLoad();
show('Connecting to IPFS...')
await prepareIPFS();
show('Loading distributed DID database...')
await window.db.load()
window.db.events.on('replicated', refresh)
window.onhashchange = refresh;
return refresh()
}
async function resolveDID(rep) {
if (rep.did && rep.did['/']) {
rep.did = (await window.ipfs.dag.get(new Cids(rep.did['/']))).value
if (rep.did && rep.did.data && rep.did.data.toString) {
rep.did = JSON.parse(rep.did.data.toString())
}
}
if (Array.isArray(rep.reviews)) {
rep.reviews = rep.reviews.map(r => {
if (r.did && r.did['/']) {
r.did['/'] = (new Cids(r.did['/'])).toBaseEncodedString()
}
return r
})
}
return rep;
}
async function refresh() {
try {
const url = getAnchor();
console.log('url', url)
let did
if (!url) {
return show('Link invalid')
}
if (url.indexOf('did:chlu:') === 0) {
const didId = url.indexOf('#') > 0 ? url.slice(0, url.indexOf('#')) : url
show('Resolving DID ID...')
console.log('didId', didId)
did = await window.db.get(didId)
if (!did) {
return
}
console.log('did addr', did)
} else {
did = url
}
show('Resolving Reputation')
const reputationMultihash = await window.db.get(did);
if (!reputationMultihash) {
return
}
console.log('rep addr', reputationMultihash)
show('Retrieving Reputation from IPFS...')
const reputationDag = await window.ipfs.dag.get(reputationMultihash)
console.log(reputationDag)
let data = null
if (reputationDag.value && reputationDag.value.data && reputationDag.value.data.toString) {
data = JSON.parse(reputationDag.value.data.toString())
} else {
data = reputationDag.value
}
show('Resolving links in Reputation')
const reputationResolved = await resolveDID(data);
return show(reputationResolved);
} catch (error) {
show(error);
}
}
main()
.catch(function(error) {
show(error);
})
function prepareIPFS() {
if (window.ipfs && window.orbitDb && window.db) {
return Promise.resolve();
} else {
return new Promise(function(resolve){
window.ipfs = new Ipfs({
EXPERIMENTAL: {
pubsub: true
},
config: {
Addresses: {
Swarm: [
// Connect to Chlu rendezvous server
'/dns4/ren.chlu.io/tcp/443/wss/p2p-websocket-star'
]
}
}
});
window.ipfs.on('ready', function(){ resolve(window.ipfs); });
})
.then(function(){
window.orbitDb = new OrbitDB(window.ipfs);
return orbitDb.kvstore('chlu-reputation-experimental-2', {
write: ['*']
});
})
.then(function(db){
window.db = db;
});
}
}
</script>
</body>
</html>