Esempio n. 1
0
    def test_wallet_keys(self):
        stm = self.bts
        stm.wallet.unlock("123")
        priv_key = stm.wallet.getPrivateKeyForPublicKey(
            str(PrivateKey(self.posting_key, prefix=stm.prefix).pubkey))
        self.assertEqual(str(priv_key), self.posting_key)
        priv_key = stm.wallet.getKeyForAccount("crea", "active")
        self.assertEqual(str(priv_key), self.active_key)
        priv_key = stm.wallet.getKeyForAccount("crea1", "posting")
        self.assertEqual(str(priv_key), self.posting_key1)

        priv_key = stm.wallet.getPrivateKeyForPublicKey(
            str(
                PrivateKey(self.active_private_key_of_crea4,
                           prefix=stm.prefix).pubkey))
        self.assertEqual(str(priv_key), self.active_private_key_of_crea4)
        priv_key = stm.wallet.getKeyForAccount("crea4", "active")
        self.assertEqual(str(priv_key), self.active_private_key_of_crea4)

        priv_key = stm.wallet.getPrivateKeyForPublicKey(
            str(
                PrivateKey(self.active_private_key_of_crea5,
                           prefix=stm.prefix).pubkey))
        self.assertEqual(str(priv_key), self.active_private_key_of_crea5)
        priv_key = stm.wallet.getKeyForAccount("crea5", "active")
        self.assertEqual(str(priv_key), self.active_private_key_of_crea5)
 def test_btsprivkey(self):
     self.assertEqual([
         format(
             PrivateKey(
                 "5HqUkGuo62BfcJU5vNhTXKJRXuUi9QSE6jp8C3uBJ2BVHtB8WSd").
             address, "BTS"),
         format(
             PrivateKey(
                 "5JWcdkhL3w4RkVPcZMdJsjos22yB5cSkPExerktvKnRNZR5gx1S").
             address, "BTS"),
         format(
             PrivateKey(
                 "5HvVz6XMx84aC5KaaBbwYrRLvWE46cH6zVnv4827SBPLorg76oq").
             address, "BTS"),
         format(
             PrivateKey(
                 "5Jete5oFNjjk3aUMkKuxgAXsp7ZyhgJbYNiNjHLvq5xzXkiqw7R").
             address, "BTS"),
         format(
             PrivateKey(
                 "5KDT58ksNsVKjYShG4Ls5ZtredybSxzmKec8juj7CojZj6LPRF7").
             address, "BTS")
     ], [
         "BTSFN9r6VYzBK8EKtMewfNbfiGCr56pHDBFi",
         "BTSdXrrTXimLb6TEt3nHnePwFmBT6Cck112",
         "BTSJQUAt4gz4civ8gSs5srTK4r82F7HvpChk",
         "BTSFPXXHXXGbyTBwdKoJaAPXRnhFNtTRS4EL",
         "BTS3qXyZnjJneeAddgNDYNYXbF7ARZrRv5dr",
     ])
 def test_encrypt(self):
     self.assertEqual([format(encrypt(PrivateKey("5HqUkGuo62BfcJU5vNhTXKJRXuUi9QSE6jp8C3uBJ2BVHtB8WSd"), "TestingOneTwoThree"), "encwif"),
                       format(encrypt(PrivateKey("5KN7MzqK5wt2TP1fQCYyHBtDrXdJuXbUzm4A9rKAteGu3Qi5CVR"), "TestingOneTwoThree"), "encwif"),
                       format(encrypt(PrivateKey("5HtasZ6ofTHP6HCwTqTkLDuLQisYPah7aUnSKfC7h4hMUVw2gi5"), "Satoshi"), "encwif")],
                      ["6PRN5mjUTtud6fUXbJXezfn6oABoSr6GSLjMbrGXRZxSUcxThxsUW8epQi",
                       "6PRVWUbkzzsbcVac2qwfssoUJAN1Xhrg6bNk8J7Nzm5H7kxEbn2Nh2ZoGg",
                       "6PRNFFkZc2NZ6dJqFfhRoFNMR9Lnyj7dYGrzdgXXVMXcxoKTePPX1dWByq"])
