From 73a5d95bea8a6230e7b5f6db320739961ba42648 Mon Sep 17 00:00:00 2001 From: Mike Hearn Date: Wed, 11 Apr 2012 23:08:39 +0200 Subject: [PATCH] Introduce additional comment and some minor reformatting / simplification of the protobuf serializer. --- .../bitcoin/store/WalletProtobufSerializer.java | 13 ++++++++----- 1 file changed, 8 insertions(+), 5 deletions(-) diff --git a/core/src/main/java/com/google/bitcoin/store/WalletProtobufSerializer.java b/core/src/main/java/com/google/bitcoin/store/WalletProtobufSerializer.java index 5be119e3..7fcff4c3 100644 --- a/core/src/main/java/com/google/bitcoin/store/WalletProtobufSerializer.java +++ b/core/src/main/java/com/google/bitcoin/store/WalletProtobufSerializer.java @@ -104,6 +104,9 @@ public class WalletProtobufSerializer { .setType(Protos.Key.Type.ORIGINAL); if (key.getPrivKeyBytes() != null) buf.setPrivateKey(ByteString.copyFrom(key.getPrivKeyBytes())); + // We serialize the public key even if the private key is present for speed reasons: we don't want to do + // lots of slow EC math to load the wallet, we prefer to store the redundant data instead. It matters more + // on mobile platforms. buf.setPublicKey(ByteString.copyFrom(key.getPubKey())); walletBuilder.addKey(buf); } @@ -115,7 +118,7 @@ public class WalletProtobufSerializer { Protos.Transaction.Builder txBuilder = Protos.Transaction.newBuilder(); txBuilder.setPool(Protos.Transaction.Pool.valueOf(wtx.getPool().getValue())) - .setHash(ByteString.copyFrom(tx.getHash().getBytes())) + .setHash(hashToByteString(tx.getHash())) .setVersion((int) tx.getVersion()); if (tx.getUpdateTime() != null) { @@ -130,8 +133,8 @@ public class WalletProtobufSerializer { for (TransactionInput input : tx.getInputs()) { Protos.TransactionInput.Builder inputBuilder = Protos.TransactionInput.newBuilder() .setScriptBytes(ByteString.copyFrom(input.getScriptBytes())) - .setTransactionOutPointHash(ByteString.copyFrom(input.getOutpoint().getHash().getBytes())) - .setTransactionOutPointIndex((int)input.getOutpoint().getIndex()); // FIXME + .setTransactionOutPointHash(hashToByteString(input.getOutpoint().getHash())) + .setTransactionOutPointIndex((int) input.getOutpoint().getIndex()); if (input.hasSequence()) { inputBuilder.setSequence((int)input.getSequence()); } @@ -146,9 +149,9 @@ public class WalletProtobufSerializer { final TransactionInput spentBy = output.getSpentBy(); if (spentBy != null) { Sha256Hash spendingHash = spentBy.getParentTransaction().getHash(); + int spentByTransactionIndex = spentBy.getParentTransaction().getInputs().indexOf(spentBy); outputBuilder.setSpentByTransactionHash(hashToByteString(spendingHash)) - .setSpentByTransactionIndex( - spentBy.getParentTransaction().getInputs().indexOf(spentBy)); + .setSpentByTransactionIndex(spentByTransactionIndex); } txBuilder.addTransactionOutput(outputBuilder); }