/
AddressUtilsTest.java
90 lines (76 loc) · 3.57 KB
/
AddressUtilsTest.java
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
package io.neow3j.utils;
import static junit.framework.TestCase.fail;
import static org.hamcrest.core.Is.is;
import static org.junit.Assert.assertArrayEquals;
import static org.junit.Assert.assertFalse;
import static org.junit.Assert.assertThat;
import static org.junit.Assert.assertTrue;
import io.neow3j.constants.NeoConstants;
import io.neow3j.crypto.Hash;
import org.junit.Test;
public class AddressUtilsTest {
@Test
public void scriptHashToAddress() {
String script =
"2102208aea0068c429a03316e37be0e3e8e21e6cda5442df4c5914a19b3a9b6de37568747476aa";
byte[] scriptHash = Hash.sha256AndThenRipemd160(Numeric.hexStringToByteArray(script));
String address = AddressUtils.scriptHashToAddress(scriptHash);
String expectedAddress = "Aa63RMYRWHPRcrZNzUnq5SNrPqoV866Spu";
assertThat(address, is(expectedAddress));
}
@Test
public void scriptHashToAddressWithAddressVersion() {
String script =
"21030529d1296dc2af1f77d8344138a77748599b69599af7ae6be57812a4ec3fa33968747476aa";
byte[] scriptHash = Hash.sha256AndThenRipemd160(Numeric.hexStringToByteArray(script));
System.out.println(Numeric.toHexString(scriptHash));
String address =
AddressUtils.scriptHashToAddress(scriptHash, NeoConstants.PRIVATENET_ADDRESS_VERSION);
String expectedAddress = "PRivaTenetyWuqK7Gj7Vd747d77ssYeDhL";
assertThat(address, is(expectedAddress));
}
@Test
public void addressToScriptHash() {
byte[] scriptHash = AddressUtils.addressToScriptHash("Aa63RMYRWHPRcrZNzUnq5SNrPqoV866Spu");
String script =
"2102208aea0068c429a03316e37be0e3e8e21e6cda5442df4c5914a19b3a9b6de37568747476aa";
byte[] expected = Hash.sha256AndThenRipemd160(Numeric.hexStringToByteArray(script));
assertArrayEquals(scriptHash, expected);
}
@Test(expected = IllegalArgumentException.class)
public void testToScriptHashLargerThan25Chars() {
AddressUtils.addressToScriptHash("AK2nJJpJr6o664CWJKi1QRXjqeic2zRp8yyyy");
}
@Test
public void testScriptHashToAddress() {
byte[] scriptHash = Numeric.hexStringToByteArray("d336d7eb9975a29b2404fdb28185e277a4b299bc");
String address = "Ab2fvZdmnM4HwDgVbdBrbTLz1wK5TcEyhU";
assertThat(AddressUtils.scriptHashToAddress(scriptHash), is(address));
}
@Test
public void testScriptHashToAddressWithVersion() {
byte[] scriptHash = Numeric.hexStringToByteArray("bbf14c0d508108e56402116aede9942a064f7dc6");
String address = "PRivaTenetyWuqK7Gj7Vd747d77ssYeDhL";
assertThat(AddressUtils.scriptHashToAddress(scriptHash, NeoConstants.PRIVATENET_ADDRESS_VERSION), is(address));
}
@Test
public void testIsValidAddress() {
assertTrue(AddressUtils.isValidAddress("Ab2fvZdmnM4HwDgVbdBrbTLz1wK5TcEyhU"));
assertTrue(AddressUtils.isValidAddress("AK2nJJpJr6o664CWJKi1QRXjqeic2zRp8y"));
assertFalse(AddressUtils.isValidAddress(""));
assertFalse(AddressUtils.isValidAddress("0"));
assertFalse(AddressUtils.isValidAddress("b2fvZdmnM4HwDgVbdBrbTLz1wK5TcEyhU"));
assertFalse(AddressUtils.isValidAddress("AAb2fvZdmnM4HwDgVbdBrbTLz1wK5TcEyhU"));
// If the address string is null, we don't want to say it is an invalid address because
// there isn't even an address to be deemed invalid. Therefore expect NullPointerException.
try {
AddressUtils.isValidAddress(null);
} catch (NullPointerException e) {
return;
}
fail();
}
@Test
public void test() {
}
}