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