Repository navigation
Expand file tree
/
Copy pathBtcPayKeySetup.java
More file actions
130 lines (110 loc) · 5.74 KB
/
Copy pathBtcPayKeySetup.java
File metadata and controls
130 lines (110 loc) · 5.74 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
import com.bitpay.sdk.Client;
import com.bitpay.sdk.Env;
import com.bitpay.sdk.model.Facade;
import com.bitpay.sdk.util.KeyUtils;
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 org.bitcoinj.core.ECKey;
import java.io.File;
import java.nio.file.Paths;
import java.util.HashMap;
import java.util.Scanner;
import java.util.concurrent.atomic.AtomicReference;
class BtcPayKeySetup {
public static void main(String[] args) {
Scanner scanner = new Scanner(System.in);
String opt, env, privateKeyPath = "", privateKeyAsHex = "", pairingCodePos = "", privateKey = "";
int keyType; // 1 = in file, 2 = as text
Client bitpay;
Env btcPayEnv = new Env("https://btcpay.vante.me:444/", "https://btcpay.vante.me:444/");
do {
System.out.println("Select target environment:");
System.out.println("Press T for testing or P for production:");
opt = scanner.next();
} while (opt.toLowerCase().equals("t") && opt.toLowerCase().equals("p"));
if (opt.toLowerCase().equals("t")) {
env = btcPayEnv.Test;
} else {
env = btcPayEnv.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 (opt.toLowerCase().equals("f") && opt.toLowerCase().equals("t"));
if (opt.toLowerCase().equals("f")) {
keyType = 1;
} else {
keyType = 2;
}
System.out.println("Generating private key... ");
try {
File directory = new File(Paths.get(".").toAbsolutePath().normalize().toString() + "/output");
if (!directory.exists()) {
directory.mkdir();
}
if (keyType == 1) {
privateKeyPath = Paths.get(".").toAbsolutePath().normalize().toString() + "/output/btcpay_private_" + env.toLowerCase() + ".key";
if (!KeyUtils.privateKeyExists(privateKeyPath)) {
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 {
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 (Exception e) {
System.out.println(e.getMessage());
System.exit(0);
}
System.out.println("Generating config file... ");
System.out.println("Assign label to btcPayClient: ");
String label = scanner.next();
try {
ObjectMapper mapper = new ObjectMapper();
bitpay = new Client(btcPayEnv, env, privateKey, new Env.Tokens(), null);
pairingCodePos = bitpay.requestClientAuthorization(Facade.Merchant, label);
HashMap<String, String> tokens = new HashMap<>();
tokens.put("merchant", bitpay.getAccessToken(Facade.Merchant));
AtomicReference<JsonNode> ApiTokens = new AtomicReference<>(mapper.valueToTree(tokens));
ObjectNode envConfig = mapper.createObjectNode();
envConfig.put("PrivateKeyPath", privateKeyPath);
envConfig.put("PrivateKey", privateKeyAsHex);
envConfig.put("ApiTokens", ApiTokens.get());
ObjectNode envTarget = mapper.createObjectNode();
envTarget.put(env, envConfig);
ObjectNode bitPayConfiguration = mapper.createObjectNode();
bitPayConfiguration.put("Environment", env).put("EnvConfig", envTarget);
ObjectNode configurationFile = mapper.createObjectNode();
configurationFile.put("BitPayConfiguration", bitPayConfiguration);
String jsonString = mapper.writerWithDefaultPrettyPrinter().writeValueAsString(configurationFile);
ObjectWriter writer = mapper.writer(new DefaultPrettyPrinter());
writer.writeValue(new File(Paths.get(".").toAbsolutePath().normalize().toString() + "/output/BtcPay.config.json"), configurationFile);
System.out.println("In location:");
System.out.println(Paths.get(".").toAbsolutePath().normalize().toString() + "/output/BitPay.config.json");
System.out.println("With the following information:");
System.out.println(jsonString);
System.out.println();
System.out.println("To complete your setup, Go to " + btcPayEnv.getTestUrl() + "api-access-request?pairingCode=" + pairingCodePos);
System.out.println();
System.out.println(pairingCodePos + " for the PointOfSale facade.");
} catch (Exception e) {
System.out.println(e.getMessage());
System.exit(0);
}
System.out.println("The private key and the config file is been generated in a directory called output in the root of this package.");
System.out.println("Make sure you store this key in a secure location and update the PrivateKeyPath in the generated config file.");
}
}