Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Store key using SubtleCrypto API in browser #1013

Open
joepio opened this issue Nov 17, 2024 · 2 comments
Open

Store key using SubtleCrypto API in browser #1013

joepio opened this issue Nov 17, 2024 · 2 comments

Comments

@joepio
Copy link
Member

joepio commented Nov 17, 2024

  • We should use the importKey function with extractable set to false to provide more security.
  • Support for ed25519 is not great, mostly behind flags.
@Polleps
Copy link
Member

Polleps commented Nov 20, 2024

15% 😭. But we could future proof by checking support and conditionally using it while sticking to local storage for browsers that don't. Also we need to think about how we want to go about this, I believe WebCrypto can not return the private key, only sign data with it meaning @tomic/lib needs to use it directly. Right now storing the key is handled by browser because that is not really the concern of lib. WebCrypto might not be available in all contexts that lib should be able to run in. It is also not always appropriate for the key to be stored in general, for example on a server that has a store instance for each connected user.

@Polleps
Copy link
Member

Polleps commented Dec 4, 2024

I took a look at the api's and it works a bit different from what I thought. The API gives you a CryptoKey object that can be stored in IndexDB. This means you're not limited to just one stored key. Still I don't think actually storing the key should be the concern of @tomic/lib but we could export some helper functions to make it simple.

However
I ran into another issue when experimenting. I can't seem to make importKey() work with our private keys.
The following code gives me an error saying it can't create the key with the specified usages.

const toArrayBuf = (str: string) => {
  const buf = new ArrayBuffer(str.length);
  const bufView = new Uint8Array(buf);

  for (let i = 0, strLen = str.length; i < strLen; i++) {
    bufView[i] = str.charCodeAt(i);
  }

  return buf;
};

const key = await crypto.subtle.importKey(
  'raw',
  toArrayBuf(atob(agent.privateKey)),
  {  name: 'Ed25519' },
   false,
   ['sign'],
);

When generating an ed25519 keypair with crypto.subtle.generateKey() it works just fine so it's likely something to do with the format the private key is stored in.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

No branches or pull requests

2 participants