diff --git a/xadesjs/xadesjs-tests.ts b/xadesjs/xadesjs-tests.ts new file mode 100644 index 0000000000..e83cb0cf6a --- /dev/null +++ b/xadesjs/xadesjs-tests.ts @@ -0,0 +1,47 @@ +/// + +let xmlString = ""; +let xmlDoc = new DOMParser().parseFromString(xmlString, "application/xml"); +let signedXml = new xadesjs.SignedXml(xmlDoc); + +let alg = { + name: "ECDSA", + namedCurve: "P-256" +}; +xadesjs.Application.crypto.subtle.generateKey( + alg, + false, + ["sign", "verify"] +) + .then((keys: CryptoKeyPair) => { + signedXml.SigningKey = keys.privateKey; + + // Add the key to the SignedXml document. + // Create a reference to be signed. + let reference = new xadesjs.Reference(); + reference.Uri = ""; + // Add an enveloped transformation to the reference. + reference.AddTransform(new xadesjs.XmlDsigEnvelopedSignatureTransform()); + // Add the reference to the SignedXml object. + signedXml.AddReference(reference); + // Set prefix for Signature namespace + signedXml.Prefix = "ds"; + + // Compute the signature. + return signedXml.ComputeSignature({ name: "ECDSA", hash: { name: "SHA-256" } } as any); + }) + .then(function () { + // Append signature + let xmlDigitalSignature = signedXml.GetXml(); + xmlDoc.documentElement.appendChild(xmlDigitalSignature); + + // Serialize XML document + let signedDocument = new XMLSerializer().serializeToString(xmlDoc); + return Promise.resolve(signedDocument); + }) + .then((doc: string) => { + console.log("Signed document:", doc); + }) + .catch((e) => { + console.error("Error", e); + }) \ No newline at end of file diff --git a/xadesjs/xadesjs.d.ts b/xadesjs/xadesjs.d.ts index 4eb7b6a1c5..9f89cab9f1 100644 --- a/xadesjs/xadesjs.d.ts +++ b/xadesjs/xadesjs.d.ts @@ -1,5 +1,15 @@ +// Type definitions for graphene-pk11 v2.0.0 +// Project: https://github.com/PeculiarVentures/graphene +// Definitions by: Stepan Miroshin +// Definitions: https://github.com/borisyankov/DefinitelyTyped + /// +/** + * A pure Javascript implementation of XMLDSIG and XAdES based on Web Crypto https://xadesjs.com + * v1.0.0 + */ + declare namespace xadesjs { const APPLICATION_XML: string;