-
Notifications
You must be signed in to change notification settings - Fork 55
/
primitives.ts
104 lines (93 loc) · 3.39 KB
/
primitives.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
import { Decoded, bech32 } from 'bech32';
import { InvalidStringError } from '../../errors';
const MAX_BECH32_LENGTH_LIMIT = 1023;
// Source: https://github.com/Microsoft/Typescript/issues/202#issuecomment-811246768
export declare class OpaqueString<T extends string> extends String {
/** This helps typescript distinguish different opaque string types. */
protected readonly __opaqueString: T;
/**
* This object is already a string, but calling this makes method
* makes typescript recognize it as such.
*/
toString(): string;
}
const isOneOf = <T>(target: T, options: T | T[]) =>
(Array.isArray(options) && options.includes(target)) || target === options;
const assertIsBech32WithPrefix = (
target: string,
prefix: string | string[],
expectedDecodedLength?: number | number[]
): void => {
let decoded: Decoded;
try {
decoded = bech32.decode(target, MAX_BECH32_LENGTH_LIMIT);
} catch (error) {
throw new InvalidStringError(`expected bech32-encoded string with '${prefix}' prefix`, error);
}
if (!isOneOf(decoded.prefix, prefix)) {
throw new InvalidStringError(`expected bech32 prefix '${prefix}', got '${decoded.prefix}''`);
}
if (expectedDecodedLength && !isOneOf(decoded.words.length, expectedDecodedLength)) {
throw new InvalidStringError(
`expected decoded length of '${expectedDecodedLength}', got '${decoded.words.length}'`
);
}
};
/**
* @param {string} target bech32 string to decode
* @param {string} prefix expected prefix
* @param {string} expectedDecodedLength number of expected words, >0
* @throws {InvalidStringError}
*/
export const typedBech32 = <T>(
target: string,
prefix: string | string[],
expectedDecodedLength?: number | number[]
) => {
assertIsBech32WithPrefix(target, prefix, expectedDecodedLength);
return target as unknown as T;
};
/**
* @param {string} target hex string to validate
* @param {string} expectedLength expected string length, >0
* @throws {InvalidStringError}
*/
export const assertIsHexString = (target: string, expectedLength?: number): void => {
if (expectedLength && target.length !== expectedLength) {
throw new InvalidStringError(`expected length '${expectedLength}', got ${target.length}`);
}
// eslint-disable-next-line wrap-regex
if (!/^[\da-f]+$/i.test(target)) {
throw new InvalidStringError('expected hex string');
}
};
/**
* @param {string} value hex string to validate
* @param {string} length expected string length, >0
* @throws {InvalidStringError}
*/
export const typedHex = <T>(value: string, length?: number): T => {
assertIsHexString(value, length);
// eslint-disable-next-line @typescript-eslint/no-explicit-any
return value as any as T;
};
/**
* 32 byte hash as hex string
*/
export type Hash32ByteBase16<T extends string = 'Hash32ByteBase16'> = OpaqueString<T>;
/**
* @param {string} value 32 byte hash as hex string
* @throws InvalidStringError
*/
export const Hash32ByteBase16 = <T extends string = 'Hash32ByteBase16'>(value: string): Hash32ByteBase16<T> =>
typedHex<Hash32ByteBase16<T>>(value, 64);
/**
* 28 byte hash as hex string
*/
export type Hash28ByteBase16<T extends string = 'Hash28ByteBase16'> = OpaqueString<T>;
/**
* @param {string} value 28 byte hash as hex string
* @throws InvalidStringError
*/
export const Hash28ByteBase16 = <T extends string = 'Hash28ByteBase16'>(value: string): Hash28ByteBase16<T> =>
typedHex<Hash32ByteBase16<T>>(value, 56);