def __init__(self, public_key=None, private_key=None, phrase=None, name=""): self.name = name self.phrase = phrase pair = {} if public_key and private_key: pair['public-key'] = public_key pair['private-key'] = private_key elif private_key: pair = key_pair_from_private_key(private_key) elif phrase: pair = key_pair_with_secret_phrase(phrase) elif public_key: pair['public-key'] = public_key pair['private-key'] = None else: pair = key_pair() self.public_key = pair['public-key'] self.private_key = pair['private-key'] self.__rpc_state = Rpc("get-wallet-state", params={"public-key": self.public_key}) self.__chain = None self.__web_wallet = WebWallet()
def test_key_pair_with_secret_phrase(self): pair1 = key_pair_with_secret_phrase("secret-phrase") self._validate_pair(pair1) pair2 = key_pair_with_secret_phrase(phrase="secret-phrase") self._validate_pair(pair2) self.assertDictEqual(pair1, pair2) self.assertDictEqual( { 'public-key': 'eZrf8Eq1qDvSCEfdeezBRhT3QSf5GkrQH9tzGNaYt8q8gH5aZ', 'private-key': 'XPBeWywB2XCgTVATLY5kYTTwNC5GwVqYyNcsTeLb7Mmn2KSNjTUzhxPJG88aiSWiuiXp7y7xHzCikNimmcz2a65mZgnBj' }, pair1 ) # todo humanize errors self.assertRaises(TypeError, key_pair_with_secret_phrase, 1) self.assertRaises(TypeError, key_pair_with_secret_phrase) self.assertRaisesWithMessage(RuntimeError, "MileCsa error: secret phrase is empty", key_pair_with_secret_phrase, "")
def test_transfer_assets(self): pair1 = key_pair_with_secret_phrase("secret-phrase") pair2 = key_pair_with_secret_phrase("destination-secret-phrase") res = transfer_assets( pair1['public-key'], pair1['private-key'], pair2['public-key'], 2**64-1, 1, 1, 0.29*100, 0.1+0.2, "22memo" ) self.assertIs(type(res), dict) self.assertEqual('29.00000', res.get('asset', {}).get('amount', '')) self.assertEqual(1, res.get('asset', {}).get('code')) self.assertEqual("22memo", res.get('description')) self.assertEqual("0.30000", res.get('fee')) self.assertEqual(pair1['public-key'], res.get('from')) self.assertEqual(pair2['public-key'], res.get('to')) self.assertEqual(str(2**64-1), res.get('block-id')) self.assertEqual(1, res.get('transaction-id')) self.assertEqual('TransferAssetsTransaction', res.get('transaction-type')) # runs without memo res = transfer_assets( pair1['public-key'], pair1['private-key'], pair2['public-key'], 2 ** 64 - 1, 1, 1, 0.29 * 100, 0.1 + 0.2 ) self.assertIs(type(res), dict) self.assertEqual("", res.get('description'))
def test_unregister_node(self): pair = key_pair_with_secret_phrase("secret-phrase") res = unregister_node( pair['public-key'], pair['private-key'], block_id=2 ** 64 - 1, transaction_id=1, fee=0.01 ) self.assertIs(type(res), dict) self.assertEqual(pair['public-key'], res.get('public-key')) self.assertEqual(str(2**64-1), res.get('block-id')) self.assertEqual(1, res.get('transaction-id')) self.assertEqual('0.01', res.get('fee')) self.assertEqual('UnregisterNodeTransaction', res.get('transaction-type'))
def test_emission(self): pair = key_pair_with_secret_phrase("secret-phrase") res = emission( pair['public-key'], pair['private-key'], 2**64-1, 1, 1, 0.1+0.2 ) self.assertIs(type(res), dict) self.assertEqual(1, res.get('asset', {}).get('code')) self.assertEqual("0.30000", res.get('fee')) self.assertEqual(pair['public-key'], res.get('from')) self.assertEqual(str(2**64-1), res.get('block-id')) self.assertEqual(1, res.get('transaction-id')) self.assertEqual('EmissionTransaction', res.get('transaction-type'))
def test_key_pair_from_private_key(self): pair1 = key_pair() pair2 = key_pair_from_private_key(pair1['private-key']) self._validate_pair(pair2) self.assertDictEqual(pair1, pair2) pair3 = key_pair_with_secret_phrase("secret-phrase") pair4 = key_pair_from_private_key(private_key=pair3['private-key']) self._validate_pair(pair4) self.assertDictEqual(pair3, pair4) # todo humanize errors self.assertRaises(TypeError, key_pair_from_private_key, 1) self.assertRaises(TypeError, key_pair_from_private_key) self.assertRaisesWithMessage(RuntimeError, "MileCsa error: private key is empty", key_pair_from_private_key, "") self.assertRaisesWithMessage(RuntimeError, "MileCsa error: Error read private key: base58 check string decode error", key_pair_from_private_key, "incorrectprivatekey")
def test_post_token_rate(self): pair = key_pair_with_secret_phrase("secret-phrase") res = post_token_rate( pair['public-key'], pair['private-key'], block_id=2 ** 64 - 1, transaction_id=1, rate=1.45, fee=0.01 ) self.assertIs(type(res), dict) self.assertEqual(pair['public-key'], res.get('public-key')) self.assertEqual(str(2**64-1), res.get('block-id')) self.assertEqual(1, res.get('transaction-id')) self.assertEqual('1.45', res.get('asset', {}).get('amount')) self.assertEqual(0, res.get('asset', {}).get('code')) self.assertEqual('0.01', res.get('fee')) self.assertEqual('PostTokenRate', res.get('transaction-type'))
def test_register_node(self): pair = key_pair_with_secret_phrase("secret-phrase") res = register_node( pair['public-key'], pair['private-key'], node_address='127.0.0.2', block_id=2**64-1, transaction_id=1, amount=0.1+0.2, fee=0.01 ) self.assertIs(type(res), dict) self.assertEqual('127.0.0.2', res.get('address')) self.assertEqual(0, res.get('asset', {}).get('code')) self.assertEqual('0.30', res.get('asset', {}).get('amount')) self.assertEqual('0.01', res.get('fee')) self.assertEqual(pair['public-key'], res.get('public-key')) self.assertEqual(str(2**64-1), res.get('block-id')) self.assertEqual(1, res.get('transaction-id')) self.assertEqual('RegisterNodeTransactionWithAmount', res.get('transaction-type'))