17
17
# Caner Candan <[email protected] >, http://caner.candan.fr
18
18
#
19
19
20
- import hashlib , logging
20
+ import logging
21
21
from .. import pks , ucg , hdc , settings
22
22
23
23
logger = logging .getLogger ("wrappers" )
@@ -26,170 +26,4 @@ class Wrapper:
26
26
def __call__ (self ):
27
27
pass
28
28
29
- class Transaction (Wrapper ):
30
- def __init__ (self , type , pgp_fingerprint , message = '' ):
31
- self .pgp_fingerprint = pgp_fingerprint
32
- self .message = message
33
- self .type = type
34
-
35
- def __call__ (self ):
36
- try :
37
- last_tx = hdc .transactions .sender .Last (self .pgp_fingerprint ).get ()
38
- except ValueError :
39
- last_tx = None
40
-
41
- context_data = {}
42
- context_data .update (settings )
43
- context_data ['version' ] = 1
44
- context_data ['number' ] = 0 if not last_tx else last_tx ['transaction' ]['number' ]+ 1
45
- context_data ['previousHash' ] = hashlib .sha1 (("%(raw)s%(signature)s" % last_tx ).encode ('ascii' )).hexdigest ().upper () if last_tx else None
46
- context_data ['message' ] = self .message
47
- context_data ['type' ] = self .type
48
- context_data .update (self .get_context_data ())
49
-
50
- tx = """\
51
- Version: %(version)d
52
- Currency: %(currency)s
53
- Sender: %(fingerprint)s
54
- Number: %(number)d
55
- """ % context_data
56
-
57
- if last_tx : tx += "PreviousHash: %(previousHash)s\n " % context_data
58
-
59
- tx += self .get_message (context_data )
60
-
61
- tx += """\
62
- Comment:
63
- %(message)s
64
- """ % context_data
65
-
66
- tx = tx .replace ("\n " , "\r \n " )
67
- txs = settings ['gpg' ].sign (tx , detach = True )
68
-
69
- return self .process (tx , txs )
70
-
71
- def get_context_data (self ):
72
- return {}
73
-
74
- def get_message (self , context_data , tx = '' ):
75
- return tx
76
-
77
- def process (self , tx , txs ):
78
- try :
79
- hdc .transactions .Process ().post (transaction = tx , signature = txs )
80
- except ValueError as e :
81
- print (e )
82
- else :
83
- return True
84
-
85
- return False
86
-
87
- class Transfer (Transaction ):
88
- def __init__ (self , pgp_fingerprint , recipient , coins , message = '' ):
89
- super ().__init__ ('TRANSFER' , pgp_fingerprint , message )
90
- self .recipient = recipient
91
- self .coins = coins
92
-
93
- def get_message (self , context_data , tx = '' ):
94
- context_data ['recipient' ] = self .recipient
95
-
96
- tx += """\
97
- Recipient: %(recipient)s
98
- Type: %(type)s
99
- Coins:
100
- """ % context_data
101
-
102
- for coin in self .coins .split (',' ):
103
- data = coin .split (':' )
104
- issuer = data [0 ]
105
- for number in data [1 :]:
106
- context_data .update (hdc .coins .View (issuer , int (number )).get ())
107
- tx += '%(id)s, %(transaction)s\n ' % context_data
108
-
109
- return tx
110
-
111
- class Issue (Transaction ):
112
- def __init__ (self , pgp_fingerprint , amendment , coins , message = '' ):
113
- super ().__init__ ('ISSUANCE' , pgp_fingerprint , message )
114
- self .amendment = amendment
115
- self .coins = coins
116
-
117
- def get_next_coin_number (self , coins ):
118
- number = 0
119
- for c in coins :
120
- candidate = int (c ['id' ].split ('-' )[1 ])
121
- if candidate > number : number = candidate
122
- return number + 1
123
-
124
- def get_message (self , context_data , tx = '' ):
125
- context_data ['amendment' ] = self .amendment
126
-
127
- tx += """\
128
- Recipient: %(fingerprint)s
129
- Type: %(type)s
130
- Coins:
131
- """ % context_data
132
-
133
- try :
134
- last_issuance = hdc .transactions .sender .issuance .Last (self .pgp_fingerprint ).get ()
135
- except ValueError :
136
- last_issuance = None
137
-
138
- previous_idx = 0 if not last_issuance else self .get_next_coin_number (last_issuance ['transaction' ]['coins' ])
139
-
140
- for idx , coin in enumerate (self .coins ):
141
- context_data ['idx' ] = idx + previous_idx
142
- context_data ['base' ], context_data ['power' ] = [int (x ) for x in coin .split (',' )]
143
- tx += '%(fingerprint)s-%(idx)d-%(base)d-%(power)d-A-%(amendment)d\n ' % context_data
144
-
145
- return tx
146
-
147
- class CoinsWrapper (Wrapper ):
148
- def __init__ (self , pgp_fingerprint ):
149
- self .pgp_fingerprint = pgp_fingerprint
150
-
151
- class CoinsGet (CoinsWrapper ):
152
- def __init__ (self , pgp_fingerprint , values ):
153
- super ().__init__ (pgp_fingerprint )
154
- self .values = values
155
-
156
- def __call__ (self ):
157
- __list = hdc .coins .List (self .pgp_fingerprint ).get ()
158
- coins = {}
159
- for c in __list ['coins' ]:
160
- for id in c ['ids' ]:
161
- n ,b ,p ,t ,i = id .split ('-' )
162
- amount = int (b ) * 10 ** int (p )
163
- coins [amount ] = {'issuer' : c ['issuer' ], 'number' : int (n ), 'base' : int (b ), 'power' : int (p ), 'type' : t , 'type_number' : int (i ), 'amount' : amount }
164
-
165
- issuers = {}
166
- for v in self .values :
167
- if v in coins :
168
- c = coins [v ]
169
- issuers [c ['issuer' ]] = issuers .get (c ['issuer' ]) or []
170
- issuers [c ['issuer' ]].append (c )
171
- else :
172
- raise ValueError ('You do not have enough coins of value (%d)' % v )
173
-
174
- res = ''
175
- for i , issuer in enumerate (issuers ):
176
- if i > 0 : res += ','
177
- res += issuer
178
- for c in issuers [issuer ]:
179
- res += ':%(number)d' % c
180
-
181
- return res
182
-
183
- class CoinsList (CoinsWrapper ):
184
- def __call__ (self ):
185
- __list = hdc .coins .List (self .pgp_fingerprint ).get ()
186
- coins = []
187
- __sum = 0
188
- for c in __list ['coins' ]:
189
- for id in c ['ids' ]:
190
- n ,b ,p ,t ,i = id .split ('-' )
191
- amount = int (b ) * 10 ** int (p )
192
- __dict = {'issuer' : c ['issuer' ], 'number' : int (n ), 'base' : int (b ), 'power' : int (p ), 'type' : t , 'type_number' : int (i ), 'amount' : amount }
193
- coins .append (__dict )
194
- __sum += amount
195
- return __sum , coins
29
+ from . import transactions , coins
0 commit comments