Skip to content

Commit

Permalink
code format 2
Browse files Browse the repository at this point in the history
  • Loading branch information
zhouxl committed Nov 12, 2024
1 parent 75886f2 commit 72b1cb1
Show file tree
Hide file tree
Showing 14 changed files with 1,273 additions and 611 deletions.
139 changes: 93 additions & 46 deletions FRW/Modules/TrustProvider/TrustJSMessageHandler.swift
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,8 @@ import web3swift
import Web3Wallet
import WebKit

// MARK: - TrustJSMessageHandler

class TrustJSMessageHandler: NSObject {
weak var webVC: BrowserViewController?

Expand All @@ -31,38 +33,34 @@ class TrustJSMessageHandler: NSObject {

extension TrustJSMessageHandler {
private func extractMethod(json: [String: Any]) -> TrustAppMethod? {
guard
let name = json["name"] as? String
guard let name = json["name"] as? String
else {
return nil
}
return TrustAppMethod(rawValue: name)
}

private func extractNetwork(json: [String: Any]) -> ProviderNetwork? {
guard
let network = json["network"] as? String
guard let network = json["network"] as? String
else {
return nil
}
return ProviderNetwork(rawValue: network)
}

private func extractMessage(json: [String: Any]) -> Data? {
guard
let params = json["object"] as? [String: Any],
let string = params["data"] as? String,
let data = Data(hexString: string)
guard let params = json["object"] as? [String: Any],
let string = params["data"] as? String,
let data = Data(hexString: string)
else {
return nil
}
return data
}

private func extractRaw(json: [String: Any]) -> String? {
guard
let params = json["object"] as? [String: Any],
let raw = params["raw"] as? String
guard let params = json["object"] as? [String: Any],
let raw = params["raw"] as? String
else {
return nil
}
Expand All @@ -77,18 +75,19 @@ extension TrustJSMessageHandler {
}

private func extractEthereumChainId(json: [String: Any]) -> Int? {
guard
let params = json["object"] as? [String: Any],
let string = params["chainId"] as? String,
let chainId = Int(String(string.dropFirst(2)), radix: 16),
chainId > 0
guard let params = json["object"] as? [String: Any],
let string = params["chainId"] as? String,
let chainId = Int(String(string.dropFirst(2)), radix: 16),
chainId > 0
else {
return nil
}
return chainId
}
}

// MARK: WKScriptMessageHandler

extension TrustJSMessageHandler: WKScriptMessageHandler {
func userContentController(_: WKUserContentController, didReceive message: WKScriptMessage) {
let json = message.json
Expand Down Expand Up @@ -150,8 +149,7 @@ extension TrustJSMessageHandler: WKScriptMessageHandler {
log.info("[Trust] switchEthereumChain")
switch network {
case .ethereum:
guard
let chainId = extractEthereumChainId(json: json)
guard let chainId = extractEthereumChainId(json: json)
else {
print("chain id is invalid")
return
Expand All @@ -177,12 +175,13 @@ extension TrustJSMessageHandler {

let title = webVC?.webView.title ?? "unknown"
let chainID = LocalUserDefaults.shared.flowNetwork.toFlowType()
let vm = BrowserAuthnViewModel(title: title,
url: url?.host ?? "unknown",
logo: url?.absoluteString.toFavIcon()?.absoluteString,
walletAddress: address,
network: chainID)
{ [weak self] result in
let vm = BrowserAuthnViewModel(
title: title,
url: url?.host ?? "unknown",
logo: url?.absoluteString.toFavIcon()?.absoluteString,
walletAddress: address,
network: chainID
) { [weak self] result in
guard let self = self else {
return
}
Expand All @@ -204,10 +203,19 @@ extension TrustJSMessageHandler {
Router.route(to: RouteMap.Explore.authn(vm))
}

MoveAssetsAction.shared.startBrowserWithMoveAssets(appName: webVC?.webView.title, callback: callback)
MoveAssetsAction.shared.startBrowserWithMoveAssets(
appName: webVC?.webView.title,
callback: callback
)
}

private func handleSignPersonal(url: URL?, network: ProviderNetwork, id: Int64, data: Data, addPrefix _: Bool) {
private func handleSignPersonal(
url: URL?,
network: ProviderNetwork,
id: Int64,
data: Data,
addPrefix _: Bool
) {
Task {
await TrustJSMessageHandler.checkCoa()
}
Expand All @@ -216,11 +224,12 @@ extension TrustJSMessageHandler {
title = "unknown"
}

let vm = BrowserSignMessageViewModel(title: title,
url: url?.absoluteString ?? "unknown",
logo: url?.absoluteString.toFavIcon()?.absoluteString,
cadence: data.hexString)
{ [weak self] result in
let vm = BrowserSignMessageViewModel(
title: title,
url: url?.absoluteString ?? "unknown",
logo: url?.absoluteString.toFavIcon()?.absoluteString,
cadence: data.hexString
) { [weak self] result in
guard let self = self else {
return
}
Expand All @@ -239,11 +248,20 @@ extension TrustJSMessageHandler {
return
}
let keyIndex = BigUInt(WalletManager.shared.keyIndex)
let proof = COAOwnershipProof(keyIninces: [keyIndex], address: address.data, capabilityPath: "evm", signatures: [sig])
let proof = COAOwnershipProof(
keyIninces: [keyIndex],
address: address.data,
capabilityPath: "evm",
signatures: [sig]
)
guard let encoded = RLP.encode(proof.rlpList) else {
return
}
webVC?.webView.tw.send(network: .ethereum, result: encoded.hexString.addHexPrefix(), to: id)
webVC?.webView.tw.send(
network: .ethereum,
result: encoded.hexString.addHexPrefix(),
to: id
)
} else {
webVC?.webView.tw.send(network: .ethereum, error: "Canceled", to: id)
}
Expand All @@ -261,7 +279,12 @@ extension TrustJSMessageHandler {
title = "unknown"
}

let vm = BrowserSignTypedMessageViewModel(title: title, urlString: url?.absoluteString ?? "unknown", logo: url?.absoluteString.toFavIcon()?.absoluteString, rawString: raw) { [weak self] result in
let vm = BrowserSignTypedMessageViewModel(
title: title,
urlString: url?.absoluteString ?? "unknown",
logo: url?.absoluteString.toFavIcon()?.absoluteString,
rawString: raw
) { [weak self] result in
guard let self = self else {
return
}
Expand All @@ -278,11 +301,20 @@ extension TrustJSMessageHandler {
return
}
let keyIndex = BigUInt(WalletManager.shared.keyIndex)
let proof = COAOwnershipProof(keyIninces: [keyIndex], address: address.data, capabilityPath: "evm", signatures: [sig])
let proof = COAOwnershipProof(
keyIninces: [keyIndex],
address: address.data,
capabilityPath: "evm",
signatures: [sig]
)
guard let encoded = RLP.encode(proof.rlpList) else {
return
}
webVC?.webView.tw.send(network: .ethereum, result: encoded.hexString.addHexPrefix(), to: id)
webVC?.webView.tw.send(
network: .ethereum,
result: encoded.hexString.addHexPrefix(),
to: id
)
} else {
webVC?.webView.tw.send(network: .ethereum, error: "Canceled", to: id)
}
Expand All @@ -291,7 +323,12 @@ extension TrustJSMessageHandler {
Router.route(to: RouteMap.Explore.signTypedMessage(vm))
}

private func handleSendTransaction(url: URL?, network _: ProviderNetwork, id: Int64, info: [String: Any]) {
private func handleSendTransaction(
url: URL?,
network _: ProviderNetwork,
id: Int64,
info: [String: Any]
) {
var title = webVC?.webView.title ?? "unknown"
if title.isEmpty {
title = "unknown"
Expand All @@ -314,12 +351,13 @@ extension TrustJSMessageHandler {
.uint64(receiveModel.gasValue),
]

let vm = BrowserAuthzViewModel(title: title,
url: url?.absoluteString ?? "unknown",
logo: url?.absoluteString.toFavIcon()?.absoluteString,
cadence: originCadence,
arguments: args.toArguments())
{ [weak self] result in
let vm = BrowserAuthzViewModel(
title: title,
url: url?.absoluteString ?? "unknown",
logo: url?.absoluteString.toFavIcon()?.absoluteString,
cadence: originCadence,
arguments: args.toArguments()
) { [weak self] result in

guard let self = self else {
self?.webVC?.webView.tw.send(network: .ethereum, error: "Canceled", to: id)
Expand All @@ -333,7 +371,12 @@ extension TrustJSMessageHandler {

Task {
do {
let txid = try await FlowNetwork.sendTransaction(amount: receiveModel.amount, data: receiveModel.dataValue, toAddress: toAddr, gas: receiveModel.gasValue)
let txid = try await FlowNetwork.sendTransaction(
amount: receiveModel.amount,
data: receiveModel.dataValue,
toAddress: toAddr,
gas: receiveModel.gasValue
)
let holder = TransactionManager.TransactionHolder(id: txid, type: .transferCoin)
TransactionManager.shared.newTransaction(holder: holder)
let result = try await txid.onceSealed()
Expand Down Expand Up @@ -387,15 +430,19 @@ extension TrustJSMessageHandler {
self?.webVC?.webView.tw.sendNull(network: .ethereum, id: id)
} else {
log.error("Unknown chain id: \(chainId)")
self?.webVC?.webView.tw.send(network: .ethereum, error: "Unknown chain id", to: id)
self?.webVC?.webView.tw.send(
network: .ethereum,
error: "Unknown chain id",
to: id
)
}
}
Router.route(to: RouteMap.Explore.switchNetwork(fromId, toId, callback))
}
}

private func signWithMessage(data: Data) -> Data? {
return WalletManager.shared.signSync(signableData: data)
WalletManager.shared.signSync(signableData: data)
}

private func cancel(id: Int64) {
Expand Down
25 changes: 18 additions & 7 deletions FRW/Modules/Wallet/Card/StyleView/MatrixRainView.swift
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,8 @@

import SwiftUI

// MARK: - MatrixRainView

struct MatrixRainView: View {
var body: some View {
ZStack {
Expand All @@ -19,7 +21,7 @@ struct MatrixRainView: View {
// Repeating the effects until it occupied the full screen
// With the help of ForEach
// For Count since our font size is 25 so width/fontSize will the give the count
ForEach(1 ... Int(size.width / 25), id: \.self) { _ in
ForEach(1...Int(size.width / 25), id: \.self) { _ in
MatrixRainCharacters(size: size)
}
}
Expand All @@ -29,23 +31,27 @@ struct MatrixRainView: View {
}
}

// MARK: - MatrixRainCharacters

struct MatrixRainCharacters: View {
var size: CGSize

// MARK: Animation Properties

@State var startAnimation: Bool = false
@State
var startAnimation: Bool = false

@State var random: Int = 0
@State
var random: Int = 0

var body: some View {
// Random Height
let randomHeight: CGFloat = .random(in: (size.height / 2) ... size.height)
let randomHeight: CGFloat = .random(in: (size.height / 2)...size.height)

VStack {
// MARK: Iterating String

ForEach(0 ..< constant.count, id: \.self) { index in
ForEach(0..<constant.count, id: \.self) { index in

// Retriving Character at String
let character = Array(constant)[getRandomIndex(index: index)]
Expand Down Expand Up @@ -78,14 +84,17 @@ struct MatrixRainCharacters: View {
// Moving Slowly down with linear Animation
// Endless loop without reversing
// Random delay for more fluent Effect
withAnimation(.linear(duration: 10).delay(.random(in: 0 ... 2)).repeatForever(autoreverses: false)) {
withAnimation(
.linear(duration: 10).delay(.random(in: 0...2))
.repeatForever(autoreverses: false)
) {
startAnimation = true
}
}
// Timer
.onReceive(Timer.publish(every: 0.2, on: .main, in: .common).autoconnect()) { _ in

random = Int.random(in: 0 ..< constant.count)
random = Int.random(in: 0..<constant.count)
}
}

Expand All @@ -105,6 +114,8 @@ struct MatrixRainCharacters: View {
}
}

// MARK: - MatrixRainView_Previews

struct MatrixRainView_Previews: PreviewProvider {
static var previews: some View {
ZStack {
Expand Down
Loading

0 comments on commit 72b1cb1

Please sign in to comment.