Skip to content

Commit

Permalink
fixes
Browse files Browse the repository at this point in the history
  • Loading branch information
popenta committed Jan 29, 2024
1 parent 92e675f commit cfccdf4
Show file tree
Hide file tree
Showing 11 changed files with 111 additions and 33 deletions.
2 changes: 1 addition & 1 deletion README.md
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
# mx-sdk-py

The Python SDK for interacting with MultiversX.
The Python SDK for interacting with MultiversX. It's an all in one sdk that can be used to create wallets, sign and broadcast transactions and many more.

## Development setup

Expand Down
39 changes: 39 additions & 0 deletions multiversx_sdk/__init__.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,39 @@
from multiversx_sdk.core.account import AccountNonceHolder
from multiversx_sdk.core.address import (Address, AddressComputer,
AddressFactory)
from multiversx_sdk.core.code_metadata import CodeMetadata
from multiversx_sdk.core.contract_query import ContractQuery
from multiversx_sdk.core.contract_query_builder import ContractQueryBuilder
from multiversx_sdk.core.message import Message, MessageComputer
from multiversx_sdk.core.token_payment import TokenPayment
from multiversx_sdk.core.tokens import (Token, TokenComputer,
TokenIdentifierParts, TokenTransfer)
from multiversx_sdk.core.transaction import Transaction, TransactionComputer
from multiversx_sdk.core.transaction_payload import TransactionPayload
from multiversx_sdk.network_providers.api_network_provider import \
ApiNetworkProvider
from multiversx_sdk.network_providers.errors import GenericError
from multiversx_sdk.network_providers.proxy_network_provider import \
ProxyNetworkProvider
from multiversx_sdk.network_providers.resources import GenericResponse
from multiversx_sdk.wallet.mnemonic import Mnemonic
from multiversx_sdk.wallet.user_keys import UserPublicKey, UserSecretKey
from multiversx_sdk.wallet.user_pem import UserPEM
from multiversx_sdk.wallet.user_signer import UserSigner
from multiversx_sdk.wallet.user_verifer import UserVerifier
from multiversx_sdk.wallet.user_wallet import UserWallet
from multiversx_sdk.wallet.validator_keys import (ValidatorPublicKey,
ValidatorSecretKey)
from multiversx_sdk.wallet.validator_signer import ValidatorSigner
from multiversx_sdk.wallet.validator_verifier import ValidatorVerifier

__all__ = [
"AccountNonceHolder", "Address", "AddressFactory", "AddressComputer",
"Transaction", "TransactionPayload", "TransactionComputer",
"Message", "MessageComputer", "CodeMetadata", "TokenPayment",
"ContractQuery", "ContractQueryBuilder",
"Token", "TokenComputer", "TokenTransfer", "TokenIdentifierParts",
"GenericError", "GenericResponse", "ApiNetworkProvider", "ProxyNetworkProvider",
"UserSigner", "Mnemonic", "UserSecretKey", "UserPublicKey", "ValidatorSecretKey",
"ValidatorPublicKey", "UserVerifier", "ValidatorSigner", "ValidatorVerifier", "UserWallet", "UserPEM"
]
Original file line number Diff line number Diff line change
Expand Up @@ -12,10 +12,11 @@
class TestSmartContractTransactionsFactory:
config = TransactionsFactoryConfig("D")
factory = SmartContractTransactionsFactory(config, TokenComputer())
testdata = Path(__file__).parent.parent.parent / "testutils" / "testdata"

def test_create_transaction_for_deploy(self):
sender = Address.new_from_bech32("erd1qyu5wthldzr8wx5c9ucg8kjagg0jfs53s8nr3zpz3hypefsdd8ssycr6th")
contract = Path(__file__).parent.parent / "testutils" / "testdata" / "adder.wasm"
contract = self.testdata / "adder.wasm"
gas_limit = 6000000
args = [0]

