Refactor key handling

This commit is contained in:
Manfred Karrer 2015-03-16 01:50:46 +01:00
parent 32db4c0c75
commit ca32014f8b
24 changed files with 147 additions and 190 deletions

View file

@ -19,7 +19,6 @@ package io.bitsquare.btc;
import org.bitcoinj.core.Address; import org.bitcoinj.core.Address;
import org.bitcoinj.core.NetworkParameters; import org.bitcoinj.core.NetworkParameters;
import org.bitcoinj.core.Utils;
import org.bitcoinj.crypto.DeterministicKey; import org.bitcoinj.crypto.DeterministicKey;
import java.io.Serializable; import java.io.Serializable;
@ -32,26 +31,25 @@ import java.util.Arrays;
*/ */
public class AddressEntry implements Serializable { public class AddressEntry implements Serializable {
private static final long serialVersionUID = 5501603992599920416L; private static final long serialVersionUID = 5501603992599920416L;
private transient DeterministicKey key; private transient DeterministicKey keyPair;
private final NetworkParameters params; private final NetworkParameters params;
private final AddressContext addressContext; private final AddressContext addressContext;
private final String offerId; private final String offerId;
private final byte[] pubKey; private final byte[] pubKey;
private final byte[] pubKeyHash; private final byte[] pubKeyHash;
public AddressEntry(DeterministicKey keyPair, NetworkParameters params, AddressContext addressContext) {
public AddressEntry(DeterministicKey key, NetworkParameters params, AddressContext addressContext) { this(keyPair, params, addressContext, null);
this(key, params, addressContext, null);
} }
public AddressEntry(DeterministicKey key, NetworkParameters params, AddressContext addressContext, String offerId) { public AddressEntry(DeterministicKey keyPair, NetworkParameters params, AddressContext addressContext, String offerId) {
this.key = key; this.keyPair = keyPair;
this.params = params; this.params = params;
this.addressContext = addressContext; this.addressContext = addressContext;
this.offerId = offerId; this.offerId = offerId;
pubKey = key.getPubOnly().getPubKey(); pubKey = keyPair.getPubOnly().getPubKey();
pubKeyHash = key.getPubOnly().getPubKeyHash(); pubKeyHash = keyPair.getPubOnly().getPubKeyHash();
} }
public String getOfferId() { public String getOfferId() {
@ -66,20 +64,16 @@ public class AddressEntry implements Serializable {
return getAddress().toString(); return getAddress().toString();
} }
public String getPubKeyAsHex() { public DeterministicKey getKeyPair() {
return Utils.HEX.encode(key.getPubKey()); return keyPair;
}
public DeterministicKey getKey() {
return key;
} }
public Address getAddress() { public Address getAddress() {
return key.toAddress(params); return keyPair.toAddress(params);
} }
public void setDeterministicKey(DeterministicKey key) { public void setDeterministicKey(DeterministicKey deterministicKey) {
this.key = key; this.keyPair = deterministicKey;
} }
public byte[] getPubKeyHash() { public byte[] getPubKeyHash() {
@ -100,7 +94,7 @@ public class AddressEntry implements Serializable {
public String toString() { public String toString() {
return "AddressEntry{" + return "AddressEntry{" +
"addressString=" + getAddress().toString() + "addressString=" + getAddress().toString() +
"key=" + key + "key=" + keyPair +
", params=" + params + ", params=" + params +
", addressContext=" + addressContext + ", addressContext=" + addressContext +
", offerId='" + offerId + '\'' + ", offerId='" + offerId + '\'' +

View file

@ -490,8 +490,7 @@ public class WalletService {
Transaction tx = new Transaction(params); Transaction tx = new Transaction(params);
byte[] data = signatureService.digestMessageWithSignature( byte[] data = signatureService.digestMessageWithSignature(getRegistrationAddressEntry().getKeyPair(), stringifiedBankAccounts);
getRegistrationAddressEntry().getKey(), stringifiedBankAccounts);
tx.addOutput(Transaction.MIN_NONDUST_OUTPUT, new ScriptBuilder().op(OP_RETURN).data(data).build()); tx.addOutput(Transaction.MIN_NONDUST_OUTPUT, new ScriptBuilder().op(OP_RETURN).data(data).build());
// We don't take a fee at the moment // We don't take a fee at the moment
@ -615,7 +614,7 @@ public class WalletService {
Transaction dummyTX = new Transaction(params); Transaction dummyTX = new Transaction(params);
// The output is just used to get the right inputs and change outputs, so we use an anonymous ECKey, as it will never be used for anything. // The output is just used to get the right inputs and change outputs, so we use an anonymous ECKey, as it will never be used for anything.
// We don't care about fee calculation differences between the real tx and that dummy tx as we use a static tx fee. // We don't care about fee calculation differences between the real tx and that dummy tx as we use a static tx fee.
TransactionOutput msOutput = new TransactionOutput(params, dummyTX, dummyOutputAmount, new ECKey()); TransactionOutput msOutput = new TransactionOutput(params, dummyTX, dummyOutputAmount, new ECKey().toAddress(params));
dummyTX.addOutput(msOutput); dummyTX.addOutput(msOutput);
// Fin the needed inputs to pay the output, optional add change output. // Fin the needed inputs to pay the output, optional add change output.
@ -859,7 +858,7 @@ public class WalletService {
TransactionOutput multiSigOutput = tx.getInput(0).getConnectedOutput(); TransactionOutput multiSigOutput = tx.getInput(0).getConnectedOutput();
Script multiSigScript = multiSigOutput.getScriptPubKey(); Script multiSigScript = multiSigOutput.getScriptPubKey();
Sha256Hash sigHash = tx.hashForSignature(0, multiSigScript, Transaction.SigHash.ALL, false); Sha256Hash sigHash = tx.hashForSignature(0, multiSigScript, Transaction.SigHash.ALL, false);
ECKey.ECDSASignature offererSignature = getAddressInfo(tradeID).getKey().sign(sigHash); ECKey.ECDSASignature offererSignature = getAddressInfo(tradeID).getKeyPair().sign(sigHash);
TransactionSignature offererTxSig = new TransactionSignature(offererSignature, Transaction.SigHash.ALL, false); TransactionSignature offererTxSig = new TransactionSignature(offererSignature, Transaction.SigHash.ALL, false);
Script inputScript = ScriptBuilder.createMultiSigInputScript(ImmutableList.of(offererTxSig)); Script inputScript = ScriptBuilder.createMultiSigInputScript(ImmutableList.of(offererTxSig));
@ -895,7 +894,7 @@ public class WalletService {
Sha256Hash sigHash = tx.hashForSignature(0, multiSigScript, Transaction.SigHash.ALL, false); Sha256Hash sigHash = tx.hashForSignature(0, multiSigScript, Transaction.SigHash.ALL, false);
log.trace("sigHash=" + sigHash); log.trace("sigHash=" + sigHash);
ECKey.ECDSASignature takerSignature = getAddressInfo(tradeID).getKey().sign(sigHash); ECKey.ECDSASignature takerSignature = getAddressInfo(tradeID).getKeyPair().sign(sigHash);
TransactionSignature takerTxSig = new TransactionSignature(takerSignature, Transaction.SigHash.ALL, false); TransactionSignature takerTxSig = new TransactionSignature(takerSignature, Transaction.SigHash.ALL, false);
TransactionSignature offererTxSig = new TransactionSignature(offererSignature, Transaction.SigHash.ALL, false); TransactionSignature offererTxSig = new TransactionSignature(offererSignature, Transaction.SigHash.ALL, false);

View file

@ -157,7 +157,7 @@ class MainViewModel implements ViewModel {
error -> log.error(error.toString()), error -> log.error(error.toString()),
() -> Platform.runLater(() -> setBitcoinNetworkSyncProgress(1.0))); () -> Platform.runLater(() -> setBitcoinNetworkSyncProgress(1.0)));
Observable<BootstrapState> messageObservable = clientNode.bootstrap(user.getMessageKeyPair(), tradeMessageService); Observable<BootstrapState> messageObservable = clientNode.bootstrap(user.getNetworkKeyPair(), tradeMessageService);
messageObservable.publish(); messageObservable.publish();
messageObservable.subscribe( messageObservable.subscribe(
state -> Platform.runLater(() -> setBootstrapState(state)), state -> Platform.runLater(() -> setBootstrapState(state)),
@ -350,9 +350,9 @@ class MainViewModel implements ViewModel {
} }
private void addMockArbitrator() { private void addMockArbitrator() {
if (accountSettings.getAcceptedArbitrators().isEmpty() && user.getMessageKeyPair() != null) { if (accountSettings.getAcceptedArbitrators().isEmpty() && user.getNetworkKeyPair() != null) {
byte[] pubKey = new ECKey().getPubKey(); byte[] pubKey = new ECKey().getPubKey();
String messagePubKeyAsHex = DSAKeyUtil.getHexStringFromPublicKey(user.getMessagePublicKey()); String messagePubKeyAsHex = DSAKeyUtil.getHexStringFromPublicKey(user.getNetworkPubKey());
List<Locale> languages = new ArrayList<>(); List<Locale> languages = new ArrayList<>();
languages.add(LanguageUtil.getDefaultLanguageLocale()); languages.add(LanguageUtil.getDefaultLanguageLocale());
List<Arbitrator.METHOD> arbitrationMethods = new ArrayList<>(); List<Arbitrator.METHOD> arbitrationMethods = new ArrayList<>();

View file

@ -370,7 +370,7 @@ public class ArbitratorRegistrationView extends ActivatableView<AnchorPane, Void
private Arbitrator getEditedArbitrator() { private Arbitrator getEditedArbitrator() {
byte[] pubKey = walletService.getArbitratorDepositAddressEntry().getPubKey(); byte[] pubKey = walletService.getArbitratorDepositAddressEntry().getPubKey();
String messagePubKeyAsHex = DSAKeyUtil.getHexStringFromPublicKey(user.getMessagePublicKey()); String messagePubKeyAsHex = DSAKeyUtil.getHexStringFromPublicKey(user.getNetworkPubKey());
String name = nameTextField.getText(); String name = nameTextField.getText();
Coin fee = formatter.parseToCoin(arbitrationFeeTextField.getText()); Coin fee = formatter.parseToCoin(arbitrationFeeTextField.getText());
String webUrl = webPageTextField.getText(); String webUrl = webPageTextField.getText();

View file

@ -122,9 +122,9 @@ class IrcAccountDataModel implements Activatable, DataModel {
} }
private void addMockArbitrator() { private void addMockArbitrator() {
if (accountSettings.getAcceptedArbitrators().isEmpty() && user.getMessageKeyPair() != null) { if (accountSettings.getAcceptedArbitrators().isEmpty() && user.getNetworkKeyPair() != null) {
byte[] pubKey = new ECKey().getPubKey(); byte[] pubKey = new ECKey().getPubKey();
String messagePubKeyAsHex = DSAKeyUtil.getHexStringFromPublicKey(user.getMessagePublicKey()); String messagePubKeyAsHex = DSAKeyUtil.getHexStringFromPublicKey(user.getNetworkPubKey());
List<Locale> languages = new ArrayList<>(); List<Locale> languages = new ArrayList<>();
languages.add(LanguageUtil.getDefaultLanguageLocale()); languages.add(LanguageUtil.getDefaultLanguageLocale());
List<Arbitrator.METHOD> arbitrationMethods = new ArrayList<>(); List<Arbitrator.METHOD> arbitrationMethods = new ArrayList<>();

View file

@ -152,9 +152,9 @@ class RestrictionsDataModel implements Activatable, DataModel {
// TODO Remove mock later // TODO Remove mock later
private void addMockArbitrator() { private void addMockArbitrator() {
if (accountSettings.getAcceptedArbitrators().isEmpty() && user.getMessageKeyPair() != null) { if (accountSettings.getAcceptedArbitrators().isEmpty() && user.getNetworkKeyPair() != null) {
byte[] pubKey = new ECKey().getPubKey(); byte[] pubKey = new ECKey().getPubKey();
String messagePubKeyAsHex = DSAKeyUtil.getHexStringFromPublicKey(user.getMessagePublicKey()); String messagePubKeyAsHex = DSAKeyUtil.getHexStringFromPublicKey(user.getNetworkPubKey());
List<Locale> languages = new ArrayList<>(); List<Locale> languages = new ArrayList<>();
languages.add(LanguageUtil.getDefaultLanguageLocale()); languages.add(LanguageUtil.getDefaultLanguageLocale());
List<Arbitrator.METHOD> arbitrationMethods = new ArrayList<>(); List<Arbitrator.METHOD> arbitrationMethods = new ArrayList<>();

View file

@ -74,7 +74,7 @@ class ClosedTradesDataModel implements Activatable, DataModel {
} }
public Direction getDirection(Offer offer) { public Direction getDirection(Offer offer) {
return offer.getMessagePublicKey().equals(user.getMessagePublicKey()) ? return offer.getMessagePublicKey().equals(user.getNetworkPubKey()) ?
offer.getDirection() : offer.getMirroredDirection(); offer.getDirection() : offer.getMirroredDirection();
} }

View file

@ -88,7 +88,7 @@ class OffersDataModel implements Activatable, DataModel {
public Direction getDirection(OpenOffer openOffer) { public Direction getDirection(OpenOffer openOffer) {
Offer offer = openOffer.getOffer(); Offer offer = openOffer.getOffer();
return offer.getMessagePublicKey().equals(user.getMessagePublicKey()) ? return offer.getMessagePublicKey().equals(user.getNetworkPubKey()) ?
offer.getDirection() : offer.getMirroredDirection(); offer.getDirection() : offer.getMirroredDirection();
} }
} }

View file

@ -130,7 +130,7 @@ class PendingTradesDataModel implements Activatable, DataModel {
selectedItem = item; selectedItem = item;
if (selectedItem != null) { if (selectedItem != null) {
isOfferer = getTrade().getOffer().getMessagePublicKey().equals(user.getMessagePublicKey()); isOfferer = getTrade().getOffer().getMessagePublicKey().equals(user.getNetworkPubKey());
Trade trade = getTrade(); Trade trade = getTrade();
trade.stateProperty().addListener(stateChangeListener); trade.stateProperty().addListener(stateChangeListener);
@ -253,7 +253,7 @@ class PendingTradesDataModel implements Activatable, DataModel {
} }
public Direction getDirection(Offer offer) { public Direction getDirection(Offer offer) {
return offer.getMessagePublicKey().equals(user.getMessagePublicKey()) ? return offer.getMessagePublicKey().equals(user.getNetworkPubKey()) ?
offer.getDirection() : offer.getMirroredDirection(); offer.getDirection() : offer.getMirroredDirection();
} }

View file

@ -213,7 +213,7 @@ class OfferBookDataModel implements Activatable, DataModel {
} }
boolean isMyOffer(Offer offer) { boolean isMyOffer(Offer offer) {
return offer.getMessagePublicKey() != null && offer.getMessagePublicKey().equals(user.getMessagePublicKey()); return offer.getMessagePublicKey() != null && offer.getMessagePublicKey().equals(user.getNetworkPubKey());
} }
Coin getAmountAsCoin() { Coin getAmountAsCoin() {

View file

@ -167,7 +167,7 @@ public class TradeManager {
BankAccount currentBankAccount = user.getCurrentBankAccount().get(); BankAccount currentBankAccount = user.getCurrentBankAccount().get();
Offer offer = new Offer(id, Offer offer = new Offer(id,
user.getMessagePublicKey(), user.getNetworkPubKey(),
direction, direction,
price.getValue(), price.getValue(),
amount, amount,

View file

@ -27,7 +27,7 @@ import io.bitsquare.trade.TradeMessageService;
import io.bitsquare.user.User; import io.bitsquare.user.User;
import io.bitsquare.util.taskrunner.SharedModel; import io.bitsquare.util.taskrunner.SharedModel;
import org.bitcoinj.core.ECKey; import org.bitcoinj.crypto.DeterministicKey;
import java.security.PublicKey; import java.security.PublicKey;
@ -47,18 +47,15 @@ public class OfferSharedModel extends SharedModel {
// derived // derived
protected final BankAccount bankAccount; protected final BankAccount bankAccount;
protected final String accountId; protected final String accountId;
protected final PublicKey messagePublicKey; protected final PublicKey networkPubKey;
protected final ECKey accountKey; protected final byte[] registrationPubKey;
protected final byte[] arbitratorPubKey;
// lazy initialized at first read access, as we don't want to create an entry before it is really needed protected final DeterministicKey registrationKeyPair;
protected AddressEntry addressInfo; protected final byte[] arbitratorPubKey;
protected final AddressEntry addressInfo;
// data written/read by tasks // data written/read by tasks
protected TradeMessage tradeMessage; protected TradeMessage tradeMessage;
protected byte[] takerPubKey;
protected String peersAccountId;
protected BankAccount peersBankAccount;
public OfferSharedModel(Offer offer, public OfferSharedModel(Offer offer,
TradeMessageService tradeMessageService, TradeMessageService tradeMessageService,
@ -74,34 +71,26 @@ public class OfferSharedModel extends SharedModel {
//TODO use default arbitrator for now //TODO use default arbitrator for now
arbitratorPubKey = offer.getArbitrators().get(0).getPubKey(); arbitratorPubKey = offer.getArbitrators().get(0).getPubKey();
registrationPubKey = walletService.getRegistrationAddressEntry().getPubKey();
registrationKeyPair = walletService.getRegistrationAddressEntry().getKeyPair();
addressInfo = walletService.getAddressInfo(offer.getId());
bankAccount = user.getBankAccount(offer.getBankAccountId()); bankAccount = user.getBankAccount(offer.getBankAccountId());
accountId = user.getAccountId(); accountId = user.getAccountId();
messagePublicKey = user.getMessagePublicKey(); networkPubKey = user.getNetworkPubKey();
accountKey = walletService.getRegistrationAddressEntry().getKey();
} }
// getter/setter // getter/setter
public AddressEntry getAddressInfo() {
if (addressInfo == null)
addressInfo = getWalletService().getAddressInfo(offer.getId());
return addressInfo; public TradeMessage getTradeMessage() {
return tradeMessage;
} }
public String getPeersAccountId() { public void setTradeMessage(TradeMessage tradeMessage) {
return peersAccountId; this.tradeMessage = tradeMessage;
} }
public void setPeersAccountId(String peersAccountId) { public Offer getOffer() {
this.peersAccountId = peersAccountId; return offer;
}
public BankAccount getPeersBankAccount() {
return peersBankAccount;
}
public void setPeersBankAccount(BankAccount peersBankAccount) {
this.peersBankAccount = peersBankAccount;
} }
public TradeMessageService getTradeMessageService() { public TradeMessageService getTradeMessageService() {
@ -120,14 +109,6 @@ public class OfferSharedModel extends SharedModel {
return signatureService; return signatureService;
} }
public Offer getOffer() {
return offer;
}
public byte[] getArbitratorPubKey() {
return arbitratorPubKey;
}
public BankAccount getBankAccount() { public BankAccount getBankAccount() {
return bankAccount; return bankAccount;
} }
@ -136,44 +117,24 @@ public class OfferSharedModel extends SharedModel {
return accountId; return accountId;
} }
public PublicKey getMessagePublicKey() { public PublicKey getNetworkPubKey() {
return messagePublicKey; return networkPubKey;
} }
public ECKey getAccountKey() { public byte[] getRegistrationPubKey() {
return accountKey; return registrationPubKey;
} }
public byte[] getTakerPubKey() { public DeterministicKey getRegistrationKeyPair() {
return takerPubKey; return registrationKeyPair;
} }
public void setTakerPubKey(byte[] takerPubKey) { public byte[] getArbitratorPubKey() {
this.takerPubKey = takerPubKey; return arbitratorPubKey;
} }
public String getTakerAccountId() { public AddressEntry getAddressInfo() {
return peersAccountId; return addressInfo;
}
public void setTakerAccountId(String peersAccountId) {
this.peersAccountId = peersAccountId;
}
public BankAccount getTakerBankAccount() {
return peersBankAccount;
}
public void setTakerBankAccount(BankAccount peersBankAccount) {
this.peersBankAccount = peersBankAccount;
}
public TradeMessage getTradeMessage() {
return tradeMessage;
}
public void setTradeMessage(TradeMessage tradeMessage) {
this.tradeMessage = tradeMessage;
} }
} }

View file

@ -44,12 +44,11 @@ public class BuyerAsOffererModel extends OfferSharedModel {
private static final Logger log = LoggerFactory.getLogger(BuyerAsOffererModel.class); private static final Logger log = LoggerFactory.getLogger(BuyerAsOffererModel.class);
// provided // provided
private final OpenOffer openOffer; private final OpenOffer openOffer;
// derived // derived
private final String offererPaybackAddress; private final byte[] offererPubKey;
// data written/read by tasks // data written/read by tasks
private Trade trade; private Trade trade;
@ -63,7 +62,7 @@ public class BuyerAsOffererModel extends OfferSharedModel {
private Coin takerPaybackAmount; private Coin takerPaybackAmount;
private String takeOfferFeeTxId; private String takeOfferFeeTxId;
private byte[] offererPubKey;
private ECKey.ECDSASignature offererSignature; private ECKey.ECDSASignature offererSignature;
private Coin offererPaybackAmount; private Coin offererPaybackAmount;
private List<TransactionOutput> offererConnectedOutputsForAllInputs; private List<TransactionOutput> offererConnectedOutputsForAllInputs;
@ -74,6 +73,7 @@ public class BuyerAsOffererModel extends OfferSharedModel {
private String takerPayoutAddress; private String takerPayoutAddress;
private Transaction offererPayoutTx; private Transaction offererPayoutTx;
private Transaction publishedDepositTx; private Transaction publishedDepositTx;
private byte[] takerPubKey;
/////////////////////////////////////////////////////////////////////////////////////////// ///////////////////////////////////////////////////////////////////////////////////////////
@ -94,10 +94,27 @@ public class BuyerAsOffererModel extends OfferSharedModel {
user); user);
this.openOffer = openOffer; this.openOffer = openOffer;
offererPaybackAddress = walletService.getAddressInfo(offer.getId()).getAddressString(); offererPubKey = getAddressInfo().getPubKey();
} }
//getter/setter //getter/setter
public List<TransactionOutput> getOffererConnectedOutputsForAllInputs() {
return offererConnectedOutputsForAllInputs;
}
public void setOffererConnectedOutputsForAllInputs(List<TransactionOutput> offererConnectedOutputsForAllInputs) {
this.offererConnectedOutputsForAllInputs = offererConnectedOutputsForAllInputs;
}
public List<TransactionOutput> getOffererOutputs() {
return offererOutputs;
}
public void setOffererOutputs(List<TransactionOutput> offererOutputs) {
this.offererOutputs = offererOutputs;
}
public OpenOffer getOpenOffer() { public OpenOffer getOpenOffer() {
return openOffer; return openOffer;
} }
@ -106,10 +123,6 @@ public class BuyerAsOffererModel extends OfferSharedModel {
return taker; return taker;
} }
public String getOffererPaybackAddress() {
return offererPaybackAddress;
}
public String getTakeOfferFeeTxId() { public String getTakeOfferFeeTxId() {
return takeOfferFeeTxId; return takeOfferFeeTxId;
@ -119,22 +132,18 @@ public class BuyerAsOffererModel extends OfferSharedModel {
this.takeOfferFeeTxId = takeOfferFeeTxId; this.takeOfferFeeTxId = takeOfferFeeTxId;
} }
@Override
public String getTakerAccountId() { public String getTakerAccountId() {
return takerAccountId; return takerAccountId;
} }
@Override
public void setTakerAccountId(String takerAccountId) { public void setTakerAccountId(String takerAccountId) {
this.takerAccountId = takerAccountId; this.takerAccountId = takerAccountId;
} }
@Override
public BankAccount getTakerBankAccount() { public BankAccount getTakerBankAccount() {
return takerBankAccount; return takerBankAccount;
} }
@Override
public void setTakerBankAccount(BankAccount takerBankAccount) { public void setTakerBankAccount(BankAccount takerBankAccount) {
this.takerBankAccount = takerBankAccount; this.takerBankAccount = takerBankAccount;
} }
@ -155,16 +164,10 @@ public class BuyerAsOffererModel extends OfferSharedModel {
this.takerContractAsJson = takerContractAsJson; this.takerContractAsJson = takerContractAsJson;
} }
public byte[] getOffererPubKey() { public byte[] getOffererPubKey() {
return offererPubKey; return offererPubKey;
} }
public void setOffererPubKey(byte[] offererPubKey) {
this.offererPubKey = offererPubKey;
}
public ECKey.ECDSASignature getOffererSignature() { public ECKey.ECDSASignature getOffererSignature() {
return offererSignature; return offererSignature;
} }
@ -201,21 +204,6 @@ public class BuyerAsOffererModel extends OfferSharedModel {
this.taker = taker; this.taker = taker;
} }
public List<TransactionOutput> getOffererConnectedOutputsForAllInputs() {
return offererConnectedOutputsForAllInputs;
}
public void setOffererConnectedOutputsForAllInputs(List<TransactionOutput> offererConnectedOutputsForAllInputs) {
this.offererConnectedOutputsForAllInputs = offererConnectedOutputsForAllInputs;
}
public List<TransactionOutput> getOffererOutputs() {
return offererOutputs;
}
public void setOffererOutputs(List<TransactionOutput> offererOutputs) {
this.offererOutputs = offererOutputs;
}
public void setTakerDepositTx(Transaction takerDepositTx) { public void setTakerDepositTx(Transaction takerDepositTx) {
this.takerDepositTx = takerDepositTx; this.takerDepositTx = takerDepositTx;
@ -264,4 +252,12 @@ public class BuyerAsOffererModel extends OfferSharedModel {
public Transaction getPublishedDepositTx() { public Transaction getPublishedDepositTx() {
return publishedDepositTx; return publishedDepositTx;
} }
public byte[] getTakerPubKey() {
return takerPubKey;
}
public void setTakerPubKey(byte[] takerPubKey) {
this.takerPubKey = takerPubKey;
}
} }

View file

@ -35,12 +35,12 @@ public class RequestDepositPayment extends Task<BuyerAsOffererModel> {
@Override @Override
protected void doRun() { protected void doRun() {
byte[] offererPubKey = model.getWalletService().getAddressInfo(model.getTrade().getId()).getPubKey(); model.getOffererPubKey();
RequestDepositPaymentMessage tradeMessage = new RequestDepositPaymentMessage( RequestDepositPaymentMessage tradeMessage = new RequestDepositPaymentMessage(
model.getTrade().getId(), model.getTrade().getId(),
model.getOffererConnectedOutputsForAllInputs(), model.getOffererConnectedOutputsForAllInputs(),
model.getOffererOutputs(), model.getOffererOutputs(),
offererPubKey, model.getOffererPubKey(),
model.getBankAccount(), model.getBankAccount(),
model.getAccountId()); model.getAccountId());

View file

@ -41,7 +41,7 @@ public class SendBankTransferStartedMessage extends Task<BuyerAsOffererModel> {
model.getOffererSignature().encodeToDER(), model.getOffererSignature().encodeToDER(),
model.getOffererPaybackAmount(), model.getOffererPaybackAmount(),
model.getTakerPaybackAmount(), model.getTakerPaybackAmount(),
model.getOffererPaybackAddress()); model.getAddressInfo().getAddressString());
model.getTradeMessageService().sendMessage(model.getTaker(), tradeMessage, new SendMessageListener() { model.getTradeMessageService().sendMessage(model.getTaker(), tradeMessage, new SendMessageListener() {
@Override @Override
public void handleResult() { public void handleResult() {

View file

@ -46,10 +46,10 @@ public class VerifyAndSignContract extends Task<BuyerAsOffererModel> {
model.getTakerAccountId(), model.getTakerAccountId(),
model.getBankAccount(), model.getBankAccount(),
model.getTakerBankAccount(), model.getTakerBankAccount(),
model.getMessagePublicKey(), model.getNetworkPubKey(),
model.getTakerMessagePublicKey()); model.getTakerMessagePublicKey());
String contractAsJson = Utilities.objectToJson(contract); String contractAsJson = Utilities.objectToJson(contract);
String signature = model.getSignatureService().signMessage(model.getAccountKey(), contractAsJson); String signature = model.getSignatureService().signMessage(model.getRegistrationKeyPair(), contractAsJson);
trade.setContract(contract); trade.setContract(contract);
trade.setContractAsJson(contractAsJson); trade.setContractAsJson(contractAsJson);

View file

@ -17,6 +17,7 @@
package io.bitsquare.trade.protocol.trade.taker; package io.bitsquare.trade.protocol.trade.taker;
import io.bitsquare.bank.BankAccount;
import io.bitsquare.btc.BlockChainService; import io.bitsquare.btc.BlockChainService;
import io.bitsquare.btc.WalletService; import io.bitsquare.btc.WalletService;
import io.bitsquare.crypto.SignatureService; import io.bitsquare.crypto.SignatureService;
@ -27,7 +28,6 @@ import io.bitsquare.trade.protocol.trade.OfferSharedModel;
import io.bitsquare.user.User; import io.bitsquare.user.User;
import org.bitcoinj.core.Coin; import org.bitcoinj.core.Coin;
import org.bitcoinj.core.ECKey;
import org.bitcoinj.core.Transaction; import org.bitcoinj.core.Transaction;
import org.bitcoinj.core.TransactionOutput; import org.bitcoinj.core.TransactionOutput;
@ -43,6 +43,9 @@ public class SellerAsTakerModel extends OfferSharedModel {
private final Trade trade; private final Trade trade;
// derived
private final byte[] takerPubKey;
// written/read by task // written/read by task
private Peer offerer; private Peer offerer;
private Transaction preparedDepositTx; private Transaction preparedDepositTx;
@ -53,7 +56,6 @@ public class SellerAsTakerModel extends OfferSharedModel {
private Coin takerPaybackAmount; private Coin takerPaybackAmount;
private byte[] offererPubKey; private byte[] offererPubKey;
private long offererTxOutIndex; private long offererTxOutIndex;
private ECKey.ECDSASignature offererSignature;
private Coin offererPaybackAmount; private Coin offererPaybackAmount;
private String offererPayoutAddress; private String offererPayoutAddress;
private List<TransactionOutput> offererConnectedOutputsForAllInputs; private List<TransactionOutput> offererConnectedOutputsForAllInputs;
@ -62,6 +64,8 @@ public class SellerAsTakerModel extends OfferSharedModel {
private List<TransactionOutput> takerOutputs; private List<TransactionOutput> takerOutputs;
private Transaction takerDepositTx; private Transaction takerDepositTx;
private Transaction publishedDepositTx; private Transaction publishedDepositTx;
private BankAccount takerBankAccount;
private String takerAccountId;
public SellerAsTakerModel(Trade trade, public SellerAsTakerModel(Trade trade,
TradeMessageService tradeMessageService, TradeMessageService tradeMessageService,
@ -77,11 +81,14 @@ public class SellerAsTakerModel extends OfferSharedModel {
user); user);
this.trade = trade; this.trade = trade;
takerPubKey = walletService.getAddressInfo(trade.getId()).getPubKey(); takerPubKey = getAddressInfo().getPubKey();
} }
// getter/setter // getter/setter
public byte[] getTakerPubKey() {
return takerPubKey;
}
public void setOffererPubKey(byte[] offererPubKey) { public void setOffererPubKey(byte[] offererPubKey) {
this.offererPubKey = offererPubKey; this.offererPubKey = offererPubKey;
} }
@ -101,6 +108,7 @@ public class SellerAsTakerModel extends OfferSharedModel {
public void setOffererOutputs(List<TransactionOutput> offererOutputs) { public void setOffererOutputs(List<TransactionOutput> offererOutputs) {
this.offererOutputs = offererOutputs; this.offererOutputs = offererOutputs;
} }
public Trade getTrade() { public Trade getTrade() {
return trade; return trade;
} }
@ -158,14 +166,6 @@ public class SellerAsTakerModel extends OfferSharedModel {
this.depositTx = depositTx; this.depositTx = depositTx;
} }
public ECKey.ECDSASignature getOffererSignature() {
return offererSignature;
}
public void setOffererSignature(ECKey.ECDSASignature offererSignature) {
this.offererSignature = offererSignature;
}
public Coin getOffererPaybackAmount() { public Coin getOffererPaybackAmount() {
return offererPaybackAmount; return offererPaybackAmount;
} }
@ -230,4 +230,20 @@ public class SellerAsTakerModel extends OfferSharedModel {
public Transaction getPublishedDepositTx() { public Transaction getPublishedDepositTx() {
return publishedDepositTx; return publishedDepositTx;
} }
public void setTakerBankAccount(BankAccount takerBankAccount) {
this.takerBankAccount = takerBankAccount;
}
public BankAccount getTakerBankAccount() {
return takerBankAccount;
}
public void setTakerAccountId(String takerAccountId) {
this.takerAccountId = takerAccountId;
}
public String getTakerAccountId() {
return takerAccountId;
}
} }

View file

@ -46,9 +46,9 @@ public class CreateAndSignContract extends Task<SellerAsTakerModel> {
model.getTakerBankAccount(), model.getTakerBankAccount(),
model.getBankAccount(), model.getBankAccount(),
model.getOffer().getMessagePublicKey(), model.getOffer().getMessagePublicKey(),
model.getMessagePublicKey()); model.getNetworkPubKey());
String contractAsJson = Utilities.objectToJson(contract); String contractAsJson = Utilities.objectToJson(contract);
String signature = model.getSignatureService().signMessage(model.getAccountKey(), contractAsJson); String signature = model.getSignatureService().signMessage(model.getRegistrationKeyPair(), contractAsJson);
trade.setContract(contract); trade.setContract(contract);
trade.setContractAsJson(contractAsJson); trade.setContractAsJson(contractAsJson);

View file

@ -22,12 +22,9 @@ import io.bitsquare.trade.protocol.trade.taker.SellerAsTakerModel;
import io.bitsquare.util.taskrunner.Task; import io.bitsquare.util.taskrunner.Task;
import io.bitsquare.util.taskrunner.TaskRunner; import io.bitsquare.util.taskrunner.TaskRunner;
import org.bitcoinj.core.ECKey;
import org.slf4j.Logger; import org.slf4j.Logger;
import org.slf4j.LoggerFactory; import org.slf4j.LoggerFactory;
import static com.google.common.base.Preconditions.checkNotNull;
import static io.bitsquare.util.Validator.*; import static io.bitsquare.util.Validator.*;
public class ProcessBankTransferStartedMessage extends Task<SellerAsTakerModel> { public class ProcessBankTransferStartedMessage extends Task<SellerAsTakerModel> {
@ -44,7 +41,7 @@ public class ProcessBankTransferStartedMessage extends Task<SellerAsTakerModel>
BankTransferStartedMessage message = (BankTransferStartedMessage) model.getTradeMessage(); BankTransferStartedMessage message = (BankTransferStartedMessage) model.getTradeMessage();
//model.setDepositTx(checkNotNull(message.getDepositTx())); //model.setDepositTx(checkNotNull(message.getDepositTx()));
model.setOffererSignature(checkNotNull(ECKey.ECDSASignature.decodeFromDER(message.getOffererSignature()))); //model.setOffererSignature(checkNotNull(ECKey.ECDSASignature.decodeFromDER(message.getOffererSignature())));
model.setOffererPaybackAmount(positiveCoinOf(nonZeroCoinOf(message.getOffererPaybackAmount()))); model.setOffererPaybackAmount(positiveCoinOf(nonZeroCoinOf(message.getOffererPaybackAmount())));
model.setTakerPaybackAmount(positiveCoinOf(nonZeroCoinOf(message.getTakerPaybackAmount()))); model.setTakerPaybackAmount(positiveCoinOf(nonZeroCoinOf(message.getTakerPaybackAmount())));
model.setOffererPayoutAddress(nonEmptyStringOf(message.getOffererPayoutAddress())); model.setOffererPayoutAddress(nonEmptyStringOf(message.getOffererPayoutAddress()));

View file

@ -45,6 +45,7 @@ public class ProcessRequestDepositPaymentMessage extends Task<SellerAsTakerModel
checkArgument(message.getOffererConnectedOutputsForAllInputs().size() > 0); checkArgument(message.getOffererConnectedOutputsForAllInputs().size() > 0);
model.setOffererOutputs(checkNotNull(message.getOffererOutputs())); model.setOffererOutputs(checkNotNull(message.getOffererOutputs()));
model.setOffererPubKey(checkNotNull(message.getOffererPubKey())); model.setOffererPubKey(checkNotNull(message.getOffererPubKey()));
model.setTakerBankAccount(checkNotNull(message.getBankAccount())); model.setTakerBankAccount(checkNotNull(message.getBankAccount()));
model.setTakerAccountId(nonEmptyStringOf(message.getAccountId())); model.setTakerAccountId(nonEmptyStringOf(message.getAccountId()));

View file

@ -39,7 +39,7 @@ public class SendSignedTakerDepositTx extends Task<SellerAsTakerModel> {
model.getTrade().getId(), model.getTrade().getId(),
model.getBankAccount(), model.getBankAccount(),
model.getAccountId(), model.getAccountId(),
model.getMessagePublicKey(), model.getNetworkPubKey(),
model.getTrade().getContractAsJson(), model.getTrade().getContractAsJson(),
model.getTrade().getTakerContractSignature(), model.getTrade().getTakerContractSignature(),
model.getTakerDepositTx(), model.getTakerDepositTx(),

View file

@ -44,7 +44,7 @@ public class SignAndPublishPayoutTx extends Task<SellerAsTakerModel> {
protected void doRun() { protected void doRun() {
try { try {
model.getWalletService().takerSignsAndSendsTx(model.getPublishedDepositTx(), model.getWalletService().takerSignsAndSendsTx(model.getPublishedDepositTx(),
model.getOffererSignature(), null,
model.getOffererPaybackAmount(), model.getOffererPaybackAmount(),
model.getTakerPaybackAmount(), model.getTakerPaybackAmount(),
model.getOffererPayoutAddress(), model.getOffererPayoutAddress(),

View file

@ -43,7 +43,7 @@ import javafx.collections.ObservableList;
public class User implements Serializable { public class User implements Serializable {
private static final long serialVersionUID = 7409078808248518638L; private static final long serialVersionUID = 7409078808248518638L;
private KeyPair messageKeyPair; private KeyPair networkKeyPair;
private String accountID; private String accountID;
// Used for serialisation (ObservableList cannot be serialized) -> serialisation will change anyway so that is // Used for serialisation (ObservableList cannot be serialized) -> serialisation will change anyway so that is
@ -57,8 +57,7 @@ public class User implements Serializable {
public User() { public User() {
// Used for serialisation (ObservableList cannot be serialized) -> serialisation will change anyway so that is // Used for serialisation (ObservableList cannot be serialized) -> serialisation will change anyway so that is
// only temporary // only temporary
bankAccounts.addListener((ListChangeListener<BankAccount>) change -> bankAccounts.addListener((ListChangeListener<BankAccount>) change -> _bankAccounts = new ArrayList<>(bankAccounts));
_bankAccounts = new ArrayList<>(bankAccounts));
currentBankAccount.addListener((ov) -> _currentBankAccount = currentBankAccount.get()); currentBankAccount.addListener((ov) -> _currentBankAccount = currentBankAccount.get());
} }
@ -72,13 +71,13 @@ public class User implements Serializable {
if (persistedUser != null) { if (persistedUser != null) {
bankAccounts.setAll(persistedUser.getSerializedBankAccounts()); bankAccounts.setAll(persistedUser.getSerializedBankAccounts());
setCurrentBankAccount(persistedUser.getSerializedCurrentBankAccount()); setCurrentBankAccount(persistedUser.getSerializedCurrentBankAccount());
messageKeyPair = persistedUser.getMessageKeyPair(); networkKeyPair = persistedUser.getNetworkKeyPair();
accountID = persistedUser.getAccountId(); accountID = persistedUser.getAccountId();
} }
else { else {
// First time // First time
// TODO use separate thread. DSAKeyUtil.getKeyPair() runs in same thread now // TODO use separate thread. DSAKeyUtil.getKeyPair() runs in same thread now
messageKeyPair = DSAKeyUtil.generateKeyPair(); networkKeyPair = DSAKeyUtil.generateKeyPair();
} }
} }
@ -153,7 +152,6 @@ public class User implements Serializable {
} }
public BankAccount getBankAccount(String bankAccountId) { public BankAccount getBankAccount(String bankAccountId) {
// TODO use steam API
for (final BankAccount bankAccount : bankAccounts) { for (final BankAccount bankAccount : bankAccounts) {
if (bankAccount.getUid().equals(bankAccountId)) { if (bankAccount.getUid().equals(bankAccountId)) {
return bankAccount; return bankAccount;
@ -162,24 +160,19 @@ public class User implements Serializable {
return null; return null;
} }
public KeyPair getMessageKeyPair() { public KeyPair getNetworkKeyPair() {
return messageKeyPair; return networkKeyPair;
} }
public PublicKey getMessagePublicKey() { public PublicKey getNetworkPubKey() {
return messageKeyPair.getPublic(); return networkKeyPair.getPublic();
}
public String getMessagePublicKeyAsString() {
return DSAKeyUtil.getHexStringFromPublicKey(getMessagePublicKey());
} }
public ObjectProperty<BankAccount> currentBankAccountProperty() { public ObjectProperty<BankAccount> currentBankAccountProperty() {
return currentBankAccount; return currentBankAccount;
} }
// Used for serialisation (ObservableList cannot be serialized) -> serialisation will change anyway so that is // Used for serialisation (ObservableList cannot be serialized)
// only temporary
List<BankAccount> getSerializedBankAccounts() { List<BankAccount> getSerializedBankAccounts() {
return _bankAccounts; return _bankAccounts;
} }

View file

@ -98,7 +98,7 @@ public class PlaceOfferProtocolTest {
tomP2PNode = new TomP2PNode(bootstrappedPeerBuilder); tomP2PNode = new TomP2PNode(bootstrappedPeerBuilder);
tradeMessageService = new TomP2PTradeMessageService(user, tomP2PNode); tradeMessageService = new TomP2PTradeMessageService(user, tomP2PNode);
Observable<BootstrapState> messageObservable = tomP2PNode.bootstrap(user.getMessageKeyPair(), tradeMessageService); Observable<BootstrapState> messageObservable = tomP2PNode.bootstrap(user.getNetworkKeyPair(), tradeMessageService);
messageObservable.publish(); messageObservable.publish();
messageObservable.subscribe( messageObservable.subscribe(
state -> log.trace("state changed: " + state), state -> log.trace("state changed: " + state),