Exemple #1
0
 def test_decryt_encrypt(self):
     memo = Memo(from_account=wif, to_account="beembot")
     base_string_length = [1, 2, 3, 4, 5, 7, 8, 9, 15, 16, 17, 32, 63, 64, 65, 127, 255, 511, 1023, 2047, 4095]
     for n in base_string_length:
         test_string = str(random.getrandbits(n))
         ret = memo.encrypt(test_string)
         ret_string = memo.decrypt(ret["message"])
         self.assertEqual(test_string, ret_string[1:])
    def parse_transfer_out_op(self, op):
        amount = Amount(op["amount"], steem_instance=self.steem)
        index = op["index"]
        account = op["from"]
        timestamp = op["timestamp"]
        encrypted = False
        processed_memo = ascii(op["memo"]).replace('\n', '').replace('\\n', '').replace('\\', '')
        if len(processed_memo) > 2 and (processed_memo[0] == '#' or processed_memo[1] == '#' or processed_memo[2] == '#') and account == "steembasicincome":
            if processed_memo[1] == '#':
                processed_memo = processed_memo[1:-1]
            elif processed_memo[2] == '#':
                processed_memo = processed_memo[2:-2]        
            memo = Memo(account, op["to"], steem_instance=self.steem)
            processed_memo = ascii(memo.decrypt(processed_memo)).replace('\n', '')
            encrypted = True
        
        if amount.amount < 1:
            data = {"index": index, "sender": account, "to": op["to"], "memo": processed_memo, "encrypted": encrypted, "referenced_accounts": None, "amount": amount.amount, "amount_symbol": amount.symbol, "timestamp": timestamp}
            self.transactionOutStorage.add(data)            
            return
        if amount.symbol == "SBD":
            # self.trxStorage.get_account(op["to"], share_type="SBD")
            shares = -int(amount.amount)
            if "http" in op["memo"] or "STEEM" not in op["memo"]:
                data = {"index": index, "sender": account, "to": op["to"], "memo": processed_memo, "encrypted": encrypted, "referenced_accounts": None, "amount": amount.amount, "amount_symbol": amount.symbol, "timestamp": timestamp}
                self.transactionOutStorage.add(data)                
                return
            trx = self.trxStorage.get_SBD_transfer(op["to"], shares, formatTimeString(op["timestamp"]))
            sponsee = json.dumps({})
            if trx:
                sponsee = trx["sponsee"]
            self.new_transfer_record(op["index"], processed_memo, op["to"], op["to"], sponsee, shares, op["timestamp"], share_type="Refund")
            # self.new_transfer_record(op["index"], op["to"], "", shares, op["timestamp"], share_type="Refund")
            data = {"index": index, "sender": account, "to": op["to"], "memo": processed_memo, "encrypted": encrypted, "referenced_accounts": sponsee, "amount": amount.amount, "amount_symbol": amount.symbol, "timestamp": timestamp}
            self.transactionOutStorage.add(data)             
            return

        else:
            data = {"index": index, "sender": account, "to": op["to"], "memo": processed_memo, "encrypted": encrypted, "referenced_accounts": None, "amount": amount.amount, "amount_symbol": amount.symbol, "timestamp": timestamp}
            self.transactionOutStorage.add(data)
            return            
    def test_transfer_memo(self):
        bts = self.bts
        bts.nobroadcast = False
        bts.wallet.unlock("123")
        acc = Account("beem", steem_instance=bts)
        tx = acc.transfer("beem1", 1.33, "SBD", memo="#Foobar")
        self.assertEqual(tx["operations"][0][0], "transfer")
        op = tx["operations"][0][1]
        self.assertIn("memo", op)
        self.assertIn("#", op["memo"])
        m = Memo(from_account=op["from"],
                 to_account=op["to"],
                 steem_instance=bts)
        memo = m.decrypt(op["memo"])
        self.assertEqual(memo, "Foobar")

        self.assertEqual(op["from"], "beem")
        self.assertEqual(op["to"], "beem1")
        amount = Amount(op["amount"], steem_instance=bts)
        self.assertEqual(float(amount), 1.33)
        bts.nobroadcast = True
