-
Notifications
You must be signed in to change notification settings - Fork 7
/
txutil.py
49 lines (40 loc) · 1.59 KB
/
txutil.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
import transactions
import logging
import msgs
import json
from utils import h2h, hash2addr
def load_tx(filename):
txdata = "".join(open(filename,"r").read().split("\n")).decode("hex")
return msgs.Tx.frombinary(txdata)[0]
def save_tx(filename, tx):
bintx = tx.tobinary().encode("hex")
open(filename,"w").write("\n".join([bintx[i:i+80] for i in range(0, len(bintx), 80)]))
def getaddress(script):
sc = script.decode("hex")
if sc[:2] == "\x76\xa9" and sc[-2:] == "\x88\xac":
return hash2addr(sc[3:-2])
if __name__ == "__main__":
import sys
import time
logging.basicConfig(format='%(name)s - %(message)s', level=logging.DEBUG)
cmd = sys.argv[1]
if cmd == "exporttx":
txhash = sys.argv[2].decode("hex")[::-1]
tx = transactions.Tx.get_by_hash(txhash)
save_tx(sys.argv[3], tx.tx)
if cmd == "importtx":
tx = load_tx(sys.argv[2])
print transactions.Tx.get_or_make(tx).hexhash
if cmd == "infotx":
txhash = sys.argv[2].decode("hex")[::-1]
tx = transactions.Tx.get_by_hash(txhash)
print json.dumps(tx.tojson(), sort_keys=True, indent=2)
if cmd == "getoutputs":
txhash = sys.argv[2].decode("hex")[::-1]
tx = transactions.Tx.get_by_hash(txhash)
outputs = []
for output, spend in zip(tx.tx.tojson()["outputs"], tx.redeemed):
output["address"] = getaddress(output["script"])
output["spend"] = h2h(spend)
outputs.append(output)
print json.dumps(outputs, sort_keys=True, indent=2)