signify-ts 0.1.0

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
Files changed (43) hide show
  1. package/LICENSE +201 -0
  2. package/README.md +25 -0
  3. package/dist/index.d.ts +1 -0
  4. package/dist/index.js +8 -0
  5. package/dist/keri/core/cigar.d.ts +8 -0
  6. package/dist/keri/core/cipher.d.ts +5 -0
  7. package/dist/keri/core/core.d.ts +68 -0
  8. package/dist/keri/core/decrypter.d.ts +10 -0
  9. package/dist/keri/core/diger.d.ts +21 -0
  10. package/dist/keri/core/encrypter.d.ts +9 -0
  11. package/dist/keri/core/indexer.d.ts +88 -0
  12. package/dist/keri/core/keeping.d.ts +1 -0
  13. package/dist/keri/core/kering.d.ts +5 -0
  14. package/dist/keri/core/manager.d.ts +151 -0
  15. package/dist/keri/core/matter.d.ts +47 -0
  16. package/dist/keri/core/siger.d.ts +22 -0
  17. package/dist/keri/core/signer.d.ts +34 -0
  18. package/dist/keri/core/utils.d.ts +16 -0
  19. package/dist/signify-ts.cjs.development.js +13 -0
  20. package/dist/signify-ts.cjs.development.js.map +1 -0
  21. package/dist/signify-ts.cjs.production.min.js +2 -0
  22. package/dist/signify-ts.cjs.production.min.js.map +1 -0
  23. package/dist/signify-ts.esm.js +9 -0
  24. package/dist/signify-ts.esm.js.map +1 -0
  25. package/package.json +66 -0
  26. package/src/index.ts +6 -0
  27. package/src/keri/core/cigar.ts +19 -0
  28. package/src/keri/core/cipher.ts +25 -0
  29. package/src/keri/core/core.ts +174 -0
  30. package/src/keri/core/decrypter.ts +66 -0
  31. package/src/keri/core/diger.ts +82 -0
  32. package/src/keri/core/encrypter.ts +60 -0
  33. package/src/keri/core/indexer.ts +406 -0
  34. package/src/keri/core/keeping.ts +9 -0
  35. package/src/keri/core/kering.ts +12 -0
  36. package/src/keri/core/manager.ts +874 -0
  37. package/src/keri/core/matter.ts +252 -0
  38. package/src/keri/core/salter.ts +85 -0
  39. package/src/keri/core/siger.ts +39 -0
  40. package/src/keri/core/signer.ts +112 -0
  41. package/src/keri/core/tsconfig.json +100 -0
  42. package/src/keri/core/utils.ts +94 -0
  43. package/src/keri/core/verfer.ts +33 -0
