-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathtest-encoding.ts
90 lines (69 loc) · 2.16 KB
/
test-encoding.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
import type { Codec } from 'multiformats/bases/base';
import crypto from 'crypto';
import { bases } from 'multiformats/basics';
function randomBytes(size: number): Uint8Array {
return crypto.randomBytes(size);
}
type MultibaseFormats = keyof typeof bases;
const basesByPrefix: Record<string, Codec<string, string>> = {};
for (const k in bases) {
const codec = bases[k];
basesByPrefix[codec.prefix] = codec;
}
function toMultibase(id: Uint8Array, format: MultibaseFormats): string {
const codec = bases[format];
return codec.encode(id);
}
function fromMultibase(idString: string): Uint8Array | undefined {
const prefix = idString[0];
const codec = basesByPrefix[prefix];
if (codec == null) {
return;
}
const buffer = codec.decode(idString);
return buffer;
}
const originalList: Array<Uint8Array> = [];
const total = 100000;
let count = total;
while (count) {
originalList.push(randomBytes(16));
count--;
}
originalList.sort(Buffer.compare);
const encodedList = originalList.map(
(bs) => toMultibase(bs, 'base64')
);
console.log(encodedList);
// const encodedList_ = encodedList.slice();
// encodedList_.sort();
// // encodedList is the same order as originalList
// // if base58btc preserves lexicographic-order
// // then encodedList_ would be the same order
// const l = encodedList[0].length;
// console.log(l);
// for (let i = 0; i < total; i++) {
// if (encodedList[i].length !== l) {
// console.log('new length', encodedList[i].length);
// }
// if (encodedList[i] !== encodedList_[i]) {
// console.log('Does not match on:', i);
// console.log('original order', encodedList[i]);
// console.log('encoded order', encodedList_[i]);
// break;
// }
// }
// const decodedList = encodedList.map(fromMultibase);
// for (let i = 0; i < total; i++) {
// // @ts-ignore
// if (!originalList[i].equals(Buffer.from(decodedList[i]))) {
// console.log('bug in the code');
// break;
// }
// }
// // 36 characters became 59 characters
// console.log(encodedList);
// // need to watch the integration testing more
// // so that concurrent testing works better
// // establish promise conventions
// // and then get on the code properly