aboutsummaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorpukkamustard <pukkamustard@posteo.net>2020-06-10 13:40:47 +0200
committerpukkamustard <pukkamustard@posteo.net>2020-06-10 13:40:47 +0200
commit408c91f3b296a2c120a990b852cfea19b98bf050 (patch)
treea1053f67ef91c7fbf2690a8d8b0e932f24a5a09e
parent34e1655cf38102d4c5b3d6c58424b14a2d04a0fc (diff)
web-demo: decode content
-rw-r--r--examples/web-demo/index.html8
-rw-r--r--examples/web-demo/src/index.js35
-rw-r--r--examples/web-demo/style.css10
-rw-r--r--src/eris.js25
4 files changed, 57 insertions, 21 deletions
diff --git a/examples/web-demo/index.html b/examples/web-demo/index.html
index ff89441..4fa7e3a 100644
--- a/examples/web-demo/index.html
+++ b/examples/web-demo/index.html
@@ -60,12 +60,16 @@
<option value="application/ld+json">JSON-LD</option>
</select>
</details>
+ <br>
+ <button id="controls-decode">Decode</button>
</div>
<div id="encoded">
<h2>Encoded</h2>
- <h3>ERIS URN</h3>
- <input id="encoded-eris-urn" type="url" readonly>
+
+ <h3>Read capability</h3>
+ <input id="encoded-eris-read-cap" type="url">
+
</input>
<br>
diff --git a/examples/web-demo/src/index.js b/examples/web-demo/src/index.js
index cc66492..155fe1e 100644
--- a/examples/web-demo/src/index.js
+++ b/examples/web-demo/src/index.js
@@ -88,9 +88,12 @@ async function main () {
const inputLoadAliceInWonderland = document.getElementById('input-load-alice-in-wonderland')
const inputLoadSampleVocabulary = document.getElementById('input-load-sample-vocabulary')
const inputLoadSampleActor = document.getElementById('input-load-sample-actor')
+
const controlsEncode = document.getElementById('controls-encode')
+ const controlsDecode = document.getElementById('controls-decode')
const controlsInputType = document.getElementById('controls-input-type')
- const encodedErisUrn = document.getElementById('encoded-eris-urn')
+
+ const encodedErisReadCap = document.getElementById('encoded-eris-read-cap')
const encodedData = document.getElementById('encoded-data')
// a ContentAddressableStorage based on a JavaScipt Map
@@ -131,14 +134,30 @@ async function main () {
return ERIS.put(input, cas)
}
- controlsEncode.onclick = function (e) {
- encode().then((urn) => {
- encodedErisUrn.value = urn
+ async function decode () {
+ const readCap = encodedErisReadCap.value
+ return ERIS.get(readCap, cas)
+ }
+
+ controlsEncode.onclick = async function (e) {
+ try {
+ const urn = await encode()
+ encodedErisReadCap.value = urn
renderBlocks(cas)
- }).catch((e) => {
- console.error(e)
- encodedErisUrn.value = 'ERROR (see console)'
- })
+ } catch (err) {
+ console.error(err)
+ encodedErisReadCap.value = 'ERROR (see console)'
+ }
+ }
+
+ controlsDecode.onclick = async function (e) {
+ try {
+ const decoded = await decode()
+ inputTextarea.value = utf8Decoder.decode(decoded)
+ } catch (err) {
+ console.error(err)
+ inputTextarea.value = 'ERROR (see console)'
+ }
}
inputLoadSampleVocabulary.onclick = function (e) {
diff --git a/examples/web-demo/style.css b/examples/web-demo/style.css
index 4cb471c..95fc931 100644
--- a/examples/web-demo/style.css
+++ b/examples/web-demo/style.css
@@ -20,14 +20,13 @@ textarea {
#input {
background-color: #EEE;
- flex-basis: 600px;
- flex-grow: 3;
+ flex: 3 0;
padding: 10px;
margin: 10px;
}
#controls {
- flex-basis: 200px;
+ flex: 1 0;
padding: 10px;
margin-top: 100px;
text-align: center;
@@ -39,12 +38,11 @@ textarea {
#encoded {
background-color: #EEE;
- flex-basis: 600px;
- flex-grow: 3;
+ flex: 3 0;
padding: 10px;
}
-#encoded-eris-urn {
+#encoded-eris-read-cap {
width: 90%;
}
diff --git a/src/eris.js b/src/eris.js
index 7f37498..e82a521 100644
--- a/src/eris.js
+++ b/src/eris.js
@@ -198,12 +198,27 @@ function decodeCapability (cap) {
if (cap.substring(0, 10) === 'urn:erisx:') {
const buffer = base32.decode(cap.substring(10))
const view = new Uint8Array(buffer)
+
+ const version = view[0]
+ if (version !== 0) throw new Error('Capability has unsupported ERIS version')
+
+ const type = view[1]
+ if (!(type === 0 || type === 1)) throw new Error('Unknown capability type')
+
+ const level = view[2]
+
+ const rootReference = view.slice(3,35)
+ if (rootReference.length !== 32) throw new Error('Could not extract root reference from ERIS capability')
+
+ const key = view.slice(35, 67)
+ if (key.length !== 32) throw new Error('Could not extract key from ERIS capability')
+
return {
- version: view[0],
- type: view[1],
- level: view[2],
- rootReference: view.slice(3, 35),
- key: view.slice(35, 67)
+ version: version,
+ type: type,
+ level: level,
+ rootReference: rootReference,
+ key: key
}
} else {
throw new Error('Can not decode ERIS URN.')