Skip to content

Commit

Permalink
make address format optional
Browse files Browse the repository at this point in the history
  • Loading branch information
ERussel committed Mar 28, 2022
1 parent f51c4b3 commit 16ebc9b
Showing 1 changed file with 25 additions and 20 deletions.
45 changes: 25 additions & 20 deletions SubstrateSdk/Classes/QR/SubstrateQRDecoder.swift
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand All @@ -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
)
}
}

0 comments on commit 16ebc9b

Please sign in to comment.