hive-nectar 0.2.9__py3-none-any.whl

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
Files changed (87) hide show
  1. hive_nectar-0.2.9.dist-info/METADATA +194 -0
  2. hive_nectar-0.2.9.dist-info/RECORD +87 -0
  3. hive_nectar-0.2.9.dist-info/WHEEL +4 -0
  4. hive_nectar-0.2.9.dist-info/entry_points.txt +2 -0
  5. hive_nectar-0.2.9.dist-info/licenses/LICENSE.txt +23 -0
  6. nectar/__init__.py +37 -0
  7. nectar/account.py +5076 -0
  8. nectar/amount.py +553 -0
  9. nectar/asciichart.py +303 -0
  10. nectar/asset.py +122 -0
  11. nectar/block.py +574 -0
  12. nectar/blockchain.py +1242 -0
  13. nectar/blockchaininstance.py +2590 -0
  14. nectar/blockchainobject.py +263 -0
  15. nectar/cli.py +5937 -0
  16. nectar/comment.py +1552 -0
  17. nectar/community.py +854 -0
  18. nectar/constants.py +95 -0
  19. nectar/discussions.py +1437 -0
  20. nectar/exceptions.py +152 -0
  21. nectar/haf.py +381 -0
  22. nectar/hive.py +630 -0
  23. nectar/imageuploader.py +114 -0
  24. nectar/instance.py +113 -0
  25. nectar/market.py +876 -0
  26. nectar/memo.py +542 -0
  27. nectar/message.py +379 -0
  28. nectar/nodelist.py +309 -0
  29. nectar/price.py +603 -0
  30. nectar/profile.py +74 -0
  31. nectar/py.typed +0 -0
  32. nectar/rc.py +333 -0
  33. nectar/snapshot.py +1024 -0
  34. nectar/storage.py +62 -0
  35. nectar/transactionbuilder.py +659 -0
  36. nectar/utils.py +630 -0
  37. nectar/version.py +3 -0
  38. nectar/vote.py +722 -0
  39. nectar/wallet.py +472 -0
  40. nectar/witness.py +728 -0
  41. nectarapi/__init__.py +12 -0
  42. nectarapi/exceptions.py +126 -0
  43. nectarapi/graphenerpc.py +596 -0
  44. nectarapi/node.py +194 -0
  45. nectarapi/noderpc.py +79 -0
  46. nectarapi/openapi.py +107 -0
  47. nectarapi/py.typed +0 -0
  48. nectarapi/rpcutils.py +98 -0
  49. nectarapi/version.py +3 -0
  50. nectarbase/__init__.py +15 -0
  51. nectarbase/ledgertransactions.py +106 -0
  52. nectarbase/memo.py +242 -0
  53. nectarbase/objects.py +521 -0
  54. nectarbase/objecttypes.py +21 -0
  55. nectarbase/operationids.py +102 -0
  56. nectarbase/operations.py +1357 -0
  57. nectarbase/py.typed +0 -0
  58. nectarbase/signedtransactions.py +89 -0
  59. nectarbase/transactions.py +11 -0
  60. nectarbase/version.py +3 -0
  61. nectargraphenebase/__init__.py +27 -0
  62. nectargraphenebase/account.py +1121 -0
  63. nectargraphenebase/aes.py +49 -0
  64. nectargraphenebase/base58.py +197 -0
  65. nectargraphenebase/bip32.py +575 -0
  66. nectargraphenebase/bip38.py +110 -0
  67. nectargraphenebase/chains.py +15 -0
  68. nectargraphenebase/dictionary.py +2 -0
  69. nectargraphenebase/ecdsasig.py +309 -0
  70. nectargraphenebase/objects.py +130 -0
  71. nectargraphenebase/objecttypes.py +8 -0
  72. nectargraphenebase/operationids.py +5 -0
  73. nectargraphenebase/operations.py +25 -0
  74. nectargraphenebase/prefix.py +13 -0
  75. nectargraphenebase/py.typed +0 -0
  76. nectargraphenebase/signedtransactions.py +221 -0
  77. nectargraphenebase/types.py +557 -0
  78. nectargraphenebase/unsignedtransactions.py +288 -0
  79. nectargraphenebase/version.py +3 -0
  80. nectarstorage/__init__.py +57 -0
  81. nectarstorage/base.py +317 -0
  82. nectarstorage/exceptions.py +15 -0
  83. nectarstorage/interfaces.py +244 -0
  84. nectarstorage/masterpassword.py +237 -0
  85. nectarstorage/py.typed +0 -0
  86. nectarstorage/ram.py +27 -0
  87. nectarstorage/sqlite.py +343 -0
