Skip to content

Commit

Permalink
Setup config
Browse files Browse the repository at this point in the history
  • Loading branch information
0xChqrles committed Jun 19, 2024
1 parent fa5620f commit a0013cd
Show file tree
Hide file tree
Showing 26 changed files with 491 additions and 380 deletions.
42 changes: 42 additions & 0 deletions app/App/Constants/Configuration.swift
Original file line number Diff line number Diff line change
@@ -0,0 +1,42 @@
//
// Configuration.swift
// Vault
//
// Created by Charles Lanier on 18/06/2024.
//

import Foundation

enum AppConfiguration {
enum Error: Swift.Error {
case missingKey, invalidValue
}

static func value<T>(for key: String) throws -> T where T: LosslessStringConvertible {
guard let object = Bundle.main.object(forInfoDictionaryKey: key) else {
throw Error.missingKey
}

switch object {
case let value as T:
return value
case let string as String:
guard let value = T(string) else { fallthrough }
return value
default:
throw Error.invalidValue
}
}

enum API {
static var baseURL: URL? {
do {
let base: String = try AppConfiguration.value(for: "API_BASE_URL")

return URL(string: "https://" + base)
} catch {
return nil
}
}
}
}
12 changes: 2 additions & 10 deletions app/App/Constants/Constants.swift
Original file line number Diff line number Diff line change
Expand Up @@ -24,23 +24,15 @@ struct Constants {

// MARK: ENV

static let vaultBaseURL: URL = {
static let vaultBaseURL: URL? = {
guard let urlString = ProcessInfo.processInfo.environment["VAULT_API_BASE_URL"],
let url = URL(string: urlString) else {
fatalError("Vault API Base URL not configured properly.")
return nil
}

return url
}()

static let starknetRpcApiKey: String = {
guard let apiKey = ProcessInfo.processInfo.environment["STARKNET_RPC_API_KEY"] else {
fatalError("Starknet RPC API key not configured properly.")
}

return apiKey
}()

// MARK: ICONS

struct Icons {
Expand Down
85 changes: 39 additions & 46 deletions app/App/Models/Model.swift
Original file line number Diff line number Diff line change
Expand Up @@ -33,6 +33,7 @@ class Model: ObservableObject {
// App
@Published var isLoading = false
@Published var showMessage = false
@Published var isProperlyConfigured: Bool

// Sending USDC
@Published var recipientContact: Contact?
Expand Down Expand Up @@ -86,28 +87,18 @@ class Model: ObservableObject {
.sorted { $0.name < $1.name }
}()

private lazy var provider: StarknetProviderProtocol = StarknetProvider(url: "https://rpc.nethermind.io/sepolia-juno/?apikey=\(Constants.starknetRpcApiKey)")!

private lazy var signer = P256Signer()

private lazy var account: StarknetAccountProtocol = {
return StarknetAccount(
address: Felt(stringLiteral: self.address),
signer: self.signer,
provider: self.provider,
chainId: .sepolia,
cairoVersion: .one
)
}()

init(vaultService: VaultService) {
// Vault API
self.vaultService = vaultService

self.isProperlyConfigured = self.vaultService.healthCheck

// Contacts
checkContactsAuthorizationStatus()

// self.address = "0x039fd69d03e3735490a86925612072c5612cbf7a0223678619a1b7f30f4bdc8f"
self.address = "0x039fd69d03e3735490a86925612072c5612cbf7a0223678619a1b7f30f4bdc8f"

self.getBalance()
}
Expand All @@ -126,17 +117,19 @@ extension Model {

// TODO: implement nickname support
vaultService.send(GetOTP(phoneNumber: phoneNumber, nickname: "nickname")) { result in
self.isLoading = false
DispatchQueue.main.async {
self.isLoading = false

switch result {
case .success(let response):
onSuccess()
switch result {
case .success:
onSuccess()

case .failure(let error):
// TODO: Handle error
#if DEBUG
print(error)
#endif
case .failure(let error):
// TODO: Handle error
#if DEBUG
print(error)
#endif
}
}
}
}
Expand All @@ -154,18 +147,20 @@ extension Model {
self.isLoading = true

vaultService.send(VerifyOTP(phoneNumber: phoneNumber, sentOTP: otp, publicKey: publicKey)) { result in
self.isLoading = false
DispatchQueue.main.async {
self.isLoading = false

switch result {
case .success(let response):
self.address = response.contract_address
onSuccess()
switch result {
case .success(let response):
self.address = response.contract_address
onSuccess()

case .failure(let error):
// TODO: Handle error
#if DEBUG
print(error)
#endif
case .failure(let error):
// TODO: Handle error
#if DEBUG
print(error)
#endif
}
}
}
} catch {
Expand All @@ -178,13 +173,15 @@ extension Model {

func getBalance() {
vaultService.send(GetBalance(address: self.address)) { result in
switch result {
case .success(let response):
self.balance = USDCAmount(from: response.balance)!
DispatchQueue.main.async {
switch result {
case .success(let response):
self.balance = USDCAmount(from: response.balance)!

case .failure(let error):
// TODO: Handle error
print(error)
case .failure(let error):
// TODO: Handle error
print(error)
}
}
}
}
Expand Down Expand Up @@ -236,17 +233,13 @@ extension Model {

extension Model {

// invoke

func executeTransaction(signedTransaction: StarknetInvokeTransactionV1) async throws -> StarknetInvokeTransactionResponse {
return try await self.provider.addInvokeTransaction(signedTransaction)
}

// sign

func signOutsideExecution(outsideExecution: OutsideExecution) async throws -> StarknetSignature {
print("MessageHash: \(self.outsideExecution!.getMessageHash(forSigner: self.account.address))")
return try self.signer.sign(transactionHash: self.outsideExecution!.getMessageHash(forSigner: self.account.address))
let feltAddress = Felt(fromHex: self.address)!

print("MessageHash: \(self.outsideExecution!.getMessageHash(forSigner: feltAddress))")
return try self.signer.sign(transactionHash: self.outsideExecution!.getMessageHash(forSigner: feltAddress))
}

// addr utils
Expand Down
4 changes: 3 additions & 1 deletion app/App/Navigation/ContentView.swift
Original file line number Diff line number Diff line change
Expand Up @@ -34,7 +34,9 @@ struct ContentView: View {
}

var body: some View {
if self.isOnboarded {
if !self.model.isProperlyConfigured {
ErrorView()
} else if self.isOnboarded {
ZStack(alignment: .bottom) {
TabView(selection: $selectedTab) {
NavigationStack {
Expand Down
23 changes: 23 additions & 0 deletions app/App/Navigation/ErrorView.swift
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
//
// ErrorView.swift
// Vault
//
// Created by Charles Lanier on 18/06/2024.
//

import SwiftUI

struct ErrorView: View {
var body: some View {
VStack {
Text("An error has occurred").textTheme(.headlineLarge)
Text("Please contact our support.").textTheme(.subtitle)
}
.frame(maxWidth: .infinity, maxHeight: .infinity)
.defaultBackground()
}
}

#Preview {
ErrorView()
}
16 changes: 16 additions & 0 deletions app/App/Services/VaultAPI/Models/APIRequest.swift
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,11 @@

import Foundation

public enum HTTPMethod: String {
case GET
case POST
}

/// All requests must conform to this protocol
/// - Discussion: You must conform to Encodable too, so that all stored public parameters
/// of types conforming this protocol will be encoded as parameters.
Expand All @@ -16,4 +21,15 @@ public protocol APIRequest: Encodable {

/// Endpoint for this request (the last part of the URL)
var resourceName: String { get }

var httpMethod: HTTPMethod { get }

var headers: [String: String] { get }
}

public extension APIRequest {

var headers: [String: String] {
return [:]
}
}
14 changes: 0 additions & 14 deletions app/App/Services/VaultAPI/Models/DataContainer.swift

This file was deleted.

13 changes: 13 additions & 0 deletions app/App/Services/VaultAPI/Models/Deployment.swift
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
//
// Deployment.swift
// Vault
//
// Created by Charles Lanier on 17/06/2024.
//

import Foundation

public struct Deployment: Decodable {
public let contract_address: String
}

10 changes: 10 additions & 0 deletions app/App/Services/VaultAPI/Models/Empty.swift
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
//
// Empty.swift
// Vault
//
// Created by Charles Lanier on 17/06/2024.
//

import Foundation

public struct Empty: Decodable {}
13 changes: 13 additions & 0 deletions app/App/Services/VaultAPI/Models/Error.swift
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
//
// Error.swift
// Vault
//
// Created by Charles Lanier on 14/06/2024.
//

import Foundation

public struct ErrorResponse: Decodable {
/// Message that usually gives more information about some error
public let message: String?
}
12 changes: 12 additions & 0 deletions app/App/Services/VaultAPI/Models/Execution.swift
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
//
// Execution.swift
// Vault
//
// Created by Charles Lanier on 16/06/2024.
//

import Foundation

public struct Execution: Decodable {
public let transaction_hash: String
}
1 change: 1 addition & 0 deletions app/App/Services/VaultAPI/Models/VaultError.swift
Original file line number Diff line number Diff line change
Expand Up @@ -10,5 +10,6 @@ import Foundation
public enum VaultError: Error {
case encoding
case decoding
case unknown
case server(message: String)
}
19 changes: 0 additions & 19 deletions app/App/Services/VaultAPI/Models/VaultResponse.swift

This file was deleted.

Loading

0 comments on commit a0013cd

Please sign in to comment.