From 851850ef554192f36172b1ae7ee8220873669be3 Mon Sep 17 00:00:00 2001 From: Kevin Froman Date: Wed, 7 Oct 2020 22:58:00 +0000 Subject: [PATCH] added modified jspow to repo --- index.html | 2 +- jspow/.gitignore | 1 + jspow/README.md | 3 ++ jspow/index.js | 83 ++++++++++++++++++++++++++++++++++++++++++++++ jspow/package.json | 15 +++++++++ onionr-jspow | 1 - 6 files changed, 103 insertions(+), 2 deletions(-) create mode 100644 jspow/.gitignore create mode 100644 jspow/README.md create mode 100644 jspow/index.js create mode 100644 jspow/package.json delete mode 160000 onionr-jspow diff --git a/index.html b/index.html index 2a9bdce..ba7497d 100644 --- a/index.html +++ b/index.html @@ -16,7 +16,7 @@ - + diff --git a/jspow/.gitignore b/jspow/.gitignore new file mode 100644 index 0000000..dbf0821 --- /dev/null +++ b/jspow/.gitignore @@ -0,0 +1 @@ +node_modules/* \ No newline at end of file diff --git a/jspow/README.md b/jspow/README.md new file mode 100644 index 0000000..c87c8c8 --- /dev/null +++ b/jspow/README.md @@ -0,0 +1,3 @@ +# OnionrPOW-js + +lazy implementation of onionr proof of work \ No newline at end of file diff --git a/jspow/index.js b/jspow/index.js new file mode 100644 index 0000000..6dc790e --- /dev/null +++ b/jspow/index.js @@ -0,0 +1,83 @@ +/* +Copyright 2020 Kevin Froman + +This program is free software: you can redistribute it and/or modify +it under the terms of the GNU General Public License as published by +the Free Software Foundation, either version 3 of the License, or +(at your option) any later version. + +This program is distributed in the hope that it will be useful, +but WITHOUT ANY WARRANTY; without even the implied warranty of +MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the +GNU General Public License for more details. + +You should have received a copy of the GNU General Public License +along with this program. If not, see . +*/ + +// version = 0.0.0 + +class Block { + constructor(meta, sig, signer, time, pow=0, c=0) { + this.meta = meta + this.sig = sig + this.signer = signer + this.time = time + this.c = c + } + } + + +let doHash = function(data){ + const shaObj = new jsSHA("SHA3-256", "UINT8ARRAY", { encoding: "UTF8" }); + shaObj.update(data) + return shaObj.getHash("UINT8ARRAY"); +} +let doHashHex = function(data){ + const shaObj = new jsSHA("SHA3-256", "UINT8ARRAY", { encoding: "UTF8" }); + shaObj.update(data) + return shaObj.getHash("HEX"); +} + +function doPow(metadata, data, difficulty){ + // Inefficient single threaded proof of work. Accepting better implementations! + var encoder = new TextEncoder("utf-8") + if (typeof data == 'string'){ + data = encoder.encode(data) + } + + metadata['c'] = 0 + metadata['n'] = Math.floor(Math.random() * Math.floor(10000)) + Math.floor(Math.random() * Math.floor(10000)) + + while (true){ + var difficultyCounter = 0 + let metadataString = encoder.encode(JSON.stringify(metadata) + "\n") + let arr = new Uint8Array(metadataString.length + data.length) + arr.set(metadataString) + arr.set(data, metadataString.length) + hash = doHash(arr) + + for (var i = 0; i < hash.length; i++){ + if (hash[i] == 0){ + difficultyCounter += 1 + if (difficultyCounter === difficulty){ + return arr + } + } + else{ + break + } + } + metadata['c'] += 1 + } + +} + +function unserialize(str, theClass) { + var instance = new theClass() // NOTE: if your constructor checks for unpassed arguments, then just pass dummy ones to prevent throwing an error + var serializedObject = JSON.parse(str) + Object.assign(instance, serializedObject) + return instance +} + + diff --git a/jspow/package.json b/jspow/package.json new file mode 100644 index 0000000..ed8f3b3 --- /dev/null +++ b/jspow/package.json @@ -0,0 +1,15 @@ +{ + "name": "onionr-jspow", + "version": "0.0.0", + "description": "Onionr POW JS Implementation", + "main": "index.js", + "type": "module", + "scripts": { + "test": "echo \"Error: no test specified\" && exit 1" + }, + "author": "", + "license": "GPL-3.0-or-later", + "dependencies": { + "sha3": "^2.1.3" + } +} diff --git a/onionr-jspow b/onionr-jspow deleted file mode 160000 index abd1872..0000000 --- a/onionr-jspow +++ /dev/null @@ -1 +0,0 @@ -Subproject commit abd1872a9174c983a75f2ffe102dab72da706b14