@transmute/x25519-key-pair
Version:
``` npm i @transmute/x25519-key-pair@latest --save ```
3 lines (2 loc) • 13.3 kB
JavaScript
;Object.defineProperty(exports,"__esModule",{value:!0});var r=require("tslib"),t=require("@stablelib/x25519"),e=require("@transmute/ld-key-pair");function n(r,t,e,n,o,i,a){try{var c=r[i](a),u=c.value}catch(r){return void e(r)}c.done?t(u):Promise.resolve(u).then(n,o)}function o(r){return function(){var t=this,e=arguments;return new Promise((function(o,i){var a=r.apply(t,e);function c(r){n(a,o,i,c,u,"next",r)}function u(r){n(a,o,i,c,u,"throw",r)}c(void 0)}))}}function i(){return(i=Object.assign||function(r){for(var t=1;t<arguments.length;t++){var e=arguments[t];for(var n in e)Object.prototype.hasOwnProperty.call(e,n)&&(r[n]=e[n])}return r}).apply(this,arguments)}var a,c=(function(r){var t=function(r){var t=Object.prototype,e=t.hasOwnProperty,n="function"==typeof Symbol?Symbol:{},o=n.iterator||"@@iterator",i=n.asyncIterator||"@@asyncIterator",a=n.toStringTag||"@@toStringTag";function c(r,t,e){return Object.defineProperty(r,t,{value:e,enumerable:!0,configurable:!0,writable:!0}),r[t]}try{c({},"")}catch(r){c=function(r,t,e){return r[t]=e}}function u(r,t,e,n){var o=Object.create((t&&t.prototype instanceof p?t:p).prototype),i=new E(n||[]);return o._invoke=function(r,t,e){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(e.method=o,e.arg=i;;){var a=e.delegate;if(a){var c=b(a,e);if(c){if(c===s)continue;return c}}if("next"===e.method)e.sent=e._sent=e.arg;else if("throw"===e.method){if("suspendedStart"===n)throw n="completed",e.arg;e.dispatchException(e.arg)}else"return"===e.method&&e.abrupt("return",e.arg);n="executing";var u=f(r,t,e);if("normal"===u.type){if(n=e.done?"completed":"suspendedYield",u.arg===s)continue;return{value:u.arg,done:e.done}}"throw"===u.type&&(n="completed",e.method="throw",e.arg=u.arg)}}}(r,e,i),o}function f(r,t,e){try{return{type:"normal",arg:r.call(t,e)}}catch(r){return{type:"throw",arg:r}}}r.wrap=u;var s={};function p(){}function l(){}function h(){}var y={};y[o]=function(){return this};var v=Object.getPrototypeOf,d=v&&v(v(k([])));d&&d!==t&&e.call(d,o)&&(y=d);var g=h.prototype=p.prototype=Object.create(y);function w(r){["next","throw","return"].forEach((function(t){c(r,t,(function(r){return this._invoke(t,r)}))}))}function m(r,t){var n;this._invoke=function(o,i){function a(){return new t((function(n,a){!function n(o,i,a,c){var u=f(r[o],r,i);if("throw"!==u.type){var s=u.arg,p=s.value;return p&&"object"==typeof p&&e.call(p,"__await")?t.resolve(p.__await).then((function(r){n("next",r,a,c)}),(function(r){n("throw",r,a,c)})):t.resolve(p).then((function(r){s.value=r,a(s)}),(function(r){return n("throw",r,a,c)}))}c(u.arg)}(o,i,n,a)}))}return n=n?n.then(a,a):a()}}function b(r,t){var e=r.iterator[t.method];if(void 0===e){if(t.delegate=null,"throw"===t.method){if(r.iterator.return&&(t.method="return",t.arg=void 0,b(r,t),"throw"===t.method))return s;t.method="throw",t.arg=new TypeError("The iterator does not provide a 'throw' method")}return s}var n=f(e,r.iterator,t.arg);if("throw"===n.type)return t.method="throw",t.arg=n.arg,t.delegate=null,s;var o=n.arg;return o?o.done?(t[r.resultName]=o.value,t.next=r.nextLoc,"return"!==t.method&&(t.method="next",t.arg=void 0),t.delegate=null,s):o:(t.method="throw",t.arg=new TypeError("iterator result is not an object"),t.delegate=null,s)}function K(r){var t={tryLoc:r[0]};1 in r&&(t.catchLoc=r[1]),2 in r&&(t.finallyLoc=r[2],t.afterLoc=r[3]),this.tryEntries.push(t)}function x(r){var t=r.completion||{};t.type="normal",delete t.arg,r.completion=t}function E(r){this.tryEntries=[{tryLoc:"root"}],r.forEach(K,this),this.reset(!0)}function k(r){if(r){var t=r[o];if(t)return t.call(r);if("function"==typeof r.next)return r;if(!isNaN(r.length)){var n=-1,i=function t(){for(;++n<r.length;)if(e.call(r,n))return t.value=r[n],t.done=!1,t;return t.value=void 0,t.done=!0,t};return i.next=i}}return{next:L}}function L(){return{value:void 0,done:!0}}return l.prototype=g.constructor=h,h.constructor=l,l.displayName=c(h,a,"GeneratorFunction"),r.isGeneratorFunction=function(r){var t="function"==typeof r&&r.constructor;return!!t&&(t===l||"GeneratorFunction"===(t.displayName||t.name))},r.mark=function(r){return Object.setPrototypeOf?Object.setPrototypeOf(r,h):(r.__proto__=h,c(r,a,"GeneratorFunction")),r.prototype=Object.create(g),r},r.awrap=function(r){return{__await:r}},w(m.prototype),m.prototype[i]=function(){return this},r.AsyncIterator=m,r.async=function(t,e,n,o,i){void 0===i&&(i=Promise);var a=new m(u(t,e,n,o),i);return r.isGeneratorFunction(e)?a:a.next().then((function(r){return r.done?r.value:a.next()}))},w(g),c(g,a,"Generator"),g[o]=function(){return this},g.toString=function(){return"[object Generator]"},r.keys=function(r){var t=[];for(var e in r)t.push(e);return t.reverse(),function e(){for(;t.length;){var n=t.pop();if(n in r)return e.value=n,e.done=!1,e}return e.done=!0,e}},r.values=k,E.prototype={constructor:E,reset:function(r){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(x),!r)for(var t in this)"t"===t.charAt(0)&&e.call(this,t)&&!isNaN(+t.slice(1))&&(this[t]=void 0)},stop:function(){this.done=!0;var r=this.tryEntries[0].completion;if("throw"===r.type)throw r.arg;return this.rval},dispatchException:function(r){if(this.done)throw r;var t=this;function n(e,n){return a.type="throw",a.arg=r,t.next=e,n&&(t.method="next",t.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=e.call(i,"catchLoc"),u=e.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(r,t){for(var n=this.tryEntries.length-1;n>=0;--n){var o=this.tryEntries[n];if(o.tryLoc<=this.prev&&e.call(o,"finallyLoc")&&this.prev<o.finallyLoc){var i=o;break}}i&&("break"===r||"continue"===r)&&i.tryLoc<=t&&t<=i.finallyLoc&&(i=null);var a=i?i.completion:{};return a.type=r,a.arg=t,i?(this.method="next",this.next=i.finallyLoc,s):this.complete(a)},complete:function(r,t){if("throw"===r.type)throw r.arg;return"break"===r.type||"continue"===r.type?this.next=r.arg:"return"===r.type?(this.rval=this.arg=r.arg,this.method="return",this.next="end"):"normal"===r.type&&t&&(this.next=t),s},finish:function(r){for(var t=this.tryEntries.length-1;t>=0;--t){var e=this.tryEntries[t];if(e.finallyLoc===r)return this.complete(e.completion,e.afterLoc),x(e),s}},catch:function(r){for(var t=this.tryEntries.length-1;t>=0;--t){var e=this.tryEntries[t];if(e.tryLoc===r){var n=e.completion;if("throw"===n.type){var o=n.arg;x(e)}return o}}throw new Error("illegal catch attempt")},delegateYield:function(r,t,e){return this.delegate={iterator:k(r),resultName:t,nextLoc:e},"next"===this.method&&(this.arg=void 0),s}},r}(r.exports);try{regeneratorRuntime=t}catch(r){Function("r","regeneratorRuntime = r")(t)}}(a={exports:{}}),a.exports);function u(r){if(r.length>=255)throw new TypeError("Alphabet too long");for(var t=new Uint8Array(256),e=0;e<t.length;e++)t[e]=255;for(var n=0;n<r.length;n++){var o=r.charAt(n),i=o.charCodeAt(0);if(255!==t[i])throw new TypeError(o+" is ambiguous");t[i]=n}var a=r.length,c=r.charAt(0),u=Math.log(a)/Math.log(256),f=Math.log(256)/Math.log(a);function s(r){if("string"!=typeof r)throw new TypeError("Expected String");if(0===r.length)return Buffer.alloc(0);var e=0;if(" "!==r[e]){for(var n=0,o=0;r[e]===c;)n++,e++;for(var i=(r.length-e)*u+1>>>0,f=new Uint8Array(i);r[e];){var s=t[r.charCodeAt(e)];if(255===s)return;for(var p=0,l=i-1;(0!==s||p<o)&&-1!==l;l--,p++)f[l]=(s+=a*f[l]>>>0)%256>>>0,s=s/256>>>0;if(0!==s)throw new Error("Non-zero carry");o=p,e++}if(" "!==r[e]){for(var h=i-o;h!==i&&0===f[h];)h++;var y=Buffer.allocUnsafe(n+(i-h));y.fill(0,0,n);for(var v=n;h!==i;)y[v++]=f[h++];return y}}}return{encode:function(t){if((Array.isArray(t)||t instanceof Uint8Array)&&(t=Buffer.from(t)),!Buffer.isBuffer(t))throw new TypeError("Expected Buffer");if(0===t.length)return"";for(var e=0,n=0,o=0,i=t.length;o!==i&&0===t[o];)o++,e++;for(var u=(i-o)*f+1>>>0,s=new Uint8Array(u);o!==i;){for(var p=t[o],l=0,h=u-1;(0!==p||l<n)&&-1!==h;h--,l++)s[h]=(p+=256*s[h]>>>0)%a>>>0,p=p/a>>>0;if(0!==p)throw new Error("Non-zero carry");n=l,o++}for(var y=u-n;y!==u&&0===s[y];)y++;for(var v=c.repeat(e);y<u;++y)v+=r.charAt(s[y]);return v},decodeUnsafe:s,decode:function(r){var t=s(r);if(t)return t;throw new Error("Non-base"+a+" character")}}}var f,s=u("123456789ABCDEFGHJKLMNPQRSTUVWXYZabcdefghijkmnopqrstuvwxyz"),p=function(r){return function(r){return Buffer.from(r||"").toString("base64")}(r).replace(/\+/g,"-").replace(/\//g,"_").replace(/=+$/g,"")},l=function(r,t){void 0===t&&(t="base58btc");var e=new Uint8Array(2+r.length);if(e[0]=236,e[1]=1,e.set(r,2),"base58btc"===t)return"z"+s.encode(e);if("base64url"===t)return"u"+p(e);throw new Error("Unsupported encoding: "+t)},h=function(r){return Uint8Array.from(Buffer.from(r.x,"base64"))},y={JsonWebKey2020:function(r){var t,e=h(r.publicKeyJwk),n=void 0;return r.privateKeyJwk&&(h(t=r.privateKeyJwk),n=Uint8Array.from(Buffer.from(t.d,"base64"))),{publicKey:e,privateKey:n}},X25519KeyAgreementKey2019:function(r){var t=s.decode(r.publicKeyBase58),e=void 0;return r.privateKeyBase58&&(e=Uint8Array.from(s.decode(r.privateKeyBase58))),{publicKey:t,privateKey:e}}},v={JsonWebKey2020:function(r,t,e,n){var o={kty:"OKP",crv:"X25519",x:p(e)},a={id:r,type:"JsonWebKey2020",controller:t,publicKeyJwk:o};return n&&(a.privateKeyJwk=i({},o,{d:p(n)})),a},X25519KeyAgreementKey2019:function(r,t,e,n){var o={id:r,type:"X25519KeyAgreementKey2019",controller:t,publicKeyBase58:s.encode(e)};return n&&(o.privateKeyBase58=s.encode(n)),o}};exports.X25519KeyPair=f=function(){function r(r){this.type="JsonWebKey2020",this.id=r.id,this.type=r.type||"JsonWebKey2020",this.controller=r.controller,this.publicKey=r.publicKey,this.privateKey=r.privateKey}r.fingerprintFromPublicKey=function(){var r=o(c.mark((function r(t){return c.wrap((function(r){for(;;)switch(r.prev=r.next){case 0:return r.next=2,f.from(t);case 2:return r.abrupt("return",l(r.sent.publicKey));case 5:case"end":return r.stop()}}),r)})));return function(t){return r.apply(this,arguments)}}(),r.fromFingerprint=function(){var r=o(c.mark((function r(t){var e,n,o,i;return c.wrap((function(r){for(;;)switch(r.prev=r.next){case 0:if(236!==(n=s.decode((e=t.fingerprint).substring(1)))[0]||1!==n[1]){r.next=12;break}return r.next=5,f.from({id:"",controller:"",type:"X25519KeyAgreementKey2019",publicKeyBase58:s.encode(n.slice(2))});case 5:return o=r.sent,r.next=8,o.fingerprint();case 8:return o.id="did:key:"+(i=r.sent)+"#"+i,o.controller="did:key:"+i,r.abrupt("return",o);case 12:throw new Error("Unsupported fingerprint type: "+e);case 13:case"end":return r.stop()}}),r)})));return function(t){return r.apply(this,arguments)}}();var e=r.prototype;return e.fingerprint=function(){var r=o(c.mark((function r(){return c.wrap((function(r){for(;;)switch(r.prev=r.next){case 0:return r.abrupt("return",l(this.publicKey));case 1:case"end":return r.stop()}}),r,this)})));return function(){return r.apply(this,arguments)}}(),e.export=function(){var r=o(c.mark((function r(t){return c.wrap((function(r){for(;;)switch(r.prev=r.next){case 0:if(void 0===t&&(t={privateKey:!1,type:"JsonWebKey2020"}),!v[t.type]){r.next=3;break}return r.abrupt("return",v[t.type](this.id,this.controller,this.publicKey,t.privateKey?this.privateKey:void 0));case 3:throw new Error("Unsupported export options: "+JSON.stringify(t));case 4:case"end":return r.stop()}}),r,this)})));return function(t){return r.apply(this,arguments)}}(),e.deriveSecret=function(){var r=o(c.mark((function r(e){var n,o,i;return c.wrap((function(r){for(;;)switch(r.prev=r.next){case 0:return n=e.publicKey,r.next=3,f.from(n);case 3:if(o=r.sent,this.privateKey){r.next=6;break}throw new Error("No private key available for deriveSecret");case 6:return i=t.sharedKey(this.privateKey,o.publicKey,!0),r.abrupt("return",i);case 8:case"end":return r.stop()}}),r,this)})));return function(t){return r.apply(this,arguments)}}(),r}(),exports.X25519KeyPair.generate=function(){var r=o(c.mark((function r(e){var n,o,i;return c.wrap((function(r){for(;;)switch(r.prev=r.next){case 0:return n=t.generateKeyPair({isAvailable:!0,randomBytes:e.secureRandom}),o=l(n.publicKey),r.abrupt("return",new f({id:(i="did:key:"+o)+"#"+o,type:"JsonWebKey2020",controller:i,publicKey:n.publicKey,privateKey:n.secretKey}));case 6:case"end":return r.stop()}}),r)})));return function(t){return r.apply(this,arguments)}}(),exports.X25519KeyPair.from=function(){var r=o(c.mark((function r(t){var e;return c.wrap((function(r){for(;;)switch(r.prev=r.next){case 0:return e=y[t.type](t),r.abrupt("return",new f({id:t.id,type:t.type,controller:t.controller,publicKey:e.publicKey,privateKey:e.privateKey}));case 2:case"end":return r.stop()}}),r)})));return function(t){return r.apply(this,arguments)}}(),exports.X25519KeyPair=f=r.__decorate([e.staticImplements()],exports.X25519KeyPair);
//# sourceMappingURL=x25519-key-pair.cjs.production.min.js.map