Example #1
0
    def decrypt(self, memo):
        """ Decrypt a memo

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

        memo_wif = self.karma.wallet.getPrivateKeyForPublicKey(
            self.to_account["options"]["memo_key"])
        if not memo_wif:
            raise MissingKeyError("Memo key for %s missing!" %
                                  self.to_account["name"])

        # TODO: Use pubkeys of the message, not pubkeys of account!
        print(
            PrivateKey(memo_wif),
            PublicKey(self.from_account["options"]["memo_key"],
                      prefix=self.karma.rpc.chain_params["prefix"]),
            memo.get("nonce"), memo.get("message"))
        return KARMAMemo.decode_memo(
            PrivateKey(memo_wif),
            PublicKey(self.from_account["options"]["memo_key"],
                      prefix=self.karma.rpc.chain_params["prefix"]),
            memo.get("nonce"), memo.get("message"))
Example #2
0
    def compareConstructedTX(self):
        # FIXME: Bet cancel does not serialize properly
        op = operations.Bet_cancel(
            **{
                "fee": {
                    "amount": 0,
                    "asset_id": "1.3.0"
                },
                "bettor_id": "1.2.5555",
                "bet_to_cancel": "1.22.1111",
                "prefix": prefix,
            })
        ops = [Operation(op)]
        tx = Signed_Transaction(ref_block_num=ref_block_num,
                                ref_block_prefix=ref_block_prefix,
                                expiration=expiration,
                                operations=ops)
        tx = tx.sign([wif], chain=prefix)
        tx.verify([PrivateKey(wif).pubkey], prefix)
        txWire = hexlify(bytes(tx)).decode("ascii")
        print("=" * 80)
        pprint(tx.json())
        print("=" * 80)

        from grapheneapi.grapheneapi import GrapheneAPI
        rpc = GrapheneAPI("localhost", 8092)
        compare = rpc.serialize_transaction(tx.json())
        print(compare[:-130])
        print(txWire[:-130])
        print(txWire[:-130] == compare[:-130])
        self.assertEqual(compare[:-130], txWire[:-130])
Example #3
0
    def encrypt(self, memo):
        """ 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.karma.wallet.getPrivateKeyForPublicKey(
            self.from_account["options"]["memo_key"])
        if not memo_wif:
            raise MissingKeyError("Memo key for %s missing!" %
                                  self.from_account["name"])

        enc = KARMAMemo.encode_memo(
            PrivateKey(memo_wif),
            PublicKey(self.to_account["options"]["memo_key"],
                      prefix=self.karma.rpc.chain_params["prefix"]), nonce,
            memo)

        return {
            "message": enc,
            "nonce": nonce,
            "from": self.from_account["options"]["memo_key"],
            "to": self.to_account["options"]["memo_key"]
        }
Example #4
0
 def test_event_group_create(self):
     op = operations.Event_group_create(
         **{
             "fee": {
                 "amount": 0,
                 "asset_id": "1.3.0"
             },
             "name": [["en", "NHL"], ["zh_Hans", "國家冰球聯盟"]],
             "sport_id": "1.0.1241",
             "prefix": prefix,
         })
     ops = [Operation(op)]
     tx = Signed_Transaction(ref_block_num=ref_block_num,
                             ref_block_prefix=ref_block_prefix,
                             expiration=expiration,
                             operations=ops)
     tx = tx.sign([wif], chain=prefix)
     tx.verify([PrivateKey(wif).pubkey], prefix)
     txWire = hexlify(bytes(tx)).decode("ascii")
     compare = ("f68585abf4dce7c80457012f000000000000000000020265"
                "6e034e484c077a685f48616e7312e59c8be5aeb6e586b0e7"
                "9083e881afe79b9fd9040000000000010000011f15a6cb74"
                "7df1d9bb3e7e607d8094624b3f27010ad461dd8d833590b0"
                "e0f72c946082083f90686e9bc225f5b28fd50c63c8150b8c"
                "45956c336a182702e17d800d")
     self.assertEqual(compare[:-130], txWire[:-130])
Example #5
0
 def test_event_create(self):
     op = operations.Event_create(
         **{
             "fee": {
                 "amount": 0,
                 "asset_id": "1.3.0"
             },
             "season": [["en", "2016-17"]],
             "start_time": "2017-03-29T09:15:05",
             "event_group_id": "1.0.1241",
             "competitors": ["0.0.0", "0.0.1"],
             "prefix": prefix,
         })
     ops = [Operation(op)]
     tx = Signed_Transaction(ref_block_num=ref_block_num,
                             ref_block_prefix=ref_block_prefix,
                             expiration=expiration,
                             operations=ops)
     tx = tx.sign([wif], chain=prefix)
     tx.verify([PrivateKey(wif).pubkey], prefix)
     txWire = hexlify(bytes(tx)).decode("ascii")
     compare = ("f68585abf4dce7c8045701300000000000000000000001026"
                "56e07323031362d313701197bdb58d9040000000000010200"
                "0000000000000001000000000000000000011f2f6eccc426e"
                "56925b29293610505598a8580dc56fc594fef541d25a7cfc8"
                "120d7b82dc6fb929bd6bb1fb8f2ab976559ea29c9adc7a9b5"
                "df9788b909c7fafe232")
     self.assertEqual(compare[:-130], txWire[:-130])
Example #6
0
 def test_competitor_create(self):
     op = operations.Competitor_create(
         **{
             "fee": {
                 "amount": 0,
                 "asset_id": "1.3.0"
             },
             "name": [["en", "Fuerth"], ["de", "Greuther Fürth"]],
             "sport_id": "1.0.1241",
             "prefix": prefix,
         })
     ops = [Operation(op)]
     tx = Signed_Transaction(ref_block_num=ref_block_num,
                             ref_block_prefix=ref_block_prefix,
                             expiration=expiration,
                             operations=ops)
     tx = tx.sign([wif], chain=prefix)
     tx.verify([PrivateKey(wif).pubkey], prefix)
     txWire = hexlify(bytes(tx)).decode("ascii")
     compare = ("f68585abf4dce7c80457012e000000000000000000020264"
                "650f47726575746865722046c3bc72746802656e06467565"
                "727468d9040000000000010000012068e0417811ad383306"
                "1dde84281b17daf22e8dfe879506ee06eb6d9c324297a811"
                "e04fa8e5983001bb7eee47d3e9fd0e7c48590c10abfa6a6f"
                "2a33ed50a2d424")
     self.assertEqual(compare[:-130], txWire[:-130])
Example #7
0
 def appendWif(self, wif):
     """ Add a wif that should be used for signing of the transaction.
     """
     if wif:
         try:
             PrivateKey(wif)
             self.wifs.append(wif)
         except:
             raise InvalidWifError
Example #8
0
 def decrypt_wif(self, encwif):
     """ decrypt a wif key
     """
     try:
         # Try to decode as wif
         PrivateKey(encwif)
         return encwif
     except:
         pass
     assert not self.locked()
     return format(bip38.decrypt(encwif, self.masterpassword), "wif")
