@novastera-oss/nitro-metamask 0.6.3 → 0.7.2

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.
Files changed (127) hide show
  1. package/NitroMetamask.podspec +12 -3
  2. package/README.md +3 -1
  3. package/android/build.gradle +14 -32
  4. package/android/cargo-ecies.gradle +60 -88
  5. package/android/src/main/aidl/io/metamask/nativesdk/IMessegeService.aidl +8 -0
  6. package/android/src/main/aidl/io/metamask/nativesdk/IMessegeServiceCallback.aidl +8 -0
  7. package/android/src/main/java/com/margelo/nitro/nitrometamask/HybridNitroMetamask.kt +101 -3
  8. package/android/src/main/java/io/metamask/androidsdk/AnyRequest.kt +8 -0
  9. package/android/src/main/java/io/metamask/androidsdk/ClientMessageServiceCallback.kt +12 -0
  10. package/android/src/main/java/io/metamask/androidsdk/ClientServiceConnection.kt +42 -0
  11. package/android/src/main/java/io/metamask/androidsdk/CommunicationClient.kt +525 -0
  12. package/android/src/main/java/io/metamask/androidsdk/CommunicationClientModule.kt +47 -0
  13. package/android/src/main/java/io/metamask/androidsdk/CommunicationClientModuleInterface.kt +11 -0
  14. package/android/src/main/java/io/metamask/androidsdk/Constants.kt +5 -0
  15. package/android/src/main/java/io/metamask/androidsdk/Crypto.kt +35 -0
  16. package/android/src/main/java/io/metamask/androidsdk/DappMetadata.kt +36 -0
  17. package/android/src/main/java/io/metamask/androidsdk/Encryption.kt +9 -0
  18. package/android/src/main/java/io/metamask/androidsdk/ErrorType.kt +41 -0
  19. package/android/src/main/java/io/metamask/androidsdk/Ethereum.kt +328 -0
  20. package/android/src/main/java/io/metamask/androidsdk/EthereumEventCallback.kt +6 -0
  21. package/android/src/main/java/io/metamask/androidsdk/EthereumMethod.kt +80 -0
  22. package/android/src/main/java/io/metamask/androidsdk/EthereumRequest.kt +7 -0
  23. package/android/src/main/java/io/metamask/androidsdk/EthereumState.kt +7 -0
  24. package/android/src/main/java/io/metamask/androidsdk/KeyExchange.kt +77 -0
  25. package/android/src/main/java/io/metamask/androidsdk/KeyExchangeMessageType.kt +20 -0
  26. package/android/src/main/java/io/metamask/androidsdk/KeyStorage.kt +122 -0
  27. package/android/src/main/java/io/metamask/androidsdk/Logger.kt +18 -0
  28. package/android/src/main/java/io/metamask/androidsdk/Message.kt +3 -0
  29. package/android/src/main/java/io/metamask/androidsdk/MessageType.kt +11 -0
  30. package/android/src/main/java/io/metamask/androidsdk/OriginatorInfo.kt +12 -0
  31. package/android/src/main/java/io/metamask/androidsdk/RequestError.kt +8 -0
  32. package/android/src/main/java/io/metamask/androidsdk/RequestInfo.kt +9 -0
  33. package/android/src/main/java/io/metamask/androidsdk/Result.kt +11 -0
  34. package/android/src/main/java/io/metamask/androidsdk/RpcRequest.kt +7 -0
  35. package/android/src/main/java/io/metamask/androidsdk/SDKInfo.kt +6 -0
  36. package/android/src/main/java/io/metamask/androidsdk/SDKOptions.kt +6 -0
  37. package/android/src/main/java/io/metamask/androidsdk/SecureStorage.kt +9 -0
  38. package/android/src/main/java/io/metamask/androidsdk/SessionConfig.kt +10 -0
  39. package/android/src/main/java/io/metamask/androidsdk/SessionManager.kt +92 -0
  40. package/android/src/main/java/io/metamask/androidsdk/SubmittedRequest.kt +8 -0
  41. package/android/src/main/java/io/metamask/androidsdk/TimeStampGenerator.kt +7 -0
  42. package/android/src/main/jniLibs/arm64-v8a/libecies.so +0 -0
  43. package/android/src/main/jniLibs/armeabi-v7a/libecies.so +0 -0
  44. package/android/src/main/jniLibs/x86/libecies.so +0 -0
  45. package/android/src/main/jniLibs/x86_64/libecies.so +0 -0
  46. package/android/src/test/java/com/margelo/nitro/nitrometamask/CancellationStateMachineTest.kt +128 -0
  47. package/android/src/test/java/com/margelo/nitro/nitrometamask/ChainIdParsingTest.kt +65 -0
  48. package/android/src/test/java/com/margelo/nitro/nitrometamask/ConfigureStateMachineTest.kt +140 -0
  49. package/android/src/test/java/com/margelo/nitro/nitrometamask/ConnectSignJsonTest.kt +76 -0
  50. package/android/src/test/java/com/margelo/nitro/nitrometamask/MetaMaskInstallationCheckTest.kt +42 -0
  51. package/android/src/test/java/com/margelo/nitro/nitrometamask/PersonalSignParamsTest.kt +75 -0
  52. package/ios/Frameworks/Ecies.xcframework/Info.plist +47 -0
  53. package/ios/Frameworks/Ecies.xcframework/ios-arm64/Headers/ecies.h +20 -0
  54. package/ios/Frameworks/Ecies.xcframework/ios-arm64/Headers/module.modulemap +4 -0
  55. package/ios/Frameworks/Ecies.xcframework/ios-arm64/libecies.a +0 -0
  56. package/ios/Frameworks/Ecies.xcframework/ios-arm64-simulator/Headers/ecies.h +20 -0
  57. package/ios/Frameworks/Ecies.xcframework/ios-arm64-simulator/Headers/module.modulemap +4 -0
  58. package/ios/Frameworks/Ecies.xcframework/ios-arm64-simulator/libecies.a +0 -0
  59. package/ios/HybridNitroMetamask.swift +119 -54
  60. package/ios/NitroMetamaskTests/CancellationStateMachineTests.swift +150 -0
  61. package/ios/NitroMetamaskTests/ChainIdParsingTests.swift +117 -0
  62. package/ios/NitroMetamaskTests/ConfigureStateMachineTests.swift +174 -0
  63. package/ios/NitroMetamaskTests/ConnectSignJsonTests.swift +168 -0
  64. package/ios/NitroMetamaskTests/DefaultDappUrlTests.swift +80 -0
  65. package/ios/NitroMetamaskTests/PersonalSignParamsTests.swift +101 -0
  66. package/ios/metamask-ios-sdk/CommunicationLayer/CommClient.swift +43 -0
  67. package/ios/metamask-ios-sdk/CommunicationLayer/CommClientFactory.swift +17 -0
  68. package/ios/metamask-ios-sdk/CommunicationLayer/CommLayer.swift +36 -0
  69. package/ios/metamask-ios-sdk/CommunicationLayer/DeeplinkCommLayer/Deeplink.swift +26 -0
  70. package/ios/metamask-ios-sdk/CommunicationLayer/DeeplinkCommLayer/DeeplinkClient.swift +199 -0
  71. package/ios/metamask-ios-sdk/CommunicationLayer/DeeplinkCommLayer/DeeplinkManager.swift +83 -0
  72. package/ios/metamask-ios-sdk/CommunicationLayer/DeeplinkCommLayer/String.swift +48 -0
  73. package/ios/metamask-ios-sdk/CommunicationLayer/DeeplinkCommLayer/URLOpener.swift +19 -0
  74. package/ios/metamask-ios-sdk/CommunicationLayer/SocketClient.swift +27 -0
  75. package/ios/metamask-ios-sdk/Crypto/Crypto.swift +72 -0
  76. package/ios/metamask-ios-sdk/Crypto/Encoding.swift +15 -0
  77. package/ios/metamask-ios-sdk/Crypto/KeyExchange.swift +236 -0
  78. package/ios/metamask-ios-sdk/DeviceInfo/DeviceInfo.swift +11 -0
  79. package/ios/metamask-ios-sdk/Ethereum/AppMetadata.swift +28 -0
  80. package/ios/metamask-ios-sdk/Ethereum/ErrorType.swift +62 -0
  81. package/ios/metamask-ios-sdk/Ethereum/Ethereum.swift +810 -0
  82. package/ios/metamask-ios-sdk/Ethereum/EthereumMethod.swift +111 -0
  83. package/ios/metamask-ios-sdk/Ethereum/EthereumRequest.swift +40 -0
  84. package/ios/metamask-ios-sdk/Ethereum/EthereumWrapper.swift +10 -0
  85. package/ios/metamask-ios-sdk/Ethereum/RPCRequest.swift +14 -0
  86. package/ios/metamask-ios-sdk/Ethereum/RequestError.swift +88 -0
  87. package/ios/metamask-ios-sdk/Ethereum/ResponseMethod.swift +22 -0
  88. package/ios/metamask-ios-sdk/Ethereum/SubmitRequest.swift +26 -0
  89. package/ios/metamask-ios-sdk/Ethereum/TimestampGenerator.swift +16 -0
  90. package/ios/metamask-ios-sdk/Extensions/NSRecursiveLock.swift +14 -0
  91. package/ios/metamask-ios-sdk/Extensions/Notification.swift +10 -0
  92. package/ios/metamask-ios-sdk/Logger/Logging.swift +27 -0
  93. package/ios/metamask-ios-sdk/Models/AddChainParameters.swift +35 -0
  94. package/ios/metamask-ios-sdk/Models/Event.swift +19 -0
  95. package/ios/metamask-ios-sdk/Models/Mappable.swift +40 -0
  96. package/ios/metamask-ios-sdk/Models/NativeCurrency.swift +25 -0
  97. package/ios/metamask-ios-sdk/Models/OriginatorInfo.swift +26 -0
  98. package/ios/metamask-ios-sdk/Models/RequestInfo.swift +18 -0
  99. package/ios/metamask-ios-sdk/Models/SignContract.swift +48 -0
  100. package/ios/metamask-ios-sdk/Models/Typealiases.swift +9 -0
  101. package/ios/metamask-ios-sdk/Persistence/SecureStore.swift +134 -0
  102. package/ios/metamask-ios-sdk/Persistence/SessionConfig.swift +24 -0
  103. package/ios/metamask-ios-sdk/Persistence/SessionManager.swift +56 -0
  104. package/ios/metamask-ios-sdk/SDK/Dependencies.swift +35 -0
  105. package/ios/metamask-ios-sdk/SDK/MetaMaskSDK.swift +215 -0
  106. package/ios/metamask-ios-sdk/SDK/SDKInfo.swift +37 -0
  107. package/ios/metamask-ios-sdk/SDK/SDKOptions.swift +16 -0
  108. package/lib/commonjs/index.js +50 -3
  109. package/lib/commonjs/index.js.map +1 -1
  110. package/lib/module/index.js +49 -3
  111. package/lib/module/index.js.map +1 -1
  112. package/lib/typescript/src/__tests__/parseNitroError.test.d.ts +2 -0
  113. package/lib/typescript/src/__tests__/parseNitroError.test.d.ts.map +1 -0
  114. package/lib/typescript/src/index.d.ts +43 -3
  115. package/lib/typescript/src/index.d.ts.map +1 -1
  116. package/lib/typescript/src/specs/nitro-metamask.nitro.d.ts +29 -1
  117. package/lib/typescript/src/specs/nitro-metamask.nitro.d.ts.map +1 -1
  118. package/package.json +21 -12
  119. package/react-native.config.js +5 -0
  120. package/rust/ecies-jni/Cargo.lock +50 -86
  121. package/rust/ecies-jni/Cargo.toml +1 -1
  122. package/rust/ecies-jni/src/lib.rs +164 -100
  123. package/src/__tests__/parseNitroError.test.ts +35 -0
  124. package/src/index.ts +53 -5
  125. package/src/specs/nitro-metamask.nitro.ts +29 -1
  126. package/scripts/verify-16k-page-alignment.py +0 -117
  127. package/scripts/verify-16k-page-alignment.sh +0 -5
@@ -0,0 +1,111 @@
1
+ //
2
+ // EthereumMethod.swift
3
+ // metamask-ios-sdk
4
+ //
5
+
6
+ import Foundation
7
+
8
+ public enum EthereumMethod: String, CaseIterable, CodableData {
9
+ case ethSign = "eth_sign"
10
+ case web3Sha = "web3_sha3"
11
+ case ethCall = "eth_call"
12
+ case ethChainId = "eth_chainId"
13
+ case ethGetCode = "eth_getCode"
14
+ case ethAccounts = "eth_accounts"
15
+ case ethGasPrice = "eth_gasPrice"
16
+ case personalSign = "personal_sign"
17
+ case ethGetBalance = "eth_getBalance"
18
+ case watchAsset = "wallet_watchAsset"
19
+ case ethBlockNumber = "eth_blockNumber"
20
+ case ethEstimateGas = "eth_estimateGas"
21
+ case ethGetStorageAt = "eth_getStorageAt"
22
+ case ethSignTypedData = "eth_signTypedData"
23
+ case ethGetBlockByHash = "eth_getBlockByHash"
24
+ case web3ClientVersion = "web3_clientVersion"
25
+ case ethRequestAccounts = "eth_requestAccounts"
26
+ case ethSendTransaction = "eth_sendTransaction"
27
+ case ethSignTypedDataV3 = "eth_signTypedData_v3"
28
+ case ethSignTypedDataV4 = "eth_signTypedData_v4"
29
+ case addEthereumChain = "wallet_addEthereumChain"
30
+ case metamaskBatch = "metamask_batch"
31
+ case metamaskOpen = "metamask_open"
32
+ case personalEcRecover = "personal_ecRecover"
33
+ case walletRevokePermissions = "wallet_revokePermissions"
34
+ case walletRequestPermissions = "wallet_requestPermissions"
35
+ case walletGetPermissions = "wallet_getPermissions"
36
+ case metamaskConnectWith = "metamask_connectwith"
37
+ case metaMaskChainChanged = "metamask_chainChanged"
38
+ case ethSendRawTransaction = "eth_sendRawTransaction"
39
+ case switchEthereumChain = "wallet_switchEthereumChain"
40
+ case ethGetTransactionCount = "eth_getTransactionCount"
41
+ case metaMaskConnectSign = "metamask_connectSign"
42
+ case metaMaskAccountsChanged = "metamask_accountsChanged"
43
+ case ethGetTransactionByHash = "eth_getTransactionByHash"
44
+ case ethGetTransactionReceipt = "eth_getTransactionReceipt"
45
+ case getMetamaskProviderState = "metamask_getProviderState"
46
+ case ethGetBlockTransactionCountByHash = "eth_getBlockTransactionCountByHash"
47
+ case ethGetBlockTransactionCountByNumber = "eth_getBlockTransactionCountByNumber"
48
+ case unknownMethod = "unknown"
49
+
50
+ static func requiresAuthorisation(_ method: EthereumMethod) -> Bool {
51
+ let methods: [EthereumMethod] = [
52
+ .ethSign,
53
+ .watchAsset,
54
+ .metamaskOpen,
55
+ .personalEcRecover,
56
+ .walletRequestPermissions,
57
+ .walletRevokePermissions,
58
+ .walletGetPermissions,
59
+ .personalSign,
60
+ .metamaskBatch,
61
+ .metaMaskConnectSign,
62
+ .metamaskConnectWith,
63
+ .ethSignTypedData,
64
+ .ethRequestAccounts,
65
+ .ethSendTransaction,
66
+ .ethSignTypedDataV3,
67
+ .ethSignTypedDataV4,
68
+ .addEthereumChain,
69
+ .switchEthereumChain
70
+ ]
71
+
72
+ return methods.contains(method)
73
+ }
74
+
75
+ static func isReadOnly(_ method: EthereumMethod) -> Bool {
76
+ !requiresAuthorisation(method)
77
+ }
78
+
79
+ static func isResultMethod(_ method: EthereumMethod) -> Bool {
80
+ let resultMethods: [EthereumMethod] = [
81
+ .ethSign,
82
+ .watchAsset,
83
+ .ethChainId,
84
+ .personalSign,
85
+ .metamaskBatch,
86
+ .walletRevokePermissions,
87
+ .walletGetPermissions,
88
+ .walletRequestPermissions,
89
+ .metaMaskConnectSign,
90
+ .metamaskConnectWith,
91
+ .ethSignTypedData,
92
+ .ethRequestAccounts,
93
+ .ethSendTransaction,
94
+ .ethSignTypedDataV3,
95
+ .ethSignTypedDataV4,
96
+ .addEthereumChain,
97
+ .switchEthereumChain,
98
+ .getMetamaskProviderState
99
+ ]
100
+
101
+ return resultMethods.contains(method)
102
+ }
103
+
104
+ static func isConnectMethod(_ method: EthereumMethod) -> Bool {
105
+ let connectMethods: [EthereumMethod] = [
106
+ .metaMaskConnectSign,
107
+ .metamaskConnectWith
108
+ ]
109
+ return connectMethods.contains(method)
110
+ }
111
+ }
@@ -0,0 +1,40 @@
1
+ //
2
+ // EthereumRequest.swift
3
+ //
4
+
5
+ import Foundation
6
+
7
+ public struct EthereumRequest<T: CodableData>: RPCRequest {
8
+ public var id: String
9
+ public let method: String
10
+ public var params: T
11
+
12
+ public var methodType: EthereumMethod {
13
+ EthereumMethod(rawValue: method) ?? .unknownMethod
14
+ }
15
+
16
+ public init(id: String = TimestampGenerator.timestamp(),
17
+ method: String,
18
+ params: T = "") {
19
+ self.id = id
20
+ self.method = method
21
+ self.params = params
22
+ }
23
+
24
+ public init(id: String = TimestampGenerator.timestamp(),
25
+ method: EthereumMethod,
26
+ params: T = "") {
27
+ self.id = id
28
+ self.method = method.rawValue
29
+ self.params = params
30
+ }
31
+
32
+ public func socketRepresentation() -> NetworkData {
33
+ let encodedParams = try? JSONSerialization.jsonObject(with: JSONEncoder().encode(params))
34
+ return [
35
+ "id": id,
36
+ "method": method,
37
+ "parameters": encodedParams
38
+ ]
39
+ }
40
+ }
@@ -0,0 +1,10 @@
1
+ //
2
+ // EthereumWrapper.swift
3
+ // metamask-ios-sdk
4
+
5
+ import Foundation
6
+
7
+ class EthereumWrapper {
8
+ var ethereum: Ethereum?
9
+ static let shared = EthereumWrapper()
10
+ }
@@ -0,0 +1,14 @@
1
+ //
2
+ // RPCRequest.swift
3
+ // metamask-ios-sdk
4
+ //
5
+
6
+ import Foundation
7
+
8
+ public protocol RPCRequest: CodableData, Mappable {
9
+ var id: String { get }
10
+ var method: String { get }
11
+ associatedtype ParameterType: CodableData
12
+ var params: ParameterType { get }
13
+ var methodType: EthereumMethod { get }
14
+ }
@@ -0,0 +1,88 @@
1
+ //
2
+ // RequestError.swift
3
+ //
4
+
5
+ import Foundation
6
+ import Combine
7
+
8
+ // MARK: - RequestError
9
+
10
+ public struct RequestError: Codable, Error {
11
+ public let code: Int
12
+ public let message: String
13
+
14
+ public init(from info: [String: Any]) {
15
+ code = info["code"] as? Int ?? -1
16
+ if let msg = info["message"] as? String ?? ErrorType(rawValue: code)?.message {
17
+ message = msg
18
+ } else if ErrorType.isServerError(code) {
19
+ message = ErrorType.serverError.message
20
+ } else {
21
+ message = "Something went wrong"
22
+ }
23
+ }
24
+
25
+ public var localizedDescription: String {
26
+ message
27
+ }
28
+
29
+ public static var genericError: RequestError {
30
+ RequestError(from: [
31
+ "code": -100,
32
+ "message": "Something went wrong"
33
+ ])
34
+ }
35
+
36
+ public static var connectError: RequestError {
37
+ RequestError(from: [
38
+ "code": -101,
39
+ "message": "Not connected. Please connect first"
40
+ ])
41
+ }
42
+
43
+ public static var invalidUrlError: RequestError {
44
+ RequestError(from: [
45
+ "code": -102,
46
+ "message": "Please use a valid url in AppMetaData"
47
+ ])
48
+ }
49
+
50
+ public static var invalidTitleError: RequestError {
51
+ RequestError(from: [
52
+ "code": -103,
53
+ "message": "Please use a valid name in AppMetaData"
54
+ ])
55
+ }
56
+
57
+ public static var invalidBatchRequestError: RequestError {
58
+ RequestError(from: [
59
+ "code": -104,
60
+ "message": "Something went wrong, check that your requests are valid"
61
+ ])
62
+ }
63
+
64
+ public static var responseError: RequestError {
65
+ RequestError(from: [
66
+ "code": -105,
67
+ "message": "Unexpected response"
68
+ ])
69
+ }
70
+
71
+ static func failWithError(_ error: RequestError) -> EthereumPublisher {
72
+ let passthroughSubject = PassthroughSubject<Any, RequestError>()
73
+ let publisher: EthereumPublisher = passthroughSubject
74
+ .receive(on: DispatchQueue.main)
75
+ .eraseToAnyPublisher()
76
+ passthroughSubject.send(completion: .failure(error))
77
+ return publisher
78
+ }
79
+ }
80
+
81
+ public extension RequestError {
82
+ var codeType: ErrorType {
83
+ guard let errorType = ErrorType(rawValue: code) else {
84
+ return ErrorType.isServerError(code) ? .serverError : .unknownError
85
+ }
86
+ return errorType
87
+ }
88
+ }
@@ -0,0 +1,22 @@
1
+ //
2
+ // ResponseMethod.swift
3
+ // metamask-ios-sdk
4
+ //
5
+
6
+ import Foundation
7
+
8
+ enum ResponseMethod: String {
9
+ case ethSign = "eth_sign"
10
+ case ethChainId = "eth_chainId"
11
+ case personalSign = "personal_sign"
12
+ case watchAsset = "wallet_watchAsset"
13
+ case signTypedData = "eth_signTypedData"
14
+ case requestAccounts = "eth_requestAccounts"
15
+ case signTransaction = "eth_signTransaction"
16
+ case sendTransaction = "eth_sendTransaction"
17
+ case signTypedDataV3 = "eth_signTypedData_v3"
18
+ case signTypedDataV4 = "eth_signTypedData_v4"
19
+ case addEthereumChain = "wallet_addEthereumChain"
20
+ case switchEthereumChain = "wallet_switchEthereumChain"
21
+ case getMetamaskProviderState = "metamask_getProviderState"
22
+ }
@@ -0,0 +1,26 @@
1
+ //
2
+ // SubmitRequest.swift
3
+ // metamask-ios-sdk
4
+ //
5
+
6
+ import Combine
7
+ import Foundation
8
+
9
+ struct SubmittedRequest {
10
+ let method: String
11
+ private let requestSubject = PassthroughSubject<Any, RequestError>()
12
+
13
+ var publisher: EthereumPublisher? {
14
+ requestSubject
15
+ .receive(on: DispatchQueue.main)
16
+ .eraseToAnyPublisher()
17
+ }
18
+
19
+ func send(_ value: Any) {
20
+ requestSubject.send(value)
21
+ }
22
+
23
+ func error(_ err: RequestError) {
24
+ requestSubject.send(completion: .failure(err))
25
+ }
26
+ }
@@ -0,0 +1,16 @@
1
+ //
2
+ // TimestampGenerator.swift
3
+ // metamask-ios-sdk
4
+ //
5
+
6
+ import Foundation
7
+
8
+ public struct TimestampGenerator {
9
+ public static func timestamp() -> String {
10
+ let currentDate = Date()
11
+ let salt = Int64(arc4random_uniform(100)) + 1
12
+ let time = Int64(currentDate.timeIntervalSince1970 * 1000)
13
+ let uniqueTime = salt + time
14
+ return String(uniqueTime)
15
+ }
16
+ }
@@ -0,0 +1,14 @@
1
+ //
2
+ // NSRecursiveLock.swift
3
+ //
4
+
5
+ import Foundation
6
+
7
+ extension NSRecursiveLock {
8
+ @inlinable @discardableResult
9
+ func sync<Value>(_ work: () -> Value) -> Value {
10
+ lock()
11
+ defer { unlock() }
12
+ return work()
13
+ }
14
+ }
@@ -0,0 +1,10 @@
1
+ //
2
+ // Notification.swift
3
+ //
4
+
5
+ import Foundation
6
+
7
+ public extension Notification.Name {
8
+ static let MetaMaskAccountChanged = Notification.Name("MetaMaskAccountChanged")
9
+ static let MetaMaskChainIdChanged = Notification.Name("MetaMaskChainChanged")
10
+ }
@@ -0,0 +1,27 @@
1
+ //
2
+ // Logging.swift
3
+ //
4
+
5
+ import OSLog
6
+ import Foundation
7
+
8
+ public class Logging {
9
+ public static func log(_ message: String) {
10
+ Logger().log("mmsdk| \(message)")
11
+ }
12
+
13
+ public static func error(_ error: String, file: String = #file, function: String = #function, line: Int = #line) {
14
+ Logger().log(
15
+ level: .error,
16
+ "\n============\nmmsdk| Error: \(error)\nFunc: \(function)\nFile: \(fileName(from: file))\nLine: \(line)\n============\n"
17
+ )
18
+ }
19
+
20
+ public static func error(_ error: Error, file: String = #file, function: String = #function, line: Int = #line) {
21
+ Logger().log(level: .error, "\n============\nmmsdk| Error \nFunc: \(function)\nFile: \(fileName(from: file))\nLine: \(line)\nError: \(error.localizedDescription)\n============\n")
22
+ }
23
+
24
+ private static func fileName(from path: String) -> String {
25
+ path.components(separatedBy: "/").last ?? ""
26
+ }
27
+ }
@@ -0,0 +1,35 @@
1
+ //
2
+ // AddChainParameters.swift
3
+ // metamask-ios-sdk
4
+ //
5
+
6
+ import Foundation
7
+
8
+ public struct AddChainParameters: CodableData {
9
+ public let chainId: String
10
+ public let chainName: String
11
+ public let rpcUrls: [String]
12
+ public let iconUrls: [String]?
13
+ public let blockExplorerUrls: [String]?
14
+ public let nativeCurrency: NativeCurrency
15
+
16
+ public init(chainId: String, chainName: String, rpcUrls: [String], iconUrls: [String]?, blockExplorerUrls: [String]?, nativeCurrency: NativeCurrency) {
17
+ self.chainId = chainId
18
+ self.chainName = chainName
19
+ self.rpcUrls = rpcUrls
20
+ self.iconUrls = iconUrls
21
+ self.blockExplorerUrls = blockExplorerUrls
22
+ self.nativeCurrency = nativeCurrency
23
+ }
24
+
25
+ public func socketRepresentation() -> NetworkData {
26
+ [
27
+ "chainId": chainId,
28
+ "chainName": chainName,
29
+ "rpcUrls": rpcUrls,
30
+ "iconUrls": iconUrls ?? [],
31
+ "blockExplorerUrls": blockExplorerUrls ?? [],
32
+ "nativeCurrency": nativeCurrency.socketRepresentation()
33
+ ]
34
+ }
35
+ }
@@ -0,0 +1,19 @@
1
+ //
2
+ // Event.swift
3
+ //
4
+
5
+ public enum Event: String {
6
+ case sdkRpcRequest = "sdk_rpc_request"
7
+ case sdkRpcRequestDone = "sdk_rpc_request_done"
8
+ case connectionRequest = "sdk_connect_request_started"
9
+ case reconnectionRequest = "sdk_reconnect_request_started"
10
+ case connected = "sdk_connection_established"
11
+ case connectionAuthorised = "sdk_connection_authorized"
12
+ case connectionRejected = "sdk_connection_rejected"
13
+ case disconnected = "sdk_disconnected"
14
+ case connectionTerminated = "sdk_connection_terminated"
15
+
16
+ var name: String {
17
+ rawValue
18
+ }
19
+ }
@@ -0,0 +1,40 @@
1
+ //
2
+ // Mappable.swift
3
+ // metamask-ios-sdk
4
+ //
5
+
6
+ import Foundation
7
+
8
+ public protocol Mappable: Codable { }
9
+
10
+ public extension Mappable {
11
+ func toDictionary() -> [String: Any]? {
12
+ let encoder = JSONEncoder()
13
+ do {
14
+ let jsonData = try encoder.encode(self)
15
+ guard let jsonObject = try JSONSerialization.jsonObject(with: jsonData, options: []) as? [String: Any] else {
16
+ Logging.error("Mappable:: Error converting JSON data to dictionary")
17
+ return nil
18
+ }
19
+ return jsonObject
20
+ } catch {
21
+ print("Error encoding JSON: \(error)")
22
+ Logging.error("Mappable:: Error encoding JSON: \(error)")
23
+ return nil
24
+ }
25
+ }
26
+
27
+ func toJsonString() -> String? {
28
+ let encoder = JSONEncoder()
29
+ do {
30
+ let jsonData = try encoder.encode(self)
31
+ return String(data: jsonData, encoding: .utf8)
32
+ } catch {
33
+ Logging.error("Error encoding JSON: \(error)")
34
+ return nil
35
+ }
36
+ }
37
+ }
38
+
39
+ extension String: Mappable {}
40
+ extension Dictionary: Mappable where Key == String, Value: Codable {}
@@ -0,0 +1,25 @@
1
+ //
2
+ // NativeCurrency.swift
3
+ // metamask-ios-sdk
4
+
5
+ import Foundation
6
+
7
+ public struct NativeCurrency: CodableData {
8
+ public let name: String?
9
+ public let symbol: String
10
+ public let decimals: Int
11
+
12
+ public init(name: String?, symbol: String, decimals: Int) {
13
+ self.name = name
14
+ self.symbol = symbol
15
+ self.decimals = decimals
16
+ }
17
+
18
+ public func socketRepresentation() -> NetworkData {
19
+ [
20
+ "name": name ?? "",
21
+ "symbol": symbol,
22
+ "decimals": decimals
23
+ ]
24
+ }
25
+ }
@@ -0,0 +1,26 @@
1
+ //
2
+ // OriginatorInfo.swift
3
+ // metamask-ios-sdk
4
+ //
5
+
6
+ import Foundation
7
+
8
+ public struct OriginatorInfo: CodableData, Mappable {
9
+ public let title: String?
10
+ public let url: String?
11
+ public let icon: String?
12
+ public let dappId: String?
13
+ public let platform: String?
14
+ public let apiVersion: String?
15
+
16
+ public func socketRepresentation() -> NetworkData {
17
+ [
18
+ "title": title,
19
+ "url": url,
20
+ "icon": icon,
21
+ "dappId": dappId,
22
+ "platform": platform,
23
+ "apiVersion": apiVersion
24
+ ]
25
+ }
26
+ }
@@ -0,0 +1,18 @@
1
+ //
2
+ // RequestInfo.swift
3
+ // metamask-ios-sdk
4
+ //
5
+
6
+ import Foundation
7
+
8
+ public struct RequestInfo: CodableData, Mappable {
9
+ public let type: String
10
+ public let originator: OriginatorInfo
11
+ public let originatorInfo: OriginatorInfo
12
+
13
+ public func socketRepresentation() -> NetworkData {
14
+ ["type": type,
15
+ "originator": originator.socketRepresentation(), // Backward compatibility with MetaMask mobile
16
+ "originatorInfo": originatorInfo.socketRepresentation()]
17
+ }
18
+ }
@@ -0,0 +1,48 @@
1
+ //
2
+ // SignContract.swift
3
+ // metamask-ios-sdk
4
+ //
5
+
6
+ import Foundation
7
+
8
+ public struct SignContract: Mappable {
9
+ let id: String
10
+ let method: String
11
+ let params: [SignContractParameter]
12
+ }
13
+
14
+ public struct SignContractParameter: Mappable {
15
+ let domain: Domain
16
+ let message: SignMessage
17
+ let primaryType: String
18
+ let types: ParameterTypes
19
+ }
20
+
21
+ public struct Domain: Mappable {
22
+ let chainId: String
23
+ let name: String
24
+ let verifyingContract: String
25
+ let version: String
26
+ }
27
+
28
+ public struct SignMessage: Mappable {
29
+ let contents: String
30
+ let from: Person
31
+ let to: Person
32
+ }
33
+
34
+ public struct Person: Mappable {
35
+ let name: String
36
+ let wallet: String
37
+ }
38
+
39
+ public struct ParameterTypes: Mappable {
40
+ let EIP712Domain: [ParameterType]
41
+ let Mail: [ParameterType]
42
+ let Person: [ParameterType]
43
+ }
44
+
45
+ public struct ParameterType: Mappable {
46
+ let name: String
47
+ let type: String
48
+ }
@@ -0,0 +1,9 @@
1
+ //
2
+ // Typealiases.swift
3
+ //
4
+
5
+ import Foundation
6
+
7
+ public typealias NetworkData = [String: Any?]
8
+ public typealias RequestTask = Task<Any, Never>
9
+ public typealias CodableData = Codable