Skip to content
This repository has been archived by the owner on Sep 4, 2024. It is now read-only.

Commit

Permalink
Merge pull request #19 from magiclabs/ariflo-sc-70937-mc-methods-move…
Browse files Browse the repository at this point in the history
…-to-magic

Move MC Methods into Mobile SDKs iOS
  • Loading branch information
Ariflo authored Mar 2, 2023
2 parents ca25a8a + 190daf4 commit c7ecb18
Show file tree
Hide file tree
Showing 12 changed files with 263 additions and 183 deletions.
2 changes: 1 addition & 1 deletion MagicSDK.podspec
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@
#
Pod::Spec.new do |s|
s.name = 'MagicSDK'
s.version = '6.2.0'
s.version = '7.0.0'
s.summary = 'Magic IOS SDK'

s.description = <<-DESC
Expand Down
74 changes: 22 additions & 52 deletions Sources/MagicSDK/Core/Magic.swift
Original file line number Diff line number Diff line change
Expand Up @@ -9,17 +9,18 @@ import MagicSDK_Web3
import WebKit


internal enum ProductType{
case MA
case MC
}

/// An instance of the Magic SDK
public class Magic: MagicCore {

// MARK: - Module
public class Magic: NSObject {
// MARK: - Log Message Warning
public let MA_EXTENSION_ONLY_MSG = "This extension only works with Magic Auth API Keys"

// MARK: - Modules
public let user: UserModule
public let auth: AuthModule
public let wallet: WalletModule

// MARK: - Property
public var rpcProvider: RpcProvider

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

public convenience init(apiKey: String, customNode: CustomNodeConfiguration, locale: String = Locale.current.identifier) {
let urlBuilder = URLBuilder(apiKey: apiKey, customNode: customNode, locale: locale, productType: ProductType.MA)
self.init(urlBuilder: urlBuilder)
self.init(urlBuilder: URLBuilder(apiKey: apiKey, customNode: customNode, locale: locale))
}

public convenience init(apiKey: String, locale: String = Locale.current.identifier) {
self.init(urlBuilder: URLBuilder(apiKey: apiKey, locale: locale, productType: .MA))
self.init(urlBuilder: URLBuilder(apiKey: apiKey, network: EthNetwork.mainnet, locale: locale))
}

/// Core constructor
private init(urlBuilder: URLBuilder) {
let rpcProvider = RpcProvider(urlBuilder: urlBuilder)
self.user = UserModule(rpcProvider: rpcProvider)
self.auth = AuthModule(rpcProvider: rpcProvider)
super.init(rpcProvider: rpcProvider)
}
}

/// An instance of the Magic SDK
public class MagicConnect: MagicCore {

public let connect: ConnectModule

/// Shared instance of `Magic`
public static var shared: MagicConnect!

public convenience init(apiKey: String) {
let urlBuilder = URLBuilder(apiKey: apiKey, ethNetwork: EthNetwork.mainnet, locale: "en_US", productType: ProductType.MC)
self.init(urlBuilder: urlBuilder)
}

public convenience init(apiKey: String, network: EthNetwork) {
let urlBuilder = URLBuilder(apiKey: apiKey, ethNetwork: network, locale: "en_US", productType: ProductType.MC)
self.init(urlBuilder: urlBuilder)
}

private init(urlBuilder: URLBuilder) {
let rpcProvider = RpcProvider(urlBuilder: urlBuilder)
self.connect = ConnectModule(rpcProvider: rpcProvider)
super.init(rpcProvider: rpcProvider)
}
}

public class MagicCore: NSObject {

public var rpcProvider: RpcProvider

internal init(rpcProvider: RpcProvider) {
self.rpcProvider = rpcProvider
self.rpcProvider = RpcProvider(urlBuilder: urlBuilder)

self.user = UserModule(rpcProvider: self.rpcProvider)
self.auth = AuthModule(rpcProvider: self.rpcProvider)
self.wallet = WalletModule(rpcProvider: self.rpcProvider)

super.init()
}
}

Expand Down
93 changes: 37 additions & 56 deletions Sources/MagicSDK/Core/Relayer/URLBuilder.swift
Original file line number Diff line number Diff line change
Expand Up @@ -15,60 +15,73 @@ import Foundation
public struct URLBuilder {

let encodedParams, url: String

static let host = "https://box.magic.link"

public let apiKey: String

init(apiKey: String, customNode: CustomNodeConfiguration? = nil, ethNetwork: EthNetwork? = nil, locale: String, productType: ProductType) {
init(apiKey: String, customNode: CustomNodeConfiguration? = nil, network: EthNetwork? = nil, locale: String) {

let data = try! JSONEncoder().encode(
UrlParamsEncodable(
apiKey: apiKey,
ethNetwork: network,
customNode: customNode,
locale: locale
)
)

let data = try! JSONEncoder().encode(paramsEncodable(apiKey: apiKey, ethNetwork: ethNetwork, customNode: customNode, locale: locale, productType: productType))
self.init(data: data, host: URLBuilder.host, apiKey: apiKey, productType: productType)
self.init(data: data, host: URLBuilder.host, apiKey: apiKey)
}

private init(data: Data, host: String, apiKey: String, productType: ProductType) {
private init(data: Data, host: String, apiKey: String) {

let jsonString = String(data: data, encoding: .utf8)!
let string = jsonString.replacingOccurrences(of: "\\", with: "")
let sanitizedJsonString = jsonString.replacingOccurrences(of: "\\", with: "")

// Encode instantiate option to params
self.apiKey = apiKey
self.encodedParams = btoa(jsonString: string)
self.encodedParams = btoa(jsonString: sanitizedJsonString)

self.url = "\(host)/send/?params=\(self.encodedParams)"
}

// MARK: - Options structs
struct paramsEncodable: Encodable {
let API_KEY: String
// MARK: - UrlParamsEncodable
struct UrlParamsEncodable: Encodable {

let apiKey: String
let locale: String
let customNode: CustomNodeConfiguration?
let ethNetwork: EthNetwork?
let productType: ProductType
init(apiKey: String, ethNetwork: EthNetwork?, customNode: CustomNodeConfiguration?, locale: String, productType: ProductType) {
self.productType = productType

init(apiKey: String, ethNetwork: EthNetwork?, customNode: CustomNodeConfiguration?, locale: String) {
self.apiKey = apiKey
self.locale = locale
self.customNode = customNode
self.ethNetwork = ethNetwork
self.API_KEY = apiKey
self.locale = locale
}

enum CodingKeys: String, CodingKey {
case sdk, bundleId, API_KEY, host, ETH_NETWORK, ext
case apiKey = "API_KEY"
case ethNetwork = "ETH_NETWORK"
case bundleId, host, sdk
}

func encode(to encoder: Encoder) throws {
var container = encoder.container(keyedBy: CodingKeys.self)

try container.encode("magic-sdk-ios", forKey: .sdk)
try container.encode(Bundle.main.bundleIdentifier, forKey: .bundleId)
try container.encode(self.API_KEY, forKey: .API_KEY)

try container.encode(apiKey, forKey: .apiKey)
try container.encode(URLBuilder.host, forKey: .host)

/// Network
if (customNode != nil) {
try container.encode(customNode, forKey: .ETH_NETWORK)

if let node = customNode {
try container.encode(node, forKey: .ethNetwork)
}
if (ethNetwork != nil) {
try container.encode(ethNetwork?.rawValue, forKey: .ETH_NETWORK)
if let network = ethNetwork {
try container.encode(network.rawValue, forKey: .ethNetwork)
}

try container.encode(ExtensionObject(productType: productType), forKey: .ext)
}
}
}
Expand All @@ -83,35 +96,3 @@ public struct CustomNodeConfiguration: Encodable {
self.chainId = chainId
}
}


// MARK: -- Extension
struct ExtensionObject: Encodable {

let productType: ProductType

init(productType: ProductType) {
self.productType = productType
}

enum CodingKeys: String, CodingKey {
case connect
}

func encode(to encoder: Encoder) throws {
var container = encoder.container(keyedBy: CodingKeys.self)

switch productType {
case .MC:
try container.encode(MCConfig(), forKey: .connect)
break
default:
break
}

}
}

internal struct MCConfig: Encodable {
let mc = true
}
17 changes: 17 additions & 0 deletions Sources/MagicSDK/Modules/Auth/AuthModule.swift
Original file line number Diff line number Diff line change
Expand Up @@ -9,8 +9,14 @@
import Foundation
import MagicSDK_Web3
import PromiseKit
import os

public class AuthModule: BaseModule {
@available(iOS 14.0, *)
private static let logger = Logger(
subsystem: Bundle.main.bundleIdentifier!,
category: String(describing: AuthModule.self)
)

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

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

// MARK: - Login with EmailOTP
public func loginWithEmailOTP (_ configuration: LoginWithEmailOTPConfiguration, response: @escaping Web3ResponseCompletion<String> ) {
if #available(iOS 14.0, *) {
AuthModule.logger.warning("loginWithEmailOTP: \(BaseWarningLog.MA_Method)")
} else {
print("loginWithEmailOTP: \(BaseWarningLog.MA_Method)")
}

let request = RPCRequest<[LoginWithEmailOTPConfiguration]>(method: AuthMethod.magic_auth_login_with_email_otp.rawValue, params: [configuration])
self.provider.send(request: request, response: response)
}
Expand Down
13 changes: 13 additions & 0 deletions Sources/MagicSDK/Modules/BaseWarningLog.swift
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
//
// BaseWarningLog.swift
//
//
// Created by Arian Flores - Magic on 2/16/23.
//

import Foundation

struct BaseWarningLog {
static let MA_Method = "This method only works with Magic Auth API Keys"
static let MC_Method = "This method only works with Magic Connect API Keys"
}
18 changes: 0 additions & 18 deletions Sources/MagicSDK/Modules/Connect/ConnectConfiguration.swift

This file was deleted.

53 changes: 0 additions & 53 deletions Sources/MagicSDK/Modules/Connect/ConnectModule.swift

This file was deleted.

Loading

0 comments on commit c7ecb18

Please sign in to comment.