-
Notifications
You must be signed in to change notification settings - Fork 8
/
Copy pathcreatepool.py
162 lines (128 loc) · 4.01 KB
/
createpool.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
import logging
import click
from stellar_base.keypair import Keypair
from stellar_base.utils import DecodeError
from stellar_base.transaction import Transaction
from stellar_base.transaction_envelope import TransactionEnvelope as Te
from stellar_base.operation import SetOptions
from stellar_base.operation import CreateAccount
from stellar_base.horizon import horizon_livenet, horizon_testnet
logger = logging.getLogger(__name__)
SIGNING_THRESHOLD = {
"low": 3,
"medium": 3,
"high": 4
}
STARTING_BALANCE = "5"
network = "PUBLIC"
def generate_pool_keypair(desired_tail=None):
kp = None
if desired_tail:
logger.debug("Looking for address ending in '%s'..." % (desired_tail,))
while True:
kp = Keypair.random()
if kp.address().decode()[-len(desired_tail):] == desired_tail:
break
else:
kp = Keypair.random()
return kp
def create_pool_account(horizon, network_id, account_secret_key, pool_keypair):
funding_account_kp = None
try:
funding_account_kp = Keypair.from_seed(account_secret_key)
except DecodeError:
logger.error("Invalid secret key, aborting")
return False
creation_operation = CreateAccount({
"destination": pool_keypair.address().decode(),
"starting_balance": STARTING_BALANCE
})
sequence = horizon.account(
funding_account_kp.address().decode()).get('sequence')
tx = Transaction(
source=funding_account_kp.address().decode(),
opts={
'sequence': sequence,
'operations': [creation_operation],
},
)
envelope = Te(tx=tx, opts={"network_id": network_id})
envelope.sign(funding_account_kp)
xdr = envelope.xdr()
response = horizon.submit(xdr)
if "_links" in response:
logger.debug("Creation of account transaction link: %s" % (
response["_links"]["transaction"]["href"],))
return True
else:
logger.error("Failed to create account. Dumping response:")
logger.error(response)
return False
def get_signers(file_path):
signers = []
with open(file_path) as f:
signers = f.read().splitlines()
return signers
def set_account_signers(horizon, pool_keypair, signers, threshold):
pool_address = pool_keypair.address().decode()
operations = [
SetOptions({
"signer_address": signer_address,
"signer_weight": 1,
"signer_type": "ed25519PublicKey",
"source_account": pool_address
})
for signer_address in signers
]
operations.append(
SetOptions({
"source_account": pool_address,
"home_domain": bytes("lumenaut.net", "utf-8"),
"master_weight": 0,
"low_threshold": threshold["low"],
"med_threshold": threshold["medium"],
"high_threshold": threshold["high"],
"inflation_dest": pool_address
})
)
sequence = horizon.account(pool_address).get('sequence')
tx = Transaction(
source=pool_address,
opts={
'sequence': sequence,
'fee': 100 * len(operations),
'operations': operations,
},
)
envelope = Te(tx=tx, opts={"network_id": network})
envelope.sign(pool_keypair)
xdr = envelope.xdr()
response = horizon.submit(xdr)
if "_links" in response:
logger.debug(
"Set options transaction href: ",
response["_links"]["transaction"]["href"])
logger.debug("Created successfully!")
return True
else:
logger.error("Failed to set account signers, dumping response:")
logger.error(response)
return False
@click.command()
@click.option('--desired-tail', type=str, default=None)
@click.option('--funding-account-secret-key', type=str, prompt=True)
@click.option('--network-id', type=click.Choice(['TESTNET', 'PUBLIC']))
@click.option(
'--signers-file', type=click.Path(exists=True), default='signers.txt')
def main(
desired_tail, funding_account_secret_key, network_id, signers_file):
horizon = (horizon_livenet() if network == 'PUBLIC' else horizon_testnet())
pool_kp = generate_pool_keypair(desired_tail)
logger.info("Pool keypair: %s | %s" % (
pool_kp.address().decode(), pool_kp.seed().decode()))
if create_pool_account(
horizon, network_id, funding_account_secret_key, pool_kp):
signers = get_signers(signers_file)
set_account_signers(horizon, pool_kp, signers, SIGNING_THRESHOLD)
if __name__ == "__main__":
main()