Esempio n. 4
0
    def decrypt(self, memo):
        """ Decrypt a memo

            :param str memo: encrypted memo message
            :returns: encrypted memo
            :rtype: str
        """
        if not memo:
            return None

        # We first try to decode assuming we received the memo
        if isinstance(
                memo,
                dict) and "to" in memo and "from" in memo and "memo" in memo:
            memo_to = Account(memo["to"], crea_instance=self.crea)
            memo_from = Account(memo["from"], crea_instance=self.crea)
            message = memo["memo"]
        else:
            memo_to = self.to_account
            memo_from = self.from_account
            message = memo
        if isinstance(memo, dict) and "nonce" in memo:
            nonce = memo.get("nonce")
        else:
            nonce = ""

        try:
            memo_wif = self.crea.wallet.getPrivateKeyForPublicKey(
                memo_to["memo_key"])
            pubkey = memo_from["memo_key"]
        except MissingKeyError:
            try:
                # if that failed, we assume that we have sent the memo
                memo_wif = self.crea.wallet.getPrivateKeyForPublicKey(
                    memo_from["memo_key"])
                pubkey = memo_to["memo_key"]
            except MissingKeyError:
                # if all fails, raise exception
                raise MissingKeyError(
                    "Non of the required memo keys are installed!"
                    "Need any of {}".format(
                        [memo_to["name"], memo_from["name"]]))

        if not hasattr(self, 'chain_prefix'):
            self.chain_prefix = self.crea.prefix

        if message[0] == '#':
            return BtsMemo.decode_memo(PrivateKey(memo_wif), message)
        else:
            return BtsMemo.decode_memo_bts(
                PrivateKey(memo_wif),
                PublicKey(pubkey, prefix=self.chain_prefix), nonce, message)
Esempio n. 5
0
    def test_shared_secrets_equal(self):

        wifs = cycle([x[0] for x in test_shared_secrets])

        for i in range(len(test_shared_secrets)):
            sender_private_key = PrivateKey(next(wifs))
            sender_public_key = sender_private_key.pubkey
            receiver_private_key = PrivateKey(next(wifs))
            receiver_public_key = receiver_private_key.pubkey

            self.assertEqual(
                get_shared_secret(sender_private_key, receiver_public_key),
                get_shared_secret(receiver_private_key, sender_public_key))
 def test_Privatekey_pubkey(self):
     self.assertEqual([
         format(
             PrivateKey(
                 "5HvVz6XMx84aC5KaaBbwYrRLvWE46cH6zVnv4827SBPLorg76oq").
             pubkey, "STX"),
         str(
             PrivateKey(
                 "5HvVz6XMx84aC5KaaBbwYrRLvWE46cH6zVnv4827SBPLorg76oq",
                 prefix="STX").pubkey)
     ], [
         "STX7W5qsanXHgRAZPijbrLMDwX6VmHqUdL2s8PZiYKD5h1R7JaqRJ",
         "STX7W5qsanXHgRAZPijbrLMDwX6VmHqUdL2s8PZiYKD5h1R7JaqRJ"
     ])
 def test_Privatekey_derive(self):
     p1 = PrivateKey("5HvVz6XMx84aC5KaaBbwYrRLvWE46cH6zVnv4827SBPLorg76oq")
     p2 = PrivateKey("5Jete5oFNjjk3aUMkKuxgAXsp7ZyhgJbYNiNjHLvq5xzXkiqw7R")
     self.assertEqual([
         format(p1.child(p2.get_secret()), "STM"),
         format(p2.child(p1.get_secret()), "STM"),
         format(p1.derive_private_key(0), "STM"),
         format(p2.derive_private_key(56), "STM")
     ], [
         "STMZiwJpC7MUmc9gn3vii3XS36nUceYEfKvFC1NLSrjB7ZRQJ7gt",
         "STM24hzNSDZYgm9C85yxJqyk32DwjXg8pCgkGVzB77hvP2XxGDdvr",
         "STM2e99iqVQUFij7Dk2nWVNC1dL8M86q37Nj4KwPHKBu1Yy49HkwA",
         "STMgqaH9RdvUtVk7NFnx4BZJRrNS7Lj35qaueAeYJ3tKEqPaLwa4"
     ])
