private WalletPocketHD testWalletSerializationForCoin(WalletPocketHD account) throws UnreadableWalletException { Protos.WalletPocket proto = account.toProtobuf(); WalletPocketHD newAccount = new WalletPocketProtobufSerializer().readWallet(proto, null); assertEquals(account.getBalance().value, newAccount.getBalance().value); Map<Sha256Hash, BitTransaction> transactions = account.getTransactions(); Map<Sha256Hash, BitTransaction> newTransactions = newAccount.getTransactions(); for (Sha256Hash txId : transactions.keySet()) { assertTrue(newTransactions.containsKey(txId)); assertEquals(transactions.get(txId), newTransactions.get(txId)); } return newAccount; }
private TransactionOutPoint getTransactionOutPoint(CoinType type, int index, ByteString hash) { return new TransactionOutPoint(type, index & 0xFFFFFFFFL, byteStringToHash(hash)); }
private static Protos.Transaction makeTxProto(WalletTransaction wtx) { if (wtx instanceof BitWalletTransaction) { return makeBitTxProto((BitWalletTransaction) wtx); } else { throw new RuntimeException("Unknown wallet transaction type: " + wtx.getClass().getName()); } }
readTransaction(txProto, coinType); wtxs.add(connectTransactionOutputs(txProto)); proto.getValue(), byteStringToHash(proto.getOutPointHash()), proto.getOutPointIndex() & 0xFFFFFFFFL, proto.getScriptBytes().toByteArray()); pocket.setLastBlockSeenHash(null); } else { pocket.setLastBlockSeenHash(byteStringToHash(walletProto.getLastSeenBlockHash()));
Protos.Transaction txProto = makeTxProto(wtx); walletBuilder.addTransaction(txProto); Protos.UnspentOutput utxoProto = makeUTXOProto(utxo); walletBuilder.addUnspentOutput(utxoProto); walletBuilder.setLastSeenBlockHash(hashToByteString(pocket.getLastBlockSeenHash()));
private void readTransaction(Protos.Transaction txProto, CoinType params) throws UnreadableWalletException { boolean isTrimmed = txProto.getIsTrimmed(); Sha256Hash hash = byteStringToHash(txProto.getHash()); TransactionOutput output = getOutput(params, tx, outputProto.getValue(), outputProto.getScriptBytes()); TransactionOutPoint outpoint = getTransactionOutPoint(params, inputProto.getTransactionOutPointIndex(), inputProto.getTransactionOutPointHash() if (txProto.getBlockRelativityOffsetsCount() > 0) relativityOffset = txProto.getBlockRelativityOffsets(i); tx.addBlockAppearance(byteStringToHash(blockHash), relativityOffset); final Value valueReceived = type.value(txProto.getValueReceived()); final Value fee = txProto.hasFee() ? type.value(txProto.getFee()) : null; bitTx = BitTransaction.fromTrimmed(byteStringToHash(txProto.getHash()), tx, valueSent, valueReceived, fee); } else { throw new UnreadableWalletException(String.format("Transaction did not deserialize completely: %s vs %s", tx.getHash(), hash)); if (txMap.containsKey(txProto.getHash())) throw new UnreadableWalletException("Wallet contained duplicate transaction " + byteStringToHash(txProto.getHash()));
Protos.Transaction.Builder txBuilder = Protos.Transaction.newBuilder(); txBuilder.setPool(getProtoPool(wtx)) .setHash(hashToByteString(bitTx.getHash())) .setVersion((int) tx.getVersion()); Protos.TransactionInput.Builder inputBuilder = Protos.TransactionInput.newBuilder() .setScriptBytes(ByteString.copyFrom(input.getScriptBytes())) .setTransactionOutPointHash(hashToByteString(input.getOutpoint().getHash())) .setTransactionOutPointIndex((int) input.getOutpoint().getIndex()); if (input.hasSequence()) outputBuilder.setSpentByTransactionHash(hashToByteString(spendingHash)) .setSpentByTransactionIndex(spentByTransactionIndex); if (appearsInHashes != null) { for (Map.Entry<Sha256Hash, Integer> entry : appearsInHashes.entrySet()) { txBuilder.addBlockHash(hashToByteString(entry.getKey())); txBuilder.addBlockRelativityOffsets(entry.getValue()); TransactionConfidence confidence = tx.getConfidence(); Protos.TransactionConfidence.Builder confidenceBuilder = Protos.TransactionConfidence.newBuilder(); writeConfidence(txBuilder, confidence, confidenceBuilder);
if (spendingTx == null || spendingTx.isTrimmed()) { throw new UnreadableWalletException(String.format("Could not connect %s to %s", tx.getHashAsString(), byteStringToHash(spentByTransactionHash))); Transaction rawTx = tx.getRawTransaction(); TransactionConfidence confidence = rawTx.getConfidence(); readConfidence(rawTx, confidenceProto, confidence);
@Test public void xpubWalletSerialized() throws Exception { WalletPocketHD account = new WalletPocketHD(rootKey, BTC, null, null); Protos.WalletPocket proto = account.toProtobuf(); WalletPocketHD newAccount = new WalletPocketProtobufSerializer().readWallet(proto, null); assertEquals(account.getPublicKeySerialized(), newAccount.getPublicKeySerialized()); }
@Test public void serializeUnencryptedEmpty() throws Exception { pocket.maybeInitializeAllKeys(); Protos.WalletPocket walletPocketProto = pocket.toProtobuf(); WalletPocketHD newPocket = new WalletPocketProtobufSerializer().readWallet(walletPocketProto, null); assertEquals(walletPocketProto.toString(), newPocket.toProtobuf().toString()); // Issued keys assertEquals(0, newPocket.keys.getNumIssuedExternalKeys()); assertEquals(0, newPocket.keys.getNumIssuedInternalKeys()); // 20 lookahead + 20 lookahead assertEquals(40, newPocket.keys.getActiveKeys().size()); }
WalletPocketProtobufSerializer pocketSerializer = new WalletPocketProtobufSerializer(); NxtFamilyWalletProtobufSerializer nxtPocketSerializer = new NxtFamilyWalletProtobufSerializer(); for (Protos.WalletPocket pocketProto : walletProto.getPocketsList()) { pocket = pocketSerializer.readWallet(pocketProto, crypter); } else if (type instanceof NxtFamily) { pocket = nxtPocketSerializer.readWallet(pocketProto, crypter);
@Test public void serializeEncryptedEmpty() throws Exception { pocket.maybeInitializeAllKeys(); pocket.encrypt(crypter, aesKey); Protos.WalletPocket walletPocketProto = pocket.toProtobuf(); WalletPocketHD newPocket = new WalletPocketProtobufSerializer().readWallet(walletPocketProto, crypter); assertEquals(walletPocketProto.toString(), newPocket.toProtobuf().toString()); pocket.decrypt(aesKey); // One is encrypted, so they should not match assertNotEquals(pocket.toProtobuf().toString(), newPocket.toProtobuf().toString()); newPocket.decrypt(aesKey); assertEquals(pocket.toProtobuf().toString(), newPocket.toProtobuf().toString()); }
@Test public void serializeEncryptedNormal() throws Exception { pocket.maybeInitializeAllKeys(); pocket.encrypt(crypter, aesKey); pocket.onConnection(getBlockchainConnection(DOGE)); assertEquals(DOGE.value(11000000000l), pocket.getBalance()); assertAllKeysEncrypted(pocket); WalletPocketHD newPocket = new WalletPocketProtobufSerializer().readWallet(pocket.toProtobuf(), crypter); assertEquals(DOGE.value(11000000000l), newPocket.getBalance()); Map<TrimmedOutPoint, OutPointOutput> expectedUtxoSet = getDummyUtxoSet(); checkUnspentOutputs(expectedUtxoSet, pocket); checkUnspentOutputs(expectedUtxoSet, newPocket); assertAllKeysEncrypted(newPocket); pocket.decrypt(aesKey); newPocket.decrypt(aesKey); assertAllKeysDecrypted(pocket); assertAllKeysDecrypted(newPocket); }
System.out.println(walletPocketProto.toString()); WalletPocketHD newPocket = new WalletPocketProtobufSerializer().readWallet(walletPocketProto, null);