def test_get_addresses_step_negative(self): """ Providing a negative ``step`` value to ``get_addresses``. This is probably a weird use case, but what the heck. """ ag = AddressGenerator(self.seed_1) # noinspection SpellCheckingInspection self.assertListEqual( ag.get_addresses(start=1, count=2, step=-1), [ Address( b'PNLOTLFSALMICK9PSW9ZWLE9KJAKPKGJZQJDAFMO' b'VLHXMJCJXFPVHOTTOYDIAUAYELXKZWZUITCQBIQKY', ), Address( b'DLEIS9XU9V9T9OURAKDUSQWBQEYFGJLRPRVEWKN9' b'SSUGIHBEIPBPEWISSAURGTQKWKWNHXGCBQTWNOGIY', ), ], )
def create_transaction(cls, text): tx = [ ProposedTransaction( # Recipient address=Address(cls.address), value=0, tag=Tag(b'TAG'), message=TryteString.from_string(text), ), ] return tx
def test_add_checksum(self): """ Checksum is added to an address without it. """ addy = Address(trytes=b'ZKIUDZXQYQAWSHPKSAATJXPAQZPGYCDCQDRSMWWCGQJNI' b'PCOORMDRNREDUDKBMUYENYTFVUNEWDBAKXMV') addy.add_checksum() self.assertTrue(addy.is_checksum_valid()) self.assertTrue(len(addy) == Address.LEN + AddressChecksum.LEN)
def test_security_level_elevated(self): """ Generating addresses with a higher security level. """ ag = AddressGenerator(self.seed_1, security_level=3) self.assertListEqual( ag.get_addresses(start=0, count=3), [ Address( b'BGHTGOUKKNTYFHYUAAPSRUEVN9QQXFOGVCH9Y9BO' b'NWXUBDLSKAWEOFZIVMHXBAYVPGDZEYCKNTUJCLPAX', ), Address( b'EGMRJEUIYFUGWAIXXZCHCZUVUUYITICVHDSHCQXG' b'FHJIVDCLTI9ZVRIKRLZQWW9CPOIXVDCBAHVGLUHI9', ), Address( b'ENPSARVJZGMMPWZTAIRHADEOZCEVIFNJWSZQHNEI' b'RVEVI9GYMFNEOGNUYCPGPSEFCSDHUHOQKDPVGDKYC', ), ], )
def sendTransaction(self, data, tag): # Structure transaction-构造交易 tx = ProposedTransaction( address=Address(iotaAddress), message=TryteString.from_unicode(data), tag=Tag(tag), value=0 ) tx = api.prepare_transfer(transfers=[tx]) result = api.send_trytes(tx['trytes'], depth=iota_depth, min_weight_magnitude=iota_min_weight_magnitude) print('Transaction sent to the tangle! result:{}'.format(result))
def test_send_unspent_inputs_to_error_already_finalized(self): """ Invoking ``send_unspent_inputs_to`` on a bundle that is already finalized. """ # Add 1 transaction so that we can finalize the bundle. self.bundle.add_transaction(ProposedTransaction( address = Address( b'TESTVALUE9DONTUSEINPRODUCTION99999XE9IVG' b'EFNDOCQCMERGUATCIEGGOHPHGFIAQEZGNHQ9W99CH' ), value = 0, )) self.bundle.finalize() with self.assertRaises(RuntimeError): self.bundle.send_unspent_inputs_to(Address(b''))
def test_init_error_too_long(self): """ Attempting to create an address longer than 81 trytes. """ with self.assertRaises(ValueError): Address( # Extra padding at the end is not ignored. # If it's an address (without checksum), then it must be 81 # trytes exactly. b'JVMTDGDPDFYHMZPMWEKKANBQSLSDTIIHAYQUMZOK' b'HXXXGJHJDQPOMDOMNRDKYCZRUFZROZDADTHZC99999')
def test_add_transaction_error_negative_value(self): """ Attempting to add a transaction with a negative value to a bundle. Use :py:meth:`ProposedBundle.add_inputs` to add inputs to a bundle. """ with self.assertRaises(ValueError): self.bundle.add_transaction(ProposedTransaction( address = Address(b''), value = -1, ))
def __init__(self, _adr, _psw): self.MAMChannel = MAML_Ed25519(root_address=Address(_adr), channel_password=_psw) prikey, pubkey = Ed25519Cipher.generate_keys() self.pubkey = pubkey self.prikey = prikey self.MAMChannel.add_trusted_pubkey('test_entity', pubkey)
def test_get_addresses_step_negative(self): """ Providing a negative ``step`` value to ``get_addresses``. This is probably a weird use case, but what the heck. """ ag = AddressGenerator(self.seed_1) # noinspection SpellCheckingInspection self.assertListEqual( ag.get_addresses(start=1, count=2, step=-1), [ Address( b'DUOVVF9WCNAEOHHWUYUFSYOOWZPDVVD9JKFLQN9Z' b'DPAKKHSBKLTRFHD9UHIWGKSGAWCOMDG9GBPYISPWR', ), Address( b'NWQBMJEBSYFCRKGLNUQZJIOQOMNMYPCIRVSVJLP9' b'OFV9CZ99LFGZHDKOUDGRVJXUDPUPCVOQBKSZLPU9K', ), ], )
def test_add_transaction_error_already_finalized(self): """ Attempting to add a transaction to a bundle that is already finalized. """ # noinspection SpellCheckingInspection self.bundle.add_transaction( ProposedTransaction( address=Address(b'TESTVALUE9DONTUSEINPRODUCTION999999DCBIE' b'U9AIE9H9BCKGMCVCUGYDKDLCAEOHOHZGW9KGS9VGH'), value=0, )) self.bundle.finalize() with self.assertRaises(RuntimeError): self.bundle.add_transaction( ProposedTransaction( address=Address(b''), value=0, ))
def test_security_level_lowered(self): """ Generating addresses with a lower security level. """ ag = AddressGenerator(self.seed_1, security_level=1) self.assertListEqual( ag.get_addresses(start=0, count=3), [ Address( b'KNDWDEEWWFVZLISLYRABGVWZCHZNZLNSEJXFKVGA' b'UFLL9UMZYEZMEJB9BDLAASWTHEKFREUDIUPY9ICKW', ), Address( b'CHOBTRTQWTMH9GWFWGWUODRSGPOJOIVJUNIQIBZL' b'HSWNYPHOD9APWJBMJMGLHFZENWFKDYWHX9JDFXTAB', ), Address( b'YHTOYQUCLDHAIDILFNPITVPYSTOCFAZIUNDYTRDZ' b'CVMVGZPONPINNVPJTOAOKHHZWLOKIZPVASTOGAKPA', ), ], )
def read(self) -> Response or None: ''' Reads from self.current_read_addr, if successful moves self.current_read_addr one hash further ''' response = self._get_MAM_from_address(Address(self.current_read_addr)) if response: self.current_read_addr = hash_tryte(self.current_read_addr + self.write_pwd) return response else: return None
def address_checksum(address): """ Takes a address (81 Characters) and converts it to an address with checksum (90 Characters) :param address: :return: """ bytes_address = bytes(address.encode('utf-8')) addy = Address(bytes_address) address = str(addy.with_valid_checksum()) return address
def test_init_error_checksum_too_long(self): """ Attempting to create an address longer than 90 trytes. """ with self.assertRaises(ValueError): Address( # Extra padding at the end is not ignored. # If it's a checksummed address, then it must be 90 trytes # exactly. b'RVORZ9SIIP9RCYMREUIXXVPQIPHVCNPQ9HZWYKFWYWZRE' b'9JQKG9REPKIASHUUECPSQO9JT9XNMVKWYGVAFOXM9MUBX9')
def test_add_inputs_no_change(self): """ Adding inputs to cover the exact amount of the bundle spend. """ # noinspection SpellCheckingInspection self.bundle.add_transaction( ProposedTransaction( address=Address(b'TESTVALUE9DONTUSEINPRODUCTION99999VELDTF' b'QHDFTHIHFE9II9WFFDFHEATEI99GEDC9BAUH9EBGZ'), value=29, )) # noinspection SpellCheckingInspection self.bundle.add_transaction( ProposedTransaction( address=Address(b'TESTVALUE9DONTUSEINPRODUCTION99999OGVEEF' b'BCYAM9ZEAADBGBHH9BPBOHFEGCFAM9DESCCHODZ9Y'), value=13, )) self.bundle.add_inputs([ self.input_1_bal_eq_40, self.input_2_bal_eq_2, ]) # Just to be tricky, add an unnecessary change address, just to # make sure the bundle ignores it. # noinspection SpellCheckingInspection self.bundle.send_unspent_inputs_to( Address(b'TESTVALUE9DONTUSEINPRODUCTION99999FDCDFD' b'VAF9NFLCSCSFFCLCW9KFL9TCAAO9IIHATCREAHGEA'), ) self.bundle.finalize() # All of the addresses that we generate for this test case have # security level set to 1, so we only need 1 transaction per # input (4 total, including the spends). # # Also note: because the transaction is already balanced, no change # transaction is necessary. self.assertEqual(len(self.bundle), 4)
def test_security_level_elevated(self): """ Generating addresses with a higher security level. """ ag = AddressGenerator(self.seed_1, security_level=3) # noinspection SpellCheckingInspection self.assertListEqual( ag.get_addresses(start=0, count=3), [ Address( b'ZWJEPOOWHOZYEMGJCJAWDETMBUEHOYFHAGOFINQA' b'CSGFNHXTMDHVHVAWQHQEBLDXKOQVKHEIU9QWLWPSV', ), Address( b'TNCURBUSWSCMWKJMZFW9SDUTVMQRAHTWVPYQFDRZ' b'ALTMDEMCVWEVWYIJZMKOCEPSJKRV9EGDDDCLMCJBL', ), Address( b'HHZUZEKUTMBBIFZDUBVSEXEPEDWATWOBBGVCHBMI' b'MBVRLDDLBBWWMECJGNSXFJOCPOFSEZOHDGPVADYCK', ), ], )
def test_generator(self): """ Creating a generator. """ ag = AddressGenerator(self.seed_2) generator = ag.create_iterator() self.assertEqual( next(generator), Address( b'FNKCVJPUANHNWNBAHFBTCONMCUBC9KCZ9EKREBCJ' b'AFMABCTEPLGGXDJXVGPXDCFOUCRBWFJFLEAVOEUPY', ), ) self.assertEqual( next(generator), Address( b'MSYILYYZLSJ99TDMGQHDOBWGHTBARCBGJZE9PIMQ' b'LTEXJXKTDREGVTPA9NDGGLQHTMGISGRAKSLYPGWMB', ), )
def sendTransfer(self, address, value, depth = 100): address = str(address) value = int(value) self.IOTA_connection.send_transfer( depth = depth, transfers = [ ProposedTransaction( address = Address(address), value = value, ), ], )
def test_pass_happy_path(self): """ Request is valid. """ request = { 'changeAddress': Address(self.trytes1), 'depth': 100, 'minWeightMagnitude': 18, 'seed': Seed(self.trytes2), 'inputs': [ Address(self.trytes3), Address(self.trytes4), ], 'transfers': [self.transfer1, self.transfer2], 'reference': TransactionHash(self.trytes1), } filter_ = self._filter(request) self.assertFilterPasses(filter_) self.assertDictEqual(filter_.cleaned_data, request)
def prepare_transferes(): new_transfer = True prepared_transferes = [] while new_transfer: get_recipient_address = True while get_recipient_address: recipient_address = raw_input( "\nPlease enter the receiving address: ") if len(recipient_address) == 81: print( "You enterd a address without checksum. Are you sure you want to continiue?" ) yes = yes_no_user_input() if yes: get_recipient_address = False else: print( "Good choice! Addresses with checksum are a lot safer to use." ) elif len(recipient_address) == 90: is_valid = is_valid_address(recipient_address) if is_valid: get_recipient_address = False else: print("Invalid address!! Please try again!") else: print( "\nYou enterd a invalid address. Address must be 81 or 90 Char long!" ) recipient_address = bytes(recipient_address) user_message = raw_input("Please enter a message: ") user_tag = raw_input("Please enter a tag: ") user_tag = bytes(user_tag) transfer_value = transfer_value_user_input() txn = \ ProposedTransaction( address=Address( recipient_address ), message=TryteString.from_string(user_message), tag=Tag(user_tag), value=transfer_value, ) prepared_transferes.append(txn) print("Do you want to prepare another transfer?") yes = yes_no_user_input() if not yes: new_transfer = False review_transfers(prepared_transferes)
def test_generator_with_offset(self): """ Creating a generator that starts at an offset greater than 0. """ ag = AddressGenerator(self.seed_1) generator = ag.create_iterator(start=1, step=2) self.assertEqual( next(generator), Address( b'PNLOTLFSALMICK9PSW9ZWLE9KJAKPKGJZQJDAFMO' b'VLHXMJCJXFPVHOTTOYDIAUAYELXKZWZUITCQBIQKY', ), ) self.assertEqual( next(generator), Address( b'IWWMMHBFWCWOZQLBNXDJ9OOTIGXXU9WNUHFGUZWR' b'9FWGIUUUQUECHPKXJLIEKZBOVSEA9BCT9DLOCNCEC', ), )
def test_fail_addresses_wrong_type(self): """ ``addresses`` is not an array. """ self.assertFilterErrors( { 'addresses': Address(self.trytes1), }, { 'addresses': [f.Type.CODE_WRONG_TYPE], }, )
def get_self_auth_txs_from_endpoint(self, pubkey: str) -> List[AuthTransaction]: ''' Retrieve list of AuthTransaction from endpoint authenticated by the pubkey and authored by the same pubkey :param address: Address :param pubkeys_list: List[str] :return: List[AuthTransaction] ''' endpoint_address = Address(hash_tryte(pubkey)) auth_tx = self.get_auth_txs_from_address(endpoint_address, [pubkey]) return auth_tx
def test_security_level_lowered(self): """ Generating addresses with a lower security level. """ ag = AddressGenerator(self.seed_1, security_level=1) # noinspection SpellCheckingInspection self.assertListEqual( ag.get_addresses(start=0, count=3), [ Address( b'QTGZZPTYYFMFG9UCTOREALIZZJ9VEASMBFLMZARF' b'LENFSNPSITZZVXH9IGPVIRAVRYMXYVXQBUORWVILF', ), Address( b'FHOFBSATJIGMLKGGPWEBIBWIPELKTEAMAQTEDNUN' b'HOJBVBAIGTPLMKSGBHWZGNXTLRMFZXASV9FNZGBNY', ), Address( b'VRYXJWWGQIKDLI9R9KFECQXCLYNBHUMCWEYDTOTZ' b'GITLQIRDZCOBWXAOTVPGKBQBXKZAZAFRLZTDBRBXW', ), ], )
def test_fail_multisigInput_null(self): """ ``multisigInput`` is null. """ self.assertFilterErrors( { 'changeAddress': Address(self.trytes_1), 'multisigInput': None, 'transfers': [ ProposedTransaction( address=Address(self.trytes_3), value=0, ), ], }, { 'multisigInput': [f.Required.CODE_EMPTY], }, )
def test_get_addresses_multiple(self): """ Generating multiple addresses in one go. """ ag = AddressGenerator(self.seed_2) self.assertListEqual( ag.get_addresses(start=0, count=3), [ Address( b'FNKCVJPUANHNWNBAHFBTCONMCUBC9KCZ9EKREBCJ' b'AFMABCTEPLGGXDJXVGPXDCFOUCRBWFJFLEAVOEUPY', ), Address( b'MSYILYYZLSJ99TDMGQHDOBWGHTBARCBGJZE9PIMQ' b'LTEXJXKTDREGVTPA9NDGGLQHTMGISGRAKSLYPGWMB', ), Address( b'IIREHGHXUHARKVZDMHGUUCHZLUEQQULLEUSJHIIB' b'WFYZIZDUFTOVHAWCKRJXUZ9CSUVLTRYSUGBVRMTOW', ), ], ) self.assertListEqual( ag.get_addresses(start=10, count=3), [ Address( b'BPXMVV9UPKBTVPJXPBHHOJYAFLALOYCGTSEDLZBH' b'NFMGEHREBQTRIPZAPREANPMZJNZZNCDIUFOYYGGFY', ), Address( b'RUCZQJWKXVDIXTLHHOKGMHOV9AKVDBG9HUQHPWNZ' b'UNKJNFVMULUSLKFJGSTBSNJMRYSJOBVBQSKVXISZB', ), Address( b'FQAKF9XVCLTBESJKWCHFOCTVABYEEJP9RXUVAEUW' b'ENFUUQK9VCHFEORHCYDUJQHNUDWNRDUDZTUGKHSPD', ), ], )
def test_pass_compatible_types(self): """ Request contains values that can be converted to the expected types. """ filter_ = self._filter({ # Any TrytesCompatible values will work here. 'changeAddress': binary_type(self.trytes1), 'seed': bytearray(self.trytes2), 'reference': binary_type(self.trytes1), 'inputs': [ binary_type(self.trytes3), bytearray(self.trytes4), ], # These values must have the correct type, however. 'transfers': [self.transfer1, self.transfer2], 'depth': 100, 'minWeightMagnitude': 18, }) self.assertFilterPasses(filter_) self.assertDictEqual( filter_.cleaned_data, { 'changeAddress': Address(self.trytes1), 'depth': 100, 'minWeightMagnitude': 18, 'seed': Seed(self.trytes2), 'reference': TransactionHash(self.trytes1), 'inputs': [ Address(self.trytes3), Address(self.trytes4), ], 'transfers': [self.transfer1, self.transfer2], })
def test_get_addresses_multiple(self): """ Generating multiple addresses in one go. """ ag = AddressGenerator(self.seed_2) # noinspection SpellCheckingInspection self.assertListEqual( ag.get_addresses(start=0, count=3), [ Address(b'SZWZMYQYWGXWAAVQSDTIOFGTZP9PWIDDUHHNGRDP' b'RCGNSXRNYWBEZIORKNNLNZHJ9QYMFYZIJJ9RFPBJT'), Address(b'N9KY9HCT9VTI99FFRIIBHQZIJOVSLFVWPOIFSHWL' b'CCIVYLIDBKJLVQFYJNPIUNATUUCIRHUHNLFBCAXIY'), Address(b'BH9BWJWHIHLJSHBYBENHLQQBOCQOOMAEJJFFBCSE' b'IMDVPDULGD9HBPNQKWBPM9SIDIMGUOGTPWMQSVVHZ'), ], ) # noinspection SpellCheckingInspection self.assertListEqual( ag.get_addresses(start=10, count=3), [ Address( b'CCKZUWMILLQLLLIFNXBFGGPXFHNROQQOYYBMLIEOLB' b'PVIVFJMQAVCCGKVGNRTKAZQLKYWMTBUEVBPGZMN', ), Address( b'XWXALLEBVQXVRYLGPPJUL9RAIUKUXERBEMVTZJOMRB' b'CGXNYA99PN9DKOPAWDSIPIRUBKFQUBQFUOKZMQW', ), Address( b'CLYKQDU9WRHEJZSLMZKVDIWLHZKEIITWXDAHFFSQCP' b'LADQKLUQLSECZMIOUDSLXRWEDAEHKRVWQRGZMLI', ), ], )
def make_transfer(iota_node, args): """ Make transfers :param iota_node: :param args: :return: """ txn = [ProposedTransaction( address=Address(Address(bytes(args['recipient_address'].encode('utf-8')))), message=TryteString.from_string(args['message']), tag=Tag(args['tag']), value=args['value'] )] api = Iota(iota_node, args['seed']) r = api.send_transfer( depth=7, transfers=txn, change_address=Address(bytes(args['deposit_address'].encode('utf-8'))), min_weight_magnitude=13 ) print(r)