added /patron_uri
authorForrest Voight <forrest@forre.st>
Sat, 7 Jan 2012 16:22:41 +0000 (11:22 -0500)
committerForrest Voight <forrest@forre.st>
Sat, 7 Jan 2012 16:22:41 +0000 (11:22 -0500)
p2pool/bitcoin/data.py
p2pool/main.py

index 57f3d0e..30a0413 100644 (file)
@@ -540,6 +540,25 @@ def pubkey_to_script2(pubkey):
 def pubkey_hash_to_script2(pubkey_hash):
     return '\x76\xa9' + ('\x14' + ShortHashType().pack(pubkey_hash)) + '\x88\xac'
 
+def script2_to_address(script2, net):
+    try:
+        pubkey = script2[1:-1]
+        script2_test = pubkey_to_script2(pubkey)
+    except:
+        pass
+    else:
+        if script2_test == script2:
+            return pubkey_to_address(pubkey, net)
+    
+    try:
+        pubkey_hash = ShortHashType().unpack(script2[3:-2])
+        script2_test2 = pubkey_hash_to_script2(pubkey_hash)
+    except:
+        pass
+    else:
+        if script2_test2 == script2:
+            return pubkey_hash_to_address(pubkey_hash, net)
+
 def script2_to_human(script2, net):
     try:
         pubkey = script2[1:-1]
index b6bab03..85afd01 100644 (file)
@@ -582,14 +582,29 @@ def main(args, net, datadir_path):
                 res[bitcoin_data.script2_to_human(script, net.PARENT)] = weights[script]/total_weight
             return json.dumps(res)
         
-        def get_current_payouts():
+        def get_current_txouts():
             wb = WorkerBridge()
             tmp_tag = str(random.randrange(2**64))
             outputs = wb.merkle_root_to_transactions[wb.get_work(tmp_tag).merkle_root][1][0]['tx_outs']
             total = sum(out['value'] for out in outputs)
             total_without_tag = sum(out['value'] for out in outputs if out['script'] != tmp_tag)
-            return json.dumps(dict((bitcoin_data.script2_to_human(out['script'], net.PARENT), out['value']*total/total_without_tag/1e8) for out in outputs if out['script'] != tmp_tag))
+            return dict((out['script'], out['value']*total/total_without_tag/1e8) for out in outputs if out['script'] != tmp_tag and out['value'])
         
+        def get_current_payouts():
+            return json.dumps(dict((bitcoin_data.script2_to_human(script, net.PARENT), value) for script, value in get_current_txouts().iteritems()))
+        
+        def get_patron_uri():
+            res = []
+            res.append("Use one of these addresses to fairly donate to everyone using P2Pool:\n")
+            txouts = get_current_txouts()
+            total = sum(txouts.itervalues())
+            normalized_txouts = dict((script, value/total) for script, value in txouts.iteritems())
+            for this in [1, 2, 5, 10, 20, 50]:
+                res.append('URI for a total of %s %s: ' % (this, net.PARENT.SYMBOL))
+                res.append('x-btc' + ''.join(':addr=%s;value=%s;send' % (bitcoin_data.script2_to_address(script, net.PARENT), this*value) for script, value in normalized_txouts.iteritems() if bitcoin_data.script2_to_address(script, net.PARENT) is not None))
+                res.append('\n')
+            return ''.join(res)
+         
         def get_global_stats():
             # averaged over last hour
             lookbehind = 3600//net.SHARE_PERIOD
@@ -661,6 +676,7 @@ def main(args, net, datadir_path):
         web_root.putChild('users', WebInterface(get_users, 'application/json'))
         web_root.putChild('fee', WebInterface(lambda: json.dumps(args.worker_fee), 'application/json'))
         web_root.putChild('current_payouts', WebInterface(get_current_payouts, 'application/json'))
+        web_root.putChild('patron_uri', WebInterface(get_patron_uri, 'text/plain'))
         web_root.putChild('global_stats', WebInterface(get_global_stats, 'application/json'))
         web_root.putChild('local_stats', WebInterface(get_local_stats, 'application/json'))
         web_root.putChild('peer_addresses', WebInterface(get_peer_addresses, 'text/plain'))