-
Notifications
You must be signed in to change notification settings - Fork 0
/
chainrpc.py
executable file
·239 lines (175 loc) · 8.09 KB
/
chainrpc.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
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
#!/usr/bin/env python3
import getpass
import fire
from jsonrpcclient import request
from decouple import config
CLIENT_RPC_URL = config('CLIENT_RPC_URL', 'http://127.0.0.1:26651')
CHAIN_RPC_URL = config('CHAIN_RPC_URL', 'http://127.0.0.1:26657')
DEFAULT_WALLET = config('DEFAULT_WALLET', 'Default')
def get_passphrase():
phrase = config('PASSPHRASE', None)
if phrase is None:
phrase = getpass.getpass('Input passphrase:')
return phrase
def call(method, *args):
rsp = request(CLIENT_RPC_URL, method, *args)
return rsp.data.result
def call_chain(method, *args):
rsp = request(CHAIN_RPC_URL, method, *args)
return rsp.data.result
def fix_address(addr):
'fire convert staking addr to int automatically, fix it.'
if isinstance(addr, int):
return hex(addr)
else:
return addr
class Address:
def list(self, name=DEFAULT_WALLET, type='staking'):
'''list addresses
:param name: Name of the wallet. [default: Default]
:params type: [staking|transfer]'''
return call('wallet_listStakingAddresses' if type == 'staking' else 'wallet_listTransferAddresses', [name, get_passphrase()])
def create(self, name=DEFAULT_WALLET, type='staking'):
'''Create address
:param name: Name of the wallet
:param type: Type of address. [staking|transfer]'''
return call(
'wallet_createStakingAddress'
if type == 'staking'
else 'wallet_createTransferAddress',
[name, get_passphrase()])
class Wallet:
def balance(self, name=DEFAULT_WALLET):
'''Get balance of wallet
:param name: Name of the wallet. [default: Default]'''
return call('wallet_balance', [name, get_passphrase()])
def list(self):
return call('wallet_list')
def create(self, name=DEFAULT_WALLET, type='Basic'):
'''create wallet
:param name: Name of the wallet. [defualt: Default]
:param type: Type of the wallet. [Basic|HD] [default: Basic]
'''
return call('wallet_create', [name, get_passphrase()], type)
def restore(self, mnemonics, name=DEFAULT_WALLET):
'''restore wallet
:param name: Name of the wallet. [defualt: Default]
:param mnemonics: mnemonics words
'''
return call('wallet_restore', [name, get_passphrase()], mnemonics)
def view_key(self, name=DEFAULT_WALLET):
return call(
'wallet_getViewKey'
[name, get_passphrase()]
)
def list_pubkey(self, name=DEFAULT_WALLET):
return call('wallet_listPublicKeys', [name, get_passphrase()])
def transactions(self, name=DEFAULT_WALLET):
return call('wallet_transactions', [name, get_passphrase()])
def send(self, to_address, amount, name=DEFAULT_WALLET, view_keys=None):
return call(
'wallet_sendToAddress',
[name, get_passphrase()],
to_address, str(amount), view_keys or [])
def sync(self, name=DEFAULT_WALLET):
return call('sync', [name, get_passphrase()])
def sync_all(self, name=DEFAULT_WALLET):
return call('sync_all', [name, get_passphrase()])
def sync_unlock(self, name=DEFAULT_WALLET):
return call('sync_unlockWallet', [name, get_passphrase()])
def sync_stop(self, name=DEFAULT_WALLET):
return call('sync_stop', [name, get_passphrase()])
class Staking:
def deposit_stake(self, to_address, inputs, name=DEFAULT_WALLET):
return call('staking_depositStake', [name, get_passphrase()], fix_address(to_address), inputs)
def state(self, address, name=DEFAULT_WALLET):
return call('staking_state', [name, get_passphrase()], fix_address(address))
def unbond_stake(self, address, amount, name=DEFAULT_WALLET):
return call('staking_unbondStake', [name, get_passphrase()], fix_address(address), amount)
def withdraw_all_unbonded_stake(self, from_address, to_address, name=DEFAULT_WALLET):
return call(
'staking_withdrawAllUnbondedStake',
[name, get_passphrase()],
fix_address(from_address), to_address, []
)
def unjail(self, address, name=DEFAULT_WALLET):
return call('staking_unjail', [name, get_passphrase()], fix_address(address))
class MultiSig:
def create_address(self, public_keys, self_public_key, required_signatures, name=DEFAULT_WALLET):
return call('multiSig_createAddress',
[name, get_passphrase()],
public_keys,
self_public_key,
required_signatures)
def new_session(self, message, signer_public_keys, self_public_key, name=DEFAULT_WALLET):
return call('multiSig_newSession',
[name, get_passphrase()],
message,
signer_public_keys,
self_public_key)
def nonce_commitment(self, session_id, passphrase):
return call('multiSig_nonceCommitment', session_id, passphrase)
def add_nonce_commitment(self, session_id, passphrase, nonce_commitment, public_key):
return call('multiSig_addNonceCommitment', session_id, passphrase, nonce_commitment, public_key)
def nonce(self, session_id, passphrase):
return call('multiSig_nonce', session_id, passphrase)
def add_nonce(self, session_id, passphrase, nonce, public_key):
return call('multiSig_addNonce', session_id, passphrase, nonce, public_key)
def partial_signature(self, session_id, passphrase):
return call('multiSig_partialSign', session_id, passphrase)
def add_partial_signature(self, session_id, passphrase, partial_signature, public_key):
return call('multiSig_addPartialSignature', session_id, passphrase, partial_signature, public_key)
def signature(self, session_id, passphrase):
return call('multiSig_signature', session_id, passphrase)
def broadcast_with_signature(self, session_id, unsigned_transaction, name=DEFAULT_WALLET):
return call('multiSig_broadcastWithSignature',
[name, get_passphrase()],
session_id,
unsigned_transaction)
class Blockchain:
def status(self):
return call_chain('status')
def info(self):
return call_chain('info')
def genesis(self):
return call_chain('genesis')
def unconfirmed_txs(self):
return call_chain('unconfirmed_txs')
def latest_height(self):
return self.status()['sync_info']['latest_block_height']
def validators(self, height='latest'):
height = height if height != 'latest' else self.latest_height()
return call_chain('validators', str(height))
def block(self, height='latest'):
height = height if height != 'latest' else self.latest_height()
return call_chain('block', str(height))
def block_results(self, height='latest'):
height = height if height != 'latest' else self.latest_height()
return call_chain('block_results', str(height))
def chain(self, min_height, max_height='latest'):
max_height = max_height if max_height != 'latest' else self.latest_height()
return call_chain('blockchain', str(min_height), str(max_height))
def commit(self, height='latest'):
height = height if height != 'latest' else self.latest_height()
return call_chain('commit', str(height))
def query(self, path, data, proof=False):
return call_chain('abci_query', path, data, proof)
def broadcast_tx_commit(self, tx):
return call_chain('broadcast_tx_commit', tx)
def broadcast_tx_sync(self, tx):
return call_chain('broadcast_tx_sync', tx)
def broadcast_tx_async(self, tx):
return call_chain('broadcast_tx_async', tx)
def tx(self, txid):
return call_chain('tx', txid)
class RPC:
def __init__(self):
self.wallet = Wallet()
self.staking = Staking()
self.address = Address()
self.multisig = MultiSig()
self.chain = Blockchain()
def raw_tx(self, inputs, outputs, view_keys):
return call('transaction_createRaw', inputs, outputs, view_keys)
if __name__ == '__main__':
fire.Fire(RPC())