def __init__(self, bitcoin_chain, path_to_secret, disable_safe_mode): super().__init__() self.allowable_wif_prefixes = wif_prefix_for_netcode( bitcoin_chain.netcode) self.path_to_secret = path_to_secret self.disable_safe_mode = disable_safe_mode self.wif = None
def wif(self, use_uncompressed=None): """ Return the WIF representation of this key, if available. If use_uncompressed is not set, the preferred representation is returned. """ wif_prefix = wif_prefix_for_netcode(self._netcode) secret_exponent = self.secret_exponent() if secret_exponent is None: return None return secret_exponent_to_wif(secret_exponent, compressed=not self._use_uncompressed(use_uncompressed), wif_prefix=wif_prefix)
def __init__(self, bitcoin_chain, path_to_secret, safe_mode=True, issuing_address=None): super().__init__() self.allowable_wif_prefixes = wif_prefix_for_netcode( bitcoin_chain.netcode) self.path_to_secret = path_to_secret self.safe_mode = safe_mode self.wif = None self.issuing_address = issuing_address
def privkey_to_wif(privkey, netcode="BTC"): """ Get private key from bitcoin wif. Args: privkey (str): Hex encoded private key Return: str: Private key encode in bitcoin wif format. """ prefix = networks.wif_prefix_for_netcode(netcode) secret_exponent = encoding.from_bytes_32(h2b(privkey)) return encoding.secret_exponent_to_wif(secret_exponent, wif_prefix=prefix)
def _calculate_all(self): for attr in "_secret_exponent _public_pair _wif_uncompressed _wif_compressed _sec_compressed" \ " _sec_uncompressed _hash160_compressed _hash160_uncompressed _address_compressed" \ " _address_uncompressed _netcode".split(): setattr(self, attr, getattr(self, attr, None)) if self._hierarchical_wallet: if self._hierarchical_wallet.is_private: self._secret_exponent = self._hierarchical_wallet.secret_exponent else: self._public_pair = self._hierarchical_wallet.public_pair self._netcode = self._hierarchical_wallet.netcode wif_prefix = wif_prefix_for_netcode(self._netcode) if self._secret_exponent: self._wif_uncompressed = secret_exponent_to_wif( self._secret_exponent, compressed=False, wif_prefix=wif_prefix) self._wif_compressed = secret_exponent_to_wif( self._secret_exponent, compressed=True, wif_prefix=wif_prefix) self._public_pair = ecdsa.public_pair_for_secret_exponent( ecdsa.generator_secp256k1, self._secret_exponent) if self._public_pair: self._sec_compressed = public_pair_to_sec(self._public_pair, compressed=True) self._sec_uncompressed = public_pair_to_sec(self._public_pair, compressed=False) self._hash160_compressed = hash160(self._sec_compressed) self._hash160_uncompressed = hash160(self._sec_uncompressed) address_prefix = address_prefix_for_netcode(self._netcode) if self._hash160_compressed: self._address_compressed = hash160_sec_to_bitcoin_address( self._hash160_compressed, address_prefix=address_prefix) if self._hash160_uncompressed: self._address_uncompressed = hash160_sec_to_bitcoin_address( self._hash160_uncompressed, address_prefix=address_prefix)
def sign_tx(self, hex_tx, tx_input, netcode): """ Sign the transaction with private key :param hex_tx: :param tx_input: :param netcode: :return: """ logging.info('Signing tx with private key') self.secret_manager.start() wif = self.secret_manager.get_wif() transaction = Tx.from_hex(hex_tx) allowable_wif_prefixes = wif_prefix_for_netcode(netcode) se = wif_to_secret_exponent(wif, allowable_wif_prefixes) lookup = build_hash160_lookup([se]) transaction.set_unspents([TxOut(coin_value=tx_input.coin_value, script=tx_input.script)]) signed_tx = transaction.sign(lookup) self.secret_manager.stop() logging.info('Finished signing transaction') return signed_tx
from cert_issuer import helpers from cert_issuer.errors import UnverifiedTransactionError from cert_issuer.helpers import internet_off_for_scope from cert_issuer.models import TransactionCosts COIN = 100000000 # satoshis in 1 btc BYTES_PER_INPUT = 148 # assuming compressed public key BYTES_PER_OUTPUT = 34 FIXED_EXTRA_BYTES = 10 OP_RETURN_BYTE_COUNT = 43 # our op_return output values always have the same length because they are SHA-256 hashes cost_constants = config.get_constants() RECOMMENDED_FEE = cost_constants.recommended_fee_per_transaction * COIN MIN_PER_OUTPUT = cost_constants.min_per_output * COIN SATOSHI_PER_BYTE = cost_constants.satoshi_per_byte ALLOWABLE_WIF_PREFIXES = wif_prefix_for_netcode(config.get_config().netcode) def create_trx(op_return_val, issuing_transaction_cost, issuing_address, tx_outs, tx_input): """ :param op_return_val: :param issuing_transaction_cost: :param issuing_address: :param tx_outs: :param tx_input: :return: """ cert_out = CMutableTxOut(0, CScript([OP_RETURN, op_return_val])) tx_ins = [CTxIn(COutPoint(tx_input.tx_hash, tx_input.tx_out_index))]
def __init__(self, bitcoin_chain): self.bitcoin_chain = bitcoin_chain self.allowable_wif_prefixes = wif_prefix_for_netcode( to_pycoin_chain(bitcoin_chain))
def __init__(self, netcode, wif_iterable, secret_exponent_db_cache): super(LazySecretExponentDBWithNetwork, self).__init__(wif_iterable, secret_exponent_db_cache) self.netcode = netcode self.wif_prefix = wif_prefix_for_netcode(netcode)
def __init__(self, bitcoin_chain): self.bitcoin_chain = bitcoin_chain self.allowable_wif_prefixes = wif_prefix_for_netcode(bitcoin_chain.netcode)