-
Notifications
You must be signed in to change notification settings - Fork 0
/
utils.js
109 lines (84 loc) · 2.92 KB
/
utils.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
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
const PrivateKey = require("@hiveio/hive-js/lib/auth/ecc/src/key_private");
const dhive = require("@hiveio/dhive");
const crypto = require("crypto");
const config = require("./config.json");
const FlatDB = require('flat-db');
// configure path to storage dir
FlatDB.configure({
dir: './storage',
});
// since now, everything will be saved under ./storage
// create Movie collection with schema
const Blocks = new FlatDB.Collection('movies', {
block: {},
witness: "",
signature: "",
number: 0,
});
// Generate a new set of hive private keys
const generateMasterKey = () => {
const masterKey = crypto.randomBytes(32);
return PrivateKey.fromBuffer(masterKey);
};
const fromWif = (wif) => {
return PrivateKey.fromWif(wif);
};
function addBlockToChain(json = null) {
if (json === null) {
return [false, "ERROR_NO_TRANSACTION"];
}
let previousBlockHash = "0000000000000000000000000000000000000000000000000000000000000000";
const count = Blocks.count();
if (count > 0) {
const [previousBlock] = Blocks.find({number: count - 1}).run();
delete previousBlock["_id_"];
delete previousBlock["_ts_"];
// create a sha-256 hash of the previous block
const sha256Hash = crypto.createHash("sha256");
// hash the string
// and set the output format
previousBlockHash = sha256Hash.update(JSON.stringify(previousBlock)).digest("hex");
}
if (json.data && json.signature && json.timestamp) {
try {
// Use dhive to recover the public key from the signature
const actualKey = dhive.Signature.fromString(json.signature).recover(dhive.cryptoUtils.sha256(JSON.stringify(json.data)));
if (actualKey.toString() !== config.network_key_public) {
return [false, "ERROR_INVALID_SIGNATURE"];
}
} catch (e) {
return [false, "ERROR_INVALID_SIGNATURE"];
}
const block = {
block: {
block_number: Blocks.count(),
previous_block: previousBlockHash,
timestamp: json.timestamp,
transactions: [
{data: json.data, signature: json.signature}
],
},
witness: config.node_identity
};
const toSign = JSON.stringify(block.block);
// Add signature to block (from this node)
const privateKeyParsed = dhive.PrivateKey.fromString(config.node_key);
block.signature = privateKeyParsed.sign(dhive.cryptoUtils.sha256(toSign)).toString();
block.number = Blocks.count();
// Add block to chain
Blocks.add(block);
return [true, "OK"];
} else {
return [false, "ERROR_MISSING_FIELDS"];
}
}
function signatureMatches(signature, publicKeyWif, data) {
try {
const actualKey = dhive.Signature.fromString(signature).recover(dhive.cryptoUtils.sha256(data));
//console.log(actualKey.toString(), publicKeyWif, data);
return actualKey.toString() === publicKeyWif;
} catch (e) {
return false;
}
}
module.exports = {generateMasterKey, fromWif, Blocks, addBlockToChain, signatureMatches};