Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

replaces request-promise with axios #7

Open
wants to merge 25 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
Show all changes
25 commits
Select commit Hold shift + click to select a range
5d33a53
refactor: replaces request-promise with axios, and other fixes
saileshbro Mar 27, 2021
50fcf18
Upgrade to GitHub-native Dependabot
dependabot-preview[bot] Apr 29, 2021
9fbd313
Merge pull request #1 from saileshbro/dependabot/add-v2-config-file
saileshbro Apr 30, 2021
451b6dd
build(deps): bump nodemon from 2.0.7 to 2.0.9
dependabot[bot] Jun 30, 2021
555e17b
Merge pull request #3 from saileshbro/dependabot/npm_and_yarn/nodemon…
saileshbro Jul 2, 2021
5022760
build(deps): bump nodemon from 2.0.9 to 2.0.12
dependabot[bot] Jul 12, 2021
6b69487
Merge pull request #6 from saileshbro/dependabot/npm_and_yarn/nodemon…
saileshbro Jul 13, 2021
bd4764e
build(deps): bump axios from 0.21.1 to 0.21.4
dependabot[bot] Sep 6, 2021
a45dce0
Merge pull request #7 from saileshbro/dependabot/npm_and_yarn/axios-0…
saileshbro Sep 7, 2021
d02b3e6
build(deps): bump nodemon from 2.0.12 to 2.0.13
dependabot[bot] Sep 23, 2021
b8d10bc
Merge pull request #8 from saileshbro/dependabot/npm_and_yarn/nodemon…
saileshbro Sep 28, 2021
9a5795c
build(deps): bump axios from 0.21.4 to 0.22.0
dependabot[bot] Oct 1, 2021
2638999
Merge pull request #9 from saileshbro/dependabot/npm_and_yarn/axios-0…
saileshbro Oct 5, 2021
746fc33
build(deps): bump axios from 0.22.0 to 0.23.0
dependabot[bot] Oct 12, 2021
bce97c3
Merge pull request #10 from saileshbro/dependabot/npm_and_yarn/axios-…
saileshbro Oct 18, 2021
761f3ee
build(deps): bump axios from 0.23.0 to 0.24.0
dependabot[bot] Oct 25, 2021
a2ef428
Merge pull request #12 from saileshbro/dependabot/npm_and_yarn/axios-…
saileshbro Oct 26, 2021
e2662db
build(deps): bump nodemon from 2.0.13 to 2.0.14
dependabot[bot] Oct 26, 2021
2098fef
Merge pull request #11 from saileshbro/dependabot/npm_and_yarn/nodemo…
saileshbro Oct 27, 2021
dbe4d0d
build(deps): bump nodemon from 2.0.14 to 2.0.15
dependabot[bot] Nov 9, 2021
ba8cdce
Merge pull request #13 from saileshbro/dependabot/npm_and_yarn/nodemo…
saileshbro Nov 14, 2021
7cc2a47
build(deps): bump body-parser from 1.19.0 to 1.19.1
dependabot[bot] Dec 10, 2021
d4b965e
Merge pull request #14 from saileshbro/dependabot/npm_and_yarn/body-p…
saileshbro Dec 14, 2021
df9bcb7
build(deps): bump express from 4.17.1 to 4.17.2
dependabot[bot] Dec 17, 2021
ec0e797
Merge pull request #15 from saileshbro/dependabot/npm_and_yarn/expres…
saileshbro Dec 18, 2021
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
8 changes: 8 additions & 0 deletions .github/dependabot.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
version: 2
updates:
- package-ecosystem: npm
directory: "/"
schedule:
interval: daily
time: "23:15"
open-pull-requests-limit: 10
336 changes: 169 additions & 167 deletions dev/blockchain.js
Original file line number Diff line number Diff line change
@@ -1,169 +1,171 @@
const sha256 = require('sha256');
const currentNodeUrl = process.argv[3];
const uuid = require('uuid/v1');
const sha256 = require('sha256')
const currentNodeUrl = process.argv[3]
const { v1: uuid } = require('uuid')

