Created
August 15, 2015 04:01
-
-
Save XertroV/17bad1e1cec089610b3b to your computer and use it in GitHub Desktop.
pycoin demonstration: send all the coins at an address
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#!/usr/bin/env python3 | |
import os | |
import argparse | |
from binascii import hexlify, unhexlify | |
import sys | |
from urllib.error import HTTPError | |
from pycoin.key import Key | |
from pycoin.tx.tx_utils import create_tx, sign_tx | |
from pycoin.services.blockchain_info import spendables_for_address, send_tx | |
parser = argparse.ArgumentParser() | |
parser.add_argument('--privkey-bytes', help='provide hexlified raw privkey bytes', default='', type=str) | |
parser.add_argument('--send-all-to', help='where to send all the money at this address', default='1MaxKayeQg4YhFkzFz4x6NDeeNv1bwKKVA', type=str) | |
args = parser.parse_args() | |
key_bytes = unhexlify(args.privkey_bytes.encode()) if args.privkey_bytes != '' else os.urandom(32) | |
private_key = Key(secret_exponent=int.from_bytes(key_bytes, 'big')) | |
address = private_key.address() | |
print('Your Bitcoin address is...', address) | |
print('Your --privkey-bytes', hexlify(key_bytes).decode()) | |
try: | |
spendables = spendables_for_address(address) | |
print('Spending', spendables) | |
except HTTPError as e: | |
print('Blockchain throws a 500 error if there are no spendables. Try sending some coins to', address, 'and try again. Remeber to copy privkey-bytes.') | |
sys.exit() | |
tx = create_tx(spendables, [args.send_all_to]) | |
print('TX created:', repr(tx)) | |
sign_tx(tx, [private_key.wif(False), private_key.wif(True)]) | |
print('Final TX:', tx) | |
print('TX Send Attempt:', send_tx(tx)) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
heh. Nice default destination address there.