PeculiarVentures / webcrypto-liner

webcrypto-liner is a polyfill that let's down-level User Agents (like IE/Edge) use libraries that depend on WebCrypto. (Keywords: Javascript, WebCrypto, Shim, Polyfill)
MIT License
148 stars 26 forks source link

Can't find elliptic js script #79

Closed matan21197 closed 3 years ago

matan21197 commented 3 years ago

when including both webcrypto-liner and elliptic js the following error is returned when trying to use ECDH functions: Unhandled Promise Rejection: Error: Cannot implement EC mechanism. Add 'https://peculiarventures.github.io/pv-webcrypto-tests/src/elliptic.js' script to your project. The error is returned when running on safari

rmhrisk commented 3 years ago

Please provide minimal example code to reproduce.

What user agent?

matan21197 commented 3 years ago

i am running from safari 13.1 on ios 13.5 and get the error when executing await crypto.subtle.importKey( "jwk", key, { name: "ECDH", namedCurve: 'p-521' }, false, [] ) the code works when including the elliptic.js script manually with a script tag, but i when I use import 'webcrypto-liner' like I want to, it outputs the error above

rmhrisk commented 3 years ago

Please provide minimal example code to reproduce.

donskov commented 3 years ago

Hello @matan21197 . I prepared for you example of how to use webcrypto-liner with import.

Install npm modules:

npm install asmcrypto.js elliptic webcrypto-liner

Import modules to your code (using dynamic imports):

function isFirefox() {
  return /firefox/i.test(window.navigator.userAgent);
}

function isEdge() {
  return /edge\/([\d.]+)/i.test(window.navigator.userAgent);
}

function isIE() {
  return !!(window.document as any).documentMode;
}

if (isEdge() || isFirefox() || isIE()) {
  if (!window['asmCrypto']) {
    window['asmCrypto'] = await import('asmcrypto.js/asmcrypto.all.es8.js');
  }

  if (!window['elliptic']) {
    window['elliptic'] = (await import('elliptic/lib/elliptic.js')).default;
  }

  await import('webcrypto-liner/build/webcrypto-liner.shim.mjs');
}

This code works with our web-components. Look, maybe you will be interested.

Thanks!

matan21197 commented 3 years ago

@donskov Thank you so much! The issue was fixed by adding: `import * as elliptic from 'elliptic';

window['elliptic'] = elliptic` Maybe it should be added to the readme that elliptic and asmCrypto need to be added to the window Again, thank you very much