Skip to content

Commit

Permalink
update from v1 to v2 wallet format
Browse files Browse the repository at this point in the history
  • Loading branch information
erasmospunk committed Mar 9, 2016
1 parent 63f7c61 commit cb1052d
Show file tree
Hide file tree
Showing 4 changed files with 83 additions and 6 deletions.
2 changes: 1 addition & 1 deletion core/src/main/java/com/coinomi/core/coins/DashMain.java
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@
*/
public class DashMain extends BitFamily {
private DashMain() {
id = "darkcoin.main"; // Do not change this id as wallets serialize this string
id = "dash.main"; // Do not change this id as wallets serialize this string

addressHeader = 76;
p2shHeader = 16;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@
*/
public class DogecoindarkMain extends PeerFamily {
private DogecoindarkMain() {
id = "dogecoindark.main";
id = "verge.main";

addressHeader = 30;
p2shHeader = 33;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -101,7 +101,7 @@ abstract public class TransactionWatcherWallet extends AbstractWallet<BitTransac
@VisibleForTesting final Map<Sha256Hash, BitTransaction> confirmed;

// All transactions together.
private final Map<Sha256Hash, BitTransaction> rawTransactions;
final Map<Sha256Hash, BitTransaction> rawTransactions;
private BitBlockchainConnection blockchainConnection;
private List<ListenerRegistration<WalletAccountEventListener>> listeners;

Expand Down Expand Up @@ -235,6 +235,15 @@ public void addWalletTransaction(BitWalletTransaction wtx) {
}
}

boolean trimTransactionIfNeeded(Sha256Hash hash) {
lock.lock();
try {
return trimTransaction(hash);
} finally {
lock.unlock();
}
}

/**
* Remove irrelevant inputs and outputs. Returns true if transaction trimmed.
*/
Expand Down Expand Up @@ -628,8 +637,7 @@ private boolean isAddressStatusChanged(AddressStatus addressStatus) {
return true;
}
}
}
finally {
} finally {
lock.unlock();
}
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,16 +2,24 @@

import com.coinomi.core.coins.CoinID;
import com.coinomi.core.coins.CoinType;
import com.coinomi.core.coins.DashMain;
import com.coinomi.core.coins.DogecoindarkMain;
import com.coinomi.core.coins.families.BitFamily;
import com.coinomi.core.coins.families.NxtFamily;
import com.coinomi.core.network.ServerClient;
import com.coinomi.core.protos.Protos;
import com.coinomi.core.util.KeyUtils;
import com.coinomi.core.wallet.families.bitcoin.BitTransaction;
import com.coinomi.core.wallet.families.bitcoin.OutPointOutput;
import com.coinomi.core.wallet.families.bitcoin.TrimmedOutPoint;
import com.coinomi.core.wallet.families.nxt.NxtFamilyWallet;
import com.coinomi.core.wallet.families.nxt.NxtFamilyWalletProtobufSerializer;
import com.google.common.base.Splitter;
import com.google.protobuf.ByteString;
import com.google.protobuf.TextFormat;

import org.bitcoinj.core.Sha256Hash;
import org.bitcoinj.core.TransactionOutput;
import org.bitcoinj.crypto.ChildNumber;
import org.bitcoinj.crypto.DeterministicKey;
import org.bitcoinj.crypto.EncryptedData;
Expand All @@ -24,9 +32,13 @@
import java.io.IOException;
import java.io.InputStream;
import java.io.OutputStream;
import java.util.HashSet;
import java.util.List;
import java.util.Set;

import static com.coinomi.core.Preconditions.checkNotNull;
import static com.google.common.base.Preconditions.checkState;
import static org.bitcoinj.core.TransactionConfidence.ConfidenceType.BUILDING;

/**
* @author John L. Jegutanis
Expand Down Expand Up @@ -163,9 +175,13 @@ public static Wallet readWallet(InputStream input) throws UnreadableWalletExcept
* @throws UnreadableWalletException thrown in various error conditions (see description).
*/
public static Wallet readWallet(Protos.Wallet walletProto) throws UnreadableWalletException {
if (walletProto.getVersion() > 1)
if (walletProto.getVersion() > 2)
throw new UnreadableWalletException.FutureVersion();

if (walletProto.getVersion() < 2) {
walletProto = updateV1toV2Proto(walletProto);
}

// Check if wallet is encrypted
final KeyCrypter crypter = getKeyCrypter(walletProto);

Expand Down Expand Up @@ -211,6 +227,10 @@ public static Wallet readWallet(Protos.Wallet walletProto) throws UnreadableWall
wallet.addAccount(pocket);
}

if (wallet.getVersion() < 2) {
updateV1toV2(wallet);
}

return wallet;
}

Expand Down Expand Up @@ -262,4 +282,53 @@ else if (walletProto.getEncryptionType() == Protos.Wallet.EncryptionType.UNENCRY
public static Protos.Wallet parseToProto(InputStream input) throws IOException {
return Protos.Wallet.parseFrom(input);
}

private static Protos.Wallet updateV1toV2Proto(Protos.Wallet walletProto) {
checkState(walletProto.getVersion() < 2, "Can update only from version < 2");
Protos.Wallet.Builder b = walletProto.toBuilder();
for (int i = 0; i < b.getPocketsCount(); i++) {
Protos.WalletPocket.Builder account = b.getPocketsBuilder(i);
if (account.getNetworkIdentifier().equals("dogecoindark.main")) {
account.setNetworkIdentifier(DogecoindarkMain.get().getId());
b.setPockets(i, account);
}
if (account.getNetworkIdentifier().equals("darkcoin.main")) {
account.setNetworkIdentifier(DashMain.get().getId());
b.setPockets(i, account);
}
}
return b.build();
}

private static void updateV1toV2(Wallet wallet) {
checkState(wallet.getVersion() < 2, "Can update only from version < 2");
wallet.setVersion(2);
for (WalletAccount walletAccount : wallet.getAllAccounts()) {
if (walletAccount instanceof WalletPocketHD) {
WalletPocketHD account = (WalletPocketHD) walletAccount;
// Force resync
account.addressesStatus.clear();
// Gather hashes to trim them later
Set<Sha256Hash> txHashes = new HashSet<>(account.rawTransactions.size());
// Reconstruct UTXO set
for (BitTransaction tx : account.rawTransactions.values()) {
txHashes.add(tx.getHash());
for (TransactionOutput txo : tx.getOutputs()) {
if (txo.isAvailableForSpending() && txo.isMineOrWatched(account)) {
OutPointOutput utxo = new OutPointOutput(tx, txo.getIndex());
if (tx.getConfidenceType() == BUILDING) {
utxo.setAppearedAtChainHeight(tx.getAppearedAtChainHeight());
utxo.setDepthInBlocks(tx.getDepthInBlocks());
}
account.unspentOutputs.put(utxo.getOutPoint(), utxo);
}
}
}
// Trim transactions
for (Sha256Hash txHash : txHashes) {
account.trimTransactionIfNeeded(txHash);
}
}
}
}
}

0 comments on commit cb1052d

Please sign in to comment.