Expand Down Expand Up @@ -188,7 +189,7 @@ def test_create_transaction_for_execute_and_send_multiple_nfts(self):
def test_create_transaction_for_upgrade(self):
sender = Address.new_from_bech32("erd1qyu5wthldzr8wx5c9ucg8kjagg0jfs53s8nr3zpz3hypefsdd8ssycr6th")
contract_address = Address.new_from_bech32("erd1qqqqqqqqqqqqqpgqhy6nl6zq07rnzry8uyh6rtyq0uzgtk3e69fqgtz9l4")
contract = Path(__file__).parent.parent / "testutils" / "testdata" / "adder.wasm"
contract = self.testdata / "adder.wasm"
gas_limit = 6000000
args = [0]

Expand Down
2 changes: 1 addition & 1 deletion multiversx_sdk/network_providers/constants.py
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
from multiversx_sdk.core import Address
from multiversx_sdk.core.address import Address

ESDT_CONTRACT_ADDRESS = Address.new_from_bech32("erd1qqqqqqqqqqqqqqqpqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqzllls8a5w6u")
METACHAIN_ID = 4294967295
Expand Down
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
from multiversx_sdk.core import Transaction
from multiversx_sdk.core.address import Address
from multiversx_sdk.core.transaction import Transaction
from multiversx_sdk.network_providers.proxy_network_provider import (
ContractQuery, ProxyNetworkProvider)

Expand Down
18 changes: 10 additions & 8 deletions multiversx_sdk/wallet/pem_entry_test.py
Original file line number Diff line number Diff line change
Expand Up @@ -4,16 +4,18 @@
from multiversx_sdk.wallet.constants import USER_SEED_LENGTH
from multiversx_sdk.wallet.pem_entry import PemEntry

testwallets = Path(__file__).parent.parent / "testutils" / "testwallets"


def test_from_text_all():
text = Path("./multiversx_sdk.wallet/testdata/alice.pem").read_text()
text = (testwallets / "alice.pem").read_text()
entries = PemEntry.from_text_all(text)
entry = entries[0]
assert len(entries) == 1
assert entry.label == "erd1qyu5wthldzr8wx5c9ucg8kjagg0jfs53s8nr3zpz3hypefsdd8ssycr6th"
assert entry.message[0:USER_SEED_LENGTH].hex() == "413f42575f7f26fad3317a778771212fdb80245850981e48b58a4f25e344e8f9"

text = Path("./multiversx_sdk.wallet/testdata/multipleUserKeys.pem").read_text()
text = (testwallets / "multipleUserKeys.pem").read_text()
entries = PemEntry.from_text_all(text)
entry = entries[0]
assert len(entries) == 3
Expand All @@ -30,12 +32,12 @@ def test_from_text_all():


def test_from_text_all_for_validators():
text = Path("./multiversx_sdk.wallet/testdata/validatorKey00.pem").read_text()
text = (testwallets / "validatorKey00.pem").read_text()
entry = PemEntry.from_text_all(text)[0]
assert entry.label == "e7beaa95b3877f47348df4dd1cb578a4f7cabf7a20bfeefe5cdd263878ff132b765e04fef6f40c93512b666c47ed7719b8902f6c922c04247989b7137e837cc81a62e54712471c97a2ddab75aa9c2f58f813ed4c0fa722bde0ab718bff382208"
assert entry.message.hex() == "7cff99bd671502db7d15bc8abc0c9a804fb925406fbdd50f1e4c17a4cd774247"

text = Path("./multiversx_sdk.wallet/testdata/multipleValidatorKeys.pem").read_text()
text = (testwallets / "multipleValidatorKeys.pem").read_text()
entries = PemEntry.from_text_all(text)

entry = entries[0]
Expand All @@ -56,18 +58,18 @@ def test_from_text_all_for_validators():


def test_to_text():
text = Path("./multiversx_sdk.wallet/testdata/alice.pem").read_text()
text = (testwallets / "alice.pem").read_text()
assert PemEntry.from_text_all(text)[0].to_text() == text.strip()

