def encode_signed_transaction(self, transaction, signature): """prepare a signed transaction message""" tag = bytes([idf.OBJECT_TAG_SIGNED_TRANSACTION]) vsn = bytes([idf.VSN]) encoded_signed_tx = encode_rlp(idf.TRANSACTION, [tag, vsn, [signature], transaction]) encoded_signature = encode(idf.SIGNATURE, signature) return encoded_signed_tx, encoded_signature
def encode_signed_transaction(self, transaction, signature): """prepare a signed transaction message""" tag = bytes([OBJECT_TAG_SIGNED_TRANSACTION]) vsn = bytes([VSN]) encoded_signed_tx = hashing.encode_rlp( "tx", [tag, vsn, [signature], transaction]) encoded_signature = hashing.encode("sg", signature) return encoded_signed_tx, encoded_signature
def build_tx_object(tx_data, tx_raw, fee_idx, min_fee): if tx_data.get("fee") < min_fee: tx_native[fee_idx] = _int(min_fee) tx_data["fee"] = min_fee tx_encoded = encode_rlp(idf.TRANSACTION, tx_native) tx = dict( data=tx_data, tx=tx_encoded, hash=TxBuilder.compute_tx_hash(tx_encoded), ) return namedtupled.map(tx, _nt_name="TxObject")
def build_tx_object(tx_data, tx_raw, fee_idx, min_fee): # if fee is not set use the min fee if tx_data.get("fee") <= 0: tx_data["fee"] = min_fee # if it is set check that is greater then the minimum fee elif tx_data.get("fee") < min_fee: raise TransactionFeeTooLow( f'Minimum transaction fee is {min_fee}, provided fee is {tx_data.get("fee")}' ) tx_native[fee_idx] = _int(tx_data.get("fee")) tx_encoded = encode_rlp(idf.TRANSACTION, tx_native) tx = dict( data=tx_data, tx=tx_encoded, hash=TxBuilder.compute_tx_hash(tx_encoded), ) return namedtupled.map(tx, _nt_name="TxObject")
def tx_spend(self, recipient_id, amount, payload, fee, ttl): """ create a spend transaction :param recipient_id: the public key of the recipient :param amount: the amount to send :param payload: the payload associated with the data :param fee: the fee for the transaction :param ttl: the relative ttl of the transaction """ # compute the absolute ttl and the nonce nonce, ttl = self._get_nonce_ttl(ttl) if self.native_transactions: sid = hashing.to_bytes(ID_TAG_ACCOUNT) + hashing.decode( self.account.get_address()) rid = hashing.to_bytes(ID_TAG_ACCOUNT) + hashing.decode( recipient_id) tx = [ hashing.to_bytes(OBJECT_TAG_SPEND_TRANSACTION), hashing.to_bytes(VSN), sid, rid, hashing.to_bytes(amount), hashing.to_bytes(fee), hashing.to_bytes(ttl), hashing.to_bytes(nonce), hashing.to_bytes(payload) ] tx = hashing.encode_rlp("tx", tx) else: # send the update transaction body = { "recipient_id": recipient_id, "amount": amount, "fee": fee, "sender_id": self.account.get_address(), "payload": payload, "ttl": ttl, "nonce": nonce, } # request a spend transaction tx = self.epoch.post_spend(body=body) return self.sign_encode_transaction(tx)
def test_hashing_rlp(): args = [ { "data": ("tx", [b"a", b"b", 1, 0, False, True]), "rlp": "tx_xmFiAYCAAXNBplc=", }, { "data": ("th", [b"a", b"b", 1, 0, False, True]), "rlp": "th_rCDULgdVmqKQR6W", }, ] for a in args: prefix, data = a.get("data") assert hashing.encode_rlp(prefix, data) == a.get("rlp") with raises(TypeError): hashing.encode_rlp("tx", "a string") # valid prefix wrong data with raises(ValueError): hashing.encode_rlp(None, [1, b'a']) with raises(ValueError): hashing.encode_rlp("eg", [1, b'a'])