forked from taustin/bcl
-
Notifications
You must be signed in to change notification settings - Fork 1
/
test.js
186 lines (174 loc) · 5.7 KB
/
test.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
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
"use strict";
const assert = require('chai').assert;
const BigInteger = require('jsbn').BigInteger;
const Block = require('./block.js');
const Client = require('./client.js');
const Miner = require('./miner.js');
const Transaction = require('./transaction.js');
const Wallet = require('./wallet.js');
const utils = require('./utils.js');
// Using these keypairs for all tests, since key generation is slow.
const kp = utils.generateKeypair();
const newKeypair = utils.generateKeypair();
describe('utils', function() {
describe('.verifySignature', function() {
let sig = utils.sign(kp.private, "hello");
it('should accept a valid signature', function() {
assert.ok(utils.verifySignature(kp.public, "hello", sig));
});
it('should reject an invalid signature', function() {
assert.ok(!utils.verifySignature(kp.public, "goodbye", sig));
});
});
});
describe("Transaction", () => {
describe("#spendOutput", () => {
let address = utils.calcAddress(kp.public);
let tx = new Transaction({
inputs: [],
outputs: [{amount: 42, address: address}],
});
it("should return the amount of tokens in the output if the input matches the output.", () => {
let nextInput = {
txID: tx.id,
outputIndex: 0,
pubKey: kp.public,
sig: utils.sign(kp.private, tx.outputs[0])
};
assert.equal(tx.spendOutput(nextInput), 42);
});
it("should throw an exception if the transaction ID is invalid.", () => {
let nextInput = {
txID: 12345,
outputIndex: 0,
pubKey: kp.public,
sig: utils.sign(kp.private, tx.outputs[0]),
};
assert.throws(() => {
tx.spendOutput(nextInput);
});
});
it("should throw and exception if the signature is invalid.", () => {
let nextInput = {
txID: tx.id,
outputIndex: 0,
pubKey: kp.public,
sig: utils.sign(newKeypair.private, tx.outputs[0]),
};
assert.throws(() => {
tx.spendOutput(nextInput);
});
});
});
describe("#isValid", () => {
let address = utils.calcAddress(kp.public);
let cbTX = new Transaction({
coinBaseReward: 1,
outputs: [{amount: 1, address: address},
{amount: 42, address: address}],
});
let utxos = {};
utxos[cbTX.id] = cbTX.outputs;
let input = {
txID: cbTX.id,
outputIndex: 1,
pubKey: kp.public,
sig: utils.sign(kp.private, cbTX.outputs[1]),
};
let newAddress = utils.calcAddress(newKeypair.public);
it("should consider a transaction valid if the outputs do not exceed the inputs", () => {
let tx = new Transaction({
inputs: [input],
outputs: [{amount: 20, address: newAddress},
{amount: 10, address: address}],
});
assert.isTrue(tx.isValid(utxos));
});
it("should consider a transaction invalid if the outputs exceed the inputs", () => {
let tx = new Transaction({
inputs: [input],
outputs: [{amount: 20, address: newAddress},
{amount: 30, address: address}],
});
assert.isFalse(tx.isValid(utxos));
});
it("should reject a transaction if the signatures on the inputs do not match the UTXOs", () => {
let badInput = {
txID: cbTX.id,
outputIndex: 1,
pubKey: newKeypair.public,
sig: utils.sign(newKeypair.private, cbTX.outputs[1]),
};
let tx = new Transaction({
inputs: [badInput],
outputs: [{amount: 40, address: newAddress}],
});
assert.isFalse(tx.isValid(utxos));
});
});
});
describe("Wallet", () => {
describe("#balance", () => {
let w = new Wallet();
let addr = w.makeAddress();
let utxo1 = { amount: 42, address: addr };
let utxo2 = { amount: 25, address: addr };
let tx = new Transaction({
inputs: [],
outputs: [utxo1, utxo2],
});
w.addUTXO(utxo1, tx.id, 0);
w.addUTXO(utxo2, tx.id, 1);
it("should return the total value of coins stored in the wallet.", () => {
assert.equal(w.balance, 67);
});
});
describe("#spendUTXOs", () => {
let w = new Wallet();
let addr = w.makeAddress();
let utxo1 = { amount: 42, address: addr };
let utxo2 = { amount: 25, address: addr };
let tx = new Transaction({
inputs: [],
outputs: [utxo1, utxo2],
});
w.addUTXO(utxo1, tx.id, 0);
w.addUTXO(utxo2, tx.id, 1);
it("should spend sufficient UTXOs to reach the balance.", () => {
assert.equal(w.coins.length, 2);
// Either UTXO should be sufficient.
let { inputs } = w.spendUTXOs(20);
let { txID, outputIndex, pubKey, sig } = inputs[0];
assert.equal(w.coins.length, 1);
assert.equal(txID, tx.id);
// Make sure the signature is valid
assert.isTrue(utils.verifySignature(pubKey, tx.outputs[outputIndex], sig));
});
});
});
describe('Block', function() {
describe('#addTransaction', function() {
// Slow test.
/*
let aliceWallet = new Wallet();
let bobWallet = new Wallet();
let charlieWallet = new Wallet();
let gb = Block.makeGenesisBlock([
{ client: {wallet: aliceWallet}, amount: 150 },
{ client: {wallet: bobWallet}, amount: 90 },
{ client: {wallet: charlieWallet}, amount: 20 },
]);
it("should update the block's utxo if the transaction was successful", function() {
let { inputs } = aliceWallet.spendUTXOs(25);
let tx = new Transaction({
inputs: inputs,
outputs: [ { address: bobWallet.makeAddress(), amount: 20 } ],
});
gb.addTransaction(tx);
bobWallet.addUTXO(tx.outputs[0], tx.id, 0);
assert.equal(aliceWallet.balance, 0);
assert.equal(bobWallet.balance, 110);
});
//*/
});
});