lycan/jspow/index.js

90 lines
2.6 KiB
JavaScript
Raw Normal View History

2020-10-07 22:58:00 +00:00
/*
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 <https://www.gnu.org/licenses/>.
*/
// 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)
}
2020-10-12 09:23:20 +00:00
metadata['c'] = -9999999
metadata['n'] = Math.floor(Math.random() * Math.floor(10000)) + Math.floor(Math.random() * Math.floor(10000))
var difficultyCounter = 0
2020-10-07 22:58:00 +00:00
while (true){
2020-10-12 09:23:20 +00:00
difficultyCounter = 0
2020-10-07 22:58:00 +00:00
let metadataString = encoder.encode(JSON.stringify(metadata) + "\n")
let arr = new Uint8Array(metadataString.length + data.length)
arr.set(metadataString)
arr.set(data, metadataString.length)
2020-10-12 09:23:20 +00:00
hash = doHashHex(arr)
2020-10-07 22:58:00 +00:00
2020-10-12 09:23:20 +00:00
for (var i = 0; i < difficulty; i++){
if (hash[i] == '0'){
2020-10-07 22:58:00 +00:00
difficultyCounter += 1
if (difficultyCounter === difficulty){
return arr
}
}
else{
break
}
}
metadata['c'] += 1
2020-10-13 07:39:58 +00:00
/*
2020-10-12 09:23:20 +00:00
if (metadata['c'] % 10000 == 0){
console.debug(metadata['c'])
}
2020-10-13 07:39:58 +00:00
*/
2020-10-07 22:58:00 +00:00
}
}
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
}