Permalink
Browse files

Correcting address sorting bug

  • Loading branch information...
1 parent a6c47cb commit 70cdd0d740236ee6ed2ca8bafb78fcf987ea83bc @fireduck64 committed Jul 11, 2011
Showing with 79 additions and 4 deletions.
  1. +2 −2 build.xml
  2. BIN lib/junit-4.8.2.jar
  3. +50 −2 src/BitcoinTimestamp.java
  4. +27 −0 test/AddressConversionTest.java
View
@@ -7,7 +7,7 @@
<property name="test" value="./test" />
<property name="build_test" value="./build_test" />
<property name="lib" value="./lib" />
- <property name="version" value="1.0.0" />
+ <property name="version" value="1.0.1" />
<path id="test.classpath">
<pathelement location="${build}" />
@@ -60,7 +60,7 @@
<attribute name="Main-Class" value="fireduck64.bitcoin.timestamp.BitcoinTimestamp"/>
</manifest>
</jar>
- <jar destfile="${jar}/BitcointTimestamp-${version}-lib.jar" basedir="build">
+ <jar destfile="${jar}/BitcoinTimestamp-${version}-lib.jar" basedir="build">
</jar>
</target>
View
Binary file not shown.
@@ -112,6 +112,18 @@ else if (mode.equals("decode"))
public static String getSendmanyCommand(String hash, TreeSet<String> addresses) throws Exception
{
+ TreeMap<String, String> hash160_to_address=new TreeMap<String, String>();
+ TreeSet<String> hash160_set = new TreeSet<String>();
+
+ //We have to sort by the same thing we are going to sort by on the other side
+ //the hash160 value for the key is a good one
+ for(String address : addresses)
+ {
+ String hash160 = getHash160FromAddress(address);
+ hash160_set.add(hash160);
+ hash160_to_address.put(hash160, address);
+
+ }
String token = hash;
BigInteger bi = new BigInteger(token, 16);
@@ -137,14 +149,15 @@ public static String getSendmanyCommand(String hash, TreeSet<String> addresses)
sb.append(" fromacct");
sb.append(" '{");
- Iterator<String> address_i = addresses.iterator();
+ Iterator<String> hash_i = hash160_set.iterator();
boolean first=true;
for(BigInteger amt : amount_values)
{
- String address = address_i.next();
+ String hash160 = hash_i.next();
+ String address = hash160_to_address.get(hash160);
int val = amt.intValue();
double d = btc_factor * val;
@@ -314,5 +327,40 @@ public static JSONObject readUrl(String url)
}
+ public static String getHash160FromAddress(String address)
+ {
+ String base58="123456789ABCDEFGHJKLMNPQRSTUVWXYZabcdefghijkmnopqrstuvwxyz";
+ TreeMap<Character, Integer> radix_value_map = new TreeMap<Character, Integer>();
+
+ for(int i=0; i<base58.length(); i++)
+ {
+ radix_value_map.put(base58.charAt(i), i);
+ }
+
+
+
+ BigInteger bi = BigInteger.ZERO;
+ BigInteger base = new BigInteger("58");
+
+ for(int i=0; i<address.length(); i++)
+ {
+ char x = address.charAt(i);
+ int val = radix_value_map.get(x);
+ bi = bi.multiply(base);
+ bi = bi.add(BigInteger.valueOf(val));
+
+ }
+
+ String hex= bi.toString(16);
+ while(hex.length() < 48)
+ {
+ hex = "0" + hex;
+ }
+
+ String hash160=hex.substring(0, 40);
+
+ return hash160;
+
+ }
}
@@ -0,0 +1,27 @@
+
+
+import org.junit.Assert;
+import org.junit.Test;
+
+import fireduck64.bitcoin.timestamp.BitcoinTimestamp;
+
+public class AddressConversionTest
+{
+ @Test
+ public void testAddressToHash160()
+ {
+
+ //Values from blockexplorer
+ //
+ Assert.assertEquals("0903a4e4cd1902a5064adecea790217c8cba3bde",
+ BitcoinTimestamp.getHash160FromAddress("1pfSvbhbjGdSabQ6fYm4dbKEFDuJW9xZw"));
+
+ Assert.assertEquals("00d1cd2e5ec984226c98f85a69852ee9e6c659bd",
+ BitcoinTimestamp.getHash160FromAddress("115LLGFjCeuR43kqPut3VgQaijykg4ZUvu"));
+
+ Assert.assertEquals("ebfac9d836423078b592d13a84ff3dad58602fe9",
+ BitcoinTimestamp.getHash160FromAddress("1NWkEAiWPLtakyh7DXSF9G8GN3ZzeaRNPr"));
+ }
+
+
+}

0 comments on commit 70cdd0d

Please sign in to comment.