Exemplo n.º 1
0
 def _from_json_data_object(self, data):
     self._nonce = BinaryData.from_json(data.get_or('nonce', ''),
                                        strencoding='base64')
     self._mint_condition = ConditionTypes.from_json(
         data.get_or('mintcondition', jsobj.new_dict()))
     self._mint_fulfillment = FulfillmentTypes.from_json(
         data.get_or('mintfulfillment', jsobj.new_dict()))
     self._miner_fees = [
         Currency.from_json(fee)
         for fee in data.get_or('minerfees', []) or []
     ]
     self._data = BinaryData.from_json(data.get_or('arbitrarydata', None)
                                       or '',
                                       strencoding='base64')
Exemplo n.º 2
0
 def result_cb(results):
     d = jsobj.new_dict()
     a = {}
     for addr, height in results:
         if height not in a:
             a[height] = []
         a[height].append(addr)
         d[height] = d.get_or(height, 0) + 1
     c_height = -1
     c_height_votes = -1
     for height, votes in jsobj.get_items(d):
         if votes > c_height_votes or (votes == c_height_votes
                                       and height > c_height):
             c_height = height
             c_height_votes = votes
     if c_height == -1:
         jslog.error(
             "update_consensus of explorer (HTTP): no explorer addresses are available"
         )
         # assign all addresses and hope for the best
         all_addresses = []
         for _, addresses in jsobj.get_items(a):
             all_addresses = jsarr.concat(all_addresses, addresses)
         self._consensus_addresses = addresses
     else:
         # select the explorer addresses with the desired height
         self._consensus_addresses = a[c_height]
Exemplo n.º 3
0
 def _transaction_from_explorer_transaction(
         self,
         etxn,
         endpoint="/?",
         resp=None):  # keyword parameters for error handling purposes only
     if resp == None:
         resp = jsobj.new_dict()
     # parse the transactions
     transaction = transactions.from_json(obj=etxn['rawtransaction'],
                                          id=etxn['id'])
     # add the parent (coin) outputs
     coininputoutputs = etxn.get_or('coininputoutputs', None) or []
     if len(transaction.coin_inputs) != len(coininputoutputs):
         raise tferrors.ExplorerInvalidResponse(
             "amount of coin inputs and parent outputs are not matching: {} != {}"
             .format(len(transaction.coin_inputs),
                     len(coininputoutputs)), endpoint, resp)
     for (idx, co) in enumerate(coininputoutputs):
         co = CoinOutput.from_json(obj=co)
         co.id = transaction.coin_inputs[idx].parentid
         transaction.coin_inputs[idx].parent_output = co
     # add the coin output ids
     coinoutputids = etxn.get_or('coinoutputids', None) or []
     if len(transaction.coin_outputs) != len(coinoutputids):
         raise tferrors.ExplorerInvalidResponse(
             "amount of coin outputs and output identifiers are not matching: {} != {}"
             .format(len(transaction.coin_outputs),
                     len(coinoutputids)), endpoint, resp)
     for (idx, id) in enumerate(coinoutputids):
         transaction.coin_outputs[idx].id = Hash.from_json(obj=id)
     # add the parent (blockstake) outputs
     blockstakeinputoutputs = etxn.get_or('blockstakeinputoutputs',
                                          None) or []
     if len(transaction.blockstake_inputs) != len(blockstakeinputoutputs):
         raise tferrors.ExplorerInvalidResponse(
             "amount of blockstake inputs and parent outputs are not matching: {} != {}"
             .format(len(transaction.blockstake_inputs),
                     len(blockstakeinputoutputs)), endpoint, resp)
     for (idx, bso) in enumerate(blockstakeinputoutputs):
         bso = BlockstakeOutput.from_json(obj=bso)
         bso.id = transaction.blockstake_inputs[idx].parentid
         transaction.blockstake_inputs[idx].parent_output = bso
     # add the blockstake output ids
     blockstakeoutputids = etxn.get_or('blockstakeoutputids', None) or []
     if len(transaction.blockstake_outputs) != len(blockstakeoutputids):
         raise tferrors.ExplorerInvalidResponse(
             "amount of blokstake outputs and output identifiers are not matching: {} != {}"
             .format(len(transaction.blockstake_inputs),
                     len(blockstakeoutputids)), endpoint, resp)
     for (idx, id) in enumerate(blockstakeoutputids):
         transaction.blockstake_outputs[idx].id = Hash.from_json(obj=id)
     # set the unconfirmed state
     transaction.unconfirmed = etxn.get_or('unconfirmed', False)
     # set the blockid and height of the transaction only if confirmed
     if not transaction.unconfirmed:
         transaction.height = int(etxn.get_or('height', -1))
         transaction.blockid = etxn.get_or('parent', None)
     # return the transaction
     return transaction
 def from_json(cls, obj):
     ff = cls()
     ct = obj.get_or('type', 0)
     if ff.ctype != ct:
         raise ValueError(
             "condition is expected to be of type {}, not {}".format(
                 ff.ctype, ct))
     ff.from_json_data_object(obj.get_or('data', jsobj.new_dict()))
     return ff
