-
Notifications
You must be signed in to change notification settings - Fork 0
/
TransactionOutput.java
31 lines (27 loc) · 1.43 KB
/
TransactionOutput.java
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
import java.security.NoSuchAlgorithmException;
import java.security.PublicKey;
//We can think of transaction outputs as a check that can be turned into cash and spent later.
class TransactionOutput {
public String id; // if for referring
public PublicKey recipient; // whom this TXO belongs to
public int value; // the value of the TXO
public String parentTransactionId; // The tx in which this TXO is created
public TransactionOutput(PublicKey recipient, int value, String parentTransactionId) throws NoSuchAlgorithmException {
this.recipient = recipient;
this.value = value;
this.parentTransactionId = parentTransactionId;
this.id = calculateHash();
}
//Calculates the hash by using recipient + value + parentTransactionId
public String calculateHash() throws NoSuchAlgorithmException {
// In fact, these three may not be unique!!! <- Problem to be solved...
String dataToHash = "" + this.recipient + this.value + this.parentTransactionId;
return StringUtil.hash(dataToHash);
}
// Finds out whether a TXO belongs to a particular public key
// So checks whether the public key of the object (recipient)
// is equal to the incoming parameter (publicKey) or not
public boolean belongsTo(PublicKey publicKey) {
return (publicKey.equals(recipient)); ////////////////////// Can be different objects, implement this properly!
}
}