def green(debug, network, auth, config_dir, compact): """Command line interface for green gdk""" global context if context is not None: # Retain context over multiple commands in repl mode return if debug: logging.basicConfig(level=logging.DEBUG) if network == 'mainnet': raise click.ClickException("This tool is not currently suitable for use on mainnet") config_dir = config_dir or os.path.expanduser(os.path.join('~', '.green-cli', network)) try: os.makedirs(config_dir) except FileExistsError: pass gdk.init({}) session = gdk.Session({'name': network}) atexit.register(session.destroy) authenticator = get_authenticator(auth, config_dir) context = Context(session, network, TwoFactorResolver(), authenticator, compact)
def login_with_pin(cls, pin): self = cls() pin_data = open(self.PIN_DATA_FILENAME).read() self.session = gdk.Session({'name': 'testnet'}) self.session.login_with_pin(str(pin), pin_data).resolve() self.fetch_subaccount() return self
def login_with_mnemonic(cls, mnemonic): self = cls() self.mnemonic = mnemonic self.session = gdk.Session({'name': 'testnet'}) self.session.login({}, self.mnemonic).resolve() self.fetch_subaccount() return self
def login_with_pin(cls, pin): self = cls() pin_data = open(self.PIN_DATA_FILENAME).read() self.session = gdk.Session({'name': 'testnet'}) credentials = {'pin': str(pin), 'pin_data': json.loads(pin_data)} self.session.login_user({}, credentials).resolve() self.fetch_subaccount() return self
def login_with_mnemonic(cls, mnemonic): self = cls() self.mnemonic = mnemonic self.session = gdk.Session({'name': 'testnet'}) credentials = {'mnemonic': self.mnemonic, 'password': ''} self.session.login_user({}, credentials).resolve() self.fetch_subaccount() return self
def create_new_wallet(cls, create_with_2fa_enabled, mnemonic=None): self = cls() # You can pass in a mnemonic generated outside GDK if you want, or have # GDK generate it for you by omitting it. 2FA is enabled if chosen and # can be enabled/disabled at any point. self.mnemonic = mnemonic or gdk.generate_mnemonic() # Session network name options are: testnet, mainnet. self.session = gdk.Session({'name': 'testnet'}) self.session.register_user({}, self.mnemonic).resolve() credentials = {'mnemonic': self.mnemonic, 'password': ''} self.session.login_user({}, credentials).resolve() self.fetch_block_height() if create_with_2fa_enabled: self.twofactor_auth_enabled(True) return self
def create_new_wallet(cls, create_with_2fa_enabled, mnemonic=None): self = cls() # Create a new wallet with a Managed Assets account. # You can pass in a mnemonic generated outside GDK if you want, or have # GDK generate it for you by omitting it. 2FA is enabled if chosen and # can be enabled/disabled at any point. self.mnemonic = mnemonic or gdk.generate_mnemonic() # Set the network name to 'liquid' for the live Liquid network. # There is currently no test Liquid network. self.session = gdk.Session({'name': 'liquid'}) self.session.register_user({}, self.mnemonic).resolve() credentials = {'mnemonic': self.mnemonic, 'password': ''} self.session.login_user({}, credentials).resolve() self.session.create_subaccount({'name': self.SUBACCOUNT_NAME, 'type': self.AMP_ACCOUNT_TYPE}).resolve() if create_with_2fa_enabled: self.twofactor_auth_enabled(True) return self
def download_file_gdk(hw_target, write_compressed, index_file, auto_select_fw): import greenaddress as gdk import base64 import json # We need to pass the relevant root certificate with open(FWSERVER_CERTIFICATE_FILE, 'r') as cf: root_cert = cf.read() gdk.init({}) session = gdk.Session({'name': 'mainnet'}) # GET the index file from the firmware server which lists the # available firmwares url = f'{FWSERVER_URL_ROOT}/{hw_target}/{index_file}' logger.info(f'Downloading firmware index file {url} using gdk') params = {'method': 'GET', 'root_certificates': [root_cert], 'urls': [url]} rslt = gdk.http_request(session.session_obj, json.dumps(params)) rslt = json.loads(rslt) assert 'body' in rslt, f'Cannot download index file {url}: {rslt.get("error")}' # Get the filename of the firmware to download fwname = get_fw_filename(rslt['body'], auto_select_fw) fwlen = get_expected_fw_length(fwname) # GET the selected firmware from the server in base64 encoding url = f'{FWSERVER_URL_ROOT}/{hw_target}/{fwname}' logger.info(f'Downloading firmware {url} using gdk') params['urls'] = [url] params['accept'] = 'base64' rslt = gdk.http_request(session.session_obj, json.dumps(params)) rslt = json.loads(rslt) assert 'body' in rslt, f'Cannot download firmware file {url}: {rslt.get("error")}' fw_b64 = rslt['body'] fwcmp = base64.b64decode(fw_b64) logger.info('Downloaded {len(fwcmp)} byte firmware') # If passed --write-compressed we write a copy of the compressed file if write_compressed: write_cmpfwfile(fwname, fwcmp) # Return return fwcmp, fwlen
def download_file_gdk(hw_target, write_compressed, release): import greenaddress as gdk import base64 gdk.init({}) session = gdk.Session({'name': 'mainnet'}) # GET the index file from the firmware server which lists the # available firmwares url = f'{FWSERVER_URL_ROOT}/{hw_target}/{FWSERVER_INDEX_FILE}' logger.info(f'Downloading firmware index file {url} using gdk') params = {'method': 'GET', 'urls': [url]} rslt = gdk.http_request(session.session_obj, json.dumps(params)) rslt = json.loads(rslt) assert 'body' in rslt, f'Cannot download index file {url}: {rslt.get("error")}' # Get the filename of the firmware to download release_data = json.loads(rslt['body'])[release] if not release_data: return None fwdata = get_fw_metadata(release_data) fwname = fwdata['filename'] # GET the selected firmware from the server in base64 encoding url = f'{FWSERVER_URL_ROOT}/{hw_target}/{fwname}' logger.info(f'Downloading firmware {url} using gdk') params = {'method': 'GET', 'urls': [url], 'accept': 'base64'} rslt = gdk.http_request(session.session_obj, json.dumps(params)) rslt = json.loads(rslt) assert 'body' in rslt, f'Cannot download firmware file {url}: {rslt.get("error")}' fw_b64 = rslt['body'] fwcmp = base64.b64decode(fw_b64) logger.info(f'Downloaded {len(fwcmp)} byte firmware') # If passed --write-compressed we write a copy of the compressed file if write_compressed: cmpfilename = f'{COMP_FW_DIR}/{fwname}' fwtools.write(fwcmp, cmpfilename) # Return return fwdata['fwsize'], fwdata.get('patch_size'), fwcmp
logger = logging.getLogger('jade') logger.setLevel(logging.DEBUG) logger.addHandler(jadehandler) logger = logging.getLogger('jade-device') logger.setLevel(logging.DEBUG) logger.addHandler(jadehandler) # We can test with the gdk http_request() function if we have the wheel installed http_request_fn = None if USE_GDK_HTTP_CLIENT: import json import greenaddress as gdk gdk.init({}) gdk_session = gdk.Session({'name': 'mainnet'}) def http_request_fn(params): reply = gdk.http_request(gdk_session.session_obj, json.dumps(params)) return json.loads(reply) if len(sys.argv) > 1 and sys.argv[1] == 'ble': print('Fetching jade version info over BLE') serial_number = sys.argv[2] if len(sys.argv) > 2 else None create_jade_fn = JadeAPI.create_ble kwargs = {'serial_number': serial_number} else: print('Fetching jade version info over serial') serial_device = sys.argv[2] if len(sys.argv) > 2 else None create_jade_fn = JadeAPI.create_serial