-
Notifications
You must be signed in to change notification settings - Fork 7
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat(backup): allow backup and import of prism wallet
This will enable backup of a wallet to another wallet given the seed is the same. It adds a protocol to enable credential exports. Fixes ATL-6610
- Loading branch information
1 parent
558164f
commit 8cd2ebc
Showing
23 changed files
with
927 additions
and
15 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
22 changes: 22 additions & 0 deletions
22
AtalaPrismSDK/Domain/Sources/Models/Credentials/ExportableCredential.swift
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,22 @@ | ||
import Foundation | ||
|
||
public protocol ExportableCredential { | ||
var exporting: Data { get } | ||
var restorationType: String { get } | ||
} | ||
|
||
public protocol CredentialImporter { | ||
func importCredential( | ||
credentialData: Data, | ||
restorationType: String, | ||
options: [CredentialOperationsOptions] | ||
) async throws -> Credential | ||
} | ||
|
||
public extension Credential { | ||
/// A Boolean value indicating whether the credential is exportable. | ||
var isExportable: Bool { self is ExportableCredential } | ||
|
||
/// Returns the exportable representation of the credential. | ||
var exportable: ExportableCredential? { self as? ExportableCredential } | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
8 changes: 8 additions & 0 deletions
8
...smSDK/Pollux/Sources/Models/AnonCreds/AnoncredsCredentialStack+ExportableCredential.swift
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,8 @@ | ||
import Domain | ||
import Foundation | ||
|
||
extension AnoncredsCredentialStack: ExportableCredential { | ||
var exporting: Data { (try? JSONEncoder().encode(credential)) ?? Data() } | ||
|
||
var restorationType: String { "anoncred" } | ||
} |
10 changes: 10 additions & 0 deletions
10
AtalaPrismSDK/Pollux/Sources/Models/JWT/JWTCredential+ExportableCredential.swift
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,10 @@ | ||
import Domain | ||
import Foundation | ||
|
||
extension JWTCredential: ExportableCredential { | ||
var exporting: Data { | ||
(try? jwtString.tryToData()) ?? Data() | ||
} | ||
|
||
var restorationType: String { "jwt" } | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
54 changes: 54 additions & 0 deletions
54
AtalaPrismSDK/Pollux/Sources/PolluxImpl+CredentialImporter.swift
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,54 @@ | ||
import Domain | ||
import Foundation | ||
|
||
extension PolluxImpl: CredentialImporter { | ||
public func importCredential(credentialData: Data, restorationType: String, options: [CredentialOperationsOptions]) async throws -> Credential { | ||
switch restorationType { | ||
case "anoncred": | ||
guard | ||
let credDefinitionDownloaderOption = options.first(where: { | ||
if case .credentialDefinitionDownloader = $0 { return true } | ||
return false | ||
}), | ||
case let CredentialOperationsOptions.credentialDefinitionDownloader(defDownloader) = credDefinitionDownloaderOption | ||
else { | ||
throw PolluxError.invalidPrismDID | ||
} | ||
guard | ||
let credSchemaDownloaderOption = options.first(where: { | ||
if case .schemaDownloader = $0 { return true } | ||
return false | ||
}), | ||
case let CredentialOperationsOptions.schemaDownloader(schemaDownloader) = credSchemaDownloaderOption | ||
else { | ||
throw PolluxError.invalidPrismDID | ||
} | ||
return try await importAnoncredCredential( | ||
credentialData: credentialData, | ||
credentialDefinitionDownloader: defDownloader, | ||
schemaDownloader: schemaDownloader | ||
) | ||
case "jwt": | ||
return try JWTCredential(data: credentialData) | ||
default: | ||
throw PolluxError.invalidCredentialError | ||
} | ||
} | ||
} | ||
|
||
private func importAnoncredCredential( | ||
credentialData: Data, | ||
credentialDefinitionDownloader: Downloader, | ||
schemaDownloader: Downloader | ||
) async throws -> Credential { | ||
let domainCred = try JSONDecoder().decode(AnonCredential.self, from: credentialData) | ||
let credentialDefinitionData = try await credentialDefinitionDownloader | ||
.downloadFromEndpoint(urlOrDID: domainCred.credentialDefinitionId) | ||
let schemaData = try await schemaDownloader | ||
.downloadFromEndpoint(urlOrDID: domainCred.schemaId) | ||
return AnoncredsCredentialStack( | ||
schema: try? JSONDecoder.didComm().decode(AnonCredentialSchema.self, from: schemaData), | ||
definition: try JSONDecoder.didComm().decode(AnonCredentialDefinition.self, from: credentialDefinitionData), | ||
credential: domainCred | ||
) | ||
} |
Oops, something went wrong.