Exemplo n.º 5
0
 def from_json(cls, obj):
     ff = cls()
     t = obj.get_or('type', 0)
     if ff.ftype != t:
         raise ValueError(
             "invalid fulfillment type {}, expected it to be of type {}".
             format(t, ff.ftype))
     ff.from_json_data_object(obj.get_or('data', jsobj.new_dict()))
     return ff
Exemplo n.º 6
0
 def from_json(cls, obj):
     """
     Create this transaction from a raw JSON Tx
     """
     txn = cls()
     tv = obj.get_or('version', -1)
     txn._from_json_txn_version_validator(tv)
     txn._from_json_data_object(obj.get_or('data', jsobj.new_dict()))
     return txn
Exemplo n.º 7
0
def _assign_block_properties_to_transacton(txn, block):
    raw_block = block.get_or('rawblock', jsobj.new_dict())
    # assign txn timestamp
    txn.timestamp = raw_block.get_or('timestamp', 0)
    # assign fee payout info
    miner_payout_ids = block.get_or('minerpayoutids', [])
    if len(miner_payout_ids) >= 2:
        txn.fee_payout_id = miner_payout_ids[1]
        txn.fee_payout_address = raw_block['minerpayouts'][1]["unlockhash"]
    # assign transaction order (index within block)
    for idx, transaction in enumerate(block.get_or('transactions', [])):
        if transaction.get_or('id', 'id') == txn.id:
            txn.transaction_order = idx
            break
Exemplo n.º 8
0
    def legacy_from_json(cls, obj):
        """
        Class method to decode v1 Tx from a legacy v0 Tx.
        """

        tv = obj.get_or('version', -1)
        if TransactionVersion.LEGACY.__ne__(tv):
            raise ValueError("legacy v0 transaction is expected to be of version {}, not version {}".format(
                TransactionVersion.LEGACY.value, tv))
        txn = cls()

        if 'data' not in obj:
            raise ValueError("no data object found in Legacy Transaction (v{})".format(
                TransactionVersion.LEGACY.value))
        txn_data = obj['data']
        if 'coininputs' in txn_data:
            for legacy_ci_info in (txn_data['coininputs'] or []):
                unlocker = legacy_ci_info.get_or('unlocker', jsobj.new_dict())
                ci_info = jsobj.as_dict({
                    'parentid': legacy_ci_info.get_or('parentid', ''),
                    'fulfillment': jsobj.as_dict({
                        'type': 1,
                        'data': jsobj.as_dict({
                            'publickey': unlocker.get_or('condition', jsobj.new_dict()).get_or('publickey'),
                            'signature': unlocker.get_or('fulfillment', jsobj.new_dict()).get_or('signature'),
                        }),
                    }),
                })
                ci = CoinInput.from_json(ci_info)
                txn._coin_inputs.append(ci)
        if 'coinoutputs' in txn_data:
            for legacy_co_info in (txn_data['coinoutputs'] or []):
                co_info = jsobj.as_dict({
                    'value': legacy_co_info.get_or('value', '0'),
                    'condition': jsobj.as_dict({
                        'type': 1,
                        'data': jsobj.as_dict({
                            'unlockhash': legacy_co_info.get_or('unlockhash', ''),
                        }),
                    }),
                })
                co = CoinOutput.from_json(co_info)
                txn._coin_outputs.append(co)
        if 'blockstakeinputs' in txn_data:
            for legacy_bsi_info in (txn_data['blockstakeinputs'] or []):
                unlocker = legacy_bsi_info.get_or('unlocker', jsobj.new_dict())
                bsi_info = jsobj.as_dict({
                    'parentid': legacy_bsi_info.get_or('parentid', ''),
                    'fulfillment': jsobj.as_dict({
                        'type': 1,
                        'data': jsobj.as_dict({
                            'publickey': unlocker.get_or('condition', jsobj.new_dict()).get_or('publickey'),
                            'signature': unlocker.get_or('fulfillment', jsobj.new_dict()).get_or('signature'),
                        }),
                    }),
                })
                bsi = BlockstakeInput.from_json(bsi_info)
                txn._blockstake_inputs.append(bsi)
        if 'blockstakeoutputs' in txn_data:
            for legacy_bso_info in (txn_data['blockstakeoutputs'] or []):
                bso_info = jsobj.as_dict({
                    'value': legacy_bso_info.get_or('value', '0'),
                    'condition': jsobj.as_dict({
                        'type': 1,
                        'data': jsobj.as_dict({
                            'unlockhash': legacy_bso_info.get_or('unlockhash', ''),
                        }),
                    }),
                })
                bso = BlockstakeOutput.from_json(bso_info)
                txn._blockstake_outputs.append(bso)

        if 'minerfees' in txn_data:
            for miner_fee in (txn_data['minerfees'] or []):
                txn._miner_fees.append(Currency.from_json(miner_fee))
        if 'arbitrarydata' in txn_data:
            txn._data = BinaryData.from_json(txn_data.get(
                'arbitrarydata', None) or '', strencoding='base64')

        txn._legacy = True
        return txn