-
Notifications
You must be signed in to change notification settings - Fork 1
/
driver.js
96 lines (76 loc) · 2.05 KB
/
driver.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
"use strict";
const Bank = require('./bank.js').Bank;
const Client = require('./client.js').Client;
const { Coin, COIN_RIS_LENGTH, IDENT_STR, BANK_STR } = require('./coin.js');
let bank = new Bank();
// The customer
let alice = new Client('Alice');
bank.registerClient(alice);
bank.deposit({
account: alice.name,
amount: 200,
});
// The merchant
let bob = new Client('Bob');
bank.registerClient(bob);
bank.deposit({
account: bob.name,
amount: 50,
});
// Show the starting balances.
console.log("Starting balances:");
bank.showBalances();
console.log();
// For simplicity, we'll assume that there is only one coin.
alice.buyCoin(bank, 20);
// Alice saves the coin for double-spending later
let coin = alice.coin;
// Normal transaction.
alice.giveCoin(bob);
bob.redeemCoin(bank);
console.log("Balances after 1st coin:");
bank.showBalances();
console.log();
// Alice attempts to double-spend the coin with bob.
alice.coin = coin;
alice.giveCoin(bob);
bob.redeemCoin(bank);
console.log("Balances after Alice's attempt to double-spend:");
bank.showBalances();
console.log();
// A second normal transaction.
alice.buyCoin(bank, 50);
alice.giveCoin(bob);
// Bob tracks the coin and RIS to redeem again later.
let secondCoin = bob.coin;
let ris = bob.ris;
bob.redeemCoin(bank);
console.log("Balances after 2nd coin spent:");
bank.showBalances();
console.log();
// Bob attempts to double-redeem the 2nd coin.
bob.coin = secondCoin;
bob.ris = ris;
bob.redeemCoin(bank);
console.log("Balances after 2nd coin spent:");
bank.showBalances();
console.log();
// Alice attempts to use a coin that has not been signed by the bank.
let forgedCoin = new Coin("PuddinTame", 100, bank.n, bank.e);
this.coin = forgedCoin;
try {
alice.giveCoin(bob);
} catch (e) {
console.log("Bob rejects the forged coin.");
console.log();
}
// Alice attempts to redeem the forged coin with the bank
try {
alice.redeemCoin(bank);
} catch (e) {
console.log("The bank rejects the forged coin.");
console.log();
}
console.log("Balances after forgery attempts:");
bank.showBalances();
console.log();