|
| 1 | +import AnoncredsSwift |
| 2 | +import Domain |
| 3 | +import Foundation |
| 4 | + |
| 5 | +struct MockAnoncredsIssuer { |
| 6 | + |
| 7 | + let issuer = "mock:issuer_id/path&q=bar" |
| 8 | + let linkSecret: LinkSecret |
| 9 | + let schema: Schema |
| 10 | + let credDef: CredentialDefinition |
| 11 | + let credDefPriv: CredentialDefinitionPrivate |
| 12 | + let credDefCorrProof: CredentialKeyCorrectnessProof |
| 13 | + |
| 14 | + init() { |
| 15 | + self.linkSecret = try! LinkSecret.newFromValue(valueString: "65965334953670062552662719679603258895632947953618378932199361160021795698890") |
| 16 | + |
| 17 | + self.schema = try! Issuer().createSchema( |
| 18 | + schemaName: "mock:uri2", |
| 19 | + schemaVersion: "0.1.0", |
| 20 | + issuerId: issuer, |
| 21 | + attrNames: ["name", "sex", "age"] |
| 22 | + ) |
| 23 | + |
| 24 | + let credDef = try! Issuer().createCredentialDefinition( |
| 25 | + schemaId: schema.name, |
| 26 | + schema: schema, |
| 27 | + issuerId: issuer, |
| 28 | + tag: "tag", |
| 29 | + signatureType: .cl, |
| 30 | + config: .init(supportRevocation: false) |
| 31 | + ) |
| 32 | + |
| 33 | + self.credDef = credDef.credentialDefinition |
| 34 | + self.credDefPriv = credDef.credentialDefinitionPrivate |
| 35 | + self.credDefCorrProof = credDef.credentialKeyCorrectnessProof |
| 36 | + } |
| 37 | + |
| 38 | + func createCredDefinition() throws { |
| 39 | + let credDef = try! Issuer().createCredentialDefinition( |
| 40 | + schemaId: "http://localhost:8000/schemas/test", |
| 41 | + schema: schema, |
| 42 | + issuerId: issuer, |
| 43 | + tag: "test", |
| 44 | + signatureType: .cl, |
| 45 | + config: .init(supportRevocation: false) |
| 46 | + ) |
| 47 | + } |
| 48 | + |
| 49 | + func createOffer() throws -> CredentialOffer { |
| 50 | + try Issuer().createCredentialOffer( |
| 51 | + schemaId: "mock:uri2", |
| 52 | + credDefId: "mock:uri3", |
| 53 | + correctnessProof: credDefCorrProof |
| 54 | + ) |
| 55 | + } |
| 56 | + |
| 57 | + func createOfferMessage() throws -> Message { |
| 58 | + let offer = try createOffer().getJson() |
| 59 | + |
| 60 | + return Message( |
| 61 | + piuri: "https://didcomm.org/issue-credential/3.0/offer-credential", |
| 62 | + body: Data(), |
| 63 | + attachments: [ |
| 64 | + .init( |
| 65 | + id: "test1", |
| 66 | + mediaType: nil, |
| 67 | + data: AttachmentBase64(base64: offer.data(using: .utf8)!.base64EncodedString()), |
| 68 | + filename: nil, |
| 69 | + format : "anoncreds/[email protected]", |
| 70 | + lastmodTime: nil, |
| 71 | + byteCount: nil, |
| 72 | + description: nil |
| 73 | + ) |
| 74 | + ], |
| 75 | + thid: "1" |
| 76 | + ) |
| 77 | + } |
| 78 | + |
| 79 | + func issueCredential( |
| 80 | + offer: CredentialOffer, |
| 81 | + request: CredentialRequest, |
| 82 | + name: String = "Test", |
| 83 | + sex: String = "M", |
| 84 | + age: String = "31" |
| 85 | + ) throws -> Message { |
| 86 | + let issued = try Issuer().createCredential( |
| 87 | + credDef: credDef, |
| 88 | + credDefPrivate: credDefPriv, |
| 89 | + credOffer: offer, |
| 90 | + credRequest: request, |
| 91 | + credValues: [ |
| 92 | + .init(raw: "name", encoded: name), |
| 93 | + .init(raw: "sex", encoded: sex), |
| 94 | + .init(raw: "age", encoded: age) |
| 95 | + ], |
| 96 | + revRegId: nil, |
| 97 | + revStatusList: nil, |
| 98 | + revocationConfig: nil |
| 99 | + ).getJson() |
| 100 | + |
| 101 | + return Message( |
| 102 | + piuri: "https://didcomm.org/issue-credential/3.0/issue-credential", |
| 103 | + body: Data(), |
| 104 | + attachments: [ |
| 105 | + .init( |
| 106 | + id: "test1", |
| 107 | + mediaType: nil, |
| 108 | + data: AttachmentBase64(base64: issued.data(using: .utf8)!.base64EncodedString()), |
| 109 | + filename: nil, |
| 110 | + format : "anoncreds/[email protected]", |
| 111 | + lastmodTime: nil, |
| 112 | + byteCount: nil, |
| 113 | + description: nil |
| 114 | + ) |
| 115 | + ], |
| 116 | + thid: "1" |
| 117 | + ) |
| 118 | + } |
| 119 | + |
| 120 | + func createPresentationRequest() throws -> (message: Message, requestStr: String) { |
| 121 | + let presentation = """ |
| 122 | +{"nonce":"1103253414365527824079144","name":"proof_req_1","version":"0.1","requested_attributes":{"sex":{"name":"sex", "restrictions":{"attr::sex::value":"M","cred_def_id":"mock:uri3"}}},"requested_predicates":{"age":{"name":"age", "p_type":">=", "p_value":18}}} |
| 123 | +""" |
| 124 | + return (Message( |
| 125 | + piuri: "", |
| 126 | + body: Data(), |
| 127 | + attachments: [ |
| 128 | + .init( |
| 129 | + data: AttachmentBase64(base64: try presentation.tryData(using: .utf8).base64EncodedString()) |
| 130 | + ) |
| 131 | + ] |
| 132 | + ), presentation) |
| 133 | + } |
| 134 | + |
| 135 | + func getSchemaJson() -> String { |
| 136 | +""" |
| 137 | +{"name":"\(schema.name)","issuerId":"\(schema.issuerId)","version":"\(schema.version)","attrNames":["name", "sex", "age"]} |
| 138 | +""" |
| 139 | + } |
| 140 | + |
| 141 | + func verifyPresentation(presentation: String, request: String) throws -> Bool { |
| 142 | + let presentation = try Presentation(jsonString: presentation) |
| 143 | + let request = try PresentationRequest(jsonString: request) |
| 144 | + let credDef = self.credDef |
| 145 | + let schema = self.schema |
| 146 | + return try Verifier().verifyPresentation( |
| 147 | + presentation: presentation, |
| 148 | + presentationRequest: request, |
| 149 | + schemas: ["mock:uri2": schema], |
| 150 | + credentialDefinitions: ["mock:uri3": credDef] |
| 151 | + ) |
| 152 | + } |
| 153 | +} |
| 154 | + |
| 155 | +struct MockDownloader: Downloader { |
| 156 | + let returnData: Data |
| 157 | + func downloadFromEndpoint(urlOrDID: String) async throws -> Data { |
| 158 | + return returnData |
| 159 | + } |
| 160 | +} |
| 161 | + |
| 162 | +struct MockAnoncredsProver { |
| 163 | + let did = "did:test:adsadiada" |
| 164 | + let linkSecret: Key |
| 165 | + let credDef: CredentialDefinition |
| 166 | + |
| 167 | + init(linkSecret: Key, credDef: CredentialDefinition) { |
| 168 | + self.linkSecret = linkSecret |
| 169 | + self.credDef = credDef |
| 170 | + } |
| 171 | + |
| 172 | + func createRequest(offer: CredentialOffer) throws -> (CredentialRequest, CredentialRequestMetadata) { |
| 173 | + let result = try Prover().createCredentialRequest( |
| 174 | + entropy: "did", |
| 175 | + proverDid: nil, |
| 176 | + credDef: credDef, |
| 177 | + linkSecret: try AnoncredsSwift.LinkSecret.newFromValue(valueString: linkSecret.raw.tryToString()), |
| 178 | + linkSecretId: "test", |
| 179 | + credentialOffer: offer |
| 180 | + ) |
| 181 | + return (result.request, result.metadata) |
| 182 | + } |
| 183 | +} |
0 commit comments