react-native-keychain: [Android] setGenericPassword not working on Samsung devices: "Attempt to invoke virtual method 'getPublicKey()' on a null object reference"

First of all, thanks for this library. It has helped me a lot already.

However, i am running into an issue on Samsung devices when calling setGenericPassword().

I am currently invoking the method like this:

Keychain.setGenericPassword('general', value, {
    accessible: Keychain.ACCESSIBLE.WHEN_UNLOCKED,
    accessControl:
      hasBiometrics && isBiometricsProtected
        ? Keychain.ACCESS_CONTROL.BIOMETRY_CURRENT_SET_OR_DEVICE_PASSCODE
        : undefined,
    service: username ? `${service}_${username}` : service,
  });

However when i do this i am getting this error on Android:

Error: Unknown error: Attempt to invoke virtual method 'java.security.PublicKey java.security.cert.Certificate.getPublicKey()' on a null object reference
    at Object.promiseMethodWrapper [as setGenericPasswordForOptions] (NativeModules.js:103)
    at Object.setGenericPassword (index.js:179)
    at setKeychain$ (Storage.ts:37)
    at tryCatch (runtime.js:63)
    at Generator.invoke [as _invoke] (runtime.js:293)
    at Generator.next (runtime.js:118)
    at tryCatch (runtime.js:63)
    at invoke (runtime.js:154)
    at runtime.js:164
    at tryCallOne (core.js:37)

Please let me know how i can fix this, my app is going to production soon.

About this issue

  • Original URL
  • State: open
  • Created 4 years ago
  • Reactions: 8
  • Comments: 18

Most upvoted comments

Hey guys!

Got similar problem. In my app’s flow, I was initially using setGenericPassword() without any accessControl option. Then I prompt user to enable biometric auth and in case he agreed I invoked setGenericPassword() again with the same credentials plus accessControl option BIOMETRY_ANY. And it failed with the error Attempt to invoke virtual method...

Through trial and errors I found out, that in that case I had to previously use resetGenericPassword() before storing the same credentials with setGenericPassword().

If this is your case, you may give this workaround a try.

Hello, this issue started happening with me when using the Keychain.setGenericPassword with Biometrics

I have this set of devices that had this issue:

Screenshot 2023-07-05 at 13 38 28

Screenshot 2023-07-05 at 13 39 10

Does anyone has an update on this issue?

Hi, I am getting this error too, in my case in a Xiaomi. How did you fix it?