Ethereum: Insert data to transaction with OP_RETURN

Ethereum op_retururn

Op_retururn is a special operating code in Ethereum that allows you to store arbitrary data at the end of each transaction. Here is a step by step guide on how to insert an additional chain with OP_Return and recover the transaction of the block chain, decoding accordingly.

Step 1: Prepare your transaction

Before starting, make sure the transaction is valid. Otherwise, modify it or create a new one that includes all the required data.

Passage 2: Coding the chain data

It is necessary to encode the data of the byte chain format using the UTF-8 coding scheme. This will be used as input for op_retururn.

`Python

Matters Json

by Eth Import Ethereum, transaction

Def Coded_string (Data):

Return Byte (json.dumps (data). Cende ('utf-8'))

Use example:

Data = {"key": "value"}

Encoded_data = code_string (data)

PRINT (Coded_data)

Output: B '{"Key": "Value"}'

Step 3: Create an OP_Return transaction

Create a new transaction object using the Ethereum library. You can use it to create an op_return transaction.

Python

by Eth Import Ethereum, transaction

Def Create_op_retururn_transation (data):

op_retururn = {

"Data": code_data,

"Gasprice": Ethereum.gas_price (),

"Gas": 1000000,

"Nance": Ethereum.gaslimit () + 1,

"Value": 0

}

Return Transaction.transation (Op_retururn)

Use example:

Op_retururn_transazion = Create_op_retururn_transation (data)

Print (op_retururn_transotion.to_sting ())

Output: transaction {Data = B '{"key": "value"}', gastprice = 0.00000000000000000000000, gas = 1000001, nance = 9999, value = 0}

Step 4: Sign the Op_retururn transaction

The transaction company with its private Ethereum key to create a valid signed transaction.

Python

by Eth Import Ethereum, Privatekey

Def sign_op_retururn_transation (transaction):

Private_key = Privatekey.from_file ("Route/A/Private/Key")

Return ethhereum.sign_transation (transaction, private_key)

Use example:

Signed

Print (signed_transation.to_sting ())

Output: transaction {Data = B '{"Key": "Value"}', Gastprice = 0.0000000000000000000000,000, Gas = 1000001, Nance = 9999, Value = 0}

passage 5: get the transaction

Get the block chain transaction using the Ethereum library.

Python

by Eth Import Ethereum

Def get_op_retururn_transation () ::

Replace with the URL or the API key of the Ethereum supplier

Provider_url = "

Initialize the Ethereum client

Et_client = Ethereum (provider_url)

Get the latest block number and the hash of the block chain transaction

Last_block_number = 1000000

Last_Block_Hash = Eth_Client.get_latost_block ()

Company your Op_Retrund transaction with the private key of the last block

Signed

Get the transaction object of the last hash block

Ortat_Block = ethhereum.parse_latost_block (Last_block_number, Ortat_Block_Hash)

Op_retururn_transotion = Ortat_block.get_op_return ()

Return op_retururn_transation

Use example:

Op_retururn_transotion = get_op_retururn_transation ()

Print (op_retururn_transotion.to_sting ())

Output: Transaction {Data = B '{"Key": "Value"}', Gasprice = 0.00000000000000000000,000, Gas = 1000001, Nice = 9999, Value = 0}

passage 6: decade op_retururn data

The chain data decodes at the end of each transaction using theJson ‘library.

“ Python

Matters Json

De decode_op_return_data (data):

Return json.loads (Data.Decode (‘Utf-8’))

Use example:

decoded_data = decode_op_return_data (op_retururn_transation.

Market Order Book

Để lại một bình luận

Email của bạn sẽ không được hiển thị công khai. Các trường bắt buộc được đánh dấu *