-
Notifications
You must be signed in to change notification settings - Fork 54
/
Copy pathinterface.ts
95 lines (85 loc) · 2.44 KB
/
interface.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
// Base encoders / decoders just base encode / decode between binary and
// textual representation. They are unaware of multibase.
/**
* Base encoder just encodes bytes into base encoded string.
*/
export interface BaseEncoder {
/**
* Base encodes to a **plain** (and not a multibase) string. Unlike
* `encode` no multibase prefix is added.
*/
baseEncode(bytes: Uint8Array): string
}
/**
* Base decoder decodes encoded with matching base encoding into bytes.
*/
export interface BaseDecoder {
/**
* Decodes **plain** (and not a multibase) string. Unlike
* decode
*/
baseDecode(text: string): Uint8Array
}
/**
* Base codec is just dual of encoder and decoder.
*/
export interface BaseCodec {
encoder: BaseEncoder
decoder: BaseDecoder
}
/**
* Multibase represents base encoded strings with a prefix first character
* describing it's encoding.
*/
export type Multibase<Prefix extends string> =
| string
| string & { [0]: Prefix }
/**
* Multibase encoder for the specific base encoding encodes bytes into
* multibase of that encoding.
*/
export interface MultibaseEncoder<Prefix extends string> {
/**
* Name of the encoding.
*/
name: string
/**
* Prefix character for that base encoding.
*/
prefix: Prefix
/**
* Encodes binary data into **multibase** string (which will have a
* prefix added).
*/
encode(bytes: Uint8Array): Multibase<Prefix>
}
/**
* Interface implemented by multibase decoder, that takes multibase strings
* to bytes. It may support single encoding like base32 or multiple encodings
* like base32, base58btc, base64. If passed multibase is incompatible it will
* throw an exception.
*/
export interface MultibaseDecoder<Prefix extends string> {
/**
* Decodes **multibase** string (which must have a multibase prefix added).
* If prefix does not match
*/
decode(multibase: Multibase<Prefix>): Uint8Array
}
/**
* Dual of multibase encoder and decoder.
*/
export interface MultibaseCodec<Prefix extends string> {
name: string
prefix: Prefix
encoder: MultibaseEncoder<Prefix>
decoder: MultibaseDecoder<Prefix>
}
export interface UnibaseDecoder<Prefix extends string> extends MultibaseDecoder<Prefix> {
// Reserve this property so it can be used to derive type.
readonly decoders?: null
readonly prefix: Prefix
}
export interface CombobaseDecoder<Prefix extends string> extends MultibaseDecoder<Prefix> {
readonly decoders: Record<Prefix, UnibaseDecoder<Prefix>>
}