openwallet-foundation / credo-ts

Typescript framework for building decentralized identity and verifiable credential solutions
https://credo.js.org
Apache License 2.0
277 stars 202 forks source link

X509: Cannot initialize GeneralNames. Incorrect incoming arguments #1969

Closed jack-robson closed 4 months ago

jack-robson commented 4 months ago

Trying to use createSelfSignedCertificate but getting type errors (commented in code). If I ignore the type errors, I get this error:

/Users/jr/Projects/typescript-project/node_modules/@peculiar/x509/build/x509.cjs.js:1034
            throw new Error("Cannot initialize GeneralNames. Incorrect incoming arguments");
                  ^
Error: Cannot initialize GeneralNames. Incorrect incoming arguments
    at new GeneralNames (/Users/jr/Projects/typescript-project/node_modules/@peculiar/x509/build/x509.cjs.js:1034:19)
    at new SubjectAlternativeNameExtension (/Users/jr/Projects/typescript-project/node_modules/@peculiar/x509/build/x509.cjs.js:1763:70)
    at /Users/jr/Projects/typescript-project/node_modules/@credo-ts/core/src/modules/x509/X509Certificate.ts:111:52
    at Array.map (<anonymous>)
    at Function.createSelfSigned (/Users/jr/Projects/typescript-project/node_modules/@credo-ts/core/src/modules/x509/X509Certificate.ts:111:33)
    at Function.createSelfSignedCertificate (/Users/jr/Projects/typescript-project/node_modules/@credo-ts/core/src/modules/x509/X509Service.ts:119:47)
    at X509Api.createSelfSignedCertificate (/Users/jr/Projects/typescript-project/node_modules/@credo-ts/core/src/modules/x509/X509Api.ts:43:30)
    at /Users/jr/Projects/typescript-project/bug.ts:40:33

Code:

import { Agent, InitConfig, KeyBackend, KeyType } from "@credo-ts/core";
import { agentDependencies } from "@credo-ts/node";
import { ariesAskar } from "@hyperledger/aries-askar-nodejs";
import { AskarModule } from "@credo-ts/askar";
import { Router } from "express";
import { OpenId4VcIssuerModule } from "@credo-ts/openid4vc";

export const openId4VciRouter = Router();

(async () => {
  const config = {
    label: "name",
    walletConfig: { id: "name", key: "name" },
  } satisfies InitConfig;
  const agent = new Agent({
    config,
    dependencies: agentDependencies,
    modules: {
      askar: new AskarModule({ ariesAskar }),
      openId4VcIssuer: new OpenId4VcIssuerModule({
        baseUrl: "http://localhost:2000/oid4vci",
        router: openId4VciRouter,
        endpoints: {
          credential: {
            // @ts-expect-error
            credentialRequestToCredentialMapper: (...args) => null,
          },
        },
      }),
    },
  });

  await agent.initialize();

  // not sure how to get he default agent key so creating another one
  const myKey = await agent.wallet.createKey({
    keyType: KeyType.Ed25519,
  });

  const cert = await agent.x509.createSelfSignedCertificate({
    key: myKey,
    extensions: [
      // @ts-expect-error - Object literal may only specify known properties, and 'type' does not exist in type '{ type: "url" | "dns"; value: string; }[]'.ts(2353)
      { type: "url", value: "animo.id" },
      // @ts-expect-error - Object literal may only specify known properties, and 'type' does not exist in type '{ type: "url" | "dns"; value: string; }[]'.ts(2353)
      { type: "dns", value: "paradym.id" },
    ],
  });

  console.log({ cert, str: cert.toString("base64") });
})();

Packages:

    "@credo-ts/askar": "^0.5.9",
    "@credo-ts/core": "^0.5.9",
    "@credo-ts/node": "^0.5.9",
    "@credo-ts/openid4vc": "^0.5.9",
    "@hyperledger/aries-askar-nodejs": "0.2.3",
berendsliedrecht commented 4 months ago

Hi!

Calling the agent.x509.createSelfSignedCertificate is being called with the wrong arguments.

  const cert = await agent.x509.createSelfSignedCertificate({
    key: myKey,
    extensions: [
-     // @ts-expect-error - Object literal may only specify known properties, and 'type' does not exist in type '{ type: "url" | "dns"; value: string; }[]'.ts(2353)
-     { type: "url", value: "animo.id" },
+     [{ type: "url", value: "animo.id" }],
-     // @ts-expect-error - Object literal may only specify known properties, and 'type' does not exist in type '{ type: "url" | "dns"; value: string; }[]'.ts(2353)
-     { type: "dns", value: "paradym.id" },
+     [{ type: "dns", value: "paradym.id" }],
    ],
  });

Every extension can have mulitple entries and you can have multiple extensions, so it has to be a list of lists.

jack-robson commented 4 months ago

Awesome, thanks!