-
Notifications
You must be signed in to change notification settings - Fork 19
/
how_to_delegate.py
164 lines (129 loc) · 4.41 KB
/
how_to_delegate.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
import argparse
import asyncio
import os
import pathlib
import typing
import pycspr
from pycspr import NodeRpcClient as NodeClient
from pycspr import NodeRpcConnectionInfo as NodeConnectionInfo
from pycspr.types.crypto import KeyAlgorithm
from pycspr.types.crypto import PrivateKey
from pycspr.types.crypto import PublicKey
from pycspr.types.node import Deploy
# Path to CCTL assets.
_PATH_TO_CCTL_ASSETS = pathlib.Path(os.getenv("CCTL")) / "assets"
_PATH_TO_CCTL_NODE = _PATH_TO_CCTL_ASSETS / "nodes" / "node-1"
_PATH_TO_CCTL_USER = _PATH_TO_CCTL_ASSETS / "users" / "user-1"
# CLI argument parser.
_ARGS = argparse.ArgumentParser("How to delegate CSPR to a validator.")
# CLI argument: path to delegator secret key - defaults to CCTL user 1.
_ARGS.add_argument(
"--delegator-secret-key-path",
default=_PATH_TO_CCTL_USER / "secret_key.pem",
dest="path_to_delegator_secret_key",
help="Path to delegator's secret_key.pem file.",
type=str,
)
# CLI argument: type of delegator secret key - defaults to ED25519.
_ARGS.add_argument(
"--delegator-secret-key-type",
default=KeyAlgorithm.ED25519.name,
dest="type_of_delegator_secret_key",
help="Type of delegator's secret key.",
type=str,
)
# CLI argument: path to validator's account key - defaults to CCTL node 1.
_ARGS.add_argument(
"--validator-account-key-path",
default=_PATH_TO_CCTL_NODE / "keys" / "public_key_hex",
dest="path_to_validator_account_key",
help="Path to validator's public_key_hex file.",
type=str,
)
# CLI argument: path to session code wasm binary.
_ARGS.add_argument(
"--path-to-wasm",
default=_PATH_TO_CCTL_ASSETS / "bin" / "delegate.wasm",
dest="path_to_wasm",
help="Path to delegate.wasm file.",
type=str,
)
# CLI argument: name of target chain - defaults to CCTL chain.
_ARGS.add_argument(
"--chain",
default="cspr-dev-cctl",
dest="chain_name",
help="Name of target chain.",
type=str,
)
# CLI argument: host address of target node - defaults to CCTL node 1.
_ARGS.add_argument(
"--node-host",
default="localhost",
dest="node_host",
help="Host address of target node.",
type=str,
)
# CLI argument: Node API JSON-RPC port - defaults to 11101 @ CCTL node 1.
_ARGS.add_argument(
"--node-port-rpc",
default=11101,
dest="node_port_rpc",
help="Node API JSON-RPC port. Typically 7777 on most nodes.",
type=int,
)
async def _main(args: argparse.Namespace):
"""Main entry point.
:param args: Parsed command line arguments.
"""
print("-" * 74)
print("PYCSPR :: How To Delegate")
print("")
print("Illustrates usage of pycspr.create_validator_delegation function.")
print("-" * 74)
# Set node client.
client: NodeClient = _get_client(args)
# Set counter-parties.
delegator, validator = _get_counter_parties(args)
# Set deploy.
deploy: Deploy = _get_deploy(args, delegator, validator)
# Approve deploy.
deploy.approve(delegator)
# Dispatch deploy to a node.
await client.send_deploy(deploy)
print(f"Deploy dispatched to node [{args.node_host}]: {deploy.hash.hex()}")
def _get_client(args: argparse.Namespace) -> NodeClient:
"""Returns a pycspr client instance.
"""
return NodeClient(NodeConnectionInfo(args.node_host, args.node_port_rpc))
def _get_counter_parties(args: argparse.Namespace) -> typing.Tuple[PrivateKey, PublicKey]:
"""Returns the 2 counter-parties participating in the delegation.
"""
delegator = pycspr.parse_private_key(
args.path_to_delegator_secret_key,
args.type_of_delegator_secret_key,
)
validator = pycspr.parse_public_key(
args.path_to_validator_account_key
)
return delegator, validator
def _get_deploy(args: argparse.Namespace, delegator: PrivateKey, validator: PublicKey) -> Deploy:
"""Returns delegation deploy to be dispatched to a node.
"""
# Set standard deploy parameters.
deploy_params = pycspr.create_deploy_parameters(
account=delegator,
chain_name=args.chain_name
)
# Set deploy.
deploy = pycspr.create_validator_delegation(
params=deploy_params,
amount=int(5e11),
public_key_of_delegator=delegator,
public_key_of_validator=validator,
path_to_wasm=args.path_to_wasm
)
return deploy
# Entry point.
if __name__ == "__main__":
asyncio.run(_main(_ARGS.parse_args()))