Exemple #4
0
 def test_decrypt_encrypt_file(self):
    
     test_dir = tempfile.mkdtemp()
     outfile = os.path.join(test_dir, 'test.txt')
     outfile_enc = os.path.join(test_dir, 'test_enc.txt')
     test_string = str(random.getrandbits(1000))
     with open(outfile, 'w') as f:
         f.write(test_string)
     memo = Memo(from_account=wif, to_account="beembot")
     memo.encrypt_binary(outfile, outfile_enc)
     memo.decrypt_binary(outfile_enc, outfile)
     with open(outfile, 'r') as f:
         content = f.read()
     self.assertEqual(test_string, content)
     shutil.rmtree(test_dir)
Exemple #5
0
    def __upload_endpoint_auth__(self) -> str:
        if self.protocol == 'IPFS' and self.upload_endpoint in constants.authenticated_ipfs_upload_endpoints:
            loginUrl = self.upload_endpoint + '/login?user='******'&network=' + self.network
            if self.network == 'dtc' and self.custom_keyid != None:
                loginUrl += '&dtckeyid=' + self.custom_keyid
            auth_request = requests.get(loginUrl)
            if auth_request.status_code != 200:
                # TODO: Raise appropriate error message
                raise RuntimeError('Could not authenticate to upload endpoint')

            # Decrypt with Avalon key
            encrypted_memo = auth_request.json()['encrypted_memo']
            decrypted_memo = None
            if self.network == 'dtc':
                decrypted_memo = decrypt.ecies_decrypt(
                    base58.b58decode(self.private_key),
                    decrypt.js_to_py_encrypted(encrypted_memo))
            elif self.network == 'hive':
                decrypted_memo = Memo(blockchain_instance=self.graphene_client
                                      ).decrypt(encrypted_memo)
                decrypted_memo = decrypted_memo[1:len(decrypted_memo)]

            # Obtain access token
            headers = {'Content-Type': 'text/plain'}
            access_token_request = requests.post(self.upload_endpoint +
                                                 '/logincb',
                                                 data=decrypted_memo,
                                                 headers=headers)
            if access_token_request.status_code != 200:
                # TODO: Raise appropriate error
                raise RuntimeError('Could not authenticate to upload endpoint')
            else:
                return access_token_request.json()['access_token']
        elif self.protocol == 'IPFS':
            self.ipfs_api = ipfshttpclient.connect(self.upload_endpoint)
        elif self.protocol == 'Skynet':
            self.skynet_api = skynet.SkynetClient(self.upload_endpoint)
        return 'noauth'
    def parse_transfer_in_op(self, op):
        amount = Amount(op["amount"], steem_instance=self.steem)
        share_type = "Standard"
        index = op["index"]
        account = op["from"]
        timestamp = op["timestamp"]
        sponsee = {}
        processed_memo = ascii(op["memo"]).replace('\n', '').replace('\\n', '').replace('\\', '')
        if len(processed_memo) > 2 and (processed_memo[0] == '#' or processed_memo[1] == '#' or processed_memo[2] == '#') and account == "steembasicincome":
            if processed_memo[1] == '#':
                processed_memo = processed_memo[1:-1]
            elif processed_memo[2] == '#':
                processed_memo = processed_memo[2:-2]        
            memo = Memo(account, op["to"], steem_instance=self.steem)
            processed_memo = ascii(memo.decrypt(processed_memo)).replace('\n', '')

        shares = int(amount.amount)
        if processed_memo.lower().replace(',', '  ').replace('"', '') == "":
            self.new_transfer_record(index, processed_memo, account, account, json.dumps(sponsee), shares, timestamp)
            return
        [sponsor, sponsee, not_parsed_words, account_error] = self.memo_parser.parse_memo(processed_memo, shares, account)        
        if amount.amount < 1:
            data = {"index": index, "sender": account, "to": self.account["name"], "memo": processed_memo, "encrypted": False, "referenced_accounts": sponsor + ";" + json.dumps(sponsee), "amount": amount.amount, "amount_symbol": amount.symbol, "timestamp": timestamp}
            self.transactionStorage.add(data)
            return
        if amount.symbol == "SBD":
            share_type = "SBD"
        
        sponsee_amount = 0
        for a in sponsee:
            sponsee_amount += sponsee[a]
        
        
        if sponsee_amount == 0 and not account_error and True:
            sponsee_account = self.get_highest_avg_share_age_account()
            sponsee = {sponsee_account: shares}
            print("%s sponsers %s with %d shares" % (sponsor, sponsee_account, shares))
            self.new_transfer_record(index, processed_memo, account, sponsor, json.dumps(sponsee), shares, timestamp, share_type=share_type)
            self.memberStorage.update_avg_share_age(sponsee_account, 0)
            self.member_data[sponsee_account]["avg_share_age"] = 0            
            return
        elif sponsee_amount == 0 and not account_error:
            sponsee = {}
            message = op["timestamp"] + " to: " + self.account["name"] + " from: " + sponsor + ' amount: ' + str(amount) + ' memo: ' + processed_memo + '\n'
            self.new_transfer_record(index, processed_memo, account, sponsor, json.dumps(sponsee), shares, timestamp, status="LessOrNoSponsee", share_type=share_type)
            return
        if sponsee_amount != shares and not account_error and True:
            sponsee_account = self.get_highest_avg_share_age_account()
            sponsee_shares = shares-sponsee_amount
            if sponsee_shares > 0:
                sponsee = {sponsee_account: sponsee_shares}
                print("%s sponsers %s with %d shares" % (sponsor, sponsee_account, sponsee_shares))
                self.new_transfer_record(index, processed_memo, account, sponsor, json.dumps(sponsee), shares, timestamp, share_type=share_type)
                self.memberStorage.update_avg_share_age(sponsee_account, 0)
                self.member_data[sponsee_account]["avg_share_age"] = 0
                return
            else:
                sponsee = {}
                self.new_transfer_record(index, processed_memo, account, sponsor, json.dumps(sponsee), shares, timestamp, status="LessOrNoSponsee", share_type=share_type)
                return
        elif sponsee_amount != shares and not account_error:
            message = op["timestamp"] + " to: " + self.account["name"] + " from: " + sponsor + ' amount: ' + str(amount) + ' memo: ' + ascii(op["memo"]) + '\n'
            self.new_transfer_record(index, processed_memo, account, sponsor, json.dumps(sponsee), shares, timestamp, status="LessOrNoSponsee", share_type=share_type)            

            return        
        if account_error:
            message = op["timestamp"] + " to: " + self.account["name"] + " from: " + sponsor + ' amount: ' + str(amount) + ' memo: ' + ascii(op["memo"]) + '\n'
            self.new_transfer_record(index, processed_memo, account, sponsor, json.dumps(sponsee), shares, timestamp, status="AccountDoesNotExist", share_type=share_type)

            return
        
        self.new_transfer_record(index, processed_memo, account, sponsor, json.dumps(sponsee), shares, timestamp, share_type=share_type)
