2022-08-01 10:15:02 +00:00
|
|
|
import base64
|
2022-07-22 07:04:26 +00:00
|
|
|
import json
|
2022-08-01 10:15:02 +00:00
|
|
|
|
|
|
|
import base58
|
2022-07-22 07:04:26 +00:00
|
|
|
from neo3 import wallet
|
|
|
|
|
2022-04-25 09:53:20 +00:00
|
|
|
|
2022-07-12 09:59:19 +00:00
|
|
|
def dict_to_attrs(attrs: dict) -> str:
|
2022-06-09 13:08:11 +00:00
|
|
|
"""
|
2022-07-12 09:59:19 +00:00
|
|
|
This function takes a dictionary of object's attributes and converts them
|
|
|
|
into string. The string is passed to `--attributes` key of neofs-cli.
|
2022-04-25 09:53:20 +00:00
|
|
|
|
|
|
|
Args:
|
2022-06-09 13:08:11 +00:00
|
|
|
attrs (dict): object attributes in {"a": "b", "c": "d"} format.
|
2022-04-25 09:53:20 +00:00
|
|
|
|
|
|
|
Returns:
|
|
|
|
(str): string in "a=b,c=d" format.
|
2022-06-09 13:08:11 +00:00
|
|
|
"""
|
2022-07-12 09:59:19 +00:00
|
|
|
return ",".join(f"{key}={value}" for key, value in attrs.items())
|
2022-07-22 07:04:26 +00:00
|
|
|
|
|
|
|
|
2022-08-01 10:15:02 +00:00
|
|
|
def __fix_wallet_schema(wallet: dict) -> None:
|
|
|
|
# Temporary function to fix wallets that do not conform to the schema
|
|
|
|
# TODO: get rid of it once issue is solved
|
|
|
|
if "name" not in wallet:
|
|
|
|
wallet["name"] = None
|
|
|
|
for account in wallet["accounts"]:
|
|
|
|
if "extra" not in account:
|
|
|
|
account["extra"] = None
|
|
|
|
|
|
|
|
|
|
|
|
def get_wallet_public_key(wallet_path: str, wallet_password: str, format: str = "hex") -> str:
|
|
|
|
# Get public key from wallet file
|
|
|
|
with open(wallet_path, "r") as file:
|
|
|
|
wallet_content = json.load(file)
|
|
|
|
__fix_wallet_schema(wallet_content)
|
|
|
|
|
2022-07-22 07:04:26 +00:00
|
|
|
wallet_from_json = wallet.Wallet.from_json(wallet_content, password=wallet_password)
|
2022-08-01 10:15:02 +00:00
|
|
|
public_key_hex = str(wallet_from_json.accounts[0].public_key)
|
|
|
|
|
|
|
|
# Convert public key to specified format
|
|
|
|
if format == "hex":
|
|
|
|
return public_key_hex
|
|
|
|
if format == "base58":
|
|
|
|
public_key_base58 = base58.b58encode(bytes.fromhex(public_key_hex))
|
|
|
|
return public_key_base58.decode("utf-8")
|
|
|
|
if format == "base64":
|
|
|
|
public_key_base64 = base64.b64encode(bytes.fromhex(public_key_hex))
|
|
|
|
return public_key_base64.decode("utf-8")
|
|
|
|
raise ValueError(f"Invalid public key format: {format}")
|