forked from udacity/nd1309_practice_block
-
Notifications
You must be signed in to change notification settings - Fork 0
/
block.js
45 lines (41 loc) · 1.13 KB
/
block.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
/**
* Import crypto-js/SHA256 library
*/
const SHA256 = require('crypto-js/sha256');
/**
* Class with a constructor for block
*/
class Block {
constructor(data){
this.id = 0;
this.nonce = 144444;
this.body = data;
this.hash = "";
}
/**
* Step 1. Implement `generateHash()`
* method that return the `self` block with the hash.
*
* Create a Promise that resolve with `self` after you create
* the hash of the object and assigned to the hash property `self.hash = ...`
*/
//
generateHash() {
// Use this to create a temporary reference of the class object
let self = this;
//Implement your code here
return new Promise(function(resolve, reject) {
// do a thing, possibly async, then…
let result = SHA256(JSON.stringify(self));
if (result != null) {
self.hash = result;
resolve(self);
}
else {
reject(Error("It broke"));
}
});
}
}
// Exporting the class Block to be reuse in other files
module.exports.Block = Block;