forked from iHabboush/DIY-Blockchain
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathblockchain.js
121 lines (104 loc) · 3.26 KB
/
blockchain.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
110
111
112
113
114
115
116
117
118
119
120
121
'use strict';
const { createHash } = require('crypto');
const signing = require('./signing');
/**
* A simple signed Transaction class for sending funds from the signer to
* another public key.
*/
class Transaction {
/**
* The constructor accepts a hex private key for the sender, a hex
* public key for the recipient, and a number amount. It will use these
* to set a number of properties, including a Secp256k1 signature.
*
* Properties:
* - source: the public key derived from the provided private key
* - recipient: the provided public key for the recipient
* - amount: the provided amount
* - signature: a unique signature generated from a combination of the
* other properties, signed with the provided private key
*/
constructor(privateKey, recipient, amount) {
// Enter your solution here
}
}
/**
* A Block class for storing an array of transactions and the hash of a
* previous block. Includes a method to calculate and set its own hash.
*/
class Block {
/**
* Accepts an array of transactions and the hash of a previous block. It
* saves these and uses them to calculate a hash.
*
* Properties:
* - transactions: the passed in transactions
* - previousHash: the passed in hash
* - nonce: just set this to some hard-coded number for now, it will be
* used later when we make blocks mineable with our own PoW algorithm
* - hash: a unique hash string generated from the other properties
*/
constructor(transactions, previousHash) {
// Your code here
}
/**
* Accepts a nonce, and generates a unique hash for the block. Updates the
* hash and nonce properties of the block accordingly.
*
* Hint:
* The format of the hash is up to you. Remember that it needs to be
* unique and deterministic, and must become invalid if any of the block's
* properties change.
*/
calculateHash(nonce) {
// Your code here
}
}
/**
* A Blockchain class for storing an array of blocks, each of which is linked
* to the previous block by their hashes. Includes methods for adding blocks,
* fetching the head block, and checking the balances of public keys.
*/
class Blockchain {
/**
* Generates a new blockchain with a single "genesis" block. This is the
* only block which may have no previous hash. It should have an empty
* transactions array, and `null` for the previous hash.
*
* Properties:
* - blocks: an array of blocks, starting with one genesis block
*/
constructor() {
// Your code here
}
/**
* Simply returns the last block added to the chain.
*/
getHeadBlock() {
// Your code here
}
/**
* Accepts an array of transactions, creating a new block with them and
* adding it to the chain.
*/
addBlock(transactions) {
// Your code here
}
/**
* Accepts a public key, calculating its "balance" based on the amounts
* transferred in all transactions stored in the chain.
*
* Note:
* There is currently no way to create new funds on the chain, so some
* keys will have a negative balance. That's okay, we'll address it when
* we make the blockchain mineable later.
*/
getBalance(publicKey) {
// Your code here
}
}
module.exports = {
Transaction,
Block,
Blockchain
};