Example #9
0
    def test_Transfer(self):
        pub = format(account.PrivateKey(wif).pubkey, prefix)
        from_account_id = "1.2.0"
        to_account_id = "1.2.1"
        amount = 1000000
        asset_id = "1.3.4"
        message = "abcdefgABCDEFG0123456789"
        nonce = "5862723643998573708"

        fee = objects.Asset(amount=0, asset_id="1.3.0")
        amount = objects.Asset(amount=int(amount), asset_id=asset_id)
        encrypted_memo = memo.encode_memo(
            account.PrivateKey(wif), account.PublicKey(pub, prefix=prefix),
            nonce, message)
        op = operations.Transfer(
            **{
                "fee": fee,
                "from": from_account_id,
                "to": to_account_id,
                "amount": amount,
                "memo": {
                    "from": pub,
                    "to": pub,
                    "nonce": nonce,
                    "message": encrypted_memo,
                },
                "prefix": prefix
            })
        ops = [Operation(op)]
        tx = Signed_Transaction(ref_block_num=ref_block_num,
                                ref_block_prefix=ref_block_prefix,
                                expiration=expiration,
                                operations=ops)
        tx = tx.sign([wif], chain=prefix)
        tx.verify([PrivateKey(wif).pubkey], prefix)
        txWire = hexlify(bytes(tx)).decode("ascii")

        compare = ("f68585abf4dce7c804570100000000000000000000000140420"
                   "f0000000000040102c0ded2bc1f1305fb0faac5e6c03ee3a192"
                   "4234985427b6167ca569d13df435cf02c0ded2bc1f1305fb0fa"
                   "ac5e6c03ee3a1924234985427b6167ca569d13df435cf8c94d1"
                   "9817945c5120fa5b6e83079a878e499e2e52a76a7739e9de409"
                   "86a8e3bd8a68ce316cee50b210000011f39e3fa7071b795491e"
                   "3b6851d61e7c959be92cc7deb5d8491cf1c3c8c99a1eb44553c"
                   "348fb8f5001a78b18233ac66727e32fc776d48e92d9639d64f6"
                   "8e641948")
        self.assertEqual(compare[:-130], txWire[:-130])
Example #10
0
    def addPrivateKey(self, wif):
        """ Add a private key to the wallet database
        """
        # it could be either graphenebase or karmabase so we can't check the type directly
        if isinstance(wif, PrivateKey) or isinstance(wif, GPHPrivateKey):
            wif = str(wif)
        try:
            pub = format(PrivateKey(wif).pubkey, self.prefix)
        except:
            raise InvalidWifError(
                "Invalid Private Key Format. Please use WIF!")

        if self.keyStorage:
            # Test if wallet exists
            if not self.created():
                raise NoWalletException
            self.keyStorage.add(self.encrypt_wif(wif), pub)
Example #11
0
    def setKeys(self, loadkeys):
        """ This method is strictly only for in memory keys that are
            passed to Wallet/karma with the ``keys`` argument
        """
        log.debug(
            "Force setting of private keys. Not using the wallet database!")
        if isinstance(loadkeys, dict):
            Wallet.keyMap = loadkeys
            loadkeys = list(loadkeys.values())
        elif not isinstance(loadkeys, list):
            loadkeys = [loadkeys]

        for wif in loadkeys:
            try:
                key = PrivateKey(wif)
            except:
                raise InvalidWifError
            Wallet.keys[format(key.pubkey, self.prefix)] = str(key)
Example #12
0
 def test_create_proposal(self):
     op = operations.Proposal_create(
         **{
             "fee": {
                 "amount": 0,
                 "asset_id": "1.3.0"
             },
             "fee_paying_account":
             "1.2.0",
             "expiration_time":
             "1970-01-01T00:00:00",
             "proposed_ops": [{
                 "op": [
                     0, {
                         "fee": {
                             "amount": 0,
                             "asset_id": "1.3.0"
                         },
                         "from": "1.2.0",
                         "to": "1.2.0",
                         "amount": {
                             "amount": 0,
                             "asset_id": "1.3.0"
                         },
                         "extensions": []
                     }
                 ]
             }],
             "extensions": []
         })
     ops = [Operation(op)]
     tx = Signed_Transaction(ref_block_num=ref_block_num,
                             ref_block_prefix=ref_block_prefix,
                             expiration=expiration,
                             operations=ops)
     tx = tx.sign([wif], chain=prefix)
     tx.verify([PrivateKey(wif).pubkey], prefix)
     txWire = hexlify(bytes(tx)).decode("ascii")
     compare = ("f68585abf4dce7c80457011600000000000000000000000000"
                "00010000000000000000000000000000000000000000000000"
                "00000001204baf7f11a7ff12337fc097ac6e82e7b68f82f02c"
                "c7e24231637c88a91ae5716674acec8a1a305073165c65e520"
                "a64769f5f62c0301ce21ab4f7c67a6801b4266")
     self.assertEqual(compare[:-130], txWire[:-130])
