Skip to content

Commit

Permalink
fix wallets creation
Browse files Browse the repository at this point in the history
  • Loading branch information
yungwine committed Oct 8, 2024
1 parent ea967cd commit 0a850c1
Show file tree
Hide file tree
Showing 3 changed files with 45 additions and 58 deletions.
67 changes: 10 additions & 57 deletions pytoniq/contract/wallets/wallet.py
Original file line number Diff line number Diff line change
Expand Up @@ -59,6 +59,8 @@ class BaseWallet(Wallet):
class for user wallets such as v4r2, v3r2, etc.
"""

VERSION = None

@classmethod
async def from_private_key(cls, provider: LiteClientLike, private_key: bytes, wc: int = 0,
wallet_id: typing.Optional[int] = None, version: str = 'v3r2'):
Expand All @@ -78,6 +80,7 @@ async def from_private_key(cls, provider: LiteClientLike, private_key: bytes, wc
@classmethod
async def from_mnemonic(cls, provider: LiteClientLike, mnemonics: typing.Union[list, str], wc: int = 0,
wallet_id: typing.Optional[int] = None, version: str = 'v3r2'):
version = cls.VERSION or version
if isinstance(mnemonics, str):
mnemonics = mnemonics.split()
assert mnemonic_is_valid(mnemonics), 'mnemonics are invalid!'
Expand All @@ -94,6 +97,7 @@ async def create(cls, provider: LiteClientLike, wc: int = 0, wallet_id: typing.O
:param version: wallet version
:return: mnemonics and Wallet instance of provided version
"""
version = cls.VERSION or version
mnemo = mnemonic_new(24)
return mnemo, await cls.from_mnemonic(provider, mnemo, wc, wallet_id, version)

Expand Down Expand Up @@ -282,83 +286,32 @@ async def is_plugin_installed(self, address: Address) -> bool:

class WalletV3R1(WalletV3):

VERSION = 'v3r1'

@classmethod
async def from_data(cls, provider: LiteClientLike, public_key: bytes, wc: int = 0,
wallet_id: typing.Optional[int] = None, **kwargs):
return await super().from_code_and_data(provider=provider, code=WALLET_V3_R1_CODE, public_key=public_key, wc=wc,
wallet_id=wallet_id, **kwargs)

@classmethod
async def from_mnemonic(cls, provider: LiteClientLike, mnemonics: typing.Union[list, str], wc: int = 0,
wallet_id: typing.Optional[int] = None):
if isinstance(mnemonics, str):
mnemonics = mnemonics.split()
assert mnemonic_is_valid(mnemonics), 'mnemonics are invalid!'
_, private_key = mnemonic_to_private_key(mnemonics)
return await super().from_private_key(provider, private_key, wc, wallet_id, 'v3r1')

@classmethod
async def create(cls, provider: LiteClientLike, wc: int = 0, wallet_id: typing.Optional[int] = None):
"""
:param provider: provider
:param wc: wallet workchain
:param wallet_id: subwallet_id
:return: mnemonics and Wallet instance of provided version
"""
return await super().create(provider=provider, wc=wc, wallet_id=wallet_id, version='v3r1')


class WalletV3R2(WalletV3):

VERSION = 'v3r2'

@classmethod
async def from_data(cls, provider: LiteClientLike, public_key: bytes, wc: int = 0,
wallet_id: typing.Optional[int] = None, **kwargs):
return await super().from_code_and_data(provider=provider, code=WALLET_V3_R2_CODE, public_key=public_key, wc=wc,
wallet_id=wallet_id, **kwargs)

@classmethod
async def from_mnemonic(cls, provider: LiteClientLike, mnemonics: typing.Union[list, str], wc: int = 0,
wallet_id: typing.Optional[int] = None):
if isinstance(mnemonics, str):
mnemonics = mnemonics.split()
assert mnemonic_is_valid(mnemonics), 'mnemonics are invalid!'
_, private_key = mnemonic_to_private_key(mnemonics)
return await super().from_private_key(provider, private_key, wc, wallet_id, 'v3r2')

@classmethod
async def create(cls, provider: LiteClientLike, wc: int = 0, wallet_id: typing.Optional[int] = None):
"""
:param provider: provider
:param wc: wallet workchain
:param wallet_id: subwallet_id
:return: mnemonics and Wallet instance of provided version
"""
return await super().create(provider=provider, wc=wc, wallet_id=wallet_id, version='v3r2')


class WalletV4R2(WalletV4):

VERSION = 'v4r2'

@classmethod
async def from_data(cls, provider: LiteClientLike, public_key: bytes, wc: int = 0,
wallet_id: typing.Optional[int] = None, **kwargs):
return await super().from_code_and_data(provider=provider, code=WALLET_V4_R2_CODE, public_key=public_key, wc=wc,
wallet_id=wallet_id, **kwargs)

@classmethod
async def from_mnemonic(cls, provider: LiteClientLike, mnemonics: typing.Union[list, str], wc: int = 0,
wallet_id: typing.Optional[int] = None):
if isinstance(mnemonics, str):
mnemonics = mnemonics.split()
assert mnemonic_is_valid(mnemonics), 'mnemonics are invalid!'
_, private_key = mnemonic_to_private_key(mnemonics)
return await super().from_private_key(provider, private_key, wc, wallet_id, 'v4r2')

@classmethod
async def create(cls, provider: LiteClientLike, wc: int = 0, wallet_id: typing.Optional[int] = None):
"""
:param provider: provider
:param wc: wallet workchain
:param wallet_id: subwallet_id
:return: mnemonics and Wallet instance of provided version
"""
return await super().create(provider=provider, wc=wc, wallet_id=wallet_id, version='v4r2')
2 changes: 1 addition & 1 deletion setup.py
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@

setuptools.setup(
name="pytoniq",
version="0.1.39",
version="0.1.40",
author="Maksim Kurbatov",
author_email="[email protected]",
description="TON Blockchain SDK",
Expand Down
34 changes: 34 additions & 0 deletions tests/test_wallet.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,34 @@
from unittest.mock import Mock

import pytest
import random

import pytest_asyncio

from pytoniq import BaseWallet, WalletV3R1, WalletV3R2, WalletV4R2


@pytest_asyncio.fixture
async def client():
c = Mock()

async def raw_get_account_state(*args, **kwargs):
return None, None

c.raw_get_account_state = raw_get_account_state
return c


@pytest.mark.asyncio
async def test_wallets(client):
m0, w0 = await BaseWallet.create(client, version='v3r1')
m1, w1 = await WalletV3R2.create(client)
m2, w2 = await WalletV4R2.create(client)
w3 = await WalletV3R2.from_mnemonic(client, mnemonics=m1)
w4 = await WalletV4R2.from_mnemonic(client, mnemonics=m2)
w5 = await BaseWallet.from_mnemonic(client, mnemonics=m0, version='v3r1')
w6 = await WalletV3R1.from_mnemonic(client, mnemonics=m0)

assert w0.address == w5.address == w6.address
assert w1.address == w3.address
assert w2.address == w4.address

0 comments on commit 0a850c1

Please sign in to comment.