/
PaymentAccountPayload.java
142 lines (110 loc) · 5.32 KB
/
PaymentAccountPayload.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
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
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
/*
* This file is part of Bisq.
*
* Bisq is free software: you can redistribute it and/or modify it
* under the terms of the GNU Affero General Public License as published by
* the Free Software Foundation, either version 3 of the License, or (at
* your option) any later version.
*
* Bisq is distributed in the hope that it will be useful, but WITHOUT
* ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
* FITNESS FOR A PARTICULAR PURPOSE. See the GNU Affero General Public
* License for more details.
*
* You should have received a copy of the GNU Affero General Public License
* along with Bisq. If not, see <http://www.gnu.org/licenses/>.
*/
package bisq.core.payment.payload;
import bisq.common.consensus.UsedForTradeContractJson;
import bisq.common.crypto.CryptoUtils;
import bisq.common.proto.network.NetworkPayload;
import bisq.common.util.JsonExclude;
import bisq.common.util.Utilities;
import org.apache.commons.lang3.ArrayUtils;
import java.nio.charset.StandardCharsets;
import java.util.HashMap;
import java.util.Map;
import lombok.EqualsAndHashCode;
import lombok.Getter;
import lombok.ToString;
import lombok.extern.slf4j.Slf4j;
import static com.google.common.base.Preconditions.checkArgument;
// That class is used in the contract for creating the contract json. Any change will break the contract.
// If a field gets added it need to be be annotated with @JsonExclude (excluded from contract).
// We should add an extraDataMap as in StoragePayload objects
@Getter
@EqualsAndHashCode
@ToString
@Slf4j
public abstract class PaymentAccountPayload implements NetworkPayload, UsedForTradeContractJson {
// Keys for excludeFromJsonDataMap
public static final String SALT = "salt";
public static final String HOLDER_NAME = "holderName";
protected final String paymentMethodId;
protected final String id;
// Is just kept for not breaking backward compatibility. Set to -1 to indicate it is no used anymore.
protected final long maxTradePeriod;
// In v0.6 we removed maxTradePeriod but we need to keep it in the PB file for backward compatibility
// protected final long maxTradePeriod;
// Used for new data (e.g. salt introduced in v0.6) which would break backward compatibility as
// PaymentAccountPayload is used for the json contract and a trade with a user who has an older version would
// fail the contract verification.
@JsonExclude
protected final Map<String, String> excludeFromJsonDataMap;
///////////////////////////////////////////////////////////////////////////////////////////
// Constructor
///////////////////////////////////////////////////////////////////////////////////////////
PaymentAccountPayload(String paymentMethodId, String id) {
this(paymentMethodId,
id,
-1,
new HashMap<>());
}
///////////////////////////////////////////////////////////////////////////////////////////
// PROTO BUFFER
///////////////////////////////////////////////////////////////////////////////////////////
protected PaymentAccountPayload(String paymentMethodId,
String id,
long maxTradePeriod,
Map<String, String> excludeFromJsonDataMapParam) {
this.paymentMethodId = paymentMethodId;
this.id = id;
this.maxTradePeriod = maxTradePeriod;
this.excludeFromJsonDataMap = excludeFromJsonDataMapParam;
// If not set (old versions) we set by default a random 256 bit salt.
// User can set salt as well by hex string.
// Persisted value will overwrite that
if (!this.excludeFromJsonDataMap.containsKey(SALT))
this.excludeFromJsonDataMap.put(SALT, Utilities.encodeToHex(CryptoUtils.getRandomBytes(32)));
}
protected protobuf.PaymentAccountPayload.Builder getPaymentAccountPayloadBuilder() {
final protobuf.PaymentAccountPayload.Builder builder = protobuf.PaymentAccountPayload.newBuilder()
.setPaymentMethodId(paymentMethodId)
.setMaxTradePeriod(maxTradePeriod)
.setId(id);
builder.putAllExcludeFromJsonData(excludeFromJsonDataMap);
return builder;
}
///////////////////////////////////////////////////////////////////////////////////////////
// API
///////////////////////////////////////////////////////////////////////////////////////////
public abstract String getPaymentDetails();
public abstract String getPaymentDetailsForTradePopup();
public byte[] getSalt() {
checkArgument(excludeFromJsonDataMap.containsKey(SALT), "Salt must have been set in excludeFromJsonDataMap.");
return Utilities.decodeFromHex(excludeFromJsonDataMap.get(SALT));
}
public void setSalt(byte[] salt) {
excludeFromJsonDataMap.put(SALT, Utilities.encodeToHex(salt));
}
// Identifying data of payment account (e.g. IBAN).
// This is critical code for verifying age of payment account.
// Any change would break validation of historical data!
public abstract byte[] getAgeWitnessInputData();
protected byte[] getAgeWitnessInputData(byte[] data) {
return ArrayUtils.addAll(paymentMethodId.getBytes(StandardCharsets.UTF_8), data);
}
public String getOwnerId() {
return null;
}
}