@dwn-protocol/id-sdk 0.2.5 → 0.2.6
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/package.json +2 -3
- package/src/agent/app-data-store.ts +0 -365
- package/src/agent/did-manager.ts +0 -393
- package/src/agent/dwn-manager.ts +0 -548
- package/src/agent/identity-manager.ts +0 -165
- package/src/agent/index.ts +0 -19
- package/src/agent/json-rpc.ts +0 -107
- package/src/agent/key-manager.ts +0 -302
- package/src/agent/kms-local.ts +0 -412
- package/src/agent/outbox.ts +0 -128
- package/src/agent/rpc-client.ts +0 -223
- package/src/agent/store-managed-did.ts +0 -295
- package/src/agent/store-managed-identity.ts +0 -243
- package/src/agent/store-managed-key.ts +0 -754
- package/src/agent/sync-manager.ts +0 -631
- package/src/agent/test-managed-agent.ts +0 -299
- package/src/agent/types/agent.ts +0 -145
- package/src/agent/types/managed-key.ts +0 -442
- package/src/agent/utils.ts +0 -190
- package/src/common/convert.ts +0 -424
- package/src/common/index.ts +0 -9
- package/src/common/multicodec.ts +0 -176
- package/src/common/object.ts +0 -43
- package/src/common/stores.ts +0 -125
- package/src/common/stream-node.ts +0 -381
- package/src/common/stream.ts +0 -406
- package/src/common/type-utils.ts +0 -117
- package/src/common/types.ts +0 -48
- package/src/credentials/credential-bbs.ts +0 -419
- package/src/credentials/credential.ts +0 -324
- package/src/credentials/index.ts +0 -5
- package/src/credentials/presentation.ts +0 -182
- package/src/credentials/status-list.ts +0 -365
- package/src/credentials/utils.ts +0 -58
- package/src/credentials/validators.ts +0 -52
- package/src/crypto/algorithms-api/aes/base.ts +0 -49
- package/src/crypto/algorithms-api/aes/ctr.ts +0 -51
- package/src/crypto/algorithms-api/aes/index.ts +0 -2
- package/src/crypto/algorithms-api/crypto-algorithm.ts +0 -127
- package/src/crypto/algorithms-api/crypto-key.ts +0 -56
- package/src/crypto/algorithms-api/ec/base.ts +0 -39
- package/src/crypto/algorithms-api/ec/ecdh.ts +0 -53
- package/src/crypto/algorithms-api/ec/ecdsa.ts +0 -37
- package/src/crypto/algorithms-api/ec/eddsa.ts +0 -30
- package/src/crypto/algorithms-api/ec/index.ts +0 -4
- package/src/crypto/algorithms-api/errors.ts +0 -29
- package/src/crypto/algorithms-api/index.ts +0 -6
- package/src/crypto/algorithms-api/pbkdf/index.ts +0 -1
- package/src/crypto/algorithms-api/pbkdf/pbkdf2.ts +0 -91
- package/src/crypto/crypto-algorithms/aes-ctr.ts +0 -70
- package/src/crypto/crypto-algorithms/bbs.ts +0 -110
- package/src/crypto/crypto-algorithms/ecdh.ts +0 -115
- package/src/crypto/crypto-algorithms/ecdsa.ts +0 -111
- package/src/crypto/crypto-algorithms/eddsa.ts +0 -110
- package/src/crypto/crypto-algorithms/index.ts +0 -6
- package/src/crypto/crypto-algorithms/pbkdf2.ts +0 -54
- package/src/crypto/crypto-primitives/aes-ctr.ts +0 -131
- package/src/crypto/crypto-primitives/aes-gcm.ts +0 -138
- package/src/crypto/crypto-primitives/bbs.ts +0 -183
- package/src/crypto/crypto-primitives/concat-kdf.ts +0 -207
- package/src/crypto/crypto-primitives/ed25519.ts +0 -201
- package/src/crypto/crypto-primitives/index.ts +0 -10
- package/src/crypto/crypto-primitives/pbkdf2.ts +0 -78
- package/src/crypto/crypto-primitives/secp256k1.ts +0 -322
- package/src/crypto/crypto-primitives/x25519.ts +0 -101
- package/src/crypto/crypto-primitives/xchacha20-poly1305.ts +0 -46
- package/src/crypto/crypto-primitives/xchacha20.ts +0 -34
- package/src/crypto/index.ts +0 -8
- package/src/crypto/jose.ts +0 -948
- package/src/crypto/types/crypto-key.ts +0 -4
- package/src/crypto/types/iddwn-crypto.ts +0 -119
- package/src/crypto/utils.ts +0 -200
- package/src/did-api.ts +0 -72
- package/src/dids/dht.ts +0 -412
- package/src/dids/did-dht.ts +0 -436
- package/src/dids/did-ion.ts +0 -613
- package/src/dids/did-key.ts +0 -791
- package/src/dids/did-resolver.ts +0 -107
- package/src/dids/index.ts +0 -9
- package/src/dids/resolver-cache-level.ts +0 -82
- package/src/dids/resolver-cache-noop.ts +0 -25
- package/src/dids/types.ts +0 -278
- package/src/dids/utils.ts +0 -129
- package/src/dwn-api.ts +0 -584
- package/src/iddwn.ts +0 -241
- package/src/identity-agent/index.ts +0 -270
- package/src/index.ts +0 -26
- package/src/interfaces/metadata.ts +0 -163
- package/src/interfaces/queue.ts +0 -108
- package/src/interfaces/services.ts +0 -122
- package/src/interfaces/transactions.ts +0 -220
- package/src/protocol.ts +0 -68
- package/src/proxy-agent/index.ts +0 -255
- package/src/record.ts +0 -521
- package/src/service-options.ts +0 -62
- package/src/typings/decentralized-identity__ion-pow-sdk.d.ts +0 -7
- package/src/user-agent/index.ts +0 -295
- package/src/utils.ts +0 -29
- package/src/vc-api.ts +0 -505
package/src/common/convert.ts
DELETED
|
@@ -1,424 +0,0 @@
|
|
|
1
|
-
import type { Multibase } from 'multiformats';
|
|
2
|
-
|
|
3
|
-
import { base58btc } from 'multiformats/bases/base58';
|
|
4
|
-
import { base64url } from 'multiformats/bases/base64';
|
|
5
|
-
|
|
6
|
-
import { isAsyncIterable, isArrayBufferSlice, universalTypeOf } from './type-utils.js';
|
|
7
|
-
|
|
8
|
-
const textEncoder = new TextEncoder();
|
|
9
|
-
const textDecoder = new TextDecoder();
|
|
10
|
-
|
|
11
|
-
export class Convert {
|
|
12
|
-
data: any;
|
|
13
|
-
format: string;
|
|
14
|
-
|
|
15
|
-
constructor(data: any, format: string) {
|
|
16
|
-
this.data = data;
|
|
17
|
-
this.format = format;
|
|
18
|
-
}
|
|
19
|
-
|
|
20
|
-
static arrayBuffer(data: ArrayBuffer): Convert {
|
|
21
|
-
return new Convert(data, 'ArrayBuffer');
|
|
22
|
-
}
|
|
23
|
-
|
|
24
|
-
static asyncIterable(data: AsyncIterable<any>): Convert {
|
|
25
|
-
if (!isAsyncIterable(data)) {
|
|
26
|
-
throw new TypeError('Input must be of type AsyncIterable.');
|
|
27
|
-
}
|
|
28
|
-
return new Convert(data, 'AsyncIterable');
|
|
29
|
-
}
|
|
30
|
-
|
|
31
|
-
static base58Btc(data: string): Convert {
|
|
32
|
-
return new Convert(data, 'Base58Btc');
|
|
33
|
-
}
|
|
34
|
-
|
|
35
|
-
static base64Url(data: string): Convert {
|
|
36
|
-
return new Convert(data, 'Base64Url');
|
|
37
|
-
}
|
|
38
|
-
|
|
39
|
-
/**
|
|
40
|
-
* Reference:
|
|
41
|
-
* The BufferSource type is a TypeScript type that represents an ArrayBuffer
|
|
42
|
-
* or one of the ArrayBufferView types, such a TypedArray (e.g., Uint8Array)
|
|
43
|
-
* or a DataView.
|
|
44
|
-
*/
|
|
45
|
-
static bufferSource(data: BufferSource): Convert {
|
|
46
|
-
return new Convert(data, 'BufferSource');
|
|
47
|
-
}
|
|
48
|
-
|
|
49
|
-
static hex(data: string): Convert {
|
|
50
|
-
if (typeof data !== 'string') {
|
|
51
|
-
throw new TypeError('Hex input must be a string.');
|
|
52
|
-
}
|
|
53
|
-
if (data.length % 2 !== 0) {
|
|
54
|
-
throw new TypeError('Hex input must have an even number of characters.');
|
|
55
|
-
}
|
|
56
|
-
return new Convert(data, 'Hex');
|
|
57
|
-
}
|
|
58
|
-
|
|
59
|
-
static multibase(data: string): Convert {
|
|
60
|
-
return new Convert(data, 'Multibase');
|
|
61
|
-
}
|
|
62
|
-
|
|
63
|
-
static object(data: Record<string, any>): Convert {
|
|
64
|
-
return new Convert(data, 'Object');
|
|
65
|
-
}
|
|
66
|
-
|
|
67
|
-
static string(data: string): Convert {
|
|
68
|
-
return new Convert(data, 'String');
|
|
69
|
-
}
|
|
70
|
-
|
|
71
|
-
static uint8Array(data: Uint8Array): Convert {
|
|
72
|
-
return new Convert(data, 'Uint8Array');
|
|
73
|
-
}
|
|
74
|
-
|
|
75
|
-
toArrayBuffer(): ArrayBuffer {
|
|
76
|
-
switch (this.format) {
|
|
77
|
-
|
|
78
|
-
case 'Base58Btc': {
|
|
79
|
-
return base58btc.baseDecode(this.data).buffer;
|
|
80
|
-
}
|
|
81
|
-
|
|
82
|
-
case 'Base64Url': {
|
|
83
|
-
return base64url.baseDecode(this.data).buffer;
|
|
84
|
-
}
|
|
85
|
-
|
|
86
|
-
case 'BufferSource': {
|
|
87
|
-
const dataType = universalTypeOf(this.data);
|
|
88
|
-
if (dataType === 'ArrayBuffer') {
|
|
89
|
-
// Data is already an ArrayBuffer, No conversion is necessary.
|
|
90
|
-
return this.data;
|
|
91
|
-
} else if (ArrayBuffer.isView(this.data)) {
|
|
92
|
-
// Data is a DataView or a different TypedArray (e.g., Uint16Array).
|
|
93
|
-
if (isArrayBufferSlice(this.data)) {
|
|
94
|
-
// Data is a slice of an ArrayBuffer. Return a new ArrayBuffer or ArrayBufferView of the same slice.
|
|
95
|
-
return this.data.buffer.slice(this.data.byteOffset, this.data.byteOffset + this.data.byteLength);
|
|
96
|
-
} else {
|
|
97
|
-
// Data is a whole ArrayBuffer viewed as a different TypedArray or DataView. Return the whole ArrayBuffer.
|
|
98
|
-
return this.data.buffer;
|
|
99
|
-
}
|
|
100
|
-
} else {
|
|
101
|
-
throw new TypeError(`${this.format} value is not of type: ArrayBuffer, DataView, or TypedArray.`);
|
|
102
|
-
}
|
|
103
|
-
}
|
|
104
|
-
|
|
105
|
-
case 'Hex': {
|
|
106
|
-
return this.toUint8Array().buffer;
|
|
107
|
-
}
|
|
108
|
-
|
|
109
|
-
case 'String': {
|
|
110
|
-
return this.toUint8Array().buffer;
|
|
111
|
-
}
|
|
112
|
-
|
|
113
|
-
case 'Uint8Array': {
|
|
114
|
-
return this.data.buffer;
|
|
115
|
-
}
|
|
116
|
-
|
|
117
|
-
default:
|
|
118
|
-
throw new TypeError(`Conversion from ${this.format} to ArrayBuffer is not supported.`);
|
|
119
|
-
}
|
|
120
|
-
}
|
|
121
|
-
|
|
122
|
-
async toArrayBufferAsync(): Promise<ArrayBuffer> {
|
|
123
|
-
switch (this.format) {
|
|
124
|
-
case 'AsyncIterable': {
|
|
125
|
-
const blob = await this.toBlobAsync();
|
|
126
|
-
return await blob.arrayBuffer();
|
|
127
|
-
}
|
|
128
|
-
|
|
129
|
-
default:
|
|
130
|
-
throw new TypeError(`Asynchronous conversion from ${this.format} to ArrayBuffer is not supported.`);
|
|
131
|
-
}
|
|
132
|
-
}
|
|
133
|
-
|
|
134
|
-
toBase58Btc(): string {
|
|
135
|
-
switch (this.format) {
|
|
136
|
-
|
|
137
|
-
case 'ArrayBuffer': {
|
|
138
|
-
const u8a = new Uint8Array(this.data);
|
|
139
|
-
return base58btc.baseEncode(u8a);
|
|
140
|
-
}
|
|
141
|
-
|
|
142
|
-
case 'Multibase': {
|
|
143
|
-
return this.data.substring(1);
|
|
144
|
-
}
|
|
145
|
-
|
|
146
|
-
case 'Uint8Array': {
|
|
147
|
-
return base58btc.baseEncode(this.data);
|
|
148
|
-
}
|
|
149
|
-
|
|
150
|
-
default:
|
|
151
|
-
throw new TypeError(`Conversion from ${this.format} to Base58Btc is not supported.`);
|
|
152
|
-
}
|
|
153
|
-
}
|
|
154
|
-
|
|
155
|
-
toBase64Url(): string {
|
|
156
|
-
switch (this.format) {
|
|
157
|
-
|
|
158
|
-
case 'ArrayBuffer': {
|
|
159
|
-
const u8a = new Uint8Array(this.data);
|
|
160
|
-
return base64url.baseEncode(u8a);
|
|
161
|
-
}
|
|
162
|
-
|
|
163
|
-
case 'BufferSource': {
|
|
164
|
-
const u8a = this.toUint8Array();
|
|
165
|
-
return base64url.baseEncode(u8a);
|
|
166
|
-
}
|
|
167
|
-
|
|
168
|
-
case 'Object': {
|
|
169
|
-
const string = JSON.stringify(this.data);
|
|
170
|
-
const u8a = textEncoder.encode(string);
|
|
171
|
-
return base64url.baseEncode(u8a);
|
|
172
|
-
}
|
|
173
|
-
|
|
174
|
-
case 'String': {
|
|
175
|
-
const u8a = textEncoder.encode(this.data);
|
|
176
|
-
return base64url.baseEncode(u8a);
|
|
177
|
-
}
|
|
178
|
-
|
|
179
|
-
case 'Uint8Array': {
|
|
180
|
-
return base64url.baseEncode(this.data);
|
|
181
|
-
}
|
|
182
|
-
|
|
183
|
-
default:
|
|
184
|
-
throw new TypeError(`Conversion from ${this.format} to Base64Url is not supported.`);
|
|
185
|
-
}
|
|
186
|
-
}
|
|
187
|
-
|
|
188
|
-
async toBlobAsync(): Promise<Blob> {
|
|
189
|
-
switch (this.format) {
|
|
190
|
-
case 'AsyncIterable': {
|
|
191
|
-
// Initialize an array to hold the chunks from the AsyncIterable.
|
|
192
|
-
const chunks = [];
|
|
193
|
-
|
|
194
|
-
// Asynchronously iterate over each chunk in the AsyncIterable.
|
|
195
|
-
for await (const chunk of (this.data as AsyncIterable<any>)) {
|
|
196
|
-
// Append each chunk to the chunks array. These chunks can be of any type, typically binary data or text.
|
|
197
|
-
chunks.push(chunk);
|
|
198
|
-
}
|
|
199
|
-
|
|
200
|
-
// Create a new Blob from the aggregated chunks.
|
|
201
|
-
// The Blob constructor combines these chunks into a single Blob object.
|
|
202
|
-
const blob = new Blob(chunks);
|
|
203
|
-
|
|
204
|
-
return blob;
|
|
205
|
-
}
|
|
206
|
-
|
|
207
|
-
default:
|
|
208
|
-
throw new TypeError(`Asynchronous conversion from ${this.format} to Blob is not supported.`);
|
|
209
|
-
}
|
|
210
|
-
}
|
|
211
|
-
|
|
212
|
-
toHex(): string {
|
|
213
|
-
// pre-calculating Hex values improves runtime by 6-10x.
|
|
214
|
-
const hexes = Array.from({ length: 256 }, (v, i) => i.toString(16).padStart(2, '0'));
|
|
215
|
-
|
|
216
|
-
switch (this.format) {
|
|
217
|
-
|
|
218
|
-
case 'ArrayBuffer': {
|
|
219
|
-
const u8a = this.toUint8Array();
|
|
220
|
-
return Convert.uint8Array(u8a).toHex();
|
|
221
|
-
}
|
|
222
|
-
|
|
223
|
-
case 'Base64Url': {
|
|
224
|
-
const u8a = this.toUint8Array();
|
|
225
|
-
return Convert.uint8Array(u8a).toHex();
|
|
226
|
-
}
|
|
227
|
-
|
|
228
|
-
case 'Uint8Array': {
|
|
229
|
-
let hex = '';
|
|
230
|
-
for (let i = 0; i < this.data.length; i++) {
|
|
231
|
-
hex += hexes[this.data[i]];
|
|
232
|
-
}
|
|
233
|
-
return hex;
|
|
234
|
-
}
|
|
235
|
-
|
|
236
|
-
default:
|
|
237
|
-
throw new TypeError(`Conversion from ${this.format} to Hex is not supported.`);
|
|
238
|
-
}
|
|
239
|
-
}
|
|
240
|
-
|
|
241
|
-
toMultibase(): Multibase<any> {
|
|
242
|
-
switch (this.format) {
|
|
243
|
-
case 'Base58Btc': {
|
|
244
|
-
return `z${this.data}`;
|
|
245
|
-
}
|
|
246
|
-
|
|
247
|
-
default:
|
|
248
|
-
throw new TypeError(`Conversion from ${this.format} to Multibase is not supported.`);
|
|
249
|
-
}
|
|
250
|
-
}
|
|
251
|
-
|
|
252
|
-
toObject(): object {
|
|
253
|
-
switch (this.format) {
|
|
254
|
-
|
|
255
|
-
case 'Base64Url': {
|
|
256
|
-
const u8a = base64url.baseDecode(this.data);
|
|
257
|
-
const text = textDecoder.decode(u8a);
|
|
258
|
-
return JSON.parse(text);
|
|
259
|
-
}
|
|
260
|
-
|
|
261
|
-
case 'String': {
|
|
262
|
-
return JSON.parse(this.data);
|
|
263
|
-
}
|
|
264
|
-
|
|
265
|
-
case 'Uint8Array': {
|
|
266
|
-
const text = textDecoder.decode(this.data);
|
|
267
|
-
return JSON.parse(text);
|
|
268
|
-
}
|
|
269
|
-
|
|
270
|
-
default:
|
|
271
|
-
throw new TypeError(`Conversion from ${this.format} to Object is not supported.`);
|
|
272
|
-
}
|
|
273
|
-
}
|
|
274
|
-
|
|
275
|
-
async toObjectAsync(): Promise<any> {
|
|
276
|
-
switch (this.format) {
|
|
277
|
-
case 'AsyncIterable': {
|
|
278
|
-
// Convert the AsyncIterable to a String.
|
|
279
|
-
const text = await this.toStringAsync();
|
|
280
|
-
|
|
281
|
-
// Parse the string as JSON. This step assumes that the string represents a valid JSON structure.
|
|
282
|
-
// JSON.parse() will convert the string into a corresponding JavaScript object.
|
|
283
|
-
const json = JSON.parse(text);
|
|
284
|
-
|
|
285
|
-
// Return the parsed JavaScript object. The type of this object will depend on the structure
|
|
286
|
-
// of the JSON in the stream. It could be an object, array, string, number, etc.
|
|
287
|
-
return json;
|
|
288
|
-
}
|
|
289
|
-
|
|
290
|
-
default:
|
|
291
|
-
throw new TypeError(`Asynchronous conversion from ${this.format} to Object is not supported.`);
|
|
292
|
-
}
|
|
293
|
-
}
|
|
294
|
-
|
|
295
|
-
toString(): string {
|
|
296
|
-
switch (this.format) {
|
|
297
|
-
|
|
298
|
-
case 'ArrayBuffer': {
|
|
299
|
-
return textDecoder.decode(this.data);
|
|
300
|
-
}
|
|
301
|
-
|
|
302
|
-
case 'Base64Url': {
|
|
303
|
-
const u8a = base64url.baseDecode(this.data);
|
|
304
|
-
return textDecoder.decode(u8a);
|
|
305
|
-
}
|
|
306
|
-
|
|
307
|
-
case 'Object': {
|
|
308
|
-
return JSON.stringify(this.data);
|
|
309
|
-
}
|
|
310
|
-
|
|
311
|
-
case 'Uint8Array': {
|
|
312
|
-
return textDecoder.decode(this.data);
|
|
313
|
-
}
|
|
314
|
-
|
|
315
|
-
default:
|
|
316
|
-
throw new TypeError(`Conversion from ${this.format} to String is not supported.`);
|
|
317
|
-
}
|
|
318
|
-
}
|
|
319
|
-
|
|
320
|
-
async toStringAsync(): Promise<string> {
|
|
321
|
-
switch (this.format) {
|
|
322
|
-
case 'AsyncIterable': {
|
|
323
|
-
// Initialize an empty string to accumulate the decoded text.
|
|
324
|
-
let str = '';
|
|
325
|
-
|
|
326
|
-
// Iterate over the chunks from the AsyncIterable.
|
|
327
|
-
for await (const chunk of (this.data as AsyncIterable<any>)) {
|
|
328
|
-
// If the chunk is already a string, concatenate it directly.
|
|
329
|
-
if (typeof chunk === 'string')
|
|
330
|
-
str += chunk;
|
|
331
|
-
else
|
|
332
|
-
// If the chunk is a Uint8Array or similar, use the decoder to convert it to a string.
|
|
333
|
-
// The `stream: true` option lets the decoder handle multi-byte characters spanning
|
|
334
|
-
// multiple chunks.
|
|
335
|
-
str += textDecoder.decode(chunk, { stream: true });
|
|
336
|
-
}
|
|
337
|
-
|
|
338
|
-
// Finalize the decoding process to handle any remaining bytes and signal the end of the stream.
|
|
339
|
-
// The `stream: false` option flushes the decoder's internal state.
|
|
340
|
-
str += textDecoder.decode(undefined, { stream: false });
|
|
341
|
-
|
|
342
|
-
// Return the accumulated string.
|
|
343
|
-
return str;
|
|
344
|
-
}
|
|
345
|
-
|
|
346
|
-
default:
|
|
347
|
-
throw new TypeError(`Asynchronous conversion from ${this.format} to String is not supported.`);
|
|
348
|
-
}
|
|
349
|
-
}
|
|
350
|
-
|
|
351
|
-
toUint8Array(): Uint8Array {
|
|
352
|
-
switch (this.format) {
|
|
353
|
-
|
|
354
|
-
case 'ArrayBuffer': {
|
|
355
|
-
// Çreate Uint8Array as a view on the ArrayBuffer.
|
|
356
|
-
// Note: The Uint8Array shares the same memory as the ArrayBuffer, so this operation is very efficient.
|
|
357
|
-
return new Uint8Array(this.data);
|
|
358
|
-
}
|
|
359
|
-
|
|
360
|
-
case 'Base58Btc': {
|
|
361
|
-
return base58btc.baseDecode(this.data);
|
|
362
|
-
}
|
|
363
|
-
|
|
364
|
-
case 'Base64Url': {
|
|
365
|
-
return base64url.baseDecode(this.data);
|
|
366
|
-
}
|
|
367
|
-
|
|
368
|
-
case 'BufferSource': {
|
|
369
|
-
const dataType = universalTypeOf(this.data);
|
|
370
|
-
if (dataType === 'Uint8Array') {
|
|
371
|
-
// Data is already a Uint8Array. No conversion is necessary.
|
|
372
|
-
// Note: Uint8Array is a type of BufferSource.
|
|
373
|
-
return this.data;
|
|
374
|
-
} else if (dataType === 'ArrayBuffer') {
|
|
375
|
-
// Data is an ArrayBuffer, create Uint8Array as a view on the ArrayBuffer.
|
|
376
|
-
// Note: The Uint8Array shares the same memory as the ArrayBuffer, so this operation is very efficient.
|
|
377
|
-
return new Uint8Array(this.data);
|
|
378
|
-
} else if (ArrayBuffer.isView(this.data)) {
|
|
379
|
-
// Data is a DataView or a different TypedArray (e.g., Uint16Array).
|
|
380
|
-
return new Uint8Array(this.data.buffer, this.data.byteOffset, this.data.byteLength);
|
|
381
|
-
} else {
|
|
382
|
-
throw new TypeError(`${this.format} value is not of type: ArrayBuffer, DataView, or TypedArray.`);
|
|
383
|
-
}
|
|
384
|
-
}
|
|
385
|
-
|
|
386
|
-
case 'Hex': {
|
|
387
|
-
const u8a = new Uint8Array(this.data.length / 2);
|
|
388
|
-
for (let i = 0; i < this.data.length; i += 2) {
|
|
389
|
-
const byteValue = parseInt(this.data.substring(i, i + 2), 16);
|
|
390
|
-
if (isNaN(byteValue)) {
|
|
391
|
-
throw new TypeError('Input is not a valid hexadecimal string.');
|
|
392
|
-
}
|
|
393
|
-
u8a[i / 2] = byteValue;
|
|
394
|
-
}
|
|
395
|
-
return u8a;
|
|
396
|
-
}
|
|
397
|
-
|
|
398
|
-
case 'Object': {
|
|
399
|
-
const string = JSON.stringify(this.data);
|
|
400
|
-
return textEncoder.encode(string);
|
|
401
|
-
}
|
|
402
|
-
|
|
403
|
-
case 'String': {
|
|
404
|
-
return textEncoder.encode(this.data);
|
|
405
|
-
}
|
|
406
|
-
|
|
407
|
-
default:
|
|
408
|
-
throw new TypeError(`Conversion from ${this.format} to Uint8Array is not supported.`);
|
|
409
|
-
}
|
|
410
|
-
}
|
|
411
|
-
|
|
412
|
-
async toUint8ArrayAsync(): Promise<Uint8Array> {
|
|
413
|
-
switch (this.format) {
|
|
414
|
-
case 'AsyncIterable': {
|
|
415
|
-
const arrayBuffer = await this.toArrayBufferAsync();
|
|
416
|
-
return new Uint8Array(arrayBuffer);
|
|
417
|
-
}
|
|
418
|
-
|
|
419
|
-
default:
|
|
420
|
-
throw new TypeError(`Asynchronous conversion from ${this.format} to Uint8Array is not supported.`);
|
|
421
|
-
}
|
|
422
|
-
}
|
|
423
|
-
|
|
424
|
-
}
|
package/src/common/index.ts
DELETED
|
@@ -1,9 +0,0 @@
|
|
|
1
|
-
export type * from './types.js';
|
|
2
|
-
|
|
3
|
-
export * from './convert.js';
|
|
4
|
-
export * from './multicodec.js';
|
|
5
|
-
export * from './object.js';
|
|
6
|
-
export * from './stores.js';
|
|
7
|
-
export * from './stream.js';
|
|
8
|
-
export * from './stream-node.js';
|
|
9
|
-
export * from './type-utils.js';
|
package/src/common/multicodec.ts
DELETED
|
@@ -1,176 +0,0 @@
|
|
|
1
|
-
import { varint } from 'multiformats';
|
|
2
|
-
|
|
3
|
-
export type MulticodecCode = number;
|
|
4
|
-
|
|
5
|
-
export type MulticodecDefinition<MulticodecCode> = {
|
|
6
|
-
code: MulticodecCode;
|
|
7
|
-
// codeBytes: Uint8Array;
|
|
8
|
-
name: string;
|
|
9
|
-
}
|
|
10
|
-
|
|
11
|
-
/**
|
|
12
|
-
* The `Multicodec` class provides an interface to prepend binary data
|
|
13
|
-
* with a prefix that identifies the data that follows.
|
|
14
|
-
* https://github.com/multiformats/multicodec/blob/master/table.csv
|
|
15
|
-
*
|
|
16
|
-
* Multicodec is a self-describing multiformat, it wraps other formats with
|
|
17
|
-
* a tiny bit of self-description. A multicodec identifier is a
|
|
18
|
-
* varint (variable integer) that indicates the format of the data.
|
|
19
|
-
*
|
|
20
|
-
* The canonical table of multicodecs can be access at the following URL:
|
|
21
|
-
* https://github.com/multiformats/multicodec/blob/master/table.csv
|
|
22
|
-
*
|
|
23
|
-
* Example usage:
|
|
24
|
-
*
|
|
25
|
-
* ```ts
|
|
26
|
-
* Multicodec.registerCodec({ code: 0xed, name: 'ed25519-pub' });
|
|
27
|
-
* const prefixedData = Multicodec.addPrefix({ code: 0xed, data: new Uint8Array(32) });
|
|
28
|
-
* ```
|
|
29
|
-
*/
|
|
30
|
-
export class Multicodec {
|
|
31
|
-
/**
|
|
32
|
-
* A static field containing a map of codec codes to their corresponding names.
|
|
33
|
-
*/
|
|
34
|
-
static codeToName = new Map<MulticodecCode, string>();
|
|
35
|
-
|
|
36
|
-
/**
|
|
37
|
-
* A static field containing a map of codec names to their corresponding codes.
|
|
38
|
-
*/
|
|
39
|
-
static nameToCode = new Map<string, MulticodecCode>();
|
|
40
|
-
|
|
41
|
-
/**
|
|
42
|
-
* Adds a multicodec prefix to input data.
|
|
43
|
-
*
|
|
44
|
-
* @param options - The options for adding a prefix.
|
|
45
|
-
* @param options.code - The codec code. Either the code or name must be provided.
|
|
46
|
-
* @param options.name - The codec name. Either the code or name must be provided.
|
|
47
|
-
* @param options.data - The data to be prefixed.
|
|
48
|
-
* @returns The data with the added prefix as a Uint8Array.
|
|
49
|
-
*/
|
|
50
|
-
public static addPrefix(options: {
|
|
51
|
-
code?: MulticodecCode,
|
|
52
|
-
data: Uint8Array,
|
|
53
|
-
name?: string,
|
|
54
|
-
}): Uint8Array {
|
|
55
|
-
let { code, data, name } = options;
|
|
56
|
-
|
|
57
|
-
if (!(name ? !code : code)) {
|
|
58
|
-
throw new Error(`Either 'name' or 'code' must be defined, but not both.`);
|
|
59
|
-
}
|
|
60
|
-
|
|
61
|
-
// If code was given, confirm it exists, or lookup code by name.
|
|
62
|
-
code = Multicodec.codeToName.has(code!) ? code : Multicodec.nameToCode.get(name!);
|
|
63
|
-
|
|
64
|
-
// Throw error if a registered Codec wasn't found.
|
|
65
|
-
if (code === undefined) {
|
|
66
|
-
throw new Error(`Unsupported multicodec: ${options.name ?? options.code}`);
|
|
67
|
-
}
|
|
68
|
-
|
|
69
|
-
// Create a new array to store the prefix and input data.
|
|
70
|
-
const prefixLength = varint.encodingLength(code);
|
|
71
|
-
const dataWithPrefix = new Uint8Array(prefixLength + data.byteLength);
|
|
72
|
-
dataWithPrefix.set(data, prefixLength);
|
|
73
|
-
|
|
74
|
-
// Prepend the prefix.
|
|
75
|
-
varint.encodeTo(code, dataWithPrefix);
|
|
76
|
-
|
|
77
|
-
return dataWithPrefix;
|
|
78
|
-
}
|
|
79
|
-
|
|
80
|
-
/**
|
|
81
|
-
* Get the Multicodec code from given prefixed data.
|
|
82
|
-
*
|
|
83
|
-
* @param options - The options for getting the codec code.
|
|
84
|
-
* @param options.prefixedData - The data to extract the codec code from.
|
|
85
|
-
* @returns - The Multicodec code as a number.
|
|
86
|
-
*/
|
|
87
|
-
public static getCodeFromData(options: {
|
|
88
|
-
prefixedData: Uint8Array
|
|
89
|
-
}): MulticodecCode {
|
|
90
|
-
const { prefixedData } = options;
|
|
91
|
-
const [code, _] = varint.decode(prefixedData);
|
|
92
|
-
|
|
93
|
-
return code;
|
|
94
|
-
}
|
|
95
|
-
|
|
96
|
-
/**
|
|
97
|
-
* Get the Multicodec code from given Multicodec name.
|
|
98
|
-
*
|
|
99
|
-
* @param options - The options for getting the codec code.
|
|
100
|
-
* @param options.name - The name to lookup.
|
|
101
|
-
* @returns - The Multicodec code as a number.
|
|
102
|
-
*/
|
|
103
|
-
public static getCodeFromName(options: {
|
|
104
|
-
name: string
|
|
105
|
-
}): MulticodecCode {
|
|
106
|
-
const { name } = options;
|
|
107
|
-
|
|
108
|
-
// Throw error if a registered Codec wasn't found.
|
|
109
|
-
const code = Multicodec.nameToCode.get(name);
|
|
110
|
-
if (code === undefined) {
|
|
111
|
-
throw new Error(`Unsupported multicodec: ${name}`);
|
|
112
|
-
}
|
|
113
|
-
|
|
114
|
-
return code;
|
|
115
|
-
}
|
|
116
|
-
|
|
117
|
-
/**
|
|
118
|
-
* Get the Multicodec name from given Multicodec code.
|
|
119
|
-
*
|
|
120
|
-
* @param options - The options for getting the codec name.
|
|
121
|
-
* @param options.name - The code to lookup.
|
|
122
|
-
* @returns - The Multicodec name as a string.
|
|
123
|
-
*/
|
|
124
|
-
public static getNameFromCode(options: {
|
|
125
|
-
code: MulticodecCode
|
|
126
|
-
}): string {
|
|
127
|
-
const { code } = options;
|
|
128
|
-
|
|
129
|
-
// Throw error if a registered Codec wasn't found.
|
|
130
|
-
const name = Multicodec.codeToName.get(code);
|
|
131
|
-
if (name === undefined) {
|
|
132
|
-
throw new Error(`Unsupported multicodec: ${code}`);
|
|
133
|
-
}
|
|
134
|
-
|
|
135
|
-
return name;
|
|
136
|
-
}
|
|
137
|
-
|
|
138
|
-
/**
|
|
139
|
-
* Registers a new codec in the Multicodec class.
|
|
140
|
-
*
|
|
141
|
-
* @param codec - The codec to be registered.
|
|
142
|
-
*/
|
|
143
|
-
public static registerCodec(codec: MulticodecDefinition<MulticodecCode>) {
|
|
144
|
-
Multicodec.codeToName.set(codec.code, codec.name);
|
|
145
|
-
Multicodec.nameToCode.set(codec.name, codec.code);
|
|
146
|
-
}
|
|
147
|
-
|
|
148
|
-
/**
|
|
149
|
-
* Returns the data with the Multicodec prefix removed.
|
|
150
|
-
*
|
|
151
|
-
* @param refixedData - The data to extract the codec code from.
|
|
152
|
-
* @returns {Uint8Array}
|
|
153
|
-
*/
|
|
154
|
-
public static removePrefix(options: {
|
|
155
|
-
prefixedData: Uint8Array
|
|
156
|
-
}): { code: MulticodecCode, name: string, data: Uint8Array } {
|
|
157
|
-
const { prefixedData } = options;
|
|
158
|
-
const [code, codeByteLength] = varint.decode(prefixedData);
|
|
159
|
-
|
|
160
|
-
// Throw error if a registered Codec wasn't found.
|
|
161
|
-
const name = Multicodec.codeToName.get(code);
|
|
162
|
-
if (name === undefined) {
|
|
163
|
-
throw new Error(`Unsupported multicodec: ${code}`);
|
|
164
|
-
}
|
|
165
|
-
|
|
166
|
-
return { code, data: prefixedData.slice(codeByteLength), name };
|
|
167
|
-
}
|
|
168
|
-
}
|
|
169
|
-
|
|
170
|
-
// Pre-defined registered codecs:
|
|
171
|
-
Multicodec.registerCodec({ code: 0xed, name: 'ed25519-pub' });
|
|
172
|
-
Multicodec.registerCodec({ code: 0x1300, name: 'ed25519-priv' });
|
|
173
|
-
Multicodec.registerCodec({ code: 0xec, name: 'x25519-pub' });
|
|
174
|
-
Multicodec.registerCodec({ code: 0x1302, name: 'x25519-priv' });
|
|
175
|
-
Multicodec.registerCodec({ code: 0xe7, name: 'secp256k1-pub' });
|
|
176
|
-
Multicodec.registerCodec({ code: 0x1301, name: 'secp256k1-priv' });
|
package/src/common/object.ts
DELETED
|
@@ -1,43 +0,0 @@
|
|
|
1
|
-
/**
|
|
2
|
-
* Checks whether the given object has any properties.
|
|
3
|
-
*/
|
|
4
|
-
export function isEmptyObject(obj: unknown): boolean {
|
|
5
|
-
if (typeof obj !== 'object' || obj === null) {
|
|
6
|
-
return false;
|
|
7
|
-
}
|
|
8
|
-
|
|
9
|
-
if (Object.getOwnPropertySymbols(obj).length > 0) {
|
|
10
|
-
return false;
|
|
11
|
-
}
|
|
12
|
-
|
|
13
|
-
return Object.keys(obj).length === 0;
|
|
14
|
-
}
|
|
15
|
-
|
|
16
|
-
/**
|
|
17
|
-
* Recursively removes all properties with an empty object or array as its value from the given object.
|
|
18
|
-
*/
|
|
19
|
-
export function removeEmptyObjects(obj: Record<string, unknown>): void {
|
|
20
|
-
Object.keys(obj).forEach(key => {
|
|
21
|
-
if (typeof(obj[key]) === 'object') {
|
|
22
|
-
// recursive remove empty object or array properties in nested objects
|
|
23
|
-
removeEmptyObjects(obj[key] as Record<string, unknown>);
|
|
24
|
-
}
|
|
25
|
-
|
|
26
|
-
if (isEmptyObject(obj[key])) {
|
|
27
|
-
delete obj[key];
|
|
28
|
-
}
|
|
29
|
-
});
|
|
30
|
-
}
|
|
31
|
-
|
|
32
|
-
/**
|
|
33
|
-
* Recursively removes all properties with `undefined` as its value from the given object.
|
|
34
|
-
*/
|
|
35
|
-
export function removeUndefinedProperties(obj: Record<string, unknown>): void {
|
|
36
|
-
Object.keys(obj).forEach(key => {
|
|
37
|
-
if (obj[key] === undefined) {
|
|
38
|
-
delete obj[key];
|
|
39
|
-
} else if (typeof(obj[key]) === 'object') {
|
|
40
|
-
removeUndefinedProperties(obj[key] as Record<string, unknown>); // recursive remove `undefined` properties in nested objects
|
|
41
|
-
}
|
|
42
|
-
});
|
|
43
|
-
}
|