@mattrglobal/bls12381-key-pair
Advanced tools
Comparing version 0.4.0 to 0.4.1-unstable.3e11f99
@@ -0,1 +1,19 @@ | ||
## 0.4.1-unstable.3e11f99 (2020-09-08) | ||
### Features | ||
* update bls12381G2 names and add keylength check to the constructor ([#27](https://github.com/mattrglobal/bls12381-key-pair/issues/27)) ([3e11f99](https://github.com/mattrglobal/bls12381-key-pair/commit/3e11f99870de14b190a72ff05faae7d90fab1f18)) | ||
## 0.4.1 (2020-09-08) | ||
### Features | ||
* update bls12381G2 names and add keylength check to the constructor ([#27](https://github.com/mattrglobal/bls12381-key-pair/issues/27)) ([3e11f99](https://github.com/mattrglobal/bls12381-key-pair/commit/3e11f99870de14b190a72ff05faae7d90fab1f18)) | ||
# [0.4.0](https://github.com/mattrglobal/bls12381-key-pair/compare/v0.3.0...v0.4.0) (2020-08-27) | ||
@@ -2,0 +20,0 @@ |
@@ -1,2 +0,2 @@ | ||
import { KeyPairOptions, KeyPairSigner, KeyPairVerifier, GenerateKeyPairOptions } from "./types"; | ||
import { JsonWebKey, KeyPairOptions, KeyPairSigner, KeyPairVerifier, GenerateKeyPairOptions, JwkKeyPairOptions } from "./types"; | ||
/** | ||
@@ -45,2 +45,9 @@ * A BLS 12-381 based key pair | ||
/** | ||
* Constructs a BLS 12-381 key pair from options | ||
* @param options [Optional] options for key pair | ||
* | ||
* @returns A BLS 12-381 G2 key pair | ||
*/ | ||
static fromJwk(options: JwkKeyPairOptions): Promise<Bls12381G2KeyPair>; | ||
/** | ||
* Constructs a BLS 12-381 key pair from a public key fingerprint | ||
@@ -74,2 +81,8 @@ * @param fingerprint [Optional] public key fingerprint | ||
/** | ||
* Returns the JWK structured public key. | ||
* | ||
* @returns The JWK public key. | ||
*/ | ||
get publicKeyJwk(): JsonWebKey; | ||
/** | ||
* Returns the base58 encoded private key. | ||
@@ -81,2 +94,8 @@ * | ||
/** | ||
* Returns the JWK formatted private key. | ||
* | ||
* @returns The JWK formatted private key. | ||
*/ | ||
get privateKeyJwk(): JsonWebKey | undefined; | ||
/** | ||
* Adds a public key base to a public key node. | ||
@@ -83,0 +102,0 @@ * |
@@ -19,4 +19,8 @@ "use strict"; | ||
/* eslint-disable @typescript-eslint/no-explicit-any */ | ||
const rfc4648_1 = require("rfc4648"); | ||
const bs58_1 = __importDefault(require("bs58")); | ||
const bbs_signatures_1 = require("@mattrglobal/bbs-signatures"); | ||
const types_1 = require("./types"); | ||
const validators_1 = require("./validators"); | ||
const utils_1 = require("./utils"); | ||
/** | ||
@@ -127,3 +131,23 @@ * z represents the multibase encoding scheme of base58 encoding | ||
this.type = "Bls12381G2Key2020"; | ||
//TODO need some assert statements here | ||
/** | ||
* The provided publicKey needs to be 384 bits / 5.85 = 65.6 | ||
* which means the base58 encoded publicKey can be either 65 or 66 chars | ||
* 5.85 = log base 2 (58) which is equivalent to the number of bits | ||
* encoded per character of a base58 encoded string. | ||
* | ||
*/ | ||
if (options.publicKeyBase58.length !== 131 && | ||
options.publicKeyBase58.length !== 132) { | ||
throw new Error(`The size of the public key is incorrect. Expected 131 or 132 chars got: ${options.publicKeyBase58.length}`); | ||
} | ||
/** | ||
* Validates the size of the private key if one is included | ||
* This is done by 256 bits / 5.85 = 43.7 which means | ||
* the base58 encoded privateKey can be either 43 or 44 chars | ||
*/ | ||
if (typeof options.privateKeyBase58 !== "undefined" && | ||
options.privateKeyBase58.length !== 43 && | ||
options.privateKeyBase58.length !== 44) { | ||
throw new Error(`The size of the private key is incorrect. Expected 65 or 66 chars got: ${options.privateKeyBase58.length}`); | ||
} | ||
this.id = options.id; | ||
@@ -159,2 +183,33 @@ this.controller = options.controller; | ||
/** | ||
* Constructs a BLS 12-381 key pair from options | ||
* @param options [Optional] options for key pair | ||
* | ||
* @returns A BLS 12-381 G2 key pair | ||
*/ | ||
static async fromJwk(options) { | ||
const { id, controller, publicKeyJwk, privateKeyJwk } = options; | ||
if (typeof privateKeyJwk !== "undefined" && | ||
/** | ||
* The type casting is verified through the use of this assert function | ||
* However because the returned interface leaves the properties as optional | ||
* they need to be cast to pass to the convert function. | ||
**/ | ||
validators_1.assertBls12381G2PrivateJwk(privateKeyJwk)) { | ||
return new Bls12381G2KeyPair({ | ||
id, | ||
controller, | ||
publicKeyBase58: utils_1.convertBase64urlToBase58(privateKeyJwk.x), | ||
privateKeyBase58: utils_1.convertBase64urlToBase58(privateKeyJwk.d) | ||
}); | ||
} | ||
if (validators_1.assertBls12381G2PublicJwk(publicKeyJwk)) { | ||
return new Bls12381G2KeyPair({ | ||
id, | ||
controller, | ||
publicKeyBase58: utils_1.convertBase64urlToBase58(publicKeyJwk.x) | ||
}); | ||
} | ||
throw Error("The JWK provided is not a valid"); | ||
} | ||
/** | ||
* Constructs a BLS 12-381 key pair from a public key fingerprint | ||
@@ -228,2 +283,15 @@ * @param fingerprint [Optional] public key fingerprint | ||
/** | ||
* Returns the JWK structured public key. | ||
* | ||
* @returns The JWK public key. | ||
*/ | ||
get publicKeyJwk() { | ||
return { | ||
kid: this.id, | ||
kty: "EC", | ||
crv: types_1.BlsCurveName.G2, | ||
x: rfc4648_1.base64url.stringify(this.publicKeyBuffer, { pad: false }) | ||
}; | ||
} | ||
/** | ||
* Returns the base58 encoded private key. | ||
@@ -240,2 +308,19 @@ * | ||
/** | ||
* Returns the JWK formatted private key. | ||
* | ||
* @returns The JWK formatted private key. | ||
*/ | ||
get privateKeyJwk() { | ||
if (this.privateKeyBuffer) { | ||
return { | ||
kid: this.id, | ||
kty: "EC", | ||
crv: types_1.BlsCurveName.G2, | ||
x: rfc4648_1.base64url.stringify(this.publicKeyBuffer, { pad: false }), | ||
d: rfc4648_1.base64url.stringify(this.privateKeyBuffer, { pad: false }) | ||
}; | ||
} | ||
return undefined; | ||
} | ||
/** | ||
* Adds a public key base to a public key node. | ||
@@ -242,0 +327,0 @@ * |
@@ -0,4 +1,7 @@ | ||
export { BlsCurveName } from "./BlsCurveName"; | ||
export { GenerateKeyPairOptions } from "./GenerateKeyPairOptions"; | ||
export { JsonWebKey } from "./JsonWebKey"; | ||
export { JwkKeyPairOptions } from "./JwkKeyPairOptions"; | ||
export { KeyPairOptions } from "./KeyPairOptions"; | ||
export { KeyPairSigner } from "./KeyPairSigner"; | ||
export { KeyPairVerifier } from "./KeyPairVerifier"; |
@@ -15,2 +15,4 @@ "use strict"; | ||
Object.defineProperty(exports, "__esModule", { value: true }); | ||
var BlsCurveName_1 = require("./BlsCurveName"); | ||
exports.BlsCurveName = BlsCurveName_1.BlsCurveName; | ||
//# sourceMappingURL=index.js.map |
{ | ||
"name": "@mattrglobal/bls12381-key-pair", | ||
"version": "0.4.0", | ||
"version": "0.4.1-unstable.3e11f99", | ||
"description": "A library for using BLS 12-381 key pairs with BBS+ signatures", | ||
@@ -69,3 +69,4 @@ "homepage": "https://github.com/mattrglobal/bls12381-key-pair", | ||
"@mattrglobal/bbs-signatures": "0.4.0", | ||
"bs58": "4.0.1" | ||
"bs58": "4.0.1", | ||
"rfc4648": "1.4.0" | ||
}, | ||
@@ -72,0 +73,0 @@ "husky": { |
Sorry, the diff of this file is not supported yet
Sorry, the diff of this file is not supported yet
License Policy Violation
LicenseThis package is not allowed per your license policy. Review the package's license to ensure compliance.
Found 1 instance in 1 package
License Policy Violation
LicenseThis package is not allowed per your license policy. Review the package's license to ensure compliance.
Found 1 instance in 1 package
65085
49
946
3
+ Addedrfc4648@1.4.0
+ Addedrfc4648@1.4.0(transitive)