-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathtransactions_test.go
48 lines (36 loc) · 1.73 KB
/
transactions_test.go
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
package main
import (
"testing"
)
// Testing the flow of encrypting a transaction using a public key and being able to decrypt the ciphertext to get back the encrypted (transaction) data
func TestTransactionEncryptionAndDecryption(t *testing.T){
wallet1 := initializeNewWallet()
// encrypt transaction using transaction data and wallet1's public key
encryptedTransaction := encryptTransaction(wallet1.PublicKey)
// decrypt encrypted transaction using wallet1's private key
decryptedMsg := decryptTransaction(wallet1.PrivateKey, encryptedTransaction)
if decryptedMsg != "transaction data" {
t.Errorf("Expected \"transaction data\" but got: %v", decryptedMsg)
}
}
// Testing the flow of signing some unique data using a private key and the verification of the signature
func TestSignAndVerifySignature(t *testing.T){
wallet1 := initializeNewWallet()
wallet2 := initializeNewWallet()
transaction := Transaction{
Sender: wallet1.PublicKey,
Receiver: wallet2.PublicKey,
Amount: 10,
}
sig, hashSum := signTransaction(wallet1.PrivateKey, transaction)
transaction.Signature = sig
if !verifySignature(sig, transaction, hashSum) {
t.Errorf("Verification that signature is indeed signed of the input data(msgHashSum) by the owner of the sender in the transaction failed when it shouldn't have")
}
// Alter hashSum then try verifying again
transaction.Amount = 10000
hashSum = generateUniqueTransactionHashSum(transaction)
if verifySignature(sig, transaction, hashSum) {
t.Errorf("Verification that signature is indeed signed of the input data(msgHashSum) by the owner of the sender in the transaction passed when it shouldn't have")
}
}