@transmute/did-key-common
Version:
This module contains commont interfaces and factories for did:key.
3 lines (2 loc) • 13.7 kB
JavaScript
"use strict";Object.defineProperty(exports,"__esModule",{value:!0});var t=require("@did-core/data-model"),e=require("@did-core/did-ld-json"),r=require("@transmute/did-context"),n=require("@transmute/security-context");function o(t,e,r,n,o,i,a){try{var c=t[i](a),u=c.value}catch(t){return void r(t)}c.done?e(u):Promise.resolve(u).then(n,o)}function i(t){return function(){var e=this,r=arguments;return new Promise((function(n,i){var a=t.apply(e,r);function c(t){o(a,n,i,c,u,"next",t)}function u(t){o(a,n,i,c,u,"throw",t)}c(void 0)}))}}function a(){return(a=Object.assign||function(t){for(var e=1;e<arguments.length;e++){var r=arguments[e];for(var n in r)Object.prototype.hasOwnProperty.call(r,n)&&(t[n]=r[n])}return t}).apply(this,arguments)}var c,u,s=(function(t){var e=function(t){var e=Object.prototype,r=e.hasOwnProperty,n="function"==typeof Symbol?Symbol:{},o=n.iterator||"@@iterator",i=n.asyncIterator||"@@asyncIterator",a=n.toStringTag||"@@toStringTag";function c(t,e,r){return Object.defineProperty(t,e,{value:r,enumerable:!0,configurable:!0,writable:!0}),t[e]}try{c({},"")}catch(t){c=function(t,e,r){return t[e]=r}}function u(t,e,r,n){var o=Object.create((e&&e.prototype instanceof f?e:f).prototype),i=new L(n||[]);return o._invoke=function(t,e,r){var n="suspendedStart";return function(o,i){if("executing"===n)throw new Error("Generator is already running");if("completed"===n){if("throw"===o)throw i;return{value:void 0,done:!0}}for(r.method=o,r.arg=i;;){var a=r.delegate;if(a){var c=x(a,r);if(c){if(c===p)continue;return c}}if("next"===r.method)r.sent=r._sent=r.arg;else if("throw"===r.method){if("suspendedStart"===n)throw n="completed",r.arg;r.dispatchException(r.arg)}else"return"===r.method&&r.abrupt("return",r.arg);n="executing";var u=s(t,e,r);if("normal"===u.type){if(n=r.done?"completed":"suspendedYield",u.arg===p)continue;return{value:u.arg,done:r.done}}"throw"===u.type&&(n="completed",r.method="throw",r.arg=u.arg)}}}(t,r,i),o}function s(t,e,r){try{return{type:"normal",arg:t.call(e,r)}}catch(t){return{type:"throw",arg:t}}}t.wrap=u;var p={};function f(){}function l(){}function h(){}var y={};y[o]=function(){return this};var d=Object.getPrototypeOf,v=d&&d(d(K([])));v&&v!==e&&r.call(v,o)&&(y=v);var _=h.prototype=f.prototype=Object.create(y);function g(t){["next","throw","return"].forEach((function(e){c(t,e,(function(t){return this._invoke(e,t)}))}))}function m(t,e){var n;this._invoke=function(o,i){function a(){return new e((function(n,a){!function n(o,i,a,c){var u=s(t[o],t,i);if("throw"!==u.type){var p=u.arg,f=p.value;return f&&"object"==typeof f&&r.call(f,"__await")?e.resolve(f.__await).then((function(t){n("next",t,a,c)}),(function(t){n("throw",t,a,c)})):e.resolve(f).then((function(t){p.value=t,a(p)}),(function(t){return n("throw",t,a,c)}))}c(u.arg)}(o,i,n,a)}))}return n=n?n.then(a,a):a()}}function x(t,e){var r=t.iterator[e.method];if(void 0===r){if(e.delegate=null,"throw"===e.method){if(t.iterator.return&&(e.method="return",e.arg=void 0,x(t,e),"throw"===e.method))return p;e.method="throw",e.arg=new TypeError("The iterator does not provide a 'throw' method")}return p}var n=s(r,t.iterator,e.arg);if("throw"===n.type)return e.method="throw",e.arg=n.arg,e.delegate=null,p;var o=n.arg;return o?o.done?(e[t.resultName]=o.value,e.next=t.nextLoc,"return"!==e.method&&(e.method="next",e.arg=void 0),e.delegate=null,p):o:(e.method="throw",e.arg=new TypeError("iterator result is not an object"),e.delegate=null,p)}function E(t){var e={tryLoc:t[0]};1 in t&&(e.catchLoc=t[1]),2 in t&&(e.finallyLoc=t[2],e.afterLoc=t[3]),this.tryEntries.push(e)}function w(t){var e=t.completion||{};e.type="normal",delete e.arg,t.completion=e}function L(t){this.tryEntries=[{tryLoc:"root"}],t.forEach(E,this),this.reset(!0)}function K(t){if(t){var e=t[o];if(e)return e.call(t);if("function"==typeof t.next)return t;if(!isNaN(t.length)){var n=-1,i=function e(){for(;++n<t.length;)if(r.call(t,n))return e.value=t[n],e.done=!1,e;return e.value=void 0,e.done=!0,e};return i.next=i}}return{next:b}}function b(){return{value:void 0,done:!0}}return l.prototype=_.constructor=h,h.constructor=l,l.displayName=c(h,a,"GeneratorFunction"),t.isGeneratorFunction=function(t){var e="function"==typeof t&&t.constructor;return!!e&&(e===l||"GeneratorFunction"===(e.displayName||e.name))},t.mark=function(t){return Object.setPrototypeOf?Object.setPrototypeOf(t,h):(t.__proto__=h,c(t,a,"GeneratorFunction")),t.prototype=Object.create(_),t},t.awrap=function(t){return{__await:t}},g(m.prototype),m.prototype[i]=function(){return this},t.AsyncIterator=m,t.async=function(e,r,n,o,i){void 0===i&&(i=Promise);var a=new m(u(e,r,n,o),i);return t.isGeneratorFunction(r)?a:a.next().then((function(t){return t.done?t.value:a.next()}))},g(_),c(_,a,"Generator"),_[o]=function(){return this},_.toString=function(){return"[object Generator]"},t.keys=function(t){var e=[];for(var r in t)e.push(r);return e.reverse(),function r(){for(;e.length;){var n=e.pop();if(n in t)return r.value=n,r.done=!1,r}return r.done=!0,r}},t.values=K,L.prototype={constructor:L,reset:function(t){if(this.prev=0,this.next=0,this.sent=this._sent=void 0,this.done=!1,this.delegate=null,this.method="next",this.arg=void 0,this.tryEntries.forEach(w),!t)for(var e in this)"t"===e.charAt(0)&&r.call(this,e)&&!isNaN(+e.slice(1))&&(this[e]=void 0)},stop:function(){this.done=!0;var t=this.tryEntries[0].completion;if("throw"===t.type)throw t.arg;return this.rval},dispatchException:function(t){if(this.done)throw t;var e=this;function n(r,n){return a.type="throw",a.arg=t,e.next=r,n&&(e.method="next",e.arg=void 0),!!n}for(var o=this.tryEntries.length-1;o>=0;--o){var i=this.tryEntries[o],a=i.completion;if("root"===i.tryLoc)return n("end");if(i.tryLoc<=this.prev){var c=r.call(i,"catchLoc"),u=r.call(i,"finallyLoc");if(c&&u){if(this.prev<i.catchLoc)return n(i.catchLoc,!0);if(this.prev<i.finallyLoc)return n(i.finallyLoc)}else if(c){if(this.prev<i.catchLoc)return n(i.catchLoc,!0)}else{if(!u)throw new Error("try statement without catch or finally");if(this.prev<i.finallyLoc)return n(i.finallyLoc)}}}},abrupt:function(t,e){for(var n=this.tryEntries.length-1;n>=0;--n){var o=this.tryEntries[n];if(o.tryLoc<=this.prev&&r.call(o,"finallyLoc")&&this.prev<o.finallyLoc){var i=o;break}}i&&("break"===t||"continue"===t)&&i.tryLoc<=e&&e<=i.finallyLoc&&(i=null);var a=i?i.completion:{};return a.type=t,a.arg=e,i?(this.method="next",this.next=i.finallyLoc,p):this.complete(a)},complete:function(t,e){if("throw"===t.type)throw t.arg;return"break"===t.type||"continue"===t.type?this.next=t.arg:"return"===t.type?(this.rval=this.arg=t.arg,this.method="return",this.next="end"):"normal"===t.type&&e&&(this.next=e),p},finish:function(t){for(var e=this.tryEntries.length-1;e>=0;--e){var r=this.tryEntries[e];if(r.finallyLoc===t)return this.complete(r.completion,r.afterLoc),w(r),p}},catch:function(t){for(var e=this.tryEntries.length-1;e>=0;--e){var r=this.tryEntries[e];if(r.tryLoc===t){var n=r.completion;if("throw"===n.type){var o=n.arg;w(r)}return o}}throw new Error("illegal catch attempt")},delegateYield:function(t,e,r){return this.delegate={iterator:K(t),resultName:e,nextLoc:r},"next"===this.method&&(this.arg=void 0),p}},t}(t.exports);try{regeneratorRuntime=e}catch(t){Function("r","regeneratorRuntime = r")(e)}}(u={exports:{}}),u.exports),p=["assertionMethod","authentication","capabilityInvocation","capabilityDelegation"],f=["keyAgreement"],l={"OKP Ed25519":[].concat(p),"OKP X25519":[].concat(f),Ed25519VerificationKey2018:[].concat(p),X25519KeyAgreementKey2019:[].concat(f),"EC BLS12381_G1":[].concat(p),"EC BLS12381_G2":[].concat(p),Bls12381G1Key2020:[].concat(p),Bls12381G2Key2020:[].concat(p),"EC P-256":[].concat(p,f),"EC P-384":[].concat(p,f),"EC P-521":[].concat(p,f),P256Key2021:[].concat(p,f),P384Key2021:[].concat(p,f),P521Key2021:[].concat(p,f),"EC secp256k1":[].concat(p,f),EcdsaSecp256k1VerificationKey2019:[].concat(p,f)},h=function(t){return[].concat(l["JsonWebKey2020"===t.type?t.publicKeyJwk.kty+" "+t.publicKeyJwk.crv:t.type]||[])},y={"OKP Ed25519":"Ed25519VerificationKey2018","OKP X25519":"X25519KeyAgreementKey2019","EC BLS12381_G1":"Bls12381G1Key2020","EC BLS12381_G2":"Bls12381G2Key2020","EC P-256":"P256Key2021","EC P-384":"P384Key2021","EC P-521":"P521Key2021","EC secp256k1":"EcdsaSecp256k1VerificationKey2019"},d=function(t){var e=y[t.kty+" "+t.crv];if(!e)throw new Error("Could not map publicKeyJwk to latest security vocab type: "+t.kty+" "+t.crv);return e},v=function(){var t=i(s.mark((function t(e,r,n){return s.wrap((function(t){for(;;)switch(t.prev=t.next){case 0:return void 0===n&&(n=!1),t.next=3,Promise.all(e.map(function(){var t=i(s.mark((function t(e){var o,i;return s.wrap((function(t){for(;;)switch(t.prev=t.next){case 0:return t.next=2,e.export({type:"JsonWebKey2020",privateKey:n});case 2:if(o=t.sent,"application/did+json"!==r){t.next=5;break}return t.abrupt("return",o);case 5:return i=d(o.publicKeyJwk),t.next=8,e.export({type:i,privateKey:n});case 8:return t.abrupt("return",t.sent);case 9:case"end":return t.stop()}}),t)})));return function(e){return t.apply(this,arguments)}}()));case 3:return t.abrupt("return",t.sent);case 5:case"end":return t.stop()}}),t)})));return function(e,r,n){return t.apply(this,arguments)}}(),_=function(){var t=i(s.mark((function t(e,r,n){var o;return s.wrap((function(t){for(;;)switch(t.prev=t.next){case 0:return t.next=2,e.fromFingerprint({fingerprint:r});case 2:if(o=t.sent,!n||!o.getDerivedKeyPairs){t.next=7;break}return t.next=6,o.getDerivedKeyPairs();case 6:return t.abrupt("return",t.sent);case 7:if(!o.getPairedKeyPairs){t.next=11;break}return t.next=10,o.getPairedKeyPairs();case 10:return t.abrupt("return",t.sent);case 11:return t.abrupt("return",[o]);case 12:case"end":return t.stop()}}),t)})));return function(e,r,n){return t.apply(this,arguments)}}(),g=function(t){var e={};return t.forEach((function(t){h(t).forEach((function(r){e[r]=e[r]?[].concat(e[r],[t.id]):[t.id]}))})),e},m=function(){var t=i(s.mark((function t(e,r,n){var o,i,c,u;return s.wrap((function(t){for(;;)switch(t.prev=t.next){case 0:return o=e.split(":")[2].split("#")[0],t.next=3,_(r,o,n.enableEncryptionKeyDerivation);case 3:return i=t.sent,t.next=6,v(i,n.accept,!1);case 6:return u=g(c=t.sent),t.abrupt("return",a({id:e,verificationMethod:c},u));case 9:case"end":return t.stop()}}),t)})));return function(e,r,n){return t.apply(this,arguments)}}(),x={JsonWebKey2020:n.constants.JSON_WEB_SIGNATURE_2020_V1_URL,Ed25519VerificationKey2018:n.constants.ED25519_2018_v1_URL,X25519KeyAgreementKey2019:n.constants.X25519_2019_v1_URL,Bls12381G1Key2020:n.constants.BLS12381_2020_V1_URL,Bls12381G2Key2020:n.constants.BLS12381_2020_V1_URL,P256Key2021:n.constants.MULTIKEY_2021_V1_URL,P384Key2021:n.constants.MULTIKEY_2021_V1_URL,P521Key2021:n.constants.MULTIKEY_2021_V1_URL,EcdsaSecp256k1VerificationKey2019:n.constants.SECP256k1_2019_v1_URL},E=function(t){var e,n=[r.constants.DID_CONTEXT_V1_URL];return null==(e=t.verificationMethod)||e.forEach((function(t){if(!x[t.type])throw new Error("Cannot generate context for unregistered verification method type: "+t.type);var e=x[t.type];n.includes(e)||n.push(e)})),n},w=((c={})[r.constants.DID_CONTEXT_V1_URL]=r.contexts.get(r.constants.DID_CONTEXT_V1_URL),c[n.constants.JSON_WEB_SIGNATURE_2020_V1_URL]=n.contexts.get(n.constants.JSON_WEB_SIGNATURE_2020_V1_URL),c[n.constants.ED25519_2018_v1_URL]=n.contexts.get(n.constants.ED25519_2018_v1_URL),c[n.constants.X25519_2019_v1_URL]=n.contexts.get(n.constants.X25519_2019_v1_URL),c[n.constants.BLS12381_2020_V1_URL]=n.contexts.get(n.constants.BLS12381_2020_V1_URL),c[n.constants.MULTIKEY_2021_V1_URL]=n.contexts.get(n.constants.MULTIKEY_2021_V1_URL),c[n.constants.SECP256k1_2019_v1_URL]=n.contexts.get(n.constants.SECP256k1_2019_v1_URL),c),L=function(t){if(w[t])return{documentUrl:t,document:w[t]};throw console.error("unsupported iri"+t),new Error("unsupported iri"+t)},K=function(r){return function(){var n=i(s.mark((function n(o,i){var c,u,p;return s.wrap((function(n){for(;;)switch(n.prev=n.next){case 0:return void 0===i&&(i={accept:"application/did+ld+json",enableEncryptionKeyDerivation:!1}),n.next=3,m(o,r,i);case 3:return u=E(c=n.sent),p=t.factory.build({entries:a({"@context":u},c)}),n.next=8,p.addRepresentation({"application/did+json":e.representation}).addRepresentation({"application/did+ld+json":e.representation}).produce(i.accept,L);case 8:return n.abrupt("return",{didDocument:JSON.parse(n.sent.toString("utf-8"))});case 10:case"end":return n.stop()}}),n)})));return function(t,e){return n.apply(this,arguments)}}()},b=function(){var t=i(s.mark((function t(e,r){return s.wrap((function(t){for(;;)switch(t.prev=t.next){case 0:if(!r.enableEncryptionKeyDerivation||!e.getDerivedKeyPairs){t.next=4;break}return t.next=3,e.getDerivedKeyPairs();case 3:return t.abrupt("return",t.sent);case 4:if(!e.getPairedKeyPairs){t.next=8;break}return t.next=7,e.getPairedKeyPairs();case 7:return t.abrupt("return",t.sent);case 8:return t.abrupt("return",[e]);case 9:case"end":return t.stop()}}),t)})));return function(e,r){return t.apply(this,arguments)}}();exports.getGenerator=function(t){var e=K(t);return function(){var r=i(s.mark((function r(n,o){var i,a;return s.wrap((function(r){for(;;)switch(r.prev=r.next){case 0:return void 0===o&&(o={accept:"application/did+json",enableEncryptionKeyDerivation:!1}),r.next=3,t.generate(n);case 3:return i=r.sent,r.next=6,b(i,o);case 6:return a=r.sent,r.next=9,e(i.controller,o);case 9:return r.t0=r.sent.didDocument,r.next=15,v(a,o.accept,!0);case 15:return r.t1=r.sent,r.abrupt("return",{didDocument:r.t0,keys:r.t1});case 17:case"end":return r.stop()}}),r)})));return function(t,e){return r.apply(this,arguments)}}()},exports.getResolver=K;
//# sourceMappingURL=did-key-common.cjs.production.min.js.map