aboutsummaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorpukkamustard <pukkamustard@posteo.net>2020-06-10 14:29:51 +0200
committerpukkamustard <pukkamustard@posteo.net>2020-06-10 14:29:51 +0200
commita5243a3496457b1a97bef3466f8b3fa802a166ae (patch)
treea6636395e94ba999dd8bd9d6871dd7d29f256ea8
parent4da0b3e845d916cb222895f7452af7537791c51c (diff)
web-demo: ability to remove and randomize blocks
-rw-r--r--examples/web-demo/index.html4
-rw-r--r--examples/web-demo/src/index.js36
-rw-r--r--examples/web-demo/style.css9
-rw-r--r--src/eris.js4
4 files changed, 47 insertions, 6 deletions
diff --git a/examples/web-demo/index.html b/examples/web-demo/index.html
index 0bc2791..7542ec2 100644
--- a/examples/web-demo/index.html
+++ b/examples/web-demo/index.html
@@ -64,7 +64,7 @@
<button id="controls-decode">Decode</button>
<br>
- <pre id="controls-error">Hi</pre>
+ <pre id="controls-error"></pre>
</div>
<div id="encoded">
@@ -90,6 +90,8 @@
<div id="blocks">
<h2>Blocks</h2>
+ <div id="block-container">
+ </div>
</div>
</main>
diff --git a/examples/web-demo/src/index.js b/examples/web-demo/src/index.js
index be2e18e..18a12d0 100644
--- a/examples/web-demo/src/index.js
+++ b/examples/web-demo/src/index.js
@@ -97,6 +97,8 @@ async function main () {
const encodedErisReadCap = document.getElementById('encoded-eris-read-cap')
const encodedData = document.getElementById('encoded-data')
+ const blockContainer = document.getElementById('block-container')
+
// a ContentAddressableStorage based on a JavaScipt Map
const cas = new ERIS.MapContentAddressableStorage()
@@ -122,9 +124,39 @@ async function main () {
}
}
+ function createBlockDiv (block, cas) {
+ const blockDiv = document.createElement('div')
+ blockDiv.className = 'block'
+
+ const blockTitle = document.createTextNode(block)
+
+ const blockRemove = document.createElement('button')
+ blockRemove.innerText = 'remove'
+ blockRemove.onclick = function (e) {
+ cas._map.delete(block)
+ renderBlocks(cas)
+ }
+
+ const blockCorrupt = document.createElement('button')
+ blockCorrupt.innerText = 'randomize'
+ blockCorrupt.onclick = function (e) {
+ const randomBytes = new Uint8Array(32)
+ window.crypto.getRandomValues(randomBytes)
+ cas._map.set(block, randomBytes)
+ }
+
+ blockDiv.appendChild(blockTitle)
+ blockDiv.appendChild(blockRemove)
+ blockDiv.appendChild(blockCorrupt)
+
+ return blockDiv
+ }
+
async function renderBlocks (cas) {
- for (const block of cas._map.entries()) {
- console.log(block)
+ blockContainer.innerHTML = ''
+ for (const block of cas._map.keys()) {
+ const blockDiv = createBlockDiv(block, cas)
+ blockContainer.appendChild(blockDiv)
}
}
diff --git a/examples/web-demo/style.css b/examples/web-demo/style.css
index ef01b83..f2693a5 100644
--- a/examples/web-demo/style.css
+++ b/examples/web-demo/style.css
@@ -61,7 +61,14 @@ textarea {
flex-grow: 1;
}
+#block-container {
+ display: flex;
+ flex-wrap: wrap;
+}
+
.block {
- background-color: #ddd;
+ background-color: #bcc;
+ margin: 10px;
+ padding: 5px;
}
diff --git a/src/eris.js b/src/eris.js
index 1c24cd1..6cfb877 100644
--- a/src/eris.js
+++ b/src/eris.js
@@ -248,11 +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')
+ if (block === undefined) throw new Error('Can not get block: ' + base32.encode(ref))
// check integrity of block
const blockHash = await crypto.hash(block)
- if (!crypto.memcmp(ref, blockHash)) throw new Error('Block is corrupted')
+ if (!await crypto.memcmp(ref, blockHash)) throw new Error('Block is corrupted: ' + base32.encode(ref))
if (nodeLevel === 0) {
// if level 0, then it is a data block