@onekeyfe/react-native-keychain-module 1.1.20 → 1.1.22

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
@@ -38,7 +38,7 @@ class KeychainModuleCore {
38
38
  throw KeychainModuleError.encodingFailed
39
39
  }
40
40
 
41
- let enableSync = params.enableSync ?? true // Default to enabled for iCloud sync
41
+ let enableSync = params.enableSync ?? true // Default to disabled; callers must explicitly opt in to iCloud sync
42
42
 
43
43
  var query: [String: Any] = [
44
44
  kSecClass as String: kSecClassGenericPassword,
@@ -59,17 +59,41 @@ class KeychainModuleCore {
59
59
  query[kSecAttrDescription as String] = description
60
60
  }
61
61
 
62
- // Delete existing item if it exists
63
- SecItemDelete(query as CFDictionary)
64
-
65
- // Add new item
62
+ // Try to add new item first; if it already exists, update it
66
63
  let status = SecItemAdd(query as CFDictionary, nil)
67
64
 
68
- guard status == errSecSuccess else {
69
- OneKeyLog.error("Keychain", "setItem: failed, OSStatus: \(status)")
70
- throw KeychainModuleError.operationFailed(status)
65
+ if status == errSecDuplicateItem {
66
+ // Item exists - update it instead of delete+add (avoids race condition window)
67
+ let searchQuery: [String: Any] = [
68
+ kSecClass as String: kSecClassGenericPassword,
69
+ kSecAttrService as String: KeychainConstants.serviceIdentifier ?? "",
70
+ kSecAttrAccount as String: params.key,
71
+ kSecAttrSynchronizable as String: kSecAttrSynchronizableAny
72
+ ]
73
+ var updateAttrs: [String: Any] = [
74
+ kSecValueData as String: valueData,
75
+ kSecAttrAccessible as String: kSecAttrAccessibleWhenUnlocked,
76
+ kSecAttrSynchronizable as String: enableSync
77
+ ]
78
+ if let label = params.label {
79
+ updateAttrs[kSecAttrLabel as String] = label
80
+ }
81
+ if let description = params.description {
82
+ updateAttrs[kSecAttrDescription as String] = description
83
+ }
84
+ let updateStatus = SecItemUpdate(searchQuery as CFDictionary, updateAttrs as CFDictionary)
85
+ guard updateStatus == errSecSuccess else {
86
+ OneKeyLog.error("Keychain", "setItem update: failed, OSStatus: \(updateStatus)")
87
+ throw KeychainModuleError.operationFailed(updateStatus)
88
+ }
89
+ OneKeyLog.info("Keychain", "setItem: updated existing")
90
+ } else {
91
+ guard status == errSecSuccess else {
92
+ OneKeyLog.error("Keychain", "setItem: failed, OSStatus: \(status)")
93
+ throw KeychainModuleError.operationFailed(status)
94
+ }
95
+ OneKeyLog.info("Keychain", "setItem: success")
71
96
  }
72
- OneKeyLog.info("Keychain", "setItem: success")
73
97
  }
74
98
 
75
99
  // MARK: - Get Item
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@onekeyfe/react-native-keychain-module",
3
- "version": "1.1.20",
3
+ "version": "1.1.22",
4
4
  "description": "react-native-keychain-module",
5
5
  "main": "./lib/module/index.js",
6
6
  "types": "./lib/typescript/src/index.d.ts",