Skip to content
Closed
Changes from 2 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
35 changes: 29 additions & 6 deletions packages/crypto/src/lib/crypto.ts
Original file line number Diff line number Diff line change
Expand Up @@ -104,6 +104,7 @@ export const unloadModules = () => {

/**
* Encrypt data with a BLS public key.
* We are using G1 for encryption and G2 for signatures
*
* @param publicKey hex-encoded string of the BLS public key to encrypt with
* @param data Uint8Array of the data to encrypt
Expand All @@ -114,12 +115,34 @@ export const encrypt = (
publicKey: string,
data: Uint8Array,
identity: Uint8Array
): string => {
return blsSdk.encrypt(
publicKey,
uint8arrayToString(data, 'base64'),
uint8arrayToString(identity, 'base64')
);
): Promise<string> => {

const publicKey = Buffer.from(publicKeyHex, 'hex');

/**
* Our system uses BLS12-381 on the G1 curve for encryption.
* However, on the SDK side (this function), we expect the public key
* to use the G2 curve for signature purposes, hence the switch on public key length.
*
* The G2 curve, `Bls12381G2`, is typically associated with signature generation/verification,
* while G1 is associated with encryption. Here, the length of the public key determines how
* we handle the encryption and the format of the returned encrypted message.
*/

if (publicKeyHex.replace('0x', '').length !== 96) {
throw new InvalidParamType(
{
info: {
publicKeyHex,
},
},
`Invalid public key length. Expecting 96 characters, got ${publicKeyHex.replace('0x', '').length} instead.`
);
}
return Buffer.from(
await blsEncrypt('Bls12381G2', publicKey, message, identity)
Copy link

Choose a reason for hiding this comment

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

The parameter message in the blsEncrypt function call should be replaced with data to correctly use the function parameter defined in the method signature. This ensures consistency with the input parameters and prevents potential errors. Consider updating the line to:

await blsEncrypt('Bls12381G2', publicKey, data, identity)

This change aligns the function call with the method's declared parameters.

Spotted by Graphite Reviewer

Is this helpful? React 👍 or 👎 to let us know.

Copy link

Choose a reason for hiding this comment

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

The blsEncrypt function is called but appears to be undefined in this scope. Please ensure it's properly imported from the appropriate module or defined within this file. If it's a custom implementation, consider adding a comment explaining its origin or providing a reference to its definition.

Spotted by Graphite Reviewer

Is this helpful? React 👍 or 👎 to let us know.

).toString('base64');

};

/**
Expand Down
Loading