Skip to content

Instantly share code, notes, and snippets.

@FrankC01
Created November 12, 2022 11:43
Show Gist options
  • Save FrankC01/37c2c74d7e0261c497390bfa6b67e3c7 to your computer and use it in GitHub Desktop.
Save FrankC01/37c2c74d7e0261c497390bfa6b67e3c7 to your computer and use it in GitHub Desktop.
Generate a SUI client configuration with keypair
# Copyright 2022 Frank V. Castellucci
# Licensed under the Apache License, Version 2.0 (the "License");
# you may not use this file except in compliance with the License.
# You may obtain a copy of the License at
# http://www.apache.org/licenses/LICENSE-2.0
# Unless required by applicable law or agreed to in writing, software
# distributed under the License is distributed on an "AS IS" BASIS,
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
# See the License for the specific language governing permissions and
# limitations under the License.
# -*- coding: utf-8 -*-
"""Generate a SUI config with unique address.
To run you need to have installed `pysui` in a python virtual environment first.
"""
import os
import pathlib
import json
import yaml
from pysui.sui.sui_config import SuiConfig
from pysui.abstracts import SignatureScheme
from pysui.sui.sui_types import SuiAddress
# Active address is place holder
CFG_PATH = "~/.sui/sui_config"
CLIENT_CFG_FILE = "~/.sui/sui_config/sui.keystore"
CLIENT_YAML = """
---
keystore:
File: ~/.sui/sui_config/sui.keystore
envs:
- alias: devnet
rpc: "https://fullnode.devnet.sui.io:443"
ws: ~
active_env: devnet
active_address: "0xd7ec1d9400a71f59d9c8213c97717e0c57e68737"
"""
# This keypair is place holder
KEYSTORE_JSON = ["AM5J7L0vXtkCXMeFBNb/BNR/515b5DVqLLXH3Ut/VBgHM4uvpQCdsDpRcppHEhN3s8U2keGPh3CzaFx9KI/FnqI="]
def setup_placeholders():
"""Do all prelim file work."""
# Check for existence of configuration before destroying it
full_path = pathlib.Path(os.path.expanduser(CFG_PATH))
if full_path.exists():
# Flash warning and prompt
print(f"Warning, this operation will desrtoy existing files in {full_path}")
else:
# Create the folder path
print(f"Creating folder path {full_path}")
os.makedirs(full_path, exist_ok=True)
# Generate the above files
client_yaml = yaml.full_load(CLIENT_YAML)
client_yaml["keystore"]["File"] = os.path.expanduser(CLIENT_CFG_FILE)
# print(client_yaml)
keystore = json.dumps(KEYSTORE_JSON, indent=2)
# print(keystore)
cfg_file = os.path.join(full_path, "client.yaml")
with open(cfg_file, "w", encoding="utf-8") as file:
yaml.dump_all(
[
client_yaml,
],
file,
)
keystore_file = os.path.join(full_path, "sui.keystore")
with open(keystore_file, "w", encoding="utf-8") as file:
file.write(keystore)
def set_new_configs(active_address: str) -> None:
"""Change active to new address and drop placeholder keypair."""
# Ready new unique address
addy = SuiAddress(str(active_address))
print(f"New Address = {addy.address}")
# Read existing configurations
full_path = pathlib.Path(os.path.expanduser(CFG_PATH))
cfg_file = os.path.join(full_path, "client.yaml")
keystore_file = os.path.join(full_path, "sui.keystore")
with open(cfg_file, "r", encoding="utf-8") as file:
client_yaml = yaml.safe_load(file)
with open(keystore_file, "r", encoding="utf-8") as file:
keystore_json = json.load(file)
# Sub in our new address string to existing configuration
client_yaml["active_address"] = addy.address
# Drop the placeholder keystring
keystore_json.pop(0)
# Save new client.yaml and sui.keystore files
with open(cfg_file, "w", encoding="utf-8") as file:
yaml.dump_all(
[
client_yaml,
],
file,
)
with open(keystore_file, "w", encoding="utf-8") as file:
file.write(json.dumps(keystore_json, indent=2))
print("New SUI devnet configuration saved and ready to use!")
print(f"Fund the new address {addy.address} through discord dev-faucet ")
def main():
"""Entry point."""
# Prepare environment
setup_placeholders()
# Load pysui for new key generation
set_new_configs(SuiConfig.default().create_new_keypair_and_address(SignatureScheme.ED25519))
if __name__ == "__main__":
main()
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment