|
10 | 10 | from src.bitpay_sdk.utils.key_utils import * |
11 | 11 | from src.bitpay_sdk.exceptions.bitpay_exception import BitPayException |
12 | 12 |
|
13 | | -is_prod = False # Set to true if the environment for which the configuration file will be generated is Production. |
14 | 13 | # Will be set to Test otherwise |
15 | | - |
16 | 14 | private_key_name = 'private_key.pem' # Add here the name for your Private key |
17 | | -generate_merchant_token = True # Set to true to generate a token for the Merchant facade |
18 | | -generate_payout_token = True # Set to true to generate a token for the Payout facade |
19 | | -your_master_password = 'YourMasterPassword' # Will be used to encrypt your PrivateKey |
| 15 | +private_key_path = os.path.join(os.path.abspath(os.curdir), private_key_name) |
| 16 | +plain_private_key = None |
20 | 17 | proxy = None |
21 | | -base_url = 'https://bitpay.com' if is_prod else 'https://test.bitpay.com' |
22 | | -env = 'Prod' if is_prod else 'Test' |
| 18 | +api_url = None |
| 19 | +environment = None |
23 | 20 | merchant_token = None |
24 | 21 | payout_token = None |
25 | 22 |
|
26 | | -root_path = os.path.abspath(os.curdir) |
27 | 23 |
|
28 | | -try: |
29 | | - private_key = generate_pem() |
30 | | - with open(os.path.join(root_path, private_key_name), "wb") as f: |
31 | | - f.write(private_key.encode()) |
32 | | -except BitPayException as e: |
33 | | - print(e) |
| 24 | +def select_env(): |
| 25 | + global environment |
| 26 | + try: |
| 27 | + print("Select target environment: ") |
| 28 | + target_environment = input('Press T for testing or P for production: \n') |
| 29 | + |
| 30 | + if target_environment.lower() == 't': |
| 31 | + environment = 'Test' |
| 32 | + elif target_environment.lower() == 'p': |
| 33 | + environment = 'Prod' |
| 34 | + else: |
| 35 | + select_env() |
| 36 | + |
| 37 | + set_environment(environment) |
| 38 | + select_create_key() |
| 39 | + except BitPayException as e: |
| 40 | + print(e) |
| 41 | + |
| 42 | + |
| 43 | +def set_environment(env): |
| 44 | + global api_url |
| 45 | + if env == 'Test': |
| 46 | + api_url = 'https://test.bitpay.com' |
| 47 | + else: |
| 48 | + api_url = 'https://bitpay.com' |
| 49 | + |
| 50 | + |
| 51 | +def select_create_key(): |
| 52 | + try: |
| 53 | + input_value = input('Press enter to generate a brand new key or enter your private key location:') |
| 54 | + if input_value == '': |
| 55 | + create_new_key() |
| 56 | + else: |
| 57 | + load_key() |
| 58 | + except BitPayException as e: |
| 59 | + print(e) |
| 60 | + |
| 61 | + |
| 62 | +def create_new_key(): |
| 63 | + try: |
| 64 | + private_key = generate_pem() |
| 65 | + store_key(private_key) |
| 66 | + except BitPayException as e: |
| 67 | + print(e) |
| 68 | + |
| 69 | + |
| 70 | +def store_key(private_key): |
| 71 | + global plain_private_key, private_key_path |
| 72 | + try: |
| 73 | + print("Select the way you want to store your private key:") |
| 74 | + input_value = input("Press F for storing in a pem file or T for plain text in your config file: ") |
| 75 | + |
| 76 | + if input_value.lower() == 'f': |
| 77 | + with open(str(private_key_path), "wb") as f: |
| 78 | + f.write(private_key.encode()) |
| 79 | + plain_private_key = None |
| 80 | + print("Private key saved at path:", private_key_path) |
| 81 | + select_tokens(private_key) |
| 82 | + elif input_value.lower() == 't': |
| 83 | + plain_private_key = private_key |
| 84 | + private_key_path = None |
| 85 | + print("Saving private key... \n") |
| 86 | + select_tokens(private_key) |
| 87 | + else: |
| 88 | + store_key(private_key) |
| 89 | + except BitPayException as e: |
| 90 | + print(e) |
| 91 | + |
| 92 | + |
| 93 | +def select_tokens(private_key): |
| 94 | + try: |
| 95 | + print("Select the tokens that you would like to request:") |
| 96 | + input_value = input('Press M for merchant, P for payout, or B for both: \n') |
| 97 | + if input_value.lower() in ['m', 'p', 'b']: |
| 98 | + print("Requesting Tokens... \n") |
| 99 | + request_tokens(input_value.lower(), private_key) |
| 100 | + else: |
| 101 | + select_tokens(private_key) |
| 102 | + except BitPayException as e: |
| 103 | + print(e) |
| 104 | + |
| 105 | + |
| 106 | +def request_tokens(token_type, private_key): |
| 107 | + global merchant_token |
| 108 | + global payout_token |
| 109 | + |
| 110 | + try: |
| 111 | + sin = get_sin_from_pem(private_key) |
| 112 | + url = "%s/tokens" % api_url |
| 113 | + headers = {"content-type": "application/json", "X-accept-version": "2.0.0"} |
34 | 114 |
|
35 | | -# Generate the public key from the private key every time (no need to store the public key). |
36 | | -try: |
37 | | - public_key = get_compressed_public_key_from_pem(private_key) |
38 | | -except BitPayException as e: |
39 | | - print(e) |
| 115 | + if token_type in ['m', 'b']: |
| 116 | + print("Requesting Merchant token... \n") |
| 117 | + facade = 'merchant' |
| 118 | + payload = {'id': sin, 'facade': facade} |
| 119 | + |
| 120 | + response = requests.post(url, verify=True, data=json.dumps(payload), headers=headers) |
| 121 | + if response.ok: |
| 122 | + merchant_token = response.json()['data'][0]['token'] |
| 123 | + print("Merchant Token: ", response.json()['data'][0]['token']) |
| 124 | + print("Merchant Token Pairing Code: ", response.json()['data'][0]['pairingCode'] + "\n") |
| 125 | + |
| 126 | + if token_type in ['p', 'b']: |
| 127 | + print("Requesting Payout token... \n") |
| 128 | + facade = 'payout' |
| 129 | + payload = {'id': sin, 'facade': facade} |
| 130 | + |
| 131 | + response = requests.post(url, verify=True, data=json.dumps(payload), headers=headers) |
| 132 | + if response.ok: |
| 133 | + payout_token = response.json()['data'][0]['token'] |
| 134 | + print("Payout Token: ", response.json()['data'][0]['token']) |
| 135 | + print("Payout Token Pairing Code: ", response.json()['data'][0]['pairingCode'] + "\n") |
| 136 | + |
| 137 | + update_config_file() |
| 138 | + except BitPayException as e: |
| 139 | + print(e) |
| 140 | + |
| 141 | + |
| 142 | +def update_config_file(): |
| 143 | + try: |
| 144 | + config = { |
| 145 | + "BitPayConfiguration": { |
| 146 | + "Environment": environment, |
| 147 | + "EnvConfig": { |
| 148 | + environment: { |
| 149 | + "PrivateKeyPath": private_key_path, |
| 150 | + "PrivateKey": plain_private_key, |
| 151 | + "ApiTokens": { |
| 152 | + "merchant": merchant_token, |
| 153 | + "payout": payout_token |
| 154 | + }, |
| 155 | + "proxy": proxy |
| 156 | + } |
| 157 | + } |
| 158 | + } |
| 159 | + } |
40 | 160 |
|
41 | | -try: |
42 | | - sin = get_sin_from_pem(private_key) |
43 | | -except BitPayException as e: |
44 | | - print(e) |
| 161 | + with open(os.path.abspath("bitpay.config.json"), 'w') as outfile: |
| 162 | + json.dump(config, outfile) |
| 163 | + print('Generated configuration file at path: ', os.path.abspath("bitpay.config.json")) |
45 | 164 |
|
46 | | -try: |
47 | | - if generate_merchant_token: |
48 | | - facade = 'merchant' |
49 | | - payload = {'id': sin, 'facade': facade} |
| 165 | + print('Configuration generated successfully! \n') |
| 166 | + print("\r\nPlease, copy the above pairing code/s and approve on your BitPay Account at the following link:\r\n") |
| 167 | + print(f"{api_url}/dashboard/merchant/api-tokens\r\n") |
| 168 | + print("\r\nOnce you have this Pairing Code/s approved you can move the generated files to a secure location " |
| 169 | + "and start using the Client.\r\n") |
| 170 | + except BitPayException as e: |
| 171 | + print(e) |
50 | 172 |
|
51 | | - url = base_url + "/tokens" |
52 | | - headers = {"content-type": "application/json", "X-accept-version": "2.0.0"} |
53 | | - response = requests.post(url, verify=True, data=json.dumps(payload), headers=headers) |
54 | 173 |
|
55 | | - if response.ok: |
56 | | - merchant_token = response.json()['data'][0]['token'] |
57 | | - print("Merchant Token: ", response.json()['data'][0]['token']) |
58 | | - print("Merchant Token Pairing Code: ", response.json()['data'][0]['pairingCode']) |
| 174 | +def load_key(): |
| 175 | + # TODO: Need to implement this function |
| 176 | + pass |
59 | 177 |
|
60 | | - if generate_payout_token: |
61 | | - facade = 'payout' |
62 | | - payload = {'id': sin, 'facade': facade} |
63 | 178 |
|
64 | | - url = base_url + "/tokens" |
65 | | - headers = {"content-type": "application/json", "X-accept-version": "2.0.0"} |
66 | | - response = requests.post(url, verify=True, data=json.dumps(payload), headers=headers) |
67 | | - |
68 | | - if response.ok: |
69 | | - payout_token = response.json()['data'][0]['token'] |
70 | | - print("Payout Token: ", response.json()['data'][0]['token']) |
71 | | - print("Payout Token Pairing Code: ", response.json()['data'][0]['pairingCode']) |
72 | | - |
73 | | -except BitPayException as e: |
74 | | - print(e) |
75 | | - |
76 | | -print("\r\nPlease, copy the above pairing code/s and approve on your BitPay Account at the following link:\r\n") |
77 | | -print(f"{base_url}/dashboard/merchant/api-tokens\r\n") |
78 | | -print("\r\nOnce you have this Pairing Code/s approved you can move the generated files to a secure location and start " |
79 | | - "using the Client.\r\n") |
80 | | - |
81 | | -# Generate config file |
82 | | -config = { |
83 | | - "BitPayConfiguration": { |
84 | | - "Environment": env, |
85 | | - "EnvConfig": { |
86 | | - "Test": { |
87 | | - "PrivateKeyPath": None if is_prod else os.path.abspath("private_key.pem"), |
88 | | - "PrivateKeySecret": None if is_prod else your_master_password, |
89 | | - "ApiTokens": { |
90 | | - "merchant": None if is_prod else merchant_token, |
91 | | - "payout": None if is_prod else payout_token |
92 | | - }, |
93 | | - "proxy": proxy |
94 | | - }, |
95 | | - "Prod": { |
96 | | - "PrivateKeyPath": os.path.abspath("private.pem") if is_prod else None, |
97 | | - "PrivateKeySecret": your_master_password if is_prod else None, |
98 | | - "ApiTokens": { |
99 | | - "merchant": merchant_token if is_prod else None, |
100 | | - "payout": payout_token if is_prod else None |
101 | | - }, |
102 | | - "proxy": proxy |
103 | | - } |
104 | | - } |
105 | | - } |
106 | | -} |
107 | | - |
108 | | -try: |
109 | | - with open(os.path.abspath("bitpay.config.json"), 'w') as outfile: |
110 | | - json.dump(config, outfile) |
111 | | -except BitPayException as e: |
112 | | - print(e) |
| 179 | +if __name__ == '__main__': |
| 180 | + select_env() |
| 181 | + |
0 commit comments