function Blockchain() {
this.chain = [];
this.pendingTransactions = [];

this.currentNodeUrl = currentNodeUrl;
this.networkNodes = [];

this.createNewBlock(100, '0', '0');
};


Blockchain.prototype.createNewBlock = function(nonce, previousBlockHash, hash) {
const newBlock = {
index: this.chain.length + 1,
timestamp: Date.now(),
transactions: this.pendingTransactions,
nonce: nonce,
hash: hash,
previousBlockHash: previousBlockHash
};

this.pendingTransactions = [];
this.chain.push(newBlock);

return newBlock;
};


Blockchain.prototype.getLastBlock = function() {
return this.chain[this.chain.length - 1];
};


Blockchain.prototype.createNewTransaction = function(amount, sender, recipient) {
const newTransaction = {
amount: amount,
sender: sender,
recipient: recipient,
transactionId: uuid().split('-').join('')
};

return newTransaction;
};


Blockchain.prototype.addTransactionToPendingTransactions = function(transactionObj) {
this.pendingTransactions.push(transactionObj);
return this.getLastBlock()['index'] + 1;
};


Blockchain.prototype.hashBlock = function(previousBlockHash, currentBlockData, nonce) {
const dataAsString = previousBlockHash + nonce.toString() + JSON.stringify(currentBlockData);
const hash = sha256(dataAsString);
return hash;
};


Blockchain.prototype.proofOfWork = function(previousBlockHash, currentBlockData) {
let nonce = 0;
let hash = this.hashBlock(previousBlockHash, currentBlockData, nonce);
while (hash.substring(0, 4) !== '0000') {
nonce++;
hash = this.hashBlock(previousBlockHash, currentBlockData, nonce);
}

return nonce;
};



Blockchain.prototype.chainIsValid = function(blockchain) {
let validChain = true;

for (var i = 1; i < blockchain.length; i++) {
const currentBlock = blockchain[i];
const prevBlock = blockchain[i - 1];
const blockHash = this.hashBlock(prevBlock['hash'], { transactions: currentBlock['transactions'], index: currentBlock['index'] }, currentBlock['nonce']);
if (blockHash.substring(0, 4) !== '0000') validChain = false;
if (currentBlock['previousBlockHash'] !== prevBlock['hash']) validChain = false;
};

const genesisBlock = blockchain[0];
const correctNonce = genesisBlock['nonce'] === 100;
const correctPreviousBlockHash = genesisBlock['previousBlockHash'] === '0';
const correctHash = genesisBlock['hash'] === '0';
const correctTransactions = genesisBlock['transactions'].length === 0;

if (!correctNonce || !correctPreviousBlockHash || !correctHash || !correctTransactions) validChain = false;

return validChain;
};


Blockchain.prototype.getBlock = function(blockHash) {
let correctBlock = null;
this.chain.forEach(block => {
if (block.hash === blockHash) correctBlock = block;
});
return correctBlock;
};


Blockchain.prototype.getTransaction = function(transactionId) {
let correctTransaction = null;
let correctBlock = null;

this.chain.forEach(block => {
block.transactions.forEach(transaction => {
if (transaction.transactionId === transactionId) {
correctTransaction = transaction;
correctBlock = block;
};
});
});

return {
transaction: correctTransaction,
block: correctBlock
};
};


Blockchain.prototype.getAddressData = function(address) {
const addressTransactions = [];
this.chain.forEach(block => {
block.transactions.forEach(transaction => {
if(transaction.sender === address || transaction.recipient === address) {
addressTransactions.push(transaction);
};
});
});

let balance = 0;
addressTransactions.forEach(transaction => {
if (transaction.recipient === address) balance += transaction.amount;
else if (transaction.sender === address) balance -= transaction.amount;
});

return {
addressTransactions: addressTransactions,
addressBalance: balance
};
};






