1.0.2 • Published 1 year ago

@dogenet/cryptodb v1.0.2

Weekly downloads
-
License
ISC
Repository
-
Last release
1 year ago

Introduction

This Node.js project demonstrates how to create a for storing and retrieving data files using a blockchain based technology. The project uses the Express.js framework, the Node.js fs module for file operations, and the crypto module for hashing and signing data.

The project is designed to be simple and easy to understand, making it a good starting point for developers who want to learn about blockchain-based data storage.

Features

  • Store data files using a blockchain
  • Retrieve data files using their hash
  • Simple and easy to understand code

Installation

Clone the repository to your local machine using the command:

git clone https://github.com/wrappedcelio/cryptodb.git

Install the dependencies by running the command:

npm install

Example Usage

storing & retrieving file

const db = require("@dogenet/cryptodb");
const fs = require("fs");
const storage = new db.storage();

const buffer = fs.readFileSync("./image.png")

storage.addData(buffer).then(res => {
  console.log(res)
});

storing & retrieving text

const db = require("@dogenet/cryptodb");
const storage = new db.storage();

storage.addData("hello, world!").then(res => {
  console.log(res)
});
// hash returned
storage.getData(hash).then(res => {
  console.log(Buffer.from(res).toString)
})
// stringify it if you stored json

Creating web api

const db = require("@dogenet/cryptodb");
const storage = new db.storage();
storage.createWebInstance(3000); // port

using web api

const axios = require("axios");

axios.post(`<your_domain_name>/data/add`, {
  data: "gm"
}).then(res => {
  // hash returned 
  console.log(res.data);
});

const hash = "hash_recieved"
axios.get(`<your_domain_name>/data/get/${hash}`).then(res => {
  console.log(res.data)
  // gm
});

for images,audio,video etc. they can be retrieved but they are returned it .dat format which you can read like:

const stream = fs.createReadStream(blob); stream.on('open', () => { stream.pipe(res); }); stream.on('error', (err) => { console.error(Error reading data file ${fileName}: ${err.message});

all hashes are stored in a blockchain like structure in the module /blockchain/chain.json

{"hash":"32735980f8d10e52a840dfd472b5b45c5d3fa426beb5c68d26516cc859bfc61b","previousHash":"a7f52f0b676390157f726113e922470a9a0b0f7bfaccd77ab8606c39ea3ce98d","user":"alice","timestamp":1677490521505,"signature":"3405809f9951e2a787711497b73feb5c009be11c2ecc023704c797be0222b86271e9e1320aa9422241ba4eec0e3a5d738dc51646d0eefae1e7bfb05fa3f466dde669d7067dfc249c4c4a8ff8e705f8613a75f4c8a8a2984a6ef3449bcd0590fed205d6f132bd2d180284605bec3a0df08d0a0b28e61fc74ab4d3c26752dcc45fcf2a59c4590fd935d914e87417f4298233de77b41e7ff3a543e845e5fac0de7afa1d1657b96176963686b5e8bd9f40d21cda6d782a2855556e06ffcf013f1b620650022269714bf0f941f44f481f4d7753cfe9fee87e6330c1aa00cfdb8d48fd3271160c7c49c8b82cc95b7514e73f1716056c39ac78f8ecfb332dd17c1b465f"}