Skip to content

Commit cd1dca6

Browse files
authored
Merge pull request #69 from helloscoopa/helloscoopa/make-tx-class-tests-to-self-contain
refactor: make tests of `Transaction` class to self contain with few minor improvements
2 parents 6900269 + d10c64a commit cd1dca6

File tree

6 files changed

+52
-56
lines changed

6 files changed

+52
-56
lines changed

README.md

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -63,7 +63,7 @@ const myChain = new Blockchain();
6363
```js
6464
// Transfer 100 coins from my wallet to "toAddress"
6565
const tx = new Transaction(myKey.getPublic('hex'), 'toAddress', 100);
66-
tx.signTransaction(myKey);
66+
tx.sign(myKey);
6767

6868
myChain.addTransaction(tx);
6969
```

src/blockchain.js

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -36,7 +36,7 @@ class Transaction {
3636
*
3737
* @param {string} signingKey
3838
*/
39-
signTransaction(signingKey) {
39+
sign(signingKey) {
4040
// You can only send a transaction from the wallet that is linked to your
4141
// key. So here we check if the fromAddress matches your publicKey
4242
if (signingKey.getPublic('hex') !== this.fromAddress) {

src/main.js

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -19,15 +19,15 @@ savjeeCoin.minePendingTransactions(myWalletAddress);
1919

2020
// Create a transaction & sign it with your key
2121
const tx1 = new Transaction(myWalletAddress, 'address2', 100);
22-
tx1.signTransaction(myKey);
22+
tx1.sign(myKey);
2323
savjeeCoin.addTransaction(tx1);
2424

2525
// Mine block
2626
savjeeCoin.minePendingTransactions(myWalletAddress);
2727

2828
// Create second transaction
2929
const tx2 = new Transaction(myWalletAddress, 'address1', 50);
30-
tx2.signTransaction(myKey);
30+
tx2.sign(myKey);
3131
savjeeCoin.addTransaction(tx2);
3232

3333
// Mine block

tests/blockchain.test.js

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -94,7 +94,7 @@ describe('Blockchain class', function() {
9494
// Create new transaction to self
9595
const tx = new Transaction(walletAddr, walletAddr, 80);
9696
tx.timestamp = 1;
97-
tx.signTransaction(signingKey);
97+
tx.sign(signingKey);
9898

9999
blockchain.addTransaction(tx);
100100
blockchain.minePendingTransactions('no_addr');

tests/helpers.js

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,7 @@ const signingKey = ec.keyFromPrivate('3d6f54430830d388052865b95c10b4aeb1bbe33c01
66
function createSignedTx(amount = 10) {
77
const txObject = new Transaction(signingKey.getPublic('hex'), 'wallet2', amount);
88
txObject.timestamp = 1;
9-
txObject.signTransaction(signingKey);
9+
txObject.sign(signingKey);
1010

1111
return txObject;
1212
}
@@ -23,7 +23,7 @@ function createBlockchainWithTx() {
2323
blockchain.minePendingTransactions(signingKey.getPublic('hex'));
2424

2525
const validTx = new Transaction(signingKey.getPublic('hex'), 'b2', 10);
26-
validTx.signTransaction(signingKey);
26+
validTx.sign(signingKey);
2727

2828
blockchain.addTransaction(validTx);
2929
blockchain.addTransaction(validTx);

tests/transaction.test.js

Lines changed: 45 additions & 49 deletions
Original file line numberDiff line numberDiff line change
@@ -1,15 +1,19 @@
11
const assert = require('assert');
2+
23
const { Transaction } = require('../src/blockchain');
34
const { createSignedTx, signingKey } = require('./helpers');
45

5-
let txObject = null;
6+
describe('Transaction class', function() {
7+
let txObject = null;
8+
const fromAddress = 'fromAddress';
9+
const toAddress = 'toAddress';
10+
const amount = 100;
611

7-
beforeEach(function() {
8-
txObject = new Transaction('fromAddress', 'toAddress', 9999);
9-
});
12+
beforeEach(function() {
13+
txObject = new Transaction(fromAddress, toAddress, amount);
14+
});
1015

11-
describe('Transaction class', function() {
12-
describe('Constructor', function() {
16+
describe('constructor', function() {
1317
it('should automatically set the current date', function() {
1418
const actual = txObject.timestamp;
1519
const minTime = Date.now() - 1000;
@@ -18,87 +22,79 @@ describe('Transaction class', function() {
1822
assert(actual > minTime && actual < maxTime, 'Tx does not have a good timestamp');
1923
});
2024

21-
2225
it('should correctly save from, to and amount', function() {
23-
txObject = new Transaction('a1', 'b1', 10);
24-
25-
assert.strict.equal(txObject.fromAddress, 'a1');
26-
assert.strict.equal(txObject.toAddress, 'b1');
27-
assert.strict.equal(txObject.amount, 10);
26+
assert.strict.equal(txObject.fromAddress, fromAddress);
27+
assert.strict.equal(txObject.toAddress, toAddress);
28+
assert.strict.equal(txObject.amount, amount);
2829
});
2930
});
3031

31-
describe('Calculate hash', function() {
32-
it('should correct calculate the SHA256', function() {
33-
txObject = new Transaction('a1', 'b1', 10);
32+
describe('calculateHash', function() {
33+
it('should correctly calculate the SHA256 hash', function() {
3434
txObject.timestamp = 1;
3535

3636
assert.strict.equal(
3737
txObject.calculateHash(),
38-
39-
// Output of SHA256(a1b1101)
40-
'21894bb7b0e56aab9eb48d4402d94628a9a179bc277542a5703f417900275153'
38+
'4be9c20f87f7baac191aa246a33b5d44af1f96f23598ac06e5f71ee222f40abf'
4139
);
4240
});
4341

44-
it('should change when we tamper with the tx', function() {
45-
txObject = new Transaction('a1', 'b1', 10);
46-
47-
const originalHash = txObject.calculateHash();
48-
txObject.amount = 100;
42+
it('should output a different hash if data is tampered in the transaction', function() {
43+
// Tamper the amount making the hash different
44+
txObject.amount = 50;
4945

5046
assert.strict.notEqual(
5147
txObject.calculateHash(),
52-
originalHash
48+
txObject.hash
5349
);
5450
});
5551
});
5652

57-
describe('isValid', function() {
58-
it('should throw error without signature', function() {
59-
assert.throws(() => { txObject.isValid(); }, Error);
60-
});
61-
53+
describe('sign', function() {
6254
it('should correctly sign transactions', function() {
6355
txObject = createSignedTx();
6456

6557
assert.strict.equal(
6658
txObject.signature,
67-
'3044022023fb1d818a0888f7563e1a3ccdd68b28e23070d6c0c1c5' +
68-
'004721ee1013f1d769022037da026cda35f95ef1ee5ced5b9f7d70' +
69-
'e102fcf841e6240950c61e8f9b6ef9f8'
59+
'3044022023fb1d818a0888f7563e1a3ccdd68b28e23070d6c0c1c5004721ee1013f1d7' +
60+
'69022037da026cda35f95ef1ee5ced5b9f7d70e102fcf841e6240950c61e8f9b6ef9f8'
7061
);
7162
});
7263

73-
it('should not sign transactions for other wallets', function() {
74-
txObject = new Transaction('not a correct wallet key', 'wallet2', 10);
75-
txObject.timestamp = 1;
64+
it('should not sign transactions with fromAddresses that does not belogs to the private key', function() {
65+
txObject.fromAddress = 'some-other-address';
7666

7767
assert.throws(() => {
78-
txObject.signTransaction(signingKey);
68+
txObject.sign(signingKey);
7969
}, Error);
8070
});
71+
});
8172

82-
it('should detect badly signed transactions', function() {
83-
txObject = createSignedTx();
84-
85-
// Tamper with it & it should be invalid!
86-
txObject.amount = 100;
87-
assert(!txObject.isValid());
88-
});
89-
90-
it('should return true with correctly signed tx', function() {
91-
txObject = createSignedTx();
73+
describe('isValid', function() {
74+
it('should return true for mining reward transactions', function() {
75+
txObject.fromAddress = null;
9276
assert(txObject.isValid());
9377
});
9478

95-
it('should fail when signature is empty string', function() {
79+
it('should throw error if signature is invalid', function() {
80+
delete txObject.signature;
81+
assert.throws(() => { txObject.isValid(); }, Error);
82+
9683
txObject.signature = '';
9784
assert.throws(() => { txObject.isValid(); }, Error);
9885
});
9986

100-
it('should return true for mining rewards', function() {
101-
txObject.fromAddress = null;
87+
it('should return false for badly signed transactions', function() {
88+
txObject = createSignedTx(10);
89+
90+
// Tamper the amount making the signature invalid
91+
txObject.amount = 50;
92+
93+
assert(!txObject.isValid());
94+
});
95+
96+
it('should return true for correctly signed tx', function() {
97+
txObject = createSignedTx(10);
10298
assert(txObject.isValid());
10399
});
104100
});

0 commit comments

Comments
 (0)