Emurgo / csl-mobile-bridge

React-native bindings for Emurgo's cardano-serialization-lib (Cardano haskell Shelley)
MIT License
23 stars 12 forks source link

Private key from entropy cannot be created without a password #112

Open trimixlover opened 1 week ago

trimixlover commented 1 week ago

What we witnessed is that when bip32PrivateKeyFromBip39Entropy is called with "" (empty string) for passphrase it is interpreted as null value when passing to ios and android bindings and because of that it errors out. To mitigate that issue, what we did is, we applied the following patch:

diff --git a/node_modules/@emurgo/csl-mobile-bridge/android/src/main/java/io/emurgo/rnhaskellshelley/HaskellShelleyModule.java b/node_modules/@emurgo/csl-mobile-bridge/android/src/main/java/io/emurgo/rnhaskellshelley/HaskellShelleyModule.java
index c2916a5..dd51e9e 100644
--- a/node_modules/@emurgo/csl-mobile-bridge/android/src/main/java/io/emurgo/rnhaskellshelley/HaskellShelleyModule.java
+++ b/node_modules/@emurgo/csl-mobile-bridge/android/src/main/java/io/emurgo/rnhaskellshelley/HaskellShelleyModule.java
@@ -1130,6 +1130,10 @@ public class HaskellShelleyModule extends ReactContextBaseJavaModule {

     @ReactMethod
     public final void csl_bridge_bip32PrivateKeyFromBip39Entropy(String entropy, String password, Promise promise) {
+        if (password  == null) {
+            password = ""
+        }
+
         Native.I
             .csl_bridge_bip32PrivateKeyFromBip39Entropy(Base64.decode(entropy, Base64.DEFAULT), Base64.decode(password, Base64.DEFAULT))
             .map(RPtr::toJs)
diff --git a/node_modules/@emurgo/csl-mobile-bridge/ios/HaskellShelley.m b/node_modules/@emurgo/csl-mobile-bridge/ios/HaskellShelley.m
index 0097431..6c3bc75 100644
--- a/node_modules/@emurgo/csl-mobile-bridge/ios/HaskellShelley.m
+++ b/node_modules/@emurgo/csl-mobile-bridge/ios/HaskellShelley.m
@@ -1589,8 +1589,13 @@ + (void)csl_bridge_initialize
     }] exec:selfPtr andResolve:resolve orReject:reject];
 }

-RCT_EXPORT_METHOD(csl_bridge_bip32PrivateKeyFromBip39Entropy:(nonnull NSString *)entropyVal withPassword:(nonnull NSString *)passwordVal withResolve:(RCTPromiseResolveBlock)resolve andReject:(RCTPromiseRejectBlock)reject)
+RCT_EXPORT_METHOD(csl_bridge_bip32PrivateKeyFromBip39Entropy:(nonnull NSString *)entropyVal withPassword:(nullable NSString *)passwordVal withResolve:(RCTPromiseResolveBlock)resolve andReject:(RCTPromiseRejectBlock)reject)
 {
+    // Set passwordVal to an empty string if it's null
+    if (passwordVal == nil) {
+        passwordVal = @"";
+    }
+
     [[CSLCSafeOperation new:^NSString*(NSArray* params, CharPtr* error) {
         RPtr result;
         NSData* dataEntropy = [NSData fromBase64:[params objectAtIndex:0]];

To reproduce the issue, there is not much you need to do. Just execute this piece of code:

Bip32PrivateKey.from_bip39_entropy(
        Buffer.from(mnemonicToEntropy(mnemonic), "hex"),
        Buffer.from(""))

Hope this helps. Also if you feel like there is blunder in usage and that this is in fact not an issue, please let me know.

lisicky commented 1 day ago

Hi @trimixlover! Could you try to pass Uint8Array.from([]) instead Buffer.from("") ? By default it should not to produce "null" on empty byte array.