Skip to content

Commit

Permalink
fix assertions
Browse files Browse the repository at this point in the history
  • Loading branch information
SamouraiDev committed Jul 23, 2017
1 parent 5a042c6 commit c718851
Show file tree
Hide file tree
Showing 3 changed files with 39 additions and 5 deletions.
2 changes: 1 addition & 1 deletion ref/java/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,7 @@ Bech32-jar-with-dependencies.jar : includes all dependencies and can be run from

### Run using Main.java harness:

java -jar target/Bech32-jar-with-dependencies.jar
java -jar -ea target/Bech32-jar-with-dependencies.jar

### Dev contact:

Expand Down
40 changes: 37 additions & 3 deletions ref/java/src/main/java/org/bech32/Main.java
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,8 @@ public class Main {
new String[] { "BC1SW50QA3JX3S", "9002751e"},
new String[] { "bc1zw508d6qejxtdg4y5r3zarvaryvg6kdaj", "8210751e76e8199196d454941c45d1b3a323"},
new String[] { "tb1qqqqqp399et2xygdj5xreqhjjvcmzhxw4aywxecjdzew6hylgvsesrxh6hy", "0020000000c4a5cad46221b2a187905e5266362b99d5e91c6ce24d165dab93e86433"},
// BIP49 test vector
new String[] { "tb1q8zt37uunpakpg8vh0tz06jnj0jz5jddn5mlts3", "001438971f73930f6c141d977ac4fd4a727c854935b3"},
};

// test vectors
Expand Down Expand Up @@ -77,15 +79,18 @@ public static void main(String[] args) {
for(String s : INVALID_ADDRESS) {

p = null;
Pair<Byte, byte[]> pair = null;

try {
p = Bech32.bech32Decode(s);
pair = SegwitAddress.decode(new String(p.getLeft()), s);
}
catch(Exception e) {
;
}

assert(p == null);
assert(p == null || pair == null);

}

System.out.println("valid segwit address test");
Expand All @@ -106,13 +111,13 @@ public static void main(String[] args) {
assert(Hex.encodeHexString(pubkey).equalsIgnoreCase(s[1]));

String address = SegwitAddress.encode(hrp.getBytes(), witVer, witProg);
assert(s[0].equals(address));
assert(s[0].equalsIgnoreCase(address));

int idx = s[0].lastIndexOf("1");
Pair<Byte, byte[]> testPair = null;
try{
testPair = SegwitAddress.decode(hrp, s[0].substring(0, idx + 1) + new String(new char[] { (char)(s[0].charAt(idx + 1) ^ 1) }) + s[0].substring(idx + 2));
// assert(!Arrays.equals(witProg, testPair.getRight()));
assert(!Arrays.equals(witProg, testPair.getRight()));
}
catch(Exception e) {
;
Expand Down Expand Up @@ -149,6 +154,35 @@ public static void main(String[] args) {
catch(Exception e) {
e.printStackTrace();
}

System.out.println("encode BIP49 test vector");
try {

Hex hex = new Hex();

String address = SegwitAddress.encode("tb".getBytes(), (byte)0x00, hex.decode("38971f73930f6c141d977ac4fd4a727c854935b3".getBytes()));
System.out.println("BIP49 test vector:" + address);

byte witVer;
String hrp = new String(Bech32.bech32Decode(address).getLeft());

byte[] witProg;
Pair<Byte, byte[]> segp = null;
segp = SegwitAddress.decode(hrp, address);
witVer = segp.getLeft();
witProg = segp.getRight();
System.out.println("decoded witVer:" + witVer);
System.out.println("decoded witProg:" + Hex.encodeHexString(witProg));

assert(!(witVer < 0 || witVer > 16));

byte[] pubkey = SegwitAddress.getScriptPubkey(witVer, witProg);
System.out.println("decoded pubkey:" + Hex.encodeHexString(pubkey));
}
catch(Exception e) {
System.out.println("Error:" + e.getMessage());
}

}

}
2 changes: 1 addition & 1 deletion ref/java/src/main/java/org/bech32/SegwitAddress.java
Original file line number Diff line number Diff line change
Expand Up @@ -50,7 +50,7 @@ public static String encode(byte[] hrp, byte witnessVersion, byte[] witnessProgr

String ret = Bech32.bech32Encode(hrp, data);

assert(Arrays.equals(data, decode(new String(hrp), ret).getRight()));
// assert(Arrays.equals(data, Bech32.bech32Decode(ret).getRight()));

return ret;
}
Expand Down

0 comments on commit c718851

Please sign in to comment.