jolocom.service.ts 11.9 KB
Newer Older
1 2 3 4 5 6
import { Injectable, Logger } from '@nestjs/common';
import { JolocomLib } from 'jolocom-lib';
import { JolocomRegistry } from 'jolocom-lib/js/registries/jolocomRegistry';
import { InjectRepository } from '@nestjs/typeorm';
import { Repository } from 'typeorm';

Hidde-Jan Jongsma's avatar
Hidde-Jan Jongsma committed
7
import { ConnectorService } from '../connector-service.interface';
8 9 10
import { Organization } from '../../organizations/organization.entity';
import { JolocomWallet } from './jolocom-wallet.entity';
import { JolocomCredentialType } from './jolocom-credential-type.entity';
Hidde-Jan Jongsma's avatar
Hidde-Jan Jongsma committed
11 12 13 14 15

@Injectable()
export class JolocomService implements ConnectorService {
  type = 'jolocom';

16 17 18 19 20 21 22 23 24 25 26 27 28
  private registry: JolocomRegistry;
  private logger: Logger;

  constructor(
    @InjectRepository(JolocomWallet)
    private walletRepository: Repository<JolocomWallet>,
  ) {
    this.registry = JolocomLib.registries.jolocom.create();
    this.logger = new Logger(JolocomService.name);
  }

  /* ConnectorService methods */

Hidde-Jan Jongsma's avatar
Hidde-Jan Jongsma committed
29
  async registerOrganization(organization: Organization) {
30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89
    const wallet = await this.createWalletForOrganization(organization);
    await this.fuelWallet(wallet);
    await this.registerWallet(wallet);
  }

  async createWalletForOrganization(organization: Organization) {
    this.logger.log(`Creating wallet for ${organization.name}`);
    const seed = JolocomWallet.randomSeed();
    const password = JolocomWallet.randomPassword();
    const keyProvider = JolocomLib.KeyProvider.fromSeed(seed, password);

    const wallet = new JolocomWallet();
    wallet.organization = organization;
    wallet.password = password;
    wallet.encryptedSeedHex = keyProvider.encryptedSeed; // Already in hex format

    await this.walletRepository.save(wallet);
    this.logger.log(`Created wallet for ${organization.name}`);
    return wallet;
  }

  async registerWallet(wallet: JolocomWallet) {
    this.logger.log(`Wallet registration started`);
    const keyProvider = this.getKeyProvider(wallet);
    const identityWallet = await this.registry.create(
      keyProvider,
      wallet.password,
    );
    this.logger.log(`Wallet registration successful`);

    // TODO: Maybe we shouldn't return here.
    return identityWallet;
  }

  async getIdentityWallet(wallet: JolocomWallet) {
    const keyProvider = this.getKeyProvider(wallet);
    const identityWallet = await this.registry.authenticate(keyProvider, {
      derivationPath: JolocomLib.KeyTypes.jolocomIdentityKey,
      encryptionPass: wallet.password,
    });
    return identityWallet;
  }

  async fuelWallet(wallet: JolocomWallet) {
    this.logger.log(`Wallet fueling started`);
    const publicEthKey = this.getPublicEthKey(wallet);
    await JolocomLib.util.fuelKeyWithEther(publicEthKey);
    this.logger.log(`Wallet fueling started`);
  }

  getPublicEthKey(wallet: JolocomWallet) {
    const keyProvider = this.getKeyProvider(wallet);
    return keyProvider.getPublicKey({
      encryptionPass: wallet.password,
      derivationPath: JolocomLib.KeyTypes.ethereumKey,
    });
  }

  getKeyProvider(wallet: JolocomWallet) {
    return new JolocomLib.KeyProvider(wallet.encryptedSeed);
Hidde-Jan Jongsma's avatar
Hidde-Jan Jongsma committed
90
  }
91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331

  /* Jolocom specific */
  // public async processCredentialIssueRequest(
  //   request: CredentialIssueRequest,
  //   response: Response,
  // ): Promise<void> {
  //   const credOffer = await this.createCredentialOfferToken(request);
  //   const token = credOffer.encode();

  //   const viewData = {
  //     requestId: request.requestId,
  //     qr: await QRCode.toDataURL(token),
  //   };
  //   console.log('Jolocom CredentialOfferToken: ', credOffer.encode());

  //   return response.render('jolocom/issue', viewData);
  // }

  // public async processCredentialVerifyRequest(
  //   verifyRequest: CredentialVerifyRequest,
  //   response: Response,
  // ): Promise<void> {
  //   // Create Jolocom interaction token
  //   const credRequestToken = await this.createCredentialRequestToken(
  //     verifyRequest,
  //   );

  //   const jwt = credRequestToken.encode();

  //   // Render interaction token (qr code) to user
  //   const viewData = {
  //     requestId: verifyRequest.requestId,
  //     qr: await QRCode.toDataURL(jwt),
  //   };

  //   return response.render('jolocom/verify', viewData);
  // }

  /**
   * Instantiate a Jolocom IdentityWallet
   * This wallet must already be registered on Etherium
   *
   * @param organization the organization for which an identifity wallet is instantiated
   */
  // protected async getIdentityWallet(
  //   organization: Organization,
  // ): Promise<IdentityWallet> {
  //   const password = organization.walletConfigs.jolocom!.password;
  //   const seed = Buffer.from(organization.walletConfigs.jolocom!.seed, 'hex');

  //   /**
  //    * From Jolocom Documentation:
  //    * You will need to instantiate a Key Provider using the seed used for identity creation
  //    * We are currently working on simplifying, and optimising this part of the api
  //    */
  //   const vaultedKeyProvider = JolocomLib.KeyProvider.fromSeed(seed, password);
  //   const registry = JolocomLib.registries.jolocom.create();

  //   return await registry.authenticate(vaultedKeyProvider, {
  //     derivationPath: JolocomLib.KeyTypes.jolocomIdentityKey,
  //     encryptionPass: password,
  //   });
  // }

  // /**
  //  * Construct a Jolocom CredentialOffer interaction token
  //  *
  //  * @param issueRequest the credential issue request
  //  */
  // protected async createCredentialOfferToken(
  //   issueRequest: CredentialIssueRequest,
  // ): Promise<JSONWebToken<CredentialOfferRequest>> {
  //   const issuer = issueRequest.getIssuer();
  //   const identityWallet = await this.getIdentityWallet(issuer);
  //   const { credentialOffers, password } = issuer.walletConfigs.jolocom!;

  //   // Get the Jolocom offeredType and metadata from configured list with credential offers
  //   const {
  //     schema: { type: offeredType },
  //     metadata = {},
  //   } = credentialOffers[issueRequest.credentialType]; // Use the credential type URI as defined in the CredentialIssueRequest

  //   // Return a Jolocom CredentialOffert interaction token
  //   return await identityWallet.create.interactionTokens.request.offer(
  //     {
  //       callbackURL: SSIServiceApp.getUrl(
  //         `/connectors/jolocom/issue/${issueRequest.requestId}`,
  //       ),
  //       offeredCredentials: [
  //         {
  //           type: offeredType[offeredType.length - 1],
  //           ...metadata,
  //         },
  //       ],
  //     },
  //     password,
  //   );
  // }

  // /**
  //  * Construct a Jolocom CredentialReceive interaction token
  //  * to actual issue the credential to the user's wallet app
  //  *
  //  * @param issueRequest the credential issue request
  //  * @param jolocomOfferResponse the Jolocom CredentialOfferResponse that is received from the user's wallet app
  //  */
  // public async createCredential(
  //   issueRequest: CredentialIssueRequest,
  //   jolocomOfferResponse: JSONWebToken<JWTEncodable>,
  // ): Promise<JSONWebToken<JWTEncodable>> {
  //   const issuer = issueRequest.getIssuer(); // the organization that wants to issue a credential
  //   const identityWallet = await this.getIdentityWallet(issuer); // jolocom identity wallet of the issuer
  //   const password = issuer.walletConfigs.jolocom!.password; // password to sign credential
  //   const subject = jolocomOfferResponse.issuer; // the wallet app that wants to receive a credential
  //   const credentialType = issuer.walletConfigs.jolocom!.credentialOffers[
  //     issueRequest.credentialType
  //   ];

  //   // Create actual credential (with data)
  //   const credential = await identityWallet.create.signedCredential(
  //     {
  //       metadata: credentialType.schema,
  //       claim: {
  //         ...issueRequest.credentialData,
  //       },
  //       subject: keyIdToDid(subject),
  //     },
  //     password,
  //   );

