From f2a6e41c82678ea3ce4879000badd16a705516ea Mon Sep 17 00:00:00 2001 From: Mike Hearn Date: Tue, 8 Jan 2013 17:33:19 +0100 Subject: [PATCH] Don't abuse BigIntegers in WalletTool/Utils when parsing hex pubkeys. --- core/src/main/java/com/google/bitcoin/core/Utils.java | 9 ++++----- .../main/java/com/google/bitcoin/tools/WalletTool.java | 7 +++---- 2 files changed, 7 insertions(+), 9 deletions(-) diff --git a/core/src/main/java/com/google/bitcoin/core/Utils.java b/core/src/main/java/com/google/bitcoin/core/Utils.java index dffb199c..6e295d62 100644 --- a/core/src/main/java/com/google/bitcoin/core/Utils.java +++ b/core/src/main/java/com/google/bitcoin/core/Utils.java @@ -17,6 +17,7 @@ package com.google.bitcoin.core; import org.spongycastle.crypto.digests.RIPEMD160Digest; +import org.spongycastle.util.encoders.Hex; import java.io.IOException; import java.io.OutputStream; @@ -434,19 +435,17 @@ public class Utils { * Attempts to parse the given string as arbitrary-length hex or base58 and then return the results, or null if * neither parse was successful. */ - public static BigInteger parseAsHexOrBase58(String data) { - BigInteger decode; + public static byte[] parseAsHexOrBase58(String data) { try { - decode = new BigInteger(data, 16); + return Hex.decode(data); } catch (Exception e) { // Didn't decode as hex, try base58. try { - decode = new BigInteger(1, Base58.decodeChecked(data)); + return Base58.decodeChecked(data); } catch (AddressFormatException e1) { return null; } } - return decode; } public static boolean isWindows() { diff --git a/tools/src/main/java/com/google/bitcoin/tools/WalletTool.java b/tools/src/main/java/com/google/bitcoin/tools/WalletTool.java index 8374824d..dedc6370 100644 --- a/tools/src/main/java/com/google/bitcoin/tools/WalletTool.java +++ b/tools/src/main/java/com/google/bitcoin/tools/WalletTool.java @@ -606,20 +606,19 @@ public class WalletTool { } if (options.has("privkey")) { String data = (String) options.valueOf("privkey"); - BigInteger decode = Utils.parseAsHexOrBase58(data); + byte[] decode = Utils.parseAsHexOrBase58(data); if (decode == null) { System.err.println("Could not understand --privkey as either hex or base58: " + data); return; } - key = new ECKey(decode); + key = new ECKey(new BigInteger(1, decode)); if (options.has("pubkey")) { // Give the user a hint. System.out.println("You don't have to specify --pubkey when a private key is supplied."); } key.setCreationTimeSeconds(creationTimeSeconds); } else if (options.has("pubkey")) { - BigInteger decode = Utils.parseAsHexOrBase58((String) options.valueOf("pubkey")); - byte[] pubkey = Utils.bigIntegerToBytes(decode, ECKey.PUBLIC_KEY_LENGTH); + byte[] pubkey = Utils.parseAsHexOrBase58((String) options.valueOf("pubkey")); key = new ECKey(null, pubkey); key.setCreationTimeSeconds(creationTimeSeconds); } else {