minimal functions
This commit is contained in:
commit
d2bbf926bd
|
@ -0,0 +1,3 @@
|
|||
Python API for ZCash
|
||||
|
||||
Forked from 5a1t, last updated for v1.0.8 rpc interface. Incomplete
|
Binary file not shown.
|
@ -0,0 +1,64 @@
|
|||
import os.path
|
||||
import sys
|
||||
import time
|
||||
|
||||
from pyzcash.rpc.ZDaemon import *
|
||||
from pyzcash.settings import *
|
||||
|
||||
|
||||
#Sweeps all unspent transparent txs, cleaning them through a temporary zaddr.
|
||||
def clean_and_collect_all(taddress=TEST_TADDR, fee=DEFAULT_FEE):
|
||||
zd = ZDaemon()
|
||||
|
||||
print "Checking balance..."
|
||||
tx_unspents = zd.getUnspentTxs()
|
||||
if not len(tx_unspents):
|
||||
print "No spendable txs available - visit a faucet or mine!"
|
||||
exit()
|
||||
|
||||
print "Generating temporary zaddress for tx..."
|
||||
zaddress_full = zd.getNewRawZAddress()
|
||||
zaddress = zaddress_full.get('zcaddress')
|
||||
zsecret = zaddress_full.get('zcsecretkey')
|
||||
|
||||
print "Generated: " + zaddress
|
||||
print "Secret: " + zsecret
|
||||
|
||||
print "Gathering and transmitting unspent txs..."
|
||||
print "Please wait..."
|
||||
notes = zd.pourAllUnspentTxs(zaddress)
|
||||
encnote1 = notes.get('encryptednote1')
|
||||
|
||||
|
||||
print "Found a note to use: \n--------------------------------------------\n" + encnote1 + "\n--------------------------------------------"
|
||||
|
||||
print "Waiting for note to show in blockchain before spendable..."
|
||||
print "This may take a few minutes..."
|
||||
while zd.receiveTx(zsecret, encnote1).get('exists') is not True:
|
||||
print zd.receiveTx(zsecret,encnote1)
|
||||
time.sleep(5)
|
||||
|
||||
print "Found note in blockchain!"
|
||||
total = zd.receiveTx(zsecret, encnote1).get('amount')
|
||||
print "Examined note and found total: " + str(total)
|
||||
|
||||
print "Spending note to target transparent address..."
|
||||
tx_response = zd.sendNoteToAddress(encnote1, zsecret, taddress, total-fee, zaddress)
|
||||
|
||||
print "Sent! Check " + taddress + " shortly."
|
||||
|
||||
|
||||
if __name__ == "__main__":
|
||||
if len(sys.argv) <= 1:
|
||||
print "Usage: python sweep_all.py <transparent address>"
|
||||
print "Ex: python sweep_all.py mfu8LbjAq15zmCDLCwUuay9cVc2FcGuY4d"
|
||||
exit()
|
||||
|
||||
taddr = sys.argv[1]
|
||||
if len(taddr) != len('mfu8LbjAq15zmCDLCwUuay9cVc2FcGuY4d'):
|
||||
print "That doesn't look like a transparent address.. Maybe you are trying to use a zaddress?"
|
||||
exit()
|
||||
|
||||
print "Address looks good!"
|
||||
clean_and_collect_all(taddr)
|
||||
|
|
@ -0,0 +1,39 @@
|
|||
import os.path
|
||||
import sys
|
||||
|
||||
from pyzcash.rpc.ZDaemon import *
|
||||
from pyzcash.settings import *
|
||||
|
||||
|
||||
def test_daemon():
|
||||
zd = ZDaemon()
|
||||
|
||||
print zd.getBlockHash(100)
|
||||
print zd.getBlockByHash(zd.getBlockHash(100))
|
||||
print zd.getBlockByHeight(100)
|
||||
print zd.getNetworkHeight()
|
||||
print zd.getNetworkDifficulty()
|
||||
print zd.getTotalBalance()
|
||||
print zd.getConnectionCount()
|
||||
print zd.getNewAddress()
|
||||
print zd.getNewZAddress()
|
||||
|
||||
print zd.getUnspentTxs()
|
||||
tx_amount = zd.getTxInfo(TEST_TXID).get('details')[0].get('amount')
|
||||
tx = zd.createNewRawTxFromTxid(TEST_TXID)
|
||||
pourtx = zd.pourRawTx(tx, TEST_ZADDR, tx_amount)
|
||||
hextx = zd.signRawTx(pourtx.get('rawtxn'))
|
||||
|
||||
print zd.sendRawTx(hextx.get('hex'))
|
||||
|
||||
print pourtx
|
||||
print hextx
|
||||
|
||||
# print zd.sendNoteToAddress(encnote, TEST_ZSECRET, TEST_TADDR, 0.33, TEST_ZADDR)
|
||||
|
||||
# print zd.sendNoteToAddress(encnote, TEST_ZSECRET, faucet_addr, 0.33, TEST_ZADDR)
|
||||
|
||||
|
||||
if __name__ == "__main__":
|
||||
test_daemon()
|
||||
|
|
@ -0,0 +1,93 @@
|
|||
import requests
|
||||
import json
|
||||
|
||||
from pyZcash.settings import *
|
||||
|
||||
class ZDaemon(object):
|
||||
|
||||
id_count = 0
|
||||
|
||||
def __init__(self, url=ZURL, user=RPCUSER, password=RPCPASSWORD, timeout=TIMEOUT):
|
||||
#TODO: check utf safety
|
||||
self.url = url
|
||||
self.user = user.encode('utf8')
|
||||
self.password = password.encode('utf8')
|
||||
self.timeout = timeout
|
||||
|
||||
def _call(self, method, *args):
|
||||
jsondata = json.dumps({ 'version': '2',
|
||||
'method': method,
|
||||
'params': args,
|
||||
'id': self.id_count})
|
||||
|
||||
r = requests.post(self.url, auth=(self.user,self.password), data=jsondata, timeout=self.timeout)
|
||||
|
||||
self.id_count += 1
|
||||
resp = json.loads(r.text)
|
||||
|
||||
#TODO: deal with errors better.
|
||||
error = resp['error']
|
||||
if error:
|
||||
print error
|
||||
|
||||
return resp['result']
|
||||
|
||||
#Block Info
|
||||
def getBlockHash(self, blockheight):
|
||||
return self._call('getblockhash', blockheight)
|
||||
|
||||
def getBlockByHash(self, blockhash):
|
||||
return self._call('getblock', blockhash)
|
||||
|
||||
def getBlockByHeight(self, blockheight):
|
||||
return self.getBlockByHash(self.getBlockHash(blockheight))
|
||||
|
||||
#Network Info
|
||||
def getNetworkHeight(self):
|
||||
return self._call('getblockcount')
|
||||
|
||||
def getNetworkDifficulty(self):
|
||||
return self._call('getdifficulty')
|
||||
|
||||
def getVersion(self):
|
||||
info = self._call('getnetworkinfo')
|
||||
client = info['subversion']
|
||||
version = client.strip('/').split(':')[1]
|
||||
return version
|
||||
|
||||
def getConnectionCount(self):
|
||||
return self._call('getconnectioncount')
|
||||
|
||||
#Wallet Info (transparent)
|
||||
def getTotalBalance(self, account=""):
|
||||
if account:
|
||||
return self._call('getbalance', account)
|
||||
else:
|
||||
return self._call('getbalance')
|
||||
|
||||
def getUnspentTxs(self, minconf=1):
|
||||
return self._call('listunspent', minconf)
|
||||
|
||||
#Raw Txs
|
||||
def getTxInfo(self, txid):
|
||||
return self._call('gettransaction', txid)
|
||||
|
||||
# taddr methods
|
||||
def getNewAddress(self, account=""):
|
||||
if account:
|
||||
return self._call('getnewaddress', account)
|
||||
else:
|
||||
return self._call('getnewaddress')
|
||||
|
||||
def sendTransparent(self, taddress, amount):
|
||||
return self._call('sendtoaddress', taddress, amount)
|
||||
|
||||
# zaddr methods
|
||||
def z_getnewaddress(self):
|
||||
return self._call('z_getnewaddress')
|
||||
|
||||
def z_listaddresses(self):
|
||||
return self._call('z_listaddresses')
|
||||
|
||||
def z_listreceivedbyaddress(self, zaddr):
|
||||
return self._call('z_listreceivedbyaddress', zaddr)
|
Binary file not shown.
Binary file not shown.
|
@ -0,0 +1,31 @@
|
|||
import os, re
|
||||
#ZCASH RPC
|
||||
# Address and port of local zcashd testnet rpc
|
||||
ZURL = "http://localhost:18232"
|
||||
#Timeout needs to be high for any pour operations
|
||||
TIMEOUT = 600
|
||||
#Default fee to use on network for txs.
|
||||
DEFAULT_FEE = 0.01
|
||||
|
||||
zcashconf = os.path.expanduser('~/.zcash/zcash.conf')
|
||||
|
||||
def read_config(filename):
|
||||
f = open(filename)
|
||||
for line in f:
|
||||
if re.match('rpcuser', line):
|
||||
user = line.strip('\n').split('=')[1]
|
||||
if re.match('rpcpassword', line):
|
||||
password = line.strip('\n').split('=')[1]
|
||||
return (user, password)
|
||||
|
||||
config = read_config(zcashconf)
|
||||
# from zcash conf
|
||||
RPCUSER = config[0]
|
||||
RPCPASSWORD = config[1]
|
||||
|
||||
#TESTS
|
||||
#for tests (sample data here - replace with your own)
|
||||
TEST_TXID = ''
|
||||
TEST_ZADDR = ""
|
||||
TEST_TADDR = ""
|
||||
TEST_ZSECRET = ""
|
Binary file not shown.
Loading…
Reference in New Issue