Esempio n. 8
0
 def encrypt_wif(self, wif):
     """ Encrypt a wif key
     """
     if self.locked():
         raise AssertionError()
     return format(
         bip38.encrypt(PrivateKey(wif, prefix=self.prefix),
                       self.masterpassword), "encwif")
Esempio n. 9
0
 def test_encrypt(self):
     for memo in test_cases:
         enc = encode_memo(PrivateKey(memo["wif"]),
                           PublicKey(memo["to"], prefix="GPH"),
                           memo["nonce"],
                           memo["plain"],
                           prefix="GPH")
         self.assertEqual(memo["message"], enc)
Esempio n. 10
0
    def encrypt(self, memo, bts_encrypt=False):
        """ Encrypt a memo

            :param str memo: clear text memo message
            :returns: encrypted memo
            :rtype: str
        """
        if not memo:
            return None

        nonce = str(random.getrandbits(64))
        memo_wif = self.crea.wallet.getPrivateKeyForPublicKey(
            self.from_account["memo_key"])
        if not memo_wif:
            raise MissingKeyError("Memo key for %s missing!" %
                                  self.from_account["name"])

        if not hasattr(self, 'chain_prefix'):
            self.chain_prefix = self.crea.prefix

        if bts_encrypt:
            enc = BtsMemo.encode_memo_bts(
                PrivateKey(memo_wif),
                PublicKey(self.to_account["memo_key"],
                          prefix=self.chain_prefix), nonce, memo)

            return {
                "message": enc,
                "nonce": nonce,
                "from": self.from_account["memo_key"],
                "to": self.to_account["memo_key"]
            }
        else:
            enc = BtsMemo.encode_memo(PrivateKey(memo_wif),
                                      PublicKey(self.to_account["memo_key"],
                                                prefix=self.chain_prefix),
                                      nonce,
                                      memo,
                                      prefix=self.chain_prefix)

            return {
                "message": enc,
                "from": self.from_account["memo_key"],
                "to": self.to_account["memo_key"]
            }
 def test_btcprivkey(self):
     self.assertEqual([
         format(
             PrivateKey(
                 "5HvVz6XMx84aC5KaaBbwYrRLvWE46cH6zVnv4827SBPLorg76oq").
             uncompressed.address, "BTC"),
         format(
             PrivateKey(
                 "5Jete5oFNjjk3aUMkKuxgAXsp7ZyhgJbYNiNjHLvq5xzXkiqw7R").
             uncompressed.address, "BTC"),
         format(
             PrivateKey(
                 "5KDT58ksNsVKjYShG4Ls5ZtredybSxzmKec8juj7CojZj6LPRF7").
             uncompressed.address, "BTC"),
     ], [
         "1G7qw8FiVfHEFrSt3tDi6YgfAdrDrEM44Z",
         "12c7KAAZfpREaQZuvjC5EhpoN6si9vekqK",
         "1Gu5191CVHmaoU3Zz3prept87jjnpFDrXL",
     ])
Esempio n. 12
0
 def doit(self, printWire=False, ops=None):
     if ops is None:
         ops = [Operation(self.op)]
     tx = Signed_Transaction(ref_block_num=self.ref_block_num,
                             ref_block_prefix=self.ref_block_prefix,
                             expiration=self.expiration,
                             operations=ops)
     tx = tx.sign([self.wif], chain=self.prefix)
     tx.verify([PrivateKey(self.wif, prefix=u"STM").pubkey], self.prefix)
     txWire = hexlify(py23_bytes(tx)).decode("ascii")
    def appendWif(self, wif):
        """ Add a wif that should be used for signing of the transaction.

            :param string wif: One wif key to use for signing
                a transaction.
        """
        if wif:
            try:
                PrivateKey(wif, prefix=self.crea.prefix)
                self.wifs.add(wif)
            except:
                raise InvalidWifError
Esempio n. 14
0
 def decrypt_wif(self, encwif):
     """ decrypt a wif key
     """
     try:
         # Try to decode as wif
         PrivateKey(encwif, prefix=self.prefix)
         return encwif
     except (ValueError, AssertionError):
         pass
     if self.locked():
         raise AssertionError()
     return format(bip38.decrypt(encwif, self.masterpassword), "wif")
