generated from TBD54566975/tbd-project-template
-
Notifications
You must be signed in to change notification settings - Fork 56
/
Copy pathconvert.ts
307 lines (247 loc) · 8.33 KB
/
convert.ts
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
import type { Multibase } from 'multiformats';
import { base58btc } from 'multiformats/bases/base58';
import { base64url } from 'multiformats/bases/base64';
import { isArrayBufferSlice, universalTypeOf } from './type-utils.js';
const textEncoder = new TextEncoder();
const textDecoder = new TextDecoder();
export class Convert {
data: any;
format: string;
constructor(data: any, format: string) {
this.data = data;
this.format = format;
}
static arrayBuffer(data: ArrayBuffer): Convert {
return new Convert(data, 'ArrayBuffer');
}
static base58Btc(data: string): Convert {
return new Convert(data, 'Base58Btc');
}
static base64Url(data: string): Convert {
return new Convert(data, 'Base64Url');
}
/**
* Reference:
* The BufferSource type is a TypeScript type that represents an ArrayBuffer
* or one of the ArrayBufferView types, such a TypedArray (e.g., Uint8Array)
* or a DataView.
*/
static bufferSource(data: BufferSource): Convert {
return new Convert(data, 'BufferSource');
}
static hex(data: string): Convert {
if (typeof data !== 'string') {
throw new TypeError('Hex input must be a string.');
}
if (data.length % 2 !== 0) {
throw new TypeError('Hex input must have an even number of characters.');
}
return new Convert(data, 'Hex');
}
static multibase(data: string): Convert {
return new Convert(data, 'Multibase');
}
static object(data: Record<string, any>): Convert {
return new Convert(data, 'Object');
}
static string(data: string): Convert {
return new Convert(data, 'String');
}
static uint8Array(data: Uint8Array): Convert {
return new Convert(data, 'Uint8Array');
}
toArrayBuffer(): ArrayBuffer {
switch (this.format) {
case 'Base58Btc': {
return base58btc.baseDecode(this.data).buffer;
}
case 'Base64Url': {
return base64url.baseDecode(this.data).buffer;
}
case 'BufferSource': {
const dataType = universalTypeOf(this.data);
if (dataType === 'ArrayBuffer') {
// Data is already an ArrayBuffer, No conversion is necessary.
return this.data;
} else if (ArrayBuffer.isView(this.data)) {
// Data is a DataView or a different TypedArray (e.g., Uint16Array).
if (isArrayBufferSlice(this.data)) {
// Data is a slice of an ArrayBuffer. Return a new ArrayBuffer or ArrayBufferView of the same slice.
return this.data.buffer.slice(this.data.byteOffset, this.data.byteOffset + this.data.byteLength);
} else {
// Data is a whole ArrayBuffer viewed as a different TypedArray or DataView. Return the whole ArrayBuffer.
return this.data.buffer;
}
} else {
throw new TypeError(`${this.format} value is not of type: ArrayBuffer, DataView, or TypedArray.`);
}
}
case 'Hex': {
return Convert.hex(this.data).toUint8Array().buffer;
}
case 'String': {
return Convert.string(this.data).toUint8Array().buffer;
}
case 'Uint8Array': {
return this.data.buffer;
}
default:
throw new TypeError(`Conversion from ${this.format} to ArrayBuffer is not supported.`);
}
}
toBase58Btc(): string {
switch (this.format) {
case 'ArrayBuffer': {
const u8a = new Uint8Array(this.data);
return base58btc.baseEncode(u8a);
}
case 'Multibase': {
return this.data.substring(1);
}
case 'Uint8Array': {
return base58btc.baseEncode(this.data);
}
default:
throw new TypeError(`Conversion from ${this.format} to Base58Btc is not supported.`);
}
}
toBase64Url(): string {
switch (this.format) {
case 'ArrayBuffer': {
const u8a = new Uint8Array(this.data);
return base64url.baseEncode(u8a);
}
case 'Object': {
const string = JSON.stringify(this.data);
const u8a = textEncoder.encode(string);
return base64url.baseEncode(u8a);
}
case 'String': {
const u8a = textEncoder.encode(this.data);
return base64url.baseEncode(u8a);
}
case 'Uint8Array': {
return base64url.baseEncode(this.data);
}
default:
throw new TypeError(`Conversion from ${this.format} to Base64Url is not supported.`);
}
}
toHex(): string {
// pre-calculating Hex values improves runtime by 6-10x.
const hexes = Array.from({ length: 256 }, (v, i) => i.toString(16).padStart(2, '0'));
switch (this.format) {
case 'ArrayBuffer': {
const u8a = new Uint8Array(this.data);
return Convert.uint8Array(u8a).toHex();
}
case 'Uint8Array': {
let hex = '';
for (let i = 0; i < this.data.length; i++) {
hex += hexes[this.data[i]];
}
return hex;
}
default:
throw new TypeError(`Conversion from ${this.format} to Object is not supported.`);
}
}
toMultibase(): Multibase<any> {
switch (this.format) {
case 'Base58Btc': {
return `z${this.data}`;
}
default:
throw new TypeError(`Conversion from ${this.format} to Multibase is not supported.`);
}
}
toObject(): object {
switch (this.format) {
case 'Base64Url': {
const u8a = base64url.baseDecode(this.data);
const string = textDecoder.decode(u8a);
return JSON.parse(string);
}
case 'String': {
return JSON.parse(this.data);
}
case 'Uint8Array': {
const string = textDecoder.decode(this.data);
return JSON.parse(string);
}
default:
throw new TypeError(`Conversion from ${this.format} to Object is not supported.`);
}
}
toString(): string {
switch (this.format) {
case 'ArrayBuffer': {
return textDecoder.decode(this.data);
}
case 'Base64Url': {
const u8a = base64url.baseDecode(this.data);
return textDecoder.decode(u8a);
}
case 'Object': {
return JSON.stringify(this.data);
}
case 'Uint8Array': {
return textDecoder.decode(this.data);
}
default:
throw new TypeError(`Conversion from ${this.format} to String is not supported.`);
}
}
toUint8Array(): Uint8Array {
switch (this.format) {
case 'ArrayBuffer': {
// Çreate Uint8Array as a view on the ArrayBuffer.
// Note: The Uint8Array shares the same memory as the ArrayBuffer, so this operation is very efficient.
return new Uint8Array(this.data);
}
case 'Base58Btc': {
return base58btc.baseDecode(this.data);
}
case 'Base64Url': {
return base64url.baseDecode(this.data);
}
case 'BufferSource': {
const dataType = universalTypeOf(this.data);
if (dataType === 'Uint8Array') {
// Data is already a Uint8Array. No conversion is necessary.
// Note: Uint8Array is a type of BufferSource.
return this.data;
} else if (dataType === 'ArrayBuffer') {
// Data is an ArrayBuffer, create Uint8Array as a view on the ArrayBuffer.
// Note: The Uint8Array shares the same memory as the ArrayBuffer, so this operation is very efficient.
return new Uint8Array(this.data);
} else if (ArrayBuffer.isView(this.data)) {
// Data is a DataView or a different TypedArray (e.g., Uint16Array).
return new Uint8Array(this.data.buffer, this.data.byteOffset, this.data.byteLength);
} else {
throw new TypeError(`${this.format} value is not of type: ArrayBuffer, DataView, or TypedArray.`);
}
}
case 'Hex': {
const u8a = new Uint8Array(this.data.length / 2);
for (let i = 0; i < this.data.length; i += 2) {
const byteValue = parseInt(this.data.substring(i, i + 2), 16);
if (isNaN(byteValue)) {
throw new TypeError('Input is not a valid hexadecimal string.');
}
u8a[i / 2] = byteValue;
}
return u8a;
}
case 'Object': {
const string = JSON.stringify(this.data);
return textEncoder.encode(string);
}
case 'String': {
return textEncoder.encode(this.data);
}
default:
throw new TypeError(`Conversion from ${this.format} to Uint8Array is not supported.`);
}
}
}