-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathtransactionbuilder.py
484 lines (411 loc) · 16.3 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
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
from .account import Account
from .asset import Asset
from bitsharesbase.objects import Operation
from bitsharesbase.account import PrivateKey, PublicKey
from bitsharesbase.signedtransactions import Signed_Transaction
from bitsharesbase import transactions, operations
from .exceptions import (
InsufficientAuthorityError,
MissingKeyError,
InvalidWifError,
WalletLocked
)
from .instance import BlockchainInstance
import logging
log = logging.getLogger(__name__)
class ProposalBuilder:
""" Proposal Builder allows us to construct an independent Proposal
that may later be added to an instance ot TransactionBuilder
:param str proposer: Account name of the proposing user
:param int proposal_expiration: Number seconds until the proposal is
supposed to expire
:param int proposal_review: Number of seconds for review of the
proposal
:param .transactionbuilder.TransactionBuilder: Specify
your own instance of transaction builder (optional)
:param instance blockchain_instance: Blockchain instance
"""
def __init__(
self,
proposer,
proposal_expiration=None,
proposal_review=None,
parent=None,
*args,
**kwargs
):
BlockchainInstance.__init__(self, *args, **kwargs)
self.set_expiration(proposal_expiration or 2 * 24 * 60 * 60)
self.set_review(proposal_review)
self.set_parent(parent)
self.set_proposer(proposer)
self.ops = list()
def is_empty(self):
return not (len(self.ops) > 0)
def set_proposer(self, p):
self.proposer = p
def set_expiration(self, p):
self.proposal_expiration = p
def set_review(self, p):
self.proposal_review = p
def set_parent(self, p):
self.parent = p
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)
parent = self.parent
if parent:
parent._set_require_reconstruction()
def list_operations(self):
return [Operation(o) for o in self.ops]
def broadcast(self):
assert self.parent, "No parent transaction provided!"
self.parent._set_require_reconstruction()
return self.parent.broadcast()
def get_parent(self):
""" This allows to referr to the actual parent of the Proposal
"""
return self.parent
def __repr__(self):
return "<Proposal ops=%s>" % str(self.ops)
def json(self):
""" Return the json formated version of this proposal
"""
raw = self.get_raw()
if not raw:
return dict()
return raw.json()
def __dict__(self):
return self.json()
def get_raw(self):
""" Returns an instance of base "Operations" for further processing
"""
if not self.ops:
return
ops = [operations.Op_wrapper(op=o) for o in list(self.ops)]
proposer = Account(
self.proposer,
blockchain_instance=self.blockchain
)
data = {
"fee": {"amount": 0, "asset_id": "1.3.0"},
"fee_paying_account": proposer["id"],
"expiration_time": transactions.formatTimeFromNow(
self.proposal_expiration),
"proposed_ops": [o.json() for o in ops],
"extensions": []
}
if self.proposal_review:
data.update({
"review_period_seconds": self.proposal_review
})
ops = operations.Proposal_create(**data)
return Operation(ops)
class TransactionBuilder(dict):
""" This class simplifies the creation of transactions by adding
operations and signers.
"""
def __init__(
self,
tx={},
proposer=None,
**kwargs
):
BlockchainInstance.__init__(self, **kwargs)
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_fee_asset(kwargs.get("fee_asset", None))
self.set_expiration(kwargs.get("expiration", self.blockchain.expiration)) or 30
def set_expiration(self, p):
self.expiration = p
def is_empty(self):
return not (len(self.ops) > 0)
def list_operations(self):
return [Operation(o) for o in self.ops]
def _is_signed(self):
return "signatures" in self and self["signatures"]
def _is_constructed(self):
return "expiration" in self and 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
"""
assert permission in ["active", "owner"], "Invalid permission"
if self.blockchain.wallet.locked():
raise WalletLocked()
# Let's define a helper function for recursion
def fetchkeys(account, perm, level=0, required_treshold=1):
# Do not travel recursion more than 2 levels
if level > 2:
return []
r = []
# Let's go through all *keys* of the account
for authority in account[perm]["key_auths"]:
try:
# Try obtain the private key from wallet
wif = self.blockchain.wallet.getPrivateKeyForPublicKey(
authority[0])
r.append([wif, authority[1]])
except Exception:
pass
# Test if we reached threshold already
if sum([x[1] for x in r]) >= required_treshold:
break
# Let's see if we still need to go through accounts
if sum([x[1] for x in r]) < required_treshold:
# go one level deeper
for authority in account[perm]["account_auths"]:
# Let's see if we can find keys for an account in
# account_auths
# This is recursive with a limit at level 2 (see above)
auth_account = Account(
authority[0], blockchain_instance=self.blockchain)
r.extend(fetchkeys(auth_account, perm, level + 1, required_treshold))
# Test if we reached threshold already and break
if sum([x[1] for x in r]) >= required_treshold:
break
return r
# Now let's actually deal with the accounts
if account not in self.signing_accounts:
# is the account an instance of public key?
if isinstance(account, PublicKey):
self.wifs.add(
self.blockchain.wallet.getPrivateKeyForPublicKey(
str(account)
)
)
# ... or should we rather obtain the keys from an account name
else:
account = Account(account, blockchain_instance=self.blockchain)
required_treshold = account[permission]["weight_threshold"]
keys = fetchkeys(account, permission, required_treshold=required_treshold)
# If we couldn't find an active key, let's try overwrite it
# with an owner key
if not keys and permission != "owner":
keys.extend(fetchkeys(account, "owner", required_treshold=required_treshold))
for x in keys:
self.wifs.add(x[0])
self.signing_accounts.append(account)
def appendWif(self, wif):
""" Add a wif that should be used for signing of the transaction.
"""
if wif:
try:
PrivateKey(wif)
self.wifs.add(wif)
except:
raise InvalidWifError
def set_fee_asset(self, fee_asset):
""" Set asset to fee
"""
from .amount import Amount
if isinstance(fee_asset, Amount):
self.fee_asset_id = fee_asset["id"]
elif isinstance(fee_asset, Asset):
self.fee_asset_id = fee_asset["id"]
elif fee_asset:
self.fee_asset_id = fee_asset
else:
self.fee_asset_id = "1.3.0"
def constructTx(self):
""" Construct the actual transaction and store it in the class's dict
store
"""
ops = list()
for op in self.ops:
if isinstance(op, ProposalBuilder):
# This operation is a proposal an needs to be deal with
# differently
proposals = op.get_raw()
if proposals:
ops.append(proposals)
else:
# otherwise, we simply wrap ops into Operations
ops.extend([Operation(op)])
# We now wrap everything into an actual transaction
ops = transactions.addRequiredFees(self.blockchain.rpc, ops,
asset_id=self.fee_asset_id)
expiration = transactions.formatTimeFromNow(
self.expiration or self.blockchain.expiration
)
ref_block_num, ref_block_prefix = transactions.getBlockParams(
self.blockchain.rpc)
self.tx = Signed_Transaction(
ref_block_num=ref_block_num,
ref_block_prefix=ref_block_prefix,
expiration=expiration,
operations=ops
)
super(TransactionBuilder, self).update(self.tx.json())
self._unset_require_reconstruction()
def sign(self):
""" Sign a provided transaction with the provided key(s)
:param dict tx: The transaction to be signed and returned
:param string wifs: One or many wif keys to use for signing
a transaction. If not present, the keys will be loaded
from the wallet as defined in "missing_signatures" key
of the transactions.
"""
self.constructTx()
if "operations" not in self or not self["operations"]:
return
# Legacy compatibility!
# If we are doing a proposal, obtain the account from the proposer_id
if self.blockchain.proposer:
proposer = Account(
self.blockchain.proposer,
blockchain_instance=self.blockchain)
self.wifs = set()
self.signing_accounts = list()
self.appendSigner(proposer["id"], "active")
# We need to set the default prefix, otherwise pubkeys are
# presented wrongly!
if self.blockchain.rpc:
operations.default_prefix = (
self.blockchain.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.blockchain.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 not self.blockchain.rpc.verify_authority(self.json()):
raise InsufficientAuthorityError
except Exception as e:
raise e
def broadcast(self):
""" Broadcast a transaction to the blockchain network
:param tx tx: Signed transaction to broadcast
"""
# 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 self.blockchain.nobroadcast:
log.warning("Not broadcasting anything!")
self.clear()
return ret
# Broadcast
try:
if self.blockchain.blocking:
ret = self.blockchain.rpc.broadcast_transaction_synchronous(
ret, api="network_broadcast")
ret.update(**ret.get("trx"))
else:
self.blockchain.rpc.broadcast_transaction(
ret, api="network_broadcast")
except Exception as e:
raise e
finally:
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):
""" 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)
FIXME: Does not work with owner keys!
"""
self.constructTx()
self["blockchain"] = self.blockchain.rpc.chain_params
if isinstance(account, PublicKey):
self["missing_signatures"] = [
str(account)
]
else:
accountObj = Account(account)
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])
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])
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:
wif = self.blockchain.wallet.getPrivateKeyForPublicKey(pub)
if wif:
self.appendWif(wif)