Beispiel #1
0
 def save_settings(self, settings, fname=None):
     maybe_mkdir(self.SETTINGS_DIR)
     if fname is None:
         fname = self.settings_fname
     self.keystore.save_aead(fname,
                             adata=json.dumps(settings).encode(),
                             key=self.keystore.settings_key)
Beispiel #2
0
 def load_config(self):
     try:
         config, _ = self.keystore.load_aead(
             self.path + "/settings", self.keystore.enc_secret
         )
         config = json.loads(config.decode())
     except Exception as e:
         print(e)
         config = {"dev": self.dev, "usb": self.usb}
         self.keystore.save_aead(
             self.path + "/settings",
             adata=json.dumps(config).encode(),
             key=self.keystore.enc_secret,
         )
     self.dev = config["dev"]
     self.usb = config["usb"]
     # add apps in dev mode
     if self.dev:
         try:
             qspi = fpath("/qspi/extensions")
             maybe_mkdir(qspi)
             maybe_mkdir(qspi + "/extra_apps")
             if qspi not in sys.path:
                 sys.path.append(qspi)
                 self.apps += load_apps("extra_apps")
         except Exception as e:
             print(e)
Beispiel #3
0
 def tempdir(self):
     if self.TEMPDIR is None:
         return None
     maybe_mkdir(self.TEMPDIR)
     path = self.TEMPDIR + "/" + type(self).__name__
     maybe_mkdir(path)
     return path
Beispiel #4
0
 def add_wallet(self, w):
     self.wallets.append(w)
     wallet_ids = sorted([int(f[0]) for f in os.ilistdir(self.path) \
                 if f[0].isdigit() and f[1] == 0x4000])
     newpath = self.path + ("/%d" % (max(wallet_ids) + 1))
     platform.maybe_mkdir(newpath)
     w.save(self.keystore, path=newpath)
Beispiel #5
0
 def __init__(self, path):
     # storage for data
     self.path = path
     maybe_mkdir(path)
     # set manager
     self.manager = None
     # check this flag in update function
     # if disabled - throw all incoming data
     self.enabled = False
     self.initialized = False
Beispiel #6
0
 async def init(self, show_fn):
     """
     Waits for keystore media
     and loads internal secret and PIN state
     """
     self.show = show_fn
     platform.maybe_mkdir(self.path)
     self.load_secret(self.path)
     self.load_state()
     # the rest we can get from parent
     await super().init(show_fn)
Beispiel #7
0
    async def init(self, show_fn):
        """
        Waits for keystore media
        and loads internal secret and PIN state
        """
        self.show = show_fn
        platform.maybe_mkdir(self.path)
        self.load_secret(self.path)

        await self.check_card()
        # the rest can be done with parent
        await super().init(show_fn)
Beispiel #8
0
 def __init__(self, desc, path=None, name="Untitled"):
     self.name = name
     self.path = path
     if self.path is not None:
         self.path = self.path.rstrip("/")
         maybe_mkdir(self.path)
     self.descriptor = desc
     # receive and change gap limits
     self.gaps = [self.GAP_LIMIT for b in range(self.descriptor.num_branches)]
     self.name = name
     self.unused_recv = 0
     self.keystore = None
Beispiel #9
0
 def save(self, keystore, path=None):
     # wallet has access to keystore only if it's saved or loaded from file
     self.keystore = keystore
     if path is not None:
         self.path = path.rstrip("/")
     if self.path is None:
         raise WalletError("Path is not defined")
     maybe_mkdir(self.path)
     desc = str(self.descriptor)
     keystore.save_aead(self.path + "/descriptor", plaintext=desc.encode())
     obj = {"gaps": self.gaps, "name": self.name, "unused_recv": self.unused_recv}
     meta = json.dumps(obj).encode()
     keystore.save_aead(self.path + "/meta", plaintext=meta)
Beispiel #10
0
 def load_wallets(self):
     """Loads all wallets from path"""
     try:
         platform.maybe_mkdir(self.path)
         # get ids of the wallets - every wallet is stored in a numeric folder
         wallet_ids = sorted([int(f[0]) for f in os.ilistdir(self.path) \
                     if f[0].isdigit() and f[1] == 0x4000])
         return [
             self.load_wallet(self.path + ("/%d" % wid))
             for wid in wallet_ids
         ]
     except:
         return []
Beispiel #11
0
def get_keystore(mnemonic="ability " * 11 + "acid", password=""):
    """Returns a dummy keystore"""
    platform.maybe_mkdir(TEST_DIR)
    platform.maybe_mkdir(TEST_DIR + "/keystore")
    ks = RAMKeyStore()
    ks.path = TEST_DIR + "/keystore"
    ks.show_loader = show_loader
    ks.show = show
    ks.load_secret(ks.path)
    ks.initialized = True
    ks._unlock("1234")
    ks.set_mnemonic(mnemonic, password)
    return ks
Beispiel #12
0
 def __init__(self, path):
     # storage for data
     self.path = path
     maybe_mkdir(path)
     if self.SETTINGS_DIR:
         maybe_mkdir(self.SETTINGS_DIR)
     # set manager
     self.manager = None
     # check this flag in update function
     # if disabled - throw all incoming data
     self.enabled = False
     self.initialized = False
     # default settings, extend it with more settings if applicable
     self.settings = {"enabled": True}
Beispiel #13
0
 def save(self, keystore, path=None):
     if path is not None:
         self.path = path.rstrip("/")
     if self.path is None:
         raise WalletError("Path is not defined")
     maybe_mkdir(self.path)
     desc = self.descriptor()
     keystore.save_aead(self.path + "/descriptor", plaintext=desc.encode())
     obj = {
         "gaps": self.gaps,
         "name": self.name,
         "unused_recv": self.unused_recv
     }
     meta = json.dumps(obj).encode()
     keystore.save_aead(self.path + "/meta", plaintext=meta)
Beispiel #14
0
 def __init__(self, script, wrapped=False, path=None, name="Untitled"):
     self.name = name
     self.path = path
     if self.path is not None:
         self.path = self.path.rstrip("/")
         maybe_mkdir(self.path)
     if type(script) not in type(self).SCRIPTS:
         raise WalletError("%s not in %s" %
                           (type(script), type(self).SCRIPTS))
     self.script = script
     self.wrapped = wrapped
     # receive and change gap limits
     self.gaps = [self.GAP_LIMIT, self.GAP_LIMIT]
     self.name = name
     self.unused_recv = 0
Beispiel #15
0
 def init(self, keystore, network="test"):
     """Loads or creates default wallets for new keystore or network"""
     self.keystore = keystore
     # add fingerprint dir
     path = self.root_path + "/" + hexlify(self.keystore.fingerprint).decode()
     platform.maybe_mkdir(path)
     if network not in NETWORKS:
         raise WalletError("Invalid network")
     self.network = network
     # add network dir
     path += "/" + network
     platform.maybe_mkdir(path)
     self.path = path
     self.wallets = self.load_wallets()
     if self.wallets is None or len(self.wallets) == 0:
         w = self.create_default_wallet(path=self.path + "/0")
         self.wallets = [w]
Beispiel #16
0
 async def init(self, show_fn):
     """
     Waits for keystore media 
     and loads internal secret and PIN state
     """
     self.show = show_fn
     platform.maybe_mkdir(self.path)
     self.load_secret(self.path)
     self.load_state()
     # check if init is called for the first time
     # and we have less than max PIN attempts
     if (not self.initialized
             and self.pin_attempts_left != self.pin_attempts_max):
         scr = Alert("Warning!",
                     "You only have %d of %d attempts\n"
                     "to enter correct PIN code!" %
                     (self.pin_attempts_left, self.pin_attempts_max),
                     button_text="OK")
         await self.show(scr)
     self.initialized = True
Beispiel #17
0
def get_wallets_app(keystore, network):
    platform.maybe_mkdir(TEST_DIR)
    platform.maybe_mkdir(TEST_DIR + "/wallets")
    platform.maybe_mkdir(TEST_DIR + "/tmp")
    BaseApp.tempdir = TEST_DIR + "/tmp"
    wapp = WalletsApp(TEST_DIR + "/wallets")
    wapp.init(keystore, network, show_loader, communicate)
    return wapp
Beispiel #18
0
def init_keystore(ks):
    platform.maybe_mkdir(ks.path)
    ks.load_secret(ks.path)
    ks.load_state()
    ks.initialized = True
Beispiel #19
0
def main(apps=None, network="main", keystore_cls=None):
    """
    apps: list of apps to load
    network: default network to operate
    keystores: list of KeyStore classes that can be used
    """
    # Init display first as it also inits the SDRAM
    display.init(False)
    # create virtual file system /sdram
    # for temp untrusted data storage
    rampath = platform.mount_sdram()

    # set working path to empty folder in sdram
    if not platform.simulator:
        cwd = rampath + "/cwd"
        platform.maybe_mkdir(cwd)
        os.chdir(cwd)

    # define hosts - USB, QR, SDCard
    # each hosts gets it's own RAM folder for data
    Host.SETTINGS_DIR = platform.fpath("/qspi/hosts")
    Specter.SETTINGS_DIR = platform.fpath("/qspi/global")
    hosts = [
        USBHost(rampath + "/usb"),
        QRHost(rampath + "/qr"),
        SDHost(rampath + "/sd"),
    ]
    # temp storage in RAM for host commands processing
    BaseApp.TEMPDIR = rampath + "/tmp"

    # define GUI
    if not platform.simulator:
        gui = SpecterGUI()
    else:
        # this GUI can simulate user actions for automated testing
        from gui.tcp_gui import TCPGUI
        gui = TCPGUI()

    # inject the folder where keystore stores it's data
    KeyStore.path = platform.fpath("/flash/keystore")
    # detect keystore to use
    if keystore_cls is not None:
        keystores = [keystore_cls]
    else:
        keystores = [
            MemoryCard,
            SDKeyStore,
        ]

    # loading apps
    if apps is None:
        apps = load_apps()

    # make Specter instance
    settings_path = platform.fpath("/flash")
    specter = Specter(
        gui=gui,
        keystores=keystores,
        hosts=hosts,
        apps=apps,
        settings_path=settings_path,
        network=network,
    )
    specter.start()
Beispiel #20
0
 def init(self):
     """Load internal secret and PIN state"""
     platform.maybe_mkdir(self.path)
     self.load_secret(self.path)
     self.load_state()
Beispiel #21
0
 def __init__(self, path: str):
     """path is the folder where this app should store data"""
     maybe_mkdir(path)
     self.path = path
Beispiel #22
0
 def __init__(self, path):
     self.root_path = path
     platform.maybe_mkdir(path)
     self.path = None
     self.wallets = []
Beispiel #23
0
 def __init__(self, path):
     self.root_path = path
     platform.maybe_mkdir(path)
     self.path = None
     self.manager = None