Exemple #7
0
            if op["status"] != "LessOrNoSponsee":
                continue
            processed_memo = ascii(op["memo"]).replace('\n', '')
            processed_memo = ascii(op["memo"]).replace('\n', '')
            if processed_memo[1] == '#':
                processed_memo = processed_memo[1:-1]
            if processed_memo[2] == '#':
                processed_memo = processed_memo[2:-2]
            print("processed_memo: %s, source: %s" %
                  (processed_memo, op["source"]))
            if len(processed_memo) > 1 and (
                    processed_memo[0] == '#' or processed_memo[1]
                    == '#') and op["source"] == "steembasicincome":

                print("found: %s" % processed_memo)
                memo = Memo(op["source"], op["account"], steem_instance=stm)
                processed_memo = ascii(memo.decrypt(processed_memo)).replace(
                    '\n', '')
                print("decrypt memo %s" % processed_memo)
                trxStorage.update_memo(op["source"], op["account"], op["memo"],
                                       processed_memo)
    if False:  # deal with encrypted memos
        print("check for encrypted memos")

        for op in transactionStorage.get_all():

            processed_memo = ascii(op["memo"]).replace('\n', '')
            processed_memo = ascii(op["memo"]).replace('\n', '')
            if processed_memo[1] == '#':
                processed_memo = processed_memo[1:-1]
            if processed_memo[2] == '#':