@sd-jwt/utils 0.3.0 → 2.0.4-next.58
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.
- package/LICENSE +201 -0
- package/dist/index.d.mts +35 -0
- package/dist/index.d.ts +35 -0
- package/dist/index.js +145 -0
- package/dist/index.mjs +115 -0
- package/package.json +58 -46
- package/src/base64url.ts +8 -0
- package/src/disclosure.ts +98 -0
- package/src/error.ts +16 -0
- package/src/index.ts +3 -0
- package/src/test/base64url.spec.ts +26 -0
- package/src/test/disclosure.spec.ts +141 -0
- package/src/test/error.spec.ts +15 -0
- package/tsconfig.json +7 -0
- package/vitest.config.mts +4 -0
- package/build/assert.d.ts +0 -1
- package/build/assert.js +0 -16
- package/build/assert.js.map +0 -1
- package/build/base64url.d.ts +0 -29
- package/build/base64url.js +0 -44
- package/build/base64url.js.map +0 -1
- package/build/getValueByKeyAnyLevel.d.ts +0 -1
- package/build/getValueByKeyAnyLevel.js +0 -20
- package/build/getValueByKeyAnyLevel.js.map +0 -1
- package/build/index.d.ts +0 -6
- package/build/index.js +0 -23
- package/build/index.js.map +0 -1
- package/build/isPromise.d.ts +0 -1
- package/build/isPromise.js +0 -11
- package/build/isPromise.js.map +0 -1
- package/build/object.d.ts +0 -5
- package/build/object.js +0 -76
- package/build/object.js.map +0 -1
- package/build/simpleDeepEqual.d.ts +0 -1
- package/build/simpleDeepEqual.js +0 -20
- package/build/simpleDeepEqual.js.map +0 -1
package/LICENSE
ADDED
|
@@ -0,0 +1,201 @@
|
|
|
1
|
+
Apache License
|
|
2
|
+
Version 2.0, January 2004
|
|
3
|
+
http://www.apache.org/licenses/
|
|
4
|
+
|
|
5
|
+
TERMS AND CONDITIONS FOR USE, REPRODUCTION, AND DISTRIBUTION
|
|
6
|
+
|
|
7
|
+
1. Definitions.
|
|
8
|
+
|
|
9
|
+
"License" shall mean the terms and conditions for use, reproduction,
|
|
10
|
+
and distribution as defined by Sections 1 through 9 of this document.
|
|
11
|
+
|
|
12
|
+
"Licensor" shall mean the copyright owner or entity authorized by
|
|
13
|
+
the copyright owner that is granting the License.
|
|
14
|
+
|
|
15
|
+
"Legal Entity" shall mean the union of the acting entity and all
|
|
16
|
+
other entities that control, are controlled by, or are under common
|
|
17
|
+
control with that entity. For the purposes of this definition,
|
|
18
|
+
"control" means (i) the power, direct or indirect, to cause the
|
|
19
|
+
direction or management of such entity, whether by contract or
|
|
20
|
+
otherwise, or (ii) ownership of fifty percent (50%) or more of the
|
|
21
|
+
outstanding shares, or (iii) beneficial ownership of such entity.
|
|
22
|
+
|
|
23
|
+
"You" (or "Your") shall mean an individual or Legal Entity
|
|
24
|
+
exercising permissions granted by this License.
|
|
25
|
+
|
|
26
|
+
"Source" form shall mean the preferred form for making modifications,
|
|
27
|
+
including but not limited to software source code, documentation
|
|
28
|
+
source, and configuration files.
|
|
29
|
+
|
|
30
|
+
"Object" form shall mean any form resulting from mechanical
|
|
31
|
+
transformation or translation of a Source form, including but
|
|
32
|
+
not limited to compiled object code, generated documentation,
|
|
33
|
+
and conversions to other media types.
|
|
34
|
+
|
|
35
|
+
"Work" shall mean the work of authorship, whether in Source or
|
|
36
|
+
Object form, made available under the License, as indicated by a
|
|
37
|
+
copyright notice that is included in or attached to the work
|
|
38
|
+
(an example is provided in the Appendix below).
|
|
39
|
+
|
|
40
|
+
"Derivative Works" shall mean any work, whether in Source or Object
|
|
41
|
+
form, that is based on (or derived from) the Work and for which the
|
|
42
|
+
editorial revisions, annotations, elaborations, or other modifications
|
|
43
|
+
represent, as a whole, an original work of authorship. For the purposes
|
|
44
|
+
of this License, Derivative Works shall not include works that remain
|
|
45
|
+
separable from, or merely link (or bind by name) to the interfaces of,
|
|
46
|
+
the Work and Derivative Works thereof.
|
|
47
|
+
|
|
48
|
+
"Contribution" shall mean any work of authorship, including
|
|
49
|
+
the original version of the Work and any modifications or additions
|
|
50
|
+
to that Work or Derivative Works thereof, that is intentionally
|
|
51
|
+
submitted to Licensor for inclusion in the Work by the copyright owner
|
|
52
|
+
or by an individual or Legal Entity authorized to submit on behalf of
|
|
53
|
+
the copyright owner. For the purposes of this definition, "submitted"
|
|
54
|
+
means any form of electronic, verbal, or written communication sent
|
|
55
|
+
to the Licensor or its representatives, including but not limited to
|
|
56
|
+
communication on electronic mailing lists, source code control systems,
|
|
57
|
+
and issue tracking systems that are managed by, or on behalf of, the
|
|
58
|
+
Licensor for the purpose of discussing and improving the Work, but
|
|
59
|
+
excluding communication that is conspicuously marked or otherwise
|
|
60
|
+
designated in writing by the copyright owner as "Not a Contribution."
|
|
61
|
+
|
|
62
|
+
"Contributor" shall mean Licensor and any individual or Legal Entity
|
|
63
|
+
on behalf of whom a Contribution has been received by Licensor and
|
|
64
|
+
subsequently incorporated within the Work.
|
|
65
|
+
|
|
66
|
+
2. Grant of Copyright License. Subject to the terms and conditions of
|
|
67
|
+
this License, each Contributor hereby grants to You a perpetual,
|
|
68
|
+
worldwide, non-exclusive, no-charge, royalty-free, irrevocable
|
|
69
|
+
copyright license to reproduce, prepare Derivative Works of,
|
|
70
|
+
publicly display, publicly perform, sublicense, and distribute the
|
|
71
|
+
Work and such Derivative Works in Source or Object form.
|
|
72
|
+
|
|
73
|
+
3. Grant of Patent License. Subject to the terms and conditions of
|
|
74
|
+
this License, each Contributor hereby grants to You a perpetual,
|
|
75
|
+
worldwide, non-exclusive, no-charge, royalty-free, irrevocable
|
|
76
|
+
(except as stated in this section) patent license to make, have made,
|
|
77
|
+
use, offer to sell, sell, import, and otherwise transfer the Work,
|
|
78
|
+
where such license applies only to those patent claims licensable
|
|
79
|
+
by such Contributor that are necessarily infringed by their
|
|
80
|
+
Contribution(s) alone or by combination of their Contribution(s)
|
|
81
|
+
with the Work to which such Contribution(s) was submitted. If You
|
|
82
|
+
institute patent litigation against any entity (including a
|
|
83
|
+
cross-claim or counterclaim in a lawsuit) alleging that the Work
|
|
84
|
+
or a Contribution incorporated within the Work constitutes direct
|
|
85
|
+
or contributory patent infringement, then any patent licenses
|
|
86
|
+
granted to You under this License for that Work shall terminate
|
|
87
|
+
as of the date such litigation is filed.
|
|
88
|
+
|
|
89
|
+
4. Redistribution. You may reproduce and distribute copies of the
|
|
90
|
+
Work or Derivative Works thereof in any medium, with or without
|
|
91
|
+
modifications, and in Source or Object form, provided that You
|
|
92
|
+
meet the following conditions:
|
|
93
|
+
|
|
94
|
+
(a) You must give any other recipients of the Work or
|
|
95
|
+
Derivative Works a copy of this License; and
|
|
96
|
+
|
|
97
|
+
(b) You must cause any modified files to carry prominent notices
|
|
98
|
+
stating that You changed the files; and
|
|
99
|
+
|
|
100
|
+
(c) You must retain, in the Source form of any Derivative Works
|
|
101
|
+
that You distribute, all copyright, patent, trademark, and
|
|
102
|
+
attribution notices from the Source form of the Work,
|
|
103
|
+
excluding those notices that do not pertain to any part of
|
|
104
|
+
the Derivative Works; and
|
|
105
|
+
|
|
106
|
+
(d) If the Work includes a "NOTICE" text file as part of its
|
|
107
|
+
distribution, then any Derivative Works that You distribute must
|
|
108
|
+
include a readable copy of the attribution notices contained
|
|
109
|
+
within such NOTICE file, excluding those notices that do not
|
|
110
|
+
pertain to any part of the Derivative Works, in at least one
|
|
111
|
+
of the following places: within a NOTICE text file distributed
|
|
112
|
+
as part of the Derivative Works; within the Source form or
|
|
113
|
+
documentation, if provided along with the Derivative Works; or,
|
|
114
|
+
within a display generated by the Derivative Works, if and
|
|
115
|
+
wherever such third-party notices normally appear. The contents
|
|
116
|
+
of the NOTICE file are for informational purposes only and
|
|
117
|
+
do not modify the License. You may add Your own attribution
|
|
118
|
+
notices within Derivative Works that You distribute, alongside
|
|
119
|
+
or as an addendum to the NOTICE text from the Work, provided
|
|
120
|
+
that such additional attribution notices cannot be construed
|
|
121
|
+
as modifying the License.
|
|
122
|
+
|
|
123
|
+
You may add Your own copyright statement to Your modifications and
|
|
124
|
+
may provide additional or different license terms and conditions
|
|
125
|
+
for use, reproduction, or distribution of Your modifications, or
|
|
126
|
+
for any such Derivative Works as a whole, provided Your use,
|
|
127
|
+
reproduction, and distribution of the Work otherwise complies with
|
|
128
|
+
the conditions stated in this License.
|
|
129
|
+
|
|
130
|
+
5. Submission of Contributions. Unless You explicitly state otherwise,
|
|
131
|
+
any Contribution intentionally submitted for inclusion in the Work
|
|
132
|
+
by You to the Licensor shall be under the terms and conditions of
|
|
133
|
+
this License, without any additional terms or conditions.
|
|
134
|
+
Notwithstanding the above, nothing herein shall supersede or modify
|
|
135
|
+
the terms of any separate license agreement you may have executed
|
|
136
|
+
with Licensor regarding such Contributions.
|
|
137
|
+
|
|
138
|
+
6. Trademarks. This License does not grant permission to use the trade
|
|
139
|
+
names, trademarks, service marks, or product names of the Licensor,
|
|
140
|
+
except as required for reasonable and customary use in describing the
|
|
141
|
+
origin of the Work and reproducing the content of the NOTICE file.
|
|
142
|
+
|
|
143
|
+
7. Disclaimer of Warranty. Unless required by applicable law or
|
|
144
|
+
agreed to in writing, Licensor provides the Work (and each
|
|
145
|
+
Contributor provides its Contributions) on an "AS IS" BASIS,
|
|
146
|
+
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or
|
|
147
|
+
implied, including, without limitation, any warranties or conditions
|
|
148
|
+
of TITLE, NON-INFRINGEMENT, MERCHANTABILITY, or FITNESS FOR A
|
|
149
|
+
PARTICULAR PURPOSE. You are solely responsible for determining the
|
|
150
|
+
appropriateness of using or redistributing the Work and assume any
|
|
151
|
+
risks associated with Your exercise of permissions under this License.
|
|
152
|
+
|
|
153
|
+
8. Limitation of Liability. In no event and under no legal theory,
|
|
154
|
+
whether in tort (including negligence), contract, or otherwise,
|
|
155
|
+
unless required by applicable law (such as deliberate and grossly
|
|
156
|
+
negligent acts) or agreed to in writing, shall any Contributor be
|
|
157
|
+
liable to You for damages, including any direct, indirect, special,
|
|
158
|
+
incidental, or consequential damages of any character arising as a
|
|
159
|
+
result of this License or out of the use or inability to use the
|
|
160
|
+
Work (including but not limited to damages for loss of goodwill,
|
|
161
|
+
work stoppage, computer failure or malfunction, or any and all
|
|
162
|
+
other commercial damages or losses), even if such Contributor
|
|
163
|
+
has been advised of the possibility of such damages.
|
|
164
|
+
|
|
165
|
+
9. Accepting Warranty or Additional Liability. While redistributing
|
|
166
|
+
the Work or Derivative Works thereof, You may choose to offer,
|
|
167
|
+
and charge a fee for, acceptance of support, warranty, indemnity,
|
|
168
|
+
or other liability obligations and/or rights consistent with this
|
|
169
|
+
License. However, in accepting such obligations, You may act only
|
|
170
|
+
on Your own behalf and on Your sole responsibility, not on behalf
|
|
171
|
+
of any other Contributor, and only if You agree to indemnify,
|
|
172
|
+
defend, and hold each Contributor harmless for any liability
|
|
173
|
+
incurred by, or claims asserted against, such Contributor by reason
|
|
174
|
+
of your accepting any such warranty or additional liability.
|
|
175
|
+
|
|
176
|
+
END OF TERMS AND CONDITIONS
|
|
177
|
+
|
|
178
|
+
APPENDIX: How to apply the Apache License to your work.
|
|
179
|
+
|
|
180
|
+
To apply the Apache License to your work, attach the following
|
|
181
|
+
boilerplate notice, with the fields enclosed by brackets "[]"
|
|
182
|
+
replaced with your own identifying information. (Don't include
|
|
183
|
+
the brackets!) The text should be enclosed in the appropriate
|
|
184
|
+
comment syntax for the file format. We also recommend that a
|
|
185
|
+
file or class name and description of purpose be included on the
|
|
186
|
+
same "printed page" as the copyright notice for easier
|
|
187
|
+
identification within third-party archives.
|
|
188
|
+
|
|
189
|
+
Copyright [yyyy] [name of copyright owner]
|
|
190
|
+
|
|
191
|
+
Licensed under the Apache License, Version 2.0 (the "License");
|
|
192
|
+
you may not use this file except in compliance with the License.
|
|
193
|
+
You may obtain a copy of the License at
|
|
194
|
+
|
|
195
|
+
http://www.apache.org/licenses/LICENSE-2.0
|
|
196
|
+
|
|
197
|
+
Unless required by applicable law or agreed to in writing, software
|
|
198
|
+
distributed under the License is distributed on an "AS IS" BASIS,
|
|
199
|
+
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
|
200
|
+
See the License for the specific language governing permissions and
|
|
201
|
+
limitations under the License.
|
package/dist/index.d.mts
ADDED
|
@@ -0,0 +1,35 @@
|
|
|
1
|
+
import { DisclosureData, HasherAndAlg, HasherAndAlgSync } from '@sd-jwt/types';
|
|
2
|
+
|
|
3
|
+
declare const Base64urlEncode: (src: string) => string;
|
|
4
|
+
declare const Base64urlDecode: (src: string) => string;
|
|
5
|
+
declare const Uint8ArrayToBase64Url: (input: Uint8Array) => string;
|
|
6
|
+
|
|
7
|
+
declare class SDJWTException extends Error {
|
|
8
|
+
details?: unknown;
|
|
9
|
+
constructor(message: string, details?: unknown);
|
|
10
|
+
getFullMessage(): string;
|
|
11
|
+
}
|
|
12
|
+
|
|
13
|
+
declare class Disclosure<T = unknown> {
|
|
14
|
+
salt: string;
|
|
15
|
+
key?: string;
|
|
16
|
+
value: T;
|
|
17
|
+
private _digest;
|
|
18
|
+
private _encoded;
|
|
19
|
+
constructor(data: DisclosureData<T>, _meta?: {
|
|
20
|
+
digest: string;
|
|
21
|
+
encoded: string;
|
|
22
|
+
});
|
|
23
|
+
static fromEncode<T>(s: string, hash: HasherAndAlg): Promise<Disclosure<T>>;
|
|
24
|
+
static fromEncodeSync<T>(s: string, hash: HasherAndAlgSync): Disclosure<T>;
|
|
25
|
+
static fromArray<T>(item: DisclosureData<T>, _meta?: {
|
|
26
|
+
digest: string;
|
|
27
|
+
encoded: string;
|
|
28
|
+
}): Disclosure<T>;
|
|
29
|
+
encode(): string;
|
|
30
|
+
decode(): DisclosureData<T>;
|
|
31
|
+
digest(hash: HasherAndAlg): Promise<string>;
|
|
32
|
+
digestSync(hash: HasherAndAlgSync): string;
|
|
33
|
+
}
|
|
34
|
+
|
|
35
|
+
export { Base64urlDecode, Base64urlEncode, Disclosure, SDJWTException, Uint8ArrayToBase64Url };
|
package/dist/index.d.ts
ADDED
|
@@ -0,0 +1,35 @@
|
|
|
1
|
+
import { DisclosureData, HasherAndAlg, HasherAndAlgSync } from '@sd-jwt/types';
|
|
2
|
+
|
|
3
|
+
declare const Base64urlEncode: (src: string) => string;
|
|
4
|
+
declare const Base64urlDecode: (src: string) => string;
|
|
5
|
+
declare const Uint8ArrayToBase64Url: (input: Uint8Array) => string;
|
|
6
|
+
|
|
7
|
+
declare class SDJWTException extends Error {
|
|
8
|
+
details?: unknown;
|
|
9
|
+
constructor(message: string, details?: unknown);
|
|
10
|
+
getFullMessage(): string;
|
|
11
|
+
}
|
|
12
|
+
|
|
13
|
+
declare class Disclosure<T = unknown> {
|
|
14
|
+
salt: string;
|
|
15
|
+
key?: string;
|
|
16
|
+
value: T;
|
|
17
|
+
private _digest;
|
|
18
|
+
private _encoded;
|
|
19
|
+
constructor(data: DisclosureData<T>, _meta?: {
|
|
20
|
+
digest: string;
|
|
21
|
+
encoded: string;
|
|
22
|
+
});
|
|
23
|
+
static fromEncode<T>(s: string, hash: HasherAndAlg): Promise<Disclosure<T>>;
|
|
24
|
+
static fromEncodeSync<T>(s: string, hash: HasherAndAlgSync): Disclosure<T>;
|
|
25
|
+
static fromArray<T>(item: DisclosureData<T>, _meta?: {
|
|
26
|
+
digest: string;
|
|
27
|
+
encoded: string;
|
|
28
|
+
}): Disclosure<T>;
|
|
29
|
+
encode(): string;
|
|
30
|
+
decode(): DisclosureData<T>;
|
|
31
|
+
digest(hash: HasherAndAlg): Promise<string>;
|
|
32
|
+
digestSync(hash: HasherAndAlgSync): string;
|
|
33
|
+
}
|
|
34
|
+
|
|
35
|
+
export { Base64urlDecode, Base64urlEncode, Disclosure, SDJWTException, Uint8ArrayToBase64Url };
|
package/dist/index.js
ADDED
|
@@ -0,0 +1,145 @@
|
|
|
1
|
+
"use strict";
|
|
2
|
+
var __defProp = Object.defineProperty;
|
|
3
|
+
var __getOwnPropDesc = Object.getOwnPropertyDescriptor;
|
|
4
|
+
var __getOwnPropNames = Object.getOwnPropertyNames;
|
|
5
|
+
var __hasOwnProp = Object.prototype.hasOwnProperty;
|
|
6
|
+
var __export = (target, all) => {
|
|
7
|
+
for (var name in all)
|
|
8
|
+
__defProp(target, name, { get: all[name], enumerable: true });
|
|
9
|
+
};
|
|
10
|
+
var __copyProps = (to, from, except, desc) => {
|
|
11
|
+
if (from && typeof from === "object" || typeof from === "function") {
|
|
12
|
+
for (let key of __getOwnPropNames(from))
|
|
13
|
+
if (!__hasOwnProp.call(to, key) && key !== except)
|
|
14
|
+
__defProp(to, key, { get: () => from[key], enumerable: !(desc = __getOwnPropDesc(from, key)) || desc.enumerable });
|
|
15
|
+
}
|
|
16
|
+
return to;
|
|
17
|
+
};
|
|
18
|
+
var __toCommonJS = (mod) => __copyProps(__defProp({}, "__esModule", { value: true }), mod);
|
|
19
|
+
var __async = (__this, __arguments, generator) => {
|
|
20
|
+
return new Promise((resolve, reject) => {
|
|
21
|
+
var fulfilled = (value) => {
|
|
22
|
+
try {
|
|
23
|
+
step(generator.next(value));
|
|
24
|
+
} catch (e) {
|
|
25
|
+
reject(e);
|
|
26
|
+
}
|
|
27
|
+
};
|
|
28
|
+
var rejected = (value) => {
|
|
29
|
+
try {
|
|
30
|
+
step(generator.throw(value));
|
|
31
|
+
} catch (e) {
|
|
32
|
+
reject(e);
|
|
33
|
+
}
|
|
34
|
+
};
|
|
35
|
+
var step = (x) => x.done ? resolve(x.value) : Promise.resolve(x.value).then(fulfilled, rejected);
|
|
36
|
+
step((generator = generator.apply(__this, __arguments)).next());
|
|
37
|
+
});
|
|
38
|
+
};
|
|
39
|
+
|
|
40
|
+
// src/index.ts
|
|
41
|
+
var src_exports = {};
|
|
42
|
+
__export(src_exports, {
|
|
43
|
+
Base64urlDecode: () => Base64urlDecode,
|
|
44
|
+
Base64urlEncode: () => Base64urlEncode,
|
|
45
|
+
Disclosure: () => Disclosure,
|
|
46
|
+
SDJWTException: () => SDJWTException,
|
|
47
|
+
Uint8ArrayToBase64Url: () => Uint8ArrayToBase64Url
|
|
48
|
+
});
|
|
49
|
+
module.exports = __toCommonJS(src_exports);
|
|
50
|
+
|
|
51
|
+
// src/base64url.ts
|
|
52
|
+
var import_js_base64 = require("js-base64");
|
|
53
|
+
var Base64urlEncode = import_js_base64.Base64.encodeURI;
|
|
54
|
+
var Base64urlDecode = import_js_base64.Base64.decode;
|
|
55
|
+
var Uint8ArrayToBase64Url = (input) => import_js_base64.Base64.fromUint8Array(input, true);
|
|
56
|
+
|
|
57
|
+
// src/error.ts
|
|
58
|
+
var SDJWTException = class _SDJWTException extends Error {
|
|
59
|
+
constructor(message, details) {
|
|
60
|
+
super(message);
|
|
61
|
+
Object.setPrototypeOf(this, _SDJWTException.prototype);
|
|
62
|
+
this.name = "SDJWTException";
|
|
63
|
+
this.details = details;
|
|
64
|
+
}
|
|
65
|
+
getFullMessage() {
|
|
66
|
+
return `${this.name}: ${this.message} ${this.details ? `- ${JSON.stringify(this.details)}` : ""}`;
|
|
67
|
+
}
|
|
68
|
+
};
|
|
69
|
+
|
|
70
|
+
// src/disclosure.ts
|
|
71
|
+
var Disclosure = class _Disclosure {
|
|
72
|
+
constructor(data, _meta) {
|
|
73
|
+
this._digest = _meta == null ? void 0 : _meta.digest;
|
|
74
|
+
this._encoded = _meta == null ? void 0 : _meta.encoded;
|
|
75
|
+
if (data.length === 2) {
|
|
76
|
+
this.salt = data[0];
|
|
77
|
+
this.value = data[1];
|
|
78
|
+
return;
|
|
79
|
+
}
|
|
80
|
+
if (data.length === 3) {
|
|
81
|
+
this.salt = data[0];
|
|
82
|
+
this.key = data[1];
|
|
83
|
+
this.value = data[2];
|
|
84
|
+
return;
|
|
85
|
+
}
|
|
86
|
+
throw new SDJWTException("Invalid disclosure data");
|
|
87
|
+
}
|
|
88
|
+
// We need to digest of the original encoded data.
|
|
89
|
+
// After decode process, we use JSON.stringify to encode the data.
|
|
90
|
+
// This can be different from the original encoded data.
|
|
91
|
+
static fromEncode(s, hash) {
|
|
92
|
+
return __async(this, null, function* () {
|
|
93
|
+
const { hasher, alg } = hash;
|
|
94
|
+
const digest = yield hasher(s, alg);
|
|
95
|
+
const digestStr = Uint8ArrayToBase64Url(digest);
|
|
96
|
+
const item = JSON.parse(Base64urlDecode(s));
|
|
97
|
+
return _Disclosure.fromArray(item, { digest: digestStr, encoded: s });
|
|
98
|
+
});
|
|
99
|
+
}
|
|
100
|
+
static fromEncodeSync(s, hash) {
|
|
101
|
+
const { hasher, alg } = hash;
|
|
102
|
+
const digest = hasher(s, alg);
|
|
103
|
+
const digestStr = Uint8ArrayToBase64Url(digest);
|
|
104
|
+
const item = JSON.parse(Base64urlDecode(s));
|
|
105
|
+
return _Disclosure.fromArray(item, { digest: digestStr, encoded: s });
|
|
106
|
+
}
|
|
107
|
+
static fromArray(item, _meta) {
|
|
108
|
+
return new _Disclosure(item, _meta);
|
|
109
|
+
}
|
|
110
|
+
encode() {
|
|
111
|
+
if (!this._encoded) {
|
|
112
|
+
this._encoded = Base64urlEncode(JSON.stringify(this.decode()));
|
|
113
|
+
}
|
|
114
|
+
return this._encoded;
|
|
115
|
+
}
|
|
116
|
+
decode() {
|
|
117
|
+
return this.key ? [this.salt, this.key, this.value] : [this.salt, this.value];
|
|
118
|
+
}
|
|
119
|
+
digest(hash) {
|
|
120
|
+
return __async(this, null, function* () {
|
|
121
|
+
const { hasher, alg } = hash;
|
|
122
|
+
if (!this._digest) {
|
|
123
|
+
const hash2 = yield hasher(this.encode(), alg);
|
|
124
|
+
this._digest = Uint8ArrayToBase64Url(hash2);
|
|
125
|
+
}
|
|
126
|
+
return this._digest;
|
|
127
|
+
});
|
|
128
|
+
}
|
|
129
|
+
digestSync(hash) {
|
|
130
|
+
const { hasher, alg } = hash;
|
|
131
|
+
if (!this._digest) {
|
|
132
|
+
const hash2 = hasher(this.encode(), alg);
|
|
133
|
+
this._digest = Uint8ArrayToBase64Url(hash2);
|
|
134
|
+
}
|
|
135
|
+
return this._digest;
|
|
136
|
+
}
|
|
137
|
+
};
|
|
138
|
+
// Annotate the CommonJS export names for ESM import in node:
|
|
139
|
+
0 && (module.exports = {
|
|
140
|
+
Base64urlDecode,
|
|
141
|
+
Base64urlEncode,
|
|
142
|
+
Disclosure,
|
|
143
|
+
SDJWTException,
|
|
144
|
+
Uint8ArrayToBase64Url
|
|
145
|
+
});
|
package/dist/index.mjs
ADDED
|
@@ -0,0 +1,115 @@
|
|
|
1
|
+
var __async = (__this, __arguments, generator) => {
|
|
2
|
+
return new Promise((resolve, reject) => {
|
|
3
|
+
var fulfilled = (value) => {
|
|
4
|
+
try {
|
|
5
|
+
step(generator.next(value));
|
|
6
|
+
} catch (e) {
|
|
7
|
+
reject(e);
|
|
8
|
+
}
|
|
9
|
+
};
|
|
10
|
+
var rejected = (value) => {
|
|
11
|
+
try {
|
|
12
|
+
step(generator.throw(value));
|
|
13
|
+
} catch (e) {
|
|
14
|
+
reject(e);
|
|
15
|
+
}
|
|
16
|
+
};
|
|
17
|
+
var step = (x) => x.done ? resolve(x.value) : Promise.resolve(x.value).then(fulfilled, rejected);
|
|
18
|
+
step((generator = generator.apply(__this, __arguments)).next());
|
|
19
|
+
});
|
|
20
|
+
};
|
|
21
|
+
|
|
22
|
+
// src/base64url.ts
|
|
23
|
+
import { Base64 } from "js-base64";
|
|
24
|
+
var Base64urlEncode = Base64.encodeURI;
|
|
25
|
+
var Base64urlDecode = Base64.decode;
|
|
26
|
+
var Uint8ArrayToBase64Url = (input) => Base64.fromUint8Array(input, true);
|
|
27
|
+
|
|
28
|
+
// src/error.ts
|
|
29
|
+
var SDJWTException = class _SDJWTException extends Error {
|
|
30
|
+
constructor(message, details) {
|
|
31
|
+
super(message);
|
|
32
|
+
Object.setPrototypeOf(this, _SDJWTException.prototype);
|
|
33
|
+
this.name = "SDJWTException";
|
|
34
|
+
this.details = details;
|
|
35
|
+
}
|
|
36
|
+
getFullMessage() {
|
|
37
|
+
return `${this.name}: ${this.message} ${this.details ? `- ${JSON.stringify(this.details)}` : ""}`;
|
|
38
|
+
}
|
|
39
|
+
};
|
|
40
|
+
|
|
41
|
+
// src/disclosure.ts
|
|
42
|
+
var Disclosure = class _Disclosure {
|
|
43
|
+
constructor(data, _meta) {
|
|
44
|
+
this._digest = _meta == null ? void 0 : _meta.digest;
|
|
45
|
+
this._encoded = _meta == null ? void 0 : _meta.encoded;
|
|
46
|
+
if (data.length === 2) {
|
|
47
|
+
this.salt = data[0];
|
|
48
|
+
this.value = data[1];
|
|
49
|
+
return;
|
|
50
|
+
}
|
|
51
|
+
if (data.length === 3) {
|
|
52
|
+
this.salt = data[0];
|
|
53
|
+
this.key = data[1];
|
|
54
|
+
this.value = data[2];
|
|
55
|
+
return;
|
|
56
|
+
}
|
|
57
|
+
throw new SDJWTException("Invalid disclosure data");
|
|
58
|
+
}
|
|
59
|
+
// We need to digest of the original encoded data.
|
|
60
|
+
// After decode process, we use JSON.stringify to encode the data.
|
|
61
|
+
// This can be different from the original encoded data.
|
|
62
|
+
static fromEncode(s, hash) {
|
|
63
|
+
return __async(this, null, function* () {
|
|
64
|
+
const { hasher, alg } = hash;
|
|
65
|
+
const digest = yield hasher(s, alg);
|
|
66
|
+
const digestStr = Uint8ArrayToBase64Url(digest);
|
|
67
|
+
const item = JSON.parse(Base64urlDecode(s));
|
|
68
|
+
return _Disclosure.fromArray(item, { digest: digestStr, encoded: s });
|
|
69
|
+
});
|
|
70
|
+
}
|
|
71
|
+
static fromEncodeSync(s, hash) {
|
|
72
|
+
const { hasher, alg } = hash;
|
|
73
|
+
const digest = hasher(s, alg);
|
|
74
|
+
const digestStr = Uint8ArrayToBase64Url(digest);
|
|
75
|
+
const item = JSON.parse(Base64urlDecode(s));
|
|
76
|
+
return _Disclosure.fromArray(item, { digest: digestStr, encoded: s });
|
|
77
|
+
}
|
|
78
|
+
static fromArray(item, _meta) {
|
|
79
|
+
return new _Disclosure(item, _meta);
|
|
80
|
+
}
|
|
81
|
+
encode() {
|
|
82
|
+
if (!this._encoded) {
|
|
83
|
+
this._encoded = Base64urlEncode(JSON.stringify(this.decode()));
|
|
84
|
+
}
|
|
85
|
+
return this._encoded;
|
|
86
|
+
}
|
|
87
|
+
decode() {
|
|
88
|
+
return this.key ? [this.salt, this.key, this.value] : [this.salt, this.value];
|
|
89
|
+
}
|
|
90
|
+
digest(hash) {
|
|
91
|
+
return __async(this, null, function* () {
|
|
92
|
+
const { hasher, alg } = hash;
|
|
93
|
+
if (!this._digest) {
|
|
94
|
+
const hash2 = yield hasher(this.encode(), alg);
|
|
95
|
+
this._digest = Uint8ArrayToBase64Url(hash2);
|
|
96
|
+
}
|
|
97
|
+
return this._digest;
|
|
98
|
+
});
|
|
99
|
+
}
|
|
100
|
+
digestSync(hash) {
|
|
101
|
+
const { hasher, alg } = hash;
|
|
102
|
+
if (!this._digest) {
|
|
103
|
+
const hash2 = hasher(this.encode(), alg);
|
|
104
|
+
this._digest = Uint8ArrayToBase64Url(hash2);
|
|
105
|
+
}
|
|
106
|
+
return this._digest;
|
|
107
|
+
}
|
|
108
|
+
};
|
|
109
|
+
export {
|
|
110
|
+
Base64urlDecode,
|
|
111
|
+
Base64urlEncode,
|
|
112
|
+
Disclosure,
|
|
113
|
+
SDJWTException,
|
|
114
|
+
Uint8ArrayToBase64Url
|
|
115
|
+
};
|
package/package.json
CHANGED
|
@@ -1,49 +1,61 @@
|
|
|
1
1
|
{
|
|
2
|
-
|
|
3
|
-
|
|
4
|
-
|
|
5
|
-
|
|
6
|
-
|
|
7
|
-
|
|
8
|
-
|
|
9
|
-
|
|
10
|
-
|
|
11
|
-
|
|
12
|
-
|
|
13
|
-
|
|
14
|
-
|
|
15
|
-
|
|
16
|
-
|
|
17
|
-
|
|
2
|
+
"name": "@sd-jwt/utils",
|
|
3
|
+
"version": "2.0.4-next.58+99e0161",
|
|
4
|
+
"description": "sd-jwt draft 7 implementation in typescript",
|
|
5
|
+
"main": "dist/index.js",
|
|
6
|
+
"module": "dist/index.mjs",
|
|
7
|
+
"types": "dist/index.d.ts",
|
|
8
|
+
"exports": {
|
|
9
|
+
".": {
|
|
10
|
+
"import": "./dist/index.mjs",
|
|
11
|
+
"require": "./dist/index.js"
|
|
12
|
+
}
|
|
13
|
+
},
|
|
14
|
+
"scripts": {
|
|
15
|
+
"build": "rm -rf **/dist && tsup",
|
|
16
|
+
"lint": "biome lint ./src",
|
|
17
|
+
"test": "pnpm run test:node && pnpm run test:browser && pnpm run test:cov",
|
|
18
|
+
"test:node": "vitest run ./src/test/*.spec.ts",
|
|
19
|
+
"test:browser": "vitest run ./src/test/*.spec.ts --environment jsdom",
|
|
20
|
+
"test:cov": "vitest run --coverage"
|
|
21
|
+
},
|
|
22
|
+
"keywords": [
|
|
23
|
+
"sd-jwt",
|
|
24
|
+
"sdjwt",
|
|
25
|
+
"sd-jwt-vc"
|
|
26
|
+
],
|
|
27
|
+
"repository": {
|
|
28
|
+
"type": "git",
|
|
29
|
+
"url": "https://github.com/openwallet-foundation-labs/sd-jwt-js"
|
|
30
|
+
},
|
|
31
|
+
"author": "Lukas.J.Han <lukas.j.han@gmail.com>",
|
|
32
|
+
"homepage": "https://github.com/openwallet-foundation-labs/sd-jwt-js/wiki",
|
|
33
|
+
"bugs": {
|
|
34
|
+
"url": "https://github.com/openwallet-foundation-labs/sd-jwt-js/issues"
|
|
35
|
+
},
|
|
36
|
+
"license": "Apache-2.0",
|
|
37
|
+
"devDependencies": {
|
|
38
|
+
"@sd-jwt/crypto-nodejs": "2.0.4-next.58+99e0161"
|
|
39
|
+
},
|
|
40
|
+
"dependencies": {
|
|
41
|
+
"@sd-jwt/types": "2.0.4-next.58+99e0161",
|
|
42
|
+
"js-base64": "^3.7.6"
|
|
43
|
+
},
|
|
44
|
+
"publishConfig": {
|
|
45
|
+
"access": "public"
|
|
46
|
+
},
|
|
47
|
+
"tsup": {
|
|
48
|
+
"entry": [
|
|
49
|
+
"./src/index.ts"
|
|
18
50
|
],
|
|
19
|
-
"
|
|
20
|
-
|
|
21
|
-
|
|
22
|
-
|
|
23
|
-
"
|
|
24
|
-
|
|
25
|
-
|
|
26
|
-
|
|
27
|
-
|
|
28
|
-
|
|
29
|
-
"access": "public"
|
|
30
|
-
},
|
|
31
|
-
"main": "build/index.js",
|
|
32
|
-
"files": [
|
|
33
|
-
"build"
|
|
34
|
-
],
|
|
35
|
-
"scripts": {
|
|
36
|
-
"build": "tsc",
|
|
37
|
-
"test": "node --require ts-node/register --test ./tests/*.test.ts"
|
|
38
|
-
},
|
|
39
|
-
"dependencies": {
|
|
40
|
-
"@sd-jwt/types": "0.3.0",
|
|
41
|
-
"buffer": "*"
|
|
42
|
-
},
|
|
43
|
-
"devDependencies": {
|
|
44
|
-
"@types/node": "*",
|
|
45
|
-
"ts-node": "*",
|
|
46
|
-
"typescript": "*"
|
|
47
|
-
},
|
|
48
|
-
"gitHead": "38e6729c173b08d2b6adb437efa72e8bbb21cb2c"
|
|
51
|
+
"sourceMap": true,
|
|
52
|
+
"splitting": false,
|
|
53
|
+
"clean": true,
|
|
54
|
+
"dts": true,
|
|
55
|
+
"format": [
|
|
56
|
+
"cjs",
|
|
57
|
+
"esm"
|
|
58
|
+
]
|
|
59
|
+
},
|
|
60
|
+
"gitHead": "99e0161ecb2c3e79d835afdc047a7b93d0509d41"
|
|
49
61
|
}
|
package/src/base64url.ts
ADDED
|
@@ -0,0 +1,98 @@
|
|
|
1
|
+
import {
|
|
2
|
+
Uint8ArrayToBase64Url,
|
|
3
|
+
Base64urlDecode,
|
|
4
|
+
Base64urlEncode,
|
|
5
|
+
} from './base64url';
|
|
6
|
+
import { SDJWTException } from './error';
|
|
7
|
+
import { HasherAndAlg, DisclosureData, HasherAndAlgSync } from '@sd-jwt/types';
|
|
8
|
+
|
|
9
|
+
export class Disclosure<T = unknown> {
|
|
10
|
+
public salt: string;
|
|
11
|
+
public key?: string;
|
|
12
|
+
public value: T;
|
|
13
|
+
private _digest: string | undefined;
|
|
14
|
+
private _encoded: string | undefined;
|
|
15
|
+
|
|
16
|
+
public constructor(
|
|
17
|
+
data: DisclosureData<T>,
|
|
18
|
+
_meta?: { digest: string; encoded: string },
|
|
19
|
+
) {
|
|
20
|
+
// If the meta is provided, then we assume that the data is already encoded and digested
|
|
21
|
+
this._digest = _meta?.digest;
|
|
22
|
+
this._encoded = _meta?.encoded;
|
|
23
|
+
|
|
24
|
+
if (data.length === 2) {
|
|
25
|
+
this.salt = data[0];
|
|
26
|
+
this.value = data[1];
|
|
27
|
+
return;
|
|
28
|
+
}
|
|
29
|
+
if (data.length === 3) {
|
|
30
|
+
this.salt = data[0];
|
|
31
|
+
this.key = data[1] as string;
|
|
32
|
+
this.value = data[2];
|
|
33
|
+
return;
|
|
34
|
+
}
|
|
35
|
+
throw new SDJWTException('Invalid disclosure data');
|
|
36
|
+
}
|
|
37
|
+
|
|
38
|
+
// We need to digest of the original encoded data.
|
|
39
|
+
// After decode process, we use JSON.stringify to encode the data.
|
|
40
|
+
// This can be different from the original encoded data.
|
|
41
|
+
public static async fromEncode<T>(s: string, hash: HasherAndAlg) {
|
|
42
|
+
const { hasher, alg } = hash;
|
|
43
|
+
const digest = await hasher(s, alg);
|
|
44
|
+
const digestStr = Uint8ArrayToBase64Url(digest);
|
|
45
|
+
const item = JSON.parse(Base64urlDecode(s)) as DisclosureData<T>;
|
|
46
|
+
return Disclosure.fromArray<T>(item, { digest: digestStr, encoded: s });
|
|
47
|
+
}
|
|
48
|
+
|
|
49
|
+
public static fromEncodeSync<T>(s: string, hash: HasherAndAlgSync) {
|
|
50
|
+
const { hasher, alg } = hash;
|
|
51
|
+
const digest = hasher(s, alg);
|
|
52
|
+
const digestStr = Uint8ArrayToBase64Url(digest);
|
|
53
|
+
const item = JSON.parse(Base64urlDecode(s)) as DisclosureData<T>;
|
|
54
|
+
return Disclosure.fromArray<T>(item, { digest: digestStr, encoded: s });
|
|
55
|
+
}
|
|
56
|
+
|
|
57
|
+
public static fromArray<T>(
|
|
58
|
+
item: DisclosureData<T>,
|
|
59
|
+
_meta?: { digest: string; encoded: string },
|
|
60
|
+
) {
|
|
61
|
+
return new Disclosure(item, _meta);
|
|
62
|
+
}
|
|
63
|
+
|
|
64
|
+
public encode() {
|
|
65
|
+
if (!this._encoded) {
|
|
66
|
+
// we use JSON.stringify to encode the data
|
|
67
|
+
// It's the most reliable and universal way to encode JSON object
|
|
68
|
+
this._encoded = Base64urlEncode(JSON.stringify(this.decode()));
|
|
69
|
+
}
|
|
70
|
+
return this._encoded;
|
|
71
|
+
}
|
|
72
|
+
|
|
73
|
+
public decode(): DisclosureData<T> {
|
|
74
|
+
return this.key
|
|
75
|
+
? [this.salt, this.key, this.value]
|
|
76
|
+
: [this.salt, this.value];
|
|
77
|
+
}
|
|
78
|
+
|
|
79
|
+
public async digest(hash: HasherAndAlg): Promise<string> {
|
|
80
|
+
const { hasher, alg } = hash;
|
|
81
|
+
if (!this._digest) {
|
|
82
|
+
const hash = await hasher(this.encode(), alg);
|
|
83
|
+
this._digest = Uint8ArrayToBase64Url(hash);
|
|
84
|
+
}
|
|
85
|
+
|
|
86
|
+
return this._digest;
|
|
87
|
+
}
|
|
88
|
+
|
|
89
|
+
public digestSync(hash: HasherAndAlgSync): string {
|
|
90
|
+
const { hasher, alg } = hash;
|
|
91
|
+
if (!this._digest) {
|
|
92
|
+
const hash = hasher(this.encode(), alg);
|
|
93
|
+
this._digest = Uint8ArrayToBase64Url(hash);
|
|
94
|
+
}
|
|
95
|
+
|
|
96
|
+
return this._digest;
|
|
97
|
+
}
|
|
98
|
+
}
|
package/src/error.ts
ADDED
|
@@ -0,0 +1,16 @@
|
|
|
1
|
+
export class SDJWTException extends Error {
|
|
2
|
+
public details?: unknown;
|
|
3
|
+
|
|
4
|
+
constructor(message: string, details?: unknown) {
|
|
5
|
+
super(message);
|
|
6
|
+
Object.setPrototypeOf(this, SDJWTException.prototype);
|
|
7
|
+
this.name = 'SDJWTException';
|
|
8
|
+
this.details = details;
|
|
9
|
+
}
|
|
10
|
+
|
|
11
|
+
getFullMessage(): string {
|
|
12
|
+
return `${this.name}: ${this.message} ${
|
|
13
|
+
this.details ? `- ${JSON.stringify(this.details)}` : ''
|
|
14
|
+
}`;
|
|
15
|
+
}
|
|
16
|
+
}
|
package/src/index.ts
ADDED
|
@@ -0,0 +1,26 @@
|
|
|
1
|
+
import { describe, expect, test } from 'vitest';
|
|
2
|
+
import {
|
|
3
|
+
Base64urlDecode,
|
|
4
|
+
Base64urlEncode,
|
|
5
|
+
Uint8ArrayToBase64Url,
|
|
6
|
+
} from '../base64url';
|
|
7
|
+
|
|
8
|
+
describe('Base64url', () => {
|
|
9
|
+
const raw = 'abcdefghijklmnopqrstuvwxyz';
|
|
10
|
+
const encoded = 'YWJjZGVmZ2hpamtsbW5vcHFyc3R1dnd4eXo';
|
|
11
|
+
test('Encode', () => {
|
|
12
|
+
expect(Base64urlEncode(raw)).toStrictEqual(encoded);
|
|
13
|
+
});
|
|
14
|
+
test('Decode', () => {
|
|
15
|
+
expect(Base64urlDecode(encoded)).toStrictEqual(raw);
|
|
16
|
+
});
|
|
17
|
+
test('Encode and decode', () => {
|
|
18
|
+
const str = 'hello world';
|
|
19
|
+
expect(Base64urlDecode(Base64urlEncode(str))).toStrictEqual(str);
|
|
20
|
+
});
|
|
21
|
+
test('Uint8Array', () => {
|
|
22
|
+
const str = 'hello world';
|
|
23
|
+
const uint8 = new TextEncoder().encode(str);
|
|
24
|
+
expect(Uint8ArrayToBase64Url(uint8)).toStrictEqual(Base64urlEncode(str));
|
|
25
|
+
});
|
|
26
|
+
});
|
|
@@ -0,0 +1,141 @@
|
|
|
1
|
+
import { generateSalt, digest as hasher } from '@sd-jwt/crypto-nodejs';
|
|
2
|
+
import { Disclosure } from '../disclosure';
|
|
3
|
+
import { describe, expect, test } from 'vitest';
|
|
4
|
+
import { Base64urlEncode, SDJWTException } from '../index';
|
|
5
|
+
|
|
6
|
+
const hash = { alg: 'SHA256', hasher };
|
|
7
|
+
|
|
8
|
+
/*
|
|
9
|
+
ref draft-ietf-oauth-selective-disclosure-jwt-07
|
|
10
|
+
Claim given_name:
|
|
11
|
+
SHA-256 Hash: jsu9yVulwQQlhFlM_3JlzMaSFzglhQG0DpfayQwLUK4
|
|
12
|
+
Disclosure: WyIyR0xDNDJzS1F2ZUNmR2ZyeU5STjl3IiwgImdpdmVuX25hbWUiLCAiSm9obiJd
|
|
13
|
+
Contents: ["2GLC42sKQveCfGfryNRN9w", "given_name", "John"]
|
|
14
|
+
For example, the SHA-256 digest of the Disclosure
|
|
15
|
+
WyI2cU1RdlJMNWhhaiIsICJmYW1pbHlfbmFtZSIsICJNw7ZiaXVzIl0 would be uutlBuYeMDyjLLTpf6Jxi7yNkEF35jdyWMn9U7b_RYY.
|
|
16
|
+
The SHA-256 digest of the Disclosure
|
|
17
|
+
WyJsa2x4RjVqTVlsR1RQVW92TU5JdkNBIiwgIkZSIl0 would be w0I8EKcdCtUPkGCNUrfwVp2xEgNjtoIDlOxc9-PlOhs.
|
|
18
|
+
*/
|
|
19
|
+
const TestDataDraft7 = {
|
|
20
|
+
claimTests: [
|
|
21
|
+
{
|
|
22
|
+
contents: '["2GLC42sKQveCfGfryNRN9w", "given_name", "John"]',
|
|
23
|
+
digest: 'jsu9yVulwQQlhFlM_3JlzMaSFzglhQG0DpfayQwLUK4',
|
|
24
|
+
disclosure:
|
|
25
|
+
'WyIyR0xDNDJzS1F2ZUNmR2ZyeU5STjl3IiwgImdpdmVuX25hbWUiLCAiSm9obiJd',
|
|
26
|
+
},
|
|
27
|
+
],
|
|
28
|
+
sha256Tests: [
|
|
29
|
+
{
|
|
30
|
+
digest: 'uutlBuYeMDyjLLTpf6Jxi7yNkEF35jdyWMn9U7b_RYY',
|
|
31
|
+
disclosure: 'WyI2cU1RdlJMNWhhaiIsICJmYW1pbHlfbmFtZSIsICJNw7ZiaXVzIl0',
|
|
32
|
+
},
|
|
33
|
+
{
|
|
34
|
+
digest: 'w0I8EKcdCtUPkGCNUrfwVp2xEgNjtoIDlOxc9-PlOhs',
|
|
35
|
+
disclosure: 'WyJsa2x4RjVqTVlsR1RQVW92TU5JdkNBIiwgIkZSIl0',
|
|
36
|
+
},
|
|
37
|
+
],
|
|
38
|
+
};
|
|
39
|
+
|
|
40
|
+
describe('Disclosure', () => {
|
|
41
|
+
test('create object disclosure', async () => {
|
|
42
|
+
const salt = generateSalt(16);
|
|
43
|
+
const disclosure = new Disclosure([salt, 'name', 'James']);
|
|
44
|
+
expect(disclosure).toBeDefined();
|
|
45
|
+
expect(disclosure.key).toBe('name');
|
|
46
|
+
expect(disclosure.value).toBe('James');
|
|
47
|
+
expect(disclosure.salt).toBe(salt);
|
|
48
|
+
});
|
|
49
|
+
|
|
50
|
+
test('create array disclosure', async () => {
|
|
51
|
+
const salt = generateSalt(16);
|
|
52
|
+
const disclosure = new Disclosure([salt, 'US']);
|
|
53
|
+
expect(disclosure).toBeDefined();
|
|
54
|
+
expect(disclosure.key).toBe(undefined);
|
|
55
|
+
expect(disclosure.value).toBe('US');
|
|
56
|
+
expect(disclosure.salt).toBe(salt);
|
|
57
|
+
});
|
|
58
|
+
|
|
59
|
+
test('create disclosure error', async () => {
|
|
60
|
+
const salt = generateSalt(16);
|
|
61
|
+
const data: [string, string, string] = [salt, 'name', 'James'];
|
|
62
|
+
data.push('any');
|
|
63
|
+
expect(() => new Disclosure(data)).toThrow();
|
|
64
|
+
try {
|
|
65
|
+
new Disclosure(data);
|
|
66
|
+
} catch (e: unknown) {
|
|
67
|
+
const error = e as SDJWTException;
|
|
68
|
+
expect(typeof error.getFullMessage()).toBe('string');
|
|
69
|
+
}
|
|
70
|
+
});
|
|
71
|
+
|
|
72
|
+
test('encode disclosure', async () => {
|
|
73
|
+
const salt = generateSalt(16);
|
|
74
|
+
const disclosure = new Disclosure([salt, 'name', 'James']);
|
|
75
|
+
const encodedDisclosure = disclosure.encode();
|
|
76
|
+
expect(encodedDisclosure).toBeDefined();
|
|
77
|
+
expect(typeof encodedDisclosure).toBe('string');
|
|
78
|
+
});
|
|
79
|
+
|
|
80
|
+
test('decode disclosure', async () => {
|
|
81
|
+
const salt = generateSalt(16);
|
|
82
|
+
const disclosure = new Disclosure([salt, 'name', 'James']);
|
|
83
|
+
const encodedDisclosure = disclosure.encode();
|
|
84
|
+
const newDisclosure = await Disclosure.fromEncode(encodedDisclosure, {
|
|
85
|
+
alg: 'SHA256',
|
|
86
|
+
hasher,
|
|
87
|
+
});
|
|
88
|
+
|
|
89
|
+
expect(newDisclosure).toBeDefined();
|
|
90
|
+
expect(newDisclosure.key).toBe('name');
|
|
91
|
+
expect(newDisclosure.value).toBe('James');
|
|
92
|
+
expect(newDisclosure.salt).toBe(salt);
|
|
93
|
+
});
|
|
94
|
+
|
|
95
|
+
test('digest disclosure #1', async () => {
|
|
96
|
+
const salt = generateSalt(16);
|
|
97
|
+
const disclosure = new Disclosure([salt, 'name', 'James']);
|
|
98
|
+
const digest = await disclosure.digest({ alg: 'SHA256', hasher });
|
|
99
|
+
expect(digest).toBeDefined();
|
|
100
|
+
expect(typeof digest).toBe('string');
|
|
101
|
+
});
|
|
102
|
+
|
|
103
|
+
test('digest disclosure #2', async () => {
|
|
104
|
+
const disclosure = new Disclosure([
|
|
105
|
+
'2GLC42sKQveCfGfryNRN9w',
|
|
106
|
+
'given_name',
|
|
107
|
+
'John',
|
|
108
|
+
]);
|
|
109
|
+
const encode = disclosure.encode();
|
|
110
|
+
expect(encode).toBe(
|
|
111
|
+
'WyIyR0xDNDJzS1F2ZUNmR2ZyeU5STjl3IiwiZ2l2ZW5fbmFtZSIsIkpvaG4iXQ',
|
|
112
|
+
);
|
|
113
|
+
const digest = await disclosure.digest(hash);
|
|
114
|
+
expect(digest).toBe('8VHiz7qTXavxvpiTYDCSr_shkUO6qRcVXjkhEnt1os4');
|
|
115
|
+
});
|
|
116
|
+
|
|
117
|
+
test('digest disclosure #2', async () => {
|
|
118
|
+
const encoded = Base64urlEncode(TestDataDraft7.claimTests[0].contents);
|
|
119
|
+
expect(encoded).toStrictEqual(TestDataDraft7.claimTests[0].disclosure);
|
|
120
|
+
|
|
121
|
+
const disclosure = await Disclosure.fromEncode(
|
|
122
|
+
TestDataDraft7.claimTests[0].disclosure,
|
|
123
|
+
hash,
|
|
124
|
+
);
|
|
125
|
+
|
|
126
|
+
const digest = await disclosure.digest(hash);
|
|
127
|
+
expect(digest).toBe(TestDataDraft7.claimTests[0].digest);
|
|
128
|
+
});
|
|
129
|
+
|
|
130
|
+
test('digest disclosure #3', async () => {
|
|
131
|
+
for (const sha256Test of TestDataDraft7.sha256Tests) {
|
|
132
|
+
const disclosure = await Disclosure.fromEncode(
|
|
133
|
+
sha256Test.disclosure,
|
|
134
|
+
hash,
|
|
135
|
+
);
|
|
136
|
+
|
|
137
|
+
const digest = await disclosure.digest(hash);
|
|
138
|
+
expect(digest).toBe(sha256Test.digest);
|
|
139
|
+
}
|
|
140
|
+
});
|
|
141
|
+
});
|
|
@@ -0,0 +1,15 @@
|
|
|
1
|
+
import { SDJWTException } from '../error';
|
|
2
|
+
import { describe, expect, test } from 'vitest';
|
|
3
|
+
|
|
4
|
+
describe('Error tests', () => {
|
|
5
|
+
test('Detail', () => {
|
|
6
|
+
try {
|
|
7
|
+
throw new SDJWTException('msg', { info: 'details' });
|
|
8
|
+
} catch (e: unknown) {
|
|
9
|
+
const exception = e as SDJWTException;
|
|
10
|
+
expect(exception.getFullMessage()).toEqual(
|
|
11
|
+
'SDJWTException: msg - {"info":"details"}',
|
|
12
|
+
);
|
|
13
|
+
}
|
|
14
|
+
});
|
|
15
|
+
});
|
package/tsconfig.json
ADDED
package/build/assert.d.ts
DELETED
|
@@ -1 +0,0 @@
|
|
|
1
|
-
export declare const assertClaimInObject: (object: Record<string, unknown>, claimKey: string, claimValue?: unknown) => void;
|
package/build/assert.js
DELETED
|
@@ -1,16 +0,0 @@
|
|
|
1
|
-
"use strict";
|
|
2
|
-
Object.defineProperty(exports, "__esModule", { value: true });
|
|
3
|
-
exports.assertClaimInObject = void 0;
|
|
4
|
-
const getValueByKeyAnyLevel_1 = require("./getValueByKeyAnyLevel");
|
|
5
|
-
const simpleDeepEqual_1 = require("./simpleDeepEqual");
|
|
6
|
-
const assertClaimInObject = (object, claimKey, claimValue) => {
|
|
7
|
-
const value = (0, getValueByKeyAnyLevel_1.getValueByKeyAnyLevel)(object, claimKey);
|
|
8
|
-
if (!value) {
|
|
9
|
-
throw new Error(`Claim key '${claimKey}' not found in any level`);
|
|
10
|
-
}
|
|
11
|
-
if (claimValue && !(0, simpleDeepEqual_1.simpleDeepEqual)(value, claimValue)) {
|
|
12
|
-
throw new Error(`Claim key '${claimKey}' was found, but values did not match`);
|
|
13
|
-
}
|
|
14
|
-
};
|
|
15
|
-
exports.assertClaimInObject = assertClaimInObject;
|
|
16
|
-
//# sourceMappingURL=assert.js.map
|
package/build/assert.js.map
DELETED
|
@@ -1 +0,0 @@
|
|
|
1
|
-
{"version":3,"file":"assert.js","sourceRoot":"","sources":["../src/assert.ts"],"names":[],"mappings":";;;AAAA,mEAA+D;AAC/D,uDAAmD;AAE5C,MAAM,mBAAmB,GAAG,CAC/B,MAA+B,EAC/B,QAAgB,EAChB,UAAoB,EACtB,EAAE;IACA,MAAM,KAAK,GAAG,IAAA,6CAAqB,EAAC,MAAM,EAAE,QAAQ,CAAC,CAAA;IAErD,IAAI,CAAC,KAAK,EAAE,CAAC;QACT,MAAM,IAAI,KAAK,CAAC,cAAc,QAAQ,0BAA0B,CAAC,CAAA;IACrE,CAAC;IAED,IAAI,UAAU,IAAI,CAAC,IAAA,iCAAe,EAAC,KAAK,EAAE,UAAU,CAAC,EAAE,CAAC;QACpD,MAAM,IAAI,KAAK,CACX,cAAc,QAAQ,uCAAuC,CAChE,CAAA;IACL,CAAC;AACL,CAAC,CAAA;AAhBY,QAAA,mBAAmB,uBAgB/B"}
|
package/build/base64url.d.ts
DELETED
|
@@ -1,29 +0,0 @@
|
|
|
1
|
-
/// <reference types="node" />
|
|
2
|
-
import { Buffer } from 'buffer';
|
|
3
|
-
export declare class Base64url {
|
|
4
|
-
/**
|
|
5
|
-
*
|
|
6
|
-
* Encode into base64url string
|
|
7
|
-
*
|
|
8
|
-
*/
|
|
9
|
-
static encode(input: string | Uint8Array | Buffer): string;
|
|
10
|
-
/**
|
|
11
|
-
*
|
|
12
|
-
* Encode from JSON into a base64url string
|
|
13
|
-
*
|
|
14
|
-
*/
|
|
15
|
-
static encodeFromJson(input: Record<string, unknown> | Array<unknown>): string;
|
|
16
|
-
/**
|
|
17
|
-
*
|
|
18
|
-
* Decode from base64url into JSON
|
|
19
|
-
*
|
|
20
|
-
*/
|
|
21
|
-
static decodeToJson<T extends Record<string, unknown> | Array<unknown> = Record<string, unknown>>(input: string): T;
|
|
22
|
-
/**
|
|
23
|
-
*
|
|
24
|
-
* Decode from base64url into a byte array
|
|
25
|
-
*
|
|
26
|
-
*/
|
|
27
|
-
static decode(input: string): Uint8Array;
|
|
28
|
-
}
|
|
29
|
-
export declare function base64ToBase64URL(base64: string): string;
|
package/build/base64url.js
DELETED
|
@@ -1,44 +0,0 @@
|
|
|
1
|
-
"use strict";
|
|
2
|
-
Object.defineProperty(exports, "__esModule", { value: true });
|
|
3
|
-
exports.base64ToBase64URL = exports.Base64url = void 0;
|
|
4
|
-
const buffer_1 = require("buffer");
|
|
5
|
-
class Base64url {
|
|
6
|
-
/**
|
|
7
|
-
*
|
|
8
|
-
* Encode into base64url string
|
|
9
|
-
*
|
|
10
|
-
*/
|
|
11
|
-
static encode(input) {
|
|
12
|
-
return base64ToBase64URL(buffer_1.Buffer.from(input).toString('base64'));
|
|
13
|
-
}
|
|
14
|
-
/**
|
|
15
|
-
*
|
|
16
|
-
* Encode from JSON into a base64url string
|
|
17
|
-
*
|
|
18
|
-
*/
|
|
19
|
-
static encodeFromJson(input) {
|
|
20
|
-
return base64ToBase64URL(buffer_1.Buffer.from(JSON.stringify(input)).toString('base64'));
|
|
21
|
-
}
|
|
22
|
-
/**
|
|
23
|
-
*
|
|
24
|
-
* Decode from base64url into JSON
|
|
25
|
-
*
|
|
26
|
-
*/
|
|
27
|
-
static decodeToJson(input) {
|
|
28
|
-
return JSON.parse(buffer_1.Buffer.from(input, 'base64').toString());
|
|
29
|
-
}
|
|
30
|
-
/**
|
|
31
|
-
*
|
|
32
|
-
* Decode from base64url into a byte array
|
|
33
|
-
*
|
|
34
|
-
*/
|
|
35
|
-
static decode(input) {
|
|
36
|
-
return Uint8Array.from(buffer_1.Buffer.from(input, 'base64'));
|
|
37
|
-
}
|
|
38
|
-
}
|
|
39
|
-
exports.Base64url = Base64url;
|
|
40
|
-
function base64ToBase64URL(base64) {
|
|
41
|
-
return base64.replace(/\+/g, '-').replace(/\//g, '_').replace(/=/g, '');
|
|
42
|
-
}
|
|
43
|
-
exports.base64ToBase64URL = base64ToBase64URL;
|
|
44
|
-
//# sourceMappingURL=base64url.js.map
|
package/build/base64url.js.map
DELETED
|
@@ -1 +0,0 @@
|
|
|
1
|
-
{"version":3,"file":"base64url.js","sourceRoot":"","sources":["../src/base64url.ts"],"names":[],"mappings":";;;AAAA,mCAA+B;AAE/B,MAAa,SAAS;IAClB;;;;OAIG;IACI,MAAM,CAAC,MAAM,CAAC,KAAmC;QACpD,OAAO,iBAAiB,CAAC,eAAM,CAAC,IAAI,CAAC,KAAK,CAAC,CAAC,QAAQ,CAAC,QAAQ,CAAC,CAAC,CAAA;IACnE,CAAC;IAED;;;;OAIG;IACI,MAAM,CAAC,cAAc,CACxB,KAA+C;QAE/C,OAAO,iBAAiB,CACpB,eAAM,CAAC,IAAI,CAAC,IAAI,CAAC,SAAS,CAAC,KAAK,CAAC,CAAC,CAAC,QAAQ,CAAC,QAAQ,CAAC,CACxD,CAAA;IACL,CAAC;IAED;;;;OAIG;IACI,MAAM,CAAC,YAAY,CAKxB,KAAa;QACX,OAAO,IAAI,CAAC,KAAK,CAAC,eAAM,CAAC,IAAI,CAAC,KAAK,EAAE,QAAQ,CAAC,CAAC,QAAQ,EAAE,CAAM,CAAA;IACnE,CAAC;IAED;;;;OAIG;IACI,MAAM,CAAC,MAAM,CAAC,KAAa;QAC9B,OAAO,UAAU,CAAC,IAAI,CAAC,eAAM,CAAC,IAAI,CAAC,KAAK,EAAE,QAAQ,CAAC,CAAC,CAAA;IACxD,CAAC;CACJ;AA7CD,8BA6CC;AAED,SAAgB,iBAAiB,CAAC,MAAc;IAC5C,OAAO,MAAM,CAAC,OAAO,CAAC,KAAK,EAAE,GAAG,CAAC,CAAC,OAAO,CAAC,KAAK,EAAE,GAAG,CAAC,CAAC,OAAO,CAAC,IAAI,EAAE,EAAE,CAAC,CAAA;AAC3E,CAAC;AAFD,8CAEC"}
|
|
@@ -1 +0,0 @@
|
|
|
1
|
-
export declare const getValueByKeyAnyLevel: <T = unknown>(obj: Record<string, unknown>, key: string) => T | undefined;
|
|
@@ -1,20 +0,0 @@
|
|
|
1
|
-
"use strict";
|
|
2
|
-
Object.defineProperty(exports, "__esModule", { value: true });
|
|
3
|
-
exports.getValueByKeyAnyLevel = void 0;
|
|
4
|
-
const getValueByKeyAnyLevel = (obj, key) => {
|
|
5
|
-
// Check if the current object has the key
|
|
6
|
-
if (obj && obj.hasOwnProperty(key)) {
|
|
7
|
-
return obj[key];
|
|
8
|
-
}
|
|
9
|
-
// If not found in the current object, iterate over its properties
|
|
10
|
-
for (const prop in obj) {
|
|
11
|
-
if (obj.hasOwnProperty(prop) && typeof obj[prop] === 'object') {
|
|
12
|
-
const result = (0, exports.getValueByKeyAnyLevel)(obj[prop], key);
|
|
13
|
-
if (result !== undefined) {
|
|
14
|
-
return result;
|
|
15
|
-
}
|
|
16
|
-
}
|
|
17
|
-
}
|
|
18
|
-
};
|
|
19
|
-
exports.getValueByKeyAnyLevel = getValueByKeyAnyLevel;
|
|
20
|
-
//# sourceMappingURL=getValueByKeyAnyLevel.js.map
|
|
@@ -1 +0,0 @@
|
|
|
1
|
-
{"version":3,"file":"getValueByKeyAnyLevel.js","sourceRoot":"","sources":["../src/getValueByKeyAnyLevel.ts"],"names":[],"mappings":";;;AAAO,MAAM,qBAAqB,GAAG,CACjC,GAA4B,EAC5B,GAAW,EACE,EAAE;IACf,0CAA0C;IAC1C,IAAI,GAAG,IAAI,GAAG,CAAC,cAAc,CAAC,GAAG,CAAC,EAAE,CAAC;QACjC,OAAO,GAAG,CAAC,GAAG,CAAM,CAAA;IACxB,CAAC;IAED,kEAAkE;IAClE,KAAK,MAAM,IAAI,IAAI,GAAG,EAAE,CAAC;QACrB,IAAI,GAAG,CAAC,cAAc,CAAC,IAAI,CAAC,IAAI,OAAO,GAAG,CAAC,IAAI,CAAC,KAAK,QAAQ,EAAE,CAAC;YAC5D,MAAM,MAAM,GAAG,IAAA,6BAAqB,EAChC,GAAG,CAAC,IAAI,CAA4B,EACpC,GAAG,CACN,CAAA;YACD,IAAI,MAAM,KAAK,SAAS,EAAE,CAAC;gBACvB,OAAO,MAAW,CAAA;YACtB,CAAC;QACL,CAAC;IACL,CAAC;AACL,CAAC,CAAA;AArBY,QAAA,qBAAqB,yBAqBjC"}
|
package/build/index.d.ts
DELETED
package/build/index.js
DELETED
|
@@ -1,23 +0,0 @@
|
|
|
1
|
-
"use strict";
|
|
2
|
-
var __createBinding = (this && this.__createBinding) || (Object.create ? (function(o, m, k, k2) {
|
|
3
|
-
if (k2 === undefined) k2 = k;
|
|
4
|
-
var desc = Object.getOwnPropertyDescriptor(m, k);
|
|
5
|
-
if (!desc || ("get" in desc ? !m.__esModule : desc.writable || desc.configurable)) {
|
|
6
|
-
desc = { enumerable: true, get: function() { return m[k]; } };
|
|
7
|
-
}
|
|
8
|
-
Object.defineProperty(o, k2, desc);
|
|
9
|
-
}) : (function(o, m, k, k2) {
|
|
10
|
-
if (k2 === undefined) k2 = k;
|
|
11
|
-
o[k2] = m[k];
|
|
12
|
-
}));
|
|
13
|
-
var __exportStar = (this && this.__exportStar) || function(m, exports) {
|
|
14
|
-
for (var p in m) if (p !== "default" && !Object.prototype.hasOwnProperty.call(exports, p)) __createBinding(exports, m, p);
|
|
15
|
-
};
|
|
16
|
-
Object.defineProperty(exports, "__esModule", { value: true });
|
|
17
|
-
__exportStar(require("./base64url"), exports);
|
|
18
|
-
__exportStar(require("./assert"), exports);
|
|
19
|
-
__exportStar(require("./getValueByKeyAnyLevel"), exports);
|
|
20
|
-
__exportStar(require("./isPromise"), exports);
|
|
21
|
-
__exportStar(require("./object"), exports);
|
|
22
|
-
__exportStar(require("./simpleDeepEqual"), exports);
|
|
23
|
-
//# sourceMappingURL=index.js.map
|
package/build/index.js.map
DELETED
|
@@ -1 +0,0 @@
|
|
|
1
|
-
{"version":3,"file":"index.js","sourceRoot":"","sources":["../src/index.ts"],"names":[],"mappings":";;;;;;;;;;;;;;;;AAAA,8CAA2B;AAC3B,2CAAwB;AACxB,0DAAuC;AACvC,8CAA2B;AAC3B,2CAAwB;AACxB,oDAAiC"}
|
package/build/isPromise.d.ts
DELETED
|
@@ -1 +0,0 @@
|
|
|
1
|
-
export declare const isPromise: <T>(value: T | Promise<T>) => value is Promise<T>;
|
package/build/isPromise.js
DELETED
|
@@ -1,11 +0,0 @@
|
|
|
1
|
-
"use strict";
|
|
2
|
-
Object.defineProperty(exports, "__esModule", { value: true });
|
|
3
|
-
exports.isPromise = void 0;
|
|
4
|
-
const isPromise = (value) => value instanceof Promise ||
|
|
5
|
-
(value &&
|
|
6
|
-
typeof value === 'object' &&
|
|
7
|
-
value !== null &&
|
|
8
|
-
'then' in value &&
|
|
9
|
-
value.then === 'function');
|
|
10
|
-
exports.isPromise = isPromise;
|
|
11
|
-
//# sourceMappingURL=isPromise.js.map
|
package/build/isPromise.js.map
DELETED
|
@@ -1 +0,0 @@
|
|
|
1
|
-
{"version":3,"file":"isPromise.js","sourceRoot":"","sources":["../src/isPromise.ts"],"names":[],"mappings":";;;AAAO,MAAM,SAAS,GAAG,CAAI,KAAqB,EAAuB,EAAE,CACvE,KAAK,YAAY,OAAO;IACxB,CAAC,KAAK;QACF,OAAO,KAAK,KAAK,QAAQ;QACzB,KAAK,KAAK,IAAI;QACd,MAAM,IAAI,KAAK;QACf,KAAK,CAAC,IAAI,KAAK,UAAU,CAAC,CAAA;AANrB,QAAA,SAAS,aAMY"}
|
package/build/object.d.ts
DELETED
|
@@ -1,5 +0,0 @@
|
|
|
1
|
-
export declare function isObject(input: any): boolean;
|
|
2
|
-
export declare function getAllKeys(object: unknown, keys?: Array<string>): Array<string>;
|
|
3
|
-
export declare function deleteByPath(object: Record<string, unknown>, path: string[]): void;
|
|
4
|
-
export declare function getByPath(item: any[] | Record<string, unknown>, path: Array<string | number>): unknown;
|
|
5
|
-
export declare function hasByPath(item: any[] | Record<string, unknown>, path: Array<string | number>): boolean;
|
package/build/object.js
DELETED
|
@@ -1,76 +0,0 @@
|
|
|
1
|
-
"use strict";
|
|
2
|
-
Object.defineProperty(exports, "__esModule", { value: true });
|
|
3
|
-
exports.hasByPath = exports.getByPath = exports.deleteByPath = exports.getAllKeys = exports.isObject = void 0;
|
|
4
|
-
function isObject(input) {
|
|
5
|
-
return typeof input === 'object' && input !== null && !Array.isArray(input);
|
|
6
|
-
}
|
|
7
|
-
exports.isObject = isObject;
|
|
8
|
-
function getAllKeys(object, keys = []) {
|
|
9
|
-
if (typeof object !== 'object' || typeof object === null)
|
|
10
|
-
return keys;
|
|
11
|
-
const record = object;
|
|
12
|
-
const objectKeys = Object.keys(record);
|
|
13
|
-
const objectValues = Object.values(record);
|
|
14
|
-
keys.push(...objectKeys);
|
|
15
|
-
for (const objectValue of objectValues) {
|
|
16
|
-
if (typeof objectValue === 'object' &&
|
|
17
|
-
objectValue !== null &&
|
|
18
|
-
!Array.isArray(objectValue)) {
|
|
19
|
-
getAllKeys(objectValue, keys);
|
|
20
|
-
}
|
|
21
|
-
}
|
|
22
|
-
return keys;
|
|
23
|
-
}
|
|
24
|
-
exports.getAllKeys = getAllKeys;
|
|
25
|
-
function deleteByPath(object, path) {
|
|
26
|
-
let currentObject = object;
|
|
27
|
-
const parts = [...path];
|
|
28
|
-
const last = parts.pop();
|
|
29
|
-
for (const part of parts) {
|
|
30
|
-
currentObject = currentObject[part];
|
|
31
|
-
if (!currentObject) {
|
|
32
|
-
return;
|
|
33
|
-
}
|
|
34
|
-
}
|
|
35
|
-
if (last) {
|
|
36
|
-
delete currentObject[last];
|
|
37
|
-
}
|
|
38
|
-
}
|
|
39
|
-
exports.deleteByPath = deleteByPath;
|
|
40
|
-
function getByPath(item, path) {
|
|
41
|
-
let current = item;
|
|
42
|
-
for (const key of path) {
|
|
43
|
-
if (Array.isArray(current)) {
|
|
44
|
-
const keyAsNumber = Number(key);
|
|
45
|
-
if (isNaN(keyAsNumber)) {
|
|
46
|
-
throw new Error(`Unable to get ${path.join('.')} from array ${item}. ${key} is not a number.`);
|
|
47
|
-
}
|
|
48
|
-
if (keyAsNumber >= current.length) {
|
|
49
|
-
throw new Error(`Unable to get ${path.join('.')} from array ${item}. ${key} is out of bounds.`);
|
|
50
|
-
}
|
|
51
|
-
current = current[keyAsNumber];
|
|
52
|
-
}
|
|
53
|
-
else if (typeof current === 'object' && current !== null) {
|
|
54
|
-
if (!(key in current)) {
|
|
55
|
-
throw new Error(`Unable to get ${path.join('.')} from ${item}. ${key} does not exists in ${current}.`);
|
|
56
|
-
}
|
|
57
|
-
current = current[key];
|
|
58
|
-
}
|
|
59
|
-
else {
|
|
60
|
-
throw new Error(`Unable to get ${path.join('.')} from ${item}. ${key} is not an object or array.`);
|
|
61
|
-
}
|
|
62
|
-
}
|
|
63
|
-
return current;
|
|
64
|
-
}
|
|
65
|
-
exports.getByPath = getByPath;
|
|
66
|
-
function hasByPath(item, path) {
|
|
67
|
-
try {
|
|
68
|
-
getByPath(item, path);
|
|
69
|
-
return true;
|
|
70
|
-
}
|
|
71
|
-
catch (_a) {
|
|
72
|
-
return false;
|
|
73
|
-
}
|
|
74
|
-
}
|
|
75
|
-
exports.hasByPath = hasByPath;
|
|
76
|
-
//# sourceMappingURL=object.js.map
|
package/build/object.js.map
DELETED
|
@@ -1 +0,0 @@
|
|
|
1
|
-
{"version":3,"file":"object.js","sourceRoot":"","sources":["../src/object.ts"],"names":[],"mappings":";;;AAAA,SAAgB,QAAQ,CAAC,KAAU;IAC/B,OAAO,OAAO,KAAK,KAAK,QAAQ,IAAI,KAAK,KAAK,IAAI,IAAI,CAAC,KAAK,CAAC,OAAO,CAAC,KAAK,CAAC,CAAA;AAC/E,CAAC;AAFD,4BAEC;AAED,SAAgB,UAAU,CACtB,MAAe,EACf,OAAsB,EAAE;IAExB,IAAI,OAAO,MAAM,KAAK,QAAQ,IAAI,OAAO,MAAM,KAAK,IAAI;QAAE,OAAO,IAAI,CAAA;IACrE,MAAM,MAAM,GAAG,MAAiC,CAAA;IAEhD,MAAM,UAAU,GAAG,MAAM,CAAC,IAAI,CAAC,MAAM,CAAC,CAAA;IACtC,MAAM,YAAY,GAAG,MAAM,CAAC,MAAM,CAAC,MAAM,CAAC,CAAA;IAC1C,IAAI,CAAC,IAAI,CAAC,GAAG,UAAU,CAAC,CAAA;IAExB,KAAK,MAAM,WAAW,IAAI,YAAY,EAAE,CAAC;QACrC,IACI,OAAO,WAAW,KAAK,QAAQ;YAC/B,WAAW,KAAK,IAAI;YACpB,CAAC,KAAK,CAAC,OAAO,CAAC,WAAW,CAAC,EAC7B,CAAC;YACC,UAAU,CAAC,WAAW,EAAE,IAAI,CAAC,CAAA;QACjC,CAAC;IACL,CAAC;IAED,OAAO,IAAI,CAAA;AACf,CAAC;AAtBD,gCAsBC;AAED,SAAgB,YAAY,CAAC,MAA+B,EAAE,IAAc;IACxE,IAAI,aAAa,GAAwC,MAAM,CAAA;IAC/D,MAAM,KAAK,GAAG,CAAC,GAAG,IAAI,CAAC,CAAA;IACvB,MAAM,IAAI,GAAG,KAAK,CAAC,GAAG,EAAE,CAAA;IACxB,KAAK,MAAM,IAAI,IAAI,KAAK,EAAE,CAAC;QACvB,aAAa,GAAG,aAAa,CAAC,IAAI,CAEnB,CAAA;QAEf,IAAI,CAAC,aAAa,EAAE,CAAC;YACjB,OAAM;QACV,CAAC;IACL,CAAC;IACD,IAAI,IAAI,EAAE,CAAC;QACP,OAAO,aAAa,CAAC,IAAI,CAAC,CAAA;IAC9B,CAAC;AACL,CAAC;AAhBD,oCAgBC;AAED,SAAgB,SAAS,CACrB,IAAqC,EACrC,IAA4B;IAE5B,IAAI,OAAO,GAAQ,IAAI,CAAA;IACvB,KAAK,MAAM,GAAG,IAAI,IAAI,EAAE,CAAC;QACrB,IAAI,KAAK,CAAC,OAAO,CAAC,OAAO,CAAC,EAAE,CAAC;YACzB,MAAM,WAAW,GAAG,MAAM,CAAC,GAAG,CAAC,CAAA;YAC/B,IAAI,KAAK,CAAC,WAAW,CAAC,EAAE,CAAC;gBACrB,MAAM,IAAI,KAAK,CACX,iBAAiB,IAAI,CAAC,IAAI,CACtB,GAAG,CACN,eAAe,IAAI,KAAK,GAAG,mBAAmB,CAClD,CAAA;YACL,CAAC;YACD,IAAI,WAAW,IAAI,OAAO,CAAC,MAAM,EAAE,CAAC;gBAChC,MAAM,IAAI,KAAK,CACX,iBAAiB,IAAI,CAAC,IAAI,CACtB,GAAG,CACN,eAAe,IAAI,KAAK,GAAG,oBAAoB,CACnD,CAAA;YACL,CAAC;YACD,OAAO,GAAG,OAAO,CAAC,WAAW,CAAC,CAAA;QAClC,CAAC;aAAM,IAAI,OAAO,OAAO,KAAK,QAAQ,IAAI,OAAO,KAAK,IAAI,EAAE,CAAC;YACzD,IAAI,CAAC,CAAC,GAAG,IAAI,OAAO,CAAC,EAAE,CAAC;gBACpB,MAAM,IAAI,KAAK,CACX,iBAAiB,IAAI,CAAC,IAAI,CACtB,GAAG,CACN,SAAS,IAAI,KAAK,GAAG,uBAAuB,OAAO,GAAG,CAC1D,CAAA;YACL,CAAC;YACD,OAAO,GAAG,OAAO,CAAC,GAAG,CAAC,CAAA;QAC1B,CAAC;aAAM,CAAC;YACJ,MAAM,IAAI,KAAK,CACX,iBAAiB,IAAI,CAAC,IAAI,CACtB,GAAG,CACN,SAAS,IAAI,KAAK,GAAG,6BAA6B,CACtD,CAAA;QACL,CAAC;IACL,CAAC;IACD,OAAO,OAAO,CAAA;AAClB,CAAC;AAzCD,8BAyCC;AAED,SAAgB,SAAS,CACrB,IAAqC,EACrC,IAA4B;IAE5B,IAAI,CAAC;QACD,SAAS,CAAC,IAAI,EAAE,IAAI,CAAC,CAAA;QACrB,OAAO,IAAI,CAAA;IACf,CAAC;IAAC,WAAM,CAAC;QACL,OAAO,KAAK,CAAA;IAChB,CAAC;AACL,CAAC;AAVD,8BAUC"}
|
|
@@ -1 +0,0 @@
|
|
|
1
|
-
export declare function simpleDeepEqual(lhs: unknown, rhs: unknown): boolean;
|
package/build/simpleDeepEqual.js
DELETED
|
@@ -1,20 +0,0 @@
|
|
|
1
|
-
"use strict";
|
|
2
|
-
Object.defineProperty(exports, "__esModule", { value: true });
|
|
3
|
-
exports.simpleDeepEqual = void 0;
|
|
4
|
-
function simpleDeepEqual(lhs, rhs) {
|
|
5
|
-
if (lhs === rhs)
|
|
6
|
-
return true;
|
|
7
|
-
if (typeof lhs !== 'object' || typeof rhs !== 'object')
|
|
8
|
-
return false;
|
|
9
|
-
const l = Object.assign({}, lhs);
|
|
10
|
-
const r = Object.assign({}, rhs);
|
|
11
|
-
Object.keys(l).forEach((key) => l[key] === undefined && delete l[key]);
|
|
12
|
-
Object.keys(r).forEach((key) => r[key] === undefined && delete r[key]);
|
|
13
|
-
const keys1 = Object.keys(l);
|
|
14
|
-
const keys2 = Object.keys(r);
|
|
15
|
-
if (keys1.length !== keys2.length)
|
|
16
|
-
return false;
|
|
17
|
-
return keys1.every((key) => simpleDeepEqual(l[key], r[key]));
|
|
18
|
-
}
|
|
19
|
-
exports.simpleDeepEqual = simpleDeepEqual;
|
|
20
|
-
//# sourceMappingURL=simpleDeepEqual.js.map
|
|
@@ -1 +0,0 @@
|
|
|
1
|
-
{"version":3,"file":"simpleDeepEqual.js","sourceRoot":"","sources":["../src/simpleDeepEqual.ts"],"names":[],"mappings":";;;AAAA,SAAgB,eAAe,CAAC,GAAY,EAAE,GAAY;IACtD,IAAI,GAAG,KAAK,GAAG;QAAE,OAAO,IAAI,CAAA;IAE5B,IAAI,OAAO,GAAG,KAAK,QAAQ,IAAI,OAAO,GAAG,KAAK,QAAQ;QAAE,OAAO,KAAK,CAAA;IAEpE,MAAM,CAAC,GAAG,kBAAK,GAAG,CAA6B,CAAA;IAC/C,MAAM,CAAC,GAAG,kBAAK,GAAG,CAA6B,CAAA;IAE/C,MAAM,CAAC,IAAI,CAAC,CAAC,CAAC,CAAC,OAAO,CAAC,CAAC,GAAG,EAAE,EAAE,CAAC,CAAC,CAAC,GAAG,CAAC,KAAK,SAAS,IAAI,OAAO,CAAC,CAAC,GAAG,CAAC,CAAC,CAAA;IACtE,MAAM,CAAC,IAAI,CAAC,CAAC,CAAC,CAAC,OAAO,CAAC,CAAC,GAAG,EAAE,EAAE,CAAC,CAAC,CAAC,GAAG,CAAC,KAAK,SAAS,IAAI,OAAO,CAAC,CAAC,GAAG,CAAC,CAAC,CAAA;IAEtE,MAAM,KAAK,GAAG,MAAM,CAAC,IAAI,CAAC,CAAC,CAAC,CAAA;IAC5B,MAAM,KAAK,GAAG,MAAM,CAAC,IAAI,CAAC,CAAC,CAAC,CAAA;IAE5B,IAAI,KAAK,CAAC,MAAM,KAAK,KAAK,CAAC,MAAM;QAAE,OAAO,KAAK,CAAA;IAE/C,OAAO,KAAK,CAAC,KAAK,CAAC,CAAC,GAAG,EAAE,EAAE,CAAC,eAAe,CAAC,CAAC,CAAC,GAAG,CAAC,EAAE,CAAC,CAAC,GAAG,CAAC,CAAC,CAAC,CAAA;AAChE,CAAC;AAjBD,0CAiBC"}
|