-
Notifications
You must be signed in to change notification settings - Fork 81
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
feat: add support for Uint8Array #320
feat: add support for Uint8Array #320
Conversation
added support for Uint8Array inside prepareSecretKey.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
couple of things:
- Does
binaryLikeToArrayBuffer
help us here? i.e. should we try it on more than justtypeof key === 'string'
and if it throws, then this function throws too? It might be the better way to go. - One small change to error message, now that we will also handle
TypedArray
s - Add a test with a
Uint8Array
that would have failed without your code changes. (look in example appimportKey()
foraes
tests)
src/keys.ts
Outdated
if (key instanceof Uint8Array) { | ||
return key.buffer; | ||
} | ||
|
||
throw new Error( | ||
'Invalid argument type for "key". Need ArrayBuffer, KeyObject, CryptoKey, string' |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Can you add TypedArray
or similar to the error message?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Thank you! Done here: 287ed66
src/keys.ts
Outdated
@@ -448,6 +448,10 @@ function prepareSecretKey( | |||
return binaryLikeToArrayBuffer(key, encoding); | |||
} | |||
|
|||
if (key instanceof Uint8Array) { |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
binaryLikeToArrayBuffer
here or earlier?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Thank you! Done here: 287ed66
All done! |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Looking good, almost there...
src/keys.ts
Outdated
@@ -444,12 +444,12 @@ function prepareSecretKey( | |||
return key; | |||
} | |||
|
|||
if (typeof key === 'string') { | |||
if (typeof key === 'string' || key instanceof Uint8Array) { |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Can we fit the other TypedArray
types in here?
Maybe just call binaryLikeToArrayBuffer
instead of type conditionals, and see if it throws.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Ye I tried it but we have some types missmatch on the first argument of binaryLikeToArrayBuffer(key, encoding)
:
The argument of type 'string | KeyObject | CryptoKey | RandomTypedArrays' is not assignable to the parameter of type 'BinaryLikeNode'.
The type 'KeyObject' is not assignable to the type 'BinaryLikeNode'.
The property 'equals' is missing in the type 'KeyObject', but is mandatory in the type 'import(“crypto”).KeyObject'.ts(2345)
And also
The argument of type 'string | CryptoKey | RandomTypedArrays' is not assignable to the parameter of type 'BinaryLikeNode'.
The type 'CryptoKey' is not assignable to the type 'BinaryLikeNode'.
The 'CryptoKey' type is missing the following properties of the 'KeyObject' type: export, equalsts(2345)
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I also tried the following one, but binaryLikeToArrayBuffer
doesn't throw any errors if the key
isn't a valid one, it just return the raw input.
if (!(key instanceof KeyObject) && !(key instanceof CryptoKey)) {
return binaryLikeToArrayBuffer(key, encoding);
}
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I wouldn't use it here, but rather after the two object types
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
or, change the types in BinaryLikeNode
- these types are a mess :(
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
TypedArray should be moved out of random, too ... le sigh
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
🚀
| Uint16Array | ||
| Uint32Array; | ||
export function getRandomValues(data: RandomTypedArrays) { | ||
export function getRandomValues(data: TypedArray) { |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
neither Float32Array nor Float64Array should be allowable here, that's why RandomTypedArrays
existed. this should be throwing a type error and should not have compiled with tsc
since it conflicts with Node's getRandomValues
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Oh, that's a good point, @boorad what do you think? I can create a different PR were we export RandomTypedArrays
again and use it for getRandomValues
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
yes plz 🙏
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
To be fair I don't think the change here is super harmful but I'm more curious how tsc was able to compile given the type error. I think the bigger concern is why there wasn't a type error here
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I'll create anyway the PR, because even if typescript doesn't complain about that it's a wrong type definition
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Done here: #330
This PR adds support for keys such as uint8array for the
prepareSecretKey
method, I noticed while trying to generate a mnemonic with the following code:That due to the checks within
prepareSecretKey
, keys in the format of Uint8Array were not supported and this generated the following exception: