-
Notifications
You must be signed in to change notification settings - Fork 36
/
Copy pathvdring.ts
94 lines (84 loc) · 2.24 KB
/
vdring.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
import { randomNonce } from '../app/coring';
import { TraitDex } from '../app/habery';
import {
Serials,
Versionage,
Version,
Ident,
versify,
Ilks,
} from '../core/core';
import { ample } from './eventing';
import { MtrDex } from './matter';
import { Prefixer } from './prefixer';
import { Serder } from './serder';
namespace vdr {
export interface VDRInceptArgs {
pre: string;
toad?: number | string;
nonce?: string;
baks?: string[];
cnfg?: string[];
version?: Version;
kind?: Serials;
code?: string;
}
export function incept({
pre,
toad,
nonce = randomNonce(),
baks = [],
cnfg = [],
version = Versionage,
kind = Serials.JSON,
code = MtrDex.Blake3_256,
}: VDRInceptArgs): Serder {
const vs = versify(Ident.KERI, version, kind, 0);
const isn = 0;
const ilk = Ilks.vcp;
if (cnfg.includes(TraitDex.NoBackers) && baks.length > 0) {
throw new Error(
`${baks.length} backers specified for NB vcp, 0 allowed`
);
}
if (new Set(baks).size < baks.length) {
throw new Error(`Invalid baks ${baks} has duplicates`);
}
let _toad: number;
if (toad === undefined) {
if (baks.length === 0) {
_toad = 0;
} else {
_toad = ample(baks.length);
}
} else {
_toad = +toad;
}
if (baks.length > 0) {
if (_toad < 1 || _toad > baks.length) {
throw new Error(`Invalid toad ${_toad} for baks in ${baks}`);
}
} else {
if (_toad != 0) {
throw new Error(`Invalid toad ${_toad} for no baks`);
}
}
const ked = {
v: vs,
t: ilk,
d: '',
i: '',
ii: pre,
s: '' + isn,
c: cnfg,
bt: _toad.toString(16),
b: baks,
n: nonce,
};
const prefixer = new Prefixer({ code }, ked);
ked.i = prefixer.qb64;
ked.d = prefixer.qb64;
return new Serder(ked);
}
}
export { vdr };