-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathTransaction.java
More file actions
98 lines (88 loc) · 3.1 KB
/
Copy pathTransaction.java
File metadata and controls
98 lines (88 loc) · 3.1 KB
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
package blockchain;
import java.security.*;
import java.util.ArrayList;
public class Transaction {
public String transactionId; // this is also the hash of the transaction.
public PublicKey sender; // senders address/public key.
public PublicKey recipient; // Recipients address/public key.
public float value;
// this is to prevent anybody else from spending funds in our wallet.
public byte[] signature;
public ArrayList<TransactionInput> inputs = new ArrayList<TransactionInput>();
public ArrayList<TransactionOutput> outputs = new ArrayList<TransactionOutput>();
// a rough count of how many transactions have been generated.
private static int sequence = 0;
// Constructor:
public Transaction(PublicKey from, PublicKey to,
float value, ArrayList<TransactionInput> inputs) {
this.sender = from;
this.recipient = to;
this.value = value;
this.inputs = inputs;
}
// This Calculates the transaction hash (which will be used as its Id)
public String calculateHash() {
//increase the sequence to avoid 2 transactions having the same hash
sequence++;
return StringUtil.applySha256(
StringUtil.getStringFromKey(sender) +
StringUtil.getStringFromKey(recipient) +
Float.toString(value) + sequence
);
}
//Signs all the data we don't wish to be tampered with.
public void generateSignature(PrivateKey privateKey) {
String data = StringUtil.getStringFromKey(sender) +
StringUtil.getStringFromKey(recipient) + Float.toString(value);
signature = StringUtil.applyECDSASig(privateKey,data);
}
//Verifies the data we signed hasn't been tampered with
public boolean verifySignature() {
String data = StringUtil.getStringFromKey(sender) +
StringUtil.getStringFromKey(recipient) + Float.toString(value);
return StringUtil.verifyECDSASig(sender, data, signature);
}
public boolean processTransaction() {
if(verifySignature() == false) {
System.out.println("#Transaction Signature failed to verify");
return false;
}
//Checks if transaction is valid:
if(getInputsValue() < MyChain.minimumTransaction) {
System.out.println("Transaction Inputs too small: " + getInputsValue());
System.out.println("Please make sure the transaction amount is greater than " +
MyChain.minimumTransaction);
return false;
}
// Check that all transaction inputs are set
for(TransactionInput i : inputs) {
// if transaction input is not set return false
if(i.UTXO == null) {
System.out.println("Transaction Input is not set!");
return false;
}
}
//Remove transaction inputs from UTXO set
for(TransactionInput i : inputs)
MyChain.UTXOs.remove(i.UTXO.id);
//Add outputs as new UTXOs
for(TransactionOutput o : outputs)
MyChain.UTXOs.put(o.id , o);
return true;
}
public float getInputsValue() {
float total = 0;
for(TransactionInput i : inputs) {
if(i.UTXO != null)
total += i.UTXO.value;
}
return total;
}
public float getOutputsValue() {
float total = 0;
for(TransactionOutput o : outputs) {
total += o.value;
}
return total;
}
}