paulmillr / noble-ed25519

Fastest 4KB JS implementation of ed25519 signatures
https://paulmillr.com/noble
MIT License
420 stars 51 forks source link

Case ArrayBuffer as byte argument #60

Closed JhonnyJason closed 2 years ago

JhonnyJason commented 2 years ago

https://github.com/paulmillr/noble-ed25519/blob/edeb48a685c386b8b2d5b4be6b7dc5c432839e05/index.ts#L842

I sometimes treat plain Buffers and ArrayBuffers as Uint8Arrays and pass them directly to functions.

The ensureBytes function helps a lot here and in Nodejs it works for all cases because Buffer is an instance of Uint8Array. ArrayBuffer in the browser is not - so I suggest to add another line:

-const bytes =  hex instanceof Uint8Array? Uint8Array.from(hex) : hexToBytes(hex);
+const isBytes = hex instanceof Uint8Array || hex isinstanceof ArrayBuffer
+const bytes =  isBytes? Uint8Array.from(hex) : hexToBytes(hex)

If it makes sense I could creae a pull request for it.

Fast fix is to take care to only pass Uint8Arrays to the functions when using the browser.

Cheers for now!

paulmillr commented 2 years ago

Not really — they are different. We're using ui8a everywhere for consistency. I'd drop nodejs buffers if it was for me, because they're insecure and shitty.