11const assert = require ( 'assert' ) ;
2- const EC = require ( 'elliptic' ) . ec ;
32
43const { Transaction } = require ( '../src/blockchain' ) ;
4+ const { createSignedTx, signingKey } = require ( './helpers' ) ;
55
66describe ( 'Transaction class' , function ( ) {
77 let txObject = null ;
8- const signingKey = ( new EC ( 'secp256k1' ) ) . keyFromPrivate ( 'some-private-key' ) ;
9- const fromAddress = signingKey . getPublic ( 'hex' ) ;
10- const toAddress = signingKey . getPublic ( 'hex' ) ;
8+ const fromAddress = 'fromAddress' ;
9+ const toAddress = 'toAddress' ;
1110 const amount = 100 ;
1211
1312 beforeEach ( function ( ) {
1413 txObject = new Transaction ( fromAddress , toAddress , amount ) ;
15- txObject . timestamp = 1 ;
16- txObject . sign ( signingKey ) ;
1714 } ) ;
1815
1916 describe ( 'constructor' , function ( ) {
2017 it ( 'should automatically set the current date' , function ( ) {
21- txObject = new Transaction ( fromAddress , toAddress , amount ) ;
22-
2318 const actual = txObject . timestamp ;
2419 const minTime = Date . now ( ) - 1000 ;
2520 const maxTime = Date . now ( ) + 1000 ;
@@ -36,9 +31,11 @@ describe('Transaction class', function() {
3631
3732 describe ( 'calculateHash' , function ( ) {
3833 it ( 'should correctly calculate the SHA256 hash' , function ( ) {
34+ txObject . timestamp = 1 ;
35+
3936 assert . strict . equal (
4037 txObject . calculateHash ( ) ,
41- '8e8e081788cba59d0e11fc881dab1f7fbe7eb3dd4f7db9d6382c892588cc912a '
38+ '4be9c20f87f7baac191aa246a33b5d44af1f96f23598ac06e5f71ee222f40abf '
4239 ) ;
4340 } ) ;
4441
@@ -55,10 +52,12 @@ describe('Transaction class', function() {
5552
5653 describe ( 'sign' , function ( ) {
5754 it ( 'should correctly sign transactions' , function ( ) {
55+ txObject = createSignedTx ( ) ;
56+
5857 assert . strict . equal (
5958 txObject . signature ,
60- '3046022100b2e5ee31c4ebea01125c73c8ef031b35e23f58f363a2ba732860f512ad99b1 ' +
61- '0b022100b89da6fff470eac3d085276dd7ce079e6d0b049181226eaf45ab0b43ecc6a0fd '
59+ '3044022023fb1d818a0888f7563e1a3ccdd68b28e23070d6c0c1c5004721ee1013f1d7 ' +
60+ '69022037da026cda35f95ef1ee5ced5b9f7d70e102fcf841e6240950c61e8f9b6ef9f8 '
6261 ) ;
6362 } ) ;
6463
@@ -86,13 +85,16 @@ describe('Transaction class', function() {
8685 } ) ;
8786
8887 it ( 'should return false for badly signed transactions' , function ( ) {
88+ txObject = createSignedTx ( 10 ) ;
89+
8990 // Tamper the amount making the signature invalid
9091 txObject . amount = 50 ;
9192
9293 assert ( ! txObject . isValid ( ) ) ;
9394 } ) ;
9495
9596 it ( 'should return true for correctly signed tx' , function ( ) {
97+ txObject = createSignedTx ( 10 ) ;
9698 assert ( txObject . isValid ( ) ) ;
9799 } ) ;
98100 } ) ;
0 commit comments