forked from bisq-network/bisq
-
Notifications
You must be signed in to change notification settings - Fork 0
/
PaymentAccount.java
172 lines (140 loc) · 5.91 KB
/
PaymentAccount.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
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
/*
* 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;
import bisq.core.locale.TradeCurrency;
import bisq.core.payment.payload.PaymentAccountPayload;
import bisq.core.payment.payload.PaymentMethod;
import bisq.core.proto.CoreProtoResolver;
import bisq.common.proto.ProtoUtil;
import bisq.common.proto.persistable.PersistablePayload;
import bisq.common.util.Utilities;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;
import java.util.Optional;
import java.util.UUID;
import java.util.stream.Collectors;
import lombok.EqualsAndHashCode;
import lombok.Getter;
import lombok.Setter;
import lombok.ToString;
import lombok.extern.slf4j.Slf4j;
import javax.annotation.Nullable;
import static com.google.common.base.Preconditions.checkNotNull;
@EqualsAndHashCode
@ToString
@Getter
@Slf4j
public abstract class PaymentAccount implements PersistablePayload {
protected final PaymentMethod paymentMethod;
@Setter
protected String id;
@Setter
protected long creationDate;
@Setter
public PaymentAccountPayload paymentAccountPayload;
@Setter
protected String accountName;
protected final List<TradeCurrency> tradeCurrencies = new ArrayList<>();
@Setter
@Nullable
protected TradeCurrency selectedTradeCurrency;
///////////////////////////////////////////////////////////////////////////////////////////
// Constructor
///////////////////////////////////////////////////////////////////////////////////////////
protected PaymentAccount(PaymentMethod paymentMethod) {
this.paymentMethod = paymentMethod;
}
public void init() {
id = UUID.randomUUID().toString();
creationDate = new Date().getTime();
paymentAccountPayload = createPayload();
}
///////////////////////////////////////////////////////////////////////////////////////////
// PROTO BUFFER
///////////////////////////////////////////////////////////////////////////////////////////
@Override
public protobuf.PaymentAccount toProtoMessage() {
checkNotNull(accountName, "accountName must not be null");
protobuf.PaymentAccount.Builder builder = protobuf.PaymentAccount.newBuilder()
.setPaymentMethod(paymentMethod.toProtoMessage())
.setId(id)
.setCreationDate(creationDate)
.setPaymentAccountPayload((protobuf.PaymentAccountPayload) paymentAccountPayload.toProtoMessage())
.setAccountName(accountName)
.addAllTradeCurrencies(ProtoUtil.collectionToProto(tradeCurrencies, protobuf.TradeCurrency.class));
Optional.ofNullable(selectedTradeCurrency).ifPresent(selectedTradeCurrency -> builder.setSelectedTradeCurrency((protobuf.TradeCurrency) selectedTradeCurrency.toProtoMessage()));
return builder.build();
}
public static PaymentAccount fromProto(protobuf.PaymentAccount proto, CoreProtoResolver coreProtoResolver) {
PaymentAccount account = PaymentAccountFactory.getPaymentAccount(PaymentMethod.getPaymentMethodById(proto.getPaymentMethod().getId()));
account.getTradeCurrencies().clear();
account.setId(proto.getId());
account.setCreationDate(proto.getCreationDate());
account.setAccountName(proto.getAccountName());
account.getTradeCurrencies().addAll(proto.getTradeCurrenciesList().stream().map(TradeCurrency::fromProto).collect(Collectors.toList()));
account.setPaymentAccountPayload(coreProtoResolver.fromProto(proto.getPaymentAccountPayload()));
if (proto.hasSelectedTradeCurrency())
account.setSelectedTradeCurrency(TradeCurrency.fromProto(proto.getSelectedTradeCurrency()));
return account;
}
///////////////////////////////////////////////////////////////////////////////////////////
// API
///////////////////////////////////////////////////////////////////////////////////////////
public Date getCreationDate() {
return new Date(creationDate);
}
public void addCurrency(TradeCurrency tradeCurrency) {
if (!tradeCurrencies.contains(tradeCurrency))
tradeCurrencies.add(tradeCurrency);
}
public void removeCurrency(TradeCurrency tradeCurrency) {
if (tradeCurrencies.contains(tradeCurrency))
tradeCurrencies.remove(tradeCurrency);
}
public boolean hasMultipleCurrencies() {
return tradeCurrencies.size() > 1;
}
public void setSingleTradeCurrency(TradeCurrency tradeCurrency) {
tradeCurrencies.clear();
tradeCurrencies.add(tradeCurrency);
setSelectedTradeCurrency(tradeCurrency);
}
@Nullable
public TradeCurrency getSingleTradeCurrency() {
if (tradeCurrencies.size() == 1)
return tradeCurrencies.get(0);
else
return null;
}
public long getMaxTradePeriod() {
return paymentMethod.getMaxTradePeriod();
}
protected abstract PaymentAccountPayload createPayload();
public void setSalt(byte[] salt) {
paymentAccountPayload.setSalt(salt);
}
public byte[] getSalt() {
return paymentAccountPayload.getSalt();
}
public void setSaltAsHex(String saltAsHex) {
setSalt(Utilities.decodeFromHex(saltAsHex));
}
public String getSaltAsHex() {
return Utilities.bytesAsHexString(getSalt());
}
}