DrMartiner
2/3/2018 - 11:15 AM

Use https://github.com/richardkiss/pycoin to create a wallet & make a transaction to testnet

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']))