示例#1
0
def is_address(address, network=None):
    """
    Check Bitcoin address.

    :param address: Bitcoin address.
    :type address: str
    :param network: Bitcoin network, defaults to testnet.
    :type network: str
    :returns: bool -- Bitcoin valid/invalid address.

    >>> from shuttle.providers.bitcoin.utils import is_address
    >>> is_address(bitcoin_address, "testnet")
    True
    """

    if isinstance(address, str):
        if network is None:
            for boolean in [True, False]:
                valid = False
                if cryptos.Bitcoin(testnet=boolean).is_address(address):
                    valid = True
                    break
            return valid
        if network == "testnet":
            return cryptos.Bitcoin(testnet=True).is_address(address)
        elif network == "mainnet":
            return cryptos.Bitcoin(testnet=False).is_address(address)
        else:
            raise NetworkError("invalid %s network" % network, "only takes testnet or mainnet")
    raise TypeError("address must be string format!")
示例#2
0
文件: utils.py 项目: meherett/swap
def is_address(address: str,
               network: Optional[str] = None,
               address_type: Optional[str] = None) -> bool:
    """
    Check Bitcoin address.

    :param address: Bitcoin address.
    :type address: str
    :param network: Bitcoin network, defaults to None.
    :type network: str
    :param address_type: Bitcoin address type, defaults to None.
    :type address_type: str

    :returns: bool -- Bitcoin valid/invalid address.

    >>> from swap.providers.bitcoin.utils import is_address
    >>> is_address(address="mrmtGq2HMmqAogSsGDjCtXUpxrb7rHThFH", network="testnet")
    True
    """

    if not isinstance(address, str):
        raise TypeError(f"Address must be str, not '{type(address)}' type.")
    if address_type and address_type not in ["p2pkh", "p2sh"]:
        raise TypeError(
            "Address type must be str and choose only 'p2pkh' or 'p2sh' types."
        )

    if network is None:
        for boolean in [True, False]:
            valid = False
            if cryptos.Bitcoin(testnet=boolean).is_address(address):
                valid = True
                break
        if address_type:
            valid = True if valid and (get_address_type(
                address=address) == address_type) else False
        return valid

    if not is_network(network=network):
        raise NetworkError(f"Invalid Bitcoin '{network}' network",
                           "choose only 'mainnet' or 'testnet' networks.")

    valid: bool = False
    if network == "mainnet":
        valid = cryptos.Bitcoin(testnet=False).is_address(address)
        if address_type:
            valid = True if valid and (get_address_type(
                address=address) == address_type) else False
    elif network == "testnet":
        valid = cryptos.Bitcoin(testnet=True).is_address(address)
        if address_type:
            valid = True if valid and (get_address_type(
                address=address) == address_type) else False
    return valid
示例#3
0
def sign_message(message, coin, key=None, encoding='utf8'):

    # message = message.encode('utf8')

    coin_dispatcher = {
        'btc': cr.Bitcoin(),
        'bch': cr.BitcoinCash(),
        'ltc': cr.Litecoin()
    }

    client = coin_dispatcher[coin]

    # if no key provided, create one randomly
    if key is None:
        priv = random.randint(1, KEY_UPPER_LIMIT)
        pub = client.privtopub(priv)
        addr = client.privtoaddr(priv)
    else:
        try:
            priv = int(key)
            pub = client.privtopub(priv)
            addr = client.privtoaddr(priv)
        except:
            priv = cr.sha256(key)
            pub = client.privtopub(priv)
            addr = client.privtoaddr(priv)

    # sign message
    # if coin == 'ltc':
    #     signature = cr.litecoin_ecdsa_sign(message, priv)
    # else:
    #     signature = cr.ecdsa_sign(message, priv)
    signature = cr.bytes_ecdsa_sign(message, priv, coin, encoding)

    if coin == 'bch':
        addr = convert.to_cash_address(addr)

    # output
    output = {
        "address": addr,
        "signature": signature,
        "message": message,
        "private_key": priv,
        "public_key": pub,
        "coin": coin
    }

    return output
示例#4
0
def transfer(currency, amount, addressto, addressfrom, secret):

    if currency == 'ETH':
        receipt = send_eth(amount, addressto, addressfrom, secret)
    if currency == 'BTC':
        coin = cryptos.Bitcoin()
    if currency == 'LTC':
        coin = cryptos.Litecoin()
    if currency == 'DASH':
        coin = cryptos.Dash()
    if currency == 'BCH':
        coin = cryptos.BitcoinCash()
    tx = coin.preparesignedtx(secret,
                              addressto,
                              amount,
                              change_addr=addressfrom)
    receipt = cryptos.pushtx(tx)
示例#5
0
def balance(currency, address):

    try:
        assert currency in [*cointable]
        if currency == 'ETH':
            balance = w3.eth.getBalance(address)
            return web3.utils.fromWei(balance)
        else:
            if currency == 'BTC':
                coin = cryptos.Bitcoin()
            if currency == 'LTC':
                coin = cryptos.Litecoin()
            if currency == 'DASH':
                coin = cryptos.Dash()
            if currency == 'BCH':
                coin = cryptos.BitcoinCash()
            balance = coin.history(address)['final_balance']
            return balance

    except Exception as e:
        print(e)
        return 0
示例#6
0
文件: cc.py 项目: pphili/bitcast
 def __init__(self, address, network=cry.Bitcoin(testnet=True)):
     self.net = network
     self.addr = address