text = Path("./multiversx_sdk.wallet/testdata/validatorKey00.pem").read_text()
text = (testwallets / "validatorKey00.pem").read_text()
assert PemEntry.from_text_all(text)[0].to_text() == text.strip()

text = Path("./multiversx_sdk.wallet/testdata/multipleUserKeys.pem").read_text()
text = (testwallets / "multipleUserKeys.pem").read_text()
entries = PemEntry.from_text_all(text)
text_actual = "\n".join([entry.to_text() for entry in entries])
assert text_actual == text.strip()

text = Path("./multiversx_sdk.wallet/testdata/multipleValidatorKeys.pem").read_text()
text = (testwallets / "multipleValidatorKeys.pem").read_text()
entries = PemEntry.from_text_all(text)
text_actual = "\n".join([entry.to_text() for entry in entries])
assert text_actual == text.strip()
2 changes: 1 addition & 1 deletion multiversx_sdk/wallet/user_keys.py
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
import nacl.signing

from multiversx_sdk.core import Address
from multiversx_sdk.core.address import Address
from multiversx_sdk.wallet.constants import (USER_PUBKEY_LENGTH,
USER_SEED_LENGTH)
from multiversx_sdk.wallet.errors import (ErrBadPublicKeyLength,
Expand Down
33 changes: 17 additions & 16 deletions multiversx_sdk/wallet/user_test.py
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@
from multiversx_sdk.wallet.user_verifer import UserVerifier
from multiversx_sdk.wallet.user_wallet import UserWallet

testwallets = Path(__file__).parent.parent / "testutils" / "testwallets"
DUMMY_MNEMONIC = "moral volcano peasant pass circle pen over picture flat shop clap goat never lyrics gather prepare woman film husband gravity behind test tiger improve"


Expand All @@ -32,13 +33,13 @@ def test_user_secret_key_generate_public_key():


def test_user_signer_from_pem_file():
pubkey = UserSigner.from_pem_file(Path("./multiversx_sdk.wallet/testdata/alice.pem"), 0).get_pubkey()
pubkey = UserSigner.from_pem_file(testwallets / "alice.pem", 0).get_pubkey()
assert Address(pubkey.buffer, "erd").to_bech32() == "erd1qyu5wthldzr8wx5c9ucg8kjagg0jfs53s8nr3zpz3hypefsdd8ssycr6th"

pubkey = UserSigner.from_pem_file(Path("./multiversx_sdk.wallet/testdata/bob.pem"), 0).get_pubkey()
pubkey = UserSigner.from_pem_file(testwallets / "bob.pem", 0).get_pubkey()
assert Address(pubkey.buffer, "erd").to_bech32() == "erd1spyavw0956vq68xj8y4tenjpq2wd5a9p2c6j8gsz7ztyrnpxrruqzu66jx"

pubkey = UserSigner.from_pem_file(Path("./multiversx_sdk.wallet/testdata/carol.pem"), 0).get_pubkey()
pubkey = UserSigner.from_pem_file(testwallets / "carol.pem", 0).get_pubkey()
assert Address(pubkey.buffer, "erd").to_bech32() == "erd1k2s324ww2g0yj38qn2ch2jwctdy8mnfxep94q9arncc6xecg3xaq6mjse8"


Expand All @@ -64,17 +65,17 @@ def test_user_wallet_to_keyfile_object_using_known_test_wallets_with_their_rando
id="65894f35-d142-41d2-9335-6ad02e0ed0be"
))

alice_saved_path = Path("./multiversx_sdk.wallet/testdata/alice.saved.json")
bob_saved_path = Path("./multiversx_sdk.wallet/testdata/bob.saved.json")
carol_saved_path = Path("./multiversx_sdk.wallet/testdata/carol.saved.json")
alice_saved_path = testwallets / "alice.saved.json"
bob_saved_path = testwallets / "bob.saved.json"
carol_saved_path = testwallets / "carol.saved.json"