Esempio n. 15
0
 def doit(self, printWire=False, ops=None):
     ops = [Operation(ops)]
     tx = Signed_Transaction(ref_block_num=self.ref_block_num,
                             ref_block_prefix=self.ref_block_prefix,
                             expiration=self.expiration,
                             operations=ops)
     start = timer()
     tx = tx.sign([self.wif], chain=self.prefix)
     end1 = timer()
     tx.verify([PrivateKey(self.wif, prefix=u"STM").pubkey], self.prefix)
     end2 = timer()
     return end2 - end1, end1 - start
Esempio n. 16
0
 def _get_pub_from_wif(self, wif):
     """ Get the pubkey as string, from the wif key as string
     """
     # it could be either graphenebase or crea so we can't check
     # the type directly
     if isinstance(wif, PrivateKey):
         wif = str(wif)
     try:
         return format(PrivateKey(wif).pubkey, self.prefix)
     except:
         raise InvalidWifError(
             "Invalid Private Key Format. Please use WIF!")
 def test_Privatekey(self):
     self.assertEqual([
         str(
             PrivateKey(
                 "5HvVz6XMx84aC5KaaBbwYrRLvWE46cH6zVnv4827SBPLorg76oq")),
         str(
             PrivateKey(
                 "5Jete5oFNjjk3aUMkKuxgAXsp7ZyhgJbYNiNjHLvq5xzXkiqw7R")),
         str(
             PrivateKey(
                 "5KDT58ksNsVKjYShG4Ls5ZtredybSxzmKec8juj7CojZj6LPRF7")),
         repr(
             PrivateKey(
                 "5HvVz6XMx84aC5KaaBbwYrRLvWE46cH6zVnv4827SBPLorg76oq")),
         repr(
             PrivateKey(
                 "5Jete5oFNjjk3aUMkKuxgAXsp7ZyhgJbYNiNjHLvq5xzXkiqw7R")),
         repr(
             PrivateKey(
                 "5KDT58ksNsVKjYShG4Ls5ZtredybSxzmKec8juj7CojZj6LPRF7")),
     ], [
         "5HvVz6XMx84aC5KaaBbwYrRLvWE46cH6zVnv4827SBPLorg76oq",
         "5Jete5oFNjjk3aUMkKuxgAXsp7ZyhgJbYNiNjHLvq5xzXkiqw7R",
         "5KDT58ksNsVKjYShG4Ls5ZtredybSxzmKec8juj7CojZj6LPRF7",
         '0e1bfc9024d1f55a7855dc690e45b2e089d2d825a4671a3c3c7e4ea4e74ec00e',
         '6e5cc4653d46e690c709ed9e0570a2c75a286ad7c1bc69a648aae6855d919d3e',
         'b84abd64d66ee1dd614230ebbe9d9c6d66d78d93927c395196666762e9ad69d8'
     ])
Esempio n. 18
0
 def test_sign_message(self, module):
     if module == "cryptography":
         if not ecda.CRYPTOGRAPHY_AVAILABLE:
             return
         ecda.SECP256K1_MODULE = "cryptography"
     elif module == "secp256k1":
         if not ecda.SECP256K1_AVAILABLE:
             return
         ecda.SECP256K1_MODULE = "secp256k1"
     else:
         ecda.SECP256K1_MODULE = module
     pub_key = py23_bytes(repr(PrivateKey(wif).pubkey), "latin")
     signature = ecda.sign_message("Foobar", wif)
     pub_key_sig = ecda.verify_message("Foobar", signature)
     self.assertEqual(hexlify(pub_key_sig), pub_key)