Example #13
0
 def test_upgrade_account(self):
     op = operations.Account_upgrade(
         **{
             "fee": {
                 "amount": 0,
                 "asset_id": "1.3.0"
             },
             "account_to_upgrade": "1.2.0",
             "upgrade_to_lifetime_member": True,
             "prefix": prefix,
         })
     ops = [Operation(op)]
     tx = Signed_Transaction(ref_block_num=ref_block_num,
                             ref_block_prefix=ref_block_prefix,
                             expiration=expiration,
                             operations=ops)
     tx = tx.sign([wif], chain=prefix)
     tx.verify([PrivateKey(wif).pubkey], prefix)
     txWire = hexlify(bytes(tx)).decode("ascii")
     compare = ("f68585abf4dce7c804570108000000000000000000000100000"
                "11f4e42562ada1d3fed8f8eb51dd58117e3a4024959c46955a0"
                "0d2a7e7e8b40ae7204f4617913aaaf028248d43e8c3463b8776"
                "0ca569007dba99a2c49de75bd69b3")
     self.assertEqual(compare[:-130], txWire[:-130])
Example #14
0
 def test_sport_create(self):
     op = operations.Sport_create(
         **{
             "fee": {
                 "amount": 0,
                 "asset_id": "1.3.0"
             },
             "name": [["en", "Football"], ["de", "Fußball"]],
             "prefix": prefix,
         })
     ops = [Operation(op)]
     tx = Signed_Transaction(ref_block_num=ref_block_num,
                             ref_block_prefix=ref_block_prefix,
                             expiration=expiration,
                             operations=ops)
     tx = tx.sign([wif], chain=prefix)
     tx.verify([PrivateKey(wif).pubkey], prefix)
     txWire = hexlify(bytes(tx)).decode("ascii")
     compare = ("f68585abf4dce7c80457012d000000000000000000020264650"
                "84675c39f62616c6c02656e08466f6f7462616c6c0000011f14"
                "eb892a0c4a6c28e0a54852d45526b0d8e017edc4eaac1c0c54c"
                "6f944ff1d7e5e909157257fe6e71a4532563564c8c063f38846"
                "5f8a9420459d5b6cb1fdfcc1")
     self.assertEqual(compare[:-130], txWire[:-130])
Example #15
0
 def test_proposal_update(self):
     op = operations.Proposal_update(
         **{
             'fee_paying_account': "1.2.1",
             'proposal': "1.10.90",
             'active_approvals_to_add': ["1.2.5"],
             "fee": {
                 "amount": 0,
                 "asset_id": "1.3.0"
             },
         })
     ops = [Operation(op)]
     tx = Signed_Transaction(ref_block_num=ref_block_num,
                             ref_block_prefix=ref_block_prefix,
                             expiration=expiration,
                             operations=ops)
     tx = tx.sign([wif], chain=prefix)
     tx.verify([PrivateKey(wif).pubkey], prefix)
     txWire = hexlify(bytes(tx)).decode("ascii")
     compare = ("f68585abf4dce7c804570117000000000000000000015a01050000000"
                "000000001200b28528d1436564f4b4a9faf38b77c17d69ea85476076e"
                "bafebbad9733ac014411b044a7570ef103a518d2e17f7250e1cd8e31c"
                "f19272395c8fe9bbce0b4bfb4")
     self.assertEqual(compare[:-130], txWire[:-130])
Example #16
0
 def test_create_account(self):
     s = {
         "fee": {
             "amount": 1467634,
             "asset_id": "1.3.0"
         },
         "registrar": "1.2.33",
         "referrer": "1.2.27",
         "referrer_percent": 3,
         "name": "foobar-f124",
         "owner": {
             "weight_threshold":
             1,
             "account_auths": [],
             'key_auths':
             [[
                 prefix +
                 '6pbVDAjRFiw6fkiKYCrkz7PFeL7XNAfefrsREwg8MKpJ9VYV9x', 1
             ],
              [
                  prefix +
                  '6zLNtyFVToBsBZDsgMhgjpwysYVbsQD6YhP3kRkQhANUB4w7Qp', 1
              ]],
             "address_auths": []
         },
         "active": {
             "weight_threshold":
             1,
             "account_auths": [],
             'key_auths':
             [[
                 prefix +
                 '6pbVDAjRFiw6fkiKYCrkz7PFeL7XNAfefrsREwg8MKpJ9VYV9x', 1
             ],
              [
                  prefix +
                  '6zLNtyFVToBsBZDsgMhgjpwysYVbsQD6YhP3kRkQhANUB4w7Qp', 1
              ],
              [
                  prefix +
                  '8CemMDjdUWSV5wKotEimhK6c4dY7p2PdzC2qM1HpAP8aLtZfE7', 1
              ]],
             "address_auths": []
         },
         "options": {
             "memo_key":
             prefix + '5TPTziKkLexhVKsQKtSpo4bAv5RnB8oXcG4sMHEwCcTf3r7dqE',
             "voting_account": "1.2.5",
             "num_witness": 0,
             "num_committee": 0,
             "votes": [],
             "extensions": []
         },
         "extensions": {},
         "prefix": prefix
     }
     op = operations.Account_create(**s)
     ops = [Operation(op)]
     tx = Signed_Transaction(ref_block_num=ref_block_num,
                             ref_block_prefix=ref_block_prefix,
                             expiration=expiration,
                             operations=ops)
     tx = tx.sign([wif], chain=prefix)
     tx.verify([PrivateKey(wif).pubkey], prefix)
     txWire = hexlify(bytes(tx)).decode("ascii")
     compare = ("f68585abf4dce7c804570105f26416000000000000211b03000b666f"
                "6f6261722d6631323401000000000202fe8cc11cc8251de6977636b5"
                "5c1ab8a9d12b0b26154ac78e56e7c4257d8bcf6901000314aa202c91"
                "58990b3ec51a1aa49b2ab5d300c97b391df3beb34bb74f3c62699e01"
                "000001000000000303b453f46013fdbccb90b09ba169c388c34d8445"
                "4a3b9fbec68d5a7819a734fca0010002fe8cc11cc8251de6977636b5"
                "5c1ab8a9d12b0b26154ac78e56e7c4257d8bcf6901000314aa202c91"
                "58990b3ec51a1aa49b2ab5d300c97b391df3beb34bb74f3c62699e01"
                "0000024ab336b4b14ba6d881675d1c782912783c43dbbe31693aa710"
                "ac1896bd7c3d61050000000000000000011f61ad276120bc3f189296"
                "2bfff7db5e8ce04d5adec9309c80529e3a978a4fa1073225a6d56929"
                "e34c9d2a563e67a8f4a227e4fadb4a3bb6ec91bfdf4e57b80efd")
     self.assertEqual(compare[:-130], txWire[:-130])
Example #17
0
def randomwif(prefix, num):
    t = PrettyTable(["wif", "pubkey"])
    for n in range(0, num):
        wif = PrivateKey()
        t.add_row([str(wif), format(wif.pubkey, prefix)])
    click.echo(str(t))
Example #18
0
 def encrypt_wif(self, wif):
     """ Encrypt a wif key
     """
     assert not self.locked()
     return format(bip38.encrypt(PrivateKey(wif), self.masterpassword),
                   "encwif")
Example #19
0
 def getAccountFromPrivateKey(self, wif):
     """ Obtain account name from private key
     """
     pub = format(PrivateKey(wif).pubkey, self.prefix)
     return self.getAccountFromPublicKey(pub)