forked from bisq-network/bisq
-
Notifications
You must be signed in to change notification settings - Fork 0
/
AccountAgeWitnessService.java
955 lines (817 loc) · 42.6 KB
/
AccountAgeWitnessService.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
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
/*
* 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.account.witness;
import bisq.core.account.sign.SignedWitness;
import bisq.core.account.sign.SignedWitnessService;
import bisq.core.filter.FilterManager;
import bisq.core.locale.CurrencyUtil;
import bisq.core.locale.Res;
import bisq.core.offer.Offer;
import bisq.core.offer.OfferDirection;
import bisq.core.offer.OfferRestrictions;
import bisq.core.payment.AssetAccount;
import bisq.core.payment.ChargeBackRisk;
import bisq.core.payment.PaymentAccount;
import bisq.core.payment.payload.PaymentAccountPayload;
import bisq.core.payment.payload.PaymentMethod;
import bisq.core.support.dispute.Dispute;
import bisq.core.support.dispute.DisputeResult;
import bisq.core.support.dispute.arbitration.TraderDataItem;
import bisq.core.trade.model.bisq_v1.Contract;
import bisq.core.trade.model.bisq_v1.Trade;
import bisq.core.trade.protocol.bisq_v1.model.TradingPeer;
import bisq.core.user.User;
import bisq.network.p2p.BootstrapListener;
import bisq.network.p2p.P2PService;
import bisq.network.p2p.storage.P2PDataStorage;
import bisq.network.p2p.storage.persistence.AppendOnlyDataStoreService;
import bisq.common.UserThread;
import bisq.common.crypto.CryptoException;
import bisq.common.crypto.Hash;
import bisq.common.crypto.KeyRing;
import bisq.common.crypto.PubKeyRing;
import bisq.common.crypto.Sig;
import bisq.common.handlers.ErrorMessageHandler;
import bisq.common.util.MathUtils;
import bisq.common.util.Tuple2;
import bisq.common.util.Utilities;
import org.bitcoinj.core.Coin;
import org.bitcoinj.core.ECKey;
import org.bitcoinj.core.Utils;
import javax.inject.Inject;
import com.google.common.annotations.VisibleForTesting;
import java.security.PublicKey;
import java.time.Clock;
import java.util.Arrays;
import java.util.Date;
import java.util.GregorianCalendar;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.Objects;
import java.util.Optional;
import java.util.Random;
import java.util.Set;
import java.util.concurrent.ConcurrentHashMap;
import java.util.concurrent.TimeUnit;
import java.util.stream.Collectors;
import java.util.stream.Stream;
import lombok.Getter;
import lombok.extern.slf4j.Slf4j;
import static com.google.common.base.Preconditions.checkNotNull;
@Slf4j
public class AccountAgeWitnessService {
private static final Date RELEASE = Utilities.getUTCDate(2017, GregorianCalendar.NOVEMBER, 11);
private static final long SAFE_ACCOUNT_AGE_DATE = Utilities.getUTCDate(2019, GregorianCalendar.MARCH, 1).getTime();
public enum AccountAge {
UNVERIFIED,
LESS_ONE_MONTH,
ONE_TO_TWO_MONTHS,
TWO_MONTHS_OR_MORE
}
public enum SignState {
UNSIGNED(Res.get("offerbook.timeSinceSigning.notSigned")),
ARBITRATOR(Res.get("offerbook.timeSinceSigning.info.arbitrator")),
PEER_INITIAL(Res.get("offerbook.timeSinceSigning.info.peer")),
PEER_LIMIT_LIFTED(Res.get("offerbook.timeSinceSigning.info.peerLimitLifted")),
PEER_SIGNER(Res.get("offerbook.timeSinceSigning.info.signer")),
BANNED(Res.get("offerbook.timeSinceSigning.info.banned"));
private String displayString;
private String hash = "";
private long daysUntilLimitLifted = 0;
SignState(String displayString) {
this.displayString = displayString;
}
public SignState addHash(String hash) {
this.hash = hash;
return this;
}
public SignState setDaysUntilLimitLifted(long days) {
this.daysUntilLimitLifted = days;
return this;
}
public String getDisplayString() {
if (!hash.isEmpty()) { // Only showing in DEBUG mode
return displayString + " " + hash;
}
return String.format(displayString, daysUntilLimitLifted);
}
public boolean isLimitLifted() {
return this == PEER_LIMIT_LIFTED || this == PEER_SIGNER || this == ARBITRATOR;
}
}
private final KeyRing keyRing;
private final P2PService p2PService;
private final User user;
private final SignedWitnessService signedWitnessService;
private final ChargeBackRisk chargeBackRisk;
private final AccountAgeWitnessStorageService accountAgeWitnessStorageService;
private final Clock clock;
private final FilterManager filterManager;
@Getter
private final AccountAgeWitnessUtils accountAgeWitnessUtils;
private final Map<P2PDataStorage.ByteArray, AccountAgeWitness> accountAgeWitnessMap = new HashMap<>();
// The accountAgeWitnessMap is very large (70k items) and access is a bit expensive. We usually only access less
// than 100 items, those who have offers online. So we use a cache for a fast lookup and only if
// not found there we use the accountAgeWitnessMap and put then the new item into our cache.
private final Map<P2PDataStorage.ByteArray, AccountAgeWitness> accountAgeWitnessCache = new ConcurrentHashMap<>();
///////////////////////////////////////////////////////////////////////////////////////////
// Constructor
///////////////////////////////////////////////////////////////////////////////////////////
@Inject
public AccountAgeWitnessService(KeyRing keyRing,
P2PService p2PService,
User user,
SignedWitnessService signedWitnessService,
ChargeBackRisk chargeBackRisk,
AccountAgeWitnessStorageService accountAgeWitnessStorageService,
AppendOnlyDataStoreService appendOnlyDataStoreService,
Clock clock,
FilterManager filterManager) {
this.keyRing = keyRing;
this.p2PService = p2PService;
this.user = user;
this.signedWitnessService = signedWitnessService;
this.chargeBackRisk = chargeBackRisk;
this.accountAgeWitnessStorageService = accountAgeWitnessStorageService;
this.clock = clock;
this.filterManager = filterManager;
accountAgeWitnessUtils = new AccountAgeWitnessUtils(
this,
signedWitnessService,
keyRing);
// We need to add that early (before onAllServicesInitialized) as it will be used at startup.
appendOnlyDataStoreService.addService(accountAgeWitnessStorageService);
}
///////////////////////////////////////////////////////////////////////////////////////////
// Lifecycle
///////////////////////////////////////////////////////////////////////////////////////////
public void onAllServicesInitialized() {
p2PService.getP2PDataStorage().addAppendOnlyDataStoreListener(payload -> {
if (payload instanceof AccountAgeWitness)
addToMap((AccountAgeWitness) payload);
});
// At startup the P2PDataStorage initializes earlier, otherwise we get the listener called.
accountAgeWitnessStorageService.getMapOfAllData().values().stream()
.filter(e -> e instanceof AccountAgeWitness)
.map(e -> (AccountAgeWitness) e)
.forEach(this::addToMap);
if (p2PService.isBootstrapped()) {
onBootStrapped();
} else {
p2PService.addP2PServiceListener(new BootstrapListener() {
@Override
public void onUpdatedDataReceived() {
onBootStrapped();
}
});
}
}
private void onBootStrapped() {
republishAllFiatAccounts();
signAndPublishSameNameAccounts();
}
// At startup we re-publish the witness data of all fiat accounts to ensure we got our data well distributed.
private void republishAllFiatAccounts() {
if (user.getPaymentAccounts() != null)
user.getPaymentAccounts().stream()
.filter(account -> !(account instanceof AssetAccount))
.forEach(account -> {
AccountAgeWitness myWitness = getMyWitness(account.getPaymentAccountPayload());
// We only publish if the date of our witness is inside the date tolerance.
// It would be rejected otherwise from the peers.
if (myWitness.isDateInTolerance(clock)) {
// We delay with a random interval of 20-60 sec to ensure to be better connected and don't
// stress the P2P network with publishing all at once at startup time.
int delayInSec = 20 + new Random().nextInt(40);
UserThread.runAfter(() ->
p2PService.addPersistableNetworkPayload(myWitness, true), delayInSec);
}
});
}
@VisibleForTesting
public void addToMap(AccountAgeWitness accountAgeWitness) {
accountAgeWitnessMap.putIfAbsent(accountAgeWitness.getHashAsByteArray(), accountAgeWitness);
}
///////////////////////////////////////////////////////////////////////////////////////////
// Generic
///////////////////////////////////////////////////////////////////////////////////////////
public void publishMyAccountAgeWitness(PaymentAccountPayload paymentAccountPayload) {
AccountAgeWitness accountAgeWitness = getMyWitness(paymentAccountPayload);
P2PDataStorage.ByteArray hash = accountAgeWitness.getHashAsByteArray();
// We use first our fast lookup cache. If its in accountAgeWitnessCache it is also in accountAgeWitnessMap
// and we do not publish.
if (accountAgeWitnessCache.containsKey(hash)) {
return;
}
if (!accountAgeWitnessMap.containsKey(hash)) {
p2PService.addPersistableNetworkPayload(accountAgeWitness, false);
}
}
public byte[] getPeerAccountAgeWitnessHash(Trade trade) {
return findTradePeerWitness(trade)
.map(AccountAgeWitness::getHash)
.orElse(null);
}
byte[] getAccountInputDataWithSalt(PaymentAccountPayload paymentAccountPayload) {
return Utilities.concatenateByteArrays(paymentAccountPayload.getAgeWitnessInputData(),
paymentAccountPayload.getSalt());
}
@VisibleForTesting
public AccountAgeWitness getNewWitness(PaymentAccountPayload paymentAccountPayload, PubKeyRing pubKeyRing) {
byte[] accountInputDataWithSalt = getAccountInputDataWithSalt(paymentAccountPayload);
byte[] hash = Hash.getSha256Ripemd160hash(Utilities.concatenateByteArrays(accountInputDataWithSalt,
pubKeyRing.getSignaturePubKeyBytes()));
return new AccountAgeWitness(hash, new Date().getTime());
}
Optional<AccountAgeWitness> findWitness(PaymentAccountPayload paymentAccountPayload,
PubKeyRing pubKeyRing) {
if (paymentAccountPayload == null) {
return Optional.empty();
}
byte[] accountInputDataWithSalt = getAccountInputDataWithSalt(paymentAccountPayload);
byte[] hash = Hash.getSha256Ripemd160hash(Utilities.concatenateByteArrays(accountInputDataWithSalt,
pubKeyRing.getSignaturePubKeyBytes()));
return getWitnessByHash(hash);
}
public Optional<AccountAgeWitness> findWitness(Offer offer) {
final Optional<String> accountAgeWitnessHash = offer.getAccountAgeWitnessHashAsHex();
return accountAgeWitnessHash.isPresent() ?
getWitnessByHashAsHex(accountAgeWitnessHash.get()) :
Optional.empty();
}
private Optional<AccountAgeWitness> findTradePeerWitness(Trade trade) {
TradingPeer tradingPeer = trade.getProcessModel().getTradePeer();
return (tradingPeer == null ||
tradingPeer.getPaymentAccountPayload() == null ||
tradingPeer.getPubKeyRing() == null) ?
Optional.empty() :
findWitness(tradingPeer.getPaymentAccountPayload(), tradingPeer.getPubKeyRing());
}
private Optional<AccountAgeWitness> getWitnessByHash(byte[] hash) {
P2PDataStorage.ByteArray hashAsByteArray = new P2PDataStorage.ByteArray(hash);
// First we look up in our fast lookup cache
if (accountAgeWitnessCache.containsKey(hashAsByteArray)) {
return Optional.of(accountAgeWitnessCache.get(hashAsByteArray));
}
if (accountAgeWitnessMap.containsKey(hashAsByteArray)) {
AccountAgeWitness accountAgeWitness = accountAgeWitnessMap.get(hashAsByteArray);
// We add it to our fast lookup cache
accountAgeWitnessCache.put(hashAsByteArray, accountAgeWitness);
return Optional.of(accountAgeWitness);
}
return Optional.empty();
}
private Optional<AccountAgeWitness> getWitnessByHashAsHex(String hashAsHex) {
return getWitnessByHash(Utilities.decodeFromHex(hashAsHex));
}
///////////////////////////////////////////////////////////////////////////////////////////
// Witness age
///////////////////////////////////////////////////////////////////////////////////////////
public long getAccountAge(AccountAgeWitness accountAgeWitness, Date now) {
log.debug("getAccountAge now={}, accountAgeWitness.getDate()={}", now.getTime(), accountAgeWitness.getDate());
return now.getTime() - accountAgeWitness.getDate();
}
// Return -1 if no witness found
public long getAccountAge(PaymentAccountPayload paymentAccountPayload, PubKeyRing pubKeyRing) {
return findWitness(paymentAccountPayload, pubKeyRing)
.map(accountAgeWitness -> getAccountAge(accountAgeWitness, new Date()))
.orElse(-1L);
}
public long getAccountAge(Offer offer) {
return findWitness(offer)
.map(accountAgeWitness -> getAccountAge(accountAgeWitness, new Date()))
.orElse(-1L);
}
public long getAccountAge(Trade trade) {
return findTradePeerWitness(trade)
.map(accountAgeWitness -> getAccountAge(accountAgeWitness, new Date()))
.orElse(-1L);
}
///////////////////////////////////////////////////////////////////////////////////////////
// Signed age
///////////////////////////////////////////////////////////////////////////////////////////
// Return -1 if not signed
public long getWitnessSignAge(AccountAgeWitness accountAgeWitness, Date now) {
List<Long> dates = signedWitnessService.getVerifiedWitnessDateList(accountAgeWitness);
if (dates.isEmpty()) {
return -1L;
} else {
return now.getTime() - dates.get(0);
}
}
// Return -1 if not signed
public long getWitnessSignAge(Offer offer, Date now) {
return findWitness(offer)
.map(witness -> getWitnessSignAge(witness, now))
.orElse(-1L);
}
public long getWitnessSignAge(Trade trade, Date now) {
return findTradePeerWitness(trade)
.map(witness -> getWitnessSignAge(witness, now))
.orElse(-1L);
}
public AccountAge getPeersAccountAgeCategory(long peersAccountAge) {
return getAccountAgeCategory(peersAccountAge);
}
private AccountAge getAccountAgeCategory(long accountAge) {
if (accountAge < 0) {
return AccountAge.UNVERIFIED;
} else if (accountAge < TimeUnit.DAYS.toMillis(30)) {
return AccountAge.LESS_ONE_MONTH;
} else if (accountAge < TimeUnit.DAYS.toMillis(60)) {
return AccountAge.ONE_TO_TWO_MONTHS;
} else {
return AccountAge.TWO_MONTHS_OR_MORE;
}
}
// Get trade limit based on a time schedule
// Buying of BTC with a payment method that has chargeback risk will use a low trade limit schedule
// All selling and all other fiat payment methods use the normal trade limit schedule
// Non fiat always has max limit
// Account types that can get signed will use time since signing, other methods use time since account age creation
// when measuring account age
private long getTradeLimit(Coin maxTradeLimit,
String currencyCode,
AccountAgeWitness accountAgeWitness,
AccountAge accountAgeCategory,
OfferDirection direction,
PaymentMethod paymentMethod) {
if (CurrencyUtil.isCryptoCurrency(currencyCode) ||
!PaymentMethod.hasChargebackRisk(paymentMethod, currencyCode) ||
direction == OfferDirection.SELL) {
return maxTradeLimit.value;
}
long limit = OfferRestrictions.TOLERATED_SMALL_TRADE_AMOUNT.value;
var factor = signedBuyFactor(accountAgeCategory);
if (factor > 0) {
limit = MathUtils.roundDoubleToLong((double) maxTradeLimit.value * factor);
}
log.debug("limit={}, factor={}, accountAgeWitnessHash={}",
Coin.valueOf(limit).toFriendlyString(),
factor,
Utilities.bytesAsHexString(accountAgeWitness.getHash()));
return limit;
}
private double signedBuyFactor(AccountAge accountAgeCategory) {
switch (accountAgeCategory) {
case TWO_MONTHS_OR_MORE:
return 1;
case ONE_TO_TWO_MONTHS:
return 0.5;
case LESS_ONE_MONTH:
case UNVERIFIED:
default:
return 0;
}
}
private double normalFactor() {
return 1;
}
///////////////////////////////////////////////////////////////////////////////////////////
// Trade limit exceptions
///////////////////////////////////////////////////////////////////////////////////////////
private boolean isImmature(AccountAgeWitness accountAgeWitness) {
return accountAgeWitness.getDate() > SAFE_ACCOUNT_AGE_DATE;
}
public boolean myHasTradeLimitException(PaymentAccount myPaymentAccount) {
return hasTradeLimitException(getMyWitness(myPaymentAccount.getPaymentAccountPayload()));
}
// There are no trade limits on accounts that
// - are mature
// - were signed by an arbitrator
private boolean hasTradeLimitException(AccountAgeWitness accountAgeWitness) {
return !isImmature(accountAgeWitness) || signedWitnessService.isSignedByArbitrator(accountAgeWitness);
}
///////////////////////////////////////////////////////////////////////////////////////////
// My witness
///////////////////////////////////////////////////////////////////////////////////////////
public AccountAgeWitness getMyWitness(PaymentAccountPayload paymentAccountPayload) {
final Optional<AccountAgeWitness> accountAgeWitnessOptional =
findWitness(paymentAccountPayload, keyRing.getPubKeyRing());
return accountAgeWitnessOptional.orElseGet(() -> getNewWitness(paymentAccountPayload, keyRing.getPubKeyRing()));
}
private byte[] getMyWitnessHash(PaymentAccountPayload paymentAccountPayload) {
return getMyWitness(paymentAccountPayload).getHash();
}
public String getMyWitnessHashAsHex(PaymentAccountPayload paymentAccountPayload) {
return Utilities.bytesAsHexString(getMyWitnessHash(paymentAccountPayload));
}
public long getMyAccountAge(PaymentAccountPayload paymentAccountPayload) {
return getAccountAge(getMyWitness(paymentAccountPayload), new Date());
}
public long getMyTradeLimit(PaymentAccount paymentAccount, String currencyCode, OfferDirection direction) {
if (paymentAccount == null)
return 0;
AccountAgeWitness accountAgeWitness = getMyWitness(paymentAccount.getPaymentAccountPayload());
Coin maxTradeLimit = paymentAccount.getPaymentMethod().getMaxTradeLimitAsCoin(currencyCode);
if (hasTradeLimitException(accountAgeWitness)) {
return maxTradeLimit.value;
}
final long accountSignAge = getWitnessSignAge(accountAgeWitness, new Date());
AccountAge accountAgeCategory = getAccountAgeCategory(accountSignAge);
return getTradeLimit(maxTradeLimit,
currencyCode,
accountAgeWitness,
accountAgeCategory,
direction,
paymentAccount.getPaymentMethod());
}
///////////////////////////////////////////////////////////////////////////////////////////
// Verification
///////////////////////////////////////////////////////////////////////////////////////////
public boolean verifyAccountAgeWitness(Trade trade,
PaymentAccountPayload peersPaymentAccountPayload,
Date peersCurrentDate,
PubKeyRing peersPubKeyRing,
byte[] nonce,
byte[] signature,
ErrorMessageHandler errorMessageHandler) {
final Optional<AccountAgeWitness> accountAgeWitnessOptional =
findWitness(peersPaymentAccountPayload, peersPubKeyRing);
// If we don't find a stored witness data we create a new dummy object which makes is easier to reuse the
// below validation methods. This peersWitness object is not used beside for validation. Some of the
// validation calls are pointless in the case we create a new Witness ourselves but the verifyPeersTradeLimit
// need still be called, so we leave also the rest for sake of simplicity.
AccountAgeWitness peersWitness;
if (accountAgeWitnessOptional.isPresent()) {
peersWitness = accountAgeWitnessOptional.get();
} else {
peersWitness = getNewWitness(peersPaymentAccountPayload, peersPubKeyRing);
log.warn("We did not find the peers witness data. That is expected with peers using an older version.");
}
// Check if date in witness is not older than the release date of that feature (was added in v0.6)
if (!isDateAfterReleaseDate(peersWitness.getDate(), RELEASE, errorMessageHandler))
return false;
// Check if peer current date is in tolerance range
if (!verifyPeersCurrentDate(peersCurrentDate, errorMessageHandler))
return false;
final byte[] peersAccountInputDataWithSalt = Utilities.concatenateByteArrays(
peersPaymentAccountPayload.getAgeWitnessInputData(), peersPaymentAccountPayload.getSalt());
byte[] hash = Hash.getSha256Ripemd160hash(Utilities.concatenateByteArrays(peersAccountInputDataWithSalt,
peersPubKeyRing.getSignaturePubKeyBytes()));
// Check if the hash in the witness data matches the hash derived from the data provided by the peer
final byte[] peersWitnessHash = peersWitness.getHash();
if (!verifyWitnessHash(peersWitnessHash, hash, errorMessageHandler))
return false;
// Check if the peers trade limit is not less than the trade amount
if (!verifyPeersTradeLimit(trade.getOffer(), trade.getTradeAmount(), peersWitness, peersCurrentDate,
errorMessageHandler)) {
log.error("verifyPeersTradeLimit failed: peersPaymentAccountPayload {}", peersPaymentAccountPayload);
return false;
}
// Check if the signature is correct
return verifySignature(peersPubKeyRing.getSignaturePubKey(), nonce, signature, errorMessageHandler);
}
public boolean verifyPeersTradeAmount(Offer offer,
Coin tradeAmount,
ErrorMessageHandler errorMessageHandler) {
checkNotNull(offer);
// In case we don't find the witness we check if the trade amount is above the
// TOLERATED_SMALL_TRADE_AMOUNT (0.01 BTC) and only in that case return false.
return findWitness(offer)
.map(witness -> verifyPeersTradeLimit(offer, tradeAmount, witness, new Date(), errorMessageHandler))
.orElse(isToleratedSmalleAmount(tradeAmount));
}
private boolean isToleratedSmalleAmount(Coin tradeAmount) {
return tradeAmount.value <= OfferRestrictions.TOLERATED_SMALL_TRADE_AMOUNT.value;
}
///////////////////////////////////////////////////////////////////////////////////////////
// Package scope verification subroutines
///////////////////////////////////////////////////////////////////////////////////////////
boolean isDateAfterReleaseDate(long witnessDateAsLong,
Date ageWitnessReleaseDate,
ErrorMessageHandler errorMessageHandler) {
// Release date minus 1 day as tolerance for not synced clocks
Date releaseDateWithTolerance = new Date(ageWitnessReleaseDate.getTime() - TimeUnit.DAYS.toMillis(1));
final Date witnessDate = new Date(witnessDateAsLong);
final boolean result = witnessDate.after(releaseDateWithTolerance);
if (!result) {
final String msg = "Witness date is set earlier than release date of ageWitness feature. " +
"ageWitnessReleaseDate=" + ageWitnessReleaseDate + ", witnessDate=" + witnessDate;
log.warn(msg);
errorMessageHandler.handleErrorMessage(msg);
}
return result;
}
private boolean verifyPeersCurrentDate(Date peersCurrentDate, ErrorMessageHandler errorMessageHandler) {
boolean result = Math.abs(peersCurrentDate.getTime() - new Date().getTime()) <= TimeUnit.DAYS.toMillis(1);
if (!result) {
String msg = "Peers current date is further than 1 day off to our current date. " +
"PeersCurrentDate=" + peersCurrentDate + "; myCurrentDate=" + new Date();
log.warn(msg);
errorMessageHandler.handleErrorMessage(msg);
}
return result;
}
private boolean verifyWitnessHash(byte[] witnessHash,
byte[] hash,
ErrorMessageHandler errorMessageHandler) {
final boolean result = Arrays.equals(witnessHash, hash);
if (!result) {
final String msg = "witnessHash is not matching peers hash. " +
"witnessHash=" + Utilities.bytesAsHexString(witnessHash) + ", hash=" + Utilities.bytesAsHexString(hash);
log.warn(msg);
errorMessageHandler.handleErrorMessage(msg);
}
return result;
}
private boolean verifyPeersTradeLimit(Offer offer,
Coin tradeAmount,
AccountAgeWitness peersWitness,
Date peersCurrentDate,
ErrorMessageHandler errorMessageHandler) {
checkNotNull(offer);
final String currencyCode = offer.getCurrencyCode();
final Coin defaultMaxTradeLimit = offer.getPaymentMethod().getMaxTradeLimitAsCoin(currencyCode);
long peersCurrentTradeLimit = defaultMaxTradeLimit.value;
if (!hasTradeLimitException(peersWitness)) {
final long accountSignAge = getWitnessSignAge(peersWitness, peersCurrentDate);
AccountAge accountAgeCategory = getPeersAccountAgeCategory(accountSignAge);
OfferDirection direction = offer.isMyOffer(keyRing) ?
offer.getMirroredDirection() : offer.getDirection();
peersCurrentTradeLimit = getTradeLimit(defaultMaxTradeLimit, currencyCode, peersWitness,
accountAgeCategory, direction, offer.getPaymentMethod());
}
// Makers current trade limit cannot be smaller than that in the offer
boolean result = tradeAmount.value <= peersCurrentTradeLimit;
if (!result) {
String msg = "The peers trade limit is less than the traded amount.\n" +
"tradeAmount=" + tradeAmount.toFriendlyString() +
"\nPeers trade limit=" + Coin.valueOf(peersCurrentTradeLimit).toFriendlyString() +
"\nOffer ID=" + offer.getShortId() +
"\nPaymentMethod=" + offer.getPaymentMethod().getId() +
"\nCurrencyCode=" + offer.getCurrencyCode();
log.warn(msg);
errorMessageHandler.handleErrorMessage(msg);
}
return result;
}
boolean verifySignature(PublicKey peersPublicKey,
byte[] nonce,
byte[] signature,
ErrorMessageHandler errorMessageHandler) {
boolean result;
try {
result = Sig.verify(peersPublicKey, nonce, signature);
} catch (CryptoException e) {
log.warn(e.toString());
result = false;
}
if (!result) {
final String msg = "Signature of nonce is not correct. " +
"peersPublicKey=" + peersPublicKey + ", nonce(hex)=" + Utilities.bytesAsHexString(nonce) +
", signature=" + Utilities.bytesAsHexString(signature);
log.warn(msg);
errorMessageHandler.handleErrorMessage(msg);
}
return result;
}
///////////////////////////////////////////////////////////////////////////////////////////
// Witness signing
///////////////////////////////////////////////////////////////////////////////////////////
public void arbitratorSignAccountAgeWitness(Coin tradeAmount,
AccountAgeWitness accountAgeWitness,
ECKey key,
PublicKey peersPubKey) {
signedWitnessService.signAndPublishAccountAgeWitness(tradeAmount, accountAgeWitness, key, peersPubKey);
}
public String arbitratorSignOrphanWitness(AccountAgeWitness accountAgeWitness,
ECKey ecKey,
long time) {
// TODO Is not found signedWitness considered an error case?
// Previous code version was throwing an exception in case no signedWitness was found...
// signAndPublishAccountAgeWitness returns an empty string in success case and error otherwise
return signedWitnessService.getSignedWitnessSet(accountAgeWitness).stream()
.findAny()
.map(SignedWitness::getWitnessOwnerPubKey)
.map(witnessOwnerPubKey ->
signedWitnessService.signAndPublishAccountAgeWitness(accountAgeWitness, ecKey,
witnessOwnerPubKey, time)
)
.orElse("No signedWitness found");
}
public String arbitratorSignOrphanPubKey(ECKey key,
byte[] peersPubKey,
long childSignTime) {
return signedWitnessService.signTraderPubKey(key, peersPubKey, childSignTime);
}
public void arbitratorSignAccountAgeWitness(AccountAgeWitness accountAgeWitness,
ECKey key,
byte[] tradersPubKey,
long time) {
signedWitnessService.signAndPublishAccountAgeWitness(accountAgeWitness, key, tradersPubKey, time);
}
public Optional<SignedWitness> traderSignAndPublishPeersAccountAgeWitness(Trade trade) {
AccountAgeWitness peersWitness = findTradePeerWitness(trade).orElse(null);
Coin tradeAmount = trade.getTradeAmount();
checkNotNull(trade.getProcessModel().getTradePeer().getPubKeyRing(), "Peer must have a keyring");
PublicKey peersPubKey = trade.getProcessModel().getTradePeer().getPubKeyRing().getSignaturePubKey();
checkNotNull(peersWitness, "Not able to find peers witness, unable to sign for trade {}",
trade.toString());
checkNotNull(tradeAmount, "Trade amount must not be null");
checkNotNull(peersPubKey, "Peers pub key must not be null");
try {
return signedWitnessService.signAndPublishAccountAgeWitness(tradeAmount, peersWitness, peersPubKey);
} catch (CryptoException e) {
log.warn("Trader failed to sign witness, exception {}", e.toString());
}
return Optional.empty();
}
public boolean publishOwnSignedWitness(SignedWitness signedWitness) {
return signedWitnessService.publishOwnSignedWitness(signedWitness);
}
// Arbitrator signing
public List<TraderDataItem> getTraderPaymentAccounts(long safeDate,
PaymentMethod paymentMethod,
List<Dispute> disputes) {
return disputes.stream()
.filter(dispute -> dispute.getContract().getPaymentMethodId().equals(paymentMethod.getId()))
.filter(this::isNotFiltered)
.filter(this::hasChargebackRisk)
.filter(this::isBuyerWinner)
.flatMap(this::getTraderData)
.filter(Objects::nonNull)
.filter(traderDataItem ->
!signedWitnessService.isSignedAccountAgeWitness(traderDataItem.getAccountAgeWitness()))
.filter(traderDataItem -> traderDataItem.getAccountAgeWitness().getDate() < safeDate)
.distinct()
.collect(Collectors.toList());
}
private boolean isNotFiltered(Dispute dispute) {
boolean isFiltered = filterManager.isNodeAddressBanned(dispute.getContract().getBuyerNodeAddress()) ||
filterManager.isNodeAddressBanned(dispute.getContract().getSellerNodeAddress()) ||
filterManager.isCurrencyBanned(dispute.getContract().getOfferPayload().getCurrencyCode()) ||
filterManager.isPaymentMethodBanned(
PaymentMethod.getPaymentMethodById(dispute.getContract().getPaymentMethodId())) ||
filterManager.arePeersPaymentAccountDataBanned(dispute.getContract().getBuyerPaymentAccountPayload()) ||
filterManager.arePeersPaymentAccountDataBanned(
dispute.getContract().getSellerPaymentAccountPayload()) ||
filterManager.isWitnessSignerPubKeyBanned(
Utils.HEX.encode(dispute.getContract().getBuyerPubKeyRing().getSignaturePubKeyBytes())) ||
filterManager.isWitnessSignerPubKeyBanned(
Utils.HEX.encode(dispute.getContract().getSellerPubKeyRing().getSignaturePubKeyBytes()));
return !isFiltered;
}
@VisibleForTesting
public boolean hasChargebackRisk(Dispute dispute) {
return chargeBackRisk.hasChargebackRisk(dispute.getContract().getPaymentMethodId(),
dispute.getContract().getOfferPayload().getCurrencyCode());
}
private boolean isBuyerWinner(Dispute dispute) {
if (!dispute.isClosed() || dispute.getDisputeResultProperty() == null)
return false;
return dispute.getDisputeResultProperty().get().getWinner() == DisputeResult.Winner.BUYER;
}
private Stream<TraderDataItem> getTraderData(Dispute dispute) {
Coin tradeAmount = dispute.getContract().getTradeAmount();
PubKeyRing buyerPubKeyRing = dispute.getContract().getBuyerPubKeyRing();
PubKeyRing sellerPubKeyRing = dispute.getContract().getSellerPubKeyRing();
PaymentAccountPayload buyerPaymentAccountPaload = dispute.getContract().getBuyerPaymentAccountPayload();
PaymentAccountPayload sellerPaymentAccountPaload = dispute.getContract().getSellerPaymentAccountPayload();
TraderDataItem buyerData = findWitness(buyerPaymentAccountPaload, buyerPubKeyRing)
.map(witness -> new TraderDataItem(
buyerPaymentAccountPaload,
witness,
tradeAmount,
buyerPubKeyRing.getSignaturePubKey()))
.orElse(null);
TraderDataItem sellerData = findWitness(sellerPaymentAccountPaload, sellerPubKeyRing)
.map(witness -> new TraderDataItem(
sellerPaymentAccountPaload,
witness,
tradeAmount,
sellerPubKeyRing.getSignaturePubKey()))
.orElse(null);
return Stream.of(buyerData, sellerData);
}
public boolean hasSignedWitness(Offer offer) {
return findWitness(offer)
.map(signedWitnessService::isSignedAccountAgeWitness)
.orElse(false);
}
public boolean peerHasSignedWitness(Trade trade) {
return findTradePeerWitness(trade)
.map(signedWitnessService::isSignedAccountAgeWitness)
.orElse(false);
}
public boolean accountIsSigner(AccountAgeWitness accountAgeWitness) {
return signedWitnessService.isSignerAccountAgeWitness(accountAgeWitness);
}
public boolean tradeAmountIsSufficient(Coin tradeAmount) {
return signedWitnessService.isSufficientTradeAmountForSigning(tradeAmount);
}
public SignState getSignState(Offer offer) {
return findWitness(offer)
.map(this::getSignState)
.orElse(SignState.UNSIGNED);
}
public SignState getSignState(Trade trade) {
return findTradePeerWitness(trade)
.map(this::getSignState)
.orElse(SignState.UNSIGNED);
}
public SignState getSignState(AccountAgeWitness accountAgeWitness) {
// Add hash to sign state info when running in debug mode
String hash = log.isDebugEnabled() ? Utilities.bytesAsHexString(accountAgeWitness.getHash()) + "\n" +
signedWitnessService.ownerPubKeyAsString(accountAgeWitness) : "";
if (signedWitnessService.isFilteredWitness(accountAgeWitness)) {
return SignState.BANNED.addHash(hash);
}
if (signedWitnessService.isSignedByArbitrator(accountAgeWitness)) {
return SignState.ARBITRATOR.addHash(hash);
} else {
final long accountSignAge = getWitnessSignAge(accountAgeWitness, new Date());
switch (getAccountAgeCategory(accountSignAge)) {
case TWO_MONTHS_OR_MORE:
case ONE_TO_TWO_MONTHS:
return SignState.PEER_SIGNER.addHash(hash);
case LESS_ONE_MONTH:
return SignState.PEER_INITIAL.addHash(hash)
.setDaysUntilLimitLifted(30 - TimeUnit.MILLISECONDS.toDays(accountSignAge));
case UNVERIFIED:
default:
return SignState.UNSIGNED.addHash(hash);
}
}
}
public Set<AccountAgeWitness> getOrphanSignedWitnesses() {
return signedWitnessService.getRootSignedWitnessSet(false).stream()
.map(signedWitness -> getWitnessByHash(signedWitness.getAccountAgeWitnessHash()).orElse(null))
.filter(Objects::nonNull)
.collect(Collectors.toSet());
}
public void signAndPublishSameNameAccounts() {
// Collect accounts that have ownerId to sign unsigned accounts with the same ownderId
var signerAccounts = Objects.requireNonNull(user.getPaymentAccounts()).stream()
.filter(account -> account.getOwnerId() != null &&
accountIsSigner(getMyWitness(account.getPaymentAccountPayload())))
.collect(Collectors.toSet());
var unsignedAccounts = user.getPaymentAccounts().stream()
.filter(account -> account.getOwnerId() != null &&
!signedWitnessService.isSignedAccountAgeWitness(
getMyWitness(account.getPaymentAccountPayload())))
.collect(Collectors.toSet());
signerAccounts.forEach(signer -> unsignedAccounts.forEach(unsigned -> {
if (signer.getOwnerId().equals(unsigned.getOwnerId())) {
try {
signedWitnessService.selfSignAndPublishAccountAgeWitness(
getMyWitness(unsigned.getPaymentAccountPayload()));
} catch (CryptoException e) {
log.warn("Self signing failed, exception {}", e.toString());
}
}
}));
}
public Set<SignedWitness> getUnsignedSignerPubKeys() {
return signedWitnessService.getUnsignedSignerPubKeys();
}
public boolean isSignWitnessTrade(Trade trade) {
checkNotNull(trade, "trade must not be null");
checkNotNull(trade.getOffer(), "offer must not be null");
Contract contract = checkNotNull(trade.getContract());
PaymentAccountPayload sellerPaymentAccountPayload = contract.getSellerPaymentAccountPayload();
AccountAgeWitness myWitness = getMyWitness(sellerPaymentAccountPayload);
getAccountAgeWitnessUtils().witnessDebugLog(trade, myWitness);
return accountIsSigner(myWitness) &&
!peerHasSignedWitness(trade) &&
tradeAmountIsSufficient(trade.getTradeAmount());
}
public String getSignInfoFromAccount(PaymentAccount paymentAccount) {
var pubKey = keyRing.getSignatureKeyPair().getPublic();
var witness = getMyWitness(paymentAccount.getPaymentAccountPayload());
return Utilities.bytesAsHexString(witness.getHash()) + "," + Utilities.bytesAsHexString(pubKey.getEncoded());
}
public Tuple2<AccountAgeWitness, byte[]> getSignInfoFromString(String signInfo) {
var parts = signInfo.split(",");
if (parts.length != 2) {
return null;
}
byte[] pubKeyHash;
Optional<AccountAgeWitness> accountAgeWitness;
try {
var accountAgeWitnessHash = Utilities.decodeFromHex(parts[0]);
pubKeyHash = Utilities.decodeFromHex(parts[1]);
accountAgeWitness = getWitnessByHash(accountAgeWitnessHash);
return accountAgeWitness
.map(ageWitness -> new Tuple2<>(ageWitness, pubKeyHash))
.orElse(null);
} catch (Exception e) {
return null;
}
}
}