How to use Bitpay with Java
Asked Answered
P

2

12

I found this post about BitPay but it's not very clear how I can use it.

https://help.bitpay.com/development/how-do-i-use-the-bitpay-java-client-library

I implemented this code:

public void createInvoice() throws BitPayException
    {
        ECKey key = KeyUtils.createEcKey();
        BitPay bitpay = new BitPay(key);
        InvoiceBuyer buyer = new InvoiceBuyer();
        buyer.setName("Satoshi");
        buyer.setEmail("[email protected]");

        Invoice invoice = new Invoice(100.0, "USD");
        invoice.setBuyer(buyer);
        invoice.setFullNotifications(true);
        invoice.setNotificationEmail("[email protected]");
        invoice.setPosData("ABCDEFGHIJKLMNOPQRSTUVWXYZ1234567890");

        Invoice createInvoice = bitpay.createInvoice(invoice);
    }

How should I implement the private key?

Plenish answered 7/9, 2016 at 13:34 Comment(3)
Check out this linkSuitcase
Yes, I already tested but I can't make working code. Can you show me working example, please?Plenish
Can you please provide stack traceWesleyanism
F
0

You need to go through the client pairing process: https://developer.bitpay.com/reference/getting-access

This will output a file/ json object containing the private key. You can save the json object in your application's resource folder and load it into your client like this:

val configFilePath = ConfigFilePath("src/main/resources/BitPay.config.json")
val bitpayClient = Client.createClientByConfigFilePath(configFilePath)
Fontana answered 22/7 at 8:29 Comment(0)
S
-2

That answer, I believe, is found in the following file: https://github.com/bitpay/java-bitpay-client/blob/master/src/main/java/controller/BitPay.java - that is to say, you would set your private key on the BitPay client instance. There you can find the appropriate constructor for your needs. You will want to use one or more of the following fields depending on your specific needs:

private ECKey _ecKey = null;
private String _identity = "";
private String _clientName = "";
private Hashtable<String, String> _tokenCache;

Edit: encryption and decryption of your private key exists here: https://github.com/bitpay/java-bitpay-client/blob/master/src/main/java/controller/KeyUtils.java

If, for instance, you used the following constructor:

public BitPay(URI privateKey) throws BitPayException, URISyntaxException,        IOException {
    this(KeyUtils.loadEcKey(privateKey), BITPAY_PLUGIN_INFO, BITPAY_URL);
}

You would pass in the URI for your private key.

Specific instructions on this available here: https://github.com/bitpay/java-bitpay-client/blob/master/GUIDE.md

Two very simple examples:

BitPay bitpay = new BitPay();
ECKey key = KeyUtils.createEcKey();
this.bitpay = new BitPay(key);

Number two:

// Create the private key external to the SDK, store it in a file, and inject    the private key into the SDK.
String privateKey = KeyUtils.getKeyStringFromFile(privateKeyFile);
ECKey key = KeyUtils.createEcKeyFromHexString(privateKey);
this.bitpay = new BitPay(key);

After implementing the private key, you'd till need to initialize the client and connect to the server.

Samsun answered 15/9, 2016 at 3:24 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.