Skip to content

Commit 8611baf

Browse files
committed
All tests green
- Final set of tests were ported. Bugs fixed where exposed.
1 parent 112aeca commit 8611baf

File tree

7 files changed

+61
-61
lines changed

7 files changed

+61
-61
lines changed

src/main/java/org/twostack/bitcoin/Address.java

+5-4
Original file line numberDiff line numberDiff line change
@@ -21,6 +21,7 @@
2121
import org.twostack.bitcoin.exception.AddressFormatException;
2222
import org.twostack.bitcoin.params.NetworkAddressType;
2323
import org.twostack.bitcoin.params.NetworkParameters;
24+
import org.twostack.bitcoin.params.NetworkType;
2425
import org.twostack.bitcoin.script.Script.ScriptType;
2526

2627
import javax.annotation.Nullable;
@@ -31,7 +32,7 @@
3132
* </p>
3233
*
3334
* <p>
34-
* Use {@link #fromString(NetworkAddressType, String)} to conveniently construct any kind of address from its textual
35+
* Use {@link #fromString(networkType, String)} to conveniently construct any kind of address from its textual
3536
* form.
3637
* </p>
3738
*/
@@ -47,7 +48,7 @@ public Address(NetworkAddressType networkAddressType, byte[] bytes) {
4748
/**
4849
* Construct an address from its textual form.
4950
*
50-
* @param addressType
51+
* @param networkType
5152
* the expected network this address is valid for, or null if the network should be derived from the
5253
* textual form
5354
* @param str
@@ -59,8 +60,8 @@ public Address(NetworkAddressType networkAddressType, byte[] bytes) {
5960
* @throws AddressFormatException.WrongNetwork
6061
* if the given string is valid but not for the expected network (eg testnet vs mainnet)
6162
*/
62-
public static Address fromString(@Nullable NetworkAddressType addressType, String str) throws AddressFormatException {
63-
return LegacyAddress.fromBase58(addressType, str);
63+
public static Address fromString(@Nullable NetworkType networkType, String str) throws AddressFormatException {
64+
return LegacyAddress.fromBase58(networkType, str);
6465
}
6566

6667
/**

src/main/java/org/twostack/bitcoin/address/LegacyAddress.java

+30-9
Original file line numberDiff line numberDiff line change
@@ -23,8 +23,10 @@
2323
import org.twostack.bitcoin.ECKey;
2424
import org.twostack.bitcoin.PublicKey;
2525
import org.twostack.bitcoin.exception.AddressFormatException;
26+
import org.twostack.bitcoin.params.AddressType;
2627
import org.twostack.bitcoin.params.NetworkAddressType;
2728
import org.twostack.bitcoin.params.NetworkParameters;
29+
import org.twostack.bitcoin.params.NetworkType;
2830
import org.twostack.bitcoin.script.Script.ScriptType;
2931

3032
import javax.annotation.Nullable;
@@ -52,8 +54,8 @@ public class LegacyAddress extends Address {
5254

5355

5456
/**
55-
* Private constructor. Use {@link #fromBase58(NetworkAddressType, String)},
56-
* {@link #fromPubKeyHash(NetworkAddressType, byte[])}, {@link #fromScriptHash(NetworkAddressType, byte[])} or
57+
* Private constructor. Use {@link #fromBase58(NetworkType, String)},
58+
* {@link #fromPubKeyHash(NetworkAddressType, byte[])}, {@link #fromScriptHash(NetworkType, byte[])} or
5759
* {@link #fromKey(NetworkAddressType, PublicKey)}.
5860
*
5961
* @param addressType
@@ -102,20 +104,26 @@ public static LegacyAddress fromKey(NetworkAddressType networkAddressType, Publi
102104
/**
103105
* Construct a {@link LegacyAddress} that represents the given P2SH script hash.
104106
*
105-
* @param networkAddressType
107+
* @param networkType
106108
* network this address is valid for
107109
* @param hash160
108110
* P2SH script hash
109111
* @return constructed address
110112
*/
111-
public static LegacyAddress fromScriptHash(NetworkAddressType networkAddressType, byte[] hash160) throws AddressFormatException {
112-
return new LegacyAddress(networkAddressType, true, hash160);
113+
public static LegacyAddress fromScriptHash(NetworkType networkType, byte[] hash160) throws AddressFormatException {
114+
115+
if (networkType == NetworkType.MAIN){
116+
return new LegacyAddress(NetworkAddressType.MAIN_P2SH, true, hash160);
117+
}else{
118+
return new LegacyAddress(NetworkAddressType.TEST_P2SH, true, hash160);
119+
}
120+
113121
}
114122

115123
/**
116124
* Construct a {@link LegacyAddress} from its base58 form.
117125
*
118-
* @param networkAddressType
126+
* @param networkType
119127
* expected network this address is valid for, or null if if the network should be derived from the
120128
* base58
121129
* @param base58
@@ -125,17 +133,30 @@ public static LegacyAddress fromScriptHash(NetworkAddressType networkAddressType
125133
* @throws AddressFormatException.WrongNetwork
126134
* if the given address is valid but for a different chain (eg testnet vs mainnet)
127135
*/
128-
public static LegacyAddress fromBase58(@Nullable NetworkAddressType networkAddressType, String base58)
136+
public static LegacyAddress fromBase58(@Nullable NetworkType networkType, String base58)
129137
throws AddressFormatException {
130138
byte[] versionAndDataBytes = Base58.decodeChecked(base58);
131139
int version = versionAndDataBytes[0] & 0xFF;
132140
byte[] bytes = Arrays.copyOfRange(versionAndDataBytes, 1, versionAndDataBytes.length);
133141

134-
if (networkAddressType == null) {
142+
if (networkType == null) {
135143
NetworkAddressType derivedType = NetworkParameters.getNetworkAddressType(version);
136144
return new LegacyAddress(derivedType, false, bytes);
137145
} else {
138-
return new LegacyAddress(networkAddressType, true, bytes);
146+
147+
AddressType versionType = NetworkParameters.getAddressType(version);
148+
NetworkAddressType versionAddressType = NetworkParameters.getNetworkAddressType(version);
149+
150+
if (! NetworkParameters.getNetworkTypes(version).contains(networkType))
151+
throw new AddressFormatException.WrongNetwork(version);
152+
153+
if (versionType == AddressType.PUBKEY_HASH){
154+
return new LegacyAddress(versionAddressType, false, bytes);
155+
}else if(versionType == AddressType.SCRIPT_HASH){
156+
return new LegacyAddress(versionAddressType, true, bytes);
157+
}
158+
159+
throw new AddressFormatException.WrongNetwork(version);
139160
}
140161

141162
}

src/main/java/org/twostack/bitcoin/crypto/DumpedPrivateKey.java

+1-1
Original file line numberDiff line numberDiff line change
@@ -54,7 +54,7 @@ public static DumpedPrivateKey fromBase58(@Nullable NetworkType networkType, Str
5454
byte[] bytes = Arrays.copyOfRange(versionAndDataBytes, 1, versionAndDataBytes.length);
5555
if (networkType == null) {
5656
for (NetworkType n : NetworkType.values())
57-
if (version == NetworkParameters.getDumpedPrivateKeyHeader(networkType))
57+
if (version == NetworkParameters.getDumpedPrivateKeyHeader(n))
5858
return new DumpedPrivateKey(n, bytes);
5959
throw new AddressFormatException.InvalidPrefix("No network found for version " + version);
6060
} else {

src/main/java/org/twostack/bitcoin/params/NetworkParameters.java

-1
Original file line numberDiff line numberDiff line change
@@ -49,7 +49,6 @@ public static int getDumpedPrivateKeyHeader(NetworkType networkType){
4949
}
5050
}
5151

52-
//FIXME: These headers are used for serializing. We don't have TESTNET serialization a.t.m
5352
/** Returns the 4 byte header for BIP32 wallet P2PKH - private key part. */
5453
public static int getBip32HeaderP2PKHpriv(NetworkType networkType) {
5554
switch (networkType) {

src/test/java/org/twostack/bitcoin/ECKeyTest.java

+1-1
Original file line numberDiff line numberDiff line change
@@ -227,7 +227,7 @@ public void verifyMessage() throws Exception {
227227
// Test vector generated by Bitcoin-Qt.
228228
String message = "hello";
229229
String sigBase64 = "HxNZdo6ggZ41hd3mM3gfJRqOQPZYcO8z8qdX2BwmpbF11CaOQV+QiZGGQxaYOncKoNW61oRuSMMF8udfK54XqI8=";
230-
Address expectedAddress = LegacyAddress.fromBase58(NetworkAddressType.MAIN_PKH, "14YPSNPi6NSXnUxtPAsyJSuw3pv7AU3Cag");
230+
Address expectedAddress = LegacyAddress.fromBase58(NetworkType.MAIN, "14YPSNPi6NSXnUxtPAsyJSuw3pv7AU3Cag");
231231
ECKey key = ECKey.signedMessageToKey(message, sigBase64);
232232
Address gotAddress = LegacyAddress.fromKey(NetworkAddressType.MAIN_PKH, PublicKey.fromBytes(key.getPubKey()));
233233
assertEquals(expectedAddress, gotAddress);

src/test/java/org/twostack/bitcoin/LegacyAddressTest.java

+20-42
Original file line numberDiff line numberDiff line change
@@ -85,44 +85,23 @@ public void stringification() {
8585

8686
@Test
8787
public void decoding() {
88-
LegacyAddress a = LegacyAddress.fromBase58(TESTNET, "n4eA2nbYqErp7H6jebchxAN59DmNpksexv");
88+
LegacyAddress a = LegacyAddress.fromBase58(NetworkType.TEST, "n4eA2nbYqErp7H6jebchxAN59DmNpksexv");
8989
assertEquals("fda79a24e50ff70ff42f7d89585da5bd19d9e5cc", Utils.HEX.encode(a.getHash()));
9090

91-
LegacyAddress b = LegacyAddress.fromBase58(MAINNET, "17kzeh4N8g49GFvdDzSf8PjaPfyoD1MndL");
91+
LegacyAddress b = LegacyAddress.fromBase58(NetworkType.MAIN, "17kzeh4N8g49GFvdDzSf8PjaPfyoD1MndL");
9292
assertEquals("4a22c3c4cbb31e4d03b15550636762bda0baf85a", Utils.HEX.encode(b.getHash()));
9393
}
9494

9595
@Test
9696
public void errorPaths() {
9797
// Check what happens if we try and decode garbage.
98-
try {
99-
LegacyAddress.fromBase58(TESTNET, "this is not a valid address!");
100-
fail();
101-
} catch (AddressFormatException.WrongNetwork e) {
102-
fail();
103-
} catch (AddressFormatException e) {
104-
// Success.
105-
}
98+
assertThrows(AddressFormatException.class, () -> LegacyAddress.fromBase58(NetworkType.TEST, "this is not a valid address!"));
10699

107100
// Check the empty case.
108-
try {
109-
LegacyAddress.fromBase58(TESTNET, "");
110-
fail();
111-
} catch (AddressFormatException.WrongNetwork e) {
112-
fail();
113-
} catch (AddressFormatException e) {
114-
// Success.
115-
}
101+
assertThrows(AddressFormatException.class, () -> LegacyAddress.fromBase58(NetworkType.TEST, ""));
116102

117103
// Check the case of a mismatched network.
118-
try {
119-
LegacyAddress.fromBase58(TESTNET, "17kzeh4N8g49GFvdDzSf8PjaPfyoD1MndL");
120-
fail();
121-
} catch (AddressFormatException.WrongNetwork e) {
122-
// Success.
123-
} catch (AddressFormatException e) {
124-
fail();
125-
}
104+
assertThrows(AddressFormatException.WrongNetwork.class, () -> LegacyAddress.fromBase58(NetworkType.TEST, "17kzeh4N8g49GFvdDzSf8PjaPfyoD1MndL") );
126105
}
127106

128107
@Test
@@ -136,10 +115,10 @@ public void getNetwork() {
136115
@Test
137116
public void p2shAddress() {
138117
// Test that we can construct P2SH addresses
139-
LegacyAddress mainNetP2SHAddress = LegacyAddress.fromBase58(NetworkAddressType.MAIN_P2SH, "35b9vsyH1KoFT5a5KtrKusaCcPLkiSo1tU");
118+
LegacyAddress mainNetP2SHAddress = LegacyAddress.fromBase58(NetworkType.MAIN , "35b9vsyH1KoFT5a5KtrKusaCcPLkiSo1tU");
140119
assertEquals(mainNetP2SHAddress.getVersion(), NetworkParameters.getNetworkVersion(NetworkAddressType.MAIN_P2SH));
141120
assertEquals(Script.ScriptType.P2SH, mainNetP2SHAddress.getOutputScriptType());
142-
LegacyAddress testNetP2SHAddress = LegacyAddress.fromBase58(NetworkAddressType.TEST_P2SH, "2MuVSxtfivPKJe93EC1Tb9UhJtGhsoWEHCe");
121+
LegacyAddress testNetP2SHAddress = LegacyAddress.fromBase58(NetworkType.TEST , "2MuVSxtfivPKJe93EC1Tb9UhJtGhsoWEHCe");
143122
assertEquals(testNetP2SHAddress.getVersion(), NetworkParameters.getNetworkVersion(NetworkAddressType.TEST_P2SH));
144123
assertEquals(Script.ScriptType.P2SH, testNetP2SHAddress.getOutputScriptType());
145124

@@ -151,15 +130,14 @@ public void p2shAddress() {
151130

152131
// Test that we can convert them from hashes
153132
byte[] hex = HEX.decode("2ac4b0b501117cc8119c5797b519538d4942e90e");
154-
LegacyAddress a = LegacyAddress.fromScriptHash(MAINNET, hex);
133+
LegacyAddress a = LegacyAddress.fromScriptHash(NetworkType.MAIN, hex);
155134
assertEquals("35b9vsyH1KoFT5a5KtrKusaCcPLkiSo1tU", a.toString());
156-
LegacyAddress b = LegacyAddress.fromScriptHash(TESTNET, HEX.decode("18a0e827269b5211eb51a4af1b2fa69333efa722"));
135+
LegacyAddress b = LegacyAddress.fromScriptHash(NetworkType.TEST, HEX.decode("18a0e827269b5211eb51a4af1b2fa69333efa722"));
157136
assertEquals("2MuVSxtfivPKJe93EC1Tb9UhJtGhsoWEHCe", b.toString());
158-
LegacyAddress c = LegacyAddress.fromScriptHash(MAINNET,
159-
ScriptPattern.extractHashFromP2SH(ScriptBuilder.createP2SHOutputScript(hex)));
137+
LegacyAddress c = LegacyAddress.fromScriptHash(NetworkType.MAIN, ScriptPattern.extractHashFromP2SH(ScriptBuilder.createP2SHOutputScript(hex)));
160138
assertEquals("35b9vsyH1KoFT5a5KtrKusaCcPLkiSo1tU", c.toString());
161139
}
162-
//
140+
163141
// @Test
164142
// public void p2shAddressCreationFromKeys() {
165143
// // import some keys from this example: https://gist.github.com/gavinandresen/3966071
@@ -193,7 +171,7 @@ public void roundtripBase58() {
193171

194172
@Test
195173
public void comparisonCloneEqualTo() throws Exception {
196-
LegacyAddress a = LegacyAddress.fromBase58(MAINNET, "1Dorian4RoXcnBv9hnQ4Y2C1an6NJ4UrjX");
174+
LegacyAddress a = LegacyAddress.fromBase58(NetworkType.MAIN, "1Dorian4RoXcnBv9hnQ4Y2C1an6NJ4UrjX");
197175
LegacyAddress b = a.clone();
198176

199177
int result = a.compareTo(b);
@@ -202,17 +180,17 @@ public void comparisonCloneEqualTo() throws Exception {
202180

203181
@Test
204182
public void comparisonLessThan() {
205-
LegacyAddress a = LegacyAddress.fromBase58(MAINNET, "1Dorian4RoXcnBv9hnQ4Y2C1an6NJ4UrjX");
206-
LegacyAddress b = LegacyAddress.fromBase58(MAINNET, "1EXoDusjGwvnjZUyKkxZ4UHEf77z6A5S4P");
183+
LegacyAddress a = LegacyAddress.fromBase58( NetworkType.MAIN , "1Dorian4RoXcnBv9hnQ4Y2C1an6NJ4UrjX");
184+
LegacyAddress b = LegacyAddress.fromBase58(NetworkType.MAIN , "1EXoDusjGwvnjZUyKkxZ4UHEf77z6A5S4P");
207185

208186
int result = a.compareTo(b);
209187
assertTrue(result < 0);
210188
}
211189

212190
@Test
213191
public void comparisonGreaterThan() {
214-
LegacyAddress a = LegacyAddress.fromBase58(MAINNET, "1EXoDusjGwvnjZUyKkxZ4UHEf77z6A5S4P");
215-
LegacyAddress b = LegacyAddress.fromBase58(MAINNET, "1Dorian4RoXcnBv9hnQ4Y2C1an6NJ4UrjX");
192+
LegacyAddress a = LegacyAddress.fromBase58(NetworkType.MAIN , "1EXoDusjGwvnjZUyKkxZ4UHEf77z6A5S4P");
193+
LegacyAddress b = LegacyAddress.fromBase58(NetworkType.MAIN , "1Dorian4RoXcnBv9hnQ4Y2C1an6NJ4UrjX");
216194

217195
int result = a.compareTo(b);
218196
assertTrue(result > 0);
@@ -221,8 +199,8 @@ public void comparisonGreaterThan() {
221199
@Test
222200
public void comparisonNotEquals() {
223201
// These addresses only differ by version byte
224-
LegacyAddress a = LegacyAddress.fromBase58(MAINNET, "14wivxvNTv9THhewPotsooizZawaWbEKE2");
225-
LegacyAddress b = LegacyAddress.fromBase58(MAINNET, "35djrWQp1pTqNsMNWuZUES5vi7EJ74m9Eh");
202+
LegacyAddress a = LegacyAddress.fromBase58(NetworkType.MAIN, "14wivxvNTv9THhewPotsooizZawaWbEKE2");
203+
LegacyAddress b = LegacyAddress.fromBase58(NetworkType.MAIN, "35djrWQp1pTqNsMNWuZUES5vi7EJ74m9Eh");
226204

227205
int result = a.compareTo(b);
228206
assertTrue(result != 0);
@@ -234,8 +212,8 @@ public void comparisonBytesVsString() throws Exception {
234212
String line;
235213
while ((line = dataSetReader.readLine()) != null) {
236214
String addr[] = line.split(",");
237-
LegacyAddress first = LegacyAddress.fromBase58(MAINNET, addr[0]);
238-
LegacyAddress second = LegacyAddress.fromBase58(MAINNET, addr[1]);
215+
LegacyAddress first = LegacyAddress.fromBase58(NetworkType.MAIN, addr[0]);
216+
LegacyAddress second = LegacyAddress.fromBase58(NetworkType.MAIN, addr[1]);
239217
assertTrue(first.compareTo(second) < 0);
240218
assertTrue(first.toString().compareTo(second.toString()) < 0);
241219
}

src/test/java/org/twostack/bitcoin/transaction/TransactionTest.java

+4-3
Original file line numberDiff line numberDiff line change
@@ -33,6 +33,7 @@
3333
import org.twostack.bitcoin.address.LegacyAddress;
3434
import org.twostack.bitcoin.exception.*;
3535
import org.twostack.bitcoin.params.NetworkAddressType;
36+
import org.twostack.bitcoin.params.NetworkType;
3637
import org.twostack.bitcoin.script.Interpreter;
3738
import org.twostack.bitcoin.script.Script;
3839

@@ -71,8 +72,8 @@ public void TestSerializeDeserialize() throws IOException {
7172
public void can_create_and_sign_transaction() throws InvalidKeyException, TransactionException, IOException, SigHashException {
7273

7374
PrivateKey privateKey = PrivateKey.fromWIF("cVVvUsNHhbrgd7aW3gnuGo2qJM45LhHhTCVXrDSJDDcNGE6qmyCs");
74-
Address changeAddress = Address.fromString(NetworkAddressType.TEST_PKH, "mu4DpTaD75nheE4z5CQazqm1ivej1vzL4L"); // my address
75-
Address recipientAddress = Address.fromString(NetworkAddressType.TEST_PKH, "n3aZKucfWmXeXhX13MREQQnqNfbrWiYKtg"); //bitcoin-cli address
75+
Address changeAddress = Address.fromString(NetworkType.TEST, "mu4DpTaD75nheE4z5CQazqm1ivej1vzL4L"); // my address
76+
Address recipientAddress = Address.fromString(NetworkType.TEST, "n3aZKucfWmXeXhX13MREQQnqNfbrWiYKtg"); //bitcoin-cli address
7677

7778
//Create a Transaction instance from the RAW transaction data create by bitcoin-cli.
7879
//this transaction contains the UTXO we are interested in
@@ -144,7 +145,7 @@ public void test_transaction_serialization_vectors() throws IOException, Transac
144145
String toAddress = txOutNode.get(0).textValue();
145146
int spendAmount = txOutNode.get(1).asInt();
146147

147-
builder.spendTo(new P2PKHLockBuilder(LegacyAddress.fromString(NetworkAddressType.MAIN_PKH, toAddress)) , BigInteger.valueOf(spendAmount));
148+
builder.spendTo(new P2PKHLockBuilder(LegacyAddress.fromString(NetworkType.TEST, toAddress)) , BigInteger.valueOf(spendAmount));
148149
}
149150

150151
//signature

0 commit comments

Comments
 (0)