Navigation Menu

Skip to content

Instantly share code, notes, and snippets.

@DrMartiner
Last active February 3, 2018 11:18
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save DrMartiner/12f36ad411f0671a22b75da727be4d06 to your computer and use it in GitHub Desktop.
Save DrMartiner/12f36ad411f0671a22b75da727be4d06 to your computer and use it in GitHub Desktop.
Use https://github.com/richardkiss/pycoin to create a wallet & make a transaction to testnet
import os
import requests
os.environ.setdefault('PYCOIN_DEFAULT_NETCODE', 'XTN')
os.environ.setdefault('PYCOIN_XTN_PROVIDERS', 'chain.so')
from pycoin.key.electrum import ElectrumWallet
from pycoin.tx.tx_utils import (create_tx, sign_tx)
from pycoin.services import spendables_for_address
# Create a wallet
PHRASE = '12 words'
wallet = ElectrumWallet(initial_key=PHRASE) # Testnet wallet
# Sign tx
spendables = spendables_for_address(wallet.address(), netcode=wallet.netcode())
unsigned_tx = create_tx(
spendables,
[('moFgB4kYfmSVD8tX86fjcKe76oqqkav52A')], # Address from testnet
fee=1000
)
sign_tx(
unsigned_tx,
wifs=[wallet.wif(use_uncompressed=True)],
netcode=wallet.netcode()
)
# Push tx
tx_hex = unsigned_tx.as_hex(include_witness_data=False)
res = requests.post('https://chain.so/api/v2/send_tx/BTCTEST', data={
'network': 'BTCTEST',
'tx_hex': tx_hex,
})
print('txid is {}'.format(res.json()['data']))
# Print balance
address = wallet.address()
res = requests.get('https://chain.so/api/v2/get_address_balance/BTCTEST/{}'.format(address))
print('Balance is {}'.format(res.json()['data']))
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment