Skip to content

Commit c5ba2e5

Browse files
author
Chris Kleeschulte
committed
Conversion to Maven, Tests adjusted to help developers
- As per mrosseel's pom file, conversion of all dependencies to use maven2. This will ease the pain of managing a bunch of jar files in lib - Repaired some deprecation warnings in bitcoinj dependency around Sha256 creation and ECKey construction. - Adjusted test harness to create tokens for pos/merchant facade, this is done once the first time someone runs the tests. The test runner just has to pair with their test account. The tester is does not have cascading test failures that might confuses as to what he/she needs to do. - Added some notes about how to do the refund test.
1 parent aea9ea0 commit c5ba2e5

File tree

4 files changed

+31
-45
lines changed

4 files changed

+31
-45
lines changed

src/main/java/controller/BitPay.java

Lines changed: 8 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -21,12 +21,14 @@
2121
import org.apache.http.util.EntityUtils;
2222
import org.bitcoinj.core.ECKey;
2323

24-
import com.fasterxml.jackson.core.JsonProcessingException;
25-
import com.fasterxml.jackson.core.type.TypeReference;
26-
import com.fasterxml.jackson.databind.JsonMappingException;
27-
import com.fasterxml.jackson.databind.JsonNode;
28-
import com.fasterxml.jackson.databind.ObjectMapper;
29-
import org.bitcoinj.core.ECKey;
24+
import java.io.IOException;
25+
import java.io.UnsupportedEncodingException;
26+
import java.net.URI;
27+
import java.net.URISyntaxException;
28+
import java.net.UnknownHostException;
29+
import java.util.Arrays;
30+
import java.util.Hashtable;
31+
import java.util.List;
3032

