some kind of blockchain is been created
This commit is contained in:
parent
b279e416a3
commit
c18318f5de
|
@ -0,0 +1 @@
|
|||
asdasd.js
|
|
@ -0,0 +1,38 @@
|
|||
"use strict";
|
||||
|
||||
// Initializin of the variable for instance of asdasd
|
||||
let blockchain;
|
||||
|
||||
function init_asdasd(asdasd) {
|
||||
if (indexedDB instanceof IDBFactory) {
|
||||
// Supports indexedDB
|
||||
|
||||
// Initializing of asdasd
|
||||
blockchain = new asdasd(
|
||||
"test",
|
||||
(text, settings, previous, created) => {
|
||||
let hash = "";
|
||||
let nonce = 0;
|
||||
|
||||
do {
|
||||
hash = nobleHashes.utils.bytesToHex(nobleHashes.blake3(
|
||||
previous + text + created + ++nonce,
|
||||
settings,
|
||||
));
|
||||
} while (hash.substring(0, 3) !== "000");
|
||||
|
||||
return { nonce, hash };
|
||||
},
|
||||
);
|
||||
} else {
|
||||
// Not supports indexed
|
||||
|
||||
// Show the error
|
||||
document.getElementsByTagName("main")[0].innerText =
|
||||
"Your browser does not support indexedDB which is used for asdasd blockchain";
|
||||
}
|
||||
}
|
||||
|
||||
if (typeof window.asdasd === "function") (() => init_asdasd(asdasd))();
|
||||
else {document.addEventListener("asdasd.initialized", (e) =>
|
||||
init_asdasd(e.asdasd));}
|
|
@ -0,0 +1,17 @@
|
|||
<!doctype html>
|
||||
<html lang="en">
|
||||
|
||||
<head>
|
||||
<meta charset="utf-8">
|
||||
<meta name="viewport" content="width=device-width, initial-scale=1">
|
||||
<title>Example</title>
|
||||
</head>
|
||||
|
||||
<body>
|
||||
<p>see "example.js"</p>
|
||||
<script src="noble-hashes.js" defer></script>
|
||||
<script src="asdasd.js" defer></script>
|
||||
<script src="example.js" defer></script>
|
||||
</body>
|
||||
|
||||
</html>
|
|
@ -0,0 +1 @@
|
|||
noble-hashes.js
|
|
@ -0,0 +1,235 @@
|
|||
"use strict";
|
||||
|
||||
if (typeof window.asdasd !== "function") {
|
||||
// Not initialized
|
||||
|
||||
// Initializing of the class in global namespace
|
||||
window.asdasd = class asdasd {
|
||||
// Name of the database in IndexDB
|
||||
name;
|
||||
|
||||
// Instance of the database session
|
||||
db;
|
||||
|
||||
/**
|
||||
* Function for generating hash (BLAKE3)
|
||||
*
|
||||
* @param {string} text Text of message
|
||||
* @param {object} settings Settings for generating hash
|
||||
* @param {string} previous Hash of the previout message in the chain
|
||||
* @param {string} created Date of message creation (unixtime)
|
||||
*
|
||||
* @return {string} Symmetric hash
|
||||
*/
|
||||
static blake3;
|
||||
|
||||
constructor(name, blake3) {
|
||||
// Reinitializing Blake3
|
||||
if (typeof blake3 === "function") this.blake3 = blake3;
|
||||
|
||||
if (typeof name === "string" && typeof this.blake3 === "function") {
|
||||
// Received name of the the database and implementation of BLAKE3
|
||||
|
||||
// Write name of the the database to property
|
||||
this.name = name;
|
||||
|
||||
// Send request to the database
|
||||
const request = indexedDB.open(this.name, 1);
|
||||
|
||||
// Listening: "upgradeneeded"
|
||||
request.addEventListener("upgradeneeded", (e) => {
|
||||
// Write response to property
|
||||
this.db = e.target.result;
|
||||
|
||||
// Initializing of instance of messages table in the database
|
||||
const messages = this.db.createObjectStore("messages", {
|
||||
keyPath: "id",
|
||||
});
|
||||
|
||||
// Index: text
|
||||
messages.createIndex("text", "text", { unique: false });
|
||||
|
||||
// Index: date of creation
|
||||
messages.createIndex("created", "created", { unique: false });
|
||||
|
||||
// Index: hash
|
||||
messages.createIndex("hash", "hash", { unique: true });
|
||||
|
||||
// Dispatch event: "initialized"
|
||||
document.dispatchEvent(
|
||||
new CustomEvent(`asdasd.${this.name}.initialized`),
|
||||
);
|
||||
});
|
||||
|
||||
// Listening: "error"
|
||||
request.addEventListener(
|
||||
"error",
|
||||
(e) => alert("the database: " + e.target.error),
|
||||
);
|
||||
|
||||
// Listening: "success"
|
||||
request.addEventListener("success", (e) => this.db = e.target.result);
|
||||
|
||||
this.pending = [];
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* Count the number of messages in the database
|
||||
*/
|
||||
count() {
|
||||
return new Promise((resolve, reject) => {
|
||||
// Initializing of transaction to the database
|
||||
const transaction = this.db.transaction(["messages"]);
|
||||
|
||||
// Initializing of table in the database
|
||||
const messages = transaction.objectStore("messages");
|
||||
|
||||
// Send request to the database
|
||||
const request = messages.count();
|
||||
|
||||
// Listening: "error"
|
||||
request.addEventListener("error", (e) => reject(e));
|
||||
|
||||
// Listening: "success"
|
||||
request.addEventListener("success", (e) => resolve(e.target.result));
|
||||
});
|
||||
}
|
||||
|
||||
write(message) {
|
||||
return new Promise((resolve, reject) => {
|
||||
this.count().then(
|
||||
(amount) => {
|
||||
this.last().then(
|
||||
(last) => {
|
||||
// Initializing of transaction to the database
|
||||
const transaction = this.db.transaction(
|
||||
["messages"],
|
||||
"readwrite",
|
||||
);
|
||||
|
||||
// Initializing of table in the database
|
||||
const messages = transaction.objectStore("messages");
|
||||
|
||||
// Initializing of the previous message in the chain hash
|
||||
const previous = last.hash ?? "popa";
|
||||
|
||||
// Initializing of message creating date
|
||||
const created = Date.now();
|
||||
|
||||
// Generate a hash of the message
|
||||
const generated = this.blake3(
|
||||
message,
|
||||
{
|
||||
dkLen: Math.ceil(message.length / 64) * 64,
|
||||
},
|
||||
previous,
|
||||
created,
|
||||
);
|
||||
|
||||
// Packing the block and send request to the database
|
||||
const request = messages.add({
|
||||
id: amount,
|
||||
nonce: generated.nonce,
|
||||
hash: generated.hash,
|
||||
previous,
|
||||
created,
|
||||
});
|
||||
|
||||
// Listening: "error"
|
||||
request.addEventListener("error", (e) => reject(e));
|
||||
|
||||
// Listening: "success"
|
||||
request.addEventListener(
|
||||
"success",
|
||||
(e) => resolve(e.target.result),
|
||||
);
|
||||
},
|
||||
(last) => reject(last),
|
||||
);
|
||||
},
|
||||
(amount) => reject(amount),
|
||||
);
|
||||
});
|
||||
}
|
||||
|
||||
/**
|
||||
* Read the message from the database
|
||||
*/
|
||||
read(id) {
|
||||
return new Promise((resolve, reject) => {
|
||||
if (typeof id === "number") {
|
||||
// Initializing of transaction to the database
|
||||
const transaction = this.db.transaction(["messages"]);
|
||||
|
||||
// Initializing of table in the database
|
||||
const messages = transaction.objectStore("messages");
|
||||
|
||||
// Send request to the database
|
||||
const request = messages.get(id);
|
||||
|
||||
// Listening: "error"
|
||||
request.addEventListener("error", (e) => reject(e));
|
||||
|
||||
// Listening: "success"
|
||||
request.addEventListener("success", (e) => resolve(e.target.result));
|
||||
} else reject("Could not initialize id");
|
||||
});
|
||||
}
|
||||
|
||||
/**
|
||||
* Read the latest message from the database
|
||||
*/
|
||||
last() {
|
||||
return new Promise((resolve, reject) => {
|
||||
this.count().then(
|
||||
(amount) => {
|
||||
if (amount > 0) {
|
||||
this.read(amount - 1).then(
|
||||
(last) => resolve(last),
|
||||
(last) => reject(last),
|
||||
);
|
||||
} else resolve({});
|
||||
},
|
||||
(amount) => reject(amount),
|
||||
);
|
||||
});
|
||||
}
|
||||
|
||||
/**
|
||||
* Read all messages from the database
|
||||
*/
|
||||
all() {
|
||||
return new Promise((resolve, reject) => {
|
||||
// Initializing of transaction to the database
|
||||
const transaction = this.db.transaction(["messages"]);
|
||||
|
||||
// Initializing of table in the database
|
||||
const messages = transaction.objectStore("messages");
|
||||
|
||||
// Send request to the database
|
||||
const request = messages.getAll();
|
||||
|
||||
// Listening: "error"
|
||||
request.addEventListener("error", (e) => reject(e));
|
||||
|
||||
// Listening: "success"
|
||||
request.addEventListener("success", (e) => resolve(e.target.result));
|
||||
});
|
||||
}
|
||||
};
|
||||
|
||||
// Dispatch event: "initialized"
|
||||
document.dispatchEvent(
|
||||
new CustomEvent("asdasd.initialized", {
|
||||
detail: { asdasd: window.asdasd },
|
||||
}),
|
||||
);
|
||||
}
|
||||
|
||||
// Dispatch event: "loaded"
|
||||
document.dispatchEvent(
|
||||
new CustomEvent("asdasd.loaded", {
|
||||
detail: { asdasd: window.asdasd },
|
||||
}),
|
||||
);
|
File diff suppressed because it is too large
Load Diff
Loading…
Reference in New Issue