示例#1
0
 def test_recovery_base58(self):
     mnemonics = generate(18)
     # Get 5 of 8 split
     shamir_shares = split_shares(mnemonics,
                                  7,
                                  10,
                                  encoding=Encoding.BASE58)
     self.assertEqual(len(shamir_shares), 10)
     recovered_mnemonics = combine_shares(shamir_shares[:9],
                                          encoding=Encoding.BASE58)
     self.assertEqual(recovered_mnemonics, mnemonics)
     #Verify not equal values with less shares
     recovered_incorrect = combine_shares(shamir_shares[:6],
                                          encoding=Encoding.BASE58)
     self.assertNotEqual(mnemonics, recovered_incorrect)
    def recover_key(self):
        """
        Callback function for combine button to recover the key
        """
        try:
            input_shares = self.recoverSharesTextEdit.toPlainText().split("\n")
            language = self.languageComboBox2.currentText()
            encoding = Encoding.BIP39
            if self.encodingComboBox2.currentIndex() == 1:
                encoding = Encoding.BASE58

            shares = list()
            for share in input_shares:
                share = share.strip()
                if share != "":
                    shares.append(share)
            recovered_key = combine_shares(shares, encoding, language=language)
            self.recoveredKeyTextEdit.setText(recovered_key)

        except BadRequest as e:
            error_dialog = QtWidgets.QErrorMessage(self)
            error_dialog.showMessage("Bad Request! Invalid input!")

        except ValueError:
            error_dialog = QtWidgets.QErrorMessage(self)
            error_dialog.showMessage(
                "Bad Request! Incorrect Language Selected!")

        except Exception:
            error_dialog = QtWidgets.QErrorMessage()
            error_dialog.showMessage("Error!")
示例#3
0
    def test_recovery(self):
        mnemonics = generate(18)
        # Get 5 of 8 split
        shamir_shares = split_shares(mnemonics, 7, 10)
        self.assertEqual(len(shamir_shares), 10)
        recovered_mnemonics = combine_shares(shamir_shares[:9])
        self.assertEqual(recovered_mnemonics, mnemonics)

        self.assertRaises(BadRequest, combine_shares, shamir_shares[:6])
示例#4
0
 def test_split_shares_base58(self):
     mnemonics = generate(15)
     # Get 5 of 8 split
     shamir_shares = split_shares(mnemonics, 5, 8, encoding=Encoding.BASE58)
     self.assertEqual(len(shamir_shares), 8)
     random.shuffle(shamir_shares)
     recovered_mnemonics = combine_shares(shamir_shares[:5],
                                          encoding=Encoding.BASE58)
     self.assertEqual(recovered_mnemonics, mnemonics)
示例#5
0
    def test_split_shares_italian(self):
        language = "italian"
        mnemonics = generate(15, language)
        # Get 5 of 8 split
        shamir_shares = split_shares(mnemonics, 5, 8, language=language)
        self.assertEqual(len(shamir_shares), 8)

        random.shuffle(shamir_shares)
        recovered_mnemonics = combine_shares(shamir_shares[:5],
                                             language=language)
        self.assertEqual(recovered_mnemonics, mnemonics)
示例#6
0
    def test_base58_french(self):
        language = "french"
        mnemonics = generate(24, language=language)
        # Get 5 of 8 split
        shamir_shares = split_shares(mnemonics,
                                     10,
                                     15,
                                     encoding=Encoding.BASE58,
                                     language=language)
        self.assertEqual(len(shamir_shares), 15)
        random.shuffle(shamir_shares)
        recovered_mnemonics = combine_shares(shamir_shares[:10],
                                             encoding=Encoding.BASE58,
                                             language=language)
        self.assertEqual(recovered_mnemonics, mnemonics)

        recovered_incorrect = combine_shares(shamir_shares[:6],
                                             encoding=Encoding.BASE58,
                                             language=language)
        self.assertNotEqual(mnemonics, recovered_incorrect)
示例#7
0
    def test_base58_italian(self):
        language = "italian"
        mnemonics = generate(15, language=language)
        # Get 5 of 8 split
        shamir_shares = split_shares(mnemonics,
                                     5,
                                     8,
                                     encoding=Encoding.BASE58,
                                     language=language)
        self.assertEqual(len(shamir_shares), 8)
        random.shuffle(shamir_shares)
        recovered_mnemonics = combine_shares(shamir_shares[:5],
                                             encoding=Encoding.BASE58,
                                             language=language)
        self.assertEqual(recovered_mnemonics, mnemonics)

        recovered_incorrect = combine_shares(shamir_shares[:3],
                                             encoding=Encoding.BASE58,
                                             language=language)
        self.assertNotEqual(mnemonics, recovered_incorrect)
示例#8
0
def recover(input_file, encoding, lang):
    """
    Recover the private key from the given shares.
    """
    shamir_shares = list()
    if input_file[0] == "SINGLE":
        file_path = input_file[1]
        # Check if file exists
        if os.path.isfile(file_path):
            file_handler = open(file_path, "r")
            for line in file_handler:
                line = line.strip()
                if line != "":
                    shamir_shares.append(line)
        else:
            raise click.FileError("File doesn't exist.")
    else:
        dir_path = input_file[1]
        if os.path.isdir(dir_path):
            file_list = os.listdir(dir_path)
            # Iterate through the files in the directory
            for share_file in file_list:
                # Check for the file format
                if os.path.splitext(share_file)[1] == ".txt":
                    file_path = os.path.join(dir_path, share_file)
                    file_handler = open(file_path, "r")
                    for line in file_handler:
                        line = line.strip()
                        if line != "":
                            shamir_shares.append(line)
                else:
                    raise click.FileError("Incorrect file format!")
        else:
            raise click.BadParameter("Directory doesn't exist!")

    
    recovered_key = combine_shares(shamir_shares, encoding, LANG_MAP[lang])
    click.echo(recovered_key)