From 74d541d8da74837c14416fae1cd81cff667ce0f7 Mon Sep 17 00:00:00 2001 From: James Sangalli Date: Sun, 5 Jan 2020 16:14:30 +0800 Subject: [PATCH] clean up force unwraps --- .../Coordinators/UniversalLinkCoordinator.swift | 4 +--- AlphaWallet/Redeem/Helpers/CreateRedeem.swift | 6 +++--- AlphaWallet/Redeem/Helpers/SignatureHelper.swift | 11 ++++++----- .../TokenCardRedemptionViewController.swift | 10 +++++++--- 4 files changed, 17 insertions(+), 14 deletions(-) diff --git a/AlphaWallet/Market/Coordinators/UniversalLinkCoordinator.swift b/AlphaWallet/Market/Coordinators/UniversalLinkCoordinator.swift index f0a5c5af2..6e6526061 100644 --- a/AlphaWallet/Market/Coordinators/UniversalLinkCoordinator.swift +++ b/AlphaWallet/Market/Coordinators/UniversalLinkCoordinator.swift @@ -609,9 +609,7 @@ class UniversalLinkCoordinator: Coordinator { } private func convert(ethCost: BigUInt) -> Decimal { - //TODO extract constant. Used elsewhere too - let divideAmount = Decimal(string: "1000000000000000000")! - let etherCostDecimal = Decimal(string: ethCost.description)! / divideAmount + let etherCostDecimal = Decimal(string: ethCost.description)! / Decimal(EthereumUnit.ether.rawValue) return etherCostDecimal } } diff --git a/AlphaWallet/Redeem/Helpers/CreateRedeem.swift b/AlphaWallet/Redeem/Helpers/CreateRedeem.swift index 0723ed34b..994bc6237 100644 --- a/AlphaWallet/Redeem/Helpers/CreateRedeem.swift +++ b/AlphaWallet/Redeem/Helpers/CreateRedeem.swift @@ -64,10 +64,10 @@ class CreateRedeem { /* the method here is easier to express with matrix programming like this: indexList = indexList - correctionFactor # reduce every element of the list by an int selection = sum(2^indexList) # raise every element and add the result back */ - var bitFieldLookup = BigUInt("0")! + var bitFieldLookup = BigUInt(0) for _ in 0...indices.count - 1 { - let adder = BigUInt("2")?.power(Int(indices[0]) - correctionFactor) - bitFieldLookup = bitFieldLookup.advanced(by: BigInt(adder!)) + let adder = BigUInt(2).power(Int(indices[0]) - correctionFactor) + bitFieldLookup = bitFieldLookup.advanced(by: BigInt(adder)) } let bitIntLength: Int = bitFieldLookup.description.count diff --git a/AlphaWallet/Redeem/Helpers/SignatureHelper.swift b/AlphaWallet/Redeem/Helpers/SignatureHelper.swift index 4d3523ea5..61edcf0f5 100644 --- a/AlphaWallet/Redeem/Helpers/SignatureHelper.swift +++ b/AlphaWallet/Redeem/Helpers/SignatureHelper.swift @@ -10,8 +10,8 @@ import Foundation import BigInt class SignatureHelper { - class func signatureAsHex(for message: String, account: EthereumAccount) -> String? { - let keystore = try! EtherKeystore() + class func signatureAsHex(for message: String, account: EthereumAccount) throws -> String? { + let keystore = try EtherKeystore() let signature = keystore.signMessageData(message.data(using: String.Encoding.utf8), for: account) let signatureHex = try? signature.dematerialize().hex(options: .upperCase) guard let data = signatureHex else { @@ -20,8 +20,9 @@ class SignatureHelper { return data } - class func signatureAsDecimal(for message: String, account: EthereumAccount) -> String? { - let signatureHex = signatureAsHex(for: message, account: account)! - return BigInt(signatureHex, radix: 16)!.description + class func signatureAsDecimal(for message: String, account: EthereumAccount) throws -> String? { + guard let signatureHex = try signatureAsHex(for: message, account: account) else { return nil } + guard let signatureDecimalString = BigInt(signatureHex, radix: 16)?.description else { return nil } + return signatureDecimalString } } diff --git a/AlphaWallet/Redeem/ViewControllers/TokenCardRedemptionViewController.swift b/AlphaWallet/Redeem/ViewControllers/TokenCardRedemptionViewController.swift index a1d6c5585..4adcc48b4 100644 --- a/AlphaWallet/Redeem/ViewControllers/TokenCardRedemptionViewController.swift +++ b/AlphaWallet/Redeem/ViewControllers/TokenCardRedemptionViewController.swift @@ -114,9 +114,13 @@ class TokenCardRedemptionViewController: UIViewController, TokenVerifiableStatus } switch session.account.type { case .real(let account): - let decimalSignature = SignatureHelper.signatureAsDecimal(for: redeemData.message, account: account)! - let qrCodeInfo = redeemData.qrCode + decimalSignature - imageView.image = qrCodeInfo.toQRCode() + do { + guard let decimalSignature = try SignatureHelper.signatureAsDecimal(for: redeemData.message, account: account) else { break } + let qrCodeInfo = redeemData.qrCode + decimalSignature + imageView.image = qrCodeInfo.toQRCode() + } catch { + break + } case .watch: break } }