Huge News!Announcing our $40M Series B led by Abstract Ventures.Learn More
Socket
Sign inDemoInstall
Socket

@transmute/did-key-secp256k1

Package Overview
Dependencies
Maintainers
3
Versions
58
Alerts
File Explorer

Advanced tools

Socket logo

Install Socket

Detect and block malicious and high-risk dependencies

Install

@transmute/did-key-secp256k1 - npm Package Compare versions

Comparing version 0.2.1-unstable.10 to 0.2.1-unstable.11

src/__tests__/jose.santity.detached.test.ts

8

CHANGELOG.md

@@ -6,2 +6,10 @@ # Change Log

## 0.2.1-unstable.11 (2020-08-13)
**Note:** Version bump only for package @transmute/did-key-secp256k1
## 0.2.1-unstable.10 (2020-08-04)

@@ -8,0 +16,0 @@

18

dist/did-key-secp256k1.cjs.development.js

@@ -395,3 +395,4 @@ 'use strict';

var signatureUInt8Array = base64url.toBuffer(encodedSignature);
var verified = secp256k1.ecdsaVerify(signatureUInt8Array, messageHashUInt8Array, publicKeyUInt8Array);
var signatureLowerS = secp256k1.signatureNormalize(signatureUInt8Array);
var verified = secp256k1.ecdsaVerify(signatureLowerS, messageHashUInt8Array, publicKeyUInt8Array);

@@ -402,3 +403,6 @@ if (verified) {

throw new Error('Cannot verify detached signature.');
var erroObject = {
signature: signatureUInt8Array.toString('hex')
};
throw new JWSVerificationFailed('ECDSA Verify Failed: ' + JSON.stringify(erroObject, null, 2));
});

@@ -449,3 +453,4 @@ } catch (e) {

var signatureUInt8Array = base64url.toBuffer(encodedSignature);
var verified = secp256k1.ecdsaVerify(signatureUInt8Array, messageHashUInt8Array, publicKeyUInt8Array);
var signatureLowerS = secp256k1.signatureNormalize(signatureUInt8Array);
var verified = secp256k1.ecdsaVerify(signatureLowerS, messageHashUInt8Array, publicKeyUInt8Array);

@@ -456,3 +461,8 @@ if (verified) {

throw new JWSVerificationFailed('signature verification failed');
var erroObject = {
signature: signatureUInt8Array.toString('hex'),
message: messageHashUInt8Array.toString('hex'),
publicKey: publicKeyUInt8Array.toString('hex')
};
throw new JWSVerificationFailed('ECDSA Verify Failed: ' + JSON.stringify(erroObject, null, 2));
});

