Skip to content

Commit

Permalink
Publish own witness received as trade message
Browse files Browse the repository at this point in the history
  • Loading branch information
sqrrm committed Jul 3, 2020
1 parent 71ff071 commit 22814d1
Show file tree
Hide file tree
Showing 3 changed files with 35 additions and 1 deletion.
Original file line number Diff line number Diff line change
Expand Up @@ -204,6 +204,17 @@ public Set<SignedWitness> getSignedWitnessSetByOwnerPubKey(byte[] ownerPubKey) {
.collect(Collectors.toSet());
}

public boolean publishOwnSignedWitness(SignedWitness signedWitness) {
if (!Arrays.equals(signedWitness.getWitnessOwnerPubKey(), keyRing.getPubKeyRing().getSignaturePubKeyBytes()) ||
!verifySigner(signedWitness)) {
return false;
}

log.info("Publish own signedWitness {}", signedWitness);
publishSignedWitness(signedWitness);
return true;
}

// Arbitrators sign with EC key
public void signAccountAgeWitness(Coin tradeAmount,
AccountAgeWitness accountAgeWitness,
Expand Down Expand Up @@ -395,6 +406,11 @@ public boolean isSufficientTradeAmountForSigning(Coin tradeAmount) {
return !tradeAmount.isLessThan(MINIMUM_TRADE_AMOUNT_FOR_SIGNING);
}

private boolean verifySigner(SignedWitness signedWitness) {
return getSignedWitnessSetByOwnerPubKey(signedWitness.getWitnessOwnerPubKey(), new Stack<>()).stream()
.anyMatch(w -> isValidSignerWitnessInternal(w, signedWitness.getDate(), new Stack<>()));
}

/**
* Checks whether the accountAgeWitness has a valid signature from a peer/arbitrator and is allowed to sign
* other accounts.
Expand All @@ -419,7 +435,7 @@ private boolean isSignerAccountAgeWitness(AccountAgeWitness accountAgeWitness, l
* Helper to isValidAccountAgeWitness(accountAgeWitness)
*
* @param signedWitness the signedWitness to validate
* @param childSignedWitnessDateMillis the date the child SignedWitness was signed or current time if it is a leave.
* @param childSignedWitnessDateMillis the date the child SignedWitness was signed or current time if it is a leaf.
* @param excludedPubKeys stack to prevent recursive loops
* @return true if signedWitness is valid, false otherwise.
*/
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -685,6 +685,10 @@ public Optional<SignedWitness> traderSignPeersAccountAgeWitness(Trade trade) {
return Optional.empty();
}

public boolean publishOwnSignedWitness(SignedWitness signedWitness) {
return signedWitnessService.publishOwnSignedWitness(signedWitness);
}

// Arbitrator signing
public List<TraderDataItem> getTraderPaymentAccounts(long safeDate,
PaymentMethod paymentMethod,
Expand Down
14 changes: 14 additions & 0 deletions core/src/main/java/bisq/core/trade/protocol/TradeProtocol.java
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,7 @@
import bisq.core.trade.messages.MediatedPayoutTxSignatureMessage;
import bisq.core.trade.messages.PeerPublishedDelayedPayoutTxMessage;
import bisq.core.trade.messages.TradeMessage;
import bisq.core.trade.messages.TraderSignedWitnessMessage;
import bisq.core.trade.protocol.tasks.ApplyFilter;
import bisq.core.trade.protocol.tasks.ProcessPeerPublishedDelayedPayoutTxMessage;
import bisq.core.trade.protocol.tasks.mediation.BroadcastMediatedPayoutTx;
Expand Down Expand Up @@ -228,6 +229,15 @@ private void handle(PeerPublishedDelayedPayoutTxMessage tradeMessage, NodeAddres
taskRunner.run();
}

///////////////////////////////////////////////////////////////////////////////////////////
// Peer has sent a SignedWitness
///////////////////////////////////////////////////////////////////////////////////////////

private void handle(TraderSignedWitnessMessage tradeMessage, NodeAddress sender) {
// Publish signed witness, if it is valid and ours
processModel.getAccountAgeWitnessService().publishOwnSignedWitness(tradeMessage.getSignedWitness());
}


///////////////////////////////////////////////////////////////////////////////////////////
// Dispatcher
Expand All @@ -240,6 +250,8 @@ protected void doHandleDecryptedMessage(TradeMessage tradeMessage, NodeAddress s
handle((MediatedPayoutTxPublishedMessage) tradeMessage, sender);
} else if (tradeMessage instanceof PeerPublishedDelayedPayoutTxMessage) {
handle((PeerPublishedDelayedPayoutTxMessage) tradeMessage, sender);
} else if (tradeMessage instanceof TraderSignedWitnessMessage) {
handle((TraderSignedWitnessMessage) tradeMessage, sender);
}
}

Expand Down Expand Up @@ -287,6 +299,8 @@ protected void doApplyMailboxTradeMessage(TradeMessage tradeMessage, NodeAddress
handle((MediatedPayoutTxPublishedMessage) tradeMessage, peerNodeAddress);
} else if (tradeMessage instanceof PeerPublishedDelayedPayoutTxMessage) {
handle((PeerPublishedDelayedPayoutTxMessage) tradeMessage, peerNodeAddress);
} else if (tradeMessage instanceof TraderSignedWitnessMessage) {
handle((TraderSignedWitnessMessage) tradeMessage, peerNodeAddress);
}
}

Expand Down

0 comments on commit 22814d1

Please sign in to comment.