/
nickel.py
90 lines (74 loc) · 3.01 KB
/
nickel.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
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
import argparse
import json
import sys
import re
from binascii import hexlify, unhexlify
from NemConnect import NemConnect
from Account import Account
class PrivateKeyAction(argparse.Action):
def __init__(self, option_strings, dest, nargs=None, **kwargs):
if nargs is not None:
raise ValueError("nargs not allowed")
super(PrivateKeyAction, self).__init__(option_strings, dest, **kwargs)
def __call__(self, parser, namespace, values, option_string=None):
if (values is None) or (len(values) != 64) or not re.match(r'[0-9A-Fa-f]{64}', values):
raise argparse.ArgumentError(self, "privkey must be 64-bytes hex string")
setattr(namespace, self.dest, values)
parser = argparse.ArgumentParser(description='Nickel tool. To get info on subcommands try: nickel.py send --help')
sub = parser.add_subparsers(help='subcommands', dest='sub')
sub.add_parser('info', help='displys node info')
harvestParser = sub.add_parser('harvest', help='start/stop harvesting')
g = harvestParser.add_mutually_exclusive_group()
g.add_argument('--unlock', metavar='PRIVKEY', action=PrivateKeyAction, help='starts harvesting using given private key')
g.add_argument('--lock', metavar='PRIVKEY', action=PrivateKeyAction, help='STOP harvesting using given private key')
sendParser = sub.add_parser('send', help="send nem")
sendParser.add_argument("key", metavar='PRIVKEY', action=PrivateKeyAction, help="sender's private key")
sendParser.add_argument("to", metavar='ADDRESS', help="recipient's address (i.e. TDGIMREMR5NSRFUOMPI5OOHLDATCABNPC5ID2SVA)")
sendParser.add_argument("amount", metavar="AMOUNT", type=int, help="amount in microNEMs")
#sendParser.add_argument("fee", metavar="FEE", type=int, help="fee in microNEMs")
#harvestingParser = sub.add_parser('harvest', help="remote harvesting")
args = parser.parse_args()
print args
def prettyPrint(j):
print json.dumps(j, indent=2)
c = NemConnect('127.0.0.1', 7890)
if args.sub == 'info':
j = c.nodeInfo()
print " [+] NODE INFO:"
prettyPrint(j)
elif args.sub == 'harvest':
msg = "UNLOCK" if args.unlock else "LOCK"
privkey = args.unlock or args.lock
a = Account(privkey)
print " [+] TRYING TO %s USING ACCOUNT" % msg
print "private: %s" % a.getHexPrivateKey()
print " public: %s" % a.getHexPublicKey()
if args.unlock:
ok,j = c.accountUnlock(privkey)
else:
ok,j = c.accountLock(privkey)
if ok:
print " [+] SUCCESSFULLY %sED" % msg
else:
prettyPrint(j)
elif args.sub == 'send':
privkey = args.key
recipient = args.to
amount = args.amount
message = 'nickel test'
a = Account(privkey)
print " [+] PREPARING TRANSACTION"
ok, j = c.transferPrepare(a.getHexPublicKey(), recipient, amount, message)
if ok and ('data' in j):
print " [+] TRYING TO SIGN PREPARED DATA"
data = unhexlify(j['data'])
sig = a.sign(data)
ok, j = c.transferAnnounce(j['data'], hexlify(sig))
if ok:
print " [+] ANNOUNCED"
else:
print " [!] announce failed"
prettyPrint(j)
else:
print " [!] prepare failed: "
prettyPrint(j)