Skip to content

Commit

Permalink
updating to bech32
Browse files Browse the repository at this point in the history
  • Loading branch information
Sajjon committed Jun 12, 2019
1 parent bb0500d commit 1f9cb7c
Show file tree
Hide file tree
Showing 21 changed files with 143 additions and 101 deletions.
5 changes: 3 additions & 2 deletions Podfile
Original file line number Diff line number Diff line change
Expand Up @@ -4,14 +4,15 @@ use_modular_headers!

def shared
# The Zilliqa Swift SDK
pod 'Zesame', '~> 1.0.0'
pod 'BigInt', :git => 'https://github.com/attaswift/BigInt.git', :branch => 'master'
pod 'Zesame', :git => 'https://github.com/OpenZesame/Zesame.git', :branch => 'bech32'

# Used for Crash reporting
pod 'Firebase/Core'
pod 'Fabric', '~> 1.9.0'
pod 'Crashlytics', '~> 3.12.0'

pod 'SwiftProtobuf', '1.5.0'

# RxSwift made this app
pod 'RxCocoa'

Expand Down
42 changes: 21 additions & 21 deletions Podfile.lock
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
PODS:
- Alamofire (5.0.0-beta.5)
- Alamofire (5.0.0-beta.6)
- BigInt (4.0.0)
- Crashlytics (3.12.0):
- Fabric (~> 1.9.0)
Expand Down Expand Up @@ -75,28 +75,27 @@ PODS:
- Differentiator (~> 3.0)
- RxCocoa (~> 4.0)
- RxSwift (~> 4.0)
- RxSwift (4.4.1):
- RxAtomic (~> 4.4)
- RxSwift (4.5.0)
- RxTest (4.4.1):
- RxAtomic (~> 4.4)
- RxSwift (~> 4.0)
- SkyFloatingLabelTextField (3.6.0)
- swift_qrcodejs (1.0.1)
- SwiftGen (6.1.0)
- SwiftLint (0.31.0)
- SwiftProtobuf (1.4.0)
- SwiftProtobuf (1.5.0)
- SwiftyBeaver (1.6.2)
- TinyConstraints (3.3.1)
- Validator (3.1.1)
- Zesame (1.0.0):
- Alamofire (~> 5.0.0-beta.3)
- Alamofire (~> 5.0.0-beta.4)
- BigInt (~> 4.0.0)
- CryptoSwift (~> 1.0.0)
- EllipticCurveKit (~> 1.0.0)
- RxSwift
- SwiftProtobuf
- RxSwift (~> 4.5.0)
- SwiftProtobuf (~> 1.5.0)

DEPENDENCIES:
- BigInt (from `https://github.com/attaswift/BigInt.git`, branch `master`)
- Crashlytics (~> 3.12.0)
- DateToolsSwift
- EFQRCode
Expand All @@ -113,14 +112,16 @@ DEPENDENCIES:
- SkyFloatingLabelTextField
- SwiftGen
- SwiftLint
- SwiftProtobuf (= 1.5.0)
- SwiftyBeaver
- TinyConstraints
- Validator
- Zesame (~> 1.0.0)
- Zesame (from `https://github.com/OpenZesame/Zesame.git`, branch `bech32`)

SPEC REPOS:
https://github.com/cocoapods/specs.git:
- Alamofire
- BigInt
- Crashlytics
- CryptoSwift
- DateToolsSwift
Expand Down Expand Up @@ -153,20 +154,19 @@ SPEC REPOS:
- SwiftyBeaver
- TinyConstraints
- Validator
- Zesame

EXTERNAL SOURCES:
BigInt:
:branch: master
:git: https://github.com/attaswift/BigInt.git
Zesame:
:branch: bech32
:git: https://github.com/OpenZesame/Zesame.git

CHECKOUT OPTIONS:
BigInt:
:commit: 108801d539eb014469c758bc0d7f734690791fb4
:git: https://github.com/attaswift/BigInt.git
Zesame:
:commit: 2cc80066fbd3b0346269ddfa66defc597968ff06
:git: https://github.com/OpenZesame/Zesame.git

SPEC CHECKSUMS:
Alamofire: 9afb08f7f43efcc71c072374e720fcf67521317d
Alamofire: cd08a4402bd12cacd0c66f23fb12fec8d6b965e4
BigInt: 2aad1a9942dc932ec8b84290d2c564a3d76f97ab
Crashlytics: 07fb167b1694128c1c9a5a5cc319b0e9c3ca0933
CryptoSwift: d81eeaa59dc5a8d03720fe919a6fd07b51f7439f
Expand All @@ -190,18 +190,18 @@ SPEC CHECKSUMS:
RxBlocking: 5508e90d36ca2304632dc9378ece4e9b5d4bf656
RxCocoa: 2f35a76bf8887872e28a1914112395b11b8e0e64
RxDataSources: a843bad90c29817f5923ec8163f4af2de084ceb3
RxSwift: 92fcf68dfef21f3e2ab1965363d9e7b3d787597e
RxSwift: f172070dfd1a93d70a9ab97a5a01166206e1c575
RxTest: f8e548755ca3c7502dc41c7d3b28802c90115a38
SkyFloatingLabelTextField: 38164979b79512f9ff9288ad8acfc4bbf5d843e3
swift_qrcodejs: c181fe5c849d30c699546a23762d7e3dd143ab37
SwiftGen: f872ca75cbd17bf7103c17f13dcfa0d9a15667b0
SwiftLint: 7a0227733d786395817373b2d0ca799fd0093ff3
SwiftProtobuf: b06646ed6cdcfc3fecd074c6000fe0c413140e4f
SwiftProtobuf: 241400280f912735c1e1b9fe675fdd2c6c4d42e2
SwiftyBeaver: 8e67ab3cd94389cbbb7a9c7cc02748d98bfee68e
TinyConstraints: dbf7519aefc01ea59d62244300c7718aec68e280
Validator: b34ab17a8fffd0c1ffd8f9002ead4d1e02cd9e6e
Zesame: b30550211eb93a2626483400f0e8e24439388411
Zesame: c7bbd6548ef648af5480338f37a94bd4fe0bc4e3

PODFILE CHECKSUM: 7d8b08bb7466142c47bc91d4be4b4cce5728f114
PODFILE CHECKSUM: 6e65ec8728ce77747f9fc47866b42ac7c7bfa1f6

COCOAPODS: 1.6.1
18 changes: 0 additions & 18 deletions Source/Application/Assets/Settings.bundle/Acknowledgements.plist
Original file line number Diff line number Diff line change
Expand Up @@ -995,24 +995,6 @@ Permission is hereby granted, free of charge, to any person obtaining a copy of
The above copyright notice and this permission notice shall be included in all copies or substantial portions of the Software.
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.</string>
<key>License</key>
<string>MIT</string>
<key>Title</key>
<string>RxAtomic</string>
<key>Type</key>
<string>PSGroupSpecifier</string>
</dict>
<dict>
<key>FooterText</key>
<string>**The MIT License**
**Copyright © 2015 Krunoslav Zaher**
**All rights reserved.**
Permission is hereby granted, free of charge, to any person obtaining a copy of this software and associated documentation files (the "Software"), to deal in the Software without restriction, including without limitation the rights to use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of the Software, and to permit persons to whom the Software is furnished to do so, subject to the following conditions:
The above copyright notice and this permission notice shall be included in all copies or substantial portions of the Software.
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.</string>
<key>License</key>
<string>MIT</string>
Expand Down
6 changes: 3 additions & 3 deletions Source/Application/Generated/SwiftGen/L10n-Constants.swift
Original file line number Diff line number Diff line change
Expand Up @@ -15,10 +15,10 @@ internal enum L10n {
internal enum Error {
internal enum Input {
internal enum Address {
/// This address isn't checksummed, yet can still be valid.
/// Invalid checksum.
internal static let addressNotChecksummed = L10n.tr("Localizable", "Error.Input.Address.AddressNotChecksummed")
/// Only 0-9 and A-F allowed
internal static let containsNonHexadecimal = L10n.tr("Localizable", "Error.Input.Address.ContainsNonHexadecimal")
/// Invalid address
internal static let invalid = L10n.tr("Localizable", "Error.Input.Address.Invalid")
/// Address is too long, should be %d
internal static func tooLong(_ p1: Int) -> String {
return L10n.tr("Localizable", "Error.Input.Address.TooLong", p1)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -27,32 +27,55 @@ import Zesame
struct AddressValidator: InputValidator {
typealias Input = String
typealias Output = Address
typealias Error = Address.Error
// typealias Error = Address.Error

func validate(input: Input) -> Validation<Output, Error> {
do {
let address = try Address(string: input)
if address.isChecksummed {
return .valid(address)
} else {
return .valid(address, remark: Error.notChecksummed)
}
return .valid(address)
} catch let addressError as Address.Error {
return .invalid(.error(addressError))
let mappedError = Error(fromAddressError: addressError)
return .invalid(.error(mappedError))
} catch {
incorrectImplementation("Address.Error should cover all errors")
}
}

}

extension AddressValidator {
enum Error: InputError {
case incorrectLength(expectedLength: Int, butGot: Int)
case notChecksummed
case noBech32NorHexstring
}
}

extension Address.Error: InputError {
public var errorMessage: String {
extension AddressValidator.Error {
init(fromAddressError: Zesame.Address.Error) {
switch fromAddressError {
case .incorrectLength(let expected, _, let butGot): self = .incorrectLength(expectedLength: expected, butGot: butGot)
case .notChecksummed: self = .notChecksummed
case .bech32DataEmpty, .notHexadecimal: self = .noBech32NorHexstring
case .invalidBech32Address(let bechError):
switch bechError {
case .checksumMismatch: self = .notChecksummed
default: self = .noBech32NorHexstring
}
}
}

var errorMessage: String {
let Message = L10n.Error.Input.Address.self

switch self {
case .notHexadecimal: return Message.containsNonHexadecimal
case .tooLong: return Message.tooLong(Address.lengthOfValidAddresses)
case .tooShort: return Message.tooShort(Address.lengthOfValidAddresses)
case .noBech32NorHexstring: return Message.invalid
case .incorrectLength(let expected, let incorrect):
if incorrect > expected {
return Message.tooLong(expected)
} else {
return Message.tooShort(expected)
}
case .notChecksummed: return Message.addressNotChecksummed
}
}
Expand Down
6 changes: 4 additions & 2 deletions Source/Application/Localization/en.lproj/Localizable.strings
Original file line number Diff line number Diff line change
Expand Up @@ -9,10 +9,12 @@
"Generic.Show" = "Show";
"Generic.Hide" = "Hide";

"Error.Input.Address.ContainsNonHexadecimal" = "Only 0-9 and A-F allowed";
//"Error.Input.Address.ContainsNonHexadecimal" = "Only 0-9 and A-F allowed";

"Error.Input.Address.Invalid" = "Invalid address";
"Error.Input.Address.TooShort" = "Address is too short, should be %d";
"Error.Input.Address.TooLong" = "Address is too long, should be %d";
"Error.Input.Address.AddressNotChecksummed" = "This address isn't checksummed, yet can still be valid.";
"Error.Input.Address.AddressNotChecksummed" = "Invalid checksum.";

"Error.Input.Amount.NonNumericString" = "String not a number";
"Error.Input.Amount.TooSmall" = "Must be at least %@";
Expand Down
14 changes: 14 additions & 0 deletions Source/Extensions/Foundation/CharacterSet_Hexadecimal.swift
Original file line number Diff line number Diff line change
Expand Up @@ -23,10 +23,24 @@
//

import Foundation
import Zesame

extension CharacterSet {
static var hexadecimalDigits: CharacterSet {
let afToAF = CharacterSet(charactersIn: "abcdefABCDEF")
return CharacterSet.decimalDigits.union(afToAF)
}

static var bech32: CharacterSet {

let lowercase = Zesame.Bech32.alphabetString.lowercased()
let uppercase = Zesame.Bech32.alphabetString.uppercased()

return CharacterSet(charactersIn: lowercase).union(CharacterSet(charactersIn: uppercase))
}

static var bech32OrHex: CharacterSet {
return CharacterSet.bech32.union(hexadecimalDigits)
}

}
Original file line number Diff line number Diff line change
Expand Up @@ -92,15 +92,15 @@ extension FloatingLabelTextField.Style {
)
}

static var address: FloatingLabelTextField.Style {
static var addressBech32OrHex: FloatingLabelTextField.Style {
return FloatingLabelTextField.Style(
typeOfInput: .hexadecimal
typeOfInput: .bech32OrHex
)
}

static var password: FloatingLabelTextField.Style {
return FloatingLabelTextField.Style(
typeOfInput: .text,
typeOfInput: .password,
isSecureTextEntry: true
)
}
Expand Down
27 changes: 16 additions & 11 deletions Source/Models/TransactionIntent.swift
Original file line number Diff line number Diff line change
Expand Up @@ -26,19 +26,24 @@ import Foundation
import Zesame

struct TransactionIntent: Codable {
let to: AddressChecksummed
let to: LegacyAddress
let amount: ZilAmount?

init(to recipient: AddressChecksummedConvertible, amount: ZilAmount? = nil) {
self.to = recipient.checksummedAddress
init(to recipient: LegacyAddress, amount: ZilAmount? = nil) {
self.to = recipient
self.amount = amount
}

init(to address: Address, amount: ZilAmount? = nil) throws {
let recipient = try address.toChecksummedLegacyAddress()
self.init(to: recipient, amount: amount)
}
}

extension TransactionIntent {
static func fromScannedQrCodeString(_ scannedString: String) -> TransactionIntent? {
if let adddress = try? AddressNotNecessarilyChecksummed(string: scannedString) {
return TransactionIntent(to: adddress)
if let address = try? Address(string: scannedString) {
return try? TransactionIntent(to: address)
} else {
guard
let json = scannedString.data(using: .utf8),
Expand All @@ -51,17 +56,17 @@ extension TransactionIntent {
}

extension TransactionIntent {
init?(to addresssHex: String, amount: String?) {
guard let recipient = try? AddressChecksummed(string: addresssHex) else { return nil }
self.init(to: recipient, amount: ZilAmount.fromQa(optionalString: amount))
init?(to recipientString: String, amount: String?) {
guard let recipient = try? Address(string: recipientString) else { return nil }
try? self.init(to: recipient, amount: ZilAmount.fromQa(optionalString: amount))
}

init?(queryParameters params: [URLQueryItem]) {
guard let hexAddress = params.first(where: { $0.name == TransactionIntent.CodingKeys.to.stringValue })?.value else {
guard let addressFromParam = params.first(where: { $0.name == TransactionIntent.CodingKeys.to.stringValue })?.value else {
return nil
}
let amount = params.first(where: { $0.name == TransactionIntent.CodingKeys.amount.stringValue })?.value
self.init(to: hexAddress, amount: amount)
self.init(to: addressFromParam, amount: amount)
}

var queryItems: [URLQueryItem] {
Expand Down
8 changes: 7 additions & 1 deletion Source/Models/Wallet.swift
Original file line number Diff line number Diff line change
Expand Up @@ -50,7 +50,13 @@ extension Wallet {
return wallet.keystore
}

var address: AddressChecksummed {
var bech32Address: Bech32Address {
do {
return try Bech32Address(ethStyleAddress: wallet.address, network: network)
} catch { incorrectImplementation("should work") }
}

var legacyAddress: LegacyAddress {
return wallet.address
}
}
2 changes: 1 addition & 1 deletion Source/Scenes/1_Main/1_Main/0_Main/MainViewModel.swift
Original file line number Diff line number Diff line change
Expand Up @@ -70,7 +70,7 @@ final class MainViewModel: BaseViewModel<

let latestBalanceAndNonce: Driver<BalanceResponse> = fetchTrigger.withLatestFrom(wallet).flatMapLatest { [unowned self] in
self.transactionUseCase
.getBalance(for: $0.address)
.getBalance(for: $0.legacyAddress)
.trackActivity(activityIndicator)
.asDriverOnErrorReturnEmpty()
.do(onNext: { [unowned self] in self.transactionUseCase.cacheBalance($0.balance) })
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -118,7 +118,7 @@ private extension PrepareTransactionView {

balanceLabels.withStyle(.horizontal)

recipientAddressField.withStyle(.address) {
recipientAddressField.withStyle(.addressBech32OrHex) {
$0.placeholder(.Field.recipient)
}

Expand Down Expand Up @@ -150,7 +150,7 @@ private extension PrepareTransactionView {
private extension PrepareTransactionView {
func prefillValuesForDebugBuilds() {
#if DEBUG
recipientAddressField.text = "89a810e6dB25912028f704b4947a6dCF724139Ae"
recipientAddressField.text = "zil175grxdeqchwnc0qghj8qsh5vnqwww353msqj82"
amountToSendField.text = Int.random(in: 100...500).description
gasPriceField.text = Int.random(in: 1000...2000).description

Expand Down
Loading

0 comments on commit 1f9cb7c

Please sign in to comment.