-
Notifications
You must be signed in to change notification settings - Fork 139
/
key.ts
64 lines (51 loc) · 1.79 KB
/
key.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
// Copyright 2017-2019 @polkadot/trie-db authors & contributors
// This software may be modified and distributed under the terms
// of the Apache-2.0 license. See the LICENSE file for details.
import { addNibblesTerminator, encodeNibbles } from '@polkadot/trie-codec/nibbles';
import { isNull } from '@polkadot/util';
import { EncodedPath } from '../types';
export function keyStartsWith (key: Uint8Array | null, partial: Uint8Array | null): boolean {
if (isNull(key) && isNull(partial)) {
return true;
} else if (isNull(key) || isNull(partial) || (key.length < partial.length)) {
return false;
}
for (let index = 0; index < partial.length; index++) {
if (key[index] !== partial[index]) {
return false;
}
}
return true;
}
export function keyEquals (key: Uint8Array | null, test: Uint8Array | null): boolean {
if (isNull(key) && isNull(test)) {
return true;
} else if (isNull(key) || isNull(test) || (key.length !== test.length)) {
return false;
}
return keyStartsWith(key, test);
}
export function computeExtensionKey (nibbles: Uint8Array): EncodedPath {
return encodeNibbles(nibbles);
}
export function computeLeafKey (nibbles: Uint8Array): EncodedPath {
return encodeNibbles(
addNibblesTerminator(nibbles)
);
}
export function getCommonPrefixLength (left: Uint8Array, right: Uint8Array): number {
for (let index = 0; index < left.length && index < right.length; index++) {
if (left[index] !== right[index]) {
return index;
}
}
return Math.min(left.length, right.length);
}
export function consumeCommonPrefix (left: Uint8Array, right: Uint8Array): [Uint8Array, Uint8Array, Uint8Array] {
const length = getCommonPrefixLength(left, right);
return [
left.subarray(0, length),
left.subarray(length),
right.subarray(length)
];
}