Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
/*
* The whole of the vault's native code in an app on the old architecture: nothing. The pod still
* needs one file to compile. There are no card fields: the card form reports unsupported_configuration
* (native fields only).
*/
#import <Foundation/Foundation.h>

void HyperswitchVaultHasNoNativeFieldsInThisBuild(void)
{
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,69 @@
#import <UIKit/UIKit.h>

#import "HyperswitchVaultProtocol.h"

NS_ASSUME_NONNULL_BEGIN

@class HyperswitchVaultHostedModule;

/* One running root of the bundle, and who receives its events. */
@interface HyperswitchVaultSurface : NSObject

/* What the bundle calls rootTag. */
@property (nonatomic, readonly) NSInteger rootTag;

/* The view that draws the root. A form's root has one too, and it is never put on screen. */
@property (nonatomic, readonly) UIView *view;

@property (nonatomic, weak, readonly, nullable) id<HyperswitchVaultEventTarget> owner;

/* New props for the running root. The root keeps its state: an update, not a restart. */
- (void)update:(NSDictionary *)props;

- (void)stop;

@end

/*
* The vault's own JavaScript engine: a second React host, started the first time a card form opens
* and kept for the life of the app process. It runs only hyperswitch-vault-fields.bundle. The card
* number and the CVC exist in THIS engine's memory and in the text inputs it draws; the merchant's
* engine never holds them.
*
* NO CODE FROM OUTSIDE. The bundle is read from the app, in every build type. This engine never
* loads JavaScript from a development server.
*
* Everything here runs on the main thread.
*/
@interface HyperswitchVaultEngine : NSObject

+ (instancetype)shared;

/* Whether this build of the app carries the bundle. */
+ (BOOL)isBundled;

/*
* Starts a root of the bundle. `owner` receives its events until stop. nil when the engine cannot
* start in this app: the form then fails before it mounts, and the fields render the plain way.
*/
- (nullable HyperswitchVaultSurface *)start:(NSString *)rootName
props:(NSDictionary *)props
owner:(id<HyperswitchVaultEventTarget>)owner;

/* From the bundle: hand the event to the root's owner. */
- (void)deliver:(NSInteger)rootTag eventName:(NSString *)eventName payload:(NSString *)payloadJson;

/* NO when the bundle cannot be reached: the caller answers the command itself. */
- (BOOL)send:(NSDictionary *)command;

/* The module of this engine that carries commands to the bundle. */
- (void)attach:(HyperswitchVaultHostedModule *)module;

/* Whether `module` is that one. Any other instance belongs to another engine (the merchant's). */
- (BOOL)isAttached:(HyperswitchVaultHostedModule *)module;

- (void)forget:(HyperswitchVaultSurface *)surface;

@end

NS_ASSUME_NONNULL_END
Original file line number Diff line number Diff line change
@@ -0,0 +1,280 @@
#import "HyperswitchVaultEngine.h"

#import <React/RCTBundleManager.h>
#import <React/RCTComponentViewFactory.h>
#import <React/RCTConvert.h>
#import <React/RCTSurfaceHostingProxyRootView.h>
#import <React/RCTSurfaceProtocol.h>
#import <ReactAppDependencyProvider/RCTAppDependencyProvider.h>
#import <React-RCTAppDelegate/RCTDefaultReactNativeFactoryDelegate.h>
#import <React-RCTAppDelegate/RCTReactNativeFactory.h>
#import <React-RCTAppDelegate/RCTRootViewFactory.h>

#import "HyperswitchVaultHostedModule.h"

#pragma mark - Surface

@interface HyperswitchVaultSurface ()
@property (nonatomic, weak) HyperswitchVaultEngine *engine;
@property (nonatomic, strong) UIView *rootView;
@property (nonatomic, strong, nullable) id<RCTSurfaceProtocol> surface;
@property (nonatomic, weak, readwrite, nullable) id<HyperswitchVaultEventTarget> owner;
@end

@implementation HyperswitchVaultSurface

- (NSInteger)rootTag
{
return _surface != nil ? _surface.rootTag : -1;
}

- (UIView *)view
{
return _rootView;
}

- (void)update:(NSDictionary *)props
{
_surface.properties = props;
}

- (void)stop
{
[_engine forget:self];
_owner = nil;
[_surface stop];
[_rootView removeFromSuperview];
}

@end

#pragma mark - The second host's delegate

/*
* What makes the second host the vault's: where its bundle is, and the one module that exists for
* the bundle to talk to. Every other native module and view it can reach is what the app links.
*/
@interface HyperswitchVaultEngineDelegate : RCTDefaultReactNativeFactoryDelegate
@property (nonatomic, weak) HyperswitchVaultEngine *engine;
@end

@implementation HyperswitchVaultEngineDelegate

+ (NSURL *)bundleFile
{
return [[NSBundle bundleForClass:HyperswitchVaultEngine.class] URLForResource:HyperswitchVaultBundleName
withExtension:HyperswitchVaultBundleExtension];
}

/* The bundle inside the app, in every build type: never a development server. */
- (NSURL *)bundleURL
{
return [HyperswitchVaultEngineDelegate bundleFile];
}

- (NSURL *)sourceURLForBridge:(RCTBridge *)bridge
{
return [self bundleURL];
}

/* The hosted module of THIS host is made here, so the engine knows which instance carries its commands. */
- (id<RCTTurboModule>)getModuleInstanceFromClass:(Class)moduleClass
{
if (moduleClass == HyperswitchVaultHostedModule.class) {
HyperswitchVaultHostedModule *module = [HyperswitchVaultHostedModule new];
[_engine attach:module];
return (id<RCTTurboModule>)module;
}
return nil;
}

@end

#pragma mark - Engine

/*
* The release level the app's own React Native factory uses. React Native allows one per process:
* a second factory created with another level raises. Expo apps record theirs in Info.plist
* (expo-build-properties `ios.reactNativeReleaseLevel`); without that key it is Stable, React
* Native's default.
*/
static RCTReleaseLevel AppReleaseLevel(void)
{
id value = [NSBundle.mainBundle objectForInfoDictionaryKey:@"ReactNativeReleaseLevel"];
if ([value isKindOfClass:NSString.class]) {
NSString *level = [(NSString *)value lowercaseString];
if ([level isEqualToString:@"experimental"]) {
return Experimental;
}
if ([level isEqualToString:@"canary"]) {
return Canary;
}
}
return Stable;
}

@implementation HyperswitchVaultEngine {
HyperswitchVaultEngineDelegate *_delegate;
RCTReactNativeFactory *_factory;
/* Set once, if the factory could not be made. Never retried in this process. */
BOOL _unavailable;
__weak HyperswitchVaultHostedModule *_module;

/*
* The running roots. An event from the bundle names its root by root tag, and the owner is found
* by asking each root for its tag when the event arrives.
*/
NSMutableArray<HyperswitchVaultSurface *> *_running;
}

+ (instancetype)shared
{
static HyperswitchVaultEngine *engine;
static dispatch_once_t once;
dispatch_once(&once, ^{
engine = [HyperswitchVaultEngine new];
});
return engine;
}

+ (BOOL)isBundled
{
return [HyperswitchVaultEngineDelegate bundleFile] != nil;
}

- (instancetype)init
{
if (self = [super init]) {
_running = [NSMutableArray new];
_delegate = [HyperswitchVaultEngineDelegate new];
_delegate.engine = self;
_delegate.dependencyProvider = [RCTAppDependencyProvider new];
}
return self;
}

/*
* The second host, made the first time a root is asked for, or nil when it cannot be made here.
*
* RCTReactNativeFactory assumes it is the app's only factory. Creating it
* - raises if the app's own factory chose another release level (one per process);
* - makes itself the process-wide provider of third-party Fabric components;
* - sets the process-wide default colour space.
* So it is created with the app's release level, the provider and the colour space are put back
* right after, and any failure (an Objective-C or a C++ exception) is caught, remembered and never
* retried: React Native's set-up may be half done after one. The forms then report that they did not
* start (unsupported_configuration in the app's JavaScript).
*/
- (nullable RCTReactNativeFactory *)factory
{
if (_factory != nil || _unavailable) {
return _factory;
}
RCTComponentViewFactory *components = [RCTComponentViewFactory currentComponentViewFactory];
id<RCTComponentViewFactoryComponentProvider> provider = components.thirdPartyFabricComponentsProvider;
RCTColorSpace colorSpace = RCTGetDefaultColorSpace();
RCTReactNativeFactory *factory = nil;
@try {
try {
factory = [[RCTReactNativeFactory alloc] initWithDelegate:_delegate releaseLevel:AppReleaseLevel()];
} catch (...) {
factory = nil;
}
} @catch (NSException *exception) {
factory = nil;
}
components.thirdPartyFabricComponentsProvider = provider;
RCTSetDefaultColorSpace(colorSpace);
_factory = factory;
_unavailable = factory == nil;
return _factory;
}

- (nullable HyperswitchVaultSurface *)start:(NSString *)rootName
props:(NSDictionary *)props
owner:(id<HyperswitchVaultEventTarget>)owner
{
RCTReactNativeFactory *factory = _unavailable ? nil : [self factory];
if (factory == nil) {
return nil;
}
/*
* The first root also creates the engine's runtime, and that can raise outside the factory's own
* set-up above: React Native's default delegate raises in createJSRuntimeFactory when the app uses
* third-party JavaScriptCore (the podspec builds the stand-in for such apps; this is the second
* line of defence). As there, the failure is caught, remembered and never retried, and the form
* reports that it did not start (unsupported_configuration in the app's JavaScript) instead of
* ending the app.
*/
UIView *rootView = nil;
@try {
try {
rootView = [factory.rootViewFactory viewWithModuleName:rootName initialProperties:props];
} catch (...) {
rootView = nil;
}
} @catch (NSException *exception) {
rootView = nil;
}
if (rootView == nil) {
_unavailable = YES;
return nil;
}
rootView.backgroundColor = UIColor.clearColor;

HyperswitchVaultSurface *surface = [HyperswitchVaultSurface new];
surface.engine = self;
surface.rootView = rootView;
surface.owner = owner;
if ([rootView isKindOfClass:RCTSurfaceHostingProxyRootView.class]) {
RCTSurfaceHostingProxyRootView *hosting = (RCTSurfaceHostingProxyRootView *)rootView;
/* As wide as the view it is put in, as tall as its own content. */
hosting.sizeFlexibility = RCTRootViewSizeFlexibilityHeight;
surface.surface = hosting.surface;
}
[_running addObject:surface];
return surface;
}

- (void)forget:(HyperswitchVaultSurface *)surface
{
[_running removeObject:surface];
}

- (void)deliver:(NSInteger)rootTag eventName:(NSString *)eventName payload:(NSString *)payloadJson
{
dispatch_block_t work = ^{
for (HyperswitchVaultSurface *surface in [self->_running copy]) {
if (surface.rootTag == rootTag) {
[surface.owner vaultEvent:eventName payload:payloadJson];
return;
}
}
};
if (NSThread.isMainThread) {
work();
} else {
dispatch_async(dispatch_get_main_queue(), work);
}
}

- (void)attach:(HyperswitchVaultHostedModule *)module
{
_module = module;
}

- (BOOL)isAttached:(HyperswitchVaultHostedModule *)module
{
return module != nil && _module == module;
}

- (BOOL)send:(NSDictionary *)command
{
HyperswitchVaultHostedModule *module = _module;
if (module == nil) {
return NO;
}
return [module sendCommand:command];
}

@end
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
#import <React/RCTViewComponentView.h>
#import <UIKit/UIKit.h>

NS_ASSUME_NONNULL_BEGIN

/*
* The native view the merchant's engine places for one card field. It is an empty frame in the
* merchant's layout; inside it sits a root of the vault's own engine, and that root draws the
* field. The merchant's engine sets three props and hears one event; it never sees what is typed.
*/
@interface HyperswitchVaultFieldView : RCTViewComponentView
@end

NS_ASSUME_NONNULL_END
Loading