2

I want to Create Wallet / Generate address like in ethereum we use web3.py but how could I do same thing in polkadot by using python?

1
  • Could you select an answer if any one of them answers your question?
    – AurevoirXavier
    Commented Sep 8, 2022 at 13:42

2 Answers 2

3

It depends on the chain.

Most of the chains are using SR25519.

Check https://github.com/polkascan/py-sr25519-bindings.

Example:

# you need to install py-sr25519-bindings first.

import sr25519
from random import randbytes

message = b"test"

raw_seed = randbytes(32)
public_key, private_key = sr25519.pair_from_seed(raw_seed)

print(f'Raw seed: {raw_seed.hex()}')
print(f'Public Key: {public_key.hex()}')
print(f'Private Key: {private_key.hex()}')

# Generate signature
signature = sr25519.sign(
    (public_key, private_key),
    message
)

print('Signature', signature.hex())

# Verify message with signature
if sr25519.verify(signature, message, public_key):
    print('Verified')
Raw seed: 2a6cdceb2fc16b07b60cec685bfe171c1d0b989e962dbf41f0e044060de2aa54
Public Key: c20c0de890c5a809a9837b439e1e7c86bb23e31932e367b66d14a82d5f429c6e
Private Key: 189c98cd0c669869df80dead8d310a8430f8e35705036f41a79555dc513a7509ee96d314905deb7dbb1f0830343694756c10e812124201d7bd44530874b6b4b9
Signature c64d79859ef905184556f87663e1b8a22eda672328f0973d293aaf379bbc5a1e074572ff2245837a04217603a5316eeaf504bbda7b4427c0393dfe7d57020c87
Verified

Some chains like Moonbeam, use ECDSA. So, web3.py works for them.

11
  • I want to create a wallet on polkadot Commented Sep 1, 2022 at 8:49
  • 1
    Polkadot uses SR25519. So, github.com/polkascan/py-sr25519-bindings is the Python library for creating a wallet on Polkadot.
    – AurevoirXavier
    Commented Sep 1, 2022 at 9:10
  • can you further elaborate it how could I create a wallet by using this library if possible it will be very helpful for me. Commented Sep 1, 2022 at 9:14
  • Ah, sorry. I made a mistake here. Let me update my answer.
    – AurevoirXavier
    Commented Sep 1, 2022 at 9:16
  • Alright I'm waiting Commented Sep 1, 2022 at 9:18
0

You can use https://github.com/polkascan/py-substrate-interface for that, it has a lot of helper functions to interface with Substrate:

from substrateinterface import Keypair
keypair = Keypair.create_from_mnemonic(Keypair.generate_mnemonic(), crypto_type=KeypairType.SR25519)

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.