-
Notifications
You must be signed in to change notification settings - Fork 54
Expand file tree
/
Copy pathBitPaySetup.java
More file actions
162 lines (140 loc) · 6.54 KB
/
Copy pathBitPaySetup.java
File metadata and controls
162 lines (140 loc) · 6.54 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
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
/*
* 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<String, String> tokens = new HashMap<>();
tokens.put("merchant", client.getAccessToken(Facade.MERCHANT));
tokens.put("payout", client.getAccessToken(Facade.PAYOUT));
final AtomicReference<JsonNode> ApiTokens = new AtomicReference<>(mapper.valueToTree(tokens));
final ObjectNode envConfig = mapper.createObjectNode();
envConfig.put("PrivateKeyPath", privateKeyPath);
envConfig.put("PrivateKey", privateKeyAsHex);
envConfig.put("ApiTokens", ApiTokens.get());
final ObjectNode envTarget = mapper.createObjectNode();
envTarget.put(env.toString(), envConfig);
final ObjectNode bitPayConfiguration = mapper.createObjectNode();
bitPayConfiguration.put("Environment", env.toString()).put("EnvConfig", envTarget);
final ObjectNode configurationFile = mapper.createObjectNode();
configurationFile.put("BitPayConfiguration", bitPayConfiguration);
final String jsonString = mapper.writerWithDefaultPrettyPrinter().writeValueAsString(configurationFile);
final ObjectWriter writer = mapper.writer(new DefaultPrettyPrinter());
writer.writeValue(
new File(Paths.get(".").toAbsolutePath().normalize() + "/output/BitPay.config.json"),
configurationFile);
System.out.println("In location:");
System.out.println(Paths.get(".").toAbsolutePath().normalize() + "/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 "
+ Config.TEST_URL
+ "dashboard/merchant/api-tokens "
+ "and pair this client with your merchant account using the pairing codes:"
);
System.out.println();
System.out.println(pairingCodeMerchant + " for the Merchant facade.");
System.out
.println(pairingCodePayout + " for the Payout facade ONLY if you have requested access for this role.");
} catch (final 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."
);
}
}