trackwrapper.js 17 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474
  1. // Generated by IcedCoffeeScript 1.7.1-c
  2. (function() {
  3. var CHECK, E, Link, PT, ST, TrackWrapper, constants, db, deq, env, iced, log, make_esc, prompt_yn, proof_type_to_string, proofs, scrapers, session, unix_time, util, __iced_k, __iced_k_noop;
  4. iced = require('iced-runtime').iced;
  5. __iced_k = __iced_k_noop = function() {};
  6. db = require('./db').db;
  7. constants = require('./constants').constants;
  8. log = require('./log');
  9. proofs = require('keybase-proofs');
  10. proof_type_to_string = proofs.proof_type_to_string;
  11. PT = proofs.constants.proof_types;
  12. ST = constants.signature_types;
  13. deq = require('deep-equal');
  14. E = require('./err').E;
  15. unix_time = require('pgp-utils').util.unix_time;
  16. make_esc = require('iced-error').make_esc;
  17. prompt_yn = require('./prompter').prompt_yn;
  18. session = require('./session').session;
  19. db = require('./db');
  20. util = require('util');
  21. env = require('./env').env;
  22. scrapers = require('./scrapers');
  23. Link = require('./sigchain').Link;
  24. CHECK = require('./display').CHECK;
  25. exports.TrackWrapper = TrackWrapper = (function() {
  26. function TrackWrapper(_arg) {
  27. this.trackee = _arg.trackee, this.tracker = _arg.tracker, this.local = _arg.local, this.remote = _arg.remote;
  28. this.uid = this.trackee.id;
  29. this.sig_chain = this.trackee.sig_chain;
  30. this._ft = null;
  31. }
  32. TrackWrapper.prototype.last = function() {
  33. return this.sig_chain.last();
  34. };
  35. TrackWrapper.prototype.table = function() {
  36. var _ref, _ref1;
  37. return ((_ref = this.sig_chain.table) != null ? (_ref1 = _ref.get(ST.REMOTE_PROOF)) != null ? _ref1.to_dict() : void 0 : void 0) || {};
  38. };
  39. TrackWrapper.prototype.flat_table = function() {
  40. if (this._ft == null) {
  41. this._ft = this.sig_chain.flattened_remote_proofs();
  42. }
  43. return this._ft;
  44. };
  45. TrackWrapper.prototype._check_remote_proof = function(rp) {
  46. var a, b, d, link, proofs_with_service_names, rkp, stub, _ref;
  47. proofs_with_service_names = [PT.twitter, PT.github, PT.reddit, PT.coinbase, PT.hackernews];
  48. if ((rkp = rp.remote_key_proof) == null) {
  49. return new E.RemoteProofError("no 'remote_key_proof field'");
  50. } else if ((stub = scrapers.alloc_stub(rkp.proof_type)) == null) {
  51. return new E.RemoteProofError("Can't allocate a scraper stub for " + rkp.proof_type);
  52. } else if (!stub.check_proof(d = rkp.check_data_json)) {
  53. return new E.RemoteProofError("Bad proof found, for check data: " + (JSON.stringify(d)));
  54. } else if ((link = this.sig_chain.lookup(rp.curr)) == null) {
  55. return new E.RemoteProofError("Failed to find a chain link for " + rp.curr);
  56. } else if (!deq((a = (_ref = link.body()) != null ? _ref.service : void 0), (b = rkp.check_data_json))) {
  57. log.info("JSON obj mismatch: " + (JSON.stringify(a)) + " != " + (JSON.stringify(b)));
  58. return new E.RemoteProofError("The check data was wrong for the signature");
  59. } else {
  60. return null;
  61. }
  62. };
  63. TrackWrapper.prototype._check_track_obj = function(o) {
  64. var a, b, err, rp, _i, _len, _ref, _ref1;
  65. err = null;
  66. if ((a = o.id) !== (b = this.trackee.id)) {
  67. err = new E.UidMismatchError("" + a + " != " + b);
  68. } else if ((a = (_ref = o.basics) != null ? _ref.username : void 0) !== (b = this.trackee.username())) {
  69. err = new E.UsernameMismatchError("" + a + " != " + b);
  70. } else {
  71. _ref1 = o.remote_proofs;
  72. for (_i = 0, _len = _ref1.length; _i < _len; _i++) {
  73. rp = _ref1[_i];
  74. if (err == null) {
  75. err = this._check_remote_proof(rp);
  76. }
  77. }
  78. }
  79. return err;
  80. };
  81. TrackWrapper.prototype._skip_remote_check = function(which) {
  82. var a, last, last_check, prob, ret, rpri, track_cert, _check_all_proofs_ok, _ref;
  83. track_cert = this[which];
  84. log.debug("+ _skip_remote_check for " + which);
  85. rpri = constants.time.remote_proof_recheck_interval;
  86. _check_all_proofs_ok = function(proofs) {
  87. var proof, _i, _len, _ref;
  88. for (_i = 0, _len = proofs.length; _i < _len; _i++) {
  89. proof = proofs[_i];
  90. if (((_ref = proof.remote_key_proof) != null ? _ref.state : void 0) !== 1) {
  91. return false;
  92. }
  93. }
  94. return true;
  95. };
  96. prob = track_cert == null ? "no track cert given" : (last = this.last()) == null ? "no last link found" : (last_check = track_cert.ctime) == null ? "no last_check" : unix_time() - last_check > rpri ? "timed out!" : !(this.sig_chain.is_track_fresh(a = (_ref = track_cert.seq_tail) != null ? _ref.payload_hash : void 0)) ? "we've signed link " + a + " which is no longer a fresh track" : !(_check_all_proofs_ok(track_cert.remote_proofs)) ? "all proofs were not OK" : void 0;
  97. ret = prob != null ? (log.debug("| problem: " + prob), track_cert != null ? log.debug("| track cert: " + (JSON.stringify(track_cert))) : void 0, last != null ? log.debug("| last link: " + (JSON.stringify(last))) : void 0, false) : (log.debug("| Timing was ok: " + (unix_time()) + " - " + last_check + " < " + rpri), true);
  98. log.debug("- _skip_remote_check -> " + ret);
  99. return ret;
  100. };
  101. TrackWrapper.prototype._skip_approval = function(which) {
  102. var a, b, dlen, prob, ret, rkp, row, rp, sub_id, tmp, track_cert;
  103. track_cert = this[which];
  104. log.debug("+ skip_approval(" + which + ")");
  105. dlen = function(d) {
  106. if (d != null) {
  107. return Object.keys(d).length;
  108. } else {
  109. return 0;
  110. }
  111. };
  112. prob = (function() {
  113. var _i, _len, _ref, _ref1, _ref2, _ref3;
  114. if (track_cert == null) {
  115. return "no cert found";
  116. } else if ((a = (_ref = track_cert.key) != null ? _ref.kid : void 0) !== (b = (_ref1 = this.trackee.merkle_data) != null ? _ref1.eldest_kid : void 0)) {
  117. return "trackee changed keys: " + a + " != " + b;
  118. } else if ((a = track_cert.remote_proofs.length) !== (b = this.flat_table().length)) {
  119. return "number of remote IDs changed: " + a + " != " + b;
  120. } else {
  121. tmp = null;
  122. _ref2 = track_cert.remote_proofs;
  123. for (_i = 0, _len = _ref2.length; _i < _len; _i++) {
  124. rp = _ref2[_i];
  125. rkp = rp.remote_key_proof;
  126. row = this.table()[rkp.proof_type];
  127. if (row == null) {
  128. tmp = "Proof deleted: " + (JSON.stringify(rkp.check_data_json));
  129. break;
  130. } else {
  131. if (!row.is_leaf()) {
  132. sub_id = scrapers.alloc_stub(rkp.proof_type).get_sub_id(rkp.check_data_json);
  133. row = row.get(sub_id);
  134. }
  135. if (row == null) {
  136. tmp = "Missing sub ID: " + (JSON.stringify(rkp.proof_type));
  137. break;
  138. }
  139. if (!row.is_leaf()) {
  140. tmp = "Got bad link, it wasn't a link at all (for proof type: " + rkp.proof_type + ")";
  141. break;
  142. } else if (!deq((a = rkp.check_data_json), (b = row != null ? (_ref3 = row.body()) != null ? _ref3.service : void 0 : void 0))) {
  143. tmp = "Remote ID changed: " + (JSON.stringify(a)) + " != " + (JSON.stringify(b));
  144. break;
  145. }
  146. }
  147. }
  148. return tmp;
  149. }
  150. }).call(this);
  151. ret = true;
  152. if (prob != null) {
  153. log.debug("| failure: " + prob);
  154. ret = false;
  155. }
  156. log.debug("- skip_approval(" + which + ") -> " + ret);
  157. return ret;
  158. };
  159. TrackWrapper.prototype.skip_remote_check = function() {
  160. var ret;
  161. ret = this._skip_remote_check('remote') ? constants.skip.REMOTE : this._skip_remote_check('local') ? constants.skip.LOCAL : constants.skip.NONE;
  162. log.debug("| skip_remote_check -> " + ret);
  163. return ret;
  164. };
  165. TrackWrapper.prototype.skip_approval = function() {
  166. var ret;
  167. ret = this._skip_approval('remote') ? constants.skip.REMOTE : this._skip_approval('local') ? constants.skip.LOCAL : constants.skip.NONE;
  168. log.debug("| skip_approval -> " + ret);
  169. return ret;
  170. };
  171. TrackWrapper.prototype.load_local = function(cb) {
  172. var err, value, ___iced_passed_deferral, __iced_deferrals, __iced_k;
  173. __iced_k = __iced_k_noop;
  174. ___iced_passed_deferral = iced.findDeferral(arguments);
  175. log.debug("+ getting local tracking info from DB");
  176. (function(_this) {
  177. return (function(__iced_k) {
  178. __iced_deferrals = new iced.Deferrals(__iced_k, {
  179. parent: ___iced_passed_deferral,
  180. filename: "/home/jacko/node-client/src/trackwrapper.iced",
  181. funcname: "TrackWrapper.load_local"
  182. });
  183. db.get({
  184. type: constants.ids.local_track,
  185. key: _this.uid
  186. }, __iced_deferrals.defer({
  187. assign_fn: (function() {
  188. return function() {
  189. err = arguments[0];
  190. return value = arguments[1];
  191. };
  192. })(),
  193. lineno: 174
  194. }));
  195. __iced_deferrals._fulfill();
  196. });
  197. })(this)((function(_this) {
  198. return function() {
  199. _this.local = value;
  200. log.debug("- completed, with result: " + (!!value));
  201. return cb(err);
  202. };
  203. })(this));
  204. };
  205. TrackWrapper.prototype.store_local = function(cb) {
  206. var err, type, ___iced_passed_deferral, __iced_deferrals, __iced_k;
  207. __iced_k = __iced_k_noop;
  208. ___iced_passed_deferral = iced.findDeferral(arguments);
  209. log.debug("+ storing local track object");
  210. type = constants.ids.local_track;
  211. (function(_this) {
  212. return (function(__iced_k) {
  213. __iced_deferrals = new iced.Deferrals(__iced_k, {
  214. parent: ___iced_passed_deferral,
  215. filename: "/home/jacko/node-client/src/trackwrapper.iced",
  216. funcname: "TrackWrapper.store_local"
  217. });
  218. db.put({
  219. type: type,
  220. key: _this.uid,
  221. value: _this.track_obj
  222. }, __iced_deferrals.defer({
  223. assign_fn: (function() {
  224. return function() {
  225. return err = arguments[0];
  226. };
  227. })(),
  228. lineno: 184
  229. }));
  230. __iced_deferrals._fulfill();
  231. });
  232. })(this)((function(_this) {
  233. return function() {
  234. log.info("" + CHECK + " Wrote tracking info to local database");
  235. log.debug("- stored local track object");
  236. return cb(err);
  237. };
  238. })(this));
  239. };
  240. TrackWrapper.remove_local_track = function(_arg, cb) {
  241. var err, uid, ___iced_passed_deferral, __iced_deferrals, __iced_k;
  242. __iced_k = __iced_k_noop;
  243. ___iced_passed_deferral = iced.findDeferral(arguments);
  244. uid = _arg.uid;
  245. log.debug("+ removing local track object for " + uid);
  246. (function(_this) {
  247. return (function(__iced_k) {
  248. __iced_deferrals = new iced.Deferrals(__iced_k, {
  249. parent: ___iced_passed_deferral,
  250. filename: "/home/jacko/node-client/src/trackwrapper.iced",
  251. funcname: "TrackWrapper.remove_local_track"
  252. });
  253. db.remove({
  254. type: constants.ids.local_track,
  255. key: uid,
  256. optional: true
  257. }, __iced_deferrals.defer({
  258. assign_fn: (function() {
  259. return function() {
  260. return err = arguments[0];
  261. };
  262. })(),
  263. lineno: 197
  264. }));
  265. __iced_deferrals._fulfill();
  266. });
  267. })(this)((function(_this) {
  268. return function() {
  269. log.debug("- removed local track object -> " + err);
  270. return cb(err);
  271. };
  272. })(this));
  273. };
  274. TrackWrapper.prototype.check = function() {
  275. var e;
  276. if (this.local) {
  277. if ((e = this._check_track_obj(this.local)) != null) {
  278. log.warn("Local tracking object was invalid: " + e.message);
  279. this.local = null;
  280. } else {
  281. log.debug("| local track checked out");
  282. }
  283. }
  284. if (this.remote != null) {
  285. if ((e = this._check_track_obj(this.remote)) != null) {
  286. log.warn("Remote tracking object was invalid: " + e.message);
  287. return this.remote = null;
  288. } else {
  289. return log.debug("| remote track checked out");
  290. }
  291. }
  292. };
  293. TrackWrapper.load = function(_arg, cb) {
  294. var err, remote, track, trackee, tracker, uid, ___iced_passed_deferral, __iced_deferrals, __iced_k, _ref;
  295. __iced_k = __iced_k_noop;
  296. ___iced_passed_deferral = iced.findDeferral(arguments);
  297. tracker = _arg.tracker, trackee = _arg.trackee;
  298. uid = trackee.id;
  299. remote = tracker != null ? (_ref = tracker.sig_chain) != null ? _ref.get_track_obj(uid) : void 0 : void 0;
  300. log.debug("+ loading Tracking info w/ remote=" + (!!remote));
  301. track = new TrackWrapper({
  302. uid: uid,
  303. trackee: trackee,
  304. tracker: tracker,
  305. remote: remote
  306. });
  307. (function(_this) {
  308. return (function(__iced_k) {
  309. __iced_deferrals = new iced.Deferrals(__iced_k, {
  310. parent: ___iced_passed_deferral,
  311. filename: "/home/jacko/node-client/src/trackwrapper.iced",
  312. funcname: "TrackWrapper.load"
  313. });
  314. track.load_local(__iced_deferrals.defer({
  315. assign_fn: (function() {
  316. return function() {
  317. return err = arguments[0];
  318. };
  319. })(),
  320. lineno: 224
  321. }));
  322. __iced_deferrals._fulfill();
  323. });
  324. })(this)((function(_this) {
  325. return function() {
  326. if (typeof err !== "undefined" && err !== null) {
  327. track = null;
  328. }
  329. if (track != null) {
  330. track.check();
  331. }
  332. log.debug("- loaded tracking info");
  333. return cb(err, track);
  334. };
  335. })(this));
  336. };
  337. TrackWrapper.prototype.is_tracking = function() {
  338. return {
  339. remote: !!this.remote,
  340. local: !!this.local
  341. };
  342. };
  343. TrackWrapper.prototype.store_remote = function(cb) {
  344. var esc, g, ___iced_passed_deferral, __iced_deferrals, __iced_k;
  345. __iced_k = __iced_k_noop;
  346. ___iced_passed_deferral = iced.findDeferral(arguments);
  347. esc = make_esc(cb, "TrackWrapper::store_remote");
  348. (function(_this) {
  349. return (function(__iced_k) {
  350. __iced_deferrals = new iced.Deferrals(__iced_k, {
  351. parent: ___iced_passed_deferral,
  352. filename: "/home/jacko/node-client/src/trackwrapper.iced",
  353. funcname: "TrackWrapper.store_remote"
  354. });
  355. _this.tracker.gen_track_proof_gen({
  356. uid: _this.uid,
  357. track_obj: _this.track_obj
  358. }, esc(__iced_deferrals.defer({
  359. assign_fn: (function() {
  360. return function() {
  361. return g = arguments[0];
  362. };
  363. })(),
  364. lineno: 238
  365. })));
  366. __iced_deferrals._fulfill();
  367. });
  368. })(this)((function(_this) {
  369. return function() {
  370. (function(__iced_k) {
  371. __iced_deferrals = new iced.Deferrals(__iced_k, {
  372. parent: ___iced_passed_deferral,
  373. filename: "/home/jacko/node-client/src/trackwrapper.iced",
  374. funcname: "TrackWrapper.store_remote"
  375. });
  376. g.run(esc(__iced_deferrals.defer({
  377. lineno: 239
  378. })));
  379. __iced_deferrals._fulfill();
  380. })(function() {
  381. log.info("" + CHECK + " Wrote tracking info to remote keybase.io server");
  382. return cb(null);
  383. });
  384. };
  385. })(this));
  386. };
  387. TrackWrapper.prototype.store_track = function(_arg, cb) {
  388. var do_remote, esc, ___iced_passed_deferral, __iced_deferrals, __iced_k;
  389. __iced_k = __iced_k_noop;
  390. ___iced_passed_deferral = iced.findDeferral(arguments);
  391. do_remote = _arg.do_remote;
  392. esc = make_esc(cb, "TrackWrapper::store_track");
  393. log.debug("+ track user (remote=" + do_remote + ")");
  394. this.track_obj = this.trackee.gen_track_obj();
  395. log.debug("| object generated: " + (JSON.stringify(this.track_obj)));
  396. (function(_this) {
  397. return (function(__iced_k) {
  398. if (do_remote) {
  399. (function(__iced_k) {
  400. __iced_deferrals = new iced.Deferrals(__iced_k, {
  401. parent: ___iced_passed_deferral,
  402. filename: "/home/jacko/node-client/src/trackwrapper.iced",
  403. funcname: "TrackWrapper.store_track"
  404. });
  405. _this.store_remote(esc(__iced_deferrals.defer({
  406. lineno: 251
  407. })));
  408. __iced_deferrals._fulfill();
  409. })(__iced_k);
  410. } else {
  411. (function(__iced_k) {
  412. __iced_deferrals = new iced.Deferrals(__iced_k, {
  413. parent: ___iced_passed_deferral,
  414. filename: "/home/jacko/node-client/src/trackwrapper.iced",
  415. funcname: "TrackWrapper.store_track"
  416. });
  417. _this.store_local(esc(__iced_deferrals.defer({
  418. lineno: 253
  419. })));
  420. __iced_deferrals._fulfill();
  421. })(__iced_k);
  422. }
  423. });
  424. })(this)((function(_this) {
  425. return function() {
  426. log.debug("- tracked user");
  427. return cb(null);
  428. };
  429. })(this));
  430. };
  431. return TrackWrapper;
  432. })();
  433. }).call(this);