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