Last active
February 3, 2018 11:18
-
-
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
This file contains 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
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