Skip to content

Commit 4e6324a

Browse files
committed
Extract pool wallet pubkey generation to WSM
1 parent a05a0c5 commit 4e6324a

File tree

2 files changed

+28
-25
lines changed

2 files changed

+28
-25
lines changed

chia/wallet/wallet_rpc_api.py

Lines changed: 10 additions & 25 deletions
Original file line numberDiff line numberDiff line change
@@ -18,7 +18,13 @@
1818
from chia.data_layer.data_layer_util import DLProof, VerifyProofResponse, dl_verify_proof
1919
from chia.data_layer.data_layer_wallet import DataLayerWallet, Mirror
2020
from chia.pools.pool_wallet import PoolWallet
21-
from chia.pools.pool_wallet_info import FARMING_TO_POOL, PoolState, PoolWalletInfo, create_pool_state
21+
from chia.pools.pool_wallet_info import (
22+
FARMING_TO_POOL,
23+
PoolState,
24+
PoolWalletInfo,
25+
create_pool_state,
26+
initial_pool_state_from_dict,
27+
)
2228
from chia.protocols.outbound_message import NodeType
2329
from chia.rpc.rpc_server import Endpoint, EndpointResult, default_get_connections
2430
from chia.rpc.util import ALL_TRANSLATION_LAYERS, RpcEndpoint, marshal
@@ -47,7 +53,6 @@
4753
parse_timelock_info,
4854
)
4955
from chia.wallet.derive_keys import (
50-
MAX_POOL_WALLETS,
5156
master_sk_to_farmer_sk,
5257
master_sk_to_pool_sk,
5358
match_address_to_sk,
@@ -1245,32 +1250,12 @@ async def create_new_wallet( # type: ignore[return]
12451250
)
12461251
elif request.wallet_type == CreateNewWalletType.POOL_WALLET:
12471252
if request.mode == WalletCreationMode.NEW:
1248-
owner_puzzle_hash: bytes32 = await action_scope.get_puzzle_hash(self.service.wallet_state_manager)
1249-
1250-
from chia.pools.pool_wallet_info import initial_pool_state_from_dict
1251-
12521253
async with self.service.wallet_state_manager.lock:
1253-
# We assign a pseudo unique id to each pool wallet, so that each one gets its own deterministic
1254-
# owner and auth keys. The public keys will go on the blockchain, and the private keys can be found
1255-
# using the root SK and trying each index from zero. The indexes are not fully unique though,
1256-
# because the PoolWallet is not created until the tx gets confirmed on chain. Therefore if we
1257-
# make multiple pool wallets at the same time, they will have the same ID.
1258-
max_pwi = 1
1259-
for _, wallet in self.service.wallet_state_manager.wallets.items():
1260-
if wallet.type() == WalletType.POOLING_WALLET:
1261-
max_pwi += 1
1262-
1263-
if max_pwi + 1 >= (MAX_POOL_WALLETS - 1):
1264-
raise ValueError(f"Too many pool wallets ({max_pwi}), cannot create any more on this key.")
1265-
1266-
owner_pk: G1Element = self.service.wallet_state_manager.main_wallet.hardened_pubkey_for_path(
1267-
# copied from chia.wallet.derive_keys. Could maybe be an exported constant in the future.
1268-
[12381, 8444, 5, max_pwi]
1269-
)
1270-
12711254
assert request.initial_target_state is not None # mypy doesn't know about our __post_init__
12721255
initial_target_state = initial_pool_state_from_dict(
1273-
request.initial_target_state, owner_pk, owner_puzzle_hash
1256+
request.initial_target_state,
1257+
self.service.wallet_state_manager.new_pool_wallet_pubkey(),
1258+
await action_scope.get_puzzle_hash(self.service.wallet_state_manager),
12741259
)
12751260
assert initial_target_state is not None
12761261

chia/wallet/wallet_state_manager.py

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -59,10 +59,12 @@
5959
from chia.wallet.db_wallet.db_wallet_puzzles import MIRROR_PUZZLE_HASH
6060
from chia.wallet.derivation_record import DerivationRecord
6161
from chia.wallet.derive_keys import (
62+
MAX_POOL_WALLETS,
6263
_derive_path,
6364
_derive_pk_unhardened,
6465
master_pk_to_wallet_pk_unhardened,
6566
master_pk_to_wallet_pk_unhardened_intermediate,
67+
master_sk_to_singleton_owner_sk,
6668
master_sk_to_wallet_sk,
6769
master_sk_to_wallet_sk_intermediate,
6870
master_sk_to_wallet_sk_unhardened,
@@ -2988,3 +2990,19 @@ async def combine_coins(
29882990
coins=set(coins),
29892991
extra_conditions=extra_conditions,
29902992
)
2993+
2994+
def new_pool_wallet_pubkey(self) -> G1Element:
2995+
# We assign a pseudo unique id to each pool wallet, so that each one gets its own deterministic
2996+
# owner and auth keys. The public keys will go on the blockchain, and the private keys can be found
2997+
# using the root SK and trying each index from zero. The indexes are not fully unique though,
2998+
# because the PoolWallet is not created until the tx gets confirmed on chain. Therefore if we
2999+
# make multiple pool wallets at the same time, they will have the same ID.
3000+
max_pwi = 1
3001+
for _, wallet in self.wallets.items():
3002+
if wallet.type() == WalletType.POOLING_WALLET:
3003+
max_pwi += 1
3004+
3005+
if max_pwi + 1 >= (MAX_POOL_WALLETS - 1):
3006+
raise ValueError(f"Too many pool wallets ({max_pwi}), cannot create any more on this key.")
3007+
3008+
return master_sk_to_singleton_owner_sk(self.get_master_private_key(), uint32(max_pwi)).get_g1()

0 commit comments

Comments
 (0)