def submit_blogpost(request): """ Post a non-encrypted blog post that anyone can read via PeerBlogs, cost 0.01 ppc """ from_address = request.POST.get('from_address') message = request.POST.get('message') rpc_raw = rpcRawProxy(helpers.get_rpc_url()) if request.POST.get('wallet_passphrase', False): rpc_raw.walletpassphrase(request.POST.get('wallet_passphrase'), 60) try: message += "|" + helpers.sign_string(rpc_raw, message, from_address) except JSONRPCException, e: if "passphrase" in e.error['message']: return HttpResponse(json.dumps( { "status": "error", "message": "Wallet locked.", "type": "wallet_locked" }, default=helpers.json_custom_parser), content_type='application/json') else: return HttpResponse(json.dumps( { "status": "error", "message": "Error while trying to sign public key." }, default=helpers.json_custom_parser), content_type='application/json')
def publish_pk(request): """ Publish your public key on the blockchain, cost 0.01 ppc. """ address = request.POST['address'] try: pub_key = helpers.get_pk(address) except ValueError: return HttpResponse(json.dumps({ "status": "error", "message": "Must create GPG keys before publishing them!" }, default=helpers.json_custom_parser), content_type='application/json') rpc_raw = rpcRawProxy(helpers.get_rpc_url()) if request.POST.get('wallet_passphrase', False): rpc_raw.walletpassphrase(request.POST['wallet_passphrase'], 60) try: signed_pub_key = pub_key + "|" + helpers.sign_string(rpc_raw, pub_key, address) except JSONRPCException, e: if "passphrase" in e.error['message']: return HttpResponse(json.dumps({ "status": "error", "message": "Wallet locked.", "type": "wallet_locked" }, default=helpers.json_custom_parser), content_type='application/json') else: return HttpResponse(json.dumps({ "status": "error", "message": "Error while trying to sign public key." }, default=helpers.json_custom_parser), content_type='application/json')
def peercoin_minting_data(request): rpc_raw = rpcRawProxy(helpers.get_rpc_url()) difficulty = rpc_raw.getdifficulty() minting_data = [] for a in rpc_raw.listunspent(0): tx_info = rpc_raw.decoderawtransaction(rpc_raw.getrawtransaction(a['txid'])) age = int(time.time()) - tx_info['time'] age = int(((age / 60) / 60) / 24) #convert to days if age < 30: coindays = 0 elif age < 120: coindays = int((age-30) * a['amount']) else: coindays = int(90 * a['amount']) minting_data.append([ a['txid'], a['address'], age, str(a['amount']), coindays, "" ]) return HttpResponse(json.dumps({ "status": "success", "difficulty": str(difficulty['proof-of-stake']), "data": minting_data }, default=helpers.json_custom_parser), content_type='application/json')
def download_payloads_thread(): """ For any keys we've picked up in the blockchain, download the corresponding payloads. """ rpc_raw = rpcRawProxy(helpers.get_rpc_url()) while True: helpers.download_payloads(rpc_raw) time.sleep(2)
def blockchain_scan_status(request): rpc_raw = rpcRawProxy(helpers.get_rpc_url()) latest_block, blocks_left = blockchain_func.get_blockchain_scan_status(rpc_raw) return HttpResponse(json.dumps({ "status":"success", "latest_block": latest_block, "blocks_left": blocks_left }, default=helpers.json_custom_parser), content_type='application/json')
def get_addresses(request): """ Get all your addresses from your wallet. """ rpc_raw = rpcRawProxy(helpers.get_rpc_url()) addresses = rpc_raw.listunspent(0) return HttpResponse(json.dumps({ "status": "success", "data":addresses }, default=helpers.json_custom_parser), content_type='application/json')
def transmit_message(request): """ Send an encrypted message via PeerMessage, cost 0.01 ppc """ from_address = request.POST['from_address'] to_address = request.POST['to_address'] message = request.POST['message'] rpc_raw = rpcRawProxy(helpers.get_rpc_url()) #get to_address's GPG pub key if not os.path.isdir(peerapps.settings.BASE_DIR + "/public_keys/gpg_" + to_address): return HttpResponse(json.dumps( { "status": "error", "message": "No public key found for that address." }, default=helpers.json_custom_parser), content_type='application/json') #encrypt message to to_address try: enc_message = helpers.encrypt_string(message, to_address) except: return HttpResponse(json.dumps( { "status": "error", "message": "No public key found for that address." }, default=helpers.json_custom_parser), content_type='application/json') if request.POST.get('wallet_passphrase', False): rpc_raw.walletpassphrase(request.POST['wallet_passphrase'], 60) try: enc_message += "|" + helpers.sign_string(rpc_raw, enc_message, from_address) except JSONRPCException, e: if "passphrase" in e.error['message']: return HttpResponse(json.dumps( { "status": "error", "message": "Wallet locked.", "type": "wallet_locked" }, default=helpers.json_custom_parser), content_type='application/json') else: return HttpResponse(json.dumps( { "status": "error", "message": "Error while trying to sign public key." }, default=helpers.json_custom_parser), content_type='application/json')
def submit_api_call(from_address, payload): rpc_raw = rpcRawProxy(helpers.get_rpc_url()) #rpc_raw.walletpassphrase(request.POST['wallet_passphrase'], 60) try: payload += "|" + helpers.sign_string(rpc_raw, payload, from_address) except JSONRPCException, e: if "passphrase" in e.error['message']: raise PeercoinError("Wallet locked.") else: raise PeercoinError("Error while trying to sign with peercoin public key.")
def scan_blogs(request): """ For our own blog and the blogs we are subscribing to, for any new blog posts we only have meta data for, download the blog post. """ rpc_raw = rpcRawProxy(helpers.get_rpc_url()) helpers.download_blgs(rpc_raw) return HttpResponse(json.dumps({"status": "success"}, default=helpers.json_custom_parser), content_type='application/json')
def scan_blockchain(): """ Scan one block in the blockchain (and mempool as well) """ rpc_raw = rpcRawProxy(helpers.get_rpc_url()) while True: try: latest_block, blocks_left = blockchain_func.scan_block(rpc_raw) app.wxPeerApps.statusConnected() if latest_block: print "On the latest block, sleeping for 10 seconds" time.sleep(10) except: app.wxPeerApps.statusDisconnected() time.sleep(2)
def view_latest_post(request): """ Get the latest blog post from external storage for a given address. """ address = request.POST.get('address') rpc_raw = rpcRawProxy(helpers.get_rpc_url()) latest_blog_post = Blog.objects.filter( address_from=address).order_by('-time')[0] blog_post = helpers.download_blg(rpc_raw, latest_blog_post.key, latest_blog_post.address_from) return HttpResponse(json.dumps({ "status": "success", "data": blog_post }, default=helpers.json_custom_parser), content_type='application/json')
def transmit_message(request): """ Send an encrypted message via PeerMessage, cost 0.01 ppc """ from_address = request.POST['from_address'] to_address = request.POST['to_address'] message = request.POST['message'] rpc_raw = rpcRawProxy(helpers.get_rpc_url()) #get to_address's GPG pub key if not os.path.isdir(peerapps.settings.BASE_DIR+"/public_keys/gpg_"+to_address): return HttpResponse(json.dumps({ "status": "error", "message": "No public key found for that address." }, default=helpers.json_custom_parser), content_type='application/json') #encrypt message to to_address try: enc_message = helpers.encrypt_string(message, to_address) except: return HttpResponse(json.dumps({ "status": "error", "message": "No public key found for that address." }, default=helpers.json_custom_parser), content_type='application/json') if request.POST.get('wallet_passphrase', False): rpc_raw.walletpassphrase(request.POST['wallet_passphrase'], 60) try: enc_message += "|" + helpers.sign_string(rpc_raw, enc_message, from_address) except JSONRPCException, e: if "passphrase" in e.error['message']: return HttpResponse(json.dumps({ "status": "error", "message": "Wallet locked.", "type": "wallet_locked" }, default=helpers.json_custom_parser), content_type='application/json') else: return HttpResponse(json.dumps({ "status": "error", "message": "Error while trying to sign public key." }, default=helpers.json_custom_parser), content_type='application/json')
def publish_pk(request): """ Publish your public key on the blockchain, cost 0.01 ppc. """ address = request.POST['address'] try: pub_key = helpers.get_pk(address) except ValueError: return HttpResponse(json.dumps( { "status": "error", "message": "Must create GPG keys before publishing them!" }, default=helpers.json_custom_parser), content_type='application/json') rpc_raw = rpcRawProxy(helpers.get_rpc_url()) if request.POST.get('wallet_passphrase', False): rpc_raw.walletpassphrase(request.POST['wallet_passphrase'], 60) try: signed_pub_key = pub_key + "|" + helpers.sign_string( rpc_raw, pub_key, address) except JSONRPCException, e: if "passphrase" in e.error['message']: return HttpResponse(json.dumps( { "status": "error", "message": "Wallet locked.", "type": "wallet_locked" }, default=helpers.json_custom_parser), content_type='application/json') else: return HttpResponse(json.dumps( { "status": "error", "message": "Error while trying to sign public key." }, default=helpers.json_custom_parser), content_type='application/json')
default=helpers.json_custom_parser), content_type='application/json') #get from_address pub_key try: pub_key = helpers.get_pk(from_address) except ValueError: return HttpResponse(json.dumps( { "status": "error", "message": "Must create GPG keys before sending messages!" }, default=helpers.json_custom_parser), content_type='application/json') rpc_raw = rpcRawProxy(helpers.get_rpc_url()) if request.POST.get('wallet_passphrase', False): rpc_raw.walletpassphrase(request.POST['wallet_passphrase'], 60) try: signed_pub_key = pub_key + "|" + helpers.sign_string( rpc_raw, pub_key, from_address) except JSONRPCException, e: return HttpResponse(json.dumps( { "status": "error", "message": "Error while trying to sign public key." }, default=helpers.json_custom_parser), content_type='application/json')
import sys import os sys.path.append('../../') os.environ['DJANGO_SETTINGS_MODULE'] = 'peerapps.settings' import django django.setup() from peermarket.errors import PeercoinError, PeerMarketError from bitcoinrpc.authproxy import JSONRPCException, AuthServiceProxy as rpcRawProxy from bitcoin.rpc import Proxy as rpcProcessedProxy import external_db import helpers, blockchain_func #from django.test import TestCase import json rpc_raw = rpcRawProxy(helpers.get_rpc_url()) blockchain_func.scan_block(rpc_raw, scan_mempool_only=True) helpers.download_payloads(rpc_raw)