3133
public class BitPay {
3234

src/main/java/controller/KeyUtils.java

Lines changed: 11 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -7,16 +7,8 @@
77

88
import java.io.*;
99
import java.math.BigInteger;
10-
import java.net.URI;
11-
import java.net.URISyntaxException;
1210
import java.security.SecureRandom;
1311

14-
import org.bitcoinj.core.Base58;
15-
import org.bitcoinj.core.ECKey;
16-
import org.bitcoinj.core.ECKey.ECDSASignature;
17-
import org.bitcoinj.core.Sha256Hash;
18-
import org.bitcoinj.core.Utils;
19-
2012
public class KeyUtils {
2113

2214
final private static char[] hexArray = "0123456789abcdef".toCharArray();
@@ -29,16 +21,20 @@ public static boolean privateKeyExists()
2921
return new File(PRIV_KEY_FILENAME).exists();
3022
}
3123

32-
public static ECKey createEcKey() {
24+
public static ECKey createEcKey()
25+
{
3326
//Default constructor uses SecureRandom numbers.
3427
return new ECKey();
3528
}
3629

37-
public static ECKey createEcKeyFromHexString(String privateKey) {
30+
public static ECKey createEcKeyFromHexString(String privateKey)
31+
{
3832
//if you are going to choose this option, please ensure this string is as random as
3933
//possible, consider http://world.std.com/~reinhold/diceware.html
4034
SecureRandom randomSeed = new SecureRandom(privateKey.getBytes());
41-
return new ECKey(randomSeed);
35+
ECKey key = new ECKey(randomSeed);
36+
37+
return key;
4238
}
4339

4440
/**
@@ -48,9 +44,10 @@ public static ECKey createEcKeyFromHexStringFile(String privKeyFile) throws IOEx
4844
return createEcKeyFromHexString(getKeyStringFromFile(privKeyFile));
4945
}
5046

51-
public static ECKey loadEcKey() throws IOException {
52-
FileInputStream fileInputStream;
53-
File file;
47+
public static ECKey loadEcKey() throws IOException
48+
{
49+
FileInputStream fileInputStream = null;
50+
File file = new File(PRIV_KEY_FILENAME);
5451

5552
if (KeyUtils.privateKey == null) {
5653
file = new File(PRIV_KEY_FILENAME);

src/test/java/test/BitPayTest.java

Lines changed: 7 additions & 22 deletions
Original file line numberDiff line numberDiff line change
@@ -9,9 +9,6 @@
99
import org.junit.BeforeClass;
1010
import org.junit.Test;
1111

12-
import java.io.IOException;
13-
import java.net.URI;
14-
import java.net.URISyntaxException;
1512
import java.text.SimpleDateFormat;
1613
import java.util.Date;
1714
import java.util.List;
@@ -27,33 +24,21 @@ public class BitPayTest {
2724

2825
private static String clientName = "BitPay Java Library Tester";
2926
private static String pairingCode;
30-
private static String refundInvoiceId = null;
31-
private static URI myKeyFile;
27+
private static String refundInvoiceId;
3228

3329
@Before
34-
public void setUp() throws BitPayException, IOException, URISyntaxException {
30+
public void setUp() throws BitPayException {
3531
//ensure the second argument (api url) is the same as the one used in setUpOneTime()
36-
bitpay = new BitPay(myKeyFile, clientName, BitPay.BITPAY_TEST_URL);
32+
bitpay = new BitPay(clientName, BitPay.BITPAY_TEST_URL);
3733
}
3834

3935
@BeforeClass
40-
public static void setUpOneTime() throws InterruptedException, IOException, BitPayException, URISyntaxException {
36+
public static void setUpOneTime() throws Exception
37+
{
4138
boolean dumpOut = false;
42-
43-
//create a key, if a file does exist at the uri, myKeyfile, a new key will be created in the construction of the client
44-
//ECKey myKey = KeyUtils.createEcKey();
45-
46-
47-
myKeyFile = new URI("file:///tmp/bitpay_private.key"); //if file exists, it will not overwrite
48-
49-
//save the somewhere that you can reuse it:
50-
//this saves a EC key to compressed ASN.1 DER encoded format
51-
//if you use your own key (not generated by our key utils), then ensure your key is in the above format
52-
//KeyUtils.saveEcKey(myKey, myKeyFile);
53-
5439
// This scenario qualifies that this (test) client does not have merchant facade access.
5540
clientName += " on " + java.net.InetAddress.getLocalHost();
56-
BitPay bitpay = new BitPay(myKeyFile, clientName, BitPay.BITPAY_TEST_URL);
41+
BitPay bitpay = new BitPay(clientName, BitPay.BITPAY_TEST_URL);
5742

5843
// Authorize this client for use with a BitPay merchant account. This client requires both
5944
// POS and MERCHANT facades.
@@ -77,7 +62,7 @@ public static void setUpOneTime() throws InterruptedException, IOException, BitP
7762
System.out.println("Info: Client is requesting POS facade access. Pair this client with your merchant account using the pairing code: " + pairingCode);
7863
dumpOut = true;
7964
//we already failed to authorize for a POS token, therefore we must sleep a bit to try to authorize for any other facade (rate limiter on the api side)
80-
Thread.sleep(10000);
65+
Thread.sleep(3000);
8166
}
8267

8368
if (!bitpay.clientIsAuthorized(BitPay.FACADE_MERCHANT))

src/test/java/test/BitPayTest2.java

Lines changed: 5 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,7 @@
77
import org.junit.BeforeClass;
88
import org.junit.Test;
99

10-
import java.net.UnknownHostException;
10+
import static org.junit.Assert.assertNotNull;
1111

1212
import static org.junit.Assert.assertNotNull;
1313

@@ -23,14 +23,14 @@ public void setUp() throws BitPayException {
2323
}
2424

2525
@BeforeClass
26-
public static void setUpOneTime() throws UnknownHostException, BitPayException
26+
public static void setUpOneTime() throws Exception
2727
{
2828
// If this test has never been run before then this test must be run twice in order to pass.
2929
// The first time this test runs it will create an identity and emit a client pairing code.
3030
// The pairing code must then be authorized in a BitPay account. Running the test a second
3131
// time should result in the authorized client (this test) running to completion.
3232
clientName += " on " + java.net.InetAddress.getLocalHost();
33-
BitPay bitpay = new BitPay(clientName, BitPay.BITPAY_TEST_URL); //this tests the old way of creating keys/clients
33+
BitPay bitpay = new BitPay(clientName, BitPay.BITPAY_TEST_URL);
3434

3535
if (!bitpay.clientIsAuthorized(BitPay.FACADE_POS))
3636
{
@@ -55,6 +55,8 @@ public void testShouldGetInvoiceId()
5555
} catch (BitPayException e) {
5656
e.printStackTrace();
5757
}
58+
System.out.println(invoice.getId());
59+
refundInvoiceId = invoice.getId();
5860
assertNotNull(invoice.getId());
5961
}
6062
}

0 commit comments

Comments
 (0)