aboutsummaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorpukkamustard <pukkamustard@posteo.net>2020-06-10 13:57:35 +0200
committerpukkamustard <pukkamustard@posteo.net>2020-06-10 13:57:35 +0200
commit4da0b3e845d916cb222895f7452af7537791c51c (patch)
tree4f41512633669b7b24fe5fa46428b97cfc023a08
parent408c91f3b296a2c120a990b852cfea19b98bf050 (diff)
web-demo: nicer error handling
-rw-r--r--examples/web-demo/index.html4
-rw-r--r--examples/web-demo/src/index.js17
-rw-r--r--examples/web-demo/style.css11
-rw-r--r--src/crypto.js5
-rw-r--r--src/eris.js5
5 files changed, 36 insertions, 6 deletions
diff --git a/examples/web-demo/index.html b/examples/web-demo/index.html
index 4fa7e3a..0bc2791 100644
--- a/examples/web-demo/index.html
+++ b/examples/web-demo/index.html
@@ -62,6 +62,9 @@
</details>
<br>
<button id="controls-decode">Decode</button>
+
+ <br>
+ <pre id="controls-error">Hi</pre>
</div>
<div id="encoded">
@@ -77,7 +80,6 @@
<details>
<summary>encoded data</summary>
- <p>The data as encoded by ERIS. For plain text this is the same as the input. RDF is normalized before encoding with ERIS.</p>
<pre id="encoded-data"></pre>
</details>
diff --git a/examples/web-demo/src/index.js b/examples/web-demo/src/index.js
index 155fe1e..be2e18e 100644
--- a/examples/web-demo/src/index.js
+++ b/examples/web-demo/src/index.js
@@ -92,6 +92,7 @@ async function main () {
const controlsEncode = document.getElementById('controls-encode')
const controlsDecode = document.getElementById('controls-decode')
const controlsInputType = document.getElementById('controls-input-type')
+ const controlsError = document.getElementById('controls-error')
const encodedErisReadCap = document.getElementById('encoded-eris-read-cap')
const encodedData = document.getElementById('encoded-data')
@@ -139,24 +140,34 @@ async function main () {
return ERIS.get(readCap, cas)
}
+ function setError (err) {
+ console.error(err)
+ controlsError.innerText = err
+ }
+
+ function clearError () {
+ controlsError.innerText = ''
+ }
+
controlsEncode.onclick = async function (e) {
+ clearError()
try {
const urn = await encode()
encodedErisReadCap.value = urn
renderBlocks(cas)
} catch (err) {
console.error(err)
- encodedErisReadCap.value = 'ERROR (see console)'
+ setError(err)
}
}
controlsDecode.onclick = async function (e) {
+ clearError()
try {
const decoded = await decode()
inputTextarea.value = utf8Decoder.decode(decoded)
} catch (err) {
- console.error(err)
- inputTextarea.value = 'ERROR (see console)'
+ setError(err)
}
}
diff --git a/examples/web-demo/style.css b/examples/web-demo/style.css
index 95fc931..ef01b83 100644
--- a/examples/web-demo/style.css
+++ b/examples/web-demo/style.css
@@ -32,8 +32,10 @@ textarea {
text-align: center;
}
-#controls-options {
- margin-top: 20px;
+#controls-error {
+ background-color: red;
+ white-space: pre-wrap;
+ overflow: auto;
}
#encoded {
@@ -48,6 +50,7 @@ textarea {
#encoded-data {
white-space: pre-wrap;
+ max-width: 90%;
max-height: 300px;
overflow: auto;
}
@@ -58,3 +61,7 @@ textarea {
flex-grow: 1;
}
+.block {
+ background-color: #ddd;
+}
+
diff --git a/src/crypto.js b/src/crypto.js
index a0119dd..1cc0166 100644
--- a/src/crypto.js
+++ b/src/crypto.js
@@ -34,6 +34,11 @@ module.exports = {
is_zero: async function (buf) {
await sodium.ready
return sodium.is_zero(buf)
+ },
+
+ memcmp: async function (a, b) {
+ await sodium.ready
+ return sodium.memcmp(a,b)
}
}
diff --git a/src/eris.js b/src/eris.js
index e82a521..1c24cd1 100644
--- a/src/eris.js
+++ b/src/eris.js
@@ -248,6 +248,11 @@ async function * decodeTree (cas, verificationKey, ref, nodeLevel, nodeCount) {
// Get block from cas
const block = await cas.get(ref)
+ if (block === undefined) throw new Error('Can not retrieve block')
+
+ // check integrity of block
+ const blockHash = await crypto.hash(block)
+ if (!crypto.memcmp(ref, blockHash)) throw new Error('Block is corrupted')
if (nodeLevel === 0) {
// if level 0, then it is a data block