sea.js 2.3 KB

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