Skip to content

TypeError: Key is not of type 'CryptoKey' #87

@ppodgorskicrido

Description

@ppodgorskicrido

I'm trying to run example from package:

import * as xadesjs from 'xadesjs';
import { Crypto } from '@peculiar/webcrypto';
import { setNodeDependencies } from 'xml-core';
import {DOMParser, XMLSerializer, DOMImplementation} from "@xmldom/xmldom";

xadesjs.Application.setEngine('NodeJS', new Crypto());
setNodeDependencies({
      DOMImplementation: DOMImplementation,
      DOMParser: DOMParser,
      XMLSerializer: XMLSerializer
    });

async function signXAdES() {
  // Generate key pair
  const keys = await crypto.subtle.generateKey(
    {
      name: "RSASSA-PKCS1-v1_5",
      modulusLength: 2048,
      publicExponent: new Uint8Array([1, 0, 1]),
      hash: "SHA-256"
    },
    false,
    ["sign", "verify"]
  );

  // Parse XML document
  const xml = xadesjs.Parse('<root><data>Hello World</data></root>');

  // Create XAdES signature
  const signedXml = new xadesjs.SignedXml();
  await signedXml.Sign(
    { name: "RSASSA-PKCS1-v1_5" },
    keys.privateKey,
    xml,
    {
      keyValue: keys.publicKey,
      references: [{ hash: "SHA-256", transforms: ["enveloped"] }],
      productionPlace: {
        country: "US",
        state: "California",
        city: "San Francisco"
      },
      signerRole: {
        claimed: ["Developer"]
      }
    }
  );

  console.log(signedXml.toString());
}

signXAdES().catch(console.error);

Error:

TypeError: Key is not of type 'CryptoKey'
    at SubtleCrypto.checkCryptoKey (<path>\node_modules\webcrypto-core\build\webcrypto-core.js:1589:19)
    at SubtleCrypto.exportKey (<path>\node_modules\webcrypto-core\build\webcrypto-core.js:1503:14)
    at RsaKeyValue.importKey (file:///<path>/node_modules/xmldsigjs/build/esm/xml/key_infos/rsa_key.js:31:53)
    at KeyValue.importKey (file:///<path>/node_modules/xmldsigjs/build/esm/xml/key_infos/key_value.js:26:32)
    at SignedXml.ApplySignOptions (file:///<path>/node_modules/xmldsigjs/build/esm/signed_xml.js:405:28)
    at SignedXml.ApplySignOptions (file:///<path>/node_modules/xadesjs/build/esm/signed_xml.js:61:21)
    at SignedXml.Sign (file:///<path>/node_modules/xmldsigjs/build/esm/signed_xml.js:40:20)
    at SignedXml.Sign (file:///<path>/node_modules/xadesjs/build/esm/signed_xml.js:45:27)
    at signXAdES (file:///<path>/index.js:31:19)

I had to add

setNodeDependencies({
    DOMImplementation: DOMImplementation,
    DOMParser: DOMParser,
    XMLSerializer: XMLSerializer
});

because of error:

Error: Node dependency not found: DOMParser. Please use 'setNodeDependencies' to register it.
    at getNodeDependency (file:///<path>/node_modules/xml-core/build/esm/utils.js:99:15)
    at Module.Parse (file:///<path>/node_modules/xml-core/build/esm/utils.js:27:25)

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions