UNPKG

3.82 kBPlain TextView Raw
1//Copyright (c) 2019 Facebook. All rights reserved.
2
3import Foundation
4import CYBAVOWallet
5
6@objc(RNWalletSDK)
7class RNWalletSDK : NSObject {
8
9 @objc func initSDK(_ configuration: NSDictionary){
10 if let endPoint = configuration["endpoint"]{
11 WalletSdk.shared.endPoint = endPoint as! String
12 }
13 if let apiCode = configuration["apiCode"]{
14 WalletSdk.shared.apiCode = apiCode as! String
15 }
16 if let apnsSandbox = configuration["apnsSandbox"]{
17 WalletSdk.shared.apnsSandbox = apnsSandbox as! Bool
18 }
19 }
20}
21
22extension RNWalletSDK {
23 @objc
24 func constantsToExport() -> [AnyHashable : Any]! {
25 var constants = [AnyHashable : Any]()
26 constants["sdkInfo"] = WalletSdk.shared.getSDKInfo()
27
28 var errorCodes = [String : Any]()
29 for err in ApiError.ErrorCode.allCases {
30 errorCodes[err.name] = String(err.rawValue)
31 }
32 constants["ErrorCodes"] = errorCodes
33 return constants
34 }
35 @objc static func requiresMainQueueSetup() -> Bool {
36 return true
37 }
38}
39
40extension RNWalletSDK{
41 class func toDictHasNil<T>(_ any: T) -> Any {
42 let mirror = Mirror(reflecting: any)
43 if let style = mirror.displayStyle {
44 if style == .collection {
45 var array: [Any] = []
46 for (_, valueMaybe) in mirror.children {
47 let value = unwrap(valueMaybe)
48 array.append(toDictHasNil(value))
49 }
50 return array
51 } else if style == .enum {
52 return (any as? RawEnum)?.anyRawValue ?? ""
53 } else {
54 var dict: [String: Any] = [:]
55 for (labelMaybe, valueMaybe) in mirror.children {
56 guard let label = labelMaybe else { continue }
57 if case Optional<Any>.none = valueMaybe {
58 dict[label] = nil
59 }else{
60 let value = unwrap(valueMaybe)
61 dict[label] = toDictHasNil(value)
62 }
63 }
64 return dict
65 }
66 } else {
67 return any
68 }
69 }
70 class func toDict<T>(_ any: T) -> Any {
71 let mirror = Mirror(reflecting: any)
72 if let style = mirror.displayStyle {
73 if style == .collection {
74 var array: [Any] = []
75 for (_, valueMaybe) in mirror.children {
76 let value = unwrap(valueMaybe)
77 array.append(toDict(value))
78 }
79 return array
80 } else if style == .enum {
81 return (any as? RawEnum)?.anyRawValue ?? ""
82 } else {
83 var dict: [String: Any] = [:]
84 for (labelMaybe, valueMaybe) in mirror.children {
85 guard let label = labelMaybe else { continue }
86 let value = unwrap(valueMaybe)
87 dict[label] = toDict(value)
88 }
89 return dict
90 }
91 } else {
92 return any
93 }
94 }
95
96 class func unwrap<T>(_ any: T) -> Any
97 {
98 let mirror = Mirror(reflecting: any)
99 guard mirror.displayStyle == .optional, let first = mirror.children.first else {
100 return any
101 }
102 return first.value
103 }
104
105}
106
107public protocol RawEnum {
108 var anyRawValue: Any { get }
109}
110
111public extension RawEnum where Self: RawRepresentable {
112 public var anyRawValue: Any {
113 get {
114 let mirror = Mirror(reflecting: self)
115 if mirror.displayStyle != .enum {
116 print("WARNING: You can only extend an enum with the Enum protocol")
117 }
118 return rawValue as Any
119
120 }
121 }
122}
123