@@ -104,6 +104,7 @@ export const unloadModules = () => {
104104
105105/**
106106 * Encrypt data with a BLS public key.
107+ * We are using G1 for encryption and G2 for signatures
107108 *
108109 * @param publicKey hex-encoded string of the BLS public key to encrypt with
109110 * @param data Uint8Array of the data to encrypt
@@ -114,12 +115,35 @@ export const encrypt = (
114115 publicKey : string ,
115116 data : Uint8Array ,
116117 identity : Uint8Array
117- ) : string => {
118- return blsSdk . encrypt (
119- publicKey ,
120- uint8arrayToString ( data , 'base64' ) ,
121- uint8arrayToString ( identity , 'base64' )
122- ) ;
118+ ) : Promise < string > => {
119+
120+ const publicKey = Buffer . from ( publicKeyHex , 'hex' ) ;
121+
122+ /**
123+ * Our system uses BLS12-381 on the G1 curve for encryption.
124+ * However, on the SDK side (this function), we expect the public key
125+ * to use the G2 curve for signature purposes, hence the switch on public key length.
126+ *
127+ * The G2 curve, `Bls12381G2`, is typically associated with signature generation/verification,
128+ * while G1 is associated with encryption. Here, the length of the public key determines how
129+ * we handle the encryption and the format of the returned encrypted message.
130+ */
131+ switch ( publicKeyHex . replace ( '0x' , '' ) . length ) {
132+
133+ /**
134+ * @deprecated - not sure if this is still used/needed
135+ */
136+ case 218 :
137+ return Buffer . from (
138+ await blsEncrypt ( 'Bls12381G2' , publicKey , message , identity )
139+ ) . toString ( 'hex' ) ;
140+ case 96 :
141+ return Buffer . from (
142+ await blsEncrypt ( 'Bls12381G2' , publicKey , message , identity )
143+ ) . toString ( 'base64' ) ;
144+ default :
145+ return '' ;
146+ }
123147} ;
124148
125149/**
0 commit comments