forked from input-output-hk/cardano-js-sdk
-
Notifications
You must be signed in to change notification settings - Fork 0
/
providerUtil.ts
45 lines (41 loc) · 1.48 KB
/
providerUtil.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
import { Cardano, ProviderError, ProviderFailure } from '../';
/* eslint-disable @typescript-eslint/no-explicit-any */
export type ToProviderError = (error: unknown) => void;
export const withProviderErrors = <T extends {}>(providerImplementation: T, toProviderError: ToProviderError) =>
Object.keys(providerImplementation).reduce((provider, key) => {
const originalValue = (providerImplementation as any)[key];
provider[key] =
typeof originalValue === 'function'
? (...args: any[]) => originalValue(...args).catch(toProviderError)
: originalValue;
return provider;
}, {} as any) as T;
const tryParseBigIntKey = (key: string) => {
try {
return BigInt(key);
} catch {
return key;
}
};
/**
* Recursively maps JSON metadata to core metadata.
* As JSON doesn't support numeric keys,
* this function assumes that all metadata (and metadatum map) keys parseable by BigInt.parse are numeric.
*/
export const jsonToMetadatum = (obj: unknown): Cardano.Metadatum => {
switch (typeof obj) {
case 'number':
return BigInt(obj);
case 'string':
case 'bigint':
return obj;
case 'object': {
if (obj === null) break;
if (Array.isArray(obj)) {
return obj.map(jsonToMetadatum);
}
return new Map(Object.keys(obj).map((key) => [tryParseBigIntKey(key), jsonToMetadatum((obj as any)[key])]));
}
}
throw new ProviderError(ProviderFailure.NotImplemented, null, `Unsupported metadatum type: ${typeof obj}`);
};