-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathhelper.js
37 lines (31 loc) · 1.09 KB
/
helper.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
const Block = require("./def/Block.js");
const blockchain = require("./def/Blockchain.js");
module.exports = {
validate: (transaction, BlockChain, callback) => {
const sender = transaction.from;
const receiver = transaction.to;
const description = transaction.desc;
const amount = transaction.amount;
let newBlockchain = new blockchain();
newBlockchain.chain = BlockChain.chain;
const balance = newBlockchain.calculateBalance(sender);
let block = null;
if ( newBlockchain.checkAmount(balance, amount) ) {
const index = newBlockchain.getLength();
const timestamp = new Date().getTime();
block = new Block(index, timestamp,
description, sender,
receiver, amount);
newBlockchain.addBlock(block);
}
callback(block);
},
findWithAttr: (array, attr, value) => {
for(var i = 0; i < array.length; i += 1) {
if(array[i][attr] === value) {
return i;
}
}
return -1;
}
}