-
-
Notifications
You must be signed in to change notification settings - Fork 73
/
transactionbuilder.py
462 lines (398 loc) · 16.6 KB
/
transactionbuilder.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
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
# This Python file uses the following encoding: utf-8
from __future__ import absolute_import
from __future__ import division
from __future__ import print_function
from __future__ import unicode_literals
from builtins import str
from future.utils import python_2_unicode_compatible
from beemgraphenebase.py23 import bytes_types, integer_types, string_types, text_type
from .account import Account
from .utils import formatTimeFromNow
from .steemconnect import SteemConnect
from beembase.objects import Operation
from beemgraphenebase.account import PrivateKey, PublicKey
from beembase.signedtransactions import Signed_Transaction
from beembase import transactions, operations
from .exceptions import (
InsufficientAuthorityError,
MissingKeyError,
InvalidWifError,
WalletLocked
)
from beem.instance import shared_steem_instance
import logging
log = logging.getLogger(__name__)
@python_2_unicode_compatible
class TransactionBuilder(dict):
""" This class simplifies the creation of transactions by adding
operations and signers.
To build your own transactions and sign them
:param dict tx: transaction (Optional). If not set, the new transaction is created.
:param int expiration: Delay in seconds until transactions are supposed
to expire *(optional)* (default is 30)
:param Steem steem_instance: If not set, shared_steem_instance() is used
.. testcode::
from beem.transactionbuilder import TransactionBuilder
from beembase.operations import Transfer
from beem import Steem
wif = "5KQwrPbwdL6PhXujxW37FSSQZ1JiwsST4cqQzDeyXtP79zkvFD3"
stm = Steem(nobroadcast=True, keys={'active': wif})
tx = TransactionBuilder(steem_instance=stm)
transfer = {"from": "test", "to": "test1", "amount": "1 STEEM", "memo": ""}
tx.appendOps(Transfer(transfer))
tx.appendSigner("test", "active") # or tx.appendWif(wif)
signed_tx = tx.sign()
broadcast_tx = tx.broadcast()
"""
def __init__(
self,
tx={},
steem_instance=None,
**kwargs
):
self.steem = steem_instance or shared_steem_instance()
self.clear()
if tx and isinstance(tx, dict):
super(TransactionBuilder, self).__init__(tx)
# Load operations
self.ops = tx["operations"]
self._require_reconstruction = False
else:
self._require_reconstruction = True
self.set_expiration(kwargs.get("expiration", 30))
def set_expiration(self, p):
"""Set expiration date"""
self.expiration = p
def is_empty(self):
"""Check if ops is empty"""
return not (len(self.ops) > 0)
def list_operations(self):
"""List all ops"""
return [Operation(o) for o in self.ops]
def _is_signed(self):
"""Check if signatures exists"""
return "signatures" in self and bool(self["signatures"])
def _is_constructed(self):
"""Check if tx is already constructed"""
return "expiration" in self and bool(self["expiration"])
def _is_require_reconstruction(self):
return self._require_reconstruction
def _set_require_reconstruction(self):
self._require_reconstruction = True
def _unset_require_reconstruction(self):
self._require_reconstruction = False
def __repr__(self):
return str(self)
def __str__(self):
return str(self.json())
def __getitem__(self, key):
if key not in self:
self.constructTx()
return dict(self).__getitem__(key)
def get_parent(self):
""" TransactionBuilders don't have parents, they are their own parent
"""
return self
def json(self):
""" Show the transaction as plain json
"""
if not self._is_constructed() or self._is_require_reconstruction():
self.constructTx()
return dict(self)
def appendOps(self, ops, append_to=None):
""" Append op(s) to the transaction builder
:param list ops: One or a list of operations
"""
if isinstance(ops, list):
self.ops.extend(ops)
else:
self.ops.append(ops)
self._set_require_reconstruction()
def appendSigner(self, account, permission):
""" Try to obtain the wif key from the wallet by telling which account
and permission is supposed to sign the transaction
It is possible to add more than one signer.
"""
if permission not in ["active", "owner", "posting"]:
raise AssertionError("Invalid permission")
account = Account(account, steem_instance=self.steem)
if permission not in account:
account = Account(account, steem_instance=self.steem, lazy=False, full=True)
account.clear_cache()
account.refresh()
if permission not in account:
account = Account(account, steem_instance=self.steem)
if permission not in account:
raise AssertionError("Could not access permission")
required_treshold = account[permission]["weight_threshold"]
if self.steem.use_sc2:
if not self.steem.steemconnect.hot_sign and permission == "posting":
if self.steem.wallet.locked():
raise WalletLocked()
self.steem.steemconnect.set_username(account["name"], permission)
return
else:
if self.steem.wallet.locked():
raise WalletLocked()
def fetchkeys(account, perm, level=0):
if level > 2:
return []
r = []
for authority in account[perm]["key_auths"]:
try:
wif = self.steem.wallet.getPrivateKeyForPublicKey(
authority[0])
if wif:
r.append([wif, authority[1]])
except ValueError:
pass
except MissingKeyError:
pass
if sum([x[1] for x in r]) < required_treshold:
# go one level deeper
for authority in account[perm]["account_auths"]:
auth_account = Account(
authority[0], steem_instance=self.steem)
r.extend(fetchkeys(auth_account, perm, level + 1))
return r
if account["name"] not in self.signing_accounts:
# is the account an instance of public key?
if isinstance(account, PublicKey):
self.wifs.add(
self.steem.wallet.getPrivateKeyForPublicKey(
str(account)
)
)
else:
if permission not in account:
raise AssertionError("Could not access permission")
required_treshold = account[permission]["weight_threshold"]
keys = fetchkeys(account, permission)
# If keys are empty, try again with active key
if not keys and permission == "posting":
_keys = fetchkeys(account, "active")
keys.extend(_keys)
# If keys are empty, try again with owner key
if not keys and permission != "owner":
_keys = fetchkeys(account, "owner")
keys.extend(_keys)
for x in keys:
self.wifs.add(x[0])
self.signing_accounts.append(account["name"])
def appendWif(self, wif):
""" Add a wif that should be used for signing of the transaction.
:param string wif: One wif key to use for signing
a transaction.
"""
if wif:
try:
PrivateKey(wif, prefix=self.steem.prefix)
self.wifs.add(wif)
except:
raise InvalidWifError
def clearWifs(self):
"""Clear all stored wifs"""
self.wifs = set()
def constructTx(self):
""" Construct the actual transaction and store it in the class's dict
store
"""
ops = list()
for op in self.ops:
# otherwise, we simply wrap ops into Operations
ops.extend([Operation(op)])
# We no wrap everything into an actual transaction
# ops = transactions.addRequiredFees(self.steem.rpc, ops)
expiration = formatTimeFromNow(
self.expiration or self.steem.expiration
)
ref_block_num, ref_block_prefix = transactions.getBlockParams(
self.steem.rpc)
self.tx = Signed_Transaction(
ref_block_prefix=ref_block_prefix,
expiration=expiration,
operations=ops,
ref_block_num=ref_block_num
)
super(TransactionBuilder, self).update(self.tx.json())
self._unset_require_reconstruction()
def sign(self, reconstruct_tx=True):
""" Sign a provided transaction with the provided key(s)
One or many wif keys to use for signing a transaction.
The wif keys can be provided by "appendWif" or the
signer can be defined "appendSigner". The wif keys
from all signer that are defined by "appendSigner
will be loaded from the wallet.
:param bool reconstruct_tx: when set to False and tx
is already contructed, it will not reconstructed
and already added signatures remain
"""
if not self._is_constructed() or (self._is_constructed() and reconstruct_tx):
self.constructTx()
if "operations" not in self or not self["operations"]:
return
if self.steem.use_sc2:
return
# We need to set the default prefix, otherwise pubkeys are
# presented wrongly!
if self.steem.rpc is not None:
operations.default_prefix = (
self.steem.rpc.chain_params["prefix"])
elif "blockchain" in self:
operations.default_prefix = self["blockchain"]["prefix"]
try:
signedtx = Signed_Transaction(**self.json())
except:
raise ValueError("Invalid TransactionBuilder Format")
if not any(self.wifs):
raise MissingKeyError
signedtx.sign(self.wifs, chain=self.steem.rpc.chain_params)
self["signatures"].extend(signedtx.json().get("signatures"))
return signedtx
def verify_authority(self):
""" Verify the authority of the signed transaction
"""
try:
if self.steem.rpc.get_use_appbase():
args = {'trx': self.json()}
else:
args = self.json()
ret = self.steem.rpc.verify_authority(args, api="database")
if not ret:
raise InsufficientAuthorityError
elif isinstance(ret, dict) and "valid" in ret and not ret["valid"]:
raise InsufficientAuthorityError
except Exception as e:
raise e
def get_potential_signatures(self):
""" Returns public key from signature
"""
if self.steem.rpc.get_use_appbase():
args = {'trx': self.json()}
else:
args = self.json()
ret = self.steem.rpc.get_potential_signatures(args, api="database")
if 'keys' in ret:
ret = ret["keys"]
return ret
def get_transaction_hex(self):
""" Returns a hex value of the transaction
"""
if self.steem.rpc.get_use_appbase():
args = {'trx': self.json()}
else:
args = self.json()
ret = self.steem.rpc.get_transaction_hex(args, api="database")
if 'hex' in ret:
ret = ret["hex"]
return ret
def get_required_signatures(self, available_keys=list()):
""" Returns public key from signature
"""
if self.steem.rpc.get_use_appbase():
args = {'trx': self.json(), 'available_keys': available_keys}
ret = self.steem.rpc.get_required_signatures(args, api="database")
else:
ret = self.steem.rpc.get_required_signatures(self.json(), available_keys, api="database")
return ret
def broadcast(self, max_block_age=-1):
""" Broadcast a transaction to the steem network
Returns the signed transaction and clears itself
after broadast
Clears itself when broadcast was not sucessfully.
:param int max_block_age: paramerter only used
for appbase ready nodes
"""
# Cannot broadcast an empty transaction
if not self._is_signed():
self.sign()
if "operations" not in self or not self["operations"]:
return
ret = self.json()
if not self.steem.offline and self.steem.rpc.get_use_appbase():
args = {'trx': self.json(), 'max_block_age': max_block_age}
else:
args = self.json()
if self.steem.nobroadcast:
log.warning("Not broadcasting anything!")
self.clear()
return ret
# Broadcast
try:
if self.steem.use_sc2:
ret = self.steem.steemconnect.boadcast_or_hot_sign(self["operations"])
elif self.steem.blocking:
ret = self.steem.rpc.broadcast_transaction_synchronous(
args, api="network_broadcast")
ret.update(**ret.get("trx"))
else:
self.steem.rpc.broadcast_transaction(
args, api="network_broadcast")
except Exception as e:
log.error("Could Not broadcasting anything!")
self.clear()
raise e
self.clear()
return ret
def clear(self):
""" Clear the transaction builder and start from scratch
"""
self.ops = []
self.wifs = set()
self.signing_accounts = []
# This makes sure that _is_constructed will return False afterwards
self["expiration"] = None
super(TransactionBuilder, self).__init__({})
def addSigningInformation(self, account, permission, reconstruct_tx=False):
""" This is a private method that adds side information to a
unsigned/partial transaction in order to simplify later
signing (e.g. for multisig or coldstorage)
Not needed when "appendWif" was already or is going to be used
FIXME: Does not work with owner keys!
:param bool reconstruct_tx: when set to False and tx
is already contructed, it will not reconstructed
and already added signatures remain
"""
if not self._is_constructed() or (self._is_constructed() and reconstruct_tx):
self.constructTx()
self["blockchain"] = self.steem.rpc.chain_params
if isinstance(account, PublicKey):
self["missing_signatures"] = [
str(account)
]
else:
accountObj = Account(account, steem_instance=self.steem)
authority = accountObj[permission]
# We add a required_authorities to be able to identify
# how to sign later. This is an array, because we
# may later want to allow multiple operations per tx
self.update({"required_authorities": {
accountObj["name"]: authority
}})
for account_auth in authority["account_auths"]:
account_auth_account = Account(account_auth[0], steem_instance=self.steem)
self["required_authorities"].update({
account_auth[0]: account_auth_account.get(permission)
})
# Try to resolve required signatures for offline signing
self["missing_signatures"] = [
x[0] for x in authority["key_auths"]
]
# Add one recursion of keys from account_auths:
for account_auth in authority["account_auths"]:
account_auth_account = Account(account_auth[0], steem_instance=self.steem)
self["missing_signatures"].extend(
[x[0] for x in account_auth_account[permission]["key_auths"]]
)
def appendMissingSignatures(self):
""" Store which accounts/keys are supposed to sign the transaction
This method is used for an offline-signer!
"""
missing_signatures = self.get("missing_signatures", [])
for pub in missing_signatures:
try:
wif = self.steem.wallet.getPrivateKeyForPublicKey(pub)
if wif:
self.appendWif(wif)
except MissingKeyError:
wif = None