def test_init(self): config = storage.InRamConfigurationStore() key_store = storage.InRamPlainKeyStore(config=config) wallet = Wallet(key_store=key_store) # InRamStore does not come with a default key self.assertFalse(wallet.created()) self.assertTrue(bool(wallet.rpc)) self.assertEqual(wallet.prefix, "DX") wif1 = PrivateKey() wif2 = PrivateKey() wallet.setKeys([wif1, wif2]) self.assertIn(str(wif1.pubkey), wallet.store.getPublicKeys()) self.assertIn(str(wif2.pubkey), wallet.store.getPublicKeys()) self.assertEqual(wallet.getPrivateKeyForPublicKey(wif1.pubkey), str(wif1)) self.assertEqual(wallet.getPrivateKeyForPublicKey(wif2.pubkey), str(wif2)) # wallet.unlock("") # wallet.lock() # is unlocked because InRamKeyStore and not encrypted self.assertFalse(wallet.store.is_encrypted()) self.assertFalse(wallet.is_encrypted()) self.assertTrue(wallet.unlocked()) self.assertFalse(wallet.locked()) wif3 = PrivateKey() wallet.addPrivateKey(wif3) self.assertIn(str(wif3.pubkey), wallet.store.getPublicKeys()) self.assertEqual(wallet.getPrivateKeyForPublicKey(wif3.pubkey), str(wif3)) wallet.removePrivateKeyFromPublicKey(wif3.pubkey) with self.assertRaises(KeyNotFound): wallet.getPrivateKeyForPublicKey(wif3.pubkey)
def test_shared_secrets_equal(self): wifs = cycle([x[0] for x in test_shared_secrets]) for _ 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 doit(self, printWire=False): ops = [Operation(self.op)] tx = Signed_Transaction( ref_block_num=ref_block_num, ref_block_prefix=ref_block_prefix, expiration=expiration, operations=ops, ) pprint(tx.json()) tx = tx.sign([wif], chain=prefix) tx.verify([PrivateKey(wif).pubkey], prefix) txWire = hexlify(bytes(tx)).decode("ascii") if printWire: print() print(txWire) print() # Test against Deex backened live = deex.rpc.get_transaction_hex(tx.json()) # Compare expected result with test unit self.assertEqual(self.cm[:-130], txWire[:-130]) # Compare expected result with online result self.assertEqual(live[:-130], txWire[:-130]) # Compare expected result with online backend self.assertEqual(live[:-130], self.cm[:-130])
def test_decrypt_bugged_padding(self): for memo in not_enough_padding: dec = decode_memo( PrivateKey(memo["wif"]), PublicKey(memo["to"], prefix="GPH"), memo["nonce"], memo["message"], ) self.assertEqual(memo["plain"], dec)
def test_encrypt(self): for memo in test_cases: enc = encode_memo( PrivateKey(memo["wif"]), PublicKey(memo["to"], prefix="GPH"), memo["nonce"], memo["plain"], ) self.assertEqual(memo["message"], enc)
def test_create_account(self): name = "".join( random.choice(string.ascii_lowercase) for _ in range(12)) key1 = PrivateKey() key2 = PrivateKey() key3 = PrivateKey() key4 = PrivateKey() tx = deex.create_account( name, registrar="init0", # 1.2.100 referrer="init1", # 1.2.101 referrer_percent=33, owner_key=format(key1.pubkey, "DX"), active_key=format(key2.pubkey, "DX"), memo_key=format(key3.pubkey, "DX"), additional_owner_keys=[format(key4.pubkey, "DX")], additional_active_keys=[format(key4.pubkey, "DX")], additional_owner_accounts=["committee-account"], # 1.2.0 additional_active_accounts=["committee-account"], proxy_account="init0", storekeys=False, ) self.assertEqual(getOperationNameForId(tx["operations"][0][0]), "account_create") op = tx["operations"][0][1] role = "active" self.assertIn(format(key4.pubkey, "DX"), [x[0] for x in op[role]["key_auths"]]) self.assertIn(format(key4.pubkey, "DX"), [x[0] for x in op[role]["key_auths"]]) self.assertIn("1.2.0", [x[0] for x in op[role]["account_auths"]]) role = "owner" self.assertIn(format(key4.pubkey, "DX"), [x[0] for x in op[role]["key_auths"]]) self.assertIn(format(key4.pubkey, "DX"), [x[0] for x in op[role]["key_auths"]]) self.assertIn("1.2.0", [x[0] for x in op[role]["account_auths"]]) self.assertEqual(op["options"]["voting_account"], "1.2.100") self.assertEqual(op["registrar"], "1.2.100") self.assertEqual(op["referrer"], "1.2.101") self.assertEqual(op["referrer_percent"], 33 * 100)
def compareConstructedTX(self): self.maxDiff = None self.op = operations.Call_order_update( **{ "fee": { "amount": 100, "asset_id": "1.3.0" }, "delta_debt": { "amount": 10000, "asset_id": "1.3.22" }, "delta_collateral": { "amount": 100000000, "asset_id": "1.3.0" }, "funding_account": "1.2.29", "extensions": { "target_collateral_ratio": 12345 }, }) ops = [Operation(self.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) # Test against Deex backened self.cm = deex.rpc.get_transaction_hex(tx.json()) print("soll: %s" % self.cm[:-130]) print("ist: %s" % txWire[:-130]) print(txWire[:-130] == self.cm[:-130]) self.assertEqual(self.cm[:-130], txWire[:-130])
from deex.instance import set_shared_blockchain_instance from deexbase.memo import ( get_shared_secret, _pad, _unpad, encode_memo, decode_memo ) test_case={ 'from': 'DX82y5ZQiKeWG8HLxukKAQtDywARAjQjPGLC9b6DYtL5LTj4fZ1o', # Sender public address (public key) 'message': '3c4c9b610201f5b17b6f858d19899a02a12fce274aedbb49ad866d79b75e3967', (Encoded message) 'nonce': '8555724032490455626', 'plain': 'abcdefghijÛ', # padding limit and last character is unicode 'to': 'DX82y5ZQiKeWG8HLxukKAQtDywARAjQjPGLC9b6DYtL5LTj4fZ1o', # public key receiver 'wif': '5KR8jzysz2kbYy3TkL3x6NRxfNXwQUWyeVAF5ZagxdqKMawGgXG'} # Private key of receiver dec = decode_memo(PrivateKey(test_case["wif"]), PublicKey(test_case["to"],prefix="DX"), test_case["nonce"], test_case["message"]) print(dec) # Decoded memo message
from deexbase.operations import Transfer from deex import DeEx, storage from deex.instance import set_shared_blockchain_instance from deexbase.memo import ( get_shared_secret, _pad, _unpad, encode_memo, decode_memo ) test_case={ 'nonce': '8555724032490455626', # Nonce use random string + microtime. Every request must have uniq nonce 'plain': 'abcdefghijÛ', # padding limit and last character is unicode 'to': 'DX82y5ZQiKeWG8HLxukKAQtDywARAjQjPGLC9b6DYtL5LTj4fZ1o', # Receiver public key (public address) 'wif': '5KR8jzysz2kbYy3TkL3x6NRxfNXwQUWyeVAF5ZagxdqKMawGgXG'} # Sender PRIVATE key. (active) enc = encode_memo(PrivateKey(test_case["wif"]), PublicKey(test_case["to"],prefix="DX"), test_case["nonce"], test_case["plain"]) print(enc) //Result encoded message.
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)