Skip to content

Commit 0ed8b92

Browse files
committed
better check for compressed keys
1 parent 5291830 commit 0ed8b92

File tree

1 file changed

+3
-20
lines changed
  • public/mbw/src/main/java/com/mycelium/wallet

1 file changed

+3
-20
lines changed

public/mbw/src/main/java/com/mycelium/wallet/Utils.java

Lines changed: 3 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -887,30 +887,13 @@ public static String getFileContent(File textfile) throws IOException {
887887
public static List<Record> getPrivKeysFromBitcoinJProtobufBackup(InputStream inStream, NetworkParameters network) throws IOException {
888888
List<Record> returnList = new ArrayList<Record>();
889889
Protos.Wallet wallet = Protos.Wallet.parseFrom(inStream);
890-
891890
for (Protos.Key k : wallet.getKeyList()) {
892891
byte[] pubKeyBytes = k.getPublicKey().toByteArray();
893892
byte[] privKeyBytes = k.getPrivateKey().toByteArray();
894-
895-
// How does bitcoinj differentiate between compressed and uncompressed keys?
896-
// This may not be the best method at all, but we simply try to create first compressed
897-
// and then uncompressed keys and check if the resulting public key matches.
898-
// Is there a better way to do this?
899-
900-
// first try compressed key
901-
InMemoryPrivateKey compressedKey = new InMemoryPrivateKey(privKeyBytes, true);
902-
if (Arrays.equals(compressedKey.getPublicKey().getPublicKeyBytes(), pubKeyBytes)) {
903-
returnList.add(Record.fromString(compressedKey.getBase58EncodedPrivateKey(network), network));
904-
continue;
905-
}
906-
907-
// if the pubkic key did not match then try uncompressed key
908-
InMemoryPrivateKey uncompressedKey = new InMemoryPrivateKey(privKeyBytes, false);
909-
if (Arrays.equals(uncompressedKey.getPublicKey().getPublicKeyBytes(), pubKeyBytes)) {
910-
returnList.add(Record.fromString(uncompressedKey.getBase58EncodedPrivateKey(network), network));
911-
}
893+
boolean keyIsCompressed = (pubKeyBytes.length == 33);
894+
InMemoryPrivateKey inMemoryKey = new InMemoryPrivateKey(privKeyBytes, keyIsCompressed);
895+
returnList.add(Record.fromString(inMemoryKey.getBase58EncodedPrivateKey(network), network));
912896
}
913-
914897
return returnList;
915898
}
916899

0 commit comments

Comments
 (0)