  //   // Wrap credential in Jolocom interaction token object
  //   return await identityWallet.create.interactionTokens.response.issue(
  //     {
  //       signedCredentials: [credential.toJSON()],
  //     },
  //     password,
  //     jolocomOfferResponse,
  //   );
  // }

  // /**
  //  * Construct a Jolocom CredentialRequest interaction token
  //  *
  //  * @param verifyRequest the credential verify request
  //  */
  // protected async createCredentialRequestToken(
  //   verifyRequest: CredentialVerifyRequest,
  // ): Promise<JSONWebToken<CredentialRequest>> {
  //   const verifier = verifyRequest.getVerifier();
  //   const identityWallet = await this.getIdentityWallet(verifier);
  //   const password = verifier.walletConfigs.jolocom!.password;
  //   const credentialRequestToken = await identityWallet.create.interactionTokens.request.share(
  //     {
  //       callbackURL: SSIServiceApp.getUrl(
  //         `/connectors/jolocom/verify/${verifyRequest.requestId}`,
  //       ),
  //       credentialRequirements: [
  //         {
  //           type: ['Credential', verifyRequest.credentialType],
  //           constraints: [
  //             // TODO: implement check on allowed issuers
  //             // constraintFunctions.is(
  //             //   "issuer",
  //             //   "did:jolo:ed19430d6e28057194870dc9b19c1ca2ad099ff090b52350add129f1049bb65d"
  //             // )
  //           ],
  //         },
  //       ],
  //     },
  //     password,
  //   );

  //   // Save CredentialRequest because it is needed to verify a CredentialResponse token in the
  //   // next step. See method receiveCredential() below
  //   Store.set(credentialRequestToken.nonce, credentialRequestToken.encode());

  //   return credentialRequestToken;
  // }

  // public async receiveCredential(
  //   verifyRequest: CredentialVerifyRequest,
  //   token: string,
  // ): Promise<any> {
  //   const jolocomCredentialResponse = JolocomLib.parse.interactionToken.fromJWT(
  //     token,
  //   );

  //   // TODO: check if this is needed here or can be done in JolocomConnector.receiveCredential() method
  //   if (!JolocomLib.util.validateDigestable(jolocomCredentialResponse)) {
  //     throw new Error('Invalid signature');
  //     // res.status(401).send("Invalid signature on interaction token");
  //   }

  //   const identityWallet = await this.getIdentityWallet(
  //     verifyRequest.getVerifier(),
  //   );

  //   // Get the CredentialShareToken (issued in previous interaction step)
  //   const jolocomCredentialRequestJWT: string = Store.get(
  //     jolocomCredentialResponse.nonce,
  //   );

  //   const jolocomCredentialRequest: JSONWebToken<CredentialRequest> = JolocomLib.parse.interactionToken.fromJWT(
  //     jolocomCredentialRequestJWT,
  //   );

  //   // The validate method will ensure the response contains a valid signature, is not expired,
  //   // lists our did in the aud (audience) section, and contains the same jti (nonce) as the request.
  //   // TODO: move this to route middleware
  //   await identityWallet.validateJWT(
  //     jolocomCredentialResponse,
  //     jolocomCredentialRequest,
  //   );

  //   const credentialResponse = jolocomCredentialResponse.interactionToken as CredentialResponse;

  //   // We check against the request we created in a previous step
  //   const validResponse = credentialResponse.satisfiesRequest(
  //     jolocomCredentialRequest.interactionToken,
  //   );

  //   if (!validResponse) {
  //     throw new Error('Incorrect credential received');
  //   }

  //   // Validate the provided credentials
  //   const providedCredentials = credentialResponse.suppliedCredentials;
  //   const signatureValidationResults = await JolocomLib.util.validateDigestables(
  //     providedCredentials,
  //   );

  //   if (signatureValidationResults.every(result => result === true)) {
  //     // The credentials can be used
  //     const data = providedCredentials.map(credential => credential.toJSON());

  //     // Handle the data in the provided credentials
  //     return verifyRequest.processCredentialData(data);
  //   } else {
  //     throw new Error('Not all provided credentials are valid');
  //   }
  // }
Hidde-Jan Jongsma's avatar
Hidde-Jan Jongsma committed
332
}