-
Notifications
You must be signed in to change notification settings - Fork 0
/
client.py
executable file
·540 lines (479 loc) · 24 KB
/
client.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
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
#!/usr/bin/python3
# -*- coding: utf-8 -*-
# Welcome to ⊔net!
#-----------------------------------------------------------------------------
# © Copyright 2013 ⊔Foundation
# This file is part of ⊔net.
#
# ⊔net is free software: you can redistribute it and/or modify
# it under the terms of the GNU General Public License as published by
# the Free Software Foundation, either version 3 of the License, or
# (at your option) any later version.
#
# ⊔net is distributed in the hope that it will be useful,
# but WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
# GNU General Public License for more details.
#
# You should have received a copy of the GNU General Public License
# along with ⊔net. If not, see <http://www.gnu.org/licenses/>.
#
# Acknowledgements:
# * ECDSA has been adapted to Python3 and simplified for 512P curve only
# code inspired from:
# Brian Warner
# * The PyCrypt library is far too complex for our needs so we used a code
# for AES inspired from:
# Josh Davis ( http://www.josh-davis.org )
# Laurent Haan (http://www.progressive-coding.com)
# * Encryption with ECC use an idea of jackjack-jj on github
#-----------------------------------------------------------------------------
import re, os, sys, urllib.parse, hashlib, http.client, base64, dbm.ndbm, datetime, functools, subprocess, time, smtplib, operator, random, getpass
__digest__ = base64.urlsafe_b64encode(hashlib.sha1(open(__file__, 'r', encoding='utf8').read().encode('utf8')).digest())[:10]
__app__ = os.path.basename(__file__)[:-3]
__dfprt__ = 36368
__base__ = '/%s/%s_%s/' % (__app__,__app__,__dfprt__)
__ppc__ = 'pingpongcash'
__email__ = 'info@%s.fr' % __ppc__
__url__ = 'http://%s.fr' % __ppc__
_SVGNS = 'xmlns="http://www.w3.org/2000/svg"'
##### ENCODING #####
PAD = lambda s:(len(s)%2)*'0'+s[2:]
def i2b(x, n=1):
"int to bytes with n padding"
z = bytes.fromhex(PAD(hex(x)))
return ((n-len(z))%n)*bytes.fromhex('00') + z
def b2i(x):
"bytes to int"
return int.from_bytes(x, 'big')
def itob64(n):
"transform int to base64"
return re.sub(b'=*$', b'', base64.urlsafe_b64encode(bytes.fromhex(PAD(hex(n)))))
def b64toi(c):
"transform base64 to int"
if c == '': return 0
return int.from_bytes(base64.urlsafe_b64decode(c + b'='*((4-(len(c)%4))%4)), 'big')
def btob64(c):
"bytes to base64"
return base64.urlsafe_b64encode(c).decode('ascii').strip('=')
def b64tob(c):
"base64 to bytes"
return base64.urlsafe_b64decode(c + b'='*((4-(len(c)%4))%4))
def H(*tab):
"hash"
return int(hashlib.sha1(b''.join(tab)).hexdigest(), 16)
def datencode(n=0):
"4 chars"
return i2b(int(time.mktime(time.gmtime()) + 3600*24*n), 4)
def hcode(m, s=10):
"_"
return (hashlib.sha1(m.encode('utf8')).digest())[:s]
def valencode(xi, p1, pf):
"xi:7p1:15pf:26"
assert (p1 <= pf or xi==0) and xi<=100 and p1<(1<<15) and pf<(1<<26)
return i2b((xi<<41) + (p1<<26) + pf, 6)
##### ECDSA NIST CURVE P-521 #####
_B = b'UZU-uWGOHJofkpohoLaFQO6i2nJbmbMV87i0iZGO8QnhVhk5Uex-k3sWUsC9O7G_BzVz34g9LDTx70Uf1GtQPwA'
_GX = b'xoWOBrcEBOnNnj7LZiOVtEKcZIE5BT-1Ifgor2BrTT26oUted-_nWSj-HcEnov-o3jNIs8GFakKb-X5-McLlvWY'
_GY = b'ARg5KWp4mjvABFyKX7QsfRvZmPVESVebRGgXr70XJz5mLJfucple9CZAxVC5AT-tB2E1PHCGonLCQIi-lHaf0WZQ'
_R = b'Af' + b'_'*42 + b'-lGGh4O_L5Zrf8wBSPcJpdA7tcm4iZxHrrtvtx6ROGQJ'
class Curve():
"The curve of points satisfying y^2 = x^3 + a*x + b (mod p)"
def __init__(self, p, a, b): self.p, self.a, self.b = p, a, b
def has_pt(self, x, y): return (y*y - (x*x*x + self.a*x + self.b)) % self.p == 0
c521 = Curve(b64toi(b'Af' + b'_'*86), -3, b64toi(_B))
class Point():
def __init__(self, curve, x, y, order = None):
self.curve, self.x, self.y, self.order = curve, x, y, order
def __add__(self, other):
if other == INFINITY: return self
if self == INFINITY: return other
if self.x == other.x:
if (self.y + other.y) % self.curve.p == 0: return INFINITY
else: return self.double()
p = self.curve.p
l = ((other.y - self.y) * inverse_mod(other.x - self.x, p)) % p
x3 = (l*l - self.x - other.x) % p
y3 = (l*(self.x - x3) - self.y) % p
return Point(self.curve, x3, y3)
def __mul__(self, e):
if self.order: e = e % self.order
if e == 0 or self == INFINITY: return INFINITY
e3, neg_self = 3*e, Point(self.curve, self.x, -self.y, self.order)
i = 1 << (len(bin(e3))-4)
result = self
while i > 1:
result = result.double()
if (e3 & i) != 0 and (e & i) == 0: result = result + self
if (e3 & i) == 0 and (e & i) != 0: result = result + neg_self
i //= 2
return result
def __rmul__(self, other): return self * other
def double(self):
if self == INFINITY: return INFINITY
p, a = self.curve.p, self.curve.a
l = ((3 * self.x * self.x + a) * inverse_mod(2 * self.y, p)) % p
x3 = (l*l - 2 * self.x) % p
y3 = (l*(self.x - x3) - self.y) % p
return Point(self.curve, x3, y3)
INFINITY = Point(None, None, None)
class ecdsa:
def __init__(self):
self.gen = Point(c521, b64toi(_GX), b64toi(_GY), b64toi(_R))
self.pkgenerator, self.pkorder = self.gen, self.gen.order
def generate(self):
secexp = randrange(self.gen.order)
pp = self.gen*secexp
self.pt, n = pp, self.gen.order
if not n: raise 'Generator point must have order!'
if not n * pp == INFINITY: raise 'Bad Generator point order!'
if pp.x < 0 or n <= pp.x or pp.y < 0 or n <= pp.y: raise 'Out of range!'
self.privkey = secexp
def sign(self, data):
rk, G, n = randrange(self.pkorder), self.pkgenerator, self.pkorder
k = rk % n
p1 = k * G
r = p1.x
s = (inverse_mod(k, n) * (H(data) + (self.privkey * r) % n)) % n
assert s != 0 and r != 0
return i2b(r, 66) + i2b(s, 66)
def decrypt(self, enctxt):
oo, x1, x2 = int(enctxt[:2], 16), b2i(enctxt[2:68]), b2i(enctxt[68:])
o1, o2, offset, p, a, b = (oo & 0x80)>>7,(oo & 0x40)>>6, oo & 0x3F, b64toi(b'Af' + b'_'*86), -3, b64toi(_B)
z1, z2 = pow(x1, 3, p) + a * x1 + b % p, pow(x2, 3, p) + a * x2 + b % p
t1, t2 = pow(z1, (p+1)//4, p), pow(z2, (p+1)//4, p)
y1, y2 = t1 if int(o1) == t1&1 else (-t1)%p, t2 if int(o2) == t2&1 else (-t2)%p
p1, p2 = Point(c521, x1, - y1), Point(c521, x2, y2)
u = p2 + self.privkey * p1
return i2b(u.x-offset)
def inverse_mod(a, m):
"_"
if a < 0 or m <= a: a = a % m
c, d = a, m
uc, vc, ud, vd = 1, 0, 0, 1
while c != 0:
q, c, d = divmod(d, c) + (c,)
uc, vc, ud, vd = ud - q*uc, vd - q*vc, uc, vc
#assert d == 1
if ud > 0: return ud
else: return ud + m
def randrange(order):
"_"
byts = (1+len('%x' % order))//2
cand = b2i(os.urandom(byts))
return cand//2 if cand >= order else cand
##### LOCAL AES-256 ##### (replace PyCrypto AES)
_IV = b'ABCDEFGHIJKLMNOP'
class AES:
sbox = [
0x63, 0x7c, 0x77, 0x7b, 0xf2, 0x6b, 0x6f, 0xc5, 0x30, 0x01, 0x67, 0x2b, 0xfe, 0xd7, 0xab, 0x76,
0xca, 0x82, 0xc9, 0x7d, 0xfa, 0x59, 0x47, 0xf0, 0xad, 0xd4, 0xa2, 0xaf, 0x9c, 0xa4, 0x72, 0xc0,
0xb7, 0xfd, 0x93, 0x26, 0x36, 0x3f, 0xf7, 0xcc, 0x34, 0xa5, 0xe5, 0xf1, 0x71, 0xd8, 0x31, 0x15,
0x04, 0xc7, 0x23, 0xc3, 0x18, 0x96, 0x05, 0x9a, 0x07, 0x12, 0x80, 0xe2, 0xeb, 0x27, 0xb2, 0x75,
0x09, 0x83, 0x2c, 0x1a, 0x1b, 0x6e, 0x5a, 0xa0, 0x52, 0x3b, 0xd6, 0xb3, 0x29, 0xe3, 0x2f, 0x84,
0x53, 0xd1, 0x00, 0xed, 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0x97, 0x35, 0x6a, 0xd4, 0xb3,
0x7d, 0xfa, 0xef, 0xc5, 0x91, 0x39, 0x72, 0xe4, 0xd3, 0xbd, 0x61, 0xc2, 0x9f, 0x25, 0x4a, 0x94,
0x33, 0x66, 0xcc, 0x83, 0x1d, 0x3a, 0x74, 0xe8, 0xcb, 0x8d, 0x01, 0x02, 0x04, 0x08, 0x10, 0x20,
0x40, 0x80, 0x1b, 0x36, 0x6c, 0xd8, 0xab, 0x4d, 0x9a, 0x2f, 0x5e, 0xbc, 0x63, 0xc6, 0x97, 0x35,
0x6a, 0xd4, 0xb3, 0x7d, 0xfa, 0xef, 0xc5, 0x91, 0x39, 0x72, 0xe4, 0xd3, 0xbd, 0x61, 0xc2, 0x9f,
0x25, 0x4a, 0x94, 0x33, 0x66, 0xcc, 0x83, 0x1d, 0x3a, 0x74, 0xe8, 0xcb, 0x8d, 0x01, 0x02, 0x04,
0x08, 0x10, 0x20, 0x40, 0x80, 0x1b, 0x36, 0x6c, 0xd8, 0xab, 0x4d, 0x9a, 0x2f, 0x5e, 0xbc, 0x63,
0xc6, 0x97, 0x35, 0x6a, 0xd4, 0xb3, 0x7d, 0xfa, 0xef, 0xc5, 0x91, 0x39, 0x72, 0xe4, 0xd3, 0xbd,
0x61, 0xc2, 0x9f, 0x25, 0x4a, 0x94, 0x33, 0x66, 0xcc, 0x83, 0x1d, 0x3a, 0x74, 0xe8, 0xcb ]
def rotate(self, word):
c = word[0]
for i in range(3): word[i] = word[i+1]
word[3] = c
return word
def core(self, word, iteration):
word = self.rotate(word)
for i in range(4): word[i] = self.sbox[word[i]]
word[0] = word[0]^self.Rcon[iteration]
return word
def expandKey(self, key, expandedKeySize):
currentSize, rconIteration, t, expandedKey = 32, 1, [0]*4, [0]*expandedKeySize
for j in range(32): expandedKey[j] = key[j]
while currentSize < expandedKeySize:
for k in range(4): t[k] = expandedKey[(currentSize - 4) + k]
if currentSize % 32 == 0:
t = self.core(t, rconIteration)
rconIteration += 1;
if (currentSize % 32) == 16:
for l in range(4): t[l] = self.sbox[t[l]]
for m in range(4):
expandedKey[currentSize] = expandedKey[currentSize - 32] ^ t[m]
currentSize += 1
return expandedKey
def addRoundKey(self, state, roundKey):
for i in range(16): state[i] ^= roundKey[i]
return state
def createRoundKey(self, expandedKey, roundKeyPointer):
return [expandedKey[roundKeyPointer+i*4+j] for j in range(4) for i in range(4)]
def g_mult(self, a, b):
p = 0
for counter in range(8):
if (b & 1) == 1: p ^= a
if p > 0x100: p ^= 0x100
hi_bit_set = (a & 0x80)
a <<= 1
if a > 0x100: a ^= 0x100
if hi_bit_set == 0x80: a ^= 0x1b
if a > 0x100: a ^= 0x100
b >>= 1
if b > 0x100: b ^= 0x100
return p
def subBytes(self, state):
for i in range(16): state[i] = self.sbox[state[i]]
return state
def shiftRows(self, state):
for i in range(4): state = self.shiftRow(state, i*4, i)
return state
def shiftRow(self, state, pointer, nbr):
for i in range(nbr):
tmp = state[pointer]
for j in range(3): state[pointer + j] = state[pointer + j+1]
state[pointer + 3] = tmp
return state
def mixColumns(self, state):
column = [0]*4
for i in range(4):
for j in range(4): column[j] = state[(j*4)+i]
column = self.mixColumn(column)
for k in range(4): state[(k*4)+i] = column[k]
return state;
def mixColumn(self, col):
mult, cpy = [2,1,1,3], [0]*4
for i in range(4): cpy[i] = col[i]
col[0] = self.g_mult(cpy[0],mult[0]) ^ self.g_mult(cpy[3],mult[1]) ^ self.g_mult(cpy[2],mult[2]) ^ self.g_mult(cpy[1],mult[3])
col[1] = self.g_mult(cpy[1],mult[0]) ^ self.g_mult(cpy[0],mult[1]) ^ self.g_mult(cpy[3],mult[2]) ^ self.g_mult(cpy[2],mult[3])
col[2] = self.g_mult(cpy[2],mult[0]) ^ self.g_mult(cpy[1],mult[1]) ^ self.g_mult(cpy[0],mult[2]) ^ self.g_mult(cpy[3],mult[3])
col[3] = self.g_mult(cpy[3],mult[0]) ^ self.g_mult(cpy[2],mult[1]) ^ self.g_mult(cpy[1],mult[2]) ^ self.g_mult(cpy[0],mult[3])
return col
def aes_round(self, state, roundKey):
state = self.subBytes(state)
state = self.shiftRows(state)
state = self.mixColumns(state)
state = self.addRoundKey(state, roundKey)
return state
def aes_main(self, state, expandedKey, nbrRounds):
state, i = self.addRoundKey(state, self.createRoundKey(expandedKey,0)), 1
for i in range(1, nbrRounds): state = self.aes_round(state, self.createRoundKey(expandedKey,16*i))
state = self.subBytes(state)
state = self.shiftRows(state)
state = self.addRoundKey(state, self.createRoundKey(expandedKey,16*nbrRounds))
return state
def enc(self, iput, key):
output, block, nbrRounds = [0]*16, [0]*16, 14
expandedKeySize = (16*(nbrRounds+1))
for i in range(4):
for j in range(4): block[(i+(j*4))] = iput[(i*4)+j]
expandedKey = self.expandKey(key, expandedKeySize)
block = self.aes_main(block, expandedKey, nbrRounds)
for k in range(4):
for l in range(4): output[(k*4)+l] = block[(k+(l*4))]
return output
def encrypt(self, stringIn, key):
plain, iput, output, cipher = [], [], [], [0]*16
cipherOut, firstRound = [], True
if stringIn != None:
for j in range(1+(len(stringIn)-1)//16):
start, end = j*16, j*16+16
if j*16+16 > len(stringIn): end = len(stringIn)
plain = (stringIn.encode('utf8'))[start:end]
if firstRound: output, firstRound = self.enc(_IV, key), False
else: output = self.enc(iput, key)
for i in range(16):
if len(plain)-1 < i: cipher[i] = 0 ^ output[i]
elif len(output)-1 < i: cipher[i] = plain[i] ^ 0
elif len(plain)-1 < i and len(output) < i: cipher[i] = 0 ^ 0
else: cipher[i] = plain[i] ^ output[i]
for k in range(end-start): cipherOut.append(cipher[k])
iput = output
return bytes(cipherOut)
def decrypt(self, cIn, key):
cipher, iput, output, plain, stringOut, fround, cipherIn = [], [], [], [0]*16, '', True, [i for i in cIn]
if cipherIn != None:
for j in range(1+(len(cipherIn)-1)//16):
start, end = j*16, j*16+16
if j*16+16 > len(cipherIn): end = len(cipherIn)
cipher = cipherIn[start:end]
if fround: output, fround = self.enc(_IV, key), False
else: output = self.enc(iput, key)
for i in range(16):
if len(output)-1 < i: plain[i] = 0 ^ cipher[i]
elif len(cipher)-1 < i: plain[i] = output[i] ^ 0
elif len(output)-1 < i and len(cipher) < i: plain[i] = 0 ^ 0
else: plain[i] = output[i] ^ cipher[i]
for k in range(end-start): stringOut += chr(plain[k])
iput = output
return stringOut.rstrip('@');
###### API #####
def register():
"_"
pp1, pp2, cm = '', '', ''
db = dbm.open('keys', 'c')
while pp1 != pp2 or len(pp1) < 4:
pp1 = getpass.getpass('Select a passphrase? ')
pp2 = getpass.getpass('The passphrase again? ')
print ('...wait')
k = ecdsa()
k.generate()
cm = i2b(k.pt.y)[-9:]
while cm in db:
k = ecdsa()
k.generate()
cm = i2b(k.pt.y)[-9:]
pub, priv = i2b(k.pt.x, 66) + i2b(k.pt.y, 66), AES().encrypt('%s' % k.privkey, hashlib.sha256(pp1.encode('utf8')).digest())
db[cm] = pub+priv
db.close()
print ('Your personnal keys generated in keys.db file. Id: %s' % (btob64(cm)))
return 'P:' + btob64(pub)
def getpub():
"_"
db = dbm.open('keys')
pub = None
for u in db.keys(): pub = db[u][:132]
return btob64(pub)
def buy(dst, prc, m=''):
"_"
db, src, k, dat = dbm.open('keys'), None, ecdsa(), datencode()
assert(len(dst) == 9)
for u in db.keys(): src, prv, pub = u, db[u][132:], db[u][:132]
pp = getpass.getpass('Passphrase for \'%s\'? ' % btob64(src))
if m == '': m = input('Message (20 chars maxi)? ')
print ('...please wait')
k.privkey, msg = int(AES().decrypt(prv, hashlib.sha256(pp.encode('utf8')).digest())), datencode() + src + dst + i2b(prc, 3) + bytes(m, 'utf8')[:20]
db.close()
return 'A:' + btob64(msg + k.sign(msg))
def decrypt(eurl):
"_"
db, src, k = dbm.open('keys'), None, ecdsa()
for u in db.keys(): src, prv = u, db[u][132:]
pp = getpass.getpass('Passphrase for \'%s\'? ' % btob64(src))
k.privkey = int(AES().decrypt(prv, hashlib.sha256(pp.encode('utf8')).digest()))
db.close()
return k.decrypt(eurl)
def reg(value):
" function attribute is a way to access matching group in one line test "
reg.v = value
return value
def buyig (ig, node):
"_"
hig = btob64(hcode(node+'/publish/'+ig))
print (hig)
res = send(node, 'IG:%s' % hig)
if reg(re.match(r'([^:]+):(\d+)$', res)): rig, nb = b64tob(bytes(reg.v.group(1), 'ascii')), i2b(int(reg.v.group(2)), 4)
else: return 'error'
db, src, k = dbm.open('keys'), None, ecdsa()
for u in db.keys(): src, prv, pub = u, db[u][132:], db[u][:132]
pp = getpass.getpass('Passphrase 111 for \'%s\'? ' % btob64(src))
print ('...please wait')
k.privkey, msg = int(AES().decrypt(prv, hashlib.sha256(pp.encode('utf8')).digest())), nb + rig + src + datencode()
print (send(node, 'B:' + btob64(msg + k.sign(msg))))
res = send(node, ig + ':%s' % b2i(nb))
if res != 'Error:': print ('http://%s/%s' % (node, k.decrypt(b64tob(bytes(res, 'ascii'))).decode('ascii')))
else: print (res)
db.close()
def postig(xi, p1, pf):
"_"
db, src, k = dbm.open('keys'), None, ecdsa()
for u in db.keys(): src, prv, pub = u, db[u][132:], db[u][:132]
pp = getpass.getpass('Passphrase for \'%s\'? ' % btob64(src))
url = input('url? ')
msg = hcode(url) + src + datencode() + valencode(xi, p1, pf)
print ('...please wait')
k.privkey = int(AES().decrypt(prv, hashlib.sha256(pp.encode('utf8')).digest()))
db.close()
return 'I:' + btob64(msg + k.sign(msg))
def send(host='localhost', data=''):
"_"
co, serv = http.client.HTTPConnection(host), '/'
co.request('POST', serv, urllib.parse.quote(data))
return co.getresponse().read().decode('utf8')
def readdb(arg):
"_"
d = dbm.open(arg)
for x in d.keys(): print ('%02d:%03d' % (len(x), len(d[x])), btob64(x),'->', btob64(d[x]))
##### MAIN #####
def usage():
"""If 'keys.db' or 'keys' file does not exist\n'./client.py' creates a new private key in this file and sent public key to a public node
If the keys file exits, './client.py' send again the public key.
'./client.pt <dbfile>' displays any gnu_berkeley-database content in base64 format
'./client.pt <price>' generates an backup icheck to an ibank in the icheck.txt file. Save this file in a secure place in case you loose your passphrase or if someone steel your smartphone with the private keys file.
'./client.pt <begin-id>' returns the full id if the <begin-id> string is unique
'./client.pt <price> <recipient>' generates and sent a transaction of a float <price> for a string valid <recipient> id
Bad transactions may result from:\n- recipient is unknown or is yourself\n- price is higher than your balance\n- signature verification returns false
Connect to %s to see balance report\nContact %s for any question"""
return usage.__doc__ % (__url__, __email__)
if __name__ == '__main__':
localnode = 'cup' # for debugging
node = '%s.fr' % __ppc__
node = localnode
if len(sys.argv)==2:
if os.path.isfile(sys.argv[1]): # read db
readdb(sys.argv[1])
elif re.match(r'\d{1,5}', sys.argv[1]): # icheck
bnk = send(node, 'IBANK')
aa = buy(b64tob(bytes(bnk, 'ascii')), int(sys.argv[1]), 'backup')
open('icheck.txt', 'w').write(aa)
print ('Transaction generated to %s ibank in \'icheck.txt\' file' % bnk)
else:
print (send(node, sys.argv[1])) # find userID
elif len(sys.argv)==1:
r = getpub() if (os.path.isfile('keys') or os.path.isfile('keys.db')) else register()
print(send(node, r)) # need Net connexion
elif len(sys.argv)==3:
if re.match('[\d\.\,]+', sys.argv[2]):
s = buy(b64tob(bytes(sys.argv[1], 'ascii')), int(float(sys.argv[2])*100)) # €
print (send(node, s))
elif re.match('\d+$', sys.argv[1]):
res = send(node, sys.argv[2] + ':' + sys.argv[1])
if res != 'Error:':
print ('http://%s/%s' % (node, decrypt(b64tob(bytes(res, 'ascii'))).decode('ascii')))
elif sys.argv[1] == 'ig': # find n
print (send(node, 'IG:' + sys.argv[2]))
elif sys.argv[1] == 'buy':
buyig(sys.argv[2], node) # ⊔
elif len(sys.argv)==4: # IG registration
s = postig(int(sys.argv[1]), int(sys.argv[2]), int(sys.argv[3]))
print (send(node, s)) # need Net connexion
else:
print (usage())
sys.exit()
# End ⊔net!