def encrypt(self, memo, bts_encrypt=False, return_enc_memo_only=False, nonce=None): """ Encrypt a memo :param str memo: clear text memo message :param bool return_enc_memo_only: When True, only the encoded memo is returned :param str nonce: when not set, a random string is generated and used :returns: encrypted memo :rtype: dict """ if not memo: return None if nonce is None: nonce = str(random.getrandbits(64)) if isinstance(self.from_account, Account): memo_wif = self.blockchain.wallet.getPrivateKeyForPublicKey( self.from_account["memo_key"]) memo_wif = PrivateKey(memo_wif) else: memo_wif = self.from_account if isinstance(self.to_account, Account): pubkey = self.to_account["memo_key"] else: pubkey = self.to_account 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.blockchain.prefix if bts_encrypt: enc = BtsMemo.encode_memo_bts( PrivateKey(memo_wif), PublicKey(pubkey, prefix=self.chain_prefix), nonce, memo) return { "message": enc, "nonce": nonce, "from": str(PrivateKey(memo_wif).pubkey), "to": str(pubkey) } else: enc = BtsMemo.encode_memo(PrivateKey(memo_wif), PublicKey(pubkey, prefix=self.chain_prefix), nonce, memo, prefix=self.chain_prefix) if return_enc_memo_only: return enc return { "message": enc, "from": str(PrivateKey(memo_wif).pubkey), "to": str(pubkey) }
def test_encrypt_bts(self): for memo in test_cases: enc = encode_memo_bts( PrivateKey(memo["wif"]), PublicKey(memo["to"], prefix="GPH"), memo["nonce"], memo["plain"], ) self.assertEqual(memo["message_bts"], enc)
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.steem.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.steem.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"] }