@@ -0,0 +1,659 @@
1
+ import logging
2
+ import struct
3
+ from binascii import unhexlify
4
+
5
+ # import time (not currently used)
6
+ from nectar.instance import shared_blockchain_instance
7
+ from nectarbase import operations
8
+ from nectarbase.ledgertransactions import Ledger_Transaction
9
+ from nectarbase.objects import Operation
10
+ from nectarbase.signedtransactions import Signed_Transaction
11
+ from nectargraphenebase.account import PrivateKey, PublicKey
12
+ from nectarstorage.exceptions import WalletLocked
13
+
14
+ from .account import Account
15
+ from .exceptions import (
16
+ InsufficientAuthorityError,
17
+ InvalidWifError,
18
+ MissingKeyError,
19
+ OfflineHasNoRPCException,
20
+ )
21
+ from .utils import formatTimeFromNow
22
+
23
+ log = logging.getLogger(__name__)
24
+
25
+
26
+ class TransactionBuilder(dict):
27
+ """This class simplifies the creation of transactions by adding
28
+ operations and signers.
29
+ To build your own transactions and sign them
30
+
31
+ :param dict tx: transaction (Optional). If not set, the new transaction is created.
32
+ :param int expiration: Delay in seconds until transactions are supposed
33
+ to expire *(optional)* (default is 300)
34
+ :param Hive blockchain_instance: If not set, shared_blockchain_instance() is used
35
+
36
+ .. testcode::
37
+
38
+ from nectar.transactionbuilder import TransactionBuilder
39
+ from nectarbase.operations import Transfer
40
+ from nectar import Hive
41
+ wif = "5KQwrPbwdL6PhXujxW37FSSQZ1JiwsST4cqQzDeyXtP79zkvFD3"
42
+ hive = Hive(nobroadcast=True, keys={'active': wif})
43
+ tx = TransactionBuilder(blockchain_instance=hive)
44
+ transfer = {"from": "test", "to": "test1", "amount": "1 HIVE", "memo": ""}
45
+ tx.appendOps(Transfer(transfer))
46
+ tx.appendSigner("test", "active") # or tx.appendWif(wif)
47
+ signed_tx = tx.sign()
48
+ broadcast_tx = tx.broadcast()
49
+
50
+ """
51
+
52
+ def __init__(self, tx=None, blockchain_instance=None, **kwargs):
53
+ """
54
+ Initialize a TransactionBuilder, optionally from an existing transaction dict.
55
+
56
+ If `tx` is a dict, its contents are loaded into the builder (operations are taken from tx["operations"])
57
+ and reconstruction is marked as not required. Otherwise the builder starts empty and is marked to
58
+ require reconstruction before use. The constructor clears any prior state, captures ledger/path and
59
+ condenser API configuration from the provided blockchain instance (or the shared instance), and sets
60
+ the transaction expiration from kwargs or the blockchain default.
61
+
62
+ Parameters:
63
+ tx (dict, optional): An existing transaction dictionary to load. When provided and valid,
64
+ operations are initialized from tx["operations"] and reconstruction is disabled.
65
+ expiration (int, optional, via kwargs): Transaction expiration (seconds or blockchain-specific
66
+ expiration value). If omitted, the blockchain instance's default expiration is used.
67
+
68
+ Notes:
69
+ - The `blockchain_instance` parameter is a shared service/client and is intentionally not
70
+ documented here beyond its effect on builder configuration.
71
+ """
72
+ self.blockchain = blockchain_instance or shared_blockchain_instance()
73
+ self.clear()
74
+ if tx and isinstance(tx, dict):
75
+ super().__init__(tx)
76
+ # Load operations
77
+ self.ops = tx["operations"]
78
+ self._require_reconstruction = False
79
+ else:
80
+ self._require_reconstruction = True
81
+ self._use_ledger = self.blockchain.use_ledger
82
+ self.path = self.blockchain.path
83
+ self.set_expiration(kwargs.get("expiration", self.blockchain.expiration))
84
+
85
+ def set_expiration(self, p):
86
+ """Set expiration date"""
87
+ self.expiration = p
88
+
89
+ def is_empty(self):
90
+ """Check if ops is empty"""
91
+ return not (len(self.ops) > 0)
92
+
93
+ def list_operations(self):
94
+ """List all ops"""
95
+ return [Operation(o, appbase=True, prefix=self.blockchain.prefix) for o in self.ops]
96
+
97
+ def _is_signed(self):
98
+ """Check if signatures exists"""
99
+ return "signatures" in self and bool(self["signatures"])
100
+
101
+ def _is_constructed(self):
102
+ """Check if tx is already constructed"""
103
+ return "expiration" in self and bool(self["expiration"])
104
+
105
+ def _is_require_reconstruction(self):
106
+ return self._require_reconstruction
107
+
108
+ def _set_require_reconstruction(self):
109
+ self._require_reconstruction = True
110
+
111
+ def _unset_require_reconstruction(self):
112
+ self._require_reconstruction = False
113
+
114
+ def _get_auth_field(self, permission):
115
+ return permission
116
+
117
+ def __repr__(self):
118
+ return "<Transaction num_ops={}, ops={}>".format(
119
+ len(self.ops), [op.__class__.__name__ for op in self.ops]
120
+ )
121
+
122
+ def __str__(self):
123
+ return str(self.json())
124
+
125
+ def __getitem__(self, key):
126
+ if key not in self:
127
+ self.constructTx()
128
+ return dict(self).__getitem__(key)
129
+
130
+ def get_parent(self):
131
+ """TransactionBuilders don't have parents, they are their own parent"""
132
+ return self
133
+
134
+ def json(self, with_prefix=False):
135
+ """Show the transaction as plain json"""
136
+ if not self._is_constructed() or self._is_require_reconstruction():
137
+ self.constructTx()
138
+ json_dict = dict(self)
139
+ if with_prefix:
140
+ json_dict["prefix"] = self.blockchain.prefix
141
+ return json_dict
142
+
143
+ def appendOps(self, ops, append_to=None):
144
+ """Append op(s) to the transaction builder
145
+
146
+ :param list ops: One or a list of operations
147
+ """
148
+ if isinstance(ops, list):
149
+ self.ops.extend(ops)
150
+ else:
151
+ self.ops.append(ops)
152
+ self._set_require_reconstruction()
153
+
154
+ def _fetchkeys(self, account, perm, level=0, required_treshold=1):
155
+ # Do not travel recursion more than 2 levels
156
+ if level > 2:
157
+ return []
158
+
159
+ r = []
160
+ wif = None
161
+ # Let's go through all *keys* of the account
162
+ for authority in account[perm]["key_auths"]:
163
+ try:
164
+ # Try obtain the private key from wallet
165
+ wif = self.blockchain.wallet.getPrivateKeyForPublicKey(authority[0])
166
+ except ValueError:
167
+ pass
168
+ except MissingKeyError:
169
+ pass
170
+
171
+ if wif:
172
+ r.append([wif, authority[1]])
173
+ # If we found a key for account, we add it
174
+ # to signing_accounts to be sure we do not resign
175
+ # another operation with the same account/wif
176
+ self.signing_accounts.append(account)
177
+
178
+ # Test if we reached threshold already
179
+ if sum([x[1] for x in r]) >= required_treshold:
180
+ break
181
+
182
+ # Let's see if we still need to go through accounts
183
+ if sum([x[1] for x in r]) < required_treshold:
184
+ # go one level deeper
185
+ for authority in account[perm]["account_auths"]:
186
+ # Let's see if we can find keys for an account in
187
+ # account_auths
188
+ # This is recursive with a limit at level 2 (see above)
189
+ auth_account = Account(authority[0], blockchain_instance=self.blockchain)
190
+ required_treshold = auth_account[perm]["weight_threshold"]
191
+ keys = self._fetchkeys(auth_account, perm, level + 1, required_treshold)
192
+
193
+ for key in keys:
194
+ r.append(key)
195
+
196
+ # Test if we reached threshold already and break
197
+ if sum([x[1] for x in r]) >= required_treshold:
198
+ break
199
+
200
+ return r
201
+
202
+ def appendSigner(self, account, permission):
203
+ """
204
+ Register an account as a signer for this transaction by locating or assigning the needed signing keys.
205
+
206
+ Attempts to resolve signing credentials for `account` at the requested `permission` and attaches them to the builder state. Behavior varies by signing mode:
207
+ - Ledger mode: verifies that the ledger-derived public key for the currently selected path is authorized for the account/permission (raises AssertionError if not).
208
+ - Wallet mode: fetches private keys from the local wallet and stores corresponding WIFs; if the account argument is a PublicKey, the matching WIF is retrieved and added.
209
+
210
+ Parameters:
211
+ account (str | Account | PublicKey): account name, Account instance, or public key identifying the signer.
212
+ permission (str): permission level to use ("active", "owner", or "posting").
213
+
214
+ Raises:
215
+ WalletLocked: if the local wallet is locked when wallet keys are required.
216
+ AssertionError: for invalid permission values, if the requested permission cannot be accessed, or if a ledger public key is not found in the account authorities.
217
+ """
218
+ if not self.blockchain.is_connected():
219
+ return
220
+ if permission not in ["active", "owner", "posting"]:
221
+ raise AssertionError("Invalid permission")
222
+ account = Account(account, blockchain_instance=self.blockchain)
223
+ auth_field = self._get_auth_field(permission)
224
+ if auth_field not in account:
225
+ account = Account(account, blockchain_instance=self.blockchain, lazy=False, full=True)
226
+ account.clear_cache()
227
+ account.refresh()
228
+ if auth_field not in account:
229
+ account = Account(account, blockchain_instance=self.blockchain)
230
+ if auth_field not in account:
231
+ raise AssertionError("Could not access permission")
232
+
233
+ if self._use_ledger:
234
+ if not self._is_constructed() or self._is_require_reconstruction():
235
+ self.constructTx()
236
+
237
+ key_found = False
238
+ if self.path is not None:
239
+ current_pubkey = self.ledgertx.get_pubkey(self.path)
240
+ for authority in account[auth_field]["key_auths"]:
241
+ if str(current_pubkey) == authority[0]:
242
+ key_found = True
243
+ if permission == "posting" and not key_found:
244
+ for authority in account["active"]["key_auths"]:
245
+ if str(current_pubkey) == authority[0]:
246
+ key_found = True
247
+ if not key_found:
248
+ for authority in account["owner"]["key_auths"]:
249
+ if str(current_pubkey) == authority[0]:
250
+ key_found = True
251
+ if not key_found:
252
+ raise AssertionError(
253
+ "Could not find pubkey from {} in path: {}!".format(account["name"], self.path)
254
+ )
255
+ return
256
+
257
+ if self.blockchain.wallet.locked():
258
+ raise WalletLocked()
259
+
260
+ if account["name"] not in self.signing_accounts:
261
+ # is the account an instance of public key?
262
+ if isinstance(account, PublicKey):
263
+ self.wifs.add(self.blockchain.wallet.getPrivateKeyForPublicKey(str(account)))
264
+ else:
265
+ if auth_field not in account:
266
+ raise AssertionError("Could not access permission")
267
+ required_treshold = account[auth_field]["weight_threshold"]
268
+ keys = self._fetchkeys(account, permission, required_treshold=required_treshold)
269
+ # If keys are empty, try again with active key
270
+ if not keys and permission == "posting":
271
+ _keys = self._fetchkeys(account, "active", required_treshold=required_treshold)
272
+ keys.extend(_keys)
273
+ # If keys are empty, try again with owner key
274
+ if not keys and permission != "owner":
275
+ _keys = self._fetchkeys(account, "owner", required_treshold=required_treshold)
276
+ keys.extend(_keys)
277
+ for x in keys:
278
+ self.appendWif(x[0])
279
+
280
+ self.signing_accounts.append(account["name"])
281
+
282
+ def appendWif(self, wif):
283
+ """Add a wif that should be used for signing of the transaction.
284
+
285
+ :param string wif: One wif key to use for signing
286
+ a transaction.
287
+ """
288
+ if wif:
289
+ try:
290
+ PrivateKey(wif, prefix=self.blockchain.prefix)
291
+ self.wifs.add(wif)
292
+ except Exception:
293
+ raise InvalidWifError
294
+
295
+ def clearWifs(self):
296
+ """Clear all stored wifs"""
297
+ self.wifs = set()
298
+
299
+ def setPath(self, path):
300
+ self.path = path
301
+
302
+ def searchPath(self, account, perm):
303
+ if not self.blockchain.use_ledger:
304
+ return
305
+ if not self._is_constructed() or self._is_require_reconstruction():
306
+ self.constructTx()
307
+ key_found = False
308
+ path = None
309
+ current_account_index = 0
310
+ current_key_index = 0
311
+ while not key_found and current_account_index < 5:
312
+ path = self.ledgertx.build_path(perm, current_account_index, current_key_index)
313
+ current_pubkey = self.ledgertx.get_pubkey(path)
314
+ key_found = False
315
+ for authority in account[perm]["key_auths"]:
316
+ if str(current_pubkey) == authority[1]:
317
+ key_found = True
318
+ if not key_found and current_key_index < 5:
319
+ current_key_index += 1
320
+ elif not key_found and current_key_index >= 5:
321
+ current_key_index = 0
322
+ current_account_index += 1
323
+ if not key_found:
324
+ return None
325
+ else:
326
+ return path
327
+
328
+ def constructTx(self, ref_block_num=None, ref_block_prefix=None):
329
+ """Construct the actual transaction and store it in the class's dict
330
+ store
331
+
332
+ """
333
+ ops = list()
334
+ for op in self.ops:
335
+ # otherwise, we simply wrap ops into Operations
336
+ ops.extend([Operation(op, appbase=True, prefix=self.blockchain.prefix)])
337
+
338
+ # calculation expiration time from last block time not system time
339
+ # it fixes transaction expiration error when pushing transactions
340
+ # when blocks are moved forward with debug_produce_block*
341
+ # Prefer chain head time when connected to align with node clock and avoid expiration drift
342
+ exp_seconds = int(self.expiration or self.blockchain.expiration or 300)
343
+ # ensure at least 5 minutes to avoid expiration race with head block time drift
344
+ exp_seconds = max(exp_seconds, 300)
345
+ from datetime import datetime, timedelta, timezone
346
+
347
+ if self.blockchain.is_connected():
348
+ dgp = self.blockchain.get_dynamic_global_properties(use_stored_data=False)
349
+ if dgp is None:
350
+ # Fallback to system time if we can't get chain time
351
+ expiration = formatTimeFromNow(exp_seconds)
352
+ else:
353
+ head_time_str = dgp.get("time")
354
+
355
+ head_time = datetime.strptime(head_time_str, "%Y-%m-%dT%H:%M:%S").replace(
356
+ tzinfo=timezone.utc
357
+ )
358
+ now_utc = datetime.now(timezone.utc)
359
+ base_time = max(head_time, now_utc)
360
+ expiration_dt = base_time + timedelta(seconds=exp_seconds)
361
+ expiration = expiration_dt.strftime("%Y-%m-%dT%H:%M:%S")
362
+ else:
363
+ expiration = formatTimeFromNow(exp_seconds)
364
+
365
+ # We now wrap everything into an actual transaction
366
+ if ref_block_num is None or ref_block_prefix is None:
367
+ ref_block_num, ref_block_prefix = self.get_block_params(use_head_block=True)
368
+ if self._use_ledger:
369
+ self.ledgertx = Ledger_Transaction(
370
+ ref_block_prefix=ref_block_prefix,
371
+ expiration=expiration,
372
+ operations=ops,
373
+ ref_block_num=ref_block_num,
374
+ custom_chains=self.blockchain.custom_chains,
375
+ prefix=self.blockchain.prefix,
376
+ )
377
+
378
+ self.tx = Signed_Transaction(
379
+ ref_block_prefix=ref_block_prefix,
380
+ expiration=expiration,
381
+ operations=ops,
382
+ ref_block_num=ref_block_num,
383
+ custom_chains=self.blockchain.custom_chains,
384
+ prefix=self.blockchain.prefix,
385
+ )
386
+
387
+ super().update(self.tx.json())
388
+ self._unset_require_reconstruction()
389
+
390
+ def get_block_params(self, use_head_block=False):
391
+ """Auxiliary method to obtain ``ref_block_num`` and
392
+ ``ref_block_prefix``. Requires a connection to a
393
+ node!
394
+ """
395
+
396
+ dynBCParams = self.blockchain.get_dynamic_global_properties(use_stored_data=False)
397
+ # fix for corner case where last_irreversible_block_num == head_block_number
398
+ # then int(dynBCParams["last_irreversible_block_num"]) + 1 does not exists
399
+ # and BlockHeader throws error
400
+ if use_head_block or int(dynBCParams["last_irreversible_block_num"]) == int(
401
+ dynBCParams["head_block_number"]
402
+ ):
403
+ ref_block_num = dynBCParams["head_block_number"] & 0xFFFF
404
+ ref_block_prefix = struct.unpack_from("<I", unhexlify(dynBCParams["head_block_id"]), 4)[
405
+ 0
406
+ ]
407
+ else:
408
+ # need to get subsequent block because block head doesn't return 'id' - stupid
409
+ from .block import BlockHeader
410
+
411
+ block = BlockHeader(
412
+ int(dynBCParams["last_irreversible_block_num"]) + 1,
413
+ blockchain_instance=self.blockchain,
414
+ )
415
+ ref_block_num = dynBCParams["last_irreversible_block_num"] & 0xFFFF
416
+ ref_block_prefix = struct.unpack_from("<I", unhexlify(block["previous"]), 4)[0]
417
+ return ref_block_num, ref_block_prefix
418
+
419
+ def sign(self, reconstruct_tx=True):
420
+ """
421
+ Sign the built transaction using a Ledger device or local WIFs and attach signatures to the builder.
422
+
423
+ If the transaction is not constructed (or if reconstruct_tx is True) the transaction will be reconstructed before signing. The method attempts signing in this order:
424
+ 1. Ledger (if ledger mode is active) — signs with the Ledger device and appends signatures.
425
+ 2. Local WIFs from the builder — uses stored WIFs to sign the transaction and appends signatures.
426
+
427
+ Parameters:
428
+ reconstruct_tx (bool): If False and the transaction is already constructed, existing signatures are preserved and the transaction will not be rebuilt before signing. Defaults to True.
429
+
430
+ Returns:
431
+ The object returned by the signing step:
432
+ - Ledger_Transaction when signed via Ledger,
433
+ - Signed_Transaction when signed locally with WIFs.
434
+ Returns None if there are no operations to sign.
435
+
436
+ Raises:
437
+ MissingKeyError: If local signing is attempted but no WIFs are available.
438
+ """
439
+ if not self._is_constructed() or (self._is_constructed() and reconstruct_tx):
440
+ self.constructTx()
441
+ if "operations" not in self or not self["operations"]:
442
+ return
443
+ # We need to set the default prefix, otherwise pubkeys are
444
+ # presented wrongly!
445
+ if self.blockchain.rpc is not None:
446
+ operations.default_prefix = self.blockchain.chain_params["prefix"]
447
+ elif "blockchain" in self:
448
+ operations.default_prefix = self["blockchain"]["prefix"]
449
+
450
+ if self._use_ledger:
451
+ # try:
452
+ # ledgertx = Ledger_Transaction(**self.json(with_prefix=True))
453
+ # ledgertx.add_custom_chains(self.blockchain.custom_chains)
454
+ # except Exception:
455
+ # raise ValueError("Invalid TransactionBuilder Format")
456
+ # ledgertx.sign(self.path, chain=self.blockchain.chain_params)
457
+ self.ledgertx.sign(self.path, chain=self.blockchain.chain_params)
458
+ self["signatures"].extend(self.ledgertx.json().get("signatures"))
459
+ return self.ledgertx
460
+ else:
461
+ if not any(self.wifs):
462
+ raise MissingKeyError
463
+
464
+ self.tx.sign(list(self.wifs), chain=self.blockchain.chain_params)
465
+ # Defensive: ensure self["signatures"] is a list before extend
466
+ if isinstance(self["signatures"], str):
467
+ log.warning(
468
+ "self['signatures'] was a string, converting to list to avoid AttributeError."
469
+ )
470
+ self["signatures"] = [self["signatures"]]
471
+ sigs = self.tx.json().get("signatures")
472
+ if isinstance(sigs, str):
473
+ sigs = [sigs]
474
+ self["signatures"].extend(sigs)
475
+ return self.tx
476
+
477
+ def verify_authority(self):
478
+ """Verify the authority of the signed transaction"""
479
+ try:
480
+ self.blockchain.rpc.set_next_node_on_empty_reply(False)
481
+ args = {"trx": self.json()}
482
+ ret = self.blockchain.rpc.verify_authority(args)
483
+ if not ret:
484
+ raise InsufficientAuthorityError
485
+ elif isinstance(ret, dict) and "valid" in ret and not ret["valid"]:
486
+ raise InsufficientAuthorityError
487
+ except Exception as e:
488
+ raise e
489
+
490
+ def get_potential_signatures(self):
491
+ """Returns public key from signature"""
492
+ if not self.blockchain.is_connected():
493
+ raise OfflineHasNoRPCException("No RPC available in offline mode!")
494
+ self.blockchain.rpc.set_next_node_on_empty_reply(False)
495
+ args = {"trx": self.json()}
496
+ ret = self.blockchain.rpc.get_potential_signatures(args)
497
+ if "keys" in ret:
498
+ ret = ret["keys"]
499
+ return ret
500
+
501
+ def get_transaction_hex(self):
502
+ """Returns a hex value of the transaction"""
503
+ if not self.blockchain.is_connected():
504
+ raise OfflineHasNoRPCException("No RPC available in offline mode!")
505
+ self.blockchain.rpc.set_next_node_on_empty_reply(False)
506
+ args = {"trx": self.json()}
507
+ ret = self.blockchain.rpc.get_transaction_hex(args)
508
+ if "hex" in ret:
509
+ ret = ret["hex"]
510
+ return ret
511
+
512
+ def get_required_signatures(self, available_keys=list()):
513
+ """
514
+ Return the subset of public keys required to sign this transaction from a set of available keys.
515
+
516
+ This method requires an active RPC connection and delegates to the node's
517
+ get_required_signatures API to determine which of the provided available_keys
518
+ are necessary to satisfy the transaction's authority requirements.
519
+
520
+ Parameters:
521
+ available_keys (list): Iterable of public key strings to consider when
522
+ determining required signers.
523
+
524
+ Returns:
525
+ list: Public key strings that the node reports are required to sign the transaction.
526
+
527
+ Raises:
528
+ OfflineHasNoRPCException: If called while offline (no RPC available).
529
+ """
530
+ if not self.blockchain.is_connected():
531
+ raise OfflineHasNoRPCException("No RPC available in offline mode!")
532
+ self.blockchain.rpc.set_next_node_on_empty_reply(False)
533
+ args = {"trx": self.json(), "available_keys": available_keys}
534
+ ret = self.blockchain.rpc.get_required_signatures(args)
535
+ return ret
536
+
537
+ def broadcast(self, max_block_age=-1, trx_id=True):
538
+ """
539
+ Broadcast the built transaction to the Hive network and clear the builder state.
540
+
541
+ If the transaction is not yet signed this method will attempt to sign it first.
542
+ If no operations are present the call returns None. When broadcasting is disabled
543
+ (nobroadcast) the constructed transaction dict is returned and the builder is cleared.
544
+
545
+ Parameters:
546
+ max_block_age (int): Passed to appbase/network_broadcast calls to constrain
547
+ acceptable block age; ignored for condenser API paths. Default -1.
548
+ trx_id (bool): If True and a signing step produced a transaction id, attach
549
+ it to the returned result when the RPC response lacks a `trx_id`. Default True.
550
+
551
+ Returns:
552
+ dict or whatever the underlying broadcast method returns, or None if there
553
+ are no operations to broadcast.
554
+
555
+ Side effects:
556
+ - Clears internal transaction state on successful broadcast or on errors.
557
+ - May raise exceptions from the signing or RPC broadcast calls.
558
+ """
559
+ # Cannot broadcast an empty transaction
560
+ if not self._is_signed():
561
+ sign_ret = self.sign()
562
+ else:
563
+ sign_ret = None
564
+
565
+ if "operations" not in self or not self["operations"]:
566
+ return
567
+ ret = self.json()
568
+
569
+ args = {"trx": self.json(), "max_block_age": max_block_age}
570
+ broadcast_api = "network_broadcast_api"
571
+
572
+ if self.blockchain.nobroadcast:
573
+ log.info("Not broadcasting anything!")
574
+ self.clear()
575
+ return ret
576
+ # Broadcast
577
+ try:
578
+ self.blockchain.rpc.set_next_node_on_empty_reply(False)
579
+ if self.blockchain.blocking:
580
+ ret = self.blockchain.rpc.broadcast_transaction_synchronous(args, api=broadcast_api)
581
+ if isinstance(ret, dict) and "trx" in ret:
582
+ ret.update(**ret.get("trx"))
583
+ else:
584
+ self.blockchain.rpc.broadcast_transaction(args, api=broadcast_api)
585
+ except Exception as e:
586
+ # log.error("Could Not broadcasting anything!")
587
+ self.clear()
588
+ raise e
589
+ if sign_ret is not None and "trx_id" not in ret and trx_id:
590
+ ret["trx_id"] = sign_ret.id
591
+ self.clear()
592
+ return ret
593
+
594
+ def clear(self):
595
+ """Clear the transaction builder and start from scratch"""
596
+ self.ops = []
597
+ self.wifs = set()
598
+ self.signing_accounts = []
599
+ self.ref_block_num = None
600
+ self.ref_block_prefix = None
601
+ # This makes sure that _is_constructed will return False afterwards
602
+ self["expiration"] = None
603
+ super().__init__({})
604
+
605
+ def addSigningInformation(self, account, permission, reconstruct_tx=False):
606
+ """This is a private method that adds side information to a
607
+ unsigned/partial transaction in order to simplify later
608
+ signing (e.g. for multisig or coldstorage)
609
+
610
+ Not needed when "appendWif" was already or is going to be used
611
+
612
+ FIXME: Does not work with owner keys!
613
+
614
+ :param bool reconstruct_tx: when set to False and tx
615
+ is already contructed, it will not reconstructed
616
+ and already added signatures remain
617
+
618
+ """
619
+ if not self._is_constructed() or (self._is_constructed() and reconstruct_tx):
620
+ self.constructTx()
621
+ self["blockchain"] = self.blockchain.chain_params
622
+
623
+ if isinstance(account, PublicKey):
624
+ self["missing_signatures"] = [str(account)]
625
+ else:
626
+ accountObj = Account(account, blockchain_instance=self.blockchain)
627
+ authority = accountObj[permission]
628
+ # We add a required_authorities to be able to identify
629
+ # how to sign later. This is an array, because we
630
+ # may later want to allow multiple operations per tx
631
+ self.update({"required_authorities": {accountObj["name"]: authority}})
632
+ for account_auth in authority["account_auths"]:
633
+ account_auth_account = Account(account_auth[0], blockchain_instance=self.blockchain)
634
+ self["required_authorities"].update(
635
+ {account_auth[0]: account_auth_account.get(permission)}
636
+ )
637
+
638
+ # Try to resolve required signatures for offline signing
639
+ self["missing_signatures"] = [x[0] for x in authority["key_auths"]]
640
+ # Add one recursion of keys from account_auths:
641
+ for account_auth in authority["account_auths"]:
642
+ account_auth_account = Account(account_auth[0], blockchain_instance=self.blockchain)
643
+ self["missing_signatures"].extend(
644
+ [x[0] for x in account_auth_account[permission]["key_auths"]]
645
+ )
646
+
647
+ def appendMissingSignatures(self):
648
+ """Store which accounts/keys are supposed to sign the transaction
649
+
650
+ This method is used for an offline-signer!
651
+ """
652
+ missing_signatures = self.get("missing_signatures", [])
653
+ for pub in missing_signatures:
654
+ try:
655
+ wif = self.blockchain.wallet.getPrivateKeyForPublicKey(pub)
656
+ if wif:
657
+ self.appendWif(wif)
658
+ except MissingKeyError:
659
+ wif = None