Skip to content

fix: presentation now selects the proper key #207

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 1 commit into from
Apr 14, 2025
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -54,24 +54,19 @@ public extension DIDCommAgent {

let subjectDID = try DID(string: subjectDIDString)

let privateKeys = try await pluto.getDIDPrivateKeys(did: subjectDID).first().await()

guard
let storedPrivateKey = privateKeys?.first
let storedPrivateKeys = try await pluto.getDIDPrivateKeys(did: subjectDID).first().await()
else { throw EdgeAgentError.cannotFindDIDKeyPairIndex }

let privateKey = try await apollo.restorePrivateKey(storedPrivateKey)

guard
let exporting = privateKey.exporting
else { throw EdgeAgentError.cannotFindDIDKeyPairIndex }
let privateKeys = try await storedPrivateKeys.asyncMap { try await apollo.restorePrivateKey($0) }
let exporting = privateKeys.compactMap(\.exporting)

format = requestType == "prism/jwt" ? "prism/jwt" : "dif/presentation-exchange/[email protected]"

presentationString = try proofableCredential.presentation(
request: request.makeMessage(),
options: options + [
.exportableKey(exporting),
.exportableKeys(exporting),
.subjectDID(subjectDID)
]
)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -110,6 +110,28 @@ Could not find key in storage please use Castor instead and provide the private
])
usingKeys.append((.master, usingPrivateKey))
}

if usingKeys.count == 1 {
let lastKeyPairIndex = try await pluto
.getPrismLastKeyPairIndex()
.first()
.await()

// If the user provided a key path index use it, if not use the last + 1
let index = keyPathIndex ?? (lastKeyPairIndex + 1)
// Create the key pair
let usingPrivateKey = try apollo.createPrivateKey(parameters: [
KeyProperties.type.rawValue: "EC",
KeyProperties.seed.rawValue: seed.value.base64Encoded(),
KeyProperties.curve.rawValue: KnownKeyCurves.secp256k1.rawValue,
KeyProperties.derivationPath.rawValue: EdgeAgentDerivationPath(
keyPurpose: .authentication,
keyIndex: index
).derivationPath.keyPathString()
])
usingKeys.append((.authentication, usingPrivateKey))
}

let groupedKeys = Dictionary(grouping: usingKeys, by: { $0.0 })
let finalKeys = groupedKeys.flatMap { (key, value) in
value.enumerated().map {
Expand Down
17 changes: 7 additions & 10 deletions EdgeAgentSDK/EdgeAgent/Sources/EdgeAgent+Proof.swift
Original file line number Diff line number Diff line change
Expand Up @@ -53,24 +53,21 @@ public extension EdgeAgent {

let subjectDID = try DID(string: subjectDIDString)

let privateKeys = try await pluto.getDIDPrivateKeys(did: subjectDID).first().await()

guard
let storedPrivateKey = privateKeys?.first
else { throw EdgeAgentError.cannotFindDIDKeyPairIndex }

let privateKey = try await apollo.restorePrivateKey(storedPrivateKey)
let storedPrivateKeys = try await pluto.getDIDPrivateKeys(did: subjectDID).first().await()
else {
throw EdgeAgentError.cannotFindDIDKeyPairIndex
}

guard
let exporting = privateKey.exporting
else { throw EdgeAgentError.cannotFindDIDKeyPairIndex }
let privateKeys = try await storedPrivateKeys.asyncMap { try await apollo.restorePrivateKey($0) }
let exporting = privateKeys.compactMap(\.exporting)

format = requestType == "prism/jwt" ? "prism/jwt" : "dif/presentation-exchange/[email protected]"

presentationString = try proofableCredential.presentation(
request: request.makeMessage(),
options: [
.exportableKey(exporting),
.exportableKeys(exporting),
.subjectDID(subjectDID),
.disclosingClaims(claims: credential.claims.map(\.key))
]
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -38,7 +38,7 @@ final class PresentationExchangeFlowTests: XCTestCase {
let credential = try JWTCredential(data: jwt.tryToData())

logger.info("Creating presentation request")
let message = try await edgeAgent.initiatePresentationRequest(
let message = try edgeAgent.initiatePresentationRequest(
type: .jwt,
fromDID: DID(method: "test", methodId: "alice"),
toDID: DID(method: "test", methodId: "bob"),
Expand Down Expand Up @@ -108,7 +108,7 @@ final class PresentationExchangeFlowTests: XCTestCase {
let credential = try SDJWTCredential(sdjwtString: sdjwt)

logger.info("Creating presentation request")
let message = try await edgeAgent.initiatePresentationRequest(
let message = try edgeAgent.initiatePresentationRequest(
type: .jwt,
fromDID: DID(method: "test", methodId: "alice"),
toDID: DID(method: "test", methodId: "bob"),
Expand Down
15 changes: 10 additions & 5 deletions EdgeAgentSDK/Pollux/Sources/Models/JWT/JWTPresentation.swift
Original file line number Diff line number Diff line change
Expand Up @@ -53,11 +53,16 @@ struct JWTPresentation {
}

guard
let exportableKeyOption = options.first(where: {
if case .exportableKey = $0 { return true }
let exportableKeysOption = options.first(where: {
if case .exportableKeys = $0 { return true }
return false
}),
case let CredentialOperationsOptions.exportableKey(exportableKey) = exportableKeyOption
case let CredentialOperationsOptions.exportableKeys(exportableKeys) = exportableKeysOption,
let exportableFirstKey = exportableKeys
.filter({
$0.jwk.crv?.lowercased() == "secp256k1"
&& !($0.jwk.kid?.contains("#master") ?? true) // TODO: This is a hardcoded fix, since prism DID doesnt not recognize master key
}).first
else {
throw PolluxError.requiresExportableKeyForOperation(operation: "Create Presentation JWT Credential")
}
Expand All @@ -68,7 +73,7 @@ struct JWTPresentation {
credential: credential,
request: requestData,
did: did,
exportableKey: exportableKey
exportableKey: exportableFirstKey
)
default:
let payload = try vcPresentation(
Expand All @@ -79,7 +84,7 @@ struct JWTPresentation {

return try vcPresentationJWTString(
payload: payload,
exportableKey: exportableKey
exportableKey: exportableFirstKey
)
}
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -12,11 +12,16 @@ struct SDJWTPresentation {
options: [CredentialOperationsOptions]
) throws -> String{
guard
let exportableKeyOption = options.first(where: {
if case .exportableKey = $0 { return true }
let exportableKeysOption = options.first(where: {
if case .exportableKeys = $0 { return true }
return false
}),
case let CredentialOperationsOptions.exportableKey(exportableKey) = exportableKeyOption
case let CredentialOperationsOptions.exportableKeys(exportableKeys) = exportableKeysOption,
let exportableFirstKey = exportableKeys
.filter({
$0.jwk.crv?.lowercased() == "secp256k1"
&& !($0.jwk.kid?.contains("#master") ?? true) // TODO: This is a hardcoded fix, since prism DID doesnt not recognize master key
}).first
else {
throw PolluxError.requiresExportableKeyForOperation(operation: "Create Presentation for SD-JWT Credential")
}
Expand All @@ -41,14 +46,14 @@ struct SDJWTPresentation {
credential: credential,
request: requestData,
disclosingClaims: disclosingClaims,
key: exportableKey
key: exportableFirstKey
)
default:
return try vcPresentation(
credential: credential,
request: requestData,
disclosingClaims: disclosingClaims,
key: exportableKey
key: exportableFirstKey
)
}
}
Expand Down