shim.js 1.5 KB

1234567891011121314151617181920212223242526272829303132333435363738
  1. const SEA = require('./root')
  2. const Buffer = require('./buffer')
  3. const api = {Buffer: Buffer}
  4. var o = {};
  5. if(SEA.window){
  6. api.crypto = window.crypto || window.msCrypto;
  7. api.subtle = (api.crypto||o).subtle || (api.crypto||o).webkitSubtle;
  8. api.TextEncoder = window.TextEncoder;
  9. api.TextDecoder = window.TextDecoder;
  10. api.random = (len) => Buffer.from(api.crypto.getRandomValues(new Uint8Array(Buffer.alloc(len))))
  11. }
  12. if(!api.crypto){try{
  13. var crypto = require('crypto', 1);
  14. const { subtle } = require('@trust/webcrypto', 1) // All but ECDH
  15. const { TextEncoder, TextDecoder } = require('text-encoding', 1)
  16. Object.assign(api, {
  17. crypto,
  18. subtle,
  19. TextEncoder,
  20. TextDecoder,
  21. random: (len) => Buffer.from(crypto.randomBytes(len))
  22. });
  23. //try{
  24. const WebCrypto = require('node-webcrypto-ossl', 1)
  25. api.ossl = new WebCrypto({directory: 'ossl'}).subtle // ECDH
  26. //}catch(e){
  27. //console.log("node-webcrypto-ossl is optionally needed for ECDH, please install if needed.");
  28. //}
  29. }catch(e){
  30. console.log("@trust/webcrypto and text-encoding are not included by default, you must add it to your package.json!");
  31. console.log("node-webcrypto-ossl is temporarily needed for ECDSA signature verification, and optionally needed for ECDH, please install if needed (currently necessary so add them to your package.json for now).");
  32. TRUST_WEBCRYPTO_OR_TEXT_ENCODING_NOT_INSTALLED;
  33. }}
  34. module.exports = api