Skip to content

Commit

Permalink
Merge pull request #64 from soramitsu/FLW-1725-CEX-accounts-QR-codes
Browse files Browse the repository at this point in the history
Cex qr decoder
  • Loading branch information
bnsports authored Apr 18, 2022
2 parents e8111ba + 464221e commit 8ffd885
Show file tree
Hide file tree
Showing 6 changed files with 67 additions and 31 deletions.
11 changes: 11 additions & 0 deletions FearlessUtils/Classes/QR/CexQRCommon.swift
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
import Foundation

public protocol CexQRDecodable: QRDecodable {}

public struct CexQRInfo: QRInfo, Equatable {
public let address: String

public init(address: String) {
self.address = address
}
}
14 changes: 14 additions & 0 deletions FearlessUtils/Classes/QR/CexQRDecoder.swift
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
import Foundation

public class CexQRDecoder: CexQRDecodable {

public init() {}

public func decode(data: Data) throws -> QRInfo {
guard let address = String(data: data, encoding: .utf8) else {
throw QRDecoderError.brokenFormat
}

return CexQRInfo(address: address)
}
}
25 changes: 25 additions & 0 deletions FearlessUtils/Classes/QR/QRCommon.swift
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
import Foundation

public protocol QRInfo {
var address: String { get }
}

public protocol QRDecodable {
func decode(data: Data) throws -> QRInfo
}

public protocol QREncodable {
func encode(info: SubstrateQRInfo) throws -> Data
}

public enum QREncoderError: Error, Equatable {
case brokenData
}

public enum QRDecoderError: Error, Equatable {
case brokenFormat
case unexpectedNumberOfFields
case undefinedPrefix
case accountIdMismatch
case wrongDecoder
}
36 changes: 11 additions & 25 deletions FearlessUtils/Classes/QR/SubstrateQRCommon.swift
Original file line number Diff line number Diff line change
@@ -1,41 +1,27 @@
import Foundation

public struct SubstrateQRInfo: Equatable {
public protocol SubstrateQREncodable: QREncodable {}
public protocol SubstrateQRDecodable: QRDecodable {}

public struct SubstrateQRInfo: QRInfo, Equatable {
public let prefix: String
public let address: String
public let rawPublicKey: Data
public let username: String?

public init(prefix: String = SubstrateQR.prefix,
address: String,
rawPublicKey: Data,
username: String?) {

public init(
prefix: String = SubstrateQR.prefix,
address: String,
rawPublicKey: Data,
username: String?
) {
self.prefix = prefix
self.address = address
self.rawPublicKey = rawPublicKey
self.username = username
}
}

public protocol SubstrateQREncodable {
func encode(info: SubstrateQRInfo) throws -> Data
}

public protocol SubstrateQRDecodable {
func decode(data: Data) throws -> SubstrateQRInfo
}

public enum SubstrateQREncoderError: Error, Equatable {
case brokenData
}

public enum SubstrateQRDecoderError: Error, Equatable {
case brokenFormat
case unexpectedNumberOfFields
case undefinedPrefix
case accountIdMismatch
}

public struct SubstrateQR {
public static let prefix: String = "substrate"
public static let fieldsSeparator: String = ":"
Expand Down
10 changes: 5 additions & 5 deletions FearlessUtils/Classes/QR/SubstrateQRDecoder.swift
Original file line number Diff line number Diff line change
Expand Up @@ -16,26 +16,26 @@ open class SubstrateQRDecoder: SubstrateQRDecodable {
self.separator = separator
}

public func decode(data: Data) throws -> SubstrateQRInfo {
public func decode(data: Data) throws -> QRInfo {
guard let fields = String(data: data, encoding: .utf8)?
.components(separatedBy: separator) else {
throw SubstrateQRDecoderError.brokenFormat
throw QRDecoderError.brokenFormat
}

guard fields.count >= 3, fields.count <= 4 else {
throw SubstrateQRDecoderError.unexpectedNumberOfFields
throw QRDecoderError.unexpectedNumberOfFields
}

guard fields[0] == prefix else {
throw SubstrateQRDecoderError.undefinedPrefix
throw QRDecoderError.undefinedPrefix
}

let address = fields[1]
let accountId = try addressFactory.accountId(fromAddress: address, type: chainType)
let publicKey = try Data(hexString: fields[2])

guard publicKey.matchPublicKeyToAccountId(accountId) else {
throw SubstrateQRDecoderError.accountIdMismatch
throw QRDecoderError.accountIdMismatch
}

let username = fields.count > 3 ? fields[3] : nil
Expand Down
2 changes: 1 addition & 1 deletion FearlessUtils/Classes/QR/SubstrateQREncoder.swift
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,7 @@ open class SubstrateQREncoder: SubstrateQREncodable {
}

guard let data = fields.joined(separator: separator).data(using: .utf8) else {
throw SubstrateQREncoderError.brokenData
throw QREncoderError.brokenData
}

return data
Expand Down

0 comments on commit 8ffd885

Please sign in to comment.