@@ -0,0 +1,406 @@
1
+ import {EmptyMaterialError} from "./kering";
2
+ import {b, b64ToInt, d, intToB64, readInt} from "./core";
3
+ const Base64 = require('urlsafe-base64');
4
+
5
+ export class IndexerCodex {
6
+ Ed25519_Sig: string = 'A' // Ed25519 sig appears same in both lists if any.
7
+ Ed25519_Crt_Sig: string = 'B' // Ed25519 sig appears in current list only.
8
+ ECDSA_256k1_Sig: string = 'C' // ECDSA secp256k1 sig appears same in both lists if any.
9
+ ECDSA_256k1_Crt_Sig: string = 'D' // ECDSA secp256k1 sig appears in current list.
10
+ Ed448_Sig: string = '0A' // Ed448 signature appears in both lists.
11
+ Ed448_Crt_Sig: string = '0B' // Ed448 signature appears in current list only.
12
+ Ed25519_Big_Sig: string = '2A' // Ed25519 sig appears in both lists.
13
+ Ed25519_Big_Crt_Sig: string = '2B' // Ed25519 sig appears in current list only.
14
+ ECDSA_256k1_Big_Sig: string = '2C' // ECDSA secp256k1 sig appears in both lists.
15
+ ECDSA_256k1_Big_Crt_Sig: string = '2D' // ECDSA secp256k1 sig appears in current list only.
16
+ Ed448_Big_Sig: string = '3A' // Ed448 signature appears in both lists.
17
+ Ed448_Big_Crt_Sig: string = '3B' // Ed448 signature appears in current list only.
18
+
19
+ }
20
+
21
+ export const IdrDex = new IndexerCodex()
22
+
23
+ export class IndexedSigCodex {
24
+ Ed25519_Sig: string = 'A' // Ed25519 sig appears same in both lists if any.
25
+ Ed25519_Crt_Sig: string = 'B' // Ed25519 sig appears in current list only.
26
+ ECDSA_256k1_Sig: string = 'C' // ECDSA secp256k1 sig appears same in both lists if any.
27
+ ECDSA_256k1_Crt_Sig: string = 'D' // ECDSA secp256k1 sig appears in current list.
28
+ Ed448_Sig: string = '0A' // Ed448 signature appears in both lists.
29
+ Ed448_Crt_Sig: string = '0B' // Ed448 signature appears in current list only.
30
+ Ed25519_Big_Sig: string = '2A' // Ed25519 sig appears in both lists.
31
+ Ed25519_Big_Crt_Sig: string = '2B' // Ed25519 sig appears in current list only.
32
+ ECDSA_256k1_Big_Sig: string = '2C' // ECDSA secp256k1 sig appears in both lists.
33
+ ECDSA_256k1_Big_Crt_Sig: string = '2D' // ECDSA secp256k1 sig appears in current list only.
34
+ Ed448_Big_Sig: string = '3A' // Ed448 signature appears in both lists.
35
+ Ed448_Big_Crt_Sig: string = '3B' // Ed448 signature appears in current list only.
36
+
37
+ has(prop: string): boolean {
38
+ let m = new Map(Array.from(Object.entries(this), (v) => [v[1], v[0]]))
39
+ return m.has(prop)
40
+ }
41
+ }
42
+
43
+ export const IdxSigDex = new IndexedSigCodex()
44
+
45
+ export class IndexedCurrentSigCodex {
46
+ Ed25519_Crt_Sig: string = 'B' // Ed25519 sig appears in current list only.
47
+ ECDSA_256k1_Crt_Sig: string = 'D' // ECDSA secp256k1 sig appears in current list only.
48
+ Ed448_Crt_Sig: string = '0B' // Ed448 signature appears in current list only.
49
+ Ed25519_Big_Crt_Sig: string = '2B' // Ed25519 sig appears in current list only.
50
+ ECDSA_256k1_Big_Crt_Sig: string = '2D' // ECDSA secp256k1 sig appears in current list only.
51
+ Ed448_Big_Crt_Sig: string = '3B' // Ed448 signature appears in current list only.
52
+
53
+ has(prop: string): boolean {
54
+ let m = new Map(Array.from(Object.entries(this), (v) => [v[1], v[0]]))
55
+ return m.has(prop)
56
+ }
57
+ }
58
+
59
+ export const IdxCrtSigDex = new IndexedCurrentSigCodex()
60
+
61
+ export class IndexedBothSigCodex {
62
+ Ed25519_Sig: string = 'A' // Ed25519 sig appears same in both lists if any.
63
+ ECDSA_256k1_Sig: string = 'C' // ECDSA secp256k1 sig appears same in both lists if any.
64
+ Ed448_Sig: string = '0A' // Ed448 signature appears in both lists.
65
+ Ed25519_Big_Sig: string = '2A' // Ed25519 sig appears in both listsy.
66
+ ECDSA_256k1_Big_Sig: string = '2C' // ECDSA secp256k1 sig appears in both lists.
67
+ Ed448_Big_Sig: string = '3A' // Ed448 signature appears in both lists.
68
+
69
+ has(prop: string): boolean {
70
+ let m = new Map(Array.from(Object.entries(this), (v) => [v[1], v[0]]))
71
+ return m.has(prop)
72
+ }
73
+ }
74
+
75
+ export const IdxBthSigDex = new IndexedBothSigCodex()
76
+
77
+ export class Xizage {
78
+ public hs: number
79
+ public ss: number
80
+ public os: number
81
+ public fs?: number
82
+ public ls: number
83
+
84
+ constructor(hs:number, ss: number, os: number, fs?: number, ls?: number) {
85
+ this.hs = hs
86
+ this.ss = ss
87
+ this.os = os
88
+ this.fs = fs
89
+ this.ls = ls!
90
+ }
91
+ }
92
+
93
+ export interface IndexerArgs {
94
+ raw?: Uint8Array | undefined,
95
+ code?: string | undefined
96
+ index?: number,
97
+ ondex?: number,
98
+ qb64b?: Uint8Array | undefined,
99
+ qb64?: string | undefined,
100
+ qb2?: Uint8Array | undefined,
101
+ }
102
+
103
+ export class Indexer {
104
+ public Codex = IdrDex
105
+
106
+ static Hards = new Map<string, number>([['A', 1], ['B', 1], ['C', 1], ['D', 1], ['E', 1], ['F', 1], ['G', 1], ['H', 1],
107
+ ['I', 1], ['J', 1], ['K', 1], ['L', 1], ['M', 1], ['N', 1], ['O', 1], ['P', 1], ['Q', 1], ['R', 1], ['S', 1],
108
+ ['T', 1], ['U', 1], ['V', 1], ['W', 1], ['X', 1], ['Y', 1], ['Z', 1], ['a', 1], ['b', 1], ['c', 1], ['d', 1],
109
+ ['e', 1], ['f', 1], ['g', 1], ['h', 1], ['i', 1], ['j', 1], ['k', 1], ['l', 1], ['m', 1], ['n', 1], ['o', 1],
110
+ ['p', 1], ['q', 1], ['r', 1], ['s', 1], ['t', 1], ['u', 1], ['v', 1], ['w', 1], ['x', 1], ['y', 1], ['z', 1],
111
+ ['0', 2], ['1', 2], ['2', 2], ['3', 2], ['4', 2]]
112
+ )
113
+
114
+ static Sizes = new Map(Object.entries({
115
+ 'A': new Xizage(1, 1, 0, 88, 0),
116
+ 'B': new Xizage(1, 1, 0, 88, 0),
117
+ 'C': new Xizage(1, 1, 0, 88, 0),
118
+ 'D': new Xizage(1, 1, 0, 88, 0),
119
+ '0A': new Xizage(2, 2, 1, 156, 0),
120
+ '0B': new Xizage(2, 2, 1, 156, 0),
121
+
122
+ '2A': new Xizage(2, 4, 2, 92, 0),
123
+ '2B': new Xizage(2, 4, 2, 92, 0),
124
+ '2C': new Xizage(2, 4, 2, 92, 0),
125
+ '2D': new Xizage(2, 4, 2, 92, 0),
126
+
127
+ '3A': new Xizage(2, 6, 3, 160, 0),
128
+ '3B': new Xizage(2, 6, 3, 160, 0),
129
+
130
+ '0z': new Xizage(2, 2, 0, undefined, 0),
131
+ '1z': new Xizage(2, 2, 1, 76, 1),
132
+ '4z': new Xizage(2, 6, 3, 80, 1),
133
+ }))
134
+
135
+ private _code: string = "";
136
+ private _index: number = -1;
137
+ private _ondex: number | undefined;
138
+ private _raw: Uint8Array = new Uint8Array(0);
139
+
140
+ constructor({raw = undefined, code = IdrDex.Ed25519_Sig, index = 0, ondex = undefined,
141
+ qb64b = undefined, qb64 = undefined, qb2 = undefined}: IndexerArgs) {
142
+
143
+ if (raw != undefined) {
144
+ if (code == undefined) {
145
+ throw new EmptyMaterialError(`Improper initialization need either (raw and code) or qb64b or qb64 or qb2.`)
146
+ }
147
+
148
+ if (!Indexer.Sizes.has(code)) {
149
+ throw new Error(`Unsupported code=${code}.`)
150
+ }
151
+
152
+ let xizage = Indexer.Sizes.get(code)!
153
+ let os = xizage.os
154
+ let fs = xizage.fs
155
+ let cs = xizage.hs + xizage.ss
156
+ let ms = xizage.ss - xizage.os
157
+
158
+ if (!Number.isInteger(index) || index < 0 || index > (64 ** ms - 1)) {
159
+ throw new Error(`Invalid index=${index} for code=${code}.`)
160
+ }
161
+
162
+ if (ondex != undefined && xizage.os != 0 && !(ondex >= 0 && ondex <= (64 ** os - 1))) {
163
+ throw new Error(`Invalid ondex=${ondex} for code=${code}.`)
164
+ }
165
+
166
+ if (IdxCrtSigDex.has(code) && ondex != undefined) {
167
+ throw new Error(`Non None ondex=${ondex} for code=${code}.`)
168
+ }
169
+
170
+ if (IdxBthSigDex.has(code)) {
171
+ if (ondex == undefined) {
172
+ ondex = index
173
+ } else {
174
+ if (ondex != index && os == 0) {
175
+ throw new Error(`Non matching ondex=${ondex} and index=${index} for code=${code}.`)
176
+ }
177
+ }
178
+ }
179
+
180
+ if (fs == undefined) {
181
+ throw new Error("variable length unsupported")
182
+ }
183
+ // TODO: Don't support this code
184
+ // if not fs: # compute fs from index
185
+ // if cs % 4:
186
+ // raise InvalidCodeSizeError(f"Whole code size not multiple of 4 for "
187
+ // f"variable length material. cs={cs}.")
188
+ // if os != 0:
189
+ // raise InvalidCodeSizeError(f"Non-zero other index size for "
190
+ // f"variable length material. os={os}.")
191
+ // fs = (index * 4) + cs
192
+ let rawsize = Math.floor((fs - cs) * 3 / 4)
193
+ raw = raw.slice(0, rawsize)
194
+
195
+ if (raw.length != rawsize) {
196
+ throw new Error(`Not enougth raw bytes for code=${code} and index=${index} ,expected ${rawsize} got ${raw.length}.`)
197
+ }
198
+
199
+ this._code = code
200
+ this._index = index
201
+ this._ondex = ondex
202
+ this._raw = raw
203
+
204
+ } else if (qb64b != undefined) {
205
+ let qb64 = d(qb64b)
206
+ this._exfil(qb64)
207
+
208
+ } else if (qb64 != undefined) {
209
+ this._exfil(qb64)
210
+ } else if (qb2 != undefined) {
211
+ this._bexfil(qb2)
212
+ } else {
213
+ throw new EmptyMaterialError(`Improper initialization need either (raw and code and index) or qb64b or qb64 or qb2.`)
214
+ }
215
+ }
216
+
217
+ private _bexfil(qb2: Uint8Array) {
218
+ throw new Error(`qb2 not yet supported: ${qb2}`)
219
+ }
220
+
221
+ public static _rawSize(code: string) {
222
+ let xizage = Indexer.Sizes.get(code)!
223
+ return Math.floor(xizage.fs! - (xizage.hs + xizage.ss) * 3 / 4)
224
+ }
225
+
226
+ get code(): string {
227
+ return this._code
228
+ }
229
+
230
+ get raw(): Uint8Array {
231
+ return this._raw
232
+ }
233
+
234
+ get index(): number {
235
+ return this._index
236
+ }
237
+
238
+ get ondex(): number | undefined {
239
+ return this._ondex
240
+ }
241
+
242
+ get qb64(): string{
243
+ return this._infil();
244
+ }
245
+
246
+ get qb64b() {
247
+ return b(this.qb64)
248
+ }
249
+
250
+
251
+ private _infil(): string {
252
+ let code = this.code
253
+ let index = this.index
254
+ let ondex = this.ondex
255
+ let raw = this.raw
256
+
257
+ let ps = (3 - (raw.length % 3)) % 3
258
+ let xizage = Indexer.Sizes.get(code)!
259
+ let cs = xizage.hs + xizage.ss
260
+ let ms = xizage.ss - xizage.os
261
+
262
+ // TODO: don't support this code
263
+ // if not fs: # compute fs from index
264
+ // if cs % 4:
265
+ // raise InvalidCodeSizeError(f"Whole code size not multiple of 4 for "
266
+ // f"variable length material. cs={cs}.")
267
+ // if os != 0:
268
+ // raise InvalidCodeSizeError(f"Non-zero other index size for "
269
+ // f"variable length material. os={os}.")
270
+ // fs = (index * 4) + cs
271
+
272
+ if (index < 0 || index > (64 ** ms -1)) {
273
+ throw new Error(`Invalid index=${index} for code=${code}.`)
274
+ }
275
+
276
+ if (ondex != undefined && xizage.os != 0 && !(ondex >= 0 && ondex <= (64 ** xizage.os - 1))) {
277
+ throw new Error(`Invalid ondex=${ondex} for os=${xizage.os} and code=${code}.`)
278
+ }
279
+
280
+ let both = `${code}${intToB64(index, ms)}${intToB64(ondex == undefined ? 0 : ondex, xizage.os)}`
281
+
282
+ if (both.length != cs) {
283
+ throw new Error(`Mismatch code size = ${cs} with table = ${both.length}.`)
284
+ }
285
+
286
+ if ((cs % 4) != ps - xizage.ls) {
287
+ throw new Error(`Invalid code=${both} for converted raw pad size=${ps}.`)
288
+ }
289
+
290
+ let bytes = new Uint8Array(ps + raw.length);
291
+ for (let i = 0; i < ps; i++) {
292
+ bytes[i] = 0;
293
+ }
294
+ for (let i = 0; i < raw.length; i++) {
295
+ let odx = i + ps
296
+ bytes[odx] = raw[i];
297
+ }
298
+
299
+ let full = both + Base64.encode(Buffer.from(bytes)).slice(ps - xizage.ls)
300
+ if (full.length != xizage.fs) {
301
+ throw new Error(`Invalid code=${both} for raw size=${raw.length}.`)
302
+ }
303
+
304
+ return full
305
+ }
306
+
307
+ _exfil(qb64: string) {
308
+ if (qb64.length == 0) {
309
+ throw new Error("Empty Material")
310
+ }
311
+
312
+ let first = qb64[0];
313
+ if (!Array.from(Indexer.Hards.keys()).includes(first)) {
314
+ throw new Error(`Unexpected code ${first}`)
315
+ }
316
+
317
+ let hs = Indexer.Hards.get(first)!
318
+ if ((qb64.length) < hs) {
319
+ throw new Error(`Need ${hs - qb64.length} more characters.`)
320
+ }
321
+
322
+ let hard = qb64.slice(0, hs)
323
+ if (!Array.from(Indexer.Sizes.keys()).includes(hard)) {
324
+ throw new Error(`Unsupported code ${hard}`)
325
+ }
326
+
327
+ let xizage = Indexer.Sizes.get(hard)!
328
+ let cs = xizage.hs + xizage.ss // both hard + soft code size
329
+ let ms = xizage.ss - xizage.os
330
+
331
+ if (qb64.length < cs) {
332
+ throw new Error(`Need ${cs - qb64.length} more characters.`)
333
+ }
334
+
335
+ let sindex = qb64.slice(hs, hs+ms)
336
+ let index = b64ToInt(sindex)
337
+
338
+ let sondex = qb64.slice(hs+ms, hs+ms+xizage.os)
339
+ let ondex
340
+ if (IdxCrtSigDex.has(hard)) {
341
+ ondex = xizage.os != 0 ? b64ToInt(sondex) : undefined
342
+ if (ondex != 0 && ondex != undefined) {
343
+ throw new Error(`Invalid ondex=${ondex} for code=${hard}.`)
344
+ } else {
345
+ ondex = undefined
346
+ }
347
+ } else {
348
+ ondex = xizage.os != 0 ? b64ToInt(sondex) : index
349
+ }
350
+
351
+ if (xizage.fs == undefined) {
352
+ throw new Error("variable length not supported")
353
+ }
354
+ // TODO: support variable length
355
+ // if not fs: # compute fs from index which means variable length
356
+ // if cs % 4:
357
+ // raise ValidationError(f"Whole code size not multiple of 4 for "
358
+ // f"variable length material. cs={cs}.")
359
+ // if os != 0:
360
+ // raise ValidationError(f"Non-zero other index size for "
361
+ // f"variable length material. os={os}.")
362
+ // fs = (index * 4) + cs
363
+
364
+ if (qb64.length < xizage.fs) {
365
+ throw new Error(`Need ${xizage.fs - qb64.length} more chars.`)
366
+ }
367
+
368
+ qb64 = qb64.slice(0, xizage.fs)
369
+ let ps = cs % 4
370
+ let pbs = 2 * ps != 0 ? ps : xizage.ls
371
+ let raw
372
+ if (ps != 0) {
373
+ let base = new Array(ps + 1).join('A') + qb64.slice(cs);
374
+ let paw = Base64.decode(base) // decode base to leave prepadded raw
375
+ let pi = (readInt(paw.slice(0, ps))) // prepad as int
376
+ if (pi & (2 ** pbs - 1)) { // masked pad bits non-zero
377
+ throw new Error(`Non zeroed prepad bits = {pi & (2 ** pbs - 1 ):<06b} in {qb64b[cs:cs+1]}.`)
378
+ }
379
+ raw = paw.slice(ps) // strip off ps prepad paw bytes
380
+ } else {
381
+ let base = qb64.slice(cs);
382
+ let paw = Base64.decode(base);
383
+ let li = readInt(paw.slice(0, xizage!.ls))
384
+ if (li != 0) {
385
+ if (li == 1) {
386
+ throw new Error(`Non zeroed lead byte = 0x{li:02x}.`)
387
+ } else {
388
+ throw new Error(`Non zeroed lead bytes = 0x{li:04x}`)
389
+ }
390
+ }
391
+ raw = paw.slice(xizage!.ls)
392
+ }
393
+
394
+ if (raw.length != Math.floor((qb64.length - cs) * 3 / 4)) {
395
+ throw new Error(`Improperly qualified material = ${qb64}`)
396
+ }
397
+
398
+ this._code = hard
399
+ this._index = index
400
+ this._ondex = ondex
401
+ this._raw = new Uint8Array(raw) // must be bytes for crpto opts and immutable not bytearray
402
+ }
403
+ }
404
+
405
+
406
+
@@ -0,0 +1,9 @@
1
+ export {};
2
+ const { SigMat } = require('./sigmat');
3
+
4
+ class Keeper {
5
+ constructor() {
6
+ }
7
+ }
8
+
9
+ module.exports = { Keeper }
@@ -0,0 +1,12 @@
1
+
2
+
3
+ export class EmptyMaterialError {
4
+ private readonly _err: Error
5
+ constructor(err: string) {
6
+ this._err = new Error(err)
7
+ }
8
+
9
+ get err() {
10
+ return this._err
11
+ }
12
+ }