@@ -459,0 +469,0 @@ } catch (e) {

@@ -1,2 +0,2 @@

"use strict";function e(e){return e&&"object"==typeof e&&"default"in e?e.default:e}Object.defineProperty(exports,"__esModule",{value:!0});var r=e(require("@trust/keyto")),t=e(require("base64url")),n=e(require("crypto")),i=e(require("bs58")),o=e(require("secp256k1")),c=e(require("canonicalize"));function u(){return(u=Object.assign||function(e){for(var r=1;r<arguments.length;r++){var t=arguments[r];for(var n in t)Object.prototype.hasOwnProperty.call(t,n)&&(e[n]=t[n])}return e}).apply(this,arguments)}function a(e){return(a=Object.setPrototypeOf?Object.getPrototypeOf:function(e){return e.__proto__||Object.getPrototypeOf(e)})(e)}function f(e,r){return(f=Object.setPrototypeOf||function(e,r){return e.__proto__=r,e})(e,r)}function s(){if("undefined"==typeof Reflect||!Reflect.construct)return!1;if(Reflect.construct.sham)return!1;if("function"==typeof Proxy)return!0;try{return Date.prototype.toString.call(Reflect.construct(Date,[],(function(){}))),!0}catch(e){return!1}}function l(e,r,t){return(l=s()?Reflect.construct:function(e,r,t){var n=[null];n.push.apply(n,r);var i=new(Function.bind.apply(e,n));return t&&f(i,t.prototype),i}).apply(null,arguments)}function p(e){var r="function"==typeof Map?new Map:void 0;return(p=function(e){if(null===e||-1===Function.toString.call(e).indexOf("[native code]"))return e;if("function"!=typeof e)throw new TypeError("Super expression must either be null or a function");if(void 0!==r){if(r.has(e))return r.get(e);r.set(e,t)}function t(){return l(e,arguments,a(this).constructor)}return t.prototype=Object.create(e.prototype,{constructor:{value:t,enumerable:!1,writable:!0,configurable:!0}}),f(t,e)})(e)}var y=function(e){var r=u({},e);delete r.d,delete r.kid,delete r.alg;var i=n.createHash("sha256").update(c(r)).digest();return t.encode(Buffer.from(i))},v=function(e){try{var t=u({},r.from(e,"blk").toJwk("private"),{crv:"secp256k1"}),n=y(t);return Promise.resolve(u({},t,{kid:n}))}catch(e){return Promise.reject(e)}},d=function(e){try{var t=e;if(66===e.length){var n=o.publicKeyConvert(Buffer.from(e,"hex"),!1);t=Buffer.from(n).toString("hex")}var i=u({},r.from(t,"blk").toJwk("public"),{crv:"secp256k1"}),c=y(i);return Promise.resolve(u({},i,{kid:c}))}catch(e){return Promise.reject(e)}},h=function(e){try{return Promise.resolve(r.from(u({},e,{crv:"K-256"}),"jwk").toString("blk","private"))}catch(e){return Promise.reject(e)}},m=function(e){try{var t=r.from(u({},e,{crv:"K-256"}),"jwk").toString("blk","public"),n=o.publicKeyConvert(Buffer.from(t,"hex"),!0);return Promise.resolve(Buffer.from(n).toString("hex"))}catch(e){return Promise.reject(e)}},b=function(e){try{return Promise.resolve(h(e)).then((function(e){return Buffer.from(e,"hex")}))}catch(e){return Promise.reject(e)}},K=function(e){try{return Promise.resolve(m(e)).then((function(e){return Buffer.from(e,"hex")}))}catch(e){return Promise.reject(e)}},g=function(e){return i.encode(Buffer.from(e,"hex"))},P=function(e){return i.encode(Buffer.from(e,"hex"))},w=function(e){try{return Promise.resolve(i.decode(e))}catch(e){return Promise.reject(e)}},B=function(e){try{return Promise.resolve(i.decode(e))}catch(e){return Promise.reject(e)}},k={__proto__:null,getKid:y,privateKeyJwkFromPrivateKeyHex:v,publicKeyJwkFromPublicKeyHex:d,privateKeyJwkFromPrivateKeyPem:function(e){var t=u({},r.from(e,"pem").toJwk("private"),{crv:"secp256k1"});return u({},t,{kid:y(t)})},publicKeyJwkFromPublicKeyPem:function(e){var t=u({},r.from(e,"pem").toJwk("public"),{crv:"secp256k1"});return u({},t,{kid:y(t)})},privateKeyHexFromJwk:h,publicKeyHexFromJwk:m,privateKeyUInt8ArrayFromJwk:b,publicKeyUInt8ArrayFromJwk:K,publicKeyBase58FromPublicKeyHex:g,privateKeyBase58FromPrivateKeyHex:P,privateKeyUInt8ArrayFromPrivateKeyBase58:w,publicKeyUInt8ArrayFromPublicKeyBase58:B,publicKeyHexFromPrivateKeyHex:function(e){try{var r=o.publicKeyCreate(new Uint8Array(Buffer.from(e,"hex")));return Promise.resolve(Buffer.from(r).toString("hex"))}catch(e){return Promise.reject(e)}}},x=function(e){var r,t;function n(r){var t;return(t=e.call(this,r)||this).name="JWSVerificationFailed",t}return t=e,(r=n).prototype=Object.create(t.prototype),r.prototype.constructor=r,r.__proto__=t,n}(p(Error)),j={__proto__:null,signDetached:function(e,r,i){void 0===i&&(i={alg:"ES256K",b64:!1,crit:["b64"]});try{return Promise.resolve(b(r)).then((function(r){var c=t.encode(JSON.stringify(i)),u=Buffer.concat([Buffer.from(c+".","utf8"),Buffer.from(e.buffer,e.byteOffset,e.length)]),a=Buffer.from(u),f=n.createHash("sha256").update(a).digest(),s=o.ecdsaSign(f,r);return c+".."+t.encode(Buffer.from(s.signature))}))}catch(e){return Promise.reject(e)}},verifyDetached:function(e,r,i){try{if(-1===e.indexOf(".."))throw new x("not a valid rfc7797 jws.");var c=e.split(".."),u=c[0],a=c[1],f=JSON.parse(t.decode(u));if("ES256K"!==f.alg)throw new Error("JWS alg is not signed with ES256K.");if(!1!==f.b64||!f.crit||!f.crit.length||"b64"!==f.crit[0])throw new Error("JWS Header is not in rfc7797 format (not detached).");return Promise.resolve(K(i)).then((function(e){var i=Buffer.concat([Buffer.from(u+".","utf8"),Buffer.from(r.buffer,r.byteOffset,r.length)]),c=Buffer.from(i),f=n.createHash("sha256").update(c).digest(),s=t.toBuffer(a);if(o.ecdsaVerify(s,f,e))return!0;throw new Error("Cannot verify detached signature.")}))}catch(e){return Promise.reject(e)}},sign:function(e,r,i){void 0===i&&(i={alg:"ES256K"});try{return Promise.resolve(b(r)).then((function(r){var c=t.encode(JSON.stringify(i)),u=t.encode(JSON.stringify(e)),a=Buffer.from(c+"."+u),f=n.createHash("sha256").update(a).digest(),s=o.ecdsaSign(f,r);return c+"."+u+"."+t.encode(s.signature)}))}catch(e){return Promise.reject(e)}},verify:function(e,r){try{return Promise.resolve(K(r)).then((function(r){var i=e.split("."),c=i[1],u=i[2],a=Buffer.from(i[0]+"."+c),f=n.createHash("sha256").update(a).digest(),s=t.toBuffer(u);if(o.ecdsaVerify(s,f,r))return JSON.parse(t.decode(c));throw new x("signature verification failed")}))}catch(e){return Promise.reject(e)}},decode:function(e,r){void 0===r&&(r={complete:!1});var n=e.split("."),i=n[1],o=n[2];return r.complete?{header:JSON.parse(t.decode(n[0])),payload:JSON.parse(t.decode(i)),signature:o}:JSON.parse(t.decode(i))}},S=function(e){var r;do{r=e()}while(!o.privateKeyVerify(r));return{publicKey:o.publicKeyCreate(r),privateKey:r}},J=function(){function e(e){void 0===e&&(e={}),this.type="EcdsaSecp256k1VerificationKey2019",this.id=e.id,this.controller=e.controller,this.privateKeyBase58=e.privateKeyBase58,this.publicKeyBase58=e.publicKeyBase58}e.fingerprintFromPublicKey=function(e){var r=i.decode(e.publicKeyBase58),t=new Uint8Array(2+r.length);return t[0]=231,t[1]=1,t.set(r,2),"z"+i.encode(t)},e.generate=function(r){void 0===r&&(r={});try{var t,n;if(r.secureRandom){var i=S(r.secureRandom);t=i.privateKey,n=i.publicKey}if(r.seed){var o=S((function(){return new Uint8Array(r.seed)}));t=o.privateKey,n=o.publicKey}if(!t)throw new Error("Cannot generate private key.");var c=g(Buffer.from(n).toString("hex")),u=P(Buffer.from(t).toString("hex")),a="did:key:"+e.fingerprintFromPublicKey({publicKeyBase58:c}),f="#"+e.fingerprintFromPublicKey({publicKeyBase58:c});return Promise.resolve(new e({id:f,controller:a,publicKeyBase58:c,privateKeyBase58:u}))}catch(e){return Promise.reject(e)}},e.from=function(r){try{var t=function(){function t(){function t(){function t(){return new e(u({},r,{privateKeyBase58:n,publicKeyBase58:i}))}var o=function(){if(r.publicKeyJwk){var e=P;return Promise.resolve(m(r.publicKeyJwk)).then((function(r){i=e.call(k,r)}))}}();return o&&o.then?o.then(t):t()}var o=function(){if(r.privateKeyJwk){var e=P;return Promise.resolve(h(r.privateKeyJwk)).then((function(r){n=e.call(k,r)}))}}();return o&&o.then?o.then(t):t()}var o=function(){if(r.publicKeyHex)return Promise.resolve(g(r.publicKeyHex)).then((function(e){i=e}))}();return o&&o.then?o.then(t):t()},n=r.privateKeyBase58,i=r.publicKeyBase58,o=function(){if(r.privateKeyHex)return Promise.resolve(P(r.privateKeyHex)).then((function(e){n=e}))}();return Promise.resolve(o&&o.then?o.then(t):t())}catch(e){return Promise.reject(e)}},e.fromFingerprint=function(r){var t=r.fingerprint,n=i.decode(t.substr(1));if(231===n[0]&&1===n[1]){var o=i.encode(n.slice(2)),c="did:key:"+e.fingerprintFromPublicKey({publicKeyBase58:o}),u="#"+e.fingerprintFromPublicKey({publicKeyBase58:o});return new e({id:u,controller:c,publicKeyBase58:o})}throw new Error("Unsupported Fingerprint Type: "+t)};var r,t=e.prototype;return t.signer=function(){if(!this.privateKeyBase58)return{sign:function(){try{throw new Error("No private key to sign with.")}catch(e){return Promise.reject(e)}}};var e=this.privateKeyBase58;return{sign:function(r){var t=r.data;try{var i=n.createHash("sha256").update(t).digest();return Promise.resolve(w(e)).then((function(e){return o.ecdsaSign(i,e).signature}))}catch(e){return Promise.reject(e)}}}},t.verifier=function(){if(!this.publicKeyBase58)return{sign:function(){try{throw new Error("No public key to verify with.")}catch(e){return Promise.reject(e)}}};var e=this.publicKeyBase58;return{verify:function(r){var t=r.data,i=r.signature;try{var c=n.createHash("sha256").update(t).digest();return Promise.resolve(B(e)).then((function(e){var r=!1;try{r=o.ecdsaVerify(i,c,e)}catch(e){console.error("An error occurred when verifying signature: ",e)}return r}))}catch(e){return Promise.reject(e)}}}},t.addEncodedPublicKey=function(e){return e.publicKeyBase58=this.publicKeyBase58,e},t.fingerprint=function(){return e.fingerprintFromPublicKey({publicKeyBase58:this.publicKeyBase58})},t.verifyFingerprint=function(e){if("string"!=typeof e||"z"!==e[0])return{error:new Error("`fingerprint` must be a multibase encoded string."),valid:!1};var r,t;try{r=i.decode(e.slice(1))}catch(e){return{error:e,valid:!1}}try{t=i.decode(this.publicKeyBase58)}catch(e){return{error:e,valid:!1}}var n="e701"===r.slice(0,2).toString("hex")&&t.equals(r.slice(2));return n?{valid:n}:{error:new Error("The fingerprint does not match the public key."),valid:!1}},t.publicNode=function(e){var r=(void 0===e?{}:e).controller,t=void 0===r?this.controller:r,n={id:this.id,type:this.type};return t&&(n.controller=t),this.addEncodedPublicKey(n),n},t.toJwk=function(e){void 0===e&&(e=!1);try{return Promise.resolve(e?v(i.decode(this.privateKeyBase58).toString("hex")):d(i.decode(this.publicKeyBase58).toString("hex")))}catch(e){return Promise.reject(e)}},t.toHex=function(e){void 0===e&&(e=!1);try{var r=function(e){if(t)return e;var r=m;return Promise.resolve(d(i.decode(n.publicKeyBase58).toString("hex"))).then((function(e){return r.call(k,e)}))},t=!1,n=this,o=function(){if(e){t=!0;var r=h;return Promise.resolve(v(i.decode(n.privateKeyBase58).toString("hex"))).then((function(e){return r.call(k,e)}))}}();return Promise.resolve(o&&o.then?o.then(r):r(o))}catch(e){return Promise.reject(e)}},(r=[{key:"publicKey",get:function(){return this.publicKeyBase58}},{key:"privateKey",get:function(){return this.privateKeyBase58}}])&&function(e,r){for(var t=0;t<r.length;t++){var n=r[t];n.enumerable=n.enumerable||!1,n.configurable=!0,"value"in n&&(n.writable=!0),Object.defineProperty(e,n.key,n)}}(e.prototype,r),e}(),O=function(e){var r="did:key:"+e.fingerprint(),t="#"+e.fingerprint();return{"@context":["https://www.w3.org/ns/did/v1",{"@base":r}],id:r,publicKey:[{id:t,type:e.type,controller:r,publicKeyBase58:e.publicKeyBase58}],authentication:[t],assertionMethod:[t],capabilityDelegation:[t],capabilityInvocation:[t]}},F={__proto__:null,computeKeyId:function(e){var r=e.key;try{return Promise.resolve("did:key:"+r.fingerprint()+"#"+r.fingerprint())}catch(e){return Promise.reject(e)}},keyToDidDoc:O,get:function(e){var r=void 0===e?{}:e,t=r.did,n=r.url;try{if(!(t=t||n))throw new TypeError('"did" must be a string.');var i=t.split("#")[0].split("did:key:").pop();return Promise.resolve(J.fromFingerprint({fingerprint:i})).then((function(e){return O(e)}))}catch(e){return Promise.reject(e)}}};exports.ES256K=j,exports.Secp256k1KeyPair=J,exports.driver=F,exports.keyUtils=k;
"use strict";function e(e){return e&&"object"==typeof e&&"default"in e?e.default:e}Object.defineProperty(exports,"__esModule",{value:!0});var r=e(require("@trust/keyto")),t=e(require("base64url")),n=e(require("crypto")),i=e(require("bs58")),o=e(require("secp256k1")),c=e(require("canonicalize"));function u(){return(u=Object.assign||function(e){for(var r=1;r<arguments.length;r++){var t=arguments[r];for(var n in t)Object.prototype.hasOwnProperty.call(t,n)&&(e[n]=t[n])}return e}).apply(this,arguments)}function a(e){return(a=Object.setPrototypeOf?Object.getPrototypeOf:function(e){return e.__proto__||Object.getPrototypeOf(e)})(e)}function f(e,r){return(f=Object.setPrototypeOf||function(e,r){return e.__proto__=r,e})(e,r)}function s(){if("undefined"==typeof Reflect||!Reflect.construct)return!1;if(Reflect.construct.sham)return!1;if("function"==typeof Proxy)return!0;try{return Date.prototype.toString.call(Reflect.construct(Date,[],(function(){}))),!0}catch(e){return!1}}function l(e,r,t){return(l=s()?Reflect.construct:function(e,r,t){var n=[null];n.push.apply(n,r);var i=new(Function.bind.apply(e,n));return t&&f(i,t.prototype),i}).apply(null,arguments)}function p(e){var r="function"==typeof Map?new Map:void 0;return(p=function(e){if(null===e||-1===Function.toString.call(e).indexOf("[native code]"))return e;if("function"!=typeof e)throw new TypeError("Super expression must either be null or a function");if(void 0!==r){if(r.has(e))return r.get(e);r.set(e,t)}function t(){return l(e,arguments,a(this).constructor)}return t.prototype=Object.create(e.prototype,{constructor:{value:t,enumerable:!1,writable:!0,configurable:!0}}),f(t,e)})(e)}var y=function(e){var r=u({},e);delete r.d,delete r.kid,delete r.alg;var i=n.createHash("sha256").update(c(r)).digest();return t.encode(Buffer.from(i))},v=function(e){try{var t=u({},r.from(e,"blk").toJwk("private"),{crv:"secp256k1"}),n=y(t);return Promise.resolve(u({},t,{kid:n}))}catch(e){return Promise.reject(e)}},d=function(e){try{var t=e;if(66===e.length){var n=o.publicKeyConvert(Buffer.from(e,"hex"),!1);t=Buffer.from(n).toString("hex")}var i=u({},r.from(t,"blk").toJwk("public"),{crv:"secp256k1"}),c=y(i);return Promise.resolve(u({},i,{kid:c}))}catch(e){return Promise.reject(e)}},h=function(e){try{return Promise.resolve(r.from(u({},e,{crv:"K-256"}),"jwk").toString("blk","private"))}catch(e){return Promise.reject(e)}},m=function(e){try{var t=r.from(u({},e,{crv:"K-256"}),"jwk").toString("blk","public"),n=o.publicKeyConvert(Buffer.from(t,"hex"),!0);return Promise.resolve(Buffer.from(n).toString("hex"))}catch(e){return Promise.reject(e)}},b=function(e){try{return Promise.resolve(h(e)).then((function(e){return Buffer.from(e,"hex")}))}catch(e){return Promise.reject(e)}},g=function(e){try{return Promise.resolve(m(e)).then((function(e){return Buffer.from(e,"hex")}))}catch(e){return Promise.reject(e)}},K=function(e){return i.encode(Buffer.from(e,"hex"))},P=function(e){return i.encode(Buffer.from(e,"hex"))},w=function(e){try{return Promise.resolve(i.decode(e))}catch(e){return Promise.reject(e)}},B=function(e){try{return Promise.resolve(i.decode(e))}catch(e){return Promise.reject(e)}},k={__proto__:null,getKid:y,privateKeyJwkFromPrivateKeyHex:v,publicKeyJwkFromPublicKeyHex:d,privateKeyJwkFromPrivateKeyPem:function(e){var t=u({},r.from(e,"pem").toJwk("private"),{crv:"secp256k1"});return u({},t,{kid:y(t)})},publicKeyJwkFromPublicKeyPem:function(e){var t=u({},r.from(e,"pem").toJwk("public"),{crv:"secp256k1"});return u({},t,{kid:y(t)})},privateKeyHexFromJwk:h,publicKeyHexFromJwk:m,privateKeyUInt8ArrayFromJwk:b,publicKeyUInt8ArrayFromJwk:g,publicKeyBase58FromPublicKeyHex:K,privateKeyBase58FromPrivateKeyHex:P,privateKeyUInt8ArrayFromPrivateKeyBase58:w,publicKeyUInt8ArrayFromPublicKeyBase58:B,publicKeyHexFromPrivateKeyHex:function(e){try{var r=o.publicKeyCreate(new Uint8Array(Buffer.from(e,"hex")));return Promise.resolve(Buffer.from(r).toString("hex"))}catch(e){return Promise.reject(e)}}},x=function(e){var r,t;function n(r){var t;return(t=e.call(this,r)||this).name="JWSVerificationFailed",t}return t=e,(r=n).prototype=Object.create(t.prototype),r.prototype.constructor=r,r.__proto__=t,n}(p(Error)),S={__proto__:null,signDetached:function(e,r,i){void 0===i&&(i={alg:"ES256K",b64:!1,crit:["b64"]});try{return Promise.resolve(b(r)).then((function(r){var c=t.encode(JSON.stringify(i)),u=Buffer.concat([Buffer.from(c+".","utf8"),Buffer.from(e.buffer,e.byteOffset,e.length)]),a=Buffer.from(u),f=n.createHash("sha256").update(a).digest(),s=o.ecdsaSign(f,r);return c+".."+t.encode(Buffer.from(s.signature))}))}catch(e){return Promise.reject(e)}},verifyDetached:function(e,r,i){try{if(-1===e.indexOf(".."))throw new x("not a valid rfc7797 jws.");var c=e.split(".."),u=c[0],a=c[1],f=JSON.parse(t.decode(u));if("ES256K"!==f.alg)throw new Error("JWS alg is not signed with ES256K.");if(!1!==f.b64||!f.crit||!f.crit.length||"b64"!==f.crit[0])throw new Error("JWS Header is not in rfc7797 format (not detached).");return Promise.resolve(g(i)).then((function(e){var i=Buffer.concat([Buffer.from(u+".","utf8"),Buffer.from(r.buffer,r.byteOffset,r.length)]),c=Buffer.from(i),f=n.createHash("sha256").update(c).digest(),s=t.toBuffer(a),l=o.signatureNormalize(s);if(o.ecdsaVerify(l,f,e))return!0;var p={signature:s.toString("hex")};throw new x("ECDSA Verify Failed: "+JSON.stringify(p,null,2))}))}catch(e){return Promise.reject(e)}},sign:function(e,r,i){void 0===i&&(i={alg:"ES256K"});try{return Promise.resolve(b(r)).then((function(r){var c=t.encode(JSON.stringify(i)),u=t.encode(JSON.stringify(e)),a=Buffer.from(c+"."+u),f=n.createHash("sha256").update(a).digest(),s=o.ecdsaSign(f,r);return c+"."+u+"."+t.encode(s.signature)}))}catch(e){return Promise.reject(e)}},verify:function(e,r){try{return Promise.resolve(g(r)).then((function(r){var i=e.split("."),c=i[1],u=i[2],a=Buffer.from(i[0]+"."+c),f=n.createHash("sha256").update(a).digest(),s=t.toBuffer(u),l=o.signatureNormalize(s);if(o.ecdsaVerify(l,f,r))return JSON.parse(t.decode(c));var p={signature:s.toString("hex"),message:f.toString("hex"),publicKey:r.toString("hex")};throw new x("ECDSA Verify Failed: "+JSON.stringify(p,null,2))}))}catch(e){return Promise.reject(e)}},decode:function(e,r){void 0===r&&(r={complete:!1});var n=e.split("."),i=n[1],o=n[2];return r.complete?{header:JSON.parse(t.decode(n[0])),payload:JSON.parse(t.decode(i)),signature:o}:JSON.parse(t.decode(i))}},j=function(e){var r;do{r=e()}while(!o.privateKeyVerify(r));return{publicKey:o.publicKeyCreate(r),privateKey:r}},J=function(){function e(e){void 0===e&&(e={}),this.type="EcdsaSecp256k1VerificationKey2019",this.id=e.id,this.controller=e.controller,this.privateKeyBase58=e.privateKeyBase58,this.publicKeyBase58=e.publicKeyBase58}e.fingerprintFromPublicKey=function(e){var r=i.decode(e.publicKeyBase58),t=new Uint8Array(2+r.length);return t[0]=231,t[1]=1,t.set(r,2),"z"+i.encode(t)},e.generate=function(r){void 0===r&&(r={});try{var t,n;if(r.secureRandom){var i=j(r.secureRandom);t=i.privateKey,n=i.publicKey}if(r.seed){var o=j((function(){return new Uint8Array(r.seed)}));t=o.privateKey,n=o.publicKey}if(!t)throw new Error("Cannot generate private key.");var c=K(Buffer.from(n).toString("hex")),u=P(Buffer.from(t).toString("hex")),a="did:key:"+e.fingerprintFromPublicKey({publicKeyBase58:c}),f="#"+e.fingerprintFromPublicKey({publicKeyBase58:c});return Promise.resolve(new e({id:f,controller:a,publicKeyBase58:c,privateKeyBase58:u}))}catch(e){return Promise.reject(e)}},e.from=function(r){try{var t=function(){function t(){function t(){function t(){return new e(u({},r,{privateKeyBase58:n,publicKeyBase58:i}))}var o=function(){if(r.publicKeyJwk){var e=P;return Promise.resolve(m(r.publicKeyJwk)).then((function(r){i=e.call(k,r)}))}}();return o&&o.then?o.then(t):t()}var o=function(){if(r.privateKeyJwk){var e=P;return Promise.resolve(h(r.privateKeyJwk)).then((function(r){n=e.call(k,r)}))}}();return o&&o.then?o.then(t):t()}var o=function(){if(r.publicKeyHex)return Promise.resolve(K(r.publicKeyHex)).then((function(e){i=e}))}();return o&&o.then?o.then(t):t()},n=r.privateKeyBase58,i=r.publicKeyBase58,o=function(){if(r.privateKeyHex)return Promise.resolve(P(r.privateKeyHex)).then((function(e){n=e}))}();return Promise.resolve(o&&o.then?o.then(t):t())}catch(e){return Promise.reject(e)}},e.fromFingerprint=function(r){var t=r.fingerprint,n=i.decode(t.substr(1));if(231===n[0]&&1===n[1]){var o=i.encode(n.slice(2)),c="did:key:"+e.fingerprintFromPublicKey({publicKeyBase58:o}),u="#"+e.fingerprintFromPublicKey({publicKeyBase58:o});return new e({id:u,controller:c,publicKeyBase58:o})}throw new Error("Unsupported Fingerprint Type: "+t)};var r,t=e.prototype;return t.signer=function(){if(!this.privateKeyBase58)return{sign:function(){try{throw new Error("No private key to sign with.")}catch(e){return Promise.reject(e)}}};var e=this.privateKeyBase58;return{sign:function(r){var t=r.data;try{var i=n.createHash("sha256").update(t).digest();return Promise.resolve(w(e)).then((function(e){return o.ecdsaSign(i,e).signature}))}catch(e){return Promise.reject(e)}}}},t.verifier=function(){if(!this.publicKeyBase58)return{sign:function(){try{throw new Error("No public key to verify with.")}catch(e){return Promise.reject(e)}}};var e=this.publicKeyBase58;return{verify:function(r){var t=r.data,i=r.signature;try{var c=n.createHash("sha256").update(t).digest();return Promise.resolve(B(e)).then((function(e){var r=!1;try{r=o.ecdsaVerify(i,c,e)}catch(e){console.error("An error occurred when verifying signature: ",e)}return r}))}catch(e){return Promise.reject(e)}}}},t.addEncodedPublicKey=function(e){return e.publicKeyBase58=this.publicKeyBase58,e},t.fingerprint=function(){return e.fingerprintFromPublicKey({publicKeyBase58:this.publicKeyBase58})},t.verifyFingerprint=function(e){if("string"!=typeof e||"z"!==e[0])return{error:new Error("`fingerprint` must be a multibase encoded string."),valid:!1};var r,t;try{r=i.decode(e.slice(1))}catch(e){return{error:e,valid:!1}}try{t=i.decode(this.publicKeyBase58)}catch(e){return{error:e,valid:!1}}var n="e701"===r.slice(0,2).toString("hex")&&t.equals(r.slice(2));return n?{valid:n}:{error:new Error("The fingerprint does not match the public key."),valid:!1}},t.publicNode=function(e){var r=(void 0===e?{}:e).controller,t=void 0===r?this.controller:r,n={id:this.id,type:this.type};return t&&(n.controller=t),this.addEncodedPublicKey(n),n},t.toJwk=function(e){void 0===e&&(e=!1);try{return Promise.resolve(e?v(i.decode(this.privateKeyBase58).toString("hex")):d(i.decode(this.publicKeyBase58).toString("hex")))}catch(e){return Promise.reject(e)}},t.toHex=function(e){void 0===e&&(e=!1);try{var r=function(e){if(t)return e;var r=m;return Promise.resolve(d(i.decode(n.publicKeyBase58).toString("hex"))).then((function(e){return r.call(k,e)}))},t=!1,n=this,o=function(){if(e){t=!0;var r=h;return Promise.resolve(v(i.decode(n.privateKeyBase58).toString("hex"))).then((function(e){return r.call(k,e)}))}}();return Promise.resolve(o&&o.then?o.then(r):r(o))}catch(e){return Promise.reject(e)}},(r=[{key:"publicKey",get:function(){return this.publicKeyBase58}},{key:"privateKey",get:function(){return this.privateKeyBase58}}])&&function(e,r){for(var t=0;t<r.length;t++){var n=r[t];n.enumerable=n.enumerable||!1,n.configurable=!0,"value"in n&&(n.writable=!0),Object.defineProperty(e,n.key,n)}}(e.prototype,r),e}(),O=function(e){var r="did:key:"+e.fingerprint(),t="#"+e.fingerprint();return{"@context":["https://www.w3.org/ns/did/v1",{"@base":r}],id:r,publicKey:[{id:t,type:e.type,controller:r,publicKeyBase58:e.publicKeyBase58}],authentication:[t],assertionMethod:[t],capabilityDelegation:[t],capabilityInvocation:[t]}},F={__proto__:null,computeKeyId:function(e){var r=e.key;try{return Promise.resolve("did:key:"+r.fingerprint()+"#"+r.fingerprint())}catch(e){return Promise.reject(e)}},keyToDidDoc:O,get:function(e){var r=void 0===e?{}:e,t=r.did,n=r.url;try{if(!(t=t||n))throw new TypeError('"did" must be a string.');var i=t.split("#")[0].split("did:key:").pop();return Promise.resolve(J.fromFingerprint({fingerprint:i})).then((function(e){return O(e)}))}catch(e){return Promise.reject(e)}}};exports.ES256K=S,exports.Secp256k1KeyPair=J,exports.driver=F,exports.keyUtils=k;
//# sourceMappingURL=did-key-secp256k1.cjs.production.min.js.map

@@ -389,3 +389,4 @@ import keyto from '@trust/keyto';

var signatureUInt8Array = base64url.toBuffer(encodedSignature);
var verified = secp256k1.ecdsaVerify(signatureUInt8Array, messageHashUInt8Array, publicKeyUInt8Array);
var signatureLowerS = secp256k1.signatureNormalize(signatureUInt8Array);
var verified = secp256k1.ecdsaVerify(signatureLowerS, messageHashUInt8Array, publicKeyUInt8Array);

@@ -396,3 +397,6 @@ if (verified) {

throw new Error('Cannot verify detached signature.');
var erroObject = {
signature: signatureUInt8Array.toString('hex')
};
throw new JWSVerificationFailed('ECDSA Verify Failed: ' + JSON.stringify(erroObject, null, 2));
});

@@ -443,3 +447,4 @@ } catch (e) {

var signatureUInt8Array = base64url.toBuffer(encodedSignature);
var verified = secp256k1.ecdsaVerify(signatureUInt8Array, messageHashUInt8Array, publicKeyUInt8Array);
var signatureLowerS = secp256k1.signatureNormalize(signatureUInt8Array);
var verified = secp256k1.ecdsaVerify(signatureLowerS, messageHashUInt8Array, publicKeyUInt8Array);

@@ -450,3 +455,8 @@ if (verified) {

throw new JWSVerificationFailed('signature verification failed');
var erroObject = {
signature: signatureUInt8Array.toString('hex'),
message: messageHashUInt8Array.toString('hex'),
publicKey: publicKeyUInt8Array.toString('hex')
};
throw new JWSVerificationFailed('ECDSA Verify Failed: ' + JSON.stringify(erroObject, null, 2));
});

@@ -453,0 +463,0 @@ } catch (e) {

@@ -5,3 +5,3 @@ {

"module": "dist/did-key-secp256k1.esm.js",
"version": "0.2.1-unstable.10",
"version": "0.2.1-unstable.11",
"publishConfig": {

@@ -35,2 +35,3 @@ "access": "public"

"bitcoin-ts": "1.14.2",
"jose": "^1.28.0",
"tsdx": "^0.13.2",

@@ -47,3 +48,3 @@ "tslib": "^2.0.0",

},
"gitHead": "af8c30ab538cbefa484c23eea23c878d57ea34f5"
"gitHead": "01239b4da5cfa58c8195c1269a057699664a0943"
}

@@ -105,11 +105,20 @@ import base64url from 'base64url';

const signatureUInt8Array = base64url.toBuffer(encodedSignature);
let signatureLowerS = secp256k1.signatureNormalize(signatureUInt8Array);
const verified = secp256k1.ecdsaVerify(
signatureUInt8Array,
signatureLowerS,
messageHashUInt8Array,
publicKeyUInt8Array
);
if (verified) {
return true;
}
throw new Error('Cannot verify detached signature.');
const erroObject = {
signature: signatureUInt8Array.toString('hex'),
// message: messageHashUInt8Array.toString('hex'),
// publicKey: publicKeyUInt8Array.toString('hex'),
};
throw new JWSVerificationFailed(
'ECDSA Verify Failed: ' + JSON.stringify(erroObject, null, 2)
);
};

@@ -164,5 +173,6 @@

const signatureUInt8Array = base64url.toBuffer(encodedSignature);
let signatureLowerS = secp256k1.signatureNormalize(signatureUInt8Array);
const verified = secp256k1.ecdsaVerify(
signatureUInt8Array,
signatureLowerS,
messageHashUInt8Array,

@@ -174,3 +184,10 @@ publicKeyUInt8Array

}
throw new JWSVerificationFailed('signature verification failed');
const erroObject = {
signature: signatureUInt8Array.toString('hex'),
message: messageHashUInt8Array.toString('hex'),
publicKey: publicKeyUInt8Array.toString('hex'),
};
throw new JWSVerificationFailed(
'ECDSA Verify Failed: ' + JSON.stringify(erroObject, null, 2)
);
};

@@ -177,0 +194,0 @@

Sorry, the diff of this file is not supported yet

Sorry, the diff of this file is not supported yet

Sorry, the diff of this file is not supported yet

SocketSocket SOC 2 Logo

Product

  • Package Alerts
  • Integrations
  • Docs
  • Pricing
  • FAQ
  • Roadmap
  • Changelog

Packages

npm

Stay in touch

Get open source security insights delivered straight into your inbox.


  • Terms
  • Privacy
  • Security

Made with ⚡️ by Socket Inc