Skip to content

Conversation

@apporc
Copy link
Contributor

@apporc apporc commented May 29, 2025

No description provided.

@apporc apporc changed the title Support webauthn key signature verification and recovery Support wa key signature verification and recovery May 29, 2025
@apporc apporc marked this pull request as draft May 29, 2025 08:51
@apporc apporc changed the title Support wa key signature verification and recovery Support wa key signature verification May 30, 2025
@apporc apporc marked this pull request as ready for review May 30, 2025 10:03
*/
export function recover(signature: Uint8Array, message: Uint8Array, type: string) {
if (type === KeyType.WA) {
throw new Error(`can't recover webauthn public keys, please use @wharfkit/webauthn.`)
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

👍

* This is suitable for cryptographic operations like verification.
*/
getCompressedKeyBytes(): Uint8Array {
return this.data.array.subarray(0, 33)
Copy link
Contributor

@dafuga dafuga May 31, 2025

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Just so the result is mutable, I would probably do this instead:

Suggested change
return this.data.array.subarray(0, 33)
return new Uint8Array(this.data.array.subarray(0, 33))

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I don't think one would need the result as mutable.

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

It's good practice to always return something that is mutable in a situation like this one because other developers might expect it to be. With your code, if someone takes the returned compressed key and modifies it for whatever reason, that will also modify the this.data value which will modify the public key itself. I don't think we ever want that to happen so I would return a new array like in my above suggestion.

Copy link
Contributor Author

@apporc apporc Jun 7, 2025

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Yeah, you are right, we should make the code robust. I didn't realize that you can unintentionally mutate the original Uint8Array while mutating the returned value. I mean developers wouldn't need to mutate the returned value, which is functionally meaningless, but you are right, what if he made a mistake.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Since the PR is merged, i added another PR to switch to use slice, i think it will solve the issue.
#117

* Returns the core 33-byte compressed public key data as a Uint8Array.
* This is suitable for cryptographic operations like verification.
*/
getCompressedKeyBytes(): Uint8Array {
Copy link
Contributor

@dafuga dafuga May 31, 2025

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

is it worth throwing an error when this.data.array is less than 33 bytes? If not, maybe we add a check in the constructor that throws an error when this.data is not the correct length?

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I am not sure whether we should improve the error handling in this PR. It would change the designed behavior of the library. Illegal bytes passed in situation was not considered at all in this library.

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Yeah, maybe it should be something that this library checks, but you're right that doesn't need to be part of this PR 👍

@aaroncox aaroncox merged commit 9b6fabd into master Jun 6, 2025
6 checks passed
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

4 participants