|
| 1 | +import { |
| 2 | + Credential, |
| 3 | + FailFast, |
| 4 | + IotaDID, |
| 5 | + IotaDocument, |
| 6 | + IotaIdentityClient, |
| 7 | + JptCredentialValidationOptions, |
| 8 | + JptCredentialValidator, |
| 9 | + JptCredentialValidatorUtils, |
| 10 | + JptPresentationValidationOptions, |
| 11 | + JptPresentationValidator, |
| 12 | + JptPresentationValidatorUtils, |
| 13 | + JwkMemStore, |
| 14 | + JwpCredentialOptions, |
| 15 | + JwpPresentationOptions, |
| 16 | + KeyIdMemStore, |
| 17 | + MethodScope, |
| 18 | + ProofAlgorithm, |
| 19 | + SelectiveDisclosurePresentation, |
| 20 | + Storage, |
| 21 | +} from "@iota/identity-wasm/node"; |
| 22 | +import { |
| 23 | + type Address, |
| 24 | + AliasOutput, |
| 25 | + Client, |
| 26 | + MnemonicSecretManager, |
| 27 | + SecretManager, |
| 28 | + SecretManagerType, |
| 29 | + Utils, |
| 30 | +} from "@iota/sdk-wasm/node"; |
| 31 | +import { API_ENDPOINT, ensureAddressHasFunds } from "../util"; |
| 32 | + |
| 33 | +/** Creates a DID Document and publishes it in a new Alias Output. |
| 34 | +
|
| 35 | +Its functionality is equivalent to the "create DID" example |
| 36 | +and exists for convenient calling from the other examples. */ |
| 37 | +export async function createDid(client: Client, secretManager: SecretManagerType, storage: Storage): Promise<{ |
| 38 | + address: Address; |
| 39 | + document: IotaDocument; |
| 40 | + fragment: string; |
| 41 | +}> { |
| 42 | + const didClient = new IotaIdentityClient(client); |
| 43 | + const networkHrp: string = await didClient.getNetworkHrp(); |
| 44 | + |
| 45 | + const secretManagerInstance = new SecretManager(secretManager); |
| 46 | + const walletAddressBech32 = (await secretManagerInstance.generateEd25519Addresses({ |
| 47 | + accountIndex: 0, |
| 48 | + range: { |
| 49 | + start: 0, |
| 50 | + end: 1, |
| 51 | + }, |
| 52 | + bech32Hrp: networkHrp, |
| 53 | + }))[0]; |
| 54 | + |
| 55 | + console.log("Wallet address Bech32:", walletAddressBech32); |
| 56 | + |
| 57 | + await ensureAddressHasFunds(client, walletAddressBech32); |
| 58 | + |
| 59 | + const address: Address = Utils.parseBech32Address(walletAddressBech32); |
| 60 | + |
| 61 | + // Create a new DID document with a placeholder DID. |
| 62 | + // The DID will be derived from the Alias Id of the Alias Output after publishing. |
| 63 | + const document = new IotaDocument(networkHrp); |
| 64 | + |
| 65 | + const fragment = await document.generateMethodJwp( |
| 66 | + storage, |
| 67 | + ProofAlgorithm.BLS12381_SHA256, |
| 68 | + undefined, |
| 69 | + MethodScope.VerificationMethod(), |
| 70 | + ); |
| 71 | + // Construct an Alias Output containing the DID document, with the wallet address |
| 72 | + // set as both the state controller and governor. |
| 73 | + const aliasOutput: AliasOutput = await didClient.newDidOutput(address, document); |
| 74 | + |
| 75 | + // Publish the Alias Output and get the published DID document. |
| 76 | + const published = await didClient.publishDidOutput(secretManager, aliasOutput); |
| 77 | + |
| 78 | + return { address, document: published, fragment }; |
| 79 | +} |
| 80 | +export async function zkp() { |
| 81 | + // =========================================================================== |
| 82 | + // Step 1: Create identity for the issuer. |
| 83 | + // =========================================================================== |
| 84 | + |
| 85 | + // Create a new client to interact with the IOTA ledger. |
| 86 | + const client = new Client({ |
| 87 | + primaryNode: API_ENDPOINT, |
| 88 | + localPow: true, |
| 89 | + }); |
| 90 | + |
| 91 | + // Creates a new wallet and identity (see "0_create_did" example). |
| 92 | + const issuerSecretManager: MnemonicSecretManager = { |
| 93 | + mnemonic: Utils.generateMnemonic(), |
| 94 | + }; |
| 95 | + const issuerStorage: Storage = new Storage( |
| 96 | + new JwkMemStore(), |
| 97 | + new KeyIdMemStore(), |
| 98 | + ); |
| 99 | + let { document: issuerDocument, fragment: issuerFragment } = await createDid( |
| 100 | + client, |
| 101 | + issuerSecretManager, |
| 102 | + issuerStorage, |
| 103 | + ); |
| 104 | + |
| 105 | + // =========================================================================== |
| 106 | + // Step 2: Issuer creates and signs a Verifiable Credential with BBS algorithm. |
| 107 | + // =========================================================================== |
| 108 | + |
| 109 | + // Create a credential subject indicating the degree earned by Alice. |
| 110 | + const subject = { |
| 111 | + name: "Alice", |
| 112 | + mainCourses: ["Object-oriented Programming", "Mathematics"], |
| 113 | + degree: { |
| 114 | + type: "BachelorDegree", |
| 115 | + name: "Bachelor of Science and Arts", |
| 116 | + }, |
| 117 | + GPA: 4.0, |
| 118 | + }; |
| 119 | + |
| 120 | + // Build credential using the above subject and issuer. |
| 121 | + const credential = new Credential({ |
| 122 | + id: "https:/example.edu/credentials/3732", |
| 123 | + issuer: issuerDocument.id(), |
| 124 | + type: "UniversityDegreeCredential", |
| 125 | + credentialSubject: subject, |
| 126 | + }); |
| 127 | + const credentialJpt = await issuerDocument |
| 128 | + .createCredentialJpt( |
| 129 | + credential, |
| 130 | + issuerStorage, |
| 131 | + issuerFragment, |
| 132 | + new JwpCredentialOptions(), |
| 133 | + ); |
| 134 | + // Validate the credential's proof using the issuer's DID Document, the credential's semantic structure, |
| 135 | + // that the issuance date is not in the future and that the expiration date is not in the past: |
| 136 | + const decodedJpt = JptCredentialValidator.validate( |
| 137 | + credentialJpt, |
| 138 | + issuerDocument, |
| 139 | + new JptCredentialValidationOptions(), |
| 140 | + FailFast.FirstError, |
| 141 | + ); |
| 142 | + |
| 143 | + // =========================================================================== |
| 144 | + // Step 3: Issuer sends the Verifiable Credential to the holder. |
| 145 | + // =========================================================================== |
| 146 | + console.log("Sending credential (as JPT) to the holder: " + credentialJpt.toString()); |
| 147 | + |
| 148 | + // ============================================================================================ |
| 149 | + // Step 4: Holder resolve Issuer's DID, retrieve Issuer's document and validate the Credential |
| 150 | + // ============================================================================================ |
| 151 | + const identityClient = new IotaIdentityClient(client); |
| 152 | + |
| 153 | + // Holder resolves issuer's DID. |
| 154 | + let issuerDid = IotaDID.parse(JptCredentialValidatorUtils.extractIssuerFromIssuedJpt(credentialJpt).toString()); |
| 155 | + let issuerDoc = await identityClient.resolveDid(issuerDid); |
| 156 | + |
| 157 | + // Holder validates the credential and retrieve the JwpIssued, needed to construct the JwpPresented |
| 158 | + let decodedCredential = JptCredentialValidator.validate( |
| 159 | + credentialJpt, |
| 160 | + issuerDoc, |
| 161 | + new JptCredentialValidationOptions(), |
| 162 | + FailFast.FirstError, |
| 163 | + ); |
| 164 | + |
| 165 | + // =========================================================================== |
| 166 | + // Step 5: Verifier sends the holder a challenge and requests a Presentation. |
| 167 | + // |
| 168 | + // Please be aware that when we mention "Presentation," we are not alluding to the Verifiable Presentation standard as defined by W3C (https://www.w3.org/TR/vc-data-model/#presentations). |
| 169 | + // Instead, our reference is to a JWP Presentation (https://datatracker.ietf.org/doc/html/draft-ietf-jose-json-web-proof#name-presented-form), which differs from the W3C standard. |
| 170 | + // =========================================================================== |
| 171 | + |
| 172 | + // A unique random challenge generated by the requester per presentation can mitigate replay attacks. |
| 173 | + const challenge = "475a7984-1bb5-4c4c-a56f-822bccd46440"; |
| 174 | + |
| 175 | + // ========================================================================================================= |
| 176 | + // Step 6: Holder engages in the Selective Disclosure of credential's attributes. |
| 177 | + // ========================================================================================================= |
| 178 | + const methodId = decodedCredential |
| 179 | + .decodedJwp() |
| 180 | + .getIssuerProtectedHeader() |
| 181 | + .kid!; |
| 182 | + const selectiveDisclosurePresentation = new SelectiveDisclosurePresentation(decodedCredential.decodedJwp()); |
| 183 | + selectiveDisclosurePresentation.concealInSubject("mainCourses[1]"); |
| 184 | + selectiveDisclosurePresentation.concealInSubject("degree.name"); |
| 185 | + |
| 186 | + // ======================================================================================================================================= |
| 187 | + // Step 7: Holder needs Issuer's Public Key to compute the Signature Proof of Knowledge and construct the Presentation |
| 188 | + // JPT. |
| 189 | + // ======================================================================================================================================= |
| 190 | + |
| 191 | + // Construct a JPT(JWP in the Presentation form) representing the Selectively Disclosed Verifiable Credential |
| 192 | + const presentationOptions = new JwpPresentationOptions(); |
| 193 | + presentationOptions.nonce = challenge; |
| 194 | + const presentationJpt = await issuerDoc |
| 195 | + .createPresentationJpt( |
| 196 | + selectiveDisclosurePresentation, |
| 197 | + methodId, |
| 198 | + presentationOptions, |
| 199 | + ); |
| 200 | + |
| 201 | + // =========================================================================== |
| 202 | + // Step 8: Holder sends a Presentation JPT to the Verifier. |
| 203 | + // =========================================================================== |
| 204 | + |
| 205 | + console.log("Sending presentation (as JPT) to the verifier: " + presentationJpt.toString()); |
| 206 | + |
| 207 | + // =========================================================================== |
| 208 | + // Step 9: Verifier receives the Presentation and verifies it. |
| 209 | + // =========================================================================== |
| 210 | + |
| 211 | + // Verifier resolve Issuer DID |
| 212 | + const issuerDidV = IotaDID.parse( |
| 213 | + JptPresentationValidatorUtils.extractIssuerFromPresentedJpt(presentationJpt).toString(), |
| 214 | + ); |
| 215 | + const issuerDocV = await identityClient.resolveDid(issuerDidV); |
| 216 | + |
| 217 | + const presentationValidationOptions = new JptPresentationValidationOptions({ nonce: challenge }); |
| 218 | + const decodedPresentedCredential = JptPresentationValidator.validate( |
| 219 | + presentationJpt, |
| 220 | + issuerDocV, |
| 221 | + presentationValidationOptions, |
| 222 | + FailFast.FirstError, |
| 223 | + ); |
| 224 | + |
| 225 | + console.log("Presented credential successfully validated: " + decodedPresentedCredential.credential()); |
| 226 | +} |
0 commit comments