@sovrahq/kms-core 1.4.0-4

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
Files changed (76) hide show
  1. package/LICENSE +201 -0
  2. package/dist/decorators/vc-suite.decorator.d.ts +4 -0
  3. package/dist/decorators/vc-suite.decorator.js +12 -0
  4. package/dist/decorators/vc-suite.decorator.js.map +1 -0
  5. package/dist/index.d.ts +16 -0
  6. package/dist/index.js +33 -0
  7. package/dist/index.js.map +1 -0
  8. package/dist/models/keypair.d.ts +10 -0
  9. package/dist/models/keypair.js +3 -0
  10. package/dist/models/keypair.js.map +1 -0
  11. package/dist/models/kms-storage.d.ts +7 -0
  12. package/dist/models/kms-storage.js +3 -0
  13. package/dist/models/kms-storage.js.map +1 -0
  14. package/dist/models/kms.d.ts +51 -0
  15. package/dist/models/kms.js +3 -0
  16. package/dist/models/kms.js.map +1 -0
  17. package/dist/models/lang.d.ts +6 -0
  18. package/dist/models/lang.js +11 -0
  19. package/dist/models/lang.js.map +1 -0
  20. package/dist/models/secret.d.ts +4 -0
  21. package/dist/models/secret.js +3 -0
  22. package/dist/models/secret.js.map +1 -0
  23. package/dist/models/suites/bbsbls2020.suite.d.ts +6 -0
  24. package/dist/models/suites/bbsbls2020.suite.js +3 -0
  25. package/dist/models/suites/bbsbls2020.suite.js.map +1 -0
  26. package/dist/models/suites/didcomm/didcomm-message-media-type.d.ts +88 -0
  27. package/dist/models/suites/didcomm/didcomm-message-media-type.js +24 -0
  28. package/dist/models/suites/didcomm/didcomm-message-media-type.js.map +1 -0
  29. package/dist/models/suites/didcomm/didcomm-message.d.ts +7 -0
  30. package/dist/models/suites/didcomm/didcomm-message.js +3 -0
  31. package/dist/models/suites/didcomm/didcomm-message.js.map +1 -0
  32. package/dist/models/suites/didcomm/didcomm-packed-message.d.ts +22 -0
  33. package/dist/models/suites/didcomm/didcomm-packed-message.js +3 -0
  34. package/dist/models/suites/didcomm/didcomm-packed-message.js.map +1 -0
  35. package/dist/models/suites/didcomm/didcomm-signed-message.d.ts +41 -0
  36. package/dist/models/suites/didcomm/didcomm-signed-message.js +3 -0
  37. package/dist/models/suites/didcomm/didcomm-signed-message.js.map +1 -0
  38. package/dist/models/suites/didcomm.suite.d.ts +26 -0
  39. package/dist/models/suites/didcomm.suite.js +3 -0
  40. package/dist/models/suites/didcomm.suite.js.map +1 -0
  41. package/dist/models/suites/es256k-signer.suite.d.ts +15 -0
  42. package/dist/models/suites/es256k-signer.suite.js +3 -0
  43. package/dist/models/suites/es256k-signer.suite.js.map +1 -0
  44. package/dist/models/suites/selective-disclosure-zkp.suite.d.ts +6 -0
  45. package/dist/models/suites/selective-disclosure-zkp.suite.js +3 -0
  46. package/dist/models/suites/selective-disclosure-zkp.suite.js.map +1 -0
  47. package/dist/models/suites/vc.suite.d.ts +13 -0
  48. package/dist/models/suites/vc.suite.js +3 -0
  49. package/dist/models/suites/vc.suite.js.map +1 -0
  50. package/dist/models/supported-suites.d.ts +13 -0
  51. package/dist/models/supported-suites.js +27 -0
  52. package/dist/models/supported-suites.js.map +1 -0
  53. package/dist/utils/base-converter.d.ts +28 -0
  54. package/dist/utils/base-converter.js +113 -0
  55. package/dist/utils/base-converter.js.map +1 -0
  56. package/package.json +40 -0
  57. package/readme.md +57 -0
  58. package/src/decorators/vc-suite.decorator.ts +11 -0
  59. package/src/index.ts +16 -0
  60. package/src/models/keypair.ts +14 -0
  61. package/src/models/kms-storage.ts +10 -0
  62. package/src/models/kms.ts +49 -0
  63. package/src/models/lang.ts +6 -0
  64. package/src/models/secret.ts +4 -0
  65. package/src/models/suites/bbsbls2020.suite.ts +8 -0
  66. package/src/models/suites/didcomm/didcomm-message-media-type.ts +103 -0
  67. package/src/models/suites/didcomm/didcomm-message.ts +8 -0
  68. package/src/models/suites/didcomm/didcomm-packed-message.ts +20 -0
  69. package/src/models/suites/didcomm/didcomm-signed-message.ts +40 -0
  70. package/src/models/suites/didcomm.suite.ts +34 -0
  71. package/src/models/suites/es256k-signer.suite.ts +15 -0
  72. package/src/models/suites/selective-disclosure-zkp.suite.ts +7 -0
  73. package/src/models/suites/vc.suite.ts +16 -0
  74. package/src/models/supported-suites.ts +22 -0
  75. package/src/utils/base-converter.ts +137 -0
  76. package/tsconfig.json +20 -0
