Remove param form AddressEntry, small refactorings...

This commit is contained in:
Manfred Karrer 2017-04-13 20:02:15 -05:00
parent 05af4dd708
commit 4c38e367b4
8 changed files with 40 additions and 54 deletions

View File

@ -1,11 +1,12 @@
package io.bisq.common.storage;
import java.io.Serializable;
import io.bisq.common.persistance.Persistable;
/**
* Used to wrap a plaintext string to distinguish at file storage and safe it as plain text instead of a serialized java object.
*/
public class PlainTextWrapper implements Serializable {
// We would not need Persistable but as it is used in Storage and Storage expects a Persistable as type we keep it...
public class PlainTextWrapper implements Persistable {
// That object is not saved to disc it is only of type Serializable to support the persistent framework.
// SerialVersionUID has no relevance here.
private static final long serialVersionUID = 0;

View File

@ -67,7 +67,6 @@ public class Storage<T extends Persistable> {
private T serializable;
private String fileName;
private int numMaxBackupFiles = 10;
@com.google.inject.Inject
private ProtobufferResolver protobufferResolver;

View File

@ -780,8 +780,7 @@ message AddressEntry {
Context context = 8;
bytes pub_key = 9;
bytes pub_key_hash = 10;
string param_id = 11;
Coin coin_locked_in_multi_sig = 12;
Coin coin_locked_in_multi_sig = 11;
}
message DeterministicKey {

View File

@ -22,17 +22,14 @@ import com.google.protobuf.Message;
import io.bisq.common.app.Version;
import io.bisq.common.persistance.Persistable;
import io.bisq.common.util.Utilities;
import io.bisq.core.btc.wallet.WalletUtils;
import io.bisq.generated.protobuffer.PB;
import lombok.EqualsAndHashCode;
import lombok.Getter;
import lombok.extern.slf4j.Slf4j;
import org.bitcoinj.core.Address;
import org.bitcoinj.core.Coin;
import org.bitcoinj.core.NetworkParameters;
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;
@ -73,8 +70,6 @@ public final class AddressEntry implements Persistable {
private final byte[] pubKey;
@Getter
private final byte[] pubKeyHash;
@Getter
private final String paramId;
@Nullable
@Getter
private Coin coinLockedInMultiSig;
@ -82,27 +77,26 @@ public final class AddressEntry implements Persistable {
@Nullable
@Getter
transient private DeterministicKey keyPair;
@Getter
transient private NetworkParameters params;
@Nullable
transient private Address address;
@Nullable
transient private String addressString;
///////////////////////////////////////////////////////////////////////////////////////////
// Constructor, initialization
///////////////////////////////////////////////////////////////////////////////////////////
public AddressEntry(DeterministicKey keyPair, NetworkParameters params, Context context) {
this(keyPair, params, context, null);
public AddressEntry(DeterministicKey keyPair, Context context) {
this(keyPair, context, null);
}
public AddressEntry(@NotNull DeterministicKey keyPair,
NetworkParameters params,
Context context,
@Nullable String offerId) {
this.keyPair = keyPair;
this.params = params;
this.context = context;
this.offerId = offerId;
paramId = params.getId();
pubKey = keyPair.getPubKey();
pubKeyHash = keyPair.getPubKeyHash();
}
@ -110,23 +104,14 @@ public final class AddressEntry implements Persistable {
// called from Resolver
public AddressEntry(byte[] pubKey,
byte[] pubKeyHash,
String paramId,
Context context,
@Nullable String offerId,
@Nullable Coin coinLockedInMultiSig) {
this.pubKey = pubKey;
this.pubKeyHash = pubKeyHash;
this.paramId = paramId;
this.context = context;
this.offerId = offerId;
this.coinLockedInMultiSig = coinLockedInMultiSig;
if (MainNetParams.ID_MAINNET.equals(paramId))
params = MainNetParams.get();
else if (MainNetParams.ID_TESTNET.equals(paramId))
params = TestNet3Params.get();
else if (MainNetParams.ID_REGTEST.equals(paramId))
params = RegTestParams.get();
}
// Set after wallet is ready
@ -160,12 +145,16 @@ public final class AddressEntry implements Persistable {
@Nullable
public String getAddressString() {
return getAddress() != null ? getAddress().toString() : null;
if (addressString == null && getAddress() != null)
addressString = getAddress().toString();
return addressString;
}
@Nullable
public Address getAddress() {
return keyPair != null ? keyPair.toAddress(params) : null;
if (address == null && keyPair != null)
address = keyPair.toAddress(WalletUtils.getParameters());
return address;
}
public boolean isOpenOffer() {
@ -199,8 +188,7 @@ public final class AddressEntry implements Persistable {
PB.AddressEntry.Builder builder = PB.AddressEntry.newBuilder()
.setContext(PB.AddressEntry.Context.valueOf(context.name()))
.setPubKey(ByteString.copyFrom(pubKey))
.setPubKeyHash(ByteString.copyFrom(pubKeyHash))
.setParamId(paramId);
.setPubKeyHash(ByteString.copyFrom(pubKeyHash));
Optional.ofNullable(offerId).ifPresent(builder::setOfferId);
Optional.ofNullable(coinLockedInMultiSig).ifPresent(coinLockedInMultiSig -> {
builder.setCoinLockedInMultiSig(PB.Coin.newBuilder().setValue(coinLockedInMultiSig.getValue()));

View File

@ -32,8 +32,8 @@ import org.bitcoinj.crypto.DeterministicKey;
import java.util.ArrayList;
import java.util.List;
import java.util.Optional;
import java.util.stream.Collectors;
import java.util.stream.Stream;
/**
* The List supporting our persistence solution.
@ -47,7 +47,7 @@ public final class AddressEntryList implements Persistable {
final transient private Storage<AddressEntryList> storage;
transient private Wallet wallet;
@Getter
private List<AddressEntry> addressEntryList = new ArrayList<>();
private List<AddressEntry> list = new ArrayList<>();
@Setter
private boolean doPersist;
@ -61,7 +61,7 @@ public final class AddressEntryList implements Persistable {
AddressEntryList persisted = storage.initAndGetPersisted(this);
if (persisted != null) {
for (AddressEntry addressEntry : persisted.getAddressEntryList()) {
for (AddressEntry addressEntry : persisted.getList()) {
DeterministicKey keyFromPubHash = (DeterministicKey) wallet.findKeyFromPubHash(addressEntry.getPubKeyHash());
if (keyFromPubHash != null) {
addressEntry.setDeterministicKey(keyFromPubHash);
@ -71,17 +71,17 @@ public final class AddressEntryList implements Persistable {
}
} else {
doPersist = true;
add(new AddressEntry(wallet.freshReceiveKey(), wallet.getParams(), AddressEntry.Context.ARBITRATOR));
add(new AddressEntry(wallet.freshReceiveKey(), AddressEntry.Context.ARBITRATOR));
persist();
}
}
private boolean add(AddressEntry addressEntry) {
return addressEntryList.add(addressEntry);
return list.add(addressEntry);
}
private boolean remove(AddressEntry addressEntry) {
return addressEntryList.remove(addressEntry);
return list.remove(addressEntry);
}
public AddressEntry addAddressEntry(AddressEntry addressEntry) {
@ -91,27 +91,28 @@ public final class AddressEntryList implements Persistable {
return addressEntry;
}
public void swapTradeToSavings(String offerId) {
Optional<AddressEntry> addressEntryOptional = addressEntryList.stream().filter(addressEntry -> offerId.equals(addressEntry.getOfferId())).findAny();
if (addressEntryOptional.isPresent()) {
AddressEntry addressEntry = addressEntryOptional.get();
boolean changed1 = add(new AddressEntry(addressEntry.getKeyPair(), wallet.getParams(), AddressEntry.Context.AVAILABLE));
list.stream().filter(addressEntry -> offerId.equals(addressEntry.getOfferId()))
.findAny().ifPresent(addressEntry -> {
boolean changed1 = add(new AddressEntry(addressEntry.getKeyPair(), AddressEntry.Context.AVAILABLE));
boolean changed2 = remove(addressEntry);
if (changed1 || changed2)
persist();
}
});
}
public void swapToAvailable(AddressEntry addressEntry) {
remove(addressEntry);
boolean changed1 = add(new AddressEntry(addressEntry.getKeyPair(), wallet.getParams(), AddressEntry.Context.AVAILABLE));
boolean changed1 = add(new AddressEntry(addressEntry.getKeyPair(), AddressEntry.Context.AVAILABLE));
boolean changed2 = remove(addressEntry);
if (changed1 || changed2)
persist();
}
public Stream<AddressEntry> stream() {
return list.stream();
}
public void persist() {
if (doPersist)
storage.queueUpForSave(50);
@ -120,7 +121,7 @@ public final class AddressEntryList implements Persistable {
@Override
public Message toProtobuf() {
final PB.DiskEnvelope build = PB.DiskEnvelope.newBuilder().setAddressEntryList(PB.AddressEntryList.newBuilder()
.addAllAddressEntry(getAddressEntryList().stream()
.addAllAddressEntry(stream()
.map(addressEntry -> ((PB.AddressEntry) addressEntry.toProtobuf()))
.collect(Collectors.toList())))
.build();

View File

@ -80,7 +80,7 @@ public class BtcWalletService extends WalletService {
void decryptWallet(@NotNull KeyParameter key) {
super.decryptWallet(key);
addressEntryList.getAddressEntryList().stream().forEach(e -> {
addressEntryList.stream().forEach(e -> {
final DeterministicKey keyPair = e.getKeyPair();
if (keyPair.isEncrypted())
e.setDeterministicKey(keyPair.decrypt(key));
@ -92,7 +92,7 @@ public class BtcWalletService extends WalletService {
void encryptWallet(KeyCrypterScrypt keyCrypterScrypt, KeyParameter key) {
super.encryptWallet(keyCrypterScrypt, key);
addressEntryList.getAddressEntryList().stream().forEach(e -> {
addressEntryList.stream().forEach(e -> {
final DeterministicKey keyPair = e.getKeyPair();
if (keyPair.isEncrypted())
e.setDeterministicKey(keyPair.encrypt(keyCrypterScrypt, key));
@ -291,7 +291,7 @@ public class BtcWalletService extends WalletService {
return addressEntry.get();
} else {
AddressEntry entry = addressEntryList.addAddressEntry(new AddressEntry(wallet.freshReceiveKey(),
wallet.getParams(), context, offerId));
context, offerId));
saveAddressEntryList();
return entry;
}
@ -316,8 +316,7 @@ public class BtcWalletService extends WalletService {
if (addressEntry.isPresent()) {
return addressEntry.get();
} else {
AddressEntry entry = addressEntryList.addAddressEntry(new AddressEntry(wallet.freshReceiveKey(),
wallet.getParams(), context));
AddressEntry entry = addressEntryList.addAddressEntry(new AddressEntry(wallet.freshReceiveKey(), context));
saveAddressEntryList();
return entry;
}
@ -349,7 +348,7 @@ public class BtcWalletService extends WalletService {
}
private List<AddressEntry> getAddressEntryListAsImmutableList() {
return ImmutableList.copyOf(addressEntryList.getAddressEntryList());
return ImmutableList.copyOf(addressEntryList.getList());
}
public void swapTradeEntryToAvailableEntry(String offerId, AddressEntry.Context context) {

View File

@ -507,7 +507,7 @@ public class WalletsSetup {
}
public Set<Address> getAddressesByContext(AddressEntry.Context context) {
return ImmutableList.copyOf(addressEntryList.getAddressEntryList()).stream()
return ImmutableList.copyOf(addressEntryList.getList()).stream()
.filter(addressEntry -> addressEntry.getContext() == context)
.map(AddressEntry::getAddress)
.collect(Collectors.toSet());

View File

@ -990,7 +990,6 @@ public class CoreProtobufferResolver implements ProtobufferResolver {
envelope.getAddressEntryList().getAddressEntryList().stream().forEach(addressEntry -> {
final AddressEntry entry = new AddressEntry(addressEntry.getPubKey().toByteArray(),
addressEntry.getPubKeyHash().toByteArray(),
addressEntry.getParamId(),
AddressEntry.Context.valueOf(addressEntry.getContext().name()),
addressEntry.getOfferId(),
Coin.valueOf(addressEntry.getCoinLockedInMultiSig().getValue()));