Use KeyBagSupplier in ProtoResolver

This commit is contained in:
Manfred Karrer 2017-03-29 12:14:46 -05:00
parent d00b8e6c90
commit 31b310e59b
4 changed files with 97 additions and 3 deletions

View file

@ -20,6 +20,7 @@ package io.bisq.core.btc;
import io.bisq.common.app.Version;
import io.bisq.common.persistance.Persistable;
import io.bisq.common.util.Utilities;
import io.bisq.core.btc.wallet.KeyBagSupplier;
import lombok.EqualsAndHashCode;
import lombok.Getter;
import lombok.extern.slf4j.Slf4j;
@ -30,11 +31,15 @@ import org.bitcoinj.crypto.DeterministicKey;
import org.bitcoinj.params.MainNetParams;
import org.bitcoinj.params.RegTestParams;
import org.bitcoinj.params.TestNet3Params;
import org.jetbrains.annotations.NotNull;
import javax.annotation.Nullable;
import java.io.IOException;
import java.io.ObjectInputStream;
import static com.google.common.base.Preconditions.checkArgument;
import static com.google.common.base.Preconditions.checkNotNull;
/**
* Every trade use a addressEntry with a dedicated address for all transactions related to the trade.
* That way we have a kind of separated trade wallet, isolated from other transactions and avoiding coin merge.
@ -63,6 +68,8 @@ public final class AddressEntry implements Persistable {
transient private DeterministicKey keyPair;
@Nullable
private final String offerId;
@Nullable
private KeyBagSupplier keyBagSupplier;
private final Context context;
private final byte[] pubKey;
private final byte[] pubKeyHash;
@ -80,7 +87,10 @@ public final class AddressEntry implements Persistable {
this(keyPair, params, context, null);
}
public AddressEntry(DeterministicKey keyPair, NetworkParameters params, Context context, @Nullable String offerId) {
public AddressEntry(@NotNull DeterministicKey keyPair,
NetworkParameters params,
Context context,
@Nullable String offerId) {
this.keyPair = keyPair;
this.params = params;
this.context = context;
@ -90,6 +100,21 @@ public final class AddressEntry implements Persistable {
pubKeyHash = keyPair.getPubKeyHash();
}
// called from Resolver
public AddressEntry(byte[] pubKey,
byte[] pubKeyHash,
String paramId,
Context context,
@Nullable String offerId,
@NotNull KeyBagSupplier keyBagSupplier) {
this.pubKey = pubKey;
this.pubKeyHash = pubKeyHash;
this.paramId = paramId;
this.context = context;
this.offerId = offerId;
this.keyBagSupplier = keyBagSupplier;
}
private void readObject(ObjectInputStream in) throws IOException, ClassNotFoundException {
try {
in.defaultReadObject();
@ -107,11 +132,26 @@ public final class AddressEntry implements Persistable {
}
// Set after wallet is ready
//todo can be removed once keyBagSupplier is used
public void setDeterministicKey(DeterministicKey deterministicKey) {
this.keyPair = deterministicKey;
}
public void setCoinLockedInMultiSig(Coin coinLockedInMultiSig) {
// getKeyPair must not be called before wallet is ready (in case we get the object recreated from disk deserialization)
// If the object is created at runtime it must be always constructed after wallet is ready.
@NotNull
public DeterministicKey getKeyPair() {
if (keyPair == null) {
checkNotNull(keyBagSupplier, "keyBagConsumer must not be null if keyPair is null (protobuffer case)");
checkNotNull(pubKeyHash, "pubKeyHash must not be null");
checkArgument(keyBagSupplier.isKeyBagReady(), "getKeyPair must nto be called before keybag is ready");
keyPair = (DeterministicKey) keyBagSupplier.getKeyBag().findKeyFromPubHash(pubKeyHash);
checkNotNull(keyPair, "keyPair must not be null");
}
return keyPair;
}
public void setCoinLockedInMultiSig(@NotNull Coin coinLockedInMultiSig) {
this.coinLockedInMultiSig = coinLockedInMultiSig;
}

View file

@ -31,6 +31,7 @@ import org.bitcoinj.core.*;
import org.bitcoinj.crypto.DeterministicKey;
import org.bitcoinj.crypto.KeyCrypterScrypt;
import org.bitcoinj.script.ScriptBuilder;
import org.bitcoinj.wallet.KeyBag;
import org.jetbrains.annotations.NotNull;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
@ -44,7 +45,7 @@ import java.util.stream.Collectors;
import static com.google.common.base.Preconditions.checkArgument;
import static com.google.common.base.Preconditions.checkNotNull;
public class BtcWalletService extends WalletService {
public class BtcWalletService extends WalletService implements KeyBagSupplier {
private static final Logger log = LoggerFactory.getLogger(BtcWalletService.class);
private final AddressEntryList addressEntryList;
@ -113,6 +114,21 @@ public class BtcWalletService extends WalletService {
}
///////////////////////////////////////////////////////////////////////////////////////////
// KeyBagSupplier
///////////////////////////////////////////////////////////////////////////////////////////
@Override
public KeyBag getKeyBag() {
return wallet;
}
@Override
public boolean isKeyBagReady() {
return wallet != null;
}
///////////////////////////////////////////////////////////////////////////////////////////
// Public Methods
///////////////////////////////////////////////////////////////////////////////////////////

View file

@ -0,0 +1,26 @@
/*
* This file is part of Bitsquare.
*
* Bitsquare 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.
*
* Bitsquare 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 Bitsquare. If not, see <http://www.gnu.org/licenses/>.
*/
package io.bisq.core.btc.wallet;
import org.bitcoinj.wallet.KeyBag;
public interface KeyBagSupplier {
KeyBag getKeyBag();
boolean isKeyBagReady();
}

View file

@ -11,7 +11,9 @@ import io.bisq.core.alert.PrivateNotificationMsg;
import io.bisq.core.alert.PrivateNotificationPayload;
import io.bisq.core.arbitration.*;
import io.bisq.core.arbitration.messages.*;
import io.bisq.core.btc.AddressEntry;
import io.bisq.core.btc.data.RawTransactionInput;
import io.bisq.core.btc.wallet.BtcWalletService;
import io.bisq.core.dao.compensation.CompensationRequestPayload;
import io.bisq.core.filter.Filter;
import io.bisq.core.filter.PaymentAccountFilter;
@ -52,6 +54,7 @@ import org.jetbrains.annotations.NotNull;
import org.jetbrains.annotations.Nullable;
import org.springframework.util.CollectionUtils;
import javax.inject.Inject;
import java.io.IOException;
import java.io.StringWriter;
import java.util.*;
@ -73,6 +76,13 @@ import static io.bisq.generated.protobuffer.PB.Envelope.MessageCase.*;
*/
@Slf4j
public class CoreProtobufferResolver implements ProtobufferResolver {
private BtcWalletService btcWalletService;
@Inject
public CoreProtobufferResolver(BtcWalletService btcWalletService) {
this.btcWalletService = btcWalletService;
}
@Override
public Optional<Msg> fromProto(PB.Envelope envelope) {
if (Objects.isNull(envelope)) {
@ -95,6 +105,8 @@ public class CoreProtobufferResolver implements ProtobufferResolver {
} catch (IOException e) {
e.printStackTrace();
}
// todo just for testing...
AddressEntry AddressEntry = new AddressEntry(null, null, null, null, null, btcWalletService);
Msg result = null;
switch (envelope.getMessageCase()) {