Ejemplo n.º 1
0
 def _from_json_data_object(self, data):
     # decode address
     if 'address' in data:
         self._address = UnlockHash.from_json(data['address'])
     else:
         self._address = None
     # decode value
     if 'value' in data:
         self._value = Currency.from_json(data['value'])
     else:
         self._value = None
     # decode transaction fee
     if 'txfee' in data:
         self._transaction_fee = Currency.from_json(data['txfee'])
     else:
         self._transaction_fee = None
     # decode blockid
     if 'blockid' in data:
         self._blockid = ERC20Hash.from_json(data['blockid'])
     else:
         self._blockid = None
     # decode transactionid
     if 'txid' in data:
         self._transactionid = ERC20Hash.from_json(data['txid'])
     else:
         self._transactionid = None
Ejemplo n.º 2
0
 def _from_json_data_object(self, data):
     # decode public key
     if 'pubkey' in data:
         self._public_key = PublicKey.from_json(data['pubkey'])
     else:
         self._public_key = None
     # decode signature
     if 'signature' in data:
         self._signature = ED25519Signature.from_json(data['signature'])
     else:
         self._signature = None
     # decode registration fee
     if 'regfee' in data:
         self.registration_fee = Currency.from_json(data['regfee'])
     else:
         self.registration_fee = None
     # decode transaction fee
     if 'txfee' in data:
         self._transaction_fee = Currency.from_json(data['txfee'])
     else:
         self._transaction_fee = None
     # decode coin inputs
     self._coin_inputs = [
         CoinInput.from_json(ci) for ci in data.get('coininputs', []) or []
     ]
     # decode refund coin output (if it exists)
     if 'refundcoinoutput' in data:
         self._refund_coin_output = CoinOutput.from_json(
             data['refundcoinoutput'])
     else:
         self._refund_coin_output = None
Ejemplo n.º 3
0
 def _from_json_data_object(self, data):
     # decode address
     if 'address' in data:
         self._address = ERC20Address.from_json(data['address'])
     else:
         self._address = None
     # decode value
     if 'value' in data:
         self._value = Currency.from_json(data['value'])
     else:
         self._value = None
     # decode transaction fee
     if 'txfee' in data:
         self._transaction_fee = Currency.from_json(data['txfee'])
     else:
         self._transaction_fee = None
     # decode coin inputs
     self._coin_inputs = [
         CoinInput.from_json(ci) for ci in data.get('coininputs', []) or []
     ]
     # decode refund coin output (if it exists)
     if 'refundcoinoutput' in data:
         self._refund_coin_output = CoinOutput.from_json(
             data['refundcoinoutput'])
     else:
         self._refund_coin_output = None
Ejemplo n.º 4
0
 def _from_json_data_object(self, data):
     # decode sender info
     if 'sender' in data:
         bot_data = data['sender']
         self._sender_botid = int(bot_data.get('id', 0) or 0)
         self._sender_signature = ED25519Signature.from_json(bot_data.get('signature', None) or None)
     else:
         self._sender_botid = None
         self._sender_signature = None
     # decode receiver info
     if 'receiver' in data:
         bot_data = data['receiver']
         self._receiver_botid = int(bot_data.get('id', 0) or 0)
         self._receiver_signature = ED25519Signature.from_json(bot_data.get('signature', None) or None)
     else:
         self._receiver_botid = None
         self._receiver_signature = None
     # decode names
     self._names = [BotName.from_json(name) for name in data.get('names', []) or []]
     # decode transaction fee
     if 'txfee' in data:
         self._transaction_fee = Currency.from_json(data['txfee'])
     else:
         self._transaction_fee = None
     # decode coin inputs
     self._coin_inputs = [CoinInput.from_json(ci) for ci in data.get('coininputs', []) or []]
     # decode refund coin output
     if 'refundcoinoutput' in data:
         self._refund_coin_output = CoinOutput.from_json(data['refundcoinoutput'])
     else:
         self._refund_coin_output = None
Ejemplo n.º 5
0
 def _from_json_data_object(self, data):
     self._botid = int(data.get('id', 0) or 0)
     addresses = data.get('addresses', {}) or {}
     self._addresses_to_add = [
         NetworkAddress.from_json(address)
         for address in addresses.get('add', []) or []
     ]
     self._addresses_to_remove = [
         NetworkAddress.from_json(address)
         for address in addresses.get('remove', []) or []
     ]
     names = data.get('names', {}) or {}
     self._names_to_add = [
         BotName.from_json(name) for name in names.get('add', []) or []
     ]
     self._names_to_remove = [
         BotName.from_json(name) for name in names.get('remove', []) or []
     ]
     self._number_of_months = int(data.get('nrofmonths', 0) or 0)
     if 'txfee' in data:
         self._transaction_fee = Currency.from_json(data['txfee'])
     else:
         self._transaction_fee = None
     self._coin_inputs = [
         CoinInput.from_json(ci) for ci in data.get('coininputs', []) or []
     ]
     if 'refundcoinoutput' in data:
         self._refund_coin_output = CoinOutput.from_json(
             data['refundcoinoutput'])
     else:
         self._refund_coin_output = None
     self._signature = ED25519Signature.from_json(
         data.get('signature', None) or None)
Ejemplo n.º 6
0
 def _from_json_data_object(self, data):
     self._addresses = [
         NetworkAddress.from_json(address)
         for address in data.get('addresses', []) or []
     ]
     self._names = [
         BotName.from_json(name) for name in data.get('names', []) or []
     ]
     self._number_of_months = int(data.get('nrofmonths', 0) or 0)
     if 'txfee' in data:
         self._transaction_fee = Currency.from_json(data['txfee'])
     else:
         self._transaction_fee = None
     self._coin_inputs = [
         CoinInput.from_json(ci) for ci in data.get('coininputs', []) or []
     ]
     if 'refundcoinoutput' in data:
         self._refund_coin_output = CoinOutput.from_json(
             data['refundcoinoutput'])
     else:
         self._refund_coin_output = None
     if 'identification' not in data or data['identification'] in (None,
                                                                   {}):
         self._public_key = None
         self._signature = None
     else:
         identification = data['identification']
         self._public_key = PublicKey.from_json(identification['publickey'])
         self._signature = ED25519Signature.from_json(
             identification['signature'], as_array=True)
Ejemplo n.º 7
0
 def _from_json_data_object(self, data):
     self._nonce = BinaryData.from_json(data.get('nonce', ''),
                                        strencoding='base64')
     self._mint_condition = conditions.from_json(
         data.get('mintcondition', {}))
     self._mint_fulfillment = fulfillments.from_json(
         data.get('mintfulfillment', {}))
     self._miner_fees = [
         Currency.from_json(fee) for fee in data.get('minerfees', []) or []
     ]
     self._data = BinaryData.from_json(data.get('arbitrarydata', None)
                                       or '',
                                       strencoding='base64')
Ejemplo n.º 8
0
 def _from_json_data_object(self, data):
     self._coin_inputs = [CoinInput.from_json(
         ci) for ci in data.get('coininputs', []) or []]
     self._coin_outputs = [CoinOutput.from_json(
         co) for co in data.get('coinoutputs', []) or []]
     self._blockstake_inputs = [BlockstakeInput.from_json(
         bsi) for bsi in data.get('blockstakeinputs', []) or []]
     self._blockstake_outputs = [BlockstakeOutput.from_json(
         bso) for bso in data.get('blockstakeoutputs', []) or []]
     self._miner_fees = [Currency.from_json(
         fee) for fee in data.get('minerfees', []) or []]
     self._data = BinaryData.from_json(
         data.get('arbitrarydata', None) or '', strencoding='base64')
Ejemplo n.º 9
0
 def _block_get_parse_cb(self, result):
     endpoint, block = result
     try:
         # parse the transactions
         transactions = []
         for etxn in block['transactions']:
             # parse the explorer transaction
             transaction = self._transaction_from_explorer_transaction(
                 etxn, endpoint=endpoint, resp=block)
             # append the transaction to the list of transactions
             transactions.append(transaction)
         rawblock = block['rawblock']
         # parse the parent id
         parentid = Hash.from_json(obj=rawblock['parentid'])
         # parse the miner payouts
         miner_payouts = []
         minerpayoutids = block.get_or('minerpayoutids', None) or []
         eminerpayouts = rawblock.get_or('minerpayouts', None) or []
         if len(eminerpayouts) != len(minerpayoutids):
             raise tferrors.ExplorerInvalidResponse(
                 "amount of miner payouts and payout ids are not matching: {} != {}"
                 .format(len(eminerpayouts),
                         len(minerpayoutids)), endpoint, block)
         for idx, mp in enumerate(eminerpayouts):
             id = Hash.from_json(minerpayoutids[idx])
             value = Currency.from_json(mp['value'])
             unlockhash = UnlockHash.from_json(mp['unlockhash'])
             miner_payouts.append(
                 ExplorerMinerPayout(id=id,
                                     value=value,
                                     unlockhash=unlockhash))
         # get the timestamp and height
         height = int(block['height'])
         timestamp = int(rawblock['timestamp'])
         # get the block's identifier
         blockid = Hash.from_json(block['blockid'])
         # for all transactions assign these properties
         for transaction in transactions:
             _assign_block_properties_to_transacton(transaction, block)
             transaction.height = height
             transaction.blockid = blockid
         # return the block, as reported by the explorer
         return ExplorerBlock(id=blockid,
                              parentid=parentid,
                              height=height,
                              timestamp=timestamp,
                              transactions=transactions,
                              miner_payouts=miner_payouts)
     except KeyError as exc:
         raise tferrors.ExplorerInvalidResponse(str(exc), endpoint,
                                                block) from exc
Ejemplo n.º 10
0
 def _from_json_data_object(self, data):
     self._nonce = BinaryData.from_json(data.get('nonce', ''),
                                        strencoding='base64')
     self._mint_fulfillment = FulfillmentTypes.from_json(
         data.get('mintfulfillment', {}))
     self._coin_outputs = [
         CoinOutput.from_json(co)
         for co in data.get('coinoutputs', []) or []
     ]
     self._miner_fees = [
         Currency.from_json(fee) for fee in data.get('minerfees', []) or []
     ]
     self._data = BinaryData.from_json(data.get('arbitrarydata', None)
                                       or '',
                                       strencoding='base64')
Ejemplo n.º 11
0
 def from_json(cls, obj):
     return cls(
         value=Currency.from_json(obj['value']),
         condition=ConditionTypes.from_json(obj['condition']))
Ejemplo n.º 12
0
 def block_get(self, value):
     """
     Get a block from an available explorer Node.
     
     @param value: the identifier or height that points to the desired block
     """
     endpoint = "/explorer/?"
     resp = {}
     try:
         # get the explorer block
         if isinstance(value, int):
             endpoint = "/explorer/blocks/{}".format(int(value))
             resp = self.explorer_get(endpoint=endpoint)
             resp = json_loads(resp)
             resp = resp['block']
         else:
             blockid = self._normalize_id(value)
             endpoint = "/explorer/hashes/" + blockid
             resp = self.explorer_get(endpoint=endpoint)
             resp = json_loads(resp)
             if resp['hashtype'] != 'blockid':
                 raise tfchain.errors.ExplorerInvalidResponse(
                     "expected hash type 'blockid' not '{}'".format(
                         resp['hashtype']), endpoint, resp)
             resp = resp['block']
             if resp['blockid'] != blockid:
                 raise tfchain.errors.ExplorerInvalidResponse(
                     "expected block ID '{}' not '{}'".format(
                         blockid, resp['blockid']), endpoint, resp)
         # parse the transactions
         transactions = []
         for etxn in resp['transactions']:
             # parse the explorer transaction
             transaction = self._transaction_from_explorer_transaction(
                 etxn, endpoint=endpoint, resp=resp)
             # append the transaction to the list of transactions
             transactions.append(transaction)
         rawblock = resp['rawblock']
         # parse the parent id
         parentid = Hash.from_json(obj=rawblock['parentid'])
         # parse the miner payouts
         miner_payouts = []
         minerpayoutids = resp.get('minerpayoutids', None) or []
         eminerpayouts = rawblock.get('minerpayouts', None) or []
         if len(eminerpayouts) != len(minerpayoutids):
             raise tfchain.errors.ExplorerInvalidResponse(
                 "amount of miner payouts and payout ids are not matching: {} != {}"
                 .format(len(eminerpayouts),
                         len(minerpayoutids)), endpoint, resp)
         for idx, mp in enumerate(eminerpayouts):
             id = Hash.from_json(minerpayoutids[idx])
             value = Currency.from_json(mp['value'])
             unlockhash = UnlockHash.from_json(mp['unlockhash'])
             miner_payouts.append(
                 ExplorerMinerPayout(id=id,
                                     value=value,
                                     unlockhash=unlockhash))
         # get the timestamp and height
         height = int(resp['height'])
         timestamp = int(rawblock['timestamp'])
         # get the block's identifier
         blockid = Hash.from_json(resp['blockid'])
         # return the block, as reported by the explorer
         return ExplorerBlock(id=blockid,
                              parentid=parentid,
                              height=height,
                              timestamp=timestamp,
                              transactions=transactions,
                              miner_payouts=miner_payouts)
     except KeyError as exc:
         # return a KeyError as an invalid Explorer Response
         raise tfchain.errors.ExplorerInvalidResponse(
             str(exc), endpoint, resp) from exc
Ejemplo n.º 13
0
    def legacy_from_json(cls, obj):
        """
        Class method to decode v1 Tx from a legacy v0 Tx.
        """

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

        if 'data' not in obj:
            raise ValueError("no data object found in Legacy Transaction (v{})".format(
                TransactionVersion.LEGACY))
        txn_data = obj['data']
        if 'coininputs' in txn_data:
            for legacy_ci_info in (txn_data['coininputs'] or []):
                unlocker = legacy_ci_info.get('unlocker', {})
                ci_info = {
                    'parentid': legacy_ci_info.get('parentid', ''),
                    'fulfillment': {
                        'type': 1,
                        'data': {
                            'publickey': unlocker.get('condition', {}).get('publickey'),
                            'signature': unlocker.get('fulfillment', {}).get('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 = {
                    'value': legacy_co_info.get('value', '0'),
                    'condition': {
                        'type': 1,
                        'data': {
                            'unlockhash': legacy_co_info.get('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('unlocker', {})
                bsi_info = {
                    'parentid': legacy_bsi_info.get('parentid', ''),
                    'fulfillment': {
                        'type': 1,
                        'data': {
                            'publickey': unlocker.get('condition', {}).get('publickey'),
                            'signature': unlocker.get('fulfillment', {}).get('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 = {
                    'value': legacy_bso_info.get('value', '0'),
                    'condition': {
                        'type': 1,
                        'data': {
                            'unlockhash': legacy_bso_info.get('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