/*
* Copyright (c) 2019 BitPay.
* All rights reserved.
*/
import com.bitpay.sdk.Client;
import com.bitpay.sdk.Config;
import com.bitpay.sdk.Environment;
import com.bitpay.sdk.PrivateKey;
import com.bitpay.sdk.model.Facade;
import com.bitpay.sdk.util.KeyUtils;
import com.bitpay.sdk.util.TokenContainer;
import com.fasterxml.jackson.core.util.DefaultPrettyPrinter;
import com.fasterxml.jackson.databind.JsonNode;
import com.fasterxml.jackson.databind.ObjectMapper;
import com.fasterxml.jackson.databind.ObjectWriter;
import com.fasterxml.jackson.databind.node.ObjectNode;
import java.io.File;
import java.nio.file.Paths;
import java.util.HashMap;
import java.util.Scanner;
import java.util.concurrent.atomic.AtomicReference;
import org.bitcoinj.core.ECKey;
public class BitPaySetup {
public static void main(final String[] args) {
final Scanner scanner = new Scanner(System.in);
String opt;
final Environment env;
String privateKeyPath = "";
String privateKeyAsHex = "";
final String pairingCodeMerchant;
final String pairingCodePayout;
String privateKey = "";
final int keyType; // 1 = in file, 2 = as text
final Client client;
do {
System.out.println("Select target environment:");
System.out.println("Press T for testing or P for production:");
opt = scanner.next();
} while ("t".equals(opt.toLowerCase()) && "p".equals(opt.toLowerCase()));
if ("t".equals(opt.toLowerCase())) {
env = Environment.TEST;
} else {
env = Environment.PROD;
}
do {
System.out.println("Select the way you want your private key:");
System.out.println("Press F for binary in a text file or T for plain text in your config file:");
opt = scanner.next();
} while ("f".equals(opt.toLowerCase()) && "t".equals(opt.toLowerCase()));
if ("f".equals(opt.toLowerCase())) {
keyType = 1;
} else {
keyType = 2;
}
System.out.println("Generating private key... ");
try {
final File directory = new File(Paths.get(".").toAbsolutePath().normalize() + "/output");
if (!directory.exists()) {
directory.mkdir();
}
if (keyType == 1) {
privateKeyPath = Paths.get(".").toAbsolutePath().normalize()
+ "/output/bitpay_private_"
+ env.toString().toLowerCase()
+ ".key";
if (!KeyUtils.privateKeyExists(privateKeyPath)) {
final ECKey ecKey = KeyUtils.createEcKey();
KeyUtils.saveEcKey(ecKey);
KeyUtils.createEcKey();
System.out.println("Private key generated successfully with public key:");
System.out.println(ecKey.getPublicKeyAsHex());
} else {
KeyUtils.loadEcKey();
}
privateKey = privateKeyPath;
} else {
final ECKey ecKey = KeyUtils.createEcKey();
privateKeyAsHex = KeyUtils.loadEcKeyAsHex(ecKey);
System.out.println("Private key generated successfully with public key:");
System.out.println(ecKey.getPublicKeyAsHex());
privateKey = privateKeyAsHex;
}
} catch (final Exception e) {
System.out.println(e.getMessage());
System.exit(0);
}
System.out.println("Generating config file... ");
try {
final ObjectMapper mapper = new ObjectMapper();
client = new Client(env, new PrivateKey(privateKey), new TokenContainer(), null, null);
pairingCodeMerchant = client.authorizeClient(Facade.MERCHANT);
pairingCodePayout = client.authorizeClient(Facade.PAYOUT);
final HashMap