netlib.js 5.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224
  1. (function(){
  2. /// CONSISTENCY ///
  3. var RESENDTIME = 2000;
  4. function resendOnFail(){
  5. setTimeout(resendOnFail, RESENDTIME);
  6. myClass.queue.trySend();
  7. };
  8. var randomNumber = parseInt(Math.random() * 100000.0);
  9. console.log(randomNumber);
  10. var myClass = {
  11. queue : {
  12. _queue: [],
  13. send : function(data){
  14. // Create
  15. console.log("Move added to the queue: " + web3.sha3(data.signature.data));
  16. this._queue.push(data);
  17. console.log(this._queue);
  18. },
  19. pop : function(data){
  20. },
  21. trySend : function(){
  22. if(!this._queue.length)return;
  23. var data = this._queue[0];
  24. myClass.sockets[myClass._channel].send(data);
  25. },
  26. _getHash : function(){
  27. if(!this._queue.length)return undefined;
  28. var data = this._queue[0];
  29. var hash = web3.sha3(data.signature.data); // already stringified!
  30. //console.log(hash);
  31. return hash;
  32. }
  33. },
  34. sockets : {},
  35. _channel: "defaultGAMECHANNEL1",
  36. _fn : function(){},
  37. _proof : false,
  38. _address : "",
  39. _solo : false,
  40. _ACKs : {},
  41. _interceptors : [],
  42. _interceptor : function(data){
  43. var temp = data;
  44. for(var i=0; i < myClass._interceptors.length ; i++){
  45. temp = myClass._interceptors[i].call(myClass, temp, data);
  46. if(temp === false){
  47. return false;
  48. }
  49. }
  50. return true;
  51. },
  52. __soloInterceptor : function(data){
  53. if(this._solo && data.who != this._address)return false;
  54. if(!this.verifyMessage(data)){
  55. //console.log("Verification Failed for:");
  56. //console.log(data);
  57. return false;
  58. }
  59. // parse Data !!!
  60. data.signature.data = JSON.parse(data.signature.data);
  61. return data;
  62. //
  63. },
  64. __ACKInterceptor : function(data){
  65. if(data.ACK){
  66. if(data.signature.data.ackSignature == this._getHash()){
  67. var item = this.queue._queue.shift();
  68. console.log("recievedACK for : " + data.signature.data.ackSignature);
  69. console.log(this.queue._queue);
  70. return false;
  71. }
  72. }
  73. return false;
  74. },
  75. __proofInterceptor : function(data){
  76. if(data.signature.data.proof){
  77. //console.log("Proof.. Sending last packet!");
  78. this.saveProof(data);
  79. this.sendLastPacket();
  80. this.sendACK(data);
  81. return false;
  82. }
  83. else{
  84. if(this._proof){
  85. if(this.verifyProof(data, this._proof)){
  86. //console.log("GOOD PROOF");
  87. this.sendACK(data);
  88. this.clearProof();
  89. return data.signature.data;
  90. }
  91. else{
  92. // BAD PROOF REPORT!!
  93. //console.log("BAD PROOF");
  94. return false;
  95. }
  96. }
  97. else{
  98. return false;
  99. //console.log("-Discarding");
  100. // i have no proof...
  101. // i can either save or discard
  102. }
  103. }
  104. return data;
  105. },
  106. __saveDataInterceptor : function(data,original) {
  107. // save original to a stack!!!;
  108. return data;
  109. },
  110. setSolo : function(add){
  111. this._address = add;
  112. this._solo = true;
  113. },
  114. setSoloInterceptor : function(){
  115. this._interceptors.push(this.__soloInterceptor);
  116. },
  117. setACKInterceptor : function(){
  118. this._interceptors.push(this.__ACKInterceptor);
  119. },
  120. setProofInterceptor : function(){
  121. this._interceptors.push(this.__proofInterceptor);
  122. },
  123. setSaveInterceptor : function(){
  124. this._interceptors.push(this.__saveDataInterceptor);
  125. },
  126. setBasicInterceptors : function(){
  127. this.setSoloInterceptor();
  128. this.setACKInterceptor();
  129. this.setProofInterceptor();
  130. // this.setSaveInterceptor(); // TODO
  131. }
  132. addInterceptor : function(fn){
  133. this._interceptors.push(fn);
  134. },
  135. startListening : function(){
  136. var self = this;
  137. // Establish whisper connection //
  138. return web3.Lib.initWhisper(this._channel)
  139. .then(function(w){
  140. w.setFunction(self._interceptor); // w.setFunction ( interceptor of fn that will handle ACKs and will send the next on queue!!)
  141. self.sockets[self._channel] = w;
  142. });
  143. },
  144. stopListening : function(){
  145. // todo
  146. },
  147. sendPacketWaitForVerification : function(packet){
  148. packet.randomNumber = randomNumber++;
  149. this._vpacket = packet;
  150. // console.log("PREPARING : ")
  151. // console.log(packet);
  152. this.sendProof(packet);
  153. if(this._proof)this.sendLastPacket();
  154. },
  155. sendLastPacket : function(){
  156. if(this._vpacket){
  157. // console.log("SENDING:");
  158. // console.log(this._vpacket);
  159. this.sendPacket(this._vpacket);
  160. delete this._vpacket;
  161. }
  162. },
  163. sendPacket : function(packet){
  164. var sign = web3.Lib.signData(web3.eth.defaultAccount, packet);
  165. return this.queue.send({
  166. who : web3.eth.defaultAccount,
  167. signature : sign
  168. });
  169. },
  170. sendProof : function(data){
  171. var hash = web3.sha3(JSON.stringify(data));
  172. var dataToSign = { datahash : hash, proof : true};
  173. var sign = web3.Lib.signData(web3.eth.defaultAccount, dataToSign );
  174. this.queue.send({
  175. who : web3.eth.defaultAccount,
  176. signature : sign
  177. });
  178. },
  179. clearProof : function(){
  180. delete this._proof;
  181. },
  182. saveProof : function(p){
  183. this._proof = p;
  184. },
  185. verifyMessage : function(packet){
  186. return web3.Lib.verifySignature(packet.who, packet.signature);
  187. },
  188. verifyProof : function(packet, proof){
  189. var hash = web3.sha3(JSON.stringify(packet.signature.data));
  190. if( proof.signature.data.datahash != hash )return false;
  191. return true;
  192. },
  193. sendACK : function(packet){
  194. // Send Verification For Signed Data
  195. //var dataToSign = JSON.stringify(packet);//data.signature.dataHash.substr(2);
  196. var hash = web3.sha3(JSON.stringify(packet.signature.data));
  197. var dataToSign = {
  198. ackSignature : hash
  199. };
  200. var verificationForSignature = web3.Lib.signData(web3.eth.defaultAccount, dataToSign);
  201. this.sockets[this._channel].send({
  202. who : web3.eth.defaultAccount,
  203. signature : verificationForSignature,
  204. ACK : true
  205. });
  206. },
  207. setChannel : function(ch){
  208. this._channel = ch;
  209. },
  210. init : function(){
  211. setTimeout(resendOnFail, RESENDTIME);
  212. }
  213. };
  214. web3.NetLib = myClass;
  215. })();