Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Cookbook for v1 #143

Open
wants to merge 21 commits into
base: feat/next
Choose a base branch
from
Open
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Prev Previous commit
Next Next commit
add starting a project section
  • Loading branch information
popenta committed Dec 10, 2024
commit 416390d6465fd71772ba2b11c3c41d9680418b8a
159 changes: 140 additions & 19 deletions examples/v1.ipynb
Original file line number Diff line number Diff line change
Expand Up @@ -295,7 295,7 @@
"- `send_transaction(transaction: Transaction) -> bytes;`\n",
"- `send_transactions(transactions: list[Transaction]) -> tuple[int, list[bytes]];`\n",
"- `get_transaction(tx_hash: str | bytes) -> TransactionOnNetwork;`\n",
"- `await_completed_transaction(tx_hash: str | bytes) -> TransactionOnNetwork;`\n",
"- `await_transaction_completed(tx_hash: str | bytes) -> TransactionOnNetwork;`\n",
"\n",
"Some other methods are exposed through a so called network provider. There are two types of network providers: ApiNetworkProvider and ProxyNetworkProvider. The ProxyNetworkProvider interacts directly with the proxy of an observing squad. The ApiNetworkProvider, as the name suggests, interacts with the api, that is a layer over the proxy. It fetches data from the network but also from Elastic Search."
]
Expand Down Expand Up @@ -1452,7 1452,7 @@
"source": [
"# we use the transaction hash we got when broadcasting the transaction\n",
"# waiting for transaction completion\n",
"transaction_on_network = entrypoint.await_completed_transaction(tx_hash)\n",
"transaction_on_network = entrypoint.await_transaction_completed(tx_hash)\n",
"\n",
"# parsing the transaction\n",
"contract_deploy_outcome = controller.parse_deploy(transaction_on_network)"
Expand Down Expand Up @@ -1556,7 1556,7 @@
"print(tx_hash.hex())\n",
"\n",
"# waiting for transaction to complete\n",
"transaction_on_network = entrypoint.await_completed_transaction(tx_hash)\n",
"transaction_on_network = entrypoint.await_transaction_completed(tx_hash)\n",
"\n",
"# parsing transaction\n",
"parser = SmartContractTransactionsOutcomeParser(abi)\n",
Expand Down Expand Up @@ -2170,7 2170,7 @@
"tx_hash = entrypoint.send_transaction(transaction)\n",
"\n",
"# if we know that the transaction is completed, we can simply call `entrypoint.get_transaction(tx_hash)`\n",
"transaction_on_network = entrypoint.await_completed_transaction(tx_hash)\n",
"transaction_on_network = entrypoint.await_transaction_completed(tx_hash)\n",
"\n",
"# extract the token identifier\n",
"parser = TokenManagementTransactionsOutcomeParser()\n",
Expand Down Expand Up @@ -2270,7 2270,7 @@
"tx_hash = entrypoint.send_transaction(transaction)\n",
"\n",
"# waits until the transaction is processed and fetches it from the network\n",
"transaction_on_network = entrypoint.await_completed_transaction(tx_hash)\n",
"transaction_on_network = entrypoint.await_transaction_completed(tx_hash)\n",
"\n",
"# extract the roles\n",
"parser = TokenManagementTransactionsOutcomeParser()\n",
Expand Down Expand Up @@ -2376,7 2376,7 @@
"tx_hash = entrypoint.send_transaction(transaction)\n",
"\n",
"# waits until the transaction is processed and fetches it from the network\n",
"transaction_on_network = entrypoint.await_completed_transaction(tx_hash)\n",
"transaction_on_network = entrypoint.await_transaction_completed(tx_hash)\n",
"\n",
"# extract the token identifier\n",
"parser = TokenManagementTransactionsOutcomeParser()\n",
Expand Down Expand Up @@ -2506,7 2506,7 @@
"tx_hash = entrypoint.send_transaction(transaction)\n",
"\n",
"# if we know that the transaction is completed, we can simply call `get_transaction(tx_hash)`\n",
"transaction_on_network = entrypoint.await_completed_transaction(tx_hash)\n",
"transaction_on_network = entrypoint.await_transaction_completed(tx_hash)\n",
"\n",
"# extract the collection identifier\n",
"parser = TokenManagementTransactionsOutcomeParser()\n",
Expand Down Expand Up @@ -2535,7 2535,7 @@
"tx_hash = entrypoint.send_transaction(transaction)\n",
"\n",
"# waits until the transaction is processed and fetches it from the network\n",
"transaction_on_network = entrypoint.await_completed_transaction(tx_hash)\n",
"transaction_on_network = entrypoint.await_transaction_completed(tx_hash)\n",
"\n",
"# extract the nft identifier\n",
"parser = TokenManagementTransactionsOutcomeParser()\n",
Expand Down Expand Up @@ -2985,7 2985,7 @@
"tx_hash = entrypoint.send_transaction(transaction)\n",
"\n",
"# waits until the transaction is processed and fetches it from the network\n",
"transaction_on_network = entrypoint.await_completed_transaction(tx_hash)\n",
"transaction_on_network = entrypoint.await_transaction_completed(tx_hash)\n",
"\n",
"# extract the contract's address\n",
"parser = DelegationTransactionsOutcomeParser()\n",
Expand Down Expand Up @@ -4194,7 4194,7 @@
"# sets the least significant bit of the options field to `1`\n",
"transaction_computer.apply_options_for_hash_signing(transaction)\n",
"\n",
"# compute the hash for signing\n",
"# compute a keccak256 hash for signing\n",
"hash = transaction_computer.compute_hash_for_signing(transaction)\n",
"\n",
"# sign and apply the signature on the transaction\n",
Expand All @@ -4217,18 4217,15 @@
"outputs": [],
"source": [
"from pathlib import Path\n",
"from multiversx_sdk import Account, Message, MessageComputer\n",
"from multiversx_sdk import Account, Message\n",
"\n",
"account = Account.new_from_pem(Path(\"../multiversx_sdk/testutils/testwallets/alice.pem\"))\n",
"\n",
"# creating a message\n",
"message = Message(data=\"this is a test message\".encode(), address=account.address)\n",
"\n",
"# signing the message\n",
"message.signature = account.sign_message(message)\n",
"\n",
"# dictionary representation of the message\n",
"print(MessageComputer().pack_message(message))"
"message.signature = account.sign_message(message)"
]
},
{
Expand Down Expand Up @@ -4259,10 4256,7 @@
"serialized_message = message_computer.compute_bytes_for_signing(message)\n",
"\n",
"# signing the message\n",
"message.signature = secret_key.sign(serialized_message)\n",
"\n",
"# dictionary representation of the message\n",
"print(message_computer.pack_message(message))"
"message.signature = secret_key.sign(serialized_message)"
]
},
{
Expand Down Expand Up @@ -4362,6 4356,133 @@
"\n",
"print(\"Transaction is signed by Alice:\", is_signed_by_alice)"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"#### Sending messages over boundaries\n",
"\n",
"Generally speaking, signed `Message` objects are meant to be sent to a remote party (e.g. a service), which can then verify the signature.\n",
"\n",
"In order to prepare a message for transmission, you can use the `MessageComputer.packMessage()` utility method:"
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {},
"outputs": [],
"source": [
"from pathlib import Path\n",
"from multiversx_sdk import Account, Message, MessageComputer\n",
"\n",
"account = Account.new_from_pem(Path(\"../multiversx_sdk/testutils/testwallets/alice.pem\"))\n",
"\n",
"# creating a message\n",
"message = Message(data=\"this is a test message\".encode(), address=account.address)\n",
"\n",
"# signing the message\n",
"message.signature = account.sign_message(message)\n",
"\n",
"message_computer = MessageComputer()\n",
"packed_message = message_computer.pack_message(message)\n",
"print(packed_message)"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"Then, on the receiving side, you can use `MessageComputer.unpackMessage()` to reconstruct the message, prior verification:"
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {},
"outputs": [],
"source": [
"from multiversx_sdk import Address, MessageComputer, UserPublicKey\n",
"\n",
"alice = Address.new_from_bech32(\"erd1qyu5wthldzr8wx5c9ucg8kjagg0jfs53s8nr3zpz3hypefsdd8ssycr6th\")\n",
"\n",
"message_computer = MessageComputer()\n",
"\n",
"# restore message\n",
"message = message_computer.unpack_message(packed_message)\n",
"\n",
"# verify signature\n",
"public_key = UserPublicKey(alice.get_public_key())\n",
"\n",
"is_signed_by_alice = public_key.verify(message_computer.compute_bytes_for_verifying(message), message.signature)\n",
"print(\"Is signed by Alice:\", is_signed_by_alice)"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"## Start your first project\n",
"\n",
"We recommend using a Python version equal to `3.11` or higher, but the sdk should work with any version higher than `3.8`. We also recommend using a virtual environment for managing dependencies. Make sure you also have `pip` installed on your machine.\n",
"\n",
"Using a Terminal or Console, create a directory on your system (hello-multiversx in this example) and make it the working directory.\n",
"\n",
"```sh\n",
"mkdir hello-multiversx\n",
"cd hello-multiversx\n",
"```\n",
"\n",
"### Create a virtual environment\n",
"\n",
"Run the following command to create and activate your virtual environment:\n",
"\n",
"```sh\n",
"python3 -m venv ./venv\n",
"source ./venv/bin/activate\n",
"```\n",
"\n",
"After the virtual environment is created, we can install the sdk running the following command:\n",
"\n",
"```sh\n",
"pip install multiversx-sdk\n",
"```\n",
"\n",
"If your project has multiple dependencies, we recommend using a `requirements.txt` file for having all dependencies in one place. Inside the file we are going to place each dependency on a new line:\n",
"\n",
"```sh\n",
"multiversx-sdk\n",
"```\n",
"\n",
"Aditionally, we can also install it directly from GitHub. Place this line on a new line of your `requirements.txt` file. In this example, we are going to install the version `1.0.0`:\n",
"\n",
"```sh\n",
"git https://[email protected]/multiversx/[email protected]#egg=multiversx_sdk\n",
"```\n",
"\n",
"If you've places all dependencies in a `requirements.txt` file, make sure you also install them by running:\n",
"\n",
"```sh\n",
"pip install -r requirements.txt\n",
"```\n",
"\n",
"We can then create a `main.py` file where we can write our code.\n",
"\n",
"### Importing objects from the sdk\n",
"\n",
"The most common classes can be imported from package level:\n",
"\n",
"```py\n",
"from multiversx_sdk import Address, Transaction\n",
"```\n",
"\n",
"When interacting with smart contracts, we might want to make use of the abi file or other contract types. We should import those from the abi subpackage.\n",
"\n",
"```py\n",
"from multiversx_sdk.abi import Abi, BigUIntValue, StringValue\n",
"```"
]
}
],
"metadata": {
Expand Down
Loading