@@ -0,0 +1,20 @@
1
+ export interface DIDCommPackedMessage {
2
+ protected: string;
3
+ iv: string;
4
+ ciphertext: string;
5
+ tag: string;
6
+ recipients: [{
7
+ encrypted_key: string;
8
+ header: {
9
+ alg: string;
10
+ iv: string;
11
+ tag: string;
12
+ epk: {
13
+ kty: string;
14
+ crv: string;
15
+ x: string;
16
+ };
17
+ kid: string;
18
+ }
19
+ }]
20
+ }
@@ -0,0 +1,40 @@
1
+ import { JWE } from 'did-jwt'
2
+ import { DIDCommMessageMediaType, IDIDCommMessage } from './didcomm-message-media-type'
3
+
4
+ /**
5
+ * Represents a plaintext DIDComm v2 message object.
6
+ * @internal
7
+ */
8
+ export type _DIDCommPlainMessage = IDIDCommMessage & { typ: DIDCommMessageMediaType.PLAIN }
9
+
10
+ /**
11
+ * Represents an encrypted DIDComm v2 message object
12
+ * @internal
13
+ */
14
+ export type _DIDCommEncryptedMessage = JWE
15
+
16
+ /**
17
+ * Type definition of a JSON serialized JWS in flattened form (only one signer)
18
+ * @internal
19
+ */
20
+ export type _FlattenedJWS = {
21
+ payload: string
22
+ protected?: string
23
+ header?: Record<string, any>
24
+ signature: string
25
+ }
26
+
27
+ /**
28
+ * Type definition of a JSON serialized JWS in generic form
29
+ * @internal
30
+ */
31
+ export type _GenericJWS = {
32
+ payload: string
33
+ signatures: [{ protected?: string; header?: Record<string, any>; signature: string }]
34
+ }
35
+
36
+ /**
37
+ * Composite type representing the 2 accepted forms of JWS DIDComm v2 message
38
+ * @internal
39
+ */
40
+ export type _DIDCommSignedMessage = _FlattenedJWS | _GenericJWS
@@ -0,0 +1,34 @@
1
+ import { IJWK } from "../../utils/base-converter";
2
+ import { IKeyPair } from "../keypair";
3
+ import { IPackDIDCommMessageArgs } from "./didcomm/didcomm-message";
4
+ import { DIDCommMessagePacking, IDIDCommMessage, IPackedDIDCommMessage, IUnpackedDIDCommMessage } from "./didcomm/didcomm-message-media-type";
5
+
6
+ export interface IDidCommKeyPair extends IKeyPair {
7
+ keyType: 'curve25519' | 'Ed25519' | 'x25519' | 'Secp256k1';
8
+ }
9
+
10
+ export interface IDIDCommSuite {
11
+ load(secrets: IDidCommKeyPair);
12
+ create(): Promise<IDidCommKeyPair>;
13
+ pack(encrypt: boolean,
14
+ toHexKeys: string[],
15
+ documentToSign: string): Promise<string>;
16
+ unpack: (encryptedDocument: string) => Promise<any>;
17
+ }
18
+
19
+ export interface IDIDCommV2Suite {
20
+ load(secrets: IDidCommKeyPair);
21
+ create(): Promise<IDidCommKeyPair>;
22
+ //Deprecated: This method will be removed soon.
23
+ pack(params: {
24
+ senderVerificationMethodId: string,
25
+ toKeys: {
26
+ verificationMethodId: string;
27
+ publicKeyHex: string;
28
+ publicKeyCrv?: string;
29
+ }[];
30
+ message: IDIDCommMessage;
31
+ packing: DIDCommMessagePacking;
32
+ }): Promise<IPackedDIDCommMessage>;
33
+ unpack: (jwe: any) => Promise<any>;
34
+ }
@@ -0,0 +1,15 @@
1
+ import { LANG } from "../lang";
2
+ import { IKeyPair } from "../keypair";
3
+ import { IJWK } from "../../utils/base-converter";
4
+
5
+ export interface IES256kKeyPair extends IKeyPair {
6
+ readonly mnemonic?: string;
7
+ readonly curve?: string;
8
+ }
9
+
10
+ export interface IES256kSuite {
11
+ load(IEthrKeyPair: IES256kKeyPair): Promise<void>;
12
+ create(params: { lang: LANG }): Promise<IES256kKeyPair>;
13
+ sign(content: string): Promise<string>;
14
+ verifySignature(originalContent: string, flatSignature: any, publicKey: IJWK): Promise<boolean>;
15
+ }
@@ -0,0 +1,7 @@
1
+ import { DIDDocument } from "@sovrahq/did-core";
2
+ import { VerifiableCredential } from "@sovrahq/vc-core";
3
+ import { IVCSuite } from "./vc.suite";
4
+
5
+ export interface SelectiveDisclosureZKPSuite extends IVCSuite {
6
+ deriveVC(signedDocument: VerifiableCredential, deriveProofFrame: string, didDocumentResolver: (did: string) => Promise<DIDDocument>): Promise<VerifiableCredential>;
7
+ }
@@ -0,0 +1,16 @@
1
+ import { DIDDocument, Purpose } from "@sovrahq/did-core";
2
+ import { VerifiableCredential } from "@sovrahq/vc-core";
3
+ import { IKeyPair } from "../keypair";
4
+
5
+ export interface IVCJsonLDKeyPair extends IKeyPair {
6
+ readonly id?: string;
7
+ readonly controller?: string;
8
+ }
9
+
10
+ export interface IVCSuite {
11
+ loadSuite(params: {
12
+ secrets: IVCJsonLDKeyPair,
13
+ useCache: boolean,
14
+ });
15
+ sign: (documentToSign: string, did: string, verificationMethodId: string, porpuse: Purpose) => Promise<any>;
16
+ }
@@ -0,0 +1,22 @@
1
+ export enum Suite {
2
+ ES256k = "es256k",
3
+ DIDComm = "didcomm",
4
+ DIDCommV2 = "didcommv2",
5
+ Bbsbls2020 = "bbsbls2020",
6
+ RsaSignature2018 = "rsaSignature2018",
7
+ Ed25519Suite = "ed25519Suite",
8
+ }
9
+
10
+ export enum VCSuite {
11
+ Bbsbls2020 = "bbsbls2020",
12
+ RsaSignature2018 = "rsaSignature2018",
13
+ }
14
+
15
+ export function getTypeBySuite(suite: Suite) {
16
+ switch (suite) {
17
+ case Suite.DIDComm: return "X25519KeyAgreementKey2019";
18
+ case Suite.Bbsbls2020: return "Bls12381G1Key2020";
19
+ case Suite.RsaSignature2018: return "RsaSignature2018";
20
+ }
21
+ return null;
22
+ }
@@ -0,0 +1,137 @@
1
+ let multibase = require("multibase");
2
+ let bs58 = require("bs58");
3
+ import { base64url as base64urlformats } from "multiformats/bases/base64";
4
+ import { TextDecoder } from "text-encoding";
5
+
6
+ export enum Base {
7
+ Hex = "hex",
8
+ Base58 = "base58",
9
+ Base64 = "base64",
10
+ JWK = "jwk",
11
+ }
12
+
13
+ export interface IJWK {
14
+ kty: string;
15
+ crv: string;
16
+ x: string;
17
+ y?: string;
18
+ }
19
+
20
+ export class BaseConverter {
21
+ private static hex2base64url(dataHex) {
22
+ const buffer = Buffer.from(dataHex, "hex");
23
+ const base64 = buffer.toString("base64");
24
+ const base64url = base64
25
+ .replace(/\+/g, "-")
26
+ .replace(/\//g, "_")
27
+ .replace(/=/g, "");
28
+ return base64url;
29
+ }
30
+
31
+ public static getPrivateJWKfromHex(_privKey: string, pubKey: string) {
32
+ let privKey = _privKey;
33
+ // remove 0x and 0x04 to be used in jose library
34
+
35
+ privKey = privKey.replace("0x", "");
36
+ pubKey = pubKey.replace("0x04", "");
37
+
38
+ return {
39
+ crv: "secp256k1",
40
+ kty: "EC",
41
+ d: BaseConverter.hex2base64url(privKey),
42
+ x: BaseConverter.hex2base64url(pubKey.substr(0, 64)),
43
+ y: BaseConverter.hex2base64url(pubKey.substr(64, 64))
44
+ };
45
+ }
46
+
47
+ static convert(value: any, fromBase: Base, toBase: Base, keyType?: string) {
48
+ if (fromBase == Base.Base58 && toBase == Base.Hex) {
49
+ const hex = this.toHexString(bs58.decode(value));
50
+ return hex;
51
+ }
52
+ if (fromBase == Base.Hex && toBase == Base.Base58) {
53
+ return bs58.encode(Buffer.from(value, "hex"));
54
+ }
55
+ if (fromBase == Base.Hex && toBase == Base.JWK) {
56
+ return this.hexToJWK(value, keyType);
57
+ }
58
+ if (fromBase == Base.JWK && toBase == Base.Hex) {
59
+ return this.JWKToHex(value);
60
+ }
61
+ if (fromBase == Base.Base58 && toBase == Base.JWK) {
62
+ const hex = this.toHexString(bs58.decode(value));
63
+ return this.hexToJWK(hex, keyType);
64
+ }
65
+ if (fromBase == Base.JWK && toBase == Base.Base58) {
66
+ return bs58.encode(Buffer.from(this.JWKToHex(value).replace("0x", ""), "hex"));
67
+ }
68
+ }
69
+
70
+ private static hexToJWK(value: string, keyType: string) {
71
+ // OKP support (Ed25519 / X25519) — x-only, no y
72
+ if (keyType === 'OKP' || keyType === 'Ed25519' || keyType === 'X25519') {
73
+ const hex = value.replace('0x04', '').replace('0x', '');
74
+ const xBuf = Buffer.from(hex, 'hex');
75
+ const crv = keyType === 'X25519' ? 'X25519' : 'Ed25519';
76
+ const b64 = xBuf.toString('base64').replace(/\+/g, '-').replace(/\//g, '_').replace(/=+$/, '');
77
+ return { kty: 'OKP', crv, x: b64 };
78
+ }
79
+
80
+ value = value.replace("0x04", "");
81
+ value = value.replace("0x", "");
82
+
83
+ return {
84
+ kty: "EC",
85
+ crv: keyType,
86
+ x: this.base64url(
87
+ Buffer.from(value.substring(0, value.length / 2), "hex")
88
+ ),
89
+ y: this.base64url(Buffer.from(value.substring(value.length / 2), "hex")),
90
+ };
91
+ }
92
+
93
+ private static base64url(buffer: Uint8Array) {
94
+ const decoder = new TextDecoder();
95
+ const bytes = multibase.encode("base64url", buffer);
96
+ return decoder.decode(bytes).slice(1);
97
+ }
98
+
99
+ private static JWKToHex(value: {
100
+ kty: string;
101
+ crv: string;
102
+ x?: string;
103
+ y?: string;
104
+ n?: string;
105
+ }) {
106
+ // OKP support (Ed25519, X25519) — only x, no y
107
+ if (value.kty === 'OKP' || value.crv === 'Ed25519' || value.crv === 'X25519') {
108
+ const xStr = value.x || '';
109
+ let padded = xStr.replace(/-/g, '+').replace(/_/g, '/');
110
+ while (padded.length % 4) padded += '=';
111
+ return '0x' + Buffer.from(padded, 'base64').toString('hex');
112
+ }
113
+
114
+ if (value.kty == "RSA" && value.n) {
115
+ return `0x${Buffer.from(base64urlformats.baseDecode(value.n)).toString("hex")}`
116
+ }
117
+ else if (value.x && value.y) {
118
+ const b1 = Buffer.from(base64urlformats.baseDecode(value.x)).toString(
119
+ "hex"
120
+ );
121
+ const b2 = Buffer.from(base64urlformats.baseDecode(value.y)).toString(
122
+ "hex"
123
+ );
124
+ return `0x${b1}${b2}`;
125
+ }
126
+
127
+ throw new Error("This JWK To Hex conversion is not supported: " + JSON.stringify(value));
128
+ }
129
+
130
+ private static fromHexString = (hexString) =>
131
+ Uint8Array.from(
132
+ hexString.match(/.{1,2}/g).map((byte) => parseInt(byte, 16))
133
+ );
134
+
135
+ private static toHexString = (bytes) =>
136
+ bytes.reduce((str, byte) => str + byte.toString(16).padStart(2, "0"), "");
137
+ }
package/tsconfig.json ADDED
@@ -0,0 +1,20 @@
1
+ {
2
+ "compilerOptions": {
3
+ "target": "es5",
4
+ "lib": [
5
+ "es6"
6
+ ],
7
+ "types": [
8
+ "reflect-metadata",
9
+ "node"
10
+ ],
11
+ "outDir": "dist",
12
+ "module": "commonjs",
13
+ "moduleResolution": "node",
14
+ "sourceMap": true,
15
+ "declaration": true,
16
+ "experimentalDecorators": true,
17
+ "emitDecoratorMetadata": true,
18
+
19
+ }
20
+ }