1
1
const assert = require ( 'assert' ) ;
2
+
2
3
const { Transaction } = require ( '../src/blockchain' ) ;
3
4
const { createSignedTx, signingKey } = require ( './helpers' ) ;
4
5
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 ;
6
11
7
- beforeEach ( function ( ) {
8
- txObject = new Transaction ( ' fromAddress' , ' toAddress' , 9999 ) ;
9
- } ) ;
12
+ beforeEach ( function ( ) {
13
+ txObject = new Transaction ( fromAddress , toAddress , amount ) ;
14
+ } ) ;
10
15
11
- describe ( 'Transaction class' , function ( ) {
12
- describe ( 'Constructor' , function ( ) {
16
+ describe ( 'constructor' , function ( ) {
13
17
it ( 'should automatically set the current date' , function ( ) {
14
18
const actual = txObject . timestamp ;
15
19
const minTime = Date . now ( ) - 1000 ;
@@ -18,87 +22,79 @@ describe('Transaction class', function() {
18
22
assert ( actual > minTime && actual < maxTime , 'Tx does not have a good timestamp' ) ;
19
23
} ) ;
20
24
21
-
22
25
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 ) ;
28
29
} ) ;
29
30
} ) ;
30
31
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 ( ) {
34
34
txObject . timestamp = 1 ;
35
35
36
36
assert . strict . equal (
37
37
txObject . calculateHash ( ) ,
38
-
39
- // Output of SHA256(a1b1101)
40
- '21894bb7b0e56aab9eb48d4402d94628a9a179bc277542a5703f417900275153'
38
+ '4be9c20f87f7baac191aa246a33b5d44af1f96f23598ac06e5f71ee222f40abf'
41
39
) ;
42
40
} ) ;
43
41
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 ;
49
45
50
46
assert . strict . notEqual (
51
47
txObject . calculateHash ( ) ,
52
- originalHash
48
+ txObject . hash
53
49
) ;
54
50
} ) ;
55
51
} ) ;
56
52
57
- describe ( 'isValid' , function ( ) {
58
- it ( 'should throw error without signature' , function ( ) {
59
- assert . throws ( ( ) => { txObject . isValid ( ) ; } , Error ) ;
60
- } ) ;
61
-
53
+ describe ( 'sign' , function ( ) {
62
54
it ( 'should correctly sign transactions' , function ( ) {
63
55
txObject = createSignedTx ( ) ;
64
56
65
57
assert . strict . equal (
66
58
txObject . signature ,
67
- '3044022023fb1d818a0888f7563e1a3ccdd68b28e23070d6c0c1c5' +
68
- '004721ee1013f1d769022037da026cda35f95ef1ee5ced5b9f7d70' +
69
- 'e102fcf841e6240950c61e8f9b6ef9f8'
59
+ '3044022023fb1d818a0888f7563e1a3ccdd68b28e23070d6c0c1c5004721ee1013f1d7' +
60
+ '69022037da026cda35f95ef1ee5ced5b9f7d70e102fcf841e6240950c61e8f9b6ef9f8'
70
61
) ;
71
62
} ) ;
72
63
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' ;
76
66
77
67
assert . throws ( ( ) => {
78
- txObject . signTransaction ( signingKey ) ;
68
+ txObject . sign ( signingKey ) ;
79
69
} , Error ) ;
80
70
} ) ;
71
+ } ) ;
81
72
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 ;
92
76
assert ( txObject . isValid ( ) ) ;
93
77
} ) ;
94
78
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
+
96
83
txObject . signature = '' ;
97
84
assert . throws ( ( ) => { txObject . isValid ( ) ; } , Error ) ;
98
85
} ) ;
99
86
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 ) ;
102
98
assert ( txObject . isValid ( ) ) ;
103
99
} ) ;
104
100
} ) ;
0 commit comments