Esempio n. 1
0
    def load_device_by_mnemonic(self,
                                mnemonic,
                                pin,
                                passphrase_protection,
                                label,
                                language,
                                skip_checksum=False):
        m = Mnemonic('english')
        if not skip_checksum and not m.check(mnemonic):
            raise Exception("Invalid mnemonic checksum")

        # Convert mnemonic to UTF8 NKFD
        mnemonic = Mnemonic.normalize_string(mnemonic)

        # Convert mnemonic to ASCII stream
        mnemonic = unicode(str(bytearray(mnemonic, 'utf-8')), 'utf-8')

        if self.features.initialized:
            raise Exception(
                "Device is initialized already. Call wipe_device() and try again."
            )

        resp = self.call(
            proto.LoadDevice(mnemonic=mnemonic,
                             pin=pin,
                             passphrase_protection=passphrase_protection,
                             language=language,
                             label=label,
                             skip_checksum=skip_checksum))
        self.init_device()
        return resp
Esempio n. 2
0
    def load_device_by_xprv(self, xprv, pin, passphrase_protection, label,
                            language):
        if self.features.initialized:
            raise Exception(
                "Device is initialized already. Call wipe_device() and try again."
            )

        if xprv[0:4] not in ('xprv', 'tprv'):
            raise Exception("Unknown type of xprv")

        if len(xprv) < 100 and len(xprv) > 112:
            raise Exception("Invalid length of xprv")

        node = types.HDNodeType()
        data = tools.b58decode(xprv, None).encode('hex')

        if data[90:92] != '00':
            raise Exception("Contain invalid private key")

        checksum = hashlib.sha256(
            hashlib.sha256(binascii.unhexlify(
                data[:156])).digest()).hexdigest()[:8]
        if checksum != data[156:]:
            raise Exception("Checksum doesn't match")

        # version 0488ade4
        # depth 00
        # fingerprint 00000000
        # child_num 00000000
        # chaincode 873dff81c02f525623fd1fe5167eac3a55a049de3d314bb42ee227ffed37d508
        # privkey   00e8f32e723decf4051aefac8e2c93c9c5b214313817cdb01a1494b917c8436b35
        # checksum e77e9d71

        node.depth = int(data[8:10], 16)
        node.fingerprint = int(data[10:18], 16)
        node.child_num = int(data[18:26], 16)
        node.chain_code = data[26:90].decode('hex')
        node.private_key = data[92:156].decode(
            'hex')  # skip 0x00 indicating privkey

        resp = self.call(
            proto.LoadDevice(node=node,
                             pin=pin,
                             passphrase_protection=passphrase_protection,
                             language=language,
                             label=label))
        self.init_device()
        return resp