alice_wallet.save(alice_saved_path, "erd")
bob_wallet.save(bob_saved_path, "erd")
carol_wallet.save(carol_saved_path, "erd")

assert alice_saved_path.read_text().strip() == Path("./multiversx_sdk.wallet/testdata/alice.json").read_text().strip()
assert bob_saved_path.read_text().strip() == Path("./multiversx_sdk.wallet/testdata/bob.json").read_text().strip()
assert carol_saved_path.read_text().strip() == Path("./multiversx_sdk.wallet/testdata/carol.json").read_text().strip()
assert alice_saved_path.read_text().strip() == (testwallets / "alice.json").read_text().strip()
assert bob_saved_path.read_text().strip() == (testwallets / "bob.json").read_text().strip()
assert carol_saved_path.read_text().strip() == (testwallets / "carol.json").read_text().strip()

alice_saved_path.unlink()
bob_saved_path.unlink()
Expand Down Expand Up @@ -119,7 +120,7 @@ def test_sign_transaction():
options=0
)

signer = UserSigner.from_pem_file(Path("./multiversx_sdk.wallet/testdata/alice.pem"))
signer = UserSigner.from_pem_file(testwallets / "alice.pem")
verifier = UserVerifier.from_address(Address.new_from_bech32("erd1qyu5wthldzr8wx5c9ucg8kjagg0jfs53s8nr3zpz3hypefsdd8ssycr6th"))
transaction_computer = TransactionComputer()

Expand All @@ -132,7 +133,7 @@ def test_sign_message():
message = Message("hello".encode())
message_computer = MessageComputer()

signer = UserSigner.from_pem_file(Path("./multiversx_sdk.wallet/testdata/alice.pem"))
signer = UserSigner.from_pem_file(testwallets / "alice.pem")
verifier = UserVerifier.from_address(Address.new_from_bech32("erd1qyu5wthldzr8wx5c9ucg8kjagg0jfs53s8nr3zpz3hypefsdd8ssycr6th"))

message.signature = signer.sign(message_computer.compute_bytes_for_signing(message))
Expand All @@ -141,7 +142,7 @@ def test_sign_message():


def test_user_pem_save():
path = Path("./multiversx_sdk.wallet/testdata/alice.pem")
path = testwallets / "alice.pem"
path_saved = path.with_suffix(".saved")
content_expected = path.read_text().strip()

Expand All @@ -154,14 +155,14 @@ def test_user_pem_save():


def test_load_secret_key_but_without_kind_field():
keystore_path = Path("./multiversx_sdk.wallet/testdata/withoutKind.json")
keystore_path = testwallets / "withoutKind.json"
secret_key = UserWallet.load_secret_key(keystore_path, "password")
actual_address = (secret_key.generate_public_key().to_address("erd")).to_bech32()
assert actual_address == "erd1qyu5wthldzr8wx5c9ucg8kjagg0jfs53s8nr3zpz3hypefsdd8ssycr6th"


def test_load_secret_key_with_unecessary_address_index():
keystore_path = Path("./multiversx_sdk.wallet/testdata/alice.json")
keystore_path = testwallets / "alice.json"

with pytest.raises(Exception, match="address_index must not be provided when kind == 'secretKey'"):
UserWallet.load_secret_key(keystore_path, "password", 42)
Expand All @@ -177,7 +178,7 @@ def test_create_keystore_file_with_mnemonic():


def test_create_keystore_with_mnemonic_with_randomness():
expected_dummy_wallet_json = Path("./multiversx_sdk.wallet/testdata/withDummyMnemonic.json").read_text()
expected_dummy_wallet_json = (testwallets / "withDummyMnemonic.json").read_text()
expected_dummy_wallet_dict = json.loads(expected_dummy_wallet_json)

randomness = Randomness(
Expand All @@ -193,7 +194,7 @@ def test_create_keystore_with_mnemonic_with_randomness():


def test_load_secret_key_with_mnemonic():
keystore_path = Path("./multiversx_sdk.wallet/testdata/withDummyMnemonic.json")
keystore_path = testwallets / "withDummyMnemonic.json"

assert UserWallet.load_secret_key(keystore_path, "password", 1).generate_public_key().to_address("erd").to_bech32() == "erd1spyavw0956vq68xj8y4tenjpq2wd5a9p2c6j8gsz7ztyrnpxrruqzu66jx"
assert UserWallet.load_secret_key(keystore_path, "password", 2).generate_public_key().to_address("erd").to_bech32() == "erd1k2s324ww2g0yj38qn2ch2jwctdy8mnfxep94q9arncc6xecg3xaq6mjse8"
Expand Down
2 changes: 1 addition & 1 deletion multiversx_sdk/wallet/user_verifer.py
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
from multiversx_sdk.core import Address
from multiversx_sdk.core.address import Address
from multiversx_sdk.wallet.interfaces import IAddress, ISignature
from multiversx_sdk.wallet.user_keys import UserPublicKey

Expand Down
6 changes: 4 additions & 2 deletions multiversx_sdk/wallet/validator_test.py
Original file line number Diff line number Diff line change
Expand Up @@ -6,13 +6,15 @@
from multiversx_sdk.wallet.validator_signer import ValidatorSigner
from multiversx_sdk.wallet.validator_verifier import ValidatorVerifier

testwallets = Path(__file__).parent.parent / "testutils" / "testwallets"


def test_validator_secret_key_generate_public_key():
assert ValidatorSecretKey.from_string("7cff99bd671502db7d15bc8abc0c9a804fb925406fbdd50f1e4c17a4cd774247").generate_public_key().hex() == "e7beaa95b3877f47348df4dd1cb578a4f7cabf7a20bfeefe5cdd263878ff132b765e04fef6f40c93512b666c47ed7719b8902f6c922c04247989b7137e837cc81a62e54712471c97a2ddab75aa9c2f58f813ed4c0fa722bde0ab718bff382208"


def test_sign_message():
signer = ValidatorSigner.from_pem_file(Path("./multiversx_sdk.wallet/testdata/validatorKey00.pem"))
signer = ValidatorSigner.from_pem_file(testwallets / "validatorKey00.pem")
message = b"hello"
signature = signer.sign(message)
assert signature.hex() == "84fd0a3a9d4f1ea2d4b40c6da67f9b786284a1c3895b7253fec7311597cda3f757862bb0690a92a13ce612c33889fd86"
Expand All @@ -31,7 +33,7 @@ def test_verify_message():


def test_pem_save():
path = Path("./multiversx_sdk.wallet/testdata/validatorKey00.pem")
path = testwallets / "validatorKey00.pem"
path_saved = path.with_suffix(".saved")

with open(path) as f:
Expand Down
33 changes: 33 additions & 0 deletions pyproject.toml
Original file line number Diff line number Diff line change
@@ -0,0 +1,33 @@
[build-system]
requires = ["hatchling"]
build-backend = "hatchling.build"

[tool.hatch.metadata]
allow-direct-references = true

[project]
name = "multiversx-sdk"
version = "0.1.0"
authors = [
{ name="MultiversX" },
]
license = "MIT"
description = "The MultiversX Python SDK."
readme = "README.md"
requires-python = ">=3.8"
classifiers = [
"Programming Language :: Python :: 3",
"License :: OSI Approved :: MIT License",
"Operating System :: OS Independent",
]
dependencies = [
"pycryptodomex==3.16.0",
"protobuf==3.20.1",
"cryptography==36.0.2",
"pynacl==1.5.0",
"mnemonic==0.20,"
"requests==2.31.0",
]

[project.urls]
"Homepage" = "https://github.com/multiversx/mx-sdk-py"

0 comments on commit cfccdf4

Please sign in to comment.