Diffstat (limited to 'frontend/delta/js/Clipperz/PM/Toll.js') (more/less context) (ignore whitespace changes)
-rw-r--r-- | frontend/delta/js/Clipperz/PM/Toll.js | 189 |
1 files changed, 189 insertions, 0 deletions
diff --git a/frontend/delta/js/Clipperz/PM/Toll.js b/frontend/delta/js/Clipperz/PM/Toll.js new file mode 100644 index 0000000..e9c3092 --- a/dev/null +++ b/frontend/delta/js/Clipperz/PM/Toll.js | |||
@@ -0,0 +1,189 @@ | |||
1 | /* | ||
2 | |||
3 | Copyright 2008-2013 Clipperz Srl | ||
4 | |||
5 | This file is part of Clipperz, the online password manager. | ||
6 | For further information about its features and functionalities please | ||
7 | refer to http://www.clipperz.com. | ||
8 | |||
9 | * Clipperz is free software: you can redistribute it and/or modify it | ||
10 | under the terms of the GNU Affero General Public License as published | ||
11 | by the Free Software Foundation, either version 3 of the License, or | ||
12 | (at your option) any later version. | ||
13 | |||
14 | * Clipperz is distributed in the hope that it will be useful, but | ||
15 | WITHOUT ANY WARRANTY; without even the implied warranty of | ||
16 | MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. | ||
17 | See the GNU Affero General Public License for more details. | ||
18 | |||
19 | * You should have received a copy of the GNU Affero General Public | ||
20 | License along with Clipperz. If not, see http://www.gnu.org/licenses/. | ||
21 | |||
22 | */ | ||
23 | |||
24 | if (typeof(Clipperz) == 'undefined') { Clipperz = {}; } | ||
25 | if (typeof(Clipperz.PM) == 'undefined') { Clipperz.PM = {}; } | ||
26 | |||
27 | //============================================================================= | ||
28 | |||
29 | Clipperz.PM.Toll = function(args) { | ||
30 | args = args || {}; | ||
31 | |||
32 | this._requestType = args.requestType; | ||
33 | this._targetValue = args.targetValue; | ||
34 | this._cost = args.cost; | ||
35 | this._toll = null; | ||
36 | |||
37 | return this; | ||
38 | } | ||
39 | |||
40 | Clipperz.PM.Toll.prototype = MochiKit.Base.update(null, { | ||
41 | |||
42 | 'toString': function() { | ||
43 | return "Clipperz.PM.Toll (" + this.requestType() + ": " + this.cost() + " - " + ((this.toll() == null)? 'UNPAID' : 'PAID') + ")"; | ||
44 | }, | ||
45 | |||
46 | //------------------------------------------------------------------------- | ||
47 | |||
48 | 'requestType': function() { | ||
49 | return this._requestType; | ||
50 | }, | ||
51 | |||
52 | //------------------------------------------------------------------------- | ||
53 | |||
54 | 'targetValue': function() { | ||
55 | return this._targetValue; | ||
56 | }, | ||
57 | |||
58 | //------------------------------------------------------------------------- | ||
59 | |||
60 | 'cost': function() { | ||
61 | return this._cost; | ||
62 | }, | ||
63 | |||
64 | //------------------------------------------------------------------------- | ||
65 | |||
66 | 'toll': function() { | ||
67 | return this._toll; | ||
68 | }, | ||
69 | |||
70 | //------------------------------------------------------------------------- | ||
71 | /* | ||
72 | '__pay': function() { | ||
73 | varresult; | ||
74 | vartargetData; | ||
75 | vartargetMatchSize; | ||
76 | var prefixMatchingBits; | ||
77 | varpayment; | ||
78 | var i; | ||
79 | |||
80 | if (this.toll() == null) { | ||
81 | i = 0; | ||
82 | targetData = new Clipperz.ByteArray("0x" + this.targetValue()); | ||
83 | targetMatchSize = this.cost(); | ||
84 | |||
85 | payment = Clipperz.Crypto.PRNG.defaultRandomGenerator().getRandomBytes(32); | ||
86 | |||
87 | do { | ||
88 | varpaymentData; | ||
89 | |||
90 | //payment = Clipperz.Crypto.PRNG.defaultRandomGenerator().getRandomBytes(32); | ||
91 | payment.increment(); | ||
92 | paymentData = Clipperz.Crypto.SHA.sha256(payment); | ||
93 | // prefixMatchingBits = this.prefixMatchingBits(targetData, paymentData); | ||
94 | prefixMatchingBits = Clipperz.ByteArray.prefixMatchingBits(targetData, paymentData); | ||
95 | i++; | ||
96 | } while (prefixMatchingBits < targetMatchSize); | ||
97 | |||
98 | this._toll = payment.toHexString().substring(2) | ||
99 | } | ||
100 | |||
101 | return this; | ||
102 | }, | ||
103 | */ | ||
104 | //------------------------------------------------------------------------- | ||
105 | |||
106 | 'innerDeferredPay': function (aTargetValue, aCost, aPayment) { | ||
107 | var deferredResult; | ||
108 | var result; | ||
109 | var payment; | ||
110 | var i; | ||
111 | |||
112 | result = null; | ||
113 | payment = aPayment; | ||
114 | i = 0; | ||
115 | |||
116 | while ((result == null) && (i < Clipperz.PM.Toll.numberOfCloseLoopIterations)) { | ||
117 | if (Clipperz.ByteArray.prefixMatchingBits(aTargetValue, Clipperz.Crypto.SHA.sha256(payment)) > aCost) { | ||
118 | result = payment; | ||
119 | } else { | ||
120 | payment.increment(); | ||
121 | } | ||
122 | |||
123 | i ++; | ||
124 | } | ||
125 | |||
126 | if (result == null) { | ||
127 | deferredResult = MochiKit.Async.callLater(Clipperz.PM.Toll.pauseBetweenEachCloseLoop, MochiKit.Base.method(this, 'innerDeferredPay', aTargetValue, aCost, aPayment)); | ||
128 | } else { | ||
129 | deferredResult = MochiKit.Async.succeed(result); | ||
130 | } | ||
131 | |||
132 | return deferredResult; | ||
133 | }, | ||
134 | |||
135 | 'deferredPay': function () { | ||
136 | vardeferredResult; | ||
137 | vartoll; | ||
138 | |||
139 | toll = this; | ||
140 | deferredResult = new Clipperz.Async.Deferred("Toll.deferredPay"); | ||
141 | //deferredResult.addLog("--->>> deferredPay - " + this.cost()); | ||
142 | deferredResult.addMethod(Clipperz.Crypto.PRNG.defaultRandomGenerator(), 'getRandomBytes', 32); | ||
143 | deferredResult.addMethod(toll, 'innerDeferredPay', new Clipperz.ByteArray("0x" + this.targetValue()), this.cost()); | ||
144 | deferredResult.addCallback(MochiKit.Base.bind(function(aPayment) { | ||
145 | var result; | ||
146 | |||
147 | result = { | ||
148 | targetValue: this.targetValue(), | ||
149 | toll: aPayment.toHexString().substr(2) | ||
150 | }; | ||
151 | |||
152 | return result; | ||
153 | }, this)); | ||
154 | //deferredResult.addLog("<<<--- deferredPay - " + this.cost()); | ||
155 | deferredResult.callback(); | ||
156 | |||
157 | return deferredResult; | ||
158 | }, | ||
159 | |||
160 | //========================================================================= | ||
161 | __syntaxFix__: "syntax fix" | ||
162 | |||
163 | }); | ||
164 | |||
165 | |||
166 | Clipperz.PM.Toll.validate = function(aTargetValue, aToll, aCost) { | ||
167 | var result; | ||
168 | vartollValue; | ||
169 | var targetValue; | ||
170 | var hashedTollValue; | ||
171 | var payedToll; | ||
172 | |||
173 | tollValue = new Clipperz.ByteArray("0x" + aToll); | ||
174 | targetValue = new Clipperz.ByteArray("0x" + aTargetValue); | ||
175 | hashedTollValue = Clipperz.Crypto.SHA.sha256(tollValue); | ||
176 | |||
177 | payedToll = Clipperz.ByteArray.prefixMatchingBits(targetValue, hashedTollValue); | ||
178 | |||
179 | if (payedToll < aCost) { | ||
180 | result = false; | ||
181 | } else { | ||
182 | result = true; | ||
183 | } | ||
184 | |||
185 | return result; | ||
186 | }; | ||
187 | |||
188 | Clipperz.PM.Toll.numberOfCloseLoopIterations = 50; | ||
189 | Clipperz.PM.Toll.pauseBetweenEachCloseLoop = 0.5; \ No newline at end of file | ||