Skip to content

Commit 3bc1178

Browse files
author
Chris Kleeschulte
committed
Creating BitPay client with URI of key
- Added constructors for passing the URI of your private key. We prefer to have users of this library pass in a URI for their key file. That way, this api isn't just writing new key files to the current working directory. If the URI does not have a key already, one will be created there (if writeable). It will be the user's responsibility to safeguard their private keys. - A bit of code cleanup in terms of spelling and removal of warnings, etc.
1 parent c5ba2e5 commit 3bc1178

File tree

3 files changed

+40
-31
lines changed

3 files changed

+40
-31
lines changed

src/main/java/controller/KeyUtils.java

Lines changed: 14 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -7,34 +7,33 @@
77

88
import java.io.*;
99
import java.math.BigInteger;
10+
import java.net.URI;
11+
import java.net.URISyntaxException;
1012
import java.security.SecureRandom;
1113

1214
public class KeyUtils {
1315

1416
final private static char[] hexArray = "0123456789abcdef".toCharArray();
1517
final private static String PRIV_KEY_FILENAME = "bitpay_private.key";
18+
private static URI privateKey;
1619

17-
public KeyUtils() {}
20+
public KeyUtils() {
21+
}
1822

19-
public static boolean privateKeyExists()
20-
{
23+
public static boolean privateKeyExists() {
2124
return new File(PRIV_KEY_FILENAME).exists();
2225
}
2326

24-
public static ECKey createEcKey()
25-
{
27+
public static ECKey createEcKey() {
2628
//Default constructor uses SecureRandom numbers.
2729
return new ECKey();
2830
}
2931

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

4039
/**
@@ -44,10 +43,9 @@ public static ECKey createEcKeyFromHexStringFile(String privKeyFile) throws IOEx
4443
return createEcKeyFromHexString(getKeyStringFromFile(privKeyFile));
4544
}
4645

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

5250
if (KeyUtils.privateKey == null) {
5351
file = new File(PRIV_KEY_FILENAME);
@@ -145,9 +143,6 @@ public static String deriveSIN(ECKey ecKey) throws IllegalArgumentException {
145143
return Base58.encode(unencodedBytes);
146144
}
147145

148-
return encoded;
149-
}
150-
151146
public static String sign(ECKey key, String input) throws UnsupportedEncodingException {
152147
byte[] data = input.getBytes("UTF8");
153148

@@ -159,9 +154,8 @@ public static String sign(ECKey key, String input) throws UnsupportedEncodingExc
159154
return bytesToHex(bytes);
160155
}
161156

162-
private static int getHexVal(char hex)
163-
{
164-
int val = (int)hex;
157+
private static int getHexVal(char hex) {
158+
int val = (int) hex;
165159
return val - (val < 58 ? 48 : (val < 97 ? 55 : 87));
166160
}
167161

src/test/java/test/BitPayTest.java

Lines changed: 22 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,9 @@
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;
1215
import java.text.SimpleDateFormat;
1316
import java.util.Date;
1417
import java.util.List;
@@ -24,21 +27,33 @@ public class BitPayTest {
2427

2528
private static String clientName = "BitPay Java Library Tester";
2629
private static String pairingCode;
27-
private static String refundInvoiceId;
30+
private static String refundInvoiceId = null;
31+
private static URI myKeyFile;
2832

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

3539
@BeforeClass
36-
public static void setUpOneTime() throws Exception
37-
{
40+
public static void setUpOneTime() throws InterruptedException, IOException, BitPayException, URISyntaxException {
3841
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+
3954
// This scenario qualifies that this (test) client does not have merchant facade access.
4055
clientName += " on " + java.net.InetAddress.getLocalHost();
41-
BitPay bitpay = new BitPay(clientName, BitPay.BITPAY_TEST_URL);
56+
BitPay bitpay = new BitPay(myKeyFile, clientName, BitPay.BITPAY_TEST_URL);
4257

4358
// Authorize this client for use with a BitPay merchant account. This client requires both
4459
// POS and MERCHANT facades.
@@ -62,7 +77,7 @@ public static void setUpOneTime() throws Exception
6277
System.out.println("Info: Client is requesting POS facade access. Pair this client with your merchant account using the pairing code: " + pairingCode);
6378
dumpOut = true;
6479
//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)
65-
Thread.sleep(3000);
80+
Thread.sleep(10000);
6681
}
6782

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

src/test/java/test/BitPayTest2.java

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

10+
import java.net.UnknownHostException;
11+
1012
import static org.junit.Assert.assertNotNull;
1113

1214
import static org.junit.Assert.assertNotNull;
@@ -23,14 +25,14 @@ public void setUp() throws BitPayException {
2325
}
2426

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

3537
if (!bitpay.clientIsAuthorized(BitPay.FACADE_POS))
3638
{
@@ -55,8 +57,6 @@ public void testShouldGetInvoiceId()
5557
} catch (BitPayException e) {
5658
e.printStackTrace();
5759
}
58-
System.out.println(invoice.getId());
59-
refundInvoiceId = invoice.getId();
6060
assertNotNull(invoice.getId());
6161
}
6262
}

0 commit comments

Comments
 (0)