Skip to content

Commit

Permalink
Adapt walletg to PysuiConfiguration. Docs update
Browse files Browse the repository at this point in the history
  • Loading branch information
FrankC01 committed Jul 14, 2024
1 parent 33c71e3 commit ed81ccc
Show file tree
Hide file tree
Showing 7 changed files with 73 additions and 35 deletions.
8 changes: 8 additions & 0 deletions doc/source/graphql_pyconfig.rst
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
GraphQL PysuiConfiguration
""""""""""""""""""""""""""""""""

PysuiConfiguration is the replacement for the legacy SuiConfig and this new class should be
used with GraphQL clients.

Anatomy of PysuiConfiguration
=============================
1 change: 1 addition & 0 deletions doc/source/index.rst
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,7 @@ Welcome to pysui's documentation!
builders
graphql
graphql_prog_txn
graphql_pyconfig
aliases
multi_sig
prog_txn
Expand Down
16 changes: 16 additions & 0 deletions pysui/sui/sui_pgql/config/confgroup.py
Original file line number Diff line number Diff line change
Expand Up @@ -101,6 +101,22 @@ def address_for_alias(self, alias: str) -> str:
return self.address_list[aliindx]
raise ValueError(f"Alias {alias} not found in group")

def alias_for_address(self, address: str) -> ProfileAlias:
"""Get alias associated with address."""
_res = self._address_exists(address=address)
if _res:
adindex = self.address_list.index(_res)
return self.alias_list[adindex]
raise ValueError(f"Address {address} not found in group")

def alias_name_for_address(self, address: str) -> str:
"""Get alias associated with address."""
_res = self._address_exists(address=address)
if _res:
adindex = self.address_list.index(_res)
return self.alias_list[adindex].alias
raise ValueError(f"Address {address} not found in group")

@property
def active_profile(self) -> Profile:
"""Gets the active profile."""
Expand Down
35 changes: 25 additions & 10 deletions pysui/sui/sui_pgql/config/conflegacy.py
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@

"""Sui Legacy Configuration (JSON RPC) setup."""

import base64
import json
import dataclasses
from typing import Optional
Expand Down Expand Up @@ -59,13 +60,18 @@ class ConfigSui(dataclasses_json.DataClassJsonMixin):
envs: list[ConfigEnv]


def address_str_from_keystring(indata: str) -> str:
"""From a 44 char keypair string create an address string."""
# Check address is legit keypair

def address_and_alias_from_keystring(
indata: str, calias: list[ProfileAlias]
) -> tuple[str, ProfileAlias]:
"""From a 44 char keypair string create an address string and return matched alias."""
_kp = keypair_from_keystring(indata).to_bytes()
digest = _kp[0:33] if _kp[0] == 0 else _kp[0:34]
return format(f"0x{hashlib.blake2b(digest, digest_size=32).hexdigest()}")
pubkey = base64.b64encode(digest).decode()
alias = next(filter(lambda pa: pa.public_key_base64 == pubkey, calias), False)
if alias:
addy = format(f"0x{hashlib.blake2b(digest, digest_size=32).hexdigest()}")
return addy, alias
raise ValueError(f"{pubkey} not found in alias list")


def load_client_yaml(client_file: Path, json_rpc_group: str) -> ProfileGroup:
Expand Down Expand Up @@ -95,21 +101,30 @@ def load_client_yaml(client_file: Path, json_rpc_group: str) -> ProfileGroup:
_prf.faucet_status_url = _TESTNET_FAUCET_STATUS_URL
_prf_list.append(_prf)

_alias_cache = [
ProfileAlias.from_dict(x)
for x in json.loads(_client_alias.read_text(encoding="utf8"))
]

_prf_keys: list[ProfileKey] = []
_prf_addy: list[str] = []
_prf_alias: list[ProfileAlias] = []
for prvkey in json.loads(keyfile.read_text(encoding="utf8")):
_prf_keys.append(ProfileKey.from_dict({"private_key_base64": prvkey}))
_prf_addy.append(address_str_from_keystring(prvkey))
addy, ally = address_and_alias_from_keystring(prvkey, _alias_cache)
_prf_addy.append(addy)
_prf_alias.append(ally)

# Build sui_config group
prg_group = ProfileGroup(
json_rpc_group,
cfg_sui.active_env,
cfg_sui.active_address,
[
ProfileAlias.from_dict(x)
for x in json.loads(_client_alias.read_text(encoding="utf8"))
],
_prf_alias,
# [
# ProfileAlias.from_dict(x)
# for x in json.loads(_client_alias.read_text(encoding="utf8"))
# ],
_prf_keys,
_prf_addy,
_prf_list,
Expand Down
2 changes: 1 addition & 1 deletion pysui/sui/sui_pgql/config/confmodel.py
Original file line number Diff line number Diff line change
Expand Up @@ -94,7 +94,7 @@ def initialize_json_rpc(
_updated = True
if sui_config.exists():
# If group doesn't exist, create it
if (_res := self._group_exists(group_name=json_rpc_group_name)) == -1:
if not self._group_exists(group_name=json_rpc_group_name):
sui_group = load_client_yaml(sui_config, json_rpc_group_name)
self.groups.append(sui_group)
_updated = True
Expand Down
38 changes: 18 additions & 20 deletions samples/cmdsg.py
Original file line number Diff line number Diff line change
Expand Up @@ -10,12 +10,8 @@
from pathlib import Path
import pprint
import sys
from pysui import (
__version__,
SyncClient,
SuiRpcResult,
SuiAddress,
)
from pysui import __version__, SuiRpcResult, SuiAddress, PysuiConfiguration

from pysui.sui.sui_pgql.pgql_clients import SuiGQLClient
from pysui.sui.sui_pgql.pgql_sync_txn import SuiTransaction
import pysui.sui.sui_pgql.pgql_query as qn
Expand Down Expand Up @@ -76,7 +72,7 @@ def sdk_version(_client: SuiGQLClient, _args: argparse.Namespace) -> None:
def sui_active_address(client: SuiGQLClient, _args: argparse.Namespace) -> None:
"""Print active address."""
print()
print(f"Active address = {client.config.active_address.address}")
print(f"Active address = {client.config.active_address}")


def sui_addresses(client: SuiGQLClient, args: argparse.Namespace) -> None:
Expand All @@ -87,23 +83,26 @@ def sui_addresses(client: SuiGQLClient, args: argparse.Namespace) -> None:
aliast = "Alias"
space = " "
active = "Def"

if args.details:
header_str = format(f"{space:^3s}{addyt:^72s}{pubkt:^34}{aliast:^64}")
header_str = format(f"{space:^3s}{addyt:^73s}{pubkt:55}{aliast:34}")
else:
header_str = format(f"{space:^3s}{addyt:^72s}")
print(header_str)
for _ in range(0, len(header_str)):
print("-", end="")
print()
for addy in client.config.addresses:
pcfg: PysuiConfiguration = client.config
pgrp = pcfg.model.active_group
for addy in pgrp.address_list:
addyl = addy
if addy == client.config.active_address:
if addy == pcfg.active_address:
addyl = f"{active:^3s}{addyl:^72s}"
else:
addyl = f"{space:^3s}{addyl:^72s}"
if args.details:
alias = client.config.al4addr(addy)
addyl = addyl + f" {client.config.pk4al(alias)} {alias}"
palias = pgrp.alias_for_address(addy)
addyl = addyl + f" {palias.public_key_base64:54s} {palias.alias}"
print(addyl)


Expand Down Expand Up @@ -135,22 +134,21 @@ def _detail_gas_objects(gas_objects: list[ptypes.SuiCoinObjectGQL]) -> None:

for_owner: str = None
if args.owner:
for_owner = args.owner
for_owner = args.owner.address
elif args.alias:
for_owner = client.config.addr4al(args.alias)
pcfg: PysuiConfiguration = client.config
for_owner = pcfg.model.active_group.address_for_alias(args.alias)
else:
for_owner = client.config.active_address

all_gas = []
gas_result = client.execute_query_node(
with_node=qn.GetCoins(owner=for_owner.address)
)
gas_result = client.execute_query_node(with_node=qn.GetCoins(owner=for_owner))
while gas_result.is_ok():
all_gas.extend(gas_result.result_data.data)
if gas_result.result_data.next_cursor.hasNextPage:
gas_result = client.execute_query_node(
with_node=qn.GetCoins(
owner=for_owner.address,
owner=for_owner,
next_page=gas_result.result_data.next_cursor,
)
)
Expand Down Expand Up @@ -551,7 +549,7 @@ def txn_txn(client: SuiGQLClient, args: argparse.Namespace) -> None:
handle_result(result)


def alias_list(client: SyncClient, args: argparse.Namespace) -> None:
def alias_list(client: SuiGQLClient, args: argparse.Namespace) -> None:
"""List address aliases."""
print()
for alias in client.config.aliases:
Expand All @@ -560,7 +558,7 @@ def alias_list(client: SyncClient, args: argparse.Namespace) -> None:
print(f"PublicKey: {client.config.pk4al(alias)}\n")


def alias_rename(client: SyncClient, args: argparse.Namespace) -> None:
def alias_rename(client: SuiGQLClient, args: argparse.Namespace) -> None:
"""List address aliases."""
print()
try:
Expand Down
8 changes: 4 additions & 4 deletions samples/walletg.py
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@
sys.path.insert(0, str(PARENT))
sys.path.insert(0, str(os.path.join(PARENT, "pysui")))

from pysui import SuiConfig
from pysui import PysuiConfiguration
from pysui.sui.sui_pgql.pgql_clients import SuiGQLClient
from pysui.sui.sui_constants import PYSUI_CLIENT_CONFIG_ENV

Expand All @@ -42,10 +42,10 @@ def main():
if cfg_local:
raise ValueError("Local not supported for GraphQL commands")
else:
cfg = SuiConfig.default_config()
print(f"Using configuration from {os.environ[PYSUI_CLIENT_CONFIG_ENV]}")
cfg = PysuiConfiguration(group_name=PysuiConfiguration.SUI_GQL_RPC_GROUP)
# print(f"Using configuration from {os.environ[PYSUI_CLIENT_CONFIG_ENV]}")

cmd_call(SuiGQLClient(config=cfg), parsed)
cmd_call(SuiGQLClient(pysui_config=cfg), parsed)
else:
print(f"Unable to resolve function for {parsed.subcommand}")

Expand Down

0 comments on commit ed81ccc

Please sign in to comment.