def test_valid_address_to_hex(self): address = 'qHmBaB5dWsxWtMh3MNh6B427L47DWavgJD' hex_address = '023b5269b46be13e6ac5c3f122ed263e9fa78114' self.assertEqual(Qtum.address_to_hex(address), hex_address) self.assertEqual(Qtum.hex_to_qtum_address(hex_address, mainnet=False), address)
def balance_put(self, data): txid = data[0] address = data[1] address = Qtum.hex_to_qtum_address(address, mainnet=False) ammount = data[2] check = self.balance.get_balance(address, "PUT") if type(check) == list: update_data_1 = self.balance.inc_balance(address, ammount, "PUT")
def test_addresses(self): entropy = "3123213213213123312c3kjifj3" private_key = "7a6be1df9cc5d88edce5443ef0fce246123295dd82afae9a57986543272157cc" wif = "L1KgWtY57mSggocxDVSDRGvLVCRYuQfj8ur7cHvuv6UkgJmXweti" public_key = "021ad7138370ef5e93fb243aff3373e2b92383818dfc20022841b655e0cd6c618c" uncompressed_public_key = "041ad7138370ef5e93fb243aff3373e2b92383818dfc20022841b655e0cd6c618cd578261c78e1adfe205c3ade8b81e1722d6058be9155eee55468fbb04b62040e" qtum_address = "QLonXSbmVhECBV3fqN3L7H9LJn8jUS3m9k" q = Qtum("3123213213213123312c3kjifj3") self.assertEqual(q.get_private_key(), private_key) self.assertEqual(q.get_wif(), wif) self.assertEqual(q.get_public_key(), public_key) self.assertEqual(q.get_uncompressed_public_key(), uncompressed_public_key) self.assertEqual(q.get_qtum_address(), qtum_address) self.assertEqual(q.private_key_to_wif(private_key), wif) self.assertEqual(q.wif_to_private_key(wif), private_key)
def test_sing_verify_invalid(self): q = Qtum('1232131234324324324234321231') public_key = q.get_uncompressed_public_key() private_key = q.get_private_key() message = 'hello' signature = Qtum.sign_message(message, private_key) message = 'corrupted' with self.assertRaises(BadSignatureError): Qtum.verify_message(message, signature, public_key)
def test_sing_verify(self): q = Qtum('1232131234324324324234321231') public_key = q.get_uncompressed_public_key() private_key = q.get_private_key() message = 'hello' signature = Qtum.sign_message(message, private_key) self.assertTrue(Qtum.verify_message(message, signature, public_key))
def test_init_from_wif(self): private_key = '5907a2e9e917fbb8ab93a4d309184d1fe2cd55030f1e616620ef794e6a5b0df5' public_key = '0356fd892d76117935853466db2bf0ac5d0eb9138bfa78c3b25d68f1b64f9a5106' uncompressed_public_key = '0456fd892d76117935853466db2bf0ac5d0eb9138bfa78c3b25d68f1b64f9a51068a6b2f8b4c80c32f6e898d06b765c3a95a2eb372dcbbf2d086bce89b4f256dcb' wif = 'cQZmGLJZAMpNY36YFKbH6gygW2eBDULpokkeTYdPg41fo4BqycEc' qtum_address = 'qgNj57RLaEsLQUpD5JZxZ6tREonrfp5z2i' hex_address = 'fa46f119b82df651edb8a7bff2172dccc7cfac9a' q = Qtum({'wif': wif}, mainnet=False) self.assertEqual(q.get_public_key(), public_key) self.assertEqual(q.get_uncompressed_public_key(), uncompressed_public_key) self.assertEqual(q.get_qtum_address(), qtum_address) self.assertEqual(q.get_hex_address(), hex_address) self.assertEqual(q.get_wif(), wif) self.assertEqual(q.get_private_key(), private_key)
async def wrapper(*args, **kwargs): import settings with open(os.path.join(settings.BASE_DIR, "keys.json")) as f: keys = json.load(f) pubkey = keys["pubkey"] message = kwargs.get("message") signature = kwargs.get("signature") try: flag = Qtum.verify_message(message, signature, pubkey) except: flag = None if not flag: result = {"error": 403, "reason": "Invalid signature"} else: result = await func(*args, **kwargs) return result
async def wrapper(*args, **kwargs): # Keys.json is file with public and private keys at projects directory with open("keys.json") as f: keys = json.load(f) pubkey = keys["pubkey"] message = kwargs.get("message") signature = kwargs.get("signature") try: flag = Qtum.verify_message(message, signature, pubkey) except: flag = None if not flag: result = {"error": 403, "reason": "Invalid signature"} else: result = await func(*args, **kwargs) return result return wrapper
def balance_put(self, data): txid = data[0] address = data[1] address = Qtum.hex_to_qtum_address(address, mainnet=mainnet_status) amount = data[2] gasLimit = data[4] gasPrice = data[5] result = self.db.check_address(address, self.coinid_put) if result: update_data_1 = self.balance.inc_balance(address, amount, self.coinid_put) self.client.log_transaction( **{ "coinid": self.coinid_put, "blocknumber": self.from_block, "blockhash": self.block_hash_num(), "vin": [], "vout": [{ address: amount }], "txid": txid, "gasLimit": gasLimit, "gasPrice": gasPrice })
def transaction_out(self, vout, txid): # parsing output try: list_address = [False] for vout_i in vout: try: script_pub_key = vout_i["scriptPubKey"] types = script_pub_key["type"] if types == "call" and self.coinid == coin_id: asm = script_pub_key["asm"] asm_split = asm.split() gasLimit = asm_split[1] gasPrice = asm_split[2] asm_data = asm_split[3] hex_address = asm_data[:8] smart_contr_address = asm_split[4] if smart_contr_address in address_smart_contract and hex_address == sign_transfer: data = asm_data[8:] signatures_list_type = ['address', 'uint'] try: decode = self.abi_to_params( data, signatures_list_type) new_decode = self.change_decode( signatures_list_type, decode) address_token = new_decode[0] value_int = new_decode[1] address_token = Qtum.hex_to_qtum_address( address_token, mainnet=mainnet_status) result = self.db.check_address( address=address_token, coinid=coin_id_put) result_keys = result.keys() if "address" in result_keys: update_data_1 = self.client.inc_balance( address_token, value_int, coin_id_put) self.storge.log_transaction( **{ "coinid": coin_id_put, "blocknumber": self.from_block, "blockhash": self.block_hash_num(), "vin": [], "vout": [{ address_token: value_int }], "txid": txid, "gasLimit": gasLimit, "gasPrice": gasPrice }) except Exception as e: # print(e) pass addresses = script_pub_key["addresses"] value = vout_i["value"] value_int = int(value * (10**8)) for adr in addresses: data = self.db.check_address(adr, self.coinid) result_keys = data.keys() if "address" in result_keys: update_data_1 = self.client.inc_balance( adr, value_int, coin_id) list_address[0] = True list_address += [{adr: value_int}] except: pass return list_address except: pass
def test_valid_signature3(self): message = 'aaa' public_key = '030646d16f7bb84333446266a7237eed33866b3478caad3da040e0ca63e7b3f1c0' signature = '304402201c1ee66e98a67e4da411eacc2388b4e33cfd6da37a11340119e2cb2c76064aa0022049c00a810b896445f7508d0499845eaf3b904661cd427e0a19ab54a50e9012ad' self.assertTrue(Qtum.verify_message(message, signature, public_key))
def test_valid_address(self): wif = 'cQZmGLJZAMpNY36YFKbH6gygW2eBDULpokkeTYdPg41fo4BqycEc' q = Qtum({'wif': wif}, mainnet=False) self.assertTrue(Qtum.verify_address(q.get_qtum_address()))
class Account(GenesisClass): balance = Balance() blockchain = Blockchain() permissions = Permissions() mailer = Mail() client_storage = SignedTornadoClient(settings.storageurl) client_withdraw = TornadoClient(settings.withdrawhost) validator = { "QTUM": lambda x: Qtum.public_key_to_hex_address(x), "ETH": lambda x: R8_Ethereum.public_key_to_checksum_address(x), "PUT": lambda x: Qtum.public_key_to_hex_address(x), } withdraw_address = { "PUTTEST": lambda x: bip32addresses.Bip32Addresses.address_to_hex(x), "QTUMTEST": lambda x: x } ident_offer = {0: "read_access", 1: "write_access"} async def withdraw_fee(self, coinid): if not isinstance(coinid, str): return { "error": 400, "reason": "Coinid type error. Must be string." } fees = { "BTCTEST": 0.001 * pow(10, 8), "ETH": 0.01 * pow(10, 8), "QTUMTEST": 0.5 * pow(10, 8), "PUTTEST": 50 * pow(10, 8), "LTCTEST": 0.05 * pow(10, 8) } try: fee = fees[coinid] except KeyError: fee = 0 return fee async def logsource(self, **params): try: result = await self.client_storage.request(method_name="logsource", **params) except Exception as e: result = {"error": 500, "reason": str(e)} return result async def createaccount(self, **params): try: result = await self.client_storage.request( method_name="createaccount", **params) except Exception as e: result = {"error": 500, "reason": str(e)} return result async def getaccountdata(self, **params): result = await self.client_storage.request( method_name="getaccountdata", **params) return result async def createwallet(self, **params): result = await self.client_storage.request(method_name="createwallet", **params) return result async def getnews(self, **params): result = await self.client_storage.request(method_name="getnews", **params) return result async def setnews(self, **params): result = await self.client_storage.request(method_name="setnews", **params) return result async def getaccountbywallet(self, **params): result = await self.client_storage.request( method_name="getaccountbywallet", **params) return result async def updatelevel(self, **params): result = await self.client_storage.request(method_name="updatelevel", **params) return result async def insertoffer(self, **params): result = await self.client_storage.request(method_name="insertoffer", **params) return result async def getoffer(self, **params): result = await self.client_storage.request(method_name="getoffer", **params) return result async def removeoffer(self, **params): result = await self.client_storage.request(method_name="removeoffer", **params) return result async def updateoffer(self, **params): result = await self.client_storage.request(method_name="updateoffer", **params) return result async def mailedconfirm(self, **params): result = await self.client_storage.request(method_name="mailedconfirm", **params) return result async def getoffers(self, **params): result = await self.client_storage.request(method_name="getoffers", **params) return result async def getuserscontent(self, **params): result = await self.client_storage.request( method_name="getuserscontent", **params) return result async def setuserscontent(self, **params): result = await self.client_storage.request( method_name="setuserscontent", **params) return result async def updateuserscontent(self, **params): result = await self.client_storage.request( method_name="updateuserscontent", **params) return result async def getallcontent(self): result = await self.client_storage.request(method_name="getallcontent") return result async def getsinglecontent(self, cid): result = await self.client_storage.request( method_name="getsinglecontent", cid=cid) return result async def changecontentowner(self, **params): result = await self.client_storage.request( method_name="changecontentowner", **params) return result async def setaccessstring(self, **params): result = await self.client_storage.request( method_name="setaccessstring", **params) return result async def getreviews(self, **params): result = await self.client_storage.request(method_name="getreviews", **params) return result async def setreview(self, **params): result = await self.client_storage.request(method_name="setreview", **params) return result async def updatereview(self, **params): result = await self.client_storage.request(method_name="updatereview", **params) return result async def writedeal(self, **params): result = await self.client_storage.request(method_name="writedeal", **params) return result async def getdeals(self, **params): result = await self.client_storage.request(method_name="getdeals", **params) return result async def updatedescription(self, **params): result = await self.client_storage.request( method_name="updatedescription", **params) return result async def setwriteprice(self, **params): result = await self.client_storage.request(method_name="setwriteprice", **params) return result async def setreadprice(self, **params): result = await self.client_storage.request(method_name="setreadprice", **params) return result async def changeowner(self, **params): result = await self.client_storage.request(method_name="changeowner", **params) return result async def sharecontent(self, **params): result = await self.client_storage.request(method_name="sharecontent", **params) return result async def get_transactions(self, **params): result = await self.client_storage.request( method_name="get_transactions", **params) return result async def save_transaction(self, **params): result = await self.client_storage.request( method_name="save_transaction", **params) return result async def withdraw(self, **params): result = await self.client_withdraw.request(method_name="withdraw", **params) return result
def test_invalid_address(self): with self.assertRaises(Exception): self.assertFalse( Qtum.verify_address('qAiXFyqVf9vxf6iu47AdfU1FVaHyULUP7e'))
def test_valid_predefined_address(self): self.assertTrue( Qtum.verify_address('qZiXFyqVf9vxf6iu47AdfU1FVaHyULUP7e'))
def test_valid_qtum_address(self): self.assertTrue( Qtum.verify_qtum_address('qZiXFyqVf9vxf6iu47AdfU1FVaHyULUP7e', mainnet=False))
def test_valid_verify(self): message = 'aaa' public_key = '040646d16f7bb84333446266a7237eed33866b3478caad3da040e0ca63e7b3f1c01f8c8ee50d8a57e2b9c35b48efdce495f0075560790bb8a87749b92ad8600239' signature = '304402201c1ee66e98a67e4da411eacc2388b4e33cfd6da37a11340119e2cb2c76064aa0022049c00a810b896445f7508d0499845eaf3b904661cd427e0a19ab54a50e9012ad' self.assertTrue(Qtum.verify_message(message, signature, public_key))
def test_valid_signature4(self): message = 'Simple message' public_key = '995d52cbc1c5b96eebeeae16383dd7c41dd409fe9eef6e13a7ca2807911dd45fbd18005bfa2272e91b9f0ebd702226db3e5f7f17531087f3ac54c51bae100d6d' signature = '1d7cb366aa960c837bdaf7423054f8a8eb9f1dfd20ae277482f51231b1865eb24fd2bd336f6687a0e8854c600dce2f18606dfd5429f8551077bb4f6ab53b832a' self.assertTrue(Qtum.verify_message(message, signature, public_key))
def test_tricky_qtum_address(self): with self.assertRaises(Exception): self.assertFalse( Qtum.verify_address('6JnZeT4rMWNrapEKqCLjNGFJRfKyALEm5'))
def test_der(self): message = 'aaa' signature = '3045022016cdbe36a5653336ff74a28cde1092c76886306b0fb9a86bbcd3b8ec15679a5602210091393fb0d4c43a461ff522ebfd8f22ef8ad412f51f29602ba7ca339c37880772' public_key = '0406838688de86e85a2a10f1c0986eeef3dadd7e8cf7bd8491dc9557132f75b655c9eb00d5fed251c7def677e1b67c49f5d4cff690282fcbd9fe97be29f4fef843' self.assertTrue(Qtum.verify_message(message, signature, public_key))