/
vtest2.py
264 lines (201 loc) · 5.33 KB
/
vtest2.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
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
import requests, json, pprint
import sys, decimal
"""
Stuff I made for myself before joining the goobit team
mostly contains old code that was later refined in valurna.py
However also contains a Voter class with methods for casting
non-multisig votes for demo & testing purposes
"""
import connect, voteCli
DEBUG=False
CHARCOST_INVERSE=10.0**5
CHARCOST= (1/CHARCOST_INVERSE)
MINING_FEE=(1.0/10**3)
testTargetAddr='n2r9JaHfra9yVT1ttX2WVRcdQFLjpDSRsP'
garbageAddr=testTargetAddr;
def printTx(txid):
donothing=True
def fundVoter(voter):
c=connect.sendCommand("walletpassphrase", [connect.phrase, 1])
#print c.json()
d=connect.sendCommand("sendtoaddress", [voter.addr, 0.1])
print d.json()
def loadCharAddrs():
addrs=[]
f=open('charAddrs.txt')
for line in(f.readlines()):
addrs.append(line.rstrip())
return addrs
charAddrs=loadCharAddrs()
def printreadable(hexed):
pprint.pprint(connect.sendCommand('decoderawtransaction', [hexed]).json())
def sentCharDebug():
res=connect.sendCommand('listreceivedbyaddress', [])
#pprint.pprint(res.json())
#print 'first 5 char addrs are:'
#for i in range(0, 5):
#print charAddrs[i]
print '\n addrs in lis received:'
for r in res.json()['result']:
#print r['address']
for q in charAddrs[0:10]:
ra=r['address'].rstrip()
q=q.rstrip()
print 'now comparing:'
print ra
#print len(r['address'])
print q
#print len(q)
if ra==q:
print ' match!'
else:
print 'no match'
def isCharAddr(addr):
for c in charAddrs:
c2=c.rstrip()
addr2=addr.rstrip()
if c2==addr2:
return True
return False
#returns list of transactions sent to char addrs
def sentCharTxs():
charTxs=[]
res=connect.sendCommand('listtransactions', ['', 10000])
for r in res.json()['result']:
if isCharAddr(r['address']):
#pprint.pprint(r['txid'])
charTxs.append(r['txid'])
charTxs=set(charTxs)
return charTxs
#takes dictionary of outputs indexed by address
#returns message composed of char values in
#addresses in order of charAddrs
def getCharStr(addrDict):
keys=addrDict.keys()
msg=''
for addr in charAddrs:
if addr in keys:
amount=addrDict[addr]['amount']
charval=int(round((amount*CHARCOST_INVERSE)))
if DEBUG:
l=amount*CHARCOST_INVERSE
print amount
print charval
print l
print int(round(l))
print ''
msg+=chr(charval)
else:
return msg
def decodeCharTransactions():
charTxs=sentCharTxs()
for txid in charTxs:
if validVote(txid):
print('displaying vote with txid '+str(txid)+":")
transaction=connect.sendCommand('gettransaction', [txid]).json()
details=transaction['result']['details']
addrSorted={}
for x in details:
if x['category']=='receive':
a=x['address']
addrSorted[a]=x
msg=getCharStr(addrSorted)
print(msg)
#should check if a txid belongs to a list of valid votes
#i.e is sent from an appropriate address
#currently simply returns True
def validVote(txid):
return True
class Voter():
name='John Q Public'
addr=''
phrase=''
def listUnspent(this):
returns=[]
res=connect.sendCommand('listunspent', [])
for r in res.json()['result']:
#print r
if r['address']==this.addr:
returns.append(r)
return returns
def printUnspent(this):
pprint.pprint(this.listUnspent())
#returns json
def createRawT(this, message):
inputSum=0
inputs=[]
for x in this.listUnspent():
inputs.append({
'txid':x['txid'],
'vout':x['vout']
})
inputSum+=x['amount']
#print inputs
outputSum=0
outputs={}
i=0
for char in message:
if i <len(charAddrs):
outputs[charAddrs[i]]=ord(char)*CHARCOST
outputSum+=(ord(char)*CHARCOST)
i+=1
outputs[garbageAddr]=(inputSum-outputSum-MINING_FEE)
r=connect.sendCommand('createrawtransaction', [inputs, outputs])
if DEBUG:
r2=connect.sendCommand('decoderawtransaction', [r.json()['result']])
pprint.pprint(r2.json())
return r
#takes json from create raw transaction
#returns json
def signRawT(this, rawt):
r=connect.sendCommand('signrawtransaction', [rawt.json()['result']])
#pprint.pprint(r.json())
return r
#takes signed raw transaction hex string as input
#returns json
def sendRawT(this, rawt):
r=connect.sendCommand('sendrawtransaction', [rawt])
return r
def pay(this, message):
this.sayPassphrase(2)
rawt=this.createRawT(message)
r= rawt.json()
if r['error']!=None:
print r['error']
sys.exit()
rj=this.signRawT(rawt).json()
#pprint.pprint(r.json())
error=rj['error']
signed=rj['result']['hex']
complete=rj['result']['complete']
printreadable(signed)
answ=raw_input("pay this? (y/n):\n")
if(answ!='y'):
print 'payment aborted'
sys.exit()
if(error!=None):
print error
return
elif complete!=True:
print 'incomplete'
return
else:
print 'sending raw transaction now!'
r=this.sendRawT(signed)
pprint.pprint(r.json())
def sayPassphrase(this, t):
connect.sendCommand('walletpassphrase', [this.phrase, t])
def cliVote(this):
msg=voteCli.doVote()
this.pay(msg)
voter=Voter()
voter.name="Steve"
voter.addr='n2Be2i6pht24yLgaX3ewfjtzZavFaLgs9o'
voter.phrase=connect.phrase
#voter.pay("This is a test")
#pprint.pprint(loadCharAddrs())
#printSentToCharAddrs()
#decodeCharTransactions()
#sentChar2()
#voter.guiVote()
#fundVoter(voter)