Skip to content

Instantly share code, notes, and snippets.

@XertroV
Created August 15, 2015 04:01
Show Gist options
  • Star 1 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save XertroV/17bad1e1cec089610b3b to your computer and use it in GitHub Desktop.
Save XertroV/17bad1e1cec089610b3b to your computer and use it in GitHub Desktop.
pycoin demonstration: send all the coins at an address
#!/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))
@knarf180
Copy link

heh. Nice default destination address there.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment