-
Notifications
You must be signed in to change notification settings - Fork 225
resolve typescript buffer-to-uint8array type mismatches #445
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
base: main
Are you sure you want to change the base?
Changes from all commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
| Original file line number | Diff line number | Diff line change | ||||
|---|---|---|---|---|---|---|
|
|
@@ -103,7 +103,7 @@ export const isEventMatchingFilter = (filter: SubscriptionFilter) => (event: Eve | |||||
| } | ||||||
|
|
||||||
| export const getEventHash = async (event: Event | UnidentifiedEvent | UnsignedEvent): Promise<string> => { | ||||||
| const id = await secp256k1.utils.sha256(Buffer.from(JSON.stringify(serializeEvent(event)))) | ||||||
| const id = await secp256k1.utils.sha256(new Uint8Array(Buffer.from(JSON.stringify(serializeEvent(event))))) | ||||||
|
|
||||||
| return Buffer.from(id).toString('hex') | ||||||
| } | ||||||
|
|
@@ -160,15 +160,15 @@ export const encryptKind4Event = ( | |||||
| receiverPubkey: Pubkey, | ||||||
| ) => (event: UnsignedEvent): UnsignedEvent => { | ||||||
| const key = secp256k1 | ||||||
| .getSharedSecret(senderPrivkey, `02${receiverPubkey}`, true) | ||||||
| .getSharedSecret(typeof senderPrivkey === 'string' ? senderPrivkey : new Uint8Array(senderPrivkey), `02${receiverPubkey}`, true) | ||||||
| .subarray(1) | ||||||
|
|
||||||
| const iv = getRandomValues(new Uint8Array(16)) | ||||||
|
|
||||||
| // deepcode ignore InsecureCipherNoIntegrity: NIP-04 Encrypted Direct Message uses aes-256-cbc | ||||||
| const cipher = createCipheriv( | ||||||
| 'aes-256-cbc', | ||||||
| Buffer.from(key), | ||||||
| new Uint8Array(key), | ||||||
|
||||||
| new Uint8Array(key), | |
| key, |
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.
new Uint8Array(Buffer.from(...))creates a full copy of the serialized event bytes. IfgetEventHashis on a hot path and events can be large, consider using a zero-copyUint8Arrayview over the Buffer’s underlyingArrayBufferwhen it’s anArrayBuffer(and only fall back to copying when the backing buffer is aSharedArrayBuffer). This preserves the type fix while avoiding an extra allocation/copy.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.
@Mohit-Davar Agree with Copilot here.