예제 #1
0
def validate_data_provider(ctx, param, value):
    """ Validates the data provider sent in via the CLI.

    Args:
        ctx (Click context): Click context object.
        param (str): Parameter that is being validated.
        value (str): Parameter value.
    """
    data_provider_params = {}
    if ctx.obj is None:
        ctx.obj = {}

    if value not in REQUIRED_DATA_PROVIDER_PARAMS:
        ctx.fail("Unknown data provider %s" % value)

    required = REQUIRED_DATA_PROVIDER_PARAMS[value]

    fail = False
    for r in required:
        if r not in ctx.params:
            s = r.replace('_', '-')
            click.echo("--%s is required to use %s." % (s, value))
            fail = True
        else:
            data_provider_params[r] = ctx.params[r]

    if fail:
        ctx.fail("One or more required arguments are missing.")

    dp = None
    if value == 'insight':
        url = ctx.params['insight_url']
        api_path = ctx.params['insight_api_path']
        dp = InsightProvider(insight_host_name=url,
                             insight_api_path=api_path)
    elif value == 'twentyone':
        dp = TwentyOneProvider()

    ctx.obj['data_provider'] = dp
    ctx.obj['data_provider_params'] = data_provider_params
예제 #2
0
 def __init__(self, wallet, db=None):
     """Initialize payment handling for on-chain payments."""
     self.db = db or OnChainSQLite3()
     self.address = wallet.get_payout_address()
     self.provider = TwentyOneProvider(TWO1_PROVIDER_HOST)
예제 #3
0
    def __init__(self,
                 config_file=TWO1_CONFIG_FILE,
                 config=None,
                 create_wallet=True):
        if not os.path.exists(TWO1_USER_FOLDER):
            os.makedirs(TWO1_USER_FOLDER)
        self.file = path(config_file).expand().abspath()
        self.dir = self.file.parent
        self.defaults = {
        }  # TODO: Rename this var. Those are not the defaults but the actual values.
        self.json_output = False  # output in json
        #  actual config.
        self.load()
        # override config variables
        if config:
            if self.verbose:
                self.vlog("Applied manual config.")

            for k, v in config:
                self.defaults[k] = v
                if self.verbose:
                    self.vlog("\t{}={}".format(k, v))

        # add wallet object
        if self.defaults.get('testwallet', None) == 'y':
            self.wallet = test_wallet.TestWallet()
        elif create_wallet:
            dp = TwentyOneProvider(TWO1_PROVIDER_HOST)

            wallet_path = self.defaults.get('wallet_path')

            if not Two1Wallet.check_wallet_file(wallet_path):
                # configure wallet with default options
                click.pause(UxString.create_wallet)

                wallet_options = {
                    'data_provider': dp,
                    'wallet_path': wallet_path
                }

                if not Two1Wallet.configure(wallet_options):
                    raise click.ClickException(
                        UxString.Error.create_wallet_failed)

                # Display the wallet mnemonic and tell user to back it up.
                # Read the wallet JSON file and extract it.
                with open(wallet_path, 'r') as f:
                    wallet_config = json.load(f)
                    mnemonic = wallet_config['master_seed']

                click.pause(UxString.create_wallet_done % (mnemonic))

            # Start the daemon, if:
            # 1. It's not already started
            # 2. It's using the default wallet path
            # 3. We're not in a virtualenv
            try:
                d = daemonizer.get_daemonizer()

                if Two1Wallet.is_configured() and \
                   wallet_path == Two1Wallet.DEFAULT_WALLET_PATH and \
                   not os.environ.get("VIRTUAL_ENV") and \
                   not d.started():
                    d.start()
                    if d.started():
                        click.echo(UxString.wallet_daemon_started)
            except (OSError, DaemonizerError):
                pass

            self.wallet = Wallet(wallet_path=wallet_path, data_provider=dp)
            self.machine_auth = MachineAuthWallet(self.wallet)
            self.channel_client = PaymentChannelClient(self.wallet)
        else:
            # This branch is hit when '21 help' or '21 update' is invoked
            pass
예제 #4
0
from two1.lib.wallet import Wallet, Two1Wallet
from two1.commands.config import TWO1_PROVIDER_HOST
from two1.lib.bitserv import PaymentServer, DatabaseDjango
from two1.lib.blockchain.twentyone_provider import TwentyOneProvider
from two1.lib.blockchain.exceptions import DataProviderError
from two1.examples.bitcoin_auth.helpers.bitcoin_auth_provider_helper import (
    BitcoinAuthProvider)

from .models import BitcoinToken, PaymentChannel, PaymentChannelSpend
from .pricing import get_price_for_request
from .exceptions import PaymentRequiredException
from .exceptions import ServiceUnavailable

if settings.WALLET_MNEMONIC:
    dp = TwentyOneProvider(TWO1_PROVIDER_HOST)
    wallet = Two1Wallet.import_from_mnemonic(dp, settings.WALLET_MNEMONIC)
else:
    wallet = Wallet()
payment_server = PaymentServer(
    wallet, DatabaseDjango(PaymentChannel, PaymentChannelSpend))


class BaseBitcoinAuthentication(BaseAuthentication):
    """Basic Bitcoin authentication.

    Attributes:
        bitcoin_provider_helper (BlockchainProvider): provides
            an api & helper methods into the blockchain.
    """
예제 #5
0
import io

import uuid

# change the receiving_key in config.py in the root folder.
from config import receiving_key, SATOSHIS_PER_MINUTE

# logging.basicConfig(level=logging.DEBUG)
logging.basicConfig(level=logging.ERROR)

auth_app = Flask(__name__, static_folder='static')
auth_app.config['SQLALCHEMY_DATABASE_URI'] = 'sqlite:////tmp/wifiportal21.db'
db = SQLAlchemy(auth_app)

K = HDPublicKey.from_b58check(receiving_key)
blockchain_provider = TwentyOneProvider()
cache = CacheManager()
receiving_account = HDAccount(hd_key=K,
                              name="hotspot receiving",
                              index=0,
                              data_provider=blockchain_provider,
                              cache_manager=cache)

SATOSHIS_PER_MBTC = 100 * 10**3
SATOSHIS_PER_BTC = 100 * 10**6

STATUS_NONE = 0
STATUS_PAYREQ = 1
STATUS_PAID = 2