Skip to content

Commit

Permalink
Merge pull request #66 from soramitsu/FLW-1874-Upload-QR-code
Browse files Browse the repository at this point in the history
FLW-1874: fetch eth address from fearless qr
  • Loading branch information
DRadmir committed May 17, 2022
2 parents 8ffd885 + 2925d79 commit 728be73
Show file tree
Hide file tree
Showing 4 changed files with 31 additions and 18 deletions.
Original file line number Diff line number Diff line change
@@ -1,16 +1,18 @@
import Foundation
import IrohaCrypto

open class SubstrateQRDecoder: SubstrateQRDecodable {
open class AddressQRDecoder: AddressQRDecodable {
public let chainType: ChainType
public let separator: String
public let prefix: String

private lazy var addressFactory = SS58AddressFactory()

public init(chainType: ChainType,
prefix: String = SubstrateQR.prefix,
separator: String = SubstrateQR.fieldsSeparator) {
public init(
chainType: ChainType,
prefix: String = SubstrateQR.prefix,
separator: String = SubstrateQR.fieldsSeparator
) {
self.prefix = prefix
self.chainType = chainType
self.separator = separator
Expand All @@ -31,18 +33,29 @@ open class SubstrateQRDecoder: SubstrateQRDecodable {
}

let address = fields[1]
let accountId = try addressFactory.accountId(fromAddress: address, type: chainType)
let publicKey = try Data(hexString: fields[2])
let username = fields.count > 3 ? fields[3] : nil
var accountId: Data?

if address.hasPrefix("0x") {
return AddressQRInfo(
address: address,
rawPublicKey: publicKey,
username: username
)
} else {
accountId = try addressFactory.accountId(fromAddress: address, type: chainType)
}

guard publicKey.matchPublicKeyToAccountId(accountId) else {
guard let accountId = accountId, publicKey.matchPublicKeyToAccountId(accountId) else {
throw QRDecoderError.accountIdMismatch
}

let username = fields.count > 3 ? fields[3] : nil

return SubstrateQRInfo(prefix: prefix,
address: address,
rawPublicKey: publicKey,
username: username)
return AddressQRInfo(
prefix: prefix,
address: address,
rawPublicKey: publicKey,
username: username
)
}
}
Original file line number Diff line number Diff line change
@@ -1,14 +1,14 @@
import Foundation
import IrohaCrypto

open class SubstrateQREncoder: SubstrateQREncodable {
open class AddressQREncoder: AddressQREncodable {
let separator: String

public init(separator: String = SubstrateQR.fieldsSeparator) {
self.separator = separator
}

public func encode(info: SubstrateQRInfo) throws -> Data {
public func encode(info: AddressQRInfo) throws -> Data {
var fields: [String] = [
info.prefix,
info.address,
Expand Down
2 changes: 1 addition & 1 deletion FearlessUtils/Classes/QR/QRCommon.swift
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@ public protocol QRDecodable {
}

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

public enum QREncoderError: Error, Equatable {
Expand Down
6 changes: 3 additions & 3 deletions FearlessUtils/Classes/QR/SubstrateQRCommon.swift
Original file line number Diff line number Diff line change
@@ -1,9 +1,9 @@
import Foundation

public protocol SubstrateQREncodable: QREncodable {}
public protocol SubstrateQRDecodable: QRDecodable {}
public protocol AddressQREncodable: QREncodable {}
public protocol AddressQRDecodable: QRDecodable {}

public struct SubstrateQRInfo: QRInfo, Equatable {
public struct AddressQRInfo: QRInfo, Equatable {
public let prefix: String
public let address: String
public let rawPublicKey: Data
Expand Down

0 comments on commit 728be73

Please sign in to comment.