Esempio n. 19
0
 def test_create_account(self):
     bts = self.bts
     name = ''.join(
         random.choice(string.ascii_lowercase) for _ in range(12))
     key1 = PrivateKey()
     key2 = PrivateKey()
     key3 = PrivateKey()
     key4 = PrivateKey()
     key5 = PrivateKey()
     tx = bts.create_account(
         name,
         creator="crea",
         owner_key=format(key1.pubkey, core_unit),
         active_key=format(key2.pubkey, core_unit),
         posting_key=format(key3.pubkey, core_unit),
         memo_key=format(key4.pubkey, core_unit),
         additional_owner_keys=[format(key5.pubkey, core_unit)],
         additional_active_keys=[format(key5.pubkey, core_unit)],
         additional_owner_accounts=["crea1"],  # 1.2.0
         additional_active_accounts=["crea1"],
         storekeys=False)
     self.assertEqual(tx["operations"][0][0], "account_create")
     op = tx["operations"][0][1]
     role = "active"
     self.assertIn(format(key5.pubkey, core_unit),
                   [x[0] for x in op[role]["key_auths"]])
     self.assertIn(format(key5.pubkey, core_unit),
                   [x[0] for x in op[role]["key_auths"]])
     self.assertIn("crea1", [x[0] for x in op[role]["account_auths"]])
     role = "owner"
     self.assertIn(format(key5.pubkey, core_unit),
                   [x[0] for x in op[role]["key_auths"]])
     self.assertIn(format(key5.pubkey, core_unit),
                   [x[0] for x in op[role]["key_auths"]])
     self.assertIn("crea1", [x[0] for x in op[role]["account_auths"]])
     self.assertEqual(op["creator"], "crea")
Esempio n. 20
0
 def test_encrypt_decrypt(self):
     base58 = u'#HU6pdQ4Hh8cFrDVooekRPVZu4BdrhAe9RxrWrei2CwfAApAPdM4PT5mSV9cV3tTuWKotYQF6suyM4JHFBZz4pcwyezPzuZ2na7uwhRcLqFotsqxWRBpaXkNks2QCnYLS8'
     text = u'#爱'
     nonce = u'1462976530069648'
     wif = str(PasswordKey("", "", role="", prefix="STM").get_private_key())
     private_key = PrivateKey(wif=wif, prefix="STM")
     public_key = private_key.pubkey
     cypertext = encode_memo(private_key,
                             public_key,
                             nonce,
                             text,
                             prefix="STM")
     self.assertEqual(cypertext, base58)
     plaintext = decode_memo(private_key, cypertext)
     self.assertEqual(plaintext, text)
Esempio n. 21
0
 def time_btcprivkey(self):
     format(PrivateKey("5HvVz6XMx84aC5KaaBbwYrRLvWE46cH6zVnv4827SBPLorg76oq").uncompressed.address, "BTC")
Esempio n. 22
0
 def test_decrypt_bts(self):
     for memo in test_cases:
         dec = decode_memo_bts(PrivateKey(memo["wif"]),
                               PublicKey(memo["to"], prefix="GPH"),
                               memo["nonce"], memo["message_bts"])
         self.assertEqual(memo["plain"], dec)
 def test_to_wif(self):
     private_key = PrivateKey(key["private_key"])
     self.assertEqual(key["private_key_WIF_format"], str(private_key))
Esempio n. 24
0
 def time_btsprivkey(self):
     format(PrivateKey("5HqUkGuo62BfcJU5vNhTXKJRXuUi9QSE6jp8C3uBJ2BVHtB8WSd").address, "BTS")
Esempio n. 25
0
 def test_shared_secret(self):
     for s in test_shared_secrets:
         priv = PrivateKey(s[0])
         pub = PublicKey(s[1], prefix="GPH")
         shared_secret = get_shared_secret(priv, pub)
         self.assertEqual(s[2], shared_secret)
Esempio n. 26
0
 def time_Privatekey(self):
     str(PrivateKey("5HvVz6XMx84aC5KaaBbwYrRLvWE46cH6zVnv4827SBPLorg76oq"))
 def test_public_from_private(self):
     private_key = PrivateKey(key["private_key"])
     public_key = private_key.get_public_key()
     self.assertEqual(key["public_key"], str(public_key))
Esempio n. 28
0
 def test_decrypt(self):
     for memo in test_cases:
         dec = decode_memo(PrivateKey(memo["wif"]), memo["message"])
         self.assertEqual(memo["plain"], dec)
 def test_calc_pub_key(self):
     private_key = PrivateKey(key["private_key"])
     public_key = private_key.pubkey
     self.assertEqual(key["bts_address"], str(public_key.address))