module.exports = Blockchain;














this.chain = []
this.pendingTransactions = []

this.currentNodeUrl = currentNodeUrl
this.networkNodes = []

this.createNewBlock(100, '0', '0')
}

Blockchain.prototype.createNewBlock = function (
nonce,
previousBlockHash,
hash,
) {
const newBlock = {
index: this.chain.length + 1,
timestamp: Date.now(),
transactions: this.pendingTransactions,
nonce: nonce,
hash: hash,
previousBlockHash: previousBlockHash,
}

this.pendingTransactions = []
this.chain.push(newBlock)

return newBlock
}

Blockchain.prototype.getLastBlock = function () {
return this.chain[this.chain.length - 1]
}

Blockchain.prototype.createNewTransaction = function (
amount,
sender,
recipient,
) {
const newTransaction = {
amount: amount,
sender: sender,
recipient: recipient,
transactionId: uuid().split('-').join(''),
}

return newTransaction
}

Blockchain.prototype.addTransactionToPendingTransactions = function (
transactionObj,
) {
this.pendingTransactions.push(transactionObj)
return this.getLastBlock()['index'] + 1
}

Blockchain.prototype.hashBlock = function (
previousBlockHash,
currentBlockData,
nonce,
) {
const dataAsString =
previousBlockHash + nonce.toString() + JSON.stringify(currentBlockData)
const hash = sha256(dataAsString)
return hash
}

Blockchain.prototype.proofOfWork = function (
previousBlockHash,
currentBlockData,
) {
let nonce = 0
let hash = this.hashBlock(previousBlockHash, currentBlockData, nonce)
while (hash.substring(0, 4) !== '0000') {
nonce++
hash = this.hashBlock(previousBlockHash, currentBlockData, nonce)
}

return nonce
}

Blockchain.prototype.chainIsValid = function (blockchain) {
let validChain = true

for (var i = 1; i < blockchain.length; i++) {
const currentBlock = blockchain[i]
const prevBlock = blockchain[i - 1]
const blockHash = this.hashBlock(
prevBlock['hash'],
{
transactions: currentBlock['transactions'],
index: currentBlock['index'],
},
currentBlock['nonce'],
)
if (blockHash.substring(0, 4) !== '0000') validChain = false
if (currentBlock['previousBlockHash'] !== prevBlock['hash'])
validChain = false
}

const genesisBlock = blockchain[0]
const correctNonce = genesisBlock['nonce'] === 100
const correctPreviousBlockHash = genesisBlock['previousBlockHash'] === '0'
const correctHash = genesisBlock['hash'] === '0'
const correctTransactions = genesisBlock['transactions'].length === 0

if (
!correctNonce ||
!correctPreviousBlockHash ||
!correctHash ||
!correctTransactions
)
validChain = false

return validChain
}

Blockchain.prototype.getBlock = function (blockHash) {
let correctBlock = null
this.chain.forEach(block => {
if (block.hash === blockHash) correctBlock = block
})
return correctBlock
}

Blockchain.prototype.getTransaction = function (transactionId) {
let correctTransaction = null
let correctBlock = null

this.chain.forEach(block => {
block.transactions.forEach(transaction => {
if (transaction.transactionId === transactionId) {
correctTransaction = transaction
correctBlock = block
}
})
})

return {
transaction: correctTransaction,
block: correctBlock,
}
}

Blockchain.prototype.getAddressData = function (address) {
const addressTransactions = []
this.chain.forEach(block => {
block.transactions.forEach(transaction => {
if (transaction.sender === address || transaction.recipient === address) {
addressTransactions.push(transaction)
}
})
})

let balance = 0
addressTransactions.forEach(transaction => {
if (transaction.recipient === address) balance += transaction.amount
else if (transaction.sender === address) balance -= transaction.amount
})

return {
addressTransactions: addressTransactions,
addressBalance: balance,
}
}

module.exports = Blockchain
Loading