sea.js 3.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081
  1. // Old Code...
  2. const __gky10 = require('./shim')
  3. const crypto = __gky10.crypto
  4. const subtle = __gky10.subtle
  5. const ossl = __gky10.ossl
  6. const TextEncoder = __gky10.TextEncoder
  7. const TextDecoder = __gky10.TextDecoder
  8. const getRandomBytes = __gky10.random
  9. const EasyIndexedDB = require('./indexed')
  10. const Buffer = require('./buffer')
  11. var settings = require('./settings');
  12. const __gky11 = require('./settings')
  13. const pbKdf2 = __gky11.pbkdf2
  14. const ecdsaKeyProps = __gky11.ecdsa.pair
  15. const ecdsaSignProps = __gky11.ecdsa.sign
  16. const ecdhKeyProps = __gky11.ecdh
  17. const keysToEcdsaJwk = __gky11.jwk
  18. const sha1hash = require('./sha1')
  19. const sha256hash = require('./sha256')
  20. const recallCryptoKey = require('./remember')
  21. const parseProps = require('./parse')
  22. // Practical examples about usage found from ./test/common.js
  23. const SEA = require('./root');
  24. SEA.work = require('./work');
  25. SEA.sign = require('./sign');
  26. SEA.verify = require('./verify');
  27. SEA.encrypt = require('./encrypt');
  28. SEA.decrypt = require('./decrypt');
  29. SEA.random = SEA.random || getRandomBytes;
  30. // This is easy way to use IndexedDB, all methods are Promises
  31. // Note: Not all SEA interfaces have to support this.
  32. SEA.EasyIndexedDB = EasyIndexedDB;
  33. // This is Buffer used in SEA and usable from Gun/SEA application also.
  34. // For documentation see https://nodejs.org/api/buffer.html
  35. SEA.Buffer = SEA.Buffer || Buffer;
  36. // These SEA functions support now ony Promises or
  37. // async/await (compatible) code, use those like Promises.
  38. //
  39. // Creates a wrapper library around Web Crypto API
  40. // for various AES, ECDSA, PBKDF2 functions we called above.
  41. // Calculate public key KeyID aka PGPv4 (result: 8 bytes as hex string)
  42. SEA.keyid = SEA.keyid || (async (pub) => {
  43. try {
  44. // base64('base64(x):base64(y)') => Buffer(xy)
  45. const pb = Buffer.concat(
  46. Buffer.from(pub, 'base64').toString('utf8').split(':')
  47. .map((t) => Buffer.from(t, 'base64'))
  48. )
  49. // id is PGPv4 compliant raw key
  50. const id = Buffer.concat([
  51. Buffer.from([0x99, pb.length / 0x100, pb.length % 0x100]), pb
  52. ])
  53. const sha1 = await sha1hash(id)
  54. const hash = Buffer.from(sha1, 'binary')
  55. return hash.toString('hex', hash.length - 8) // 16-bit ID as hex
  56. } catch (e) {
  57. console.log(e)
  58. throw e
  59. }
  60. });
  61. // all done!
  62. // Obviously it is missing MANY necessary features. This is only an alpha release.
  63. // Please experiment with it, audit what I've done so far, and complain about what needs to be added.
  64. // SEA should be a full suite that is easy and seamless to use.
  65. // Again, scroll naer the top, where I provide an EXAMPLE of how to create a user and sign in.
  66. // Once logged in, the rest of the code you just read handled automatically signing/validating data.
  67. // But all other behavior needs to be equally easy, like opinionated ways of
  68. // Adding friends (trusted public keys), sending private messages, etc.
  69. // Cheers! Tell me what you think.
  70. var Gun = (SEA.window||{}).Gun || require('./gun', 1);
  71. Gun.SEA = SEA;
  72. SEA.Gun = Gun;
  73. module.exports = SEA