EOSIO / eosio-swift-vault

Utility library for managing keys and signing with Apple's Keychain and Secure Enclave
https://eosio.github.io/eosio-swift-vault/
MIT License
12 stars 8 forks source link

Crash when saving EosioVault.VaultKey metadata with enum values. #40

Closed opi-smccoole closed 4 years ago

opi-smccoole commented 5 years ago

When the vault saves metadata for a key it uses the JSONSerialization class to convert the metadata dictionary to JSON for storage. According to the Apple documentation, JSONSerialization has the following restrictions on the types of values that the Dictionary can have. The relevant passage is included below.

You use the JSONSerialization class to convert JSON to Foundation objects and convert Foundation objects to JSON.

A Foundation object that may be converted to JSON must have the following properties:

The top level object is an NSArray or NSDictionary.

All objects are instances of NSString, NSNumber, NSArray, NSDictionary, or NSNull.

All dictionary keys are instances of NSString.

Numbers are not NaN or infinity.

Other rules may apply. Calling isValidJSONObject(_:) or attempting a conversion are the definitive ways to tell if a given object can be converted to JSON data.

When the supplied metadata Dictionary for an EosioVault.VaultKey operation contains a non-supported type it will result in the following application crash:

Terminating app due to uncaught exception 'NSInvalidArgumentException', reason: 'Invalid type in JSON write (__SwiftValue)'

Currently the work around is to only use the appropriate types in VaultKey metadata. Ideally a future fix would be to adopt the newer JSONDecoder/JSONEncoder interfaces for VaultKey metadata so that the Dictionary values only need to conform to Codeable.