Exemplo n.º 1
0
    def export_ms_wallet(wallet: Multisig_Wallet, fp, name):
        # Build the text file Coldcard needs to understand the multisig wallet
        # it is participating in. All involved Coldcards can share same file.
        assert isinstance(wallet, Multisig_Wallet)

        print('# Exported from Electrum', file=fp)
        print(f'Name: {name:.20s}', file=fp)
        print(f'Policy: {wallet.m} of {wallet.n}', file=fp)
        print(f'Format: {wallet.txin_type.upper()}' , file=fp)

        xpubs = []
        for xpub, ks in zip(wallet.get_master_public_keys(), wallet.get_keystores()):  # type: str, KeyStoreWithMPK
            fp_bytes, der_full = ks.get_fp_and_derivation_to_be_used_in_partial_tx(der_suffix=[], only_der_suffix=False)
            fp_hex = fp_bytes.hex().upper()
            der_prefix_str = bip32.convert_bip32_intpath_to_strpath(der_full)
            xpubs.append( (fp_hex, xpub, der_prefix_str) )

        # Before v3.2.1 derivation didn't matter too much to the Coldcard, since it
        # could use key path data from PSBT or USB request as needed. However,
        # derivation data is now required.

        print('', file=fp)

        assert len(xpubs) == wallet.n
        for xfp, xpub, der_prefix in xpubs:
            print(f'Derivation: {der_prefix}', file=fp)
            print(f'{xfp}: {xpub}\n', file=fp)
Exemplo n.º 2
0
 def create_wallet(self, text, password):
     if self.wallet_type == 'standard':
         self.wallet = Wallet.from_text(text, password, self.storage)
         self.run('create_addresses')
     else:
         self.storage.put('wallet_type', self.wallet_type)
         self.wallet = Multisig_Wallet(self.storage)
         self.wallet.add_seed(text, password)
         self.wallet.create_master_keys(password)
         action = self.wallet.get_action()
         self.run(action)
Exemplo n.º 3
0
    def export_ms_wallet(wallet: Multisig_Wallet, fp, name):
        # Build the text file Coldcard needs to understand the multisig wallet
        # it is participating in. All involved Coldcards can share same file.
        assert isinstance(wallet, Multisig_Wallet)

        print('# Exported from Electrum', file=fp)
        print(f'Name: {name:.20s}', file=fp)
        print(f'Policy: {wallet.m} of {wallet.n}', file=fp)
        print(f'Format: {wallet.txin_type.upper()}', file=fp)

        xpubs = []
        derivs = set()
        for xpub, ks in zip(
                wallet.get_master_public_keys(),
                wallet.get_keystores()):  # type: str, KeyStoreWithMPK
            fp_bytes, der_full = ks.get_fp_and_derivation_to_be_used_in_partial_tx(
                der_suffix=[], only_der_suffix=False)
            fp_hex = fp_bytes.hex().upper()
            der_prefix_str = bip32.convert_bip32_intpath_to_strpath(der_full)
            xpubs.append((fp_hex, xpub, der_prefix_str))
            derivs.add(der_prefix_str)

        # Derivation doesn't matter too much to the Coldcard, since it
        # uses key path data from PSBT or USB request as needed. However,
        # if there is a clear value, provide it.
        if len(derivs) == 1:
            print("Derivation: " + derivs.pop(), file=fp)

        print('', file=fp)

        assert len(xpubs) == wallet.n
        for xfp, xpub, der_prefix in xpubs:
            if derivs:
                # show as a comment if unclear
                print(f'# derivation: {der_prefix}', file=fp)

            print(f'{xfp}: {xpub}\n', file=fp)
Exemplo n.º 4
0
    def btc_multisig_config(
        self, coin, bip32_path: List[int], wallet: Multisig_Wallet, xtype: str,
    ):
        """
        Set and get a multisig config with the current device and some other arbitrary xpubs.
        Registers it on the device if not already registered.
        xtype: 'p2wsh' | 'p2wsh-p2sh'
        """
        assert xtype in ("p2wsh", "p2wsh-p2sh")
        if self.bitbox02_device is None:
            raise Exception(
                "Need to setup communication first before attempting any BitBox02 calls"
            )
        account_keypath = bip32_path[:-2]
        xpubs = wallet.get_master_public_keys()
        our_xpub = self.get_xpub(
            bip32.convert_bip32_intpath_to_strpath(account_keypath), xtype
        )

        multisig_config = bitbox02.btc.BTCScriptConfig(
            multisig=bitbox02.btc.BTCScriptConfig.Multisig(
                threshold=wallet.m,
                xpubs=[util.parse_xpub(xpub) for xpub in xpubs],
                our_xpub_index=xpubs.index(our_xpub),
                script_type={
                    "p2wsh": bitbox02.btc.BTCScriptConfig.Multisig.P2WSH,
                    "p2wsh-p2sh": bitbox02.btc.BTCScriptConfig.Multisig.P2WSH_P2SH,
                }[xtype]
            )
        )

        is_registered = self.bitbox02_device.btc_is_script_config_registered(
            coin, multisig_config, account_keypath
        )
        if not is_registered:
            name = self.handler.name_multisig_account()
            try:
                self.bitbox02_device.btc_register_script_config(
                    coin=coin,
                    script_config=multisig_config,
                    keypath=account_keypath,
                    name=name,
                )
            except bitbox02.DuplicateEntryException:
                raise
            except:
                raise UserFacingException("Failed to register multisig\naccount configuration on BitBox02")
        return multisig_config
Exemplo n.º 5
0
class BaseWizard(object):

    def __init__(self, config, network, storage):
        super(BaseWizard, self).__init__()
        self.config  = config
        self.network = network
        self.storage = storage
        self.wallet = None

    def run(self, action, *args):
        '''Entry point of our Installation wizard'''
        if not action:
            return
        if hasattr(self, action):
            f = getattr(self, action)
            apply(f, *args)
        else:
            raise BaseException("unknown action", action)

    def new(self):
        name = os.path.basename(self.storage.path)
        msg = "\n".join([
            _("Welcome to the Electrum installation wizard."),
            _("The wallet '%s' does not exist.") % name,
            _("What kind of wallet do you want to create?")
        ])
        choices = [
            (_('Standard wallet'), 'create_standard'),
            (_('Multi-signature wallet'), 'create_multisig'),
        ]
        self.choice_dialog(msg=msg, choices=choices, run_prev=self.cancel, run_next=self.run)

    def choose_seed(self):
        msg = ' '.join([
            _("Do you want to create a new seed, or to restore a wallet using an existing seed?")
        ])
        choices = [
            (_('Create a new seed'), 'create_seed'),
            (_('I already have a seed'), 'restore_seed'),
            (_('Watching-only wallet'), 'restore_xpub')
        ]
        self.choice_dialog(msg=msg, choices=choices, run_prev=self.new, run_next=self.run)

    def create_multisig(self):
        def f(m, n):
            self.wallet_type = "%dof%d"%(m, n)
            self.run('choose_seed')
        name = os.path.basename(self.storage.path)
        self.multisig_dialog(run_prev=self.new, run_next=f)

    def restore_seed(self):
        msg = _('Please type your seed phrase using the virtual keyboard.')
        self.restore_seed_dialog(run_prev=self.new, run_next=self.enter_pin, test=Wallet.is_seed, message=msg)

    def restore_xpub(self):
        title = "MASTER PUBLIC KEY"
        message =  _('To create a watching-only wallet, paste your master public key, or scan it using the camera button.')
        self.add_xpub_dialog(run_prev=self.new, run_next=lambda xpub: self.create_wallet(xpub, None), title=title, message=message, test=Wallet.is_mpk)

    def create_standard(self):
        self.wallet_type = 'standard'
        self.run('choose_seed')

    def create_wallet(self, text, password):
        if self.wallet_type == 'standard':
            self.wallet = Wallet.from_text(text, password, self.storage)
            self.run('create_addresses')
        else:
            self.storage.put('wallet_type', self.wallet_type)
            self.wallet = Multisig_Wallet(self.storage)
            self.wallet.add_seed(text, password)
            self.wallet.create_master_keys(password)
            action = self.wallet.get_action()
            self.run(action)

    def add_cosigners(self):
        xpub = self.wallet.master_public_keys.get('x1/')
        self.show_xpub_dialog(run_prev=self.create_multisig, run_next=self.add_cosigner, xpub=xpub, test=Wallet.is_xpub)

    def add_cosigner(self):
        def on_xpub(xpub):
            self.wallet.add_cosigner(xpub)
            i = self.wallet.get_missing_cosigner()
            action = 'add_cosigner' if i else 'create_main_account'
            self.run(action)
        title = "ADD COSIGNER"
        message = _('Please paste your cosigners master public key, or scan it using the camera button.')
        self.add_xpub_dialog(run_prev=self.add_cosigners, run_next=on_xpub, title=title, message=message, test=Wallet.is_xpub)

    def create_main_account(self):
        self.wallet.create_main_account()
        self.run('create_addresses')

    def create_addresses(self):
        def task():
            self.wallet.create_main_account()
            self.wallet.synchronize()
        msg= _("Electrum is generating your addresses, please wait.")
        self.waiting_dialog(task, msg, on_complete=self.terminate)

    def create_seed(self):
        from electrum_ltc.wallet import BIP32_Wallet
        seed = BIP32_Wallet.make_seed()
        msg = _("If you forget your PIN or lose your device, your seed phrase will be the "
                "only way to recover your funds.")
        self.show_seed_dialog(run_prev=self.new, run_next=self.confirm_seed, message=msg, seed_text=seed)

    def confirm_seed(self, seed):
        assert Wallet.is_seed(seed)
        msg = _('Please retype your seed phrase, to confirm that you properly saved it')
        self.restore_seed_dialog(run_prev=self.create_seed, run_next=self.enter_pin, test=lambda x: x==seed, message=msg)

    def enter_pin(self, seed):
        def callback(pin):
            action = 'confirm_pin' if pin else 'create_wallet'
            self.run(action, (seed, pin))
        self.password_dialog('Choose a PIN code', callback)

    def confirm_pin(self, seed, pin):
        def callback(conf):
            if conf == pin:
                self.run('create_wallet', (seed, pin))
            else:
                self.show_error(_('PIN mismatch'))
                self.run('enter_pin', (seed,))
        self.password_dialog('Confirm your PIN code', callback)

    def terminate(self):
        self.wallet.start_threads(self.network)
        self.dispatch('on_wizard_complete', self.wallet)

    def cancel(self):
        self.dispatch('on_wizard_complete', None)
        return True