xadesjs: XAdES.SignedXml is not a constructor (electronJS)

Hello , I am trying to create an xadesjs signature of an xml string and I use electronjs as javascript library which allows to create desktop applications using html, css and js brief when calling the function (Sign) the console shows me this error:: TypeError: XAdES.SignedXml is not a constructor ! PS: the signature works in the browser but not in the console of the electron app !!!

index.html

<html>

<head>
  <meta charset="UTF-8">
  <title>BerryAgent</title>
  <link rel="stylesheet" type="text/css" href="style.css">
  <link rel="stylesheet" href="https://maxcdn.bootstrapcdn.com/bootstrap/4.0.0/css/bootstrap.min.css" integrity="sha384-Gn5384xqQ1aoWXA+058RXPxPg6fy4IWvTNh0E263XmFcJlSAwiGgFAW/dAiS6JXm"
    crossorigin="anonymous">
</head>

<body>

  <script src="https://peculiarventures.github.io/pv-webcrypto-tests/src/promise.js"></script> 
  <script src="https://peculiarventures.github.io/pv-webcrypto-tests/src/webcrypto-liner.min.js"></script> 
  <script src="https://peculiarventures.github.io/pv-webcrypto-tests/src/asmcrypto.js"></script> 
  <script src="https://peculiarventures.github.io/pv-webcrypto-tests/src/elliptic.js"></script>
  <script src="https://peculiarventures.github.io/xadesjs/src/xades.min.js"></script>
  <script type="text/javascript" src="node_modules/xadesjs/dist/xades.js"></script> 
  <script type="text/javascript" src="./main.js">
    //You can also require other files to run in this process
  </script> 

  <script>
    function show_sign_div() {
      var signDiv = document.getElementById("signDIV");
      var verifyDiv = document.getElementById("verifyDIV");
      signDiv.style.display = "block";
      verifyDiv.style.display = "none";
    }
    function show_verify_div() {
      var signDiv = document.getElementById("signDIV");
      var verifyDiv = document.getElementById("verifyDIV");
      signDiv.style.display = "none";
      verifyDiv.style.display = "block";
    }
  </script>

<div class="split left">
    <div class="centered">
      <h2>
        <button class="btn btn-secondary btn-lg" onclick="show_sign_div();">Sign</button>
      </h2>
      <div id="signDIV" style="display: none;">
        <h1>XADES::SIGN</h1>
        <form id="SignxmlForm">
          <h4>Set xml</h4>
          <pre id="signature"><code></code></pre>
          <textarea id="xml1" cols="30" rows="10">
            </textarea>
          <input type="button" value="Sign" onclick="sign()">
        </form>
      </div>
    </div>
  </div>

</body>
</html>`

main.js

function getXml() {
    return document.getElementById("xml1").value;
}

function generateKey(alg) {
    return crypto.subtle.generateKey(alg, false, ["sign", "verify"])
}

 function exportKey(key) {
     return crypto.subtle.exportKey("jwk", key)
 }

function error(e) {
    alert(e.message);
    console.error(e);
}

function sign() {
    // var transforms = [];
    // if (isEnveloped())
    //     transforms.push("enveloped");
    // transforms.push(getCanonMethod());
    // console.log(transforms);

    // var alg = getAlgorithm();
    var keys, signature, res = {};
     Promise.resolve()
         .then(function () {
             return generateKey({
                                name: "ECDSA",
                                namedCurve: "P-256",
                                modulusLength: 1024,
                                publicExponent: new Uint8Array([1, 0, 1]),
                             });
         })
         .then(function (ks) {
          keys = ks;
             return exportKey(ks.publicKey)
         })
         .then(function (jwk) {
             res.jwk = jwk;
         })
        .then(function () {
            signature = new XAdES.SignedXml();

            return signature.Sign(                  // Signing document
                { name: "ECDSA", hash: { name: "SHA-1" } },                                    // algorithm
                keys.privateKey,                        // key
                XAdES.Parse(getXml()),                  // document
                {                                       // options
                    keyValue: keys.publicKey,
                    references: [
                        { hash: "SHA-256", transforms: ["enveloped"] }
                    ],
                    productionPlace: {
                        country: "Country",
                        state: "State",
                        city: "City",
                        code: "Code",
                    },
                    signerRoles: {
                        claimed: ["Some role"]
                    }
                })
        })
        .then(function () {
            var sig = signature.toString()
            res.signature = sig;

            //document.getElementById("jwk").value = JSON.stringify(res.jwk);
            //document.getElementById("signature").value = res.signature;
            console.log(res.signature) ; 
        })
        .catch(function (e) {
            console.error(e);
        });

}

About this issue

  • Original URL
  • State: closed
  • Created 6 years ago
  • Comments: 15 (8 by maintainers)

Most upvoted comments

I created test application https://github.com/microshine/electron-xadesjs

  • clone it
  • run npm install from created folder
  • run app electron . (Electron should be installed globally)

it works Thanks a lot @microshine . I think it’s due to electron version !

@digitalberry-ahmed try to comment xadesjs

<!-- <script type="text/javascript" src="node_modules/xadesjs/dist/xades.js"></script> -->

and use require in main.js

const XAdES = require("xadesjs");

Electron: 1.8.4

To me it sounds like there is a scope problem here, that xadesjs is loaded ahead of invocation or similar.

use npm list xadesjs instead of npm -v