@@ -45,6 +45,7 @@ export interface BlsSignatureShare {
4545
4646/**
4747 * Encrypt data with a BLS public key.
48+ * We are using G1 for encryption and G2 for signatures
4849 *
4950 * @param publicKeyHex hex-encoded string of the BLS public key to encrypt with
5051 * @param message Uint8Array of the data to encrypt
@@ -58,18 +59,29 @@ export const encrypt = async (
5859) : Promise < string > => {
5960 const publicKey = Buffer . from ( publicKeyHex , 'hex' ) ;
6061
61- switch ( publicKeyHex . replace ( '0x' , '' ) . length ) {
62- case 218 :
63- return Buffer . from (
64- await blsEncrypt ( 'Bls12381G2' , publicKey , message , identity )
65- ) . toString ( 'hex' ) ;
66- case 96 :
67- return Buffer . from (
68- await blsEncrypt ( 'Bls12381G2' , publicKey , message , identity )
69- ) . toString ( 'base64' ) ;
70- default :
71- return '' ;
62+ /**
63+ * Our system uses BLS12-381 on the G1 curve for encryption.
64+ * However, on the SDK side (this function), we expect the public key
65+ * to use the G2 curve for signature purposes, hence the switch on public key length.
66+ *
67+ * The G2 curve, `Bls12381G2`, is typically associated with signature generation/verification,
68+ * while G1 is associated with encryption. Here, the length of the public key determines how
69+ * we handle the encryption and the format of the returned encrypted message.
70+ */
71+ if ( publicKeyHex . replace ( '0x' , '' ) . length !== 96 ) {
72+ throw new InvalidParamType (
73+ {
74+ info : {
75+ publicKeyHex,
76+ } ,
77+ } ,
78+ `Invalid public key length. Expecting 96 characters, got ${ publicKeyHex . replace ( '0x' , '' ) . length } instead.`
79+ ) ;
7280 }
81+ return Buffer . from (
82+ await blsEncrypt ( 'Bls12381G2' , publicKey , message , identity )
83+ ) . toString ( 'base64' ) ;
84+
7385} ;
7486
7587/**
0 commit comments