Skip to content

Commit

Permalink
[ABW-1762] FactorSourceID -> enum (#583)
Browse files Browse the repository at this point in the history
  • Loading branch information
CyonAlexRDX committed Jun 14, 2023
1 parent 13d51c9 commit 293d79a
Show file tree
Hide file tree
Showing 54 changed files with 705 additions and 311 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -132,7 +132,7 @@ extension DeviceFactorSourceClient {
let factorSourceID = deviceFactorSource.id

guard
let loadedMnemonicWithPassphrase = try await secureStorageClient.loadMnemonicByFactorSourceID(factorSourceID, purpose.loadMnemonicPurpose)
let loadedMnemonicWithPassphrase = try await secureStorageClient.loadMnemonicByFactorSourceID(factorSourceID.embed(), purpose.loadMnemonicPurpose)
else {
throw FailedToDeviceFactorSourceForSigning()
}
Expand Down Expand Up @@ -174,7 +174,7 @@ extension DeviceFactorSourceClient {
let entitySignature = SignatureOfEntity(
signerEntity: entity,
derivationPath: derivationPath,
factorSourceID: factorSourceID,
factorSourceID: factorSourceID.embed(),
signatureWithPublicKey: signatureWithPublicKey
)

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,7 @@ extension DeviceFactorSourceClient: DependencyKey {

guard
let mnemonicWithPassphrase = try await secureStorageClient
.loadMnemonicByFactorSourceID(factorSourceID, request.loadMnemonicPurpose)
.loadMnemonicByFactorSourceID(factorSourceID.embed(), request.loadMnemonicPurpose)
else {
loggerGlobal.critical("Failed to find factor source with ID: '\(factorSourceID)'")
throw FailedToFindFactorSource()
Expand Down Expand Up @@ -53,7 +53,7 @@ extension DeviceFactorSourceClient: DependencyKey {

guard let deviceFactorSource,
let mnemonicWithPassphrase = try await secureStorageClient.loadMnemonicByFactorSourceID(
deviceFactorSource.id,
deviceFactorSource.id.embed(),
.checkingAccounts
)
else {
Expand Down
4 changes: 2 additions & 2 deletions Sources/Clients/EngineToolkitClient/LockFeeCallMethod.swift
Original file line number Diff line number Diff line change
Expand Up @@ -218,10 +218,10 @@ extension EngineToolkitClient {
nftDescription: String = "Artsy cool unique NFT"
) throws -> TransactionManifest {
let faucetAddress = try faucetAddress(for: networkID)
let instructions: [any InstructionProtocol] = [
let instructions: [any InstructionProtocol] = try [
lockFeeCallMethod(address: faucetAddress),

try CreateNonFungibleResourceWithInitialSupply(
CreateNonFungibleResourceWithInitialSupply(
idType: .init(.string("NonFungibleIdType::Integer")),
schema: [
.tuple([
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -39,7 +39,7 @@ extension FactorSourcesClient {
public typealias GetFactorSources = @Sendable () async throws -> FactorSources
public typealias FactorSourcesAsyncSequence = @Sendable () async -> AnyAsyncSequence<FactorSources>
public typealias AddPrivateHDFactorSource = @Sendable (AddPrivateHDFactorSourceRequest) async throws -> FactorSourceID
public typealias CheckIfHasOlympiaFactorSourceForAccounts = @Sendable (NonEmpty<OrderedSet<OlympiaAccountToMigrate>>) async -> FactorSourceID?
public typealias CheckIfHasOlympiaFactorSourceForAccounts = @Sendable (NonEmpty<OrderedSet<OlympiaAccountToMigrate>>) async -> FactorSourceID.FromHash?
public typealias SaveFactorSource = @Sendable (FactorSource) async throws -> Void
public typealias GetSigningFactors = @Sendable (GetSigningFactorsRequest) async throws -> SigningFactors
public typealias UpdateLastUsed = @Sendable (UpdateFactorSourceLastUsedRequest) async throws -> Void
Expand Down Expand Up @@ -123,11 +123,11 @@ extension FactorSourcesClient {

// MARK: - UpdateFactorSourceLastUsedRequest
public struct UpdateFactorSourceLastUsedRequest: Sendable, Hashable {
public let factorSourceIDs: [FactorSource.ID]
public let factorSourceIDs: [FactorSourceID]
public let lastUsedOn: Date
public let usagePurpose: SigningPurpose
public init(
factorSourceIDs: [FactorSource.ID],
factorSourceIDs: [FactorSourceID],
usagePurpose: SigningPurpose,
lastUsedOn: Date = .init()
) {
Expand All @@ -150,7 +150,7 @@ public enum MnemonicBasedFactorSourceKind: Sendable, Hashable {

// MARK: - SigningFactor
public struct SigningFactor: Sendable, Hashable, Identifiable {
public typealias ID = FactorSource.ID
public typealias ID = FactorSourceID
public var id: ID { factorSource.id }
public let factorSource: FactorSource
public typealias Signers = NonEmpty<IdentifiedArrayOf<Signer>>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -58,7 +58,7 @@ extension FactorSourcesClient: DependencyKey {

return factorSourceID
},
checkIfHasOlympiaFactorSourceForAccounts: { softwareAccounts -> FactorSourceID? in
checkIfHasOlympiaFactorSourceForAccounts: { softwareAccounts -> FactorSourceID.FromHash? in
guard softwareAccounts.allSatisfy({ $0.accountType == .software }) else {
assertionFailure("Unexpectedly received hardware account, unable to verify.")
return nil
Expand All @@ -79,7 +79,7 @@ extension FactorSourcesClient: DependencyKey {
}
// YES Managed to validate all software accounts against existing factor source
loggerGlobal.debug("Existing factor source found for selected Olympia software accounts.")
return factorSourceID
return factorSourceID.extract(FactorSourceID.FromHash.self)
}

return nil // Did not find any Olympia `.device` factor sources
Expand Down Expand Up @@ -138,7 +138,7 @@ internal func signingFactors(
}()

let id = factorInstance.factorSourceID
guard let factorSource = allFactorSources[id: id] else {
guard let factorSource = allFactorSources[id: id.embed()] else {
assertionFailure("Bad! factor source not found")
throw FactorSourceNotFound()
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@ extension ImportLegacyWalletClient: DependencyKey {

@Sendable func migrate(
accounts: NonEmpty<Set<OlympiaAccountToMigrate>>,
factorSouceID: FactorSourceID
factorSouceID: FactorSourceID.FromHash
) async throws -> (accounts: NonEmpty<OrderedSet<MigratedAccount>>, networkID: NetworkID) {
let sortedOlympia = accounts.sorted(by: \.addressIndex)
let networkID = Radix.Gateway.default.network.id // we import to the default network, not the current.
Expand All @@ -23,7 +23,7 @@ extension ImportLegacyWalletClient: DependencyKey {
for olympiaAccount in sortedOlympia {
let publicKey = SLIP10.PublicKey.ecdsaSecp256k1(olympiaAccount.publicKey)
let factorInstance = HierarchicalDeterministicFactorInstance(
factorSourceID: factorSouceID,
id: factorSouceID,
publicKey: publicKey,
derivationPath: olympiaAccount.path.wrapAsDerivationPath()
)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -11,12 +11,12 @@ public struct ScannedParsedOlympiaWalletToMigrate: Sendable, Hashable {
// MARK: - MigrateOlympiaSoftwareAccountsToBabylonRequest
public struct MigrateOlympiaSoftwareAccountsToBabylonRequest: Sendable, Hashable {
public let olympiaAccounts: Set<OlympiaAccountToMigrate>
public let olympiaFactorSouceID: FactorSourceID
public let olympiaFactorSouceID: FactorSourceID.FromHash
public let olympiaFactorSource: PrivateHDFactorSource?

public init(
olympiaAccounts: Set<OlympiaAccountToMigrate>,
olympiaFactorSouceID: FactorSourceID,
olympiaFactorSouceID: FactorSourceID.FromHash,
olympiaFactorSource: PrivateHDFactorSource?
) {
self.olympiaAccounts = olympiaAccounts
Expand All @@ -28,11 +28,11 @@ public struct MigrateOlympiaSoftwareAccountsToBabylonRequest: Sendable, Hashable
// MARK: - MigrateOlympiaHardwareAccountsToBabylonRequest
public struct MigrateOlympiaHardwareAccountsToBabylonRequest: Sendable, Hashable {
public let olympiaAccounts: NonEmpty<Set<OlympiaAccountToMigrate>>
public let ledgerFactorSourceID: FactorSourceID
public let ledgerFactorSourceID: FactorSourceID.FromHash

public init(
olympiaAccounts: NonEmpty<Set<OlympiaAccountToMigrate>>,
ledgerFactorSourceID: FactorSourceID
ledgerFactorSourceID: FactorSourceID.FromHash
) {
self.olympiaAccounts = olympiaAccounts
self.ledgerFactorSourceID = ledgerFactorSourceID
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -86,7 +86,7 @@ extension LedgerHardwareWalletClient: DependencyKey {
let entitySignature = SignatureOfEntity(
signerEntity: requiredSigner.entity,
derivationPath: signature.derivationPath,
factorSourceID: requiredSigningFactor.factorSourceID,
factorSourceID: requiredSigningFactor.factorSourceID.embed(),
signatureWithPublicKey: signature.signature
)

Expand Down Expand Up @@ -175,9 +175,10 @@ extension LedgerHardwareWalletFactorSource {
guard let model = P2P.LedgerHardwareWallet.Model(rawValue: hint.model.rawValue) else {
throw UnrecognizedLedgerModel(model: hint.model.rawValue)
}

return P2P.LedgerHardwareWallet.LedgerDevice(
name: NonEmptyString(maybeString: hint.name),
id: Data(id.hexCodable.data.dropFirst()).hex, // Connector Extension only cares about the hash not the factor source ID prefix.
id: id.body.data.data.hex,
model: model
)
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -27,9 +27,9 @@ extension SecureStorageClient {
public typealias LoadProfileSnapshotData = @Sendable (ProfileSnapshot.Header.ID) async throws -> Data?

public typealias SaveMnemonicForFactorSource = @Sendable (PrivateHDFactorSource) async throws -> Void
public typealias LoadMnemonicByFactorSourceID = @Sendable (FactorSource.ID, LoadMnemonicPurpose) async throws -> MnemonicWithPassphrase?
public typealias LoadMnemonicByFactorSourceID = @Sendable (FactorSourceID, LoadMnemonicPurpose) async throws -> MnemonicWithPassphrase?

public typealias DeleteMnemonicByFactorSourceID = @Sendable (FactorSource.ID) async throws -> Void
public typealias DeleteMnemonicByFactorSourceID = @Sendable (FactorSourceID) async throws -> Void
public typealias DeleteProfileAndMnemonicsByFactorSourceIDs = @Sendable (ProfileSnapshot.Header.ID, _ keepInICloudIfPresent: Bool) async throws -> Void

public typealias LoadProfileHeaderList = @Sendable () async throws -> ProfileSnapshot.HeaderList?
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -185,7 +185,7 @@ extension SecureStorageClient: DependencyKey {
let mnemonicWithPassphrase = privateFactorSource.mnemonicWithPassphrase
let data = try jsonEncoder().encode(mnemonicWithPassphrase)
let mostSecureAccesibilityAndAuthenticationPolicy = try await queryMostSecureAccesibilityAndAuthenticationPolicy()
let key = key(factorSourceID: factorSource.id)
let key = key(factorSourceID: factorSource.id.embed())

try await keychainClient.setDataWithAuthenticationPolicyIfAble(
data: data,
Expand Down Expand Up @@ -306,6 +306,6 @@ extension ProfileSnapshot.Header.ID {
}
}

private func key(factorSourceID: FactorSource.ID) -> KeychainClient.Key {
.init(rawValue: .init(rawValue: factorSourceID.hexCodable.hex())!)
private func key(factorSourceID: FactorSourceID) -> KeychainClient.Key {
.init(rawValue: .init(rawValue: factorSourceID.description)!)
}
Original file line number Diff line number Diff line change
Expand Up @@ -37,7 +37,7 @@ extension P2P {
public struct LedgerDevice: Sendable, Hashable, Codable {
public let name: NonEmptyString?

/// `FactorSource.ID`
/// `FactorSourceID`
public let id: String
public let model: Model

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@ extension Profile.Network.Account {
),
securityState: .unsecured(.init(
transactionSigning: .init(
factorSourceID: .init(hex: "de09bfa80bcc9b75d6ad82d59730f7b179cbc668ba6ad4008721d5e6a179ff55f1"),
factorSourceID: .device(hash: "09bfa80bcc9b75d6ad82d59730f7b179cbc668ba6ad4008721d5e6a179ff55f1"),
publicKey: .eddsaEd25519(.init(
compressedRepresentation: Data(
hex: "7bf9f97c0cac8c6c112d716069ccc169283b9838fa2f951c625b3d4ca0a8f05b")
Expand All @@ -29,7 +29,7 @@ extension Profile.Network.Account {
),
securityState: .unsecured(.init(
transactionSigning: .init(
factorSourceID: .init(hex: "de09bfa80bcc9b75d6ad82d59730f7b179cbc668ba6ad4008721d5e6a179ff55f1"),
factorSourceID: .device(hash: "09bfa80bcc9b75d6ad82d59730f7b179cbc668ba6ad4008721d5e6a179ff55f1"),
publicKey: .eddsaEd25519(.init(
compressedRepresentation: Data(
hex: "b862c4ef84a4a97c37760636f6b94d1fba7b4881ac15a073f6c57e2996bbeca8")
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@ extension Profile.Network.Persona {
),
securityState: .unsecured(.init(
transactionSigning: .init(
factorSourceID: .init(hex: "de09bfa80bcc9b75d6ad82d59730f7b179cbc668ba6ad4008721d5e6a179ff55f1"),
factorSourceID: .device(hash: "09bfa80bcc9b75d6ad82d59730f7b179cbc668ba6ad4008721d5e6a179ff55f1"),
publicKey: .eddsaEd25519(.init(
compressedRepresentation: Data(
hex: "7bf9f97c0cac8c6c112d716069ccc169283b9838fa2f951c625b3d4ca0a8f05b")
Expand All @@ -35,7 +35,7 @@ extension Profile.Network.Persona {
),
securityState: .unsecured(.init(
transactionSigning: .init(
factorSourceID: .init(hex: "de09bfa80bcc9b75d6ad82d59730f7b179cbc668ba6ad4008721d5e6a179ff55f1"),
factorSourceID: .device(hash: "09bfa80bcc9b75d6ad82d59730f7b179cbc668ba6ad4008721d5e6a179ff55f1"),
publicKey: .eddsaEd25519(.init(
compressedRepresentation: Data(
hex: "b862c4ef84a4a97c37760636f6b94d1fba7b4881ac15a073f6c57e2996bbeca8")
Expand Down
Loading

0 comments on commit 293d79a

Please sign in to comment.