diff options
author | pukkamustard <pukkamustard@posteo.net> | 2020-06-10 15:10:47 +0200 |
---|---|---|
committer | pukkamustard <pukkamustard@posteo.net> | 2020-06-10 15:10:47 +0200 |
commit | bde4ce46aa2031bb7b1b811048ea8b577d320f82 (patch) | |
tree | ff75b8db635fd3c62eaf45375ec1767e05401db7 | |
parent | 86e7f01770be28a71e305b00fe36057395759de5 (diff) |
web-demo: verify capability
-rw-r--r-- | examples/web-demo/index.html | 10 | ||||
-rw-r--r-- | examples/web-demo/src/index.js | 15 | ||||
-rw-r--r-- | examples/web-demo/style.css | 9 | ||||
-rw-r--r-- | src/eris.js | 43 |
4 files changed, 71 insertions, 6 deletions
diff --git a/examples/web-demo/index.html b/examples/web-demo/index.html index 66b2650..62d9dd3 100644 --- a/examples/web-demo/index.html +++ b/examples/web-demo/index.html @@ -13,7 +13,7 @@ <div id="notes"> <p>This is a demo of ERIS - An Encoding for Robust Immutable Storage.</p> - <p>ERIS encodes any content into uniformly sized (4kB) encrypted blocks. The original content can only be decoded with the URN (which contains the encryption key). </p> + <p>ERIS encodes any content into uniformly sized (4kB) encrypted blocks. The original content can only be decoded with the read capability (an URN which contains the encryption key). </p> </div> @@ -63,6 +63,9 @@ </details> <br> <button id="controls-decode">← Decode</button> + <br> + <br> + <button id="controls-verify">Verify</button> <br> <pre id="controls-error"></pre> @@ -73,8 +76,13 @@ <h2>Encoded</h2> <h3>Read capability</h3> + <p>The read capability allows content to be read</p> <input id="encoded-eris-read-cap" type="url"></input> + <h3>Verification capability</h3> + <p>The verification capability allows all blocks to be verified for integrity, but does not allow the content to be read</p> + <input id="encoded-eris-verification-cap" type="url"></input> + <div id="blocks"> <h3>Blocks</h3> <div id="block-container"> diff --git a/examples/web-demo/src/index.js b/examples/web-demo/src/index.js index 61815af..ea4db5f 100644 --- a/examples/web-demo/src/index.js +++ b/examples/web-demo/src/index.js @@ -91,11 +91,13 @@ async function main () { const controlsEncode = document.getElementById('controls-encode') const controlsDecode = document.getElementById('controls-decode') + const controlsVerify = document.getElementById('controls-verify') const controlsInputType = document.getElementById('controls-input-type') const controlsError = document.getElementById('controls-error') const controlsSuccess = document.getElementById('controls-success') const encodedErisReadCap = document.getElementById('encoded-eris-read-cap') + const encodedErisVerificationCap = document.getElementById('encoded-eris-verification-cap') const blockContainer = document.getElementById('block-container') // a ContentAddressableStorage based on a JavaScipt Map @@ -186,6 +188,8 @@ async function main () { try { const urn = await encode() encodedErisReadCap.value = urn + const verifyUrn = await ERIS.deriveVerificationCapability(urn) + encodedErisVerificationCap.value = verifyUrn renderBlocks(cas) setSuccess('Encoded!') } catch (err) { @@ -205,6 +209,17 @@ async function main () { } } + controlsVerify.onclick = async function (e) { + setSuccess('') + try { + const verificationCap = encodedErisVerificationCap.value + await ERIS.verify(verificationCap, cas) + setSuccess('Verification passed!') + } catch (err) { + setError(err) + } + } + inputLoadSampleVocabulary.onclick = function (e) { inputTextarea.value = signify controlsInputType.value = 'text/turtle' diff --git a/examples/web-demo/style.css b/examples/web-demo/style.css index d91edcb..3bafd3e 100644 --- a/examples/web-demo/style.css +++ b/examples/web-demo/style.css @@ -32,6 +32,11 @@ textarea { text-align: center; } +#controls button { + font-size: 18px; + font-family: monospace; +} + #controls-error { background-color: red; white-space: pre-wrap; @@ -54,6 +59,10 @@ textarea { width: 90%; } +#encoded-eris-verification-cap { + width: 90%; +} + #encoded-data { white-space: pre-wrap; max-width: 90%; diff --git a/src/eris.js b/src/eris.js index 6cfb877..e4ddaea 100644 --- a/src/eris.js +++ b/src/eris.js @@ -173,14 +173,14 @@ async function buildMerkleTree (input, verificationKey, cas) { return finalize(state, 0) } -function makeReadCapability (level, rootReference, readKey) { +function makeCapability(type, level, rootReference, readKey) { const cap = new Uint8Array(67) // Set version to 0 cap.set([0], 0) - // Set type to 0 (for read capability) - cap.set([0], 1) + // Set type + cap.set([0], type) // Set level cap.set([level], 2) @@ -241,7 +241,7 @@ async function put (content, cas = new NullContentAddressableStorage()) { const tree = await buildMerkleTree(paddedAndEncrypted, verificationKey, cas) - return makeReadCapability(tree.level, tree.rootReference, readKey) + return makeCapability(0, tree.level, tree.rootReference, readKey) } async function * decodeTree (cas, verificationKey, ref, nodeLevel, nodeCount) { @@ -317,10 +317,43 @@ async function get (capability, cas) { return unpadded } +async function verify (capability, cas) { + capability = decodeCapability(capability) + + var verificationKey + + if (capability.type !== 0) { + verificationKey = await crypto.derive_verification_key(capability.key) + } else { + verificationKey = capability.key + } + + const blockGenerator = decodeTree(cas, verificationKey, capability.rootReference, capability.level, 0) + + await concatBlocks(blockGenerator) + + return true +} + +async function deriveVerificationCapability (capability) { + capability = decodeCapability(capability) + + if (capability.type !== 0) { + throw new Error('Not a read capability') + } + + const verificationKey = await crypto.derive_verification_key(capability.key) + return makeCapability(1, capability.level, capability.rootReference, verificationKey) +} + module.exports = { ContentAddressableStorage: ContentAddressableStorage, NullContentAddressableStorage: NullContentAddressableStorage, MapContentAddressableStorage: MapContentAddressableStorage, + put: put, - get: get + get: get, + verify: verify, + + deriveVerificationCapability: deriveVerificationCapability } |