How to use the bitcoinlib.encoding.to_hexstring function in bitcoinlib

To help you get started, we’ve selected a few bitcoinlib examples, based on popular ways it is used in public projects.

Secure your code as it's written. Use Snyk Code to scan source code in minutes - no build needed - and fix issues immediately.

github 1200wd / bitcoinlib / bitcoinlib / wallets.py View on Github external
tx_key.used = True
            tx_input = sess.query(DbTransactionInput). \
                filter_by(transaction_id=tx_id, index_n=ti.index_n).scalar()
            if not tx_input:
                index_n = ti.index_n
                if index_n is None:
                    last_index_n = sess.query(DbTransactionInput.index_n).\
                        filter_by(transaction_id=tx_id). \
                        order_by(DbTransactionInput.index_n.desc()).first()
                    index_n = 0
                    if last_index_n:
                        index_n = last_index_n[0] + 1

                new_tx_item = DbTransactionInput(
                    transaction_id=tx_id, output_n=ti.output_n_int, key_id=key_id, value=ti.value,
                    prev_hash=to_hexstring(ti.prev_hash), index_n=index_n, double_spend=ti.double_spend,
                    script=to_hexstring(ti.unlocking_script), script_type=ti.script_type)
                sess.add(new_tx_item)
            elif key_id:
                tx_input.key_id = key_id
                if ti.value:
                    tx_input.value = ti.value
                if ti.prev_hash:
                    tx_input.prev_hash = to_hexstring(ti.prev_hash)
                if ti.unlocking_script:
                    tx_input.script = to_hexstring(ti.unlocking_script)

            sess.commit()
        for to in self.outputs:
            tx_key = sess.query(DbKey).\
                filter_by(wallet_id=self.hdwallet.wallet_id, address=to.address).scalar()
            key_id = None
github 1200wd / bitcoinlib / bitcoinlib / wallets.py View on Github external
sess.commit()
        for to in self.outputs:
            tx_key = sess.query(DbKey).\
                filter_by(wallet_id=self.hdwallet.wallet_id, address=to.address).scalar()
            key_id = None
            if tx_key:
                key_id = tx_key.id
                tx_key.used = True
            spent = to.spent
            tx_output = sess.query(DbTransactionOutput). \
                filter_by(transaction_id=tx_id, output_n=to.output_n).scalar()
            if not tx_output:
                new_tx_item = DbTransactionOutput(
                    transaction_id=tx_id, output_n=to.output_n, key_id=key_id, value=to.value, spent=spent,
                    script=to_hexstring(to.lock_script), script_type=to.script_type)
                sess.add(new_tx_item)
            elif key_id:
                tx_output.key_id = key_id
                tx_output.spent = spent if spent is not None else tx_output.spent
            sess.commit()
        return tx_id
github 1200wd / bitcoinlib / bitcoinlib / wallets.py View on Github external
depth = 5 if len(set(depths)) != 1 else depths[0]

            # Calculate redeemscript and address and add multisig key to database
            redeemscript = serialize_multisig_redeemscript(public_key_list, n_required=self.multisig_n_required)
            address = pubkeyhash_to_addr(script_to_pubkeyhash(redeemscript),
                                         versionbyte=Network(network).prefix_address_p2sh)
            if len(set([x['path'] for x in public_keys])) == 1:
                path = public_keys[0]['path']
            else:
                path = "multisig-%d-of-" % self.multisig_n_required + '/'.join(public_key_ids)
            if not name:
                name = "Multisig Key " + '/'.join(public_key_ids)
            multisig_key = DbKey(
                name=name, wallet_id=self.wallet_id, purpose=self.purpose, account_id=account_id,
                depth=depth, change=change, address_index=0, parent_id=0, is_private=False, path=path,
                public=to_hexstring(redeemscript), wif='multisig-%s' % address, address=address,
                key_type='multisig', network_name=network)
            self._session.add(multisig_key)
            self._session.commit()
            for child_id in public_key_ids:
                self._session.add(DbKeyMultisigChildren(key_order=public_key_ids.index(child_id),
                                                        parent_id=multisig_key.id, child_id=child_id))
            self._session.commit()
            return HDWalletKey(multisig_key.id, session=self._session)
github 1200wd / bitcoinlib / bitcoinlib / wallets.py View on Github external
elif self.scheme == 'multisig':
                depth = 5
                account_id = None
            else:
                depth = 0
        addresslist = self.addresslist(account_id=account_id, used=used, network=network, key_id=key_id,
                                       change=change, depth=depth)
        srv = Service(network=network)
        txs = srv.gettransactions(addresslist)
        if txs is False:
            raise WalletError("No response from any service provider, could not update transactions")
        utxo_set = set()
        for t in txs:
            wt = HDWalletTransaction.from_transaction(self, t)
            wt.save()
            utxos = [(to_hexstring(ti.prev_hash), ti.output_n_int) for ti in wt.inputs]
            utxo_set.update(utxos)

        for utxo in list(utxo_set):
            tos = self._session.query(DbTransactionOutput).join(DbTransaction).\
                filter(DbTransaction.hash == utxo[0], DbTransactionOutput.output_n == utxo[1],
                       DbTransactionOutput.spent.op("IS")(False)).all()
            for u in tos:
                u.spent = True
        self._session.commit()
        self._balance_update(account_id=account_id, network=network, key_id=key_id, min_confirms=0)

        return len(txs)
github 1200wd / bitcoinlib / bitcoinlib / wallets.py View on Github external
tx_input = sess.query(DbTransactionInput). \
                filter_by(transaction_id=tx_id, index_n=ti.index_n).scalar()
            if not tx_input:
                index_n = ti.index_n
                if index_n is None:
                    last_index_n = sess.query(DbTransactionInput.index_n).\
                        filter_by(transaction_id=tx_id). \
                        order_by(DbTransactionInput.index_n.desc()).first()
                    index_n = 0
                    if last_index_n:
                        index_n = last_index_n[0] + 1

                new_tx_item = DbTransactionInput(
                    transaction_id=tx_id, output_n=ti.output_n_int, key_id=key_id, value=ti.value,
                    prev_hash=to_hexstring(ti.prev_hash), index_n=index_n, double_spend=ti.double_spend,
                    script=to_hexstring(ti.unlocking_script), script_type=ti.script_type)
                sess.add(new_tx_item)
            elif key_id:
                tx_input.key_id = key_id
                if ti.value:
                    tx_input.value = ti.value
                if ti.prev_hash:
                    tx_input.prev_hash = to_hexstring(ti.prev_hash)
                if ti.unlocking_script:
                    tx_input.script = to_hexstring(ti.unlocking_script)

            sess.commit()
        for to in self.outputs:
            tx_key = sess.query(DbKey).\
                filter_by(wallet_id=self.hdwallet.wallet_id, address=to.address).scalar()
            key_id = None
            if tx_key:
github 1200wd / bitcoinlib / bitcoinlib / services / blockcypher.py View on Github external
t.confirmations = tx['confirmations']
        t.block_height = tx['block_height']
        t.block_hash = tx.get('block_hash')
        t.fee = tx['fees']
        t.rawtx = tx['hex']
        t.size = int(len(tx['hex']) / 2)
        t.network = self.network
        t.input_total = 0
        if t.coinbase:
            t.input_total = t.output_total
        if len(t.inputs) != len(tx['inputs']):
            raise ClientError("Invalid number of inputs provided. Raw tx: %d, blockcypher: %d" %
                              (len(t.inputs), len(tx['inputs'])))
        for n, i in enumerate(t.inputs):
            if not t.coinbase and not (tx['inputs'][n]['output_index'] == i.output_n_int and
                        tx['inputs'][n]['prev_hash'] == to_hexstring(i.prev_hash)):
                raise ClientError("Transaction inputs do not match raw transaction")
            if 'output_value' in tx['inputs'][n]:
                i.value = tx['inputs'][n]['output_value']
                t.input_total += i.value
        if len(t.outputs) != len(tx['outputs']):
            raise ClientError("Invalid number of outputs provided. Raw tx: %d, blockcypher: %d" %
                              (len(t.outputs), len(tx['outputs'])))
        for n, o in enumerate(t.outputs):
            if 'spent_by' in tx['outputs'][n]:
                o.spent = True
        t.raw_hex()
        return t
github 1200wd / bitcoinlib / bitcoinlib / services / smartbit.py View on Github external
t = self.gettransaction(txid)
            for utxo in t.outputs:
                if utxo.address != address:
                    continue
                utxos.append(
                    {
                        'address': utxo.address,
                        'tx_hash': t.hash,
                        'confirmations': t.confirmations,
                        'output_n': utxo.output_n,
                        'input_n': 0,
                        'block_height': t.block_height,
                        'fee': t.fee,
                        'size': t.size,
                        'value': utxo.value,
                        'script': to_hexstring(utxo.lock_script),
                        'date': t.date
                    })
        return utxos
github 1200wd / bitcoinlib / bitcoinlib / tools / clw.py View on Github external
def print_transaction(wt):
    tx_dict = {
        'network': wt.network.name, 'fee': wt.fee, 'raw': wt.raw_hex(), 'outputs': [{
            'address': o.address, 'value': o.value
        } for o in wt.outputs], 'inputs': [{
            'prev_hash': to_hexstring(i.prev_hash), 'output_n': struct.unpack('>I', i.output_n)[0],
            'address': i.address, 'signatures': [{
                'signature': s.hex(), 'sig_der': s.as_der_encoded(as_hex=True),
                'pub_key': s.public_key.public_hex,
            } for s in i.signatures], 'value': i.value
        } for i in wt.inputs]
    }
    pprint(tx_dict)