Skip to content
This repository was archived by the owner on Oct 10, 2025. It is now read-only.

Commit c7ecb18

Browse files
authored
Merge pull request #19 from magiclabs/ariflo-sc-70937-mc-methods-move-to-magic
Move MC Methods into Mobile SDKs iOS
2 parents ca25a8a + 190daf4 commit c7ecb18

File tree

12 files changed

+263
-183
lines changed

12 files changed

+263
-183
lines changed

MagicSDK.podspec

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,7 @@
33
#
44
Pod::Spec.new do |s|
55
s.name = 'MagicSDK'
6-
s.version = '6.2.0'
6+
s.version = '7.0.0'
77
s.summary = 'Magic IOS SDK'
88

99
s.description = <<-DESC

Sources/MagicSDK/Core/Magic.swift

Lines changed: 22 additions & 52 deletions
Original file line numberDiff line numberDiff line change
@@ -9,17 +9,18 @@ import MagicSDK_Web3
99
import WebKit
1010

1111

12-
internal enum ProductType{
13-
case MA
14-
case MC
15-
}
16-
1712
/// An instance of the Magic SDK
18-
public class Magic: MagicCore {
19-
20-
// MARK: - Module
13+
public class Magic: NSObject {
14+
// MARK: - Log Message Warning
15+
public let MA_EXTENSION_ONLY_MSG = "This extension only works with Magic Auth API Keys"
16+
17+
// MARK: - Modules
2118
public let user: UserModule
2219
public let auth: AuthModule
20+
public let wallet: WalletModule
21+
22+
// MARK: - Property
23+
public var rpcProvider: RpcProvider
2324

2425
/// Shared instance of `Magic`
2526
public static var shared: Magic!
@@ -30,60 +31,29 @@ public class Magic: MagicCore {
3031
///
3132
/// - Parameters:
3233
/// - apiKey: Your client ID. From https://dashboard.Magic.com
33-
/// - ethNetwork: Network setting
34-
public convenience init(apiKey: String, network: EthNetwork, locale: String = Locale.current.identifier) {
35-
self.init(urlBuilder: URLBuilder(apiKey: apiKey, locale: locale, productType: .MA))
34+
/// - ethNetwork: Etherum Network setting (ie. mainnet or goerli)
35+
/// - customNode: A custom RPC node
36+
public convenience init(apiKey: String, ethNetwork: EthNetwork, locale: String = Locale.current.identifier) {
37+
self.init(urlBuilder: URLBuilder(apiKey: apiKey, network: ethNetwork, locale: locale))
3638
}
3739

3840
public convenience init(apiKey: String, customNode: CustomNodeConfiguration, locale: String = Locale.current.identifier) {
39-
let urlBuilder = URLBuilder(apiKey: apiKey, customNode: customNode, locale: locale, productType: ProductType.MA)
40-
self.init(urlBuilder: urlBuilder)
41+
self.init(urlBuilder: URLBuilder(apiKey: apiKey, customNode: customNode, locale: locale))
4142
}
4243

4344
public convenience init(apiKey: String, locale: String = Locale.current.identifier) {
44-
self.init(urlBuilder: URLBuilder(apiKey: apiKey, locale: locale, productType: .MA))
45+
self.init(urlBuilder: URLBuilder(apiKey: apiKey, network: EthNetwork.mainnet, locale: locale))
4546
}
4647

4748
/// Core constructor
4849
private init(urlBuilder: URLBuilder) {
49-
let rpcProvider = RpcProvider(urlBuilder: urlBuilder)
50-
self.user = UserModule(rpcProvider: rpcProvider)
51-
self.auth = AuthModule(rpcProvider: rpcProvider)
52-
super.init(rpcProvider: rpcProvider)
53-
}
54-
}
55-
56-
/// An instance of the Magic SDK
57-
public class MagicConnect: MagicCore {
58-
59-
public let connect: ConnectModule
60-
61-
/// Shared instance of `Magic`
62-
public static var shared: MagicConnect!
63-
64-
public convenience init(apiKey: String) {
65-
let urlBuilder = URLBuilder(apiKey: apiKey, ethNetwork: EthNetwork.mainnet, locale: "en_US", productType: ProductType.MC)
66-
self.init(urlBuilder: urlBuilder)
67-
}
68-
69-
public convenience init(apiKey: String, network: EthNetwork) {
70-
let urlBuilder = URLBuilder(apiKey: apiKey, ethNetwork: network, locale: "en_US", productType: ProductType.MC)
71-
self.init(urlBuilder: urlBuilder)
72-
}
73-
74-
private init(urlBuilder: URLBuilder) {
75-
let rpcProvider = RpcProvider(urlBuilder: urlBuilder)
76-
self.connect = ConnectModule(rpcProvider: rpcProvider)
77-
super.init(rpcProvider: rpcProvider)
78-
}
79-
}
80-
81-
public class MagicCore: NSObject {
82-
83-
public var rpcProvider: RpcProvider
84-
85-
internal init(rpcProvider: RpcProvider) {
86-
self.rpcProvider = rpcProvider
50+
self.rpcProvider = RpcProvider(urlBuilder: urlBuilder)
51+
52+
self.user = UserModule(rpcProvider: self.rpcProvider)
53+
self.auth = AuthModule(rpcProvider: self.rpcProvider)
54+
self.wallet = WalletModule(rpcProvider: self.rpcProvider)
55+
56+
super.init()
8757
}
8858
}
8959

Sources/MagicSDK/Core/Relayer/URLBuilder.swift

Lines changed: 37 additions & 56 deletions
Original file line numberDiff line numberDiff line change
@@ -15,60 +15,73 @@ import Foundation
1515
public struct URLBuilder {
1616

1717
let encodedParams, url: String
18+
1819
static let host = "https://box.magic.link"
19-
2020
public let apiKey: String
2121

22-
init(apiKey: String, customNode: CustomNodeConfiguration? = nil, ethNetwork: EthNetwork? = nil, locale: String, productType: ProductType) {
22+
init(apiKey: String, customNode: CustomNodeConfiguration? = nil, network: EthNetwork? = nil, locale: String) {
23+
24+
let data = try! JSONEncoder().encode(
25+
UrlParamsEncodable(
26+
apiKey: apiKey,
27+
ethNetwork: network,
28+
customNode: customNode,
29+
locale: locale
30+
)
31+
)
2332

24-
let data = try! JSONEncoder().encode(paramsEncodable(apiKey: apiKey, ethNetwork: ethNetwork, customNode: customNode, locale: locale, productType: productType))
25-
self.init(data: data, host: URLBuilder.host, apiKey: apiKey, productType: productType)
33+
self.init(data: data, host: URLBuilder.host, apiKey: apiKey)
2634
}
2735

28-
private init(data: Data, host: String, apiKey: String, productType: ProductType) {
36+
private init(data: Data, host: String, apiKey: String) {
37+
2938
let jsonString = String(data: data, encoding: .utf8)!
30-
let string = jsonString.replacingOccurrences(of: "\\", with: "")
39+
let sanitizedJsonString = jsonString.replacingOccurrences(of: "\\", with: "")
40+
3141
// Encode instantiate option to params
3242
self.apiKey = apiKey
33-
self.encodedParams = btoa(jsonString: string)
43+
self.encodedParams = btoa(jsonString: sanitizedJsonString)
44+
3445
self.url = "\(host)/send/?params=\(self.encodedParams)"
3546
}
3647

37-
// MARK: - Options structs
38-
struct paramsEncodable: Encodable {
39-
let API_KEY: String
48+
// MARK: - UrlParamsEncodable
49+
struct UrlParamsEncodable: Encodable {
50+
51+
let apiKey: String
4052
let locale: String
4153
let customNode: CustomNodeConfiguration?
4254
let ethNetwork: EthNetwork?
43-
let productType: ProductType
44-
init(apiKey: String, ethNetwork: EthNetwork?, customNode: CustomNodeConfiguration?, locale: String, productType: ProductType) {
45-
self.productType = productType
55+
56+
init(apiKey: String, ethNetwork: EthNetwork?, customNode: CustomNodeConfiguration?, locale: String) {
57+
self.apiKey = apiKey
58+
self.locale = locale
4659
self.customNode = customNode
4760
self.ethNetwork = ethNetwork
48-
self.API_KEY = apiKey
49-
self.locale = locale
5061
}
5162

5263
enum CodingKeys: String, CodingKey {
53-
case sdk, bundleId, API_KEY, host, ETH_NETWORK, ext
64+
case apiKey = "API_KEY"
65+
case ethNetwork = "ETH_NETWORK"
66+
case bundleId, host, sdk
5467
}
5568

5669
func encode(to encoder: Encoder) throws {
5770
var container = encoder.container(keyedBy: CodingKeys.self)
71+
5872
try container.encode("magic-sdk-ios", forKey: .sdk)
5973
try container.encode(Bundle.main.bundleIdentifier, forKey: .bundleId)
60-
try container.encode(self.API_KEY, forKey: .API_KEY)
74+
75+
try container.encode(apiKey, forKey: .apiKey)
6176
try container.encode(URLBuilder.host, forKey: .host)
6277

63-
/// Network
64-
if (customNode != nil) {
65-
try container.encode(customNode, forKey: .ETH_NETWORK)
78+
79+
if let node = customNode {
80+
try container.encode(node, forKey: .ethNetwork)
6681
}
67-
if (ethNetwork != nil) {
68-
try container.encode(ethNetwork?.rawValue, forKey: .ETH_NETWORK)
82+
if let network = ethNetwork {
83+
try container.encode(network.rawValue, forKey: .ethNetwork)
6984
}
70-
71-
try container.encode(ExtensionObject(productType: productType), forKey: .ext)
7285
}
7386
}
7487
}
@@ -83,35 +96,3 @@ public struct CustomNodeConfiguration: Encodable {
8396
self.chainId = chainId
8497
}
8598
}
86-
87-
88-
// MARK: -- Extension
89-
struct ExtensionObject: Encodable {
90-
91-
let productType: ProductType
92-
93-
init(productType: ProductType) {
94-
self.productType = productType
95-
}
96-
97-
enum CodingKeys: String, CodingKey {
98-
case connect
99-
}
100-
101-
func encode(to encoder: Encoder) throws {
102-
var container = encoder.container(keyedBy: CodingKeys.self)
103-
104-
switch productType {
105-
case .MC:
106-
try container.encode(MCConfig(), forKey: .connect)
107-
break
108-
default:
109-
break
110-
}
111-
112-
}
113-
}
114-
115-
internal struct MCConfig: Encodable {
116-
let mc = true
117-
}

Sources/MagicSDK/Modules/Auth/AuthModule.swift

Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -9,8 +9,14 @@
99
import Foundation
1010
import MagicSDK_Web3
1111
import PromiseKit
12+
import os
1213

1314
public class AuthModule: BaseModule {
15+
@available(iOS 14.0, *)
16+
private static let logger = Logger(
17+
subsystem: Bundle.main.bundleIdentifier!,
18+
category: String(describing: AuthModule.self)
19+
)
1420

1521
// MARK: - Login with magic link
1622
public func loginWithMagicLink (_ configuration: LoginWithMagicLinkConfiguration, response: @escaping Web3ResponseCompletion<String> ) {
@@ -32,6 +38,11 @@ public class AuthModule: BaseModule {
3238

3339
// MARK: - Login with SMS
3440
public func loginWithSMS (_ configuration: LoginWithSmsConfiguration, response: @escaping Web3ResponseCompletion<String> ) {
41+
if #available(iOS 14.0, *) {
42+
AuthModule.logger.warning("loginWithSMS: \(BaseWarningLog.MA_Method)")
43+
} else {
44+
print("loginWithSMS: \(BaseWarningLog.MA_Method)")
45+
}
3546
let request = RPCRequest<[LoginWithSmsConfiguration]>(method: AuthMethod.magic_auth_login_with_sms.rawValue, params: [configuration])
3647
self.provider.send(request: request, response: response)
3748
}
@@ -44,6 +55,12 @@ public class AuthModule: BaseModule {
4455

4556
// MARK: - Login with EmailOTP
4657
public func loginWithEmailOTP (_ configuration: LoginWithEmailOTPConfiguration, response: @escaping Web3ResponseCompletion<String> ) {
58+
if #available(iOS 14.0, *) {
59+
AuthModule.logger.warning("loginWithEmailOTP: \(BaseWarningLog.MA_Method)")
60+
} else {
61+
print("loginWithEmailOTP: \(BaseWarningLog.MA_Method)")
62+
}
63+
4764
let request = RPCRequest<[LoginWithEmailOTPConfiguration]>(method: AuthMethod.magic_auth_login_with_email_otp.rawValue, params: [configuration])
4865
self.provider.send(request: request, response: response)
4966
}
Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,13 @@
1+
//
2+
// BaseWarningLog.swift
3+
//
4+
//
5+
// Created by Arian Flores - Magic on 2/16/23.
6+
//
7+
8+
import Foundation
9+
10+
struct BaseWarningLog {
11+
static let MA_Method = "This method only works with Magic Auth API Keys"
12+
static let MC_Method = "This method only works with Magic Connect API Keys"
13+
}

Sources/MagicSDK/Modules/Connect/ConnectConfiguration.swift

Lines changed: 0 additions & 18 deletions
This file was deleted.

Sources/MagicSDK/Modules/Connect/ConnectModule.swift

Lines changed: 0 additions & 53 deletions
This file was deleted.

0 commit comments

Comments
 (0)