|
4 | 4 | # file COPYING or http://www.opensource.org/licenses/mit-license.php.
|
5 | 5 |
|
6 | 6 | import argparse
|
| 7 | +import io |
| 8 | +import requests |
7 | 9 | import subprocess
|
8 | 10 | import sys
|
9 |
| -import requests |
10 | 11 |
|
11 | 12 | DEFAULT_GLOBAL_FAUCET = 'https://signetfaucet.com/claim'
|
| 13 | +DEFAULT_GLOBAL_CAPTCHA = 'https://signetfaucet.com/captcha' |
12 | 14 | GLOBAL_FIRST_BLOCK_HASH = '00000086d6b2636cb2a392d45edc4ec544a10024d30141c9adf4bfd9de533b53'
|
13 | 15 |
|
| 16 | +# braille unicode block |
| 17 | +BASE = 0x2800 |
| 18 | +BIT_PER_PIXEL = [ |
| 19 | + [0x01, 0x08], |
| 20 | + [0x02, 0x10], |
| 21 | + [0x04, 0x20], |
| 22 | + [0x40, 0x80], |
| 23 | +] |
| 24 | +BW = 2 |
| 25 | +BH = 4 |
| 26 | + |
| 27 | +# imagemagick or compatible fork (used for converting SVG) |
| 28 | +CONVERT = 'convert' |
| 29 | + |
| 30 | +class PPMImage: |
| 31 | + ''' |
| 32 | + Load a PPM image (Pillow-ish API). |
| 33 | + ''' |
| 34 | + def __init__(self, f): |
| 35 | + if f.readline() != b'P6\n': |
| 36 | + raise ValueError('Invalid ppm format: header') |
| 37 | + line = f.readline() |
| 38 | + (width, height) = (int(x) for x in line.rstrip().split(b' ')) |
| 39 | + if f.readline() != b'255\n': |
| 40 | + raise ValueError('Invalid ppm format: color depth') |
| 41 | + data = f.read(width * height * 3) |
| 42 | + stride = width * 3 |
| 43 | + self.size = (width, height) |
| 44 | + self._grid = [[tuple(data[stride * y + 3 * x:stride * y + 3 * (x + 1)]) for x in range(width)] for y in range(height)] |
| 45 | + |
| 46 | + def getpixel(self, pos): |
| 47 | + return self._grid[pos[1]][pos[0]] |
| 48 | + |
| 49 | +def print_image(img, threshold=128): |
| 50 | + '''Print black-and-white image to terminal in braille unicode characters.''' |
| 51 | + x_blocks = (img.size[0] + BW - 1) // BW |
| 52 | + y_blocks = (img.size[1] + BH - 1) // BH |
| 53 | + |
| 54 | + for yb in range(y_blocks): |
| 55 | + line = [] |
| 56 | + for xb in range(x_blocks): |
| 57 | + ch = BASE |
| 58 | + for y in range(BH): |
| 59 | + for x in range(BW): |
| 60 | + try: |
| 61 | + val = img.getpixel((xb * BW + x, yb * BH + y)) |
| 62 | + except IndexError: |
| 63 | + pass |
| 64 | + else: |
| 65 | + if val[0] < threshold: |
| 66 | + ch |= BIT_PER_PIXEL[y][x] |
| 67 | + line.append(chr(ch)) |
| 68 | + print(''.join(line)) |
| 69 | + |
14 | 70 | parser = argparse.ArgumentParser(description='Script to get coins from a faucet.', epilog='You may need to start with double-dash (--) when providing bitcoin-cli arguments.')
|
15 | 71 | parser.add_argument('-c', '--cmd', dest='cmd', default='bitcoin-cli', help='bitcoin-cli command to use')
|
16 | 72 | parser.add_argument('-f', '--faucet', dest='faucet', default=DEFAULT_GLOBAL_FAUCET, help='URL of the faucet')
|
| 73 | +parser.add_argument('-g', '--captcha', dest='captcha', default=DEFAULT_GLOBAL_CAPTCHA, help='URL of the faucet captcha, or empty if no captcha is needed') |
17 | 74 | parser.add_argument('-a', '--addr', dest='addr', default='', help='Bitcoin address to which the faucet should send')
|
18 | 75 | parser.add_argument('-p', '--password', dest='password', default='', help='Faucet password, if any')
|
| 76 | +parser.add_argument('-n', '--amount', dest='amount', default='0.001', help='Amount to request (0.001-0.1, default is 0.001)') |
| 77 | +parser.add_argument('-i', '--imagemagick', dest='imagemagick', default=CONVERT, help='Path to imagemagick convert utility') |
19 | 78 | parser.add_argument('bitcoin_cli_args', nargs='*', help='Arguments to pass on to bitcoin-cli (default: -signet)')
|
20 | 79 |
|
21 | 80 | args = parser.parse_args()
|
@@ -43,14 +102,43 @@ def bitcoin_cli(rpc_command_and_params):
|
43 | 102 | if curr_signet_hash != GLOBAL_FIRST_BLOCK_HASH:
|
44 | 103 | print('The global faucet cannot be used with a custom Signet network. Please use the global signet or setup your custom faucet to use this functionality.\n')
|
45 | 104 | exit(1)
|
| 105 | +else: |
| 106 | + # For custom faucets, don't request captcha by default. |
| 107 | + if args.captcha == DEFAULT_GLOBAL_CAPTCHA: |
| 108 | + args.captcha = '' |
46 | 109 |
|
47 | 110 | if args.addr == '':
|
48 | 111 | # get address for receiving coins
|
49 | 112 | args.addr = bitcoin_cli(['getnewaddress', 'faucet', 'bech32'])
|
50 | 113 |
|
51 |
| -data = {'address': args.addr, 'password': args.password} |
| 114 | +data = {'address': args.addr, 'password': args.password, 'amount': args.amount} |
| 115 | + |
| 116 | +# Store cookies |
| 117 | +# for debugging: print(session.cookies.get_dict()) |
| 118 | +session = requests.Session() |
| 119 | + |
| 120 | +if args.captcha != '': # Retrieve a captcha |
| 121 | + try: |
| 122 | + res = session.get(args.captcha) |
| 123 | + except: |
| 124 | + print('Unexpected error when contacting faucet:', sys.exc_info()[0]) |
| 125 | + exit(1) |
| 126 | + |
| 127 | + # Convert SVG image to PPM, and load it |
| 128 | + try: |
| 129 | + rv = subprocess.run([args.imagemagick, '-', '-depth', '8', 'ppm:-'], input=res.content, check=True, capture_output=True) |
| 130 | + except FileNotFoundError: |
| 131 | + print('The binary', args.imagemagick, 'could not be found. Please make sure ImageMagick (or a compatible fork) is installed and that the correct path is specified.') |
| 132 | + exit(1) |
| 133 | + img = PPMImage(io.BytesIO(rv.stdout)) |
| 134 | + |
| 135 | + # Terminal interaction |
| 136 | + print_image(img) |
| 137 | + print('Enter captcha: ', end='') |
| 138 | + data['captcha'] = input() |
| 139 | + |
52 | 140 | try:
|
53 |
| - res = requests.post(args.faucet, data=data) |
| 141 | + res = session.post(args.faucet, data=data) |
54 | 142 | except:
|
55 | 143 | print('Unexpected error when contacting faucet:', sys.exc_info()[0])
|
56 | 144 | exit(1)
|
|
0 commit comments