sea.js 2.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657
  1. var shim = require('./shim');
  2. // Practical examples about usage found in tests.
  3. var SEA = require('./root');
  4. SEA.work = require('./work');
  5. SEA.sign = require('./sign');
  6. SEA.verify = require('./verify');
  7. SEA.encrypt = require('./encrypt');
  8. SEA.decrypt = require('./decrypt');
  9. SEA.opt.aeskey = require('./aeskey'); // not official!
  10. SEA.random = SEA.random || shim.random;
  11. // This is Buffer used in SEA and usable from Gun/SEA application also.
  12. // For documentation see https://nodejs.org/api/buffer.html
  13. SEA.Buffer = SEA.Buffer || require('./buffer');
  14. // These SEA functions support now ony Promises or
  15. // async/await (compatible) code, use those like Promises.
  16. //
  17. // Creates a wrapper library around Web Crypto API
  18. // for various AES, ECDSA, PBKDF2 functions we called above.
  19. // Calculate public key KeyID aka PGPv4 (result: 8 bytes as hex string)
  20. SEA.keyid = SEA.keyid || (async (pub) => {
  21. try {
  22. // base64('base64(x):base64(y)') => Buffer(xy)
  23. const pb = Buffer.concat(
  24. pub.replace(/-/g, '+').replace(/_/g, '/').split('.')
  25. .map((t) => Buffer.from(t, 'base64'))
  26. )
  27. // id is PGPv4 compliant raw key
  28. const id = Buffer.concat([
  29. Buffer.from([0x99, pb.length / 0x100, pb.length % 0x100]), pb
  30. ])
  31. const sha1 = await sha1hash(id)
  32. const hash = Buffer.from(sha1, 'binary')
  33. return hash.toString('hex', hash.length - 8) // 16-bit ID as hex
  34. } catch (e) {
  35. console.log(e)
  36. throw e
  37. }
  38. });
  39. // all done!
  40. // Obviously it is missing MANY necessary features. This is only an alpha release.
  41. // Please experiment with it, audit what I've done so far, and complain about what needs to be added.
  42. // SEA should be a full suite that is easy and seamless to use.
  43. // Again, scroll naer the top, where I provide an EXAMPLE of how to create a user and sign in.
  44. // Once logged in, the rest of the code you just read handled automatically signing/validating data.
  45. // But all other behavior needs to be equally easy, like opinionated ways of
  46. // Adding friends (trusted public keys), sending private messages, etc.
  47. // Cheers! Tell me what you think.
  48. var Gun = (SEA.window||{}).Gun || require((typeof MODULE == "undefined"?'.':'')+'./gun', 1);
  49. Gun.SEA = SEA;
  50. SEA.GUN = SEA.Gun = Gun;
  51. module.exports = SEA