Создание криптокошельков на PYTHON
Всех приветствую, в этой мини статье расскажу как создавать различные криптокошельки
По факту я просто покажу библиотеку HDWallet, пару примеров увидите.
Функция для создания BTC кошелька:
Create BTC:
from hdwallet import HDWallet
from hdwallet.utils import generate_entropy
from hdwallet.symbols import BTC as BITCOIN
from hdwallet import BIP84HDWallet
import json
from typing import Optional
def create_btc():
STRENGTH: int = 160
LANGUAGE: str = "english"
ENTROPY: str = generate_entropy(strength=STRENGTH)
PASSPHRASE: Optional[str] = None
hdwallet: HDWallet = HDWallet(symbol=BITCOIN, use_default_path=False)
hdwallet.from_entropy(
entropy=ENTROPY, language=LANGUAGE, passphrase=PASSPHRASE
)
hdwallet.from_index(44, hardened=True)
hdwallet.from_index(0, hardened=True)
hdwallet.from_index(0, hardened=True)
hdwallet.from_index(0)
hdwallet.from_index(0)
return json.dumps(hdwallet.dumps(), indent=4, ensure_ascii=False)
create = create_btc()
print(create)
JSON:
{
"cryptocurrency": "Bitcoin",
"symbol": "BTC",
"network": "mainnet",
"strength": 160,
"entropy": "beaa46ca70196e2fc3018624fd51863048f1dc75",
"mnemonic": "salt fall raw then notice blood army cotton ceiling tunnel ghost gauge monitor until stuff",
"language": "english",
"passphrase": null,
"seed": "9519d084ac4a10b76ddd0e8cdf085a2454b3884b45be063ba8806dff67f6f3015626b7ba7823194574b6f3ba5aa5cf0427a35e7dc60abb87991330562c6751c7",
"root_xprivate_key": "xprv9s21ZrQH143K2S9Thfi6nxTWGo7cHgSsSBDGrjGc6vF5BhYjtdVmp1jDk9inxqgJ8hR462f76AvGaqv3eAo3HrXdCWow5AVPuihAmW8xSvM",
"root_xpublic_key": "xpub661MyMwAqRbcEvDvohF7A6QEppx6h9AioQ8sf7gDfFn44VstSAp2Mp3hbTrw8Gjecmm8tw3Fq4PfoAgqCMzjofjBxDPBJ4ZgjK5A5YXCPAR",
"xprivate_key": "xprvA3Bu4ywVedK1cdUbWuDaSWdMYr2koPLc2vGpQV51PQ9Wq8JywWWVPi1ybxwNTnNqqBPV5PHfvKuzX4xXe62JhKmyCayT1Q663auFH4E4o1u",
"xpublic_key": "xpub6GBFUVUPUzsJq7Z4cvkaoea66ssFCr4TQ9CRCsUcwjgVhve8V3pjwWLTTFhi3hotpjqAZ2JFBMuQQGMzveenH4hpP5tPY3HGKg2R7cToJJK",
"uncompressed": "1139340a0c1b2dd349154234a5076b660e1d05f1cf96fbe4b114b06d20c65524db73488a0acd7ce3976402342536d27c6947011bd76ce585ec7fc1f96f2549a5",
"compressed": "031139340a0c1b2dd349154234a5076b660e1d05f1cf96fbe4b114b06d20c65524",
"chain_code": "1a08265a5476727be4bf16badd124b74f1784de7f19006c76133c53591877e4e",
"private_key": "2cdd1ccc7495448f1e3563f4a60a25751fe1e1cf87382a59caae01e22caf0c13",
"public_key": "031139340a0c1b2dd349154234a5076b660e1d05f1cf96fbe4b114b06d20c65524",
"wif": "KxivKSbcTtuqe6AzsgdEvgm19yW577CTJ9iKVDTy3pkMxN78xikq",
"finger_print": "453ec172",
"semantic": "p2pkh",
"path": "m/44'/0'/0'/0/0",
"hash": "453ec172632637af4c639fa018dcc6e9887430d8",
"addresses": {
"p2pkh": "17K8oaNi1nDLQ3DffKYzFuspbfquU2iqnq",
"p2sh": "3PwyCFPYEks5F53Ug1Cr97aphGnGzCAPhD",
"p2wpkh": "bc1qg5lvzunrycm67nrrn7sp3hxxaxy8gvxc6hewjd",
"p2wpkh_in_p2sh": "3JLwWDc5Wpo9LfqMPjbr5DPxWCZ9HYwx7A",
"p2wsh": "bc1qg838dq0zyh2wmkpu3zurusclsvragrvswewukevyaszgedxy7ulstphq02",
"p2wsh_in_p2sh": "3GUHFkeKrwZc7LND5UfnTeh4YjySTHaTTa"
}
}
Получение дочернего адреса биткоина:
Python:
from hdwallet import HDWallet
from hdwallet.utils import generate_entropy
from hdwallet.symbols import BTC as BITCOIN
from hdwallet import BIP84HDWallet
import json
from typing import Optional
def give_child(mnemonic):
bip84_hdwallet: BIP84HDWallet = BIP84HDWallet(symbol=BITCOIN, account=0, change=False, address=1)# меняя парамеирт address получаем другой дочерний адрес
bip84_hdwallet.from_mnemonic(mnemonic=mnemonic) #mnemonic это сид фраза
ad = bip84_hdwallet.address()
return ad
b = give_child('salt fall raw then notice blood army cotton ceiling tunnel ghost gauge monitor until stuff')
print(b)
#вывод: bc1qcufs2mc5zepa8tr530euqgds959m06qgd8mcqx
Данная библиотека поддерживает и другие монеты: LTC, ETH, TRON и много других.
Список поддерживаемых монет и подробную документацию с примерами можно посомтреть на их PyPi: https://pypi.org/project/hdwallet/