-
Notifications
You must be signed in to change notification settings - Fork 54
/
interface.ts
70 lines (62 loc) · 2.07 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
// # Multihash
/**
* Represents a multihash digest which carries information about the
* hashing algorithm and an actual hash digest.
*/
// Note: In the current version there is no first class multihash
// representation (plain Uint8Array is used instead) instead there seems to be
// a bunch of places that parse it to extract (code, digest, size). By creating
// this first class representation we avoid reparsing and things generally fit
// really nicely.
export interface MultihashDigest<Code extends number = number> {
/**
* Code of the multihash
*/
code: Code
/**
* Raw digest (without a hashing algorithm info)
*/
digest: Uint8Array
/**
* byte length of the `this.digest`
*/
size: number
/**
* Binary representation of this multihash digest.
*/
bytes: Uint8Array
}
/**
* Hasher represents a hashing algorithm implementation that produces as
* `MultihashDigest`.
*/
export interface MultihashHasher<Code extends number = number> {
/**
* Takes binary `input` and returns it (multi) hash digest. Return value is
* either promise of a digest or a digest. This way general use can `await`
* while performance critical code may asses return value to decide whether
* await is needed.
*/
digest(input: Uint8Array): Promise<MultihashDigest<Code>> | MultihashDigest<Code>
/**
* Name of the multihash
*/
name: string
/**
* Code of the multihash
*/
code: Code
}
/**
* Sync variant of `MultihashHasher` that refines return type of the `digest`
* to `MultihashDigest`. It is subtype of `MultihashHasher` so implementations
* of this interface can be passed anywhere `MultihashHasher` is expected,
* allowing consumer to either `await` or check the return type to decide
* whether to await or proceed with return value.
*
* `SyncMultihashHasher` is useful in certain APIs where async hashing would be
* impractical e.g. implementation of Hash Array Mapped Trie (HAMT).
*/
export interface SyncMultihashHasher<Code extends number = number> extends MultihashHasher<Code> {
digest(input: Uint8Array): MultihashDigest<Code>
}