diff --git a/SubstrateSdk/Classes/QR/SubstrateQRDecoder.swift b/SubstrateSdk/Classes/QR/SubstrateQRDecoder.swift index 0b2fc1b..a19e075 100644 --- a/SubstrateSdk/Classes/QR/SubstrateQRDecoder.swift +++ b/SubstrateSdk/Classes/QR/SubstrateQRDecoder.swift @@ -2,15 +2,17 @@ import Foundation import IrohaCrypto open class SubstrateQRDecoder: SubstrateQRDecodable { - public let addressFormat: QRAddressFormat + public let addressFormat: QRAddressFormat? public let separator: String public let prefix: String private lazy var addressFactory = SS58AddressFactory() - public init(addressFormat: QRAddressFormat, - prefix: String = SubstrateQR.prefix, - separator: String = SubstrateQR.fieldsSeparator) { + public init( + addressFormat: QRAddressFormat? = nil, + prefix: String = SubstrateQR.prefix, + separator: String = SubstrateQR.fieldsSeparator + ) { self.prefix = prefix self.addressFormat = addressFormat self.separator = separator @@ -31,31 +33,34 @@ open class SubstrateQRDecoder: SubstrateQRDecodable { } let address = fields[1] - let accountId: Data let publicKey = try Data(hexString: fields[2]) - switch addressFormat { - case .substrate(let type): - accountId = try addressFactory.accountId(fromAddress: address, type: type) + if let addressFormat = addressFormat { + switch addressFormat { + case .substrate(let type): + let accountId = try addressFactory.accountId(fromAddress: address, type: type) - guard publicKey.matchPublicKeyToAccountId(accountId) else { - throw SubstrateQRDecoderError.accountIdMismatch - } - case .ethereum: - accountId = try Data(hexString: address) + guard publicKey.matchPublicKeyToAccountId(accountId) else { + throw SubstrateQRDecoderError.accountIdMismatch + } + case .ethereum: + let accountId = try Data(hexString: address) - let expectedAccountId = try publicKey.ethereumAddressFromPublicKey() + let expectedAccountId = try publicKey.ethereumAddressFromPublicKey() - guard accountId == expectedAccountId else { - throw SubstrateQRDecoderError.accountIdMismatch + guard accountId == expectedAccountId else { + throw SubstrateQRDecoderError.accountIdMismatch + } } } let username = fields.count > 3 ? fields[3] : nil - return SubstrateQRInfo(prefix: prefix, - address: address, - rawPublicKey: publicKey, - username: username) + return SubstrateQRInfo( + prefix: prefix, + address: address, + rawPublicKey: publicKey, + username: username + ) } }