-
Notifications
You must be signed in to change notification settings - Fork 13
/
index.js
66 lines (50 loc) · 2.16 KB
/
index.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
import BlockChain from './src/final/blockchain';
import Transaction from './src/final/transaction';
import { createWallet, validateWallet } from './src/final/wallet';
/*
* Init Blockchain
*/
const SIMPLE_BLOCKCHAIN = new BlockChain();
/*
* Setup wallets
*
* Create your wallet and Alice's wallet
*/
const myWallet = createWallet();
const aliceWallet = createWallet();
// Looking at the output, I did get the public key from the private key.
console.log("is the myWallet from privateKey equals to publicKey?", validateWallet(myWallet.privateKey, myWallet.publicKey));
/*
* Transactions
*
* Initiate a transaction and transfer 60 from your own account to Alice's account.
*/
const tx1 = new Transaction(myWallet.publicKey, aliceWallet.publicKey, 60);
// Sign with private key
tx1.signTransaction(myWallet.keyPair);
// Submit a transaction
SIMPLE_BLOCKCHAIN.addTransaction(tx1);
console.log('starting the miner of block 1...');
SIMPLE_BLOCKCHAIN.minePendingTransactions(myWallet.publicKey);
// Add more transactions and blocks so we can have a sample blockchain with 3 blocks of height
const tx2 = new Transaction(myWallet.publicKey, aliceWallet.publicKey, 80);
tx2.signTransaction(myWallet.keyPair);
SIMPLE_BLOCKCHAIN.addTransaction(tx2);
console.log('starting the miner of block 2...');
SIMPLE_BLOCKCHAIN.minePendingTransactions(myWallet.publicKey);
const tx3 = new Transaction(myWallet.publicKey, aliceWallet.publicKey, 100);
tx3.signTransaction(myWallet.keyPair);
SIMPLE_BLOCKCHAIN.addTransaction(tx3);
console.log('starting the miner of block 3...');
SIMPLE_BLOCKCHAIN.minePendingTransactions(myWallet.publicKey);
// If the transfer is successful, the balance of Alice's account will be 60.
console.log("Balance of Alice's account is: ", SIMPLE_BLOCKCHAIN.getBalanceOfAddress(aliceWallet.publicKey));
/*
* Blockchain validation test
*/
console.log("is the chain valid? " + SIMPLE_BLOCKCHAIN.isChainValid());
// We manually altered data in the blockchain
SIMPLE_BLOCKCHAIN.chain[1].transactions[0].amount = 200;
console.log("is the chain still valid? " + SIMPLE_BLOCKCHAIN.isChainValid());
// We print the whole blockchain
console.log(JSON.stringify(SIMPLE_BLOCKCHAIN, null, 4));