aboutsummaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorpukkamustard <pukkamustard@posteo.net>2020-06-05 18:55:52 +0200
committerpukkamustard <pukkamustard@posteo.net>2020-06-05 18:55:52 +0200
commit771ff2ff01af7af792b1957acd381216edbfa559 (patch)
treed99611a564a3ebd91a05b0b70e2074d1d99db57e
parent8732b575759469c38b2280f720d2ff34205ad0f6 (diff)
eris encoding works
-rw-r--r--base32.js96
-rw-r--r--eris/index.js29
2 files changed, 122 insertions, 3 deletions
diff --git a/base32.js b/base32.js
new file mode 100644
index 0000000..340602b
--- /dev/null
+++ b/base32.js
@@ -0,0 +1,96 @@
+/* Copyright (c) 2020 pukkamustard
+ * Copyright (c) 2016-2017 Linus Unneb├Ąck
+ *
+ * Permission is hereby granted, free of charge, to any person obtaining a copy
+ * of this software and associated documentation files (the "Software"), to deal
+ * in the Software without restriction, including without limitation the rights
+ * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
+ * copies of the Software, and to permit persons to whom the Software is
+ * furnished to do so, subject to the following conditions:
+ *
+ * The above copyright notice and this permission notice shall be included in all
+ * copies or substantial portions of the Software.
+ *
+ * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
+ * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
+ * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
+ * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
+ * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
+ * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
+ * SOFTWARE.
+ *
+ */
+
+/* Commentary:
+ * This code is adapted from https://github.com/LinusU/base32-decode and https://github.com/LinusU/base32-decode (both MIT licensed) with following modifications:
+ *
+ * - combine encode and decode in single module
+ * - only support RFC4648 alphabet
+ * - disable padding
+ *
+ * */
+
+// RFC4648
+const alphabet = 'ABCDEFGHIJKLMNOPQRSTUVWXYZ234567'
+
+function encode (buffer) {
+ var length = buffer.byteLength
+ var view = new Uint8Array(buffer)
+
+ var bits = 0
+ var value = 0
+ var output = ''
+
+ for (var i = 0; i < length; i++) {
+ value = (value << 8) | view[i]
+ bits += 8
+
+ while (bits >= 5) {
+ output += alphabet[(value >>> (bits - 5)) & 31]
+ bits -= 5
+ }
+ }
+
+ if (bits > 0) {
+ output += alphabet[(value << (5 - bits)) & 31]
+ }
+
+ return output
+}
+
+function readChar (alphabet, char) {
+ var idx = alphabet.indexOf(char)
+
+ if (idx === -1) {
+ throw new Error('Invalid character found: ' + char)
+ }
+
+ return idx
+}
+
+function decode (input) {
+ var length = input.length
+
+ var bits = 0
+ var value = 0
+
+ var index = 0
+ var output = new Uint8Array((length * 5 / 8) | 0)
+
+ for (var i = 0; i < length; i++) {
+ value = (value << 5) | readChar(alphabet, input[i])
+ bits += 5
+
+ if (bits >= 8) {
+ output[index++] = (value >>> (bits - 8)) & 255
+ bits -= 8
+ }
+ }
+
+ return output.buffer
+}
+
+module.exports = {
+ encode: encode,
+ decode: decode
+}
diff --git a/eris/index.js b/eris/index.js
index bb26b0d..312b670 100644
--- a/eris/index.js
+++ b/eris/index.js
@@ -1,4 +1,5 @@
const crypto = require('./crypto.js')
+const base32 = require('../base32.js')
function ContentAddressableStorage (put, get) {
this.put = put
@@ -154,6 +155,27 @@ async function buildMerkleTree (input, verificationKey, cas) {
return finalize(state, 0)
}
+function makeReadCapability (level, rootReference, readKey) {
+ const cap = new Uint8Array(67)
+
+ // Set version to 0
+ cap.set([0], 0)
+
+ // Set type to 1 (for read capability)
+ cap.set([1], 1)
+
+ // Set level
+ cap.set([level], 2)
+
+ // Set root reference
+ cap.set(rootReference, 3)
+
+ // Set key
+ cap.set(readKey, 35)
+
+ return "urn:erisx:".concat(base32.encode(cap))
+}
+
async function put (content, cas = new NullContentAddressableStorage()) {
// read key is the hash of the content
const readKey = await crypto.hash(content)
@@ -168,12 +190,13 @@ async function put (content, cas = new NullContentAddressableStorage()) {
// derive the verification key from the read key
const verificationKey = await crypto.derive_verification_key(readKey)
- return buildMerkleTree(paddedAndEncrypted, verificationKey, cas)
+ const tree = await buildMerkleTree(paddedAndEncrypted, verificationKey, cas)
+
+ return makeReadCapability(tree.level, tree.rootReference, readKey)
}
module.exports = {
ContentAddressableStorage: ContentAddressableStorage,
NullContentAddressableStorage: NullContentAddressableStorage,
- put: put,
- blockGenerator: blockGenerator
+ put: put
}