signify-ts 0.1.0 → 0.2.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 (105) hide show
  1. package/README.md +101 -5
  2. package/dist/buffer-polyfill.d.ts +1 -0
  3. package/dist/exports.d.ts +40 -0
  4. package/dist/index.d.ts +4 -1
  5. package/dist/keri/app/aiding.d.ts +138 -0
  6. package/dist/keri/app/clienting.d.ts +182 -0
  7. package/dist/keri/app/contacting.d.ts +93 -0
  8. package/dist/keri/app/controller.d.ts +49 -0
  9. package/dist/keri/app/coring.d.ts +121 -0
  10. package/dist/keri/app/credentialing.d.ts +239 -0
  11. package/dist/keri/app/escrowing.d.ts +19 -0
  12. package/dist/keri/app/exchanging.d.ts +60 -0
  13. package/dist/keri/app/grouping.d.ts +44 -0
  14. package/dist/keri/app/habery.d.ts +48 -0
  15. package/dist/keri/app/notifying.d.ts +34 -0
  16. package/dist/keri/core/authing.d.ts +10 -0
  17. package/dist/keri/core/bexter.d.ts +7 -0
  18. package/dist/keri/core/cigar.d.ts +8 -8
  19. package/dist/keri/core/cipher.d.ts +5 -5
  20. package/dist/keri/core/core.d.ts +59 -68
  21. package/dist/keri/core/counter.d.ts +43 -0
  22. package/dist/keri/core/decrypter.d.ts +10 -10
  23. package/dist/keri/core/diger.d.ts +21 -21
  24. package/dist/keri/core/encrypter.d.ts +9 -9
  25. package/dist/keri/core/eventing.d.ts +54 -0
  26. package/dist/keri/core/httping.d.ts +44 -0
  27. package/dist/keri/core/indexer.d.ts +98 -88
  28. package/dist/keri/core/keeping.d.ts +103 -1
  29. package/dist/keri/core/kering.d.ts +5 -5
  30. package/dist/keri/core/manager.d.ts +174 -151
  31. package/dist/keri/core/matter.d.ts +116 -47
  32. package/dist/keri/core/number.d.ts +7 -0
  33. package/dist/keri/core/pather.d.ts +7 -0
  34. package/dist/keri/core/prefixer.d.ts +15 -0
  35. package/dist/keri/core/saider.d.ts +23 -0
  36. package/dist/keri/core/salter.d.ts +23 -0
  37. package/dist/keri/core/seqner.d.ts +15 -0
  38. package/dist/keri/core/serder.d.ts +31 -0
  39. package/dist/keri/core/siger.d.ts +18 -18
  40. package/dist/keri/core/signer.d.ts +34 -34
  41. package/dist/keri/core/tholder.d.ts +29 -0
  42. package/dist/keri/core/utils.d.ts +20 -14
  43. package/dist/keri/core/vdring.d.ts +16 -0
  44. package/dist/keri/core/verfer.d.ts +13 -0
  45. package/dist/keri/end/ending.d.ts +13 -0
  46. package/dist/math.d.ts +1 -0
  47. package/dist/ready.d.ts +1 -0
  48. package/dist/signify-ts.cjs +7835 -0
  49. package/dist/signify-ts.cjs.development.js +9493 -5
  50. package/dist/signify-ts.cjs.development.js.map +1 -1
  51. package/dist/signify-ts.cjs.map +7 -0
  52. package/dist/signify-ts.cjs.production.min.js +1 -1
  53. package/dist/signify-ts.cjs.production.min.js.map +1 -1
  54. package/dist/signify-ts.esm.js +9341 -5
  55. package/dist/signify-ts.esm.js.map +1 -1
  56. package/dist/signify-ts.mjs +7811 -0
  57. package/dist/signify-ts.mjs.map +7 -0
  58. package/package.json +51 -49
  59. package/src/buffer-polyfill.ts +5 -0
  60. package/src/exports.ts +44 -0
  61. package/src/index.ts +4 -6
  62. package/src/keri/app/aiding.ts +493 -0
  63. package/src/keri/app/clienting.ts +492 -0
  64. package/src/keri/app/contacting.ts +188 -0
  65. package/src/keri/app/controller.ts +372 -0
  66. package/src/keri/app/coring.ts +225 -0
  67. package/src/keri/app/credentialing.ts +820 -0
  68. package/src/keri/app/escrowing.ts +34 -0
  69. package/src/keri/app/exchanging.ts +220 -0
  70. package/src/keri/app/grouping.ts +90 -0
  71. package/src/keri/app/habery.ts +185 -0
  72. package/src/keri/app/notifying.ts +69 -0
  73. package/src/keri/core/authing.ts +117 -0
  74. package/src/keri/core/bexter.ts +138 -0
  75. package/src/keri/core/cigar.ts +11 -12
  76. package/src/keri/core/cipher.ts +23 -14
  77. package/src/keri/core/core.ts +249 -97
  78. package/src/keri/core/counter.ts +273 -0
  79. package/src/keri/core/decrypter.ts +48 -34
  80. package/src/keri/core/diger.ts +25 -24
  81. package/src/keri/core/encrypter.ts +37 -27
  82. package/src/keri/core/eventing.ts +581 -0
  83. package/src/keri/core/httping.ts +209 -0
  84. package/src/keri/core/indexer.ts +314 -211
  85. package/src/keri/core/keeping.ts +679 -4
  86. package/src/keri/core/kering.ts +4 -6
  87. package/src/keri/core/manager.ts +759 -450
  88. package/src/keri/core/matter.ts +404 -135
  89. package/src/keri/core/number.ts +66 -0
  90. package/src/keri/core/pather.ts +98 -0
  91. package/src/keri/core/prefixer.ts +233 -0
  92. package/src/keri/core/saider.ts +186 -0
  93. package/src/keri/core/salter.ts +76 -41
  94. package/src/keri/core/seqner.ts +46 -0
  95. package/src/keri/core/serder.ts +163 -0
  96. package/src/keri/core/siger.ts +19 -18
  97. package/src/keri/core/signer.ts +74 -53
  98. package/src/keri/core/tholder.ts +218 -0
  99. package/src/keri/core/utils.ts +120 -45
  100. package/src/keri/core/vdring.ts +94 -0
  101. package/src/keri/core/verfer.ts +25 -11
  102. package/src/keri/end/ending.ts +170 -0
  103. package/src/math.ts +3 -0
  104. package/src/ready.ts +5 -0
  105. package/src/keri/core/tsconfig.json +0 -100
@@ -1,107 +1,339 @@
1
- import {EmptyMaterialError} from "./kering";
2
-
3
- const {intToB64, readInt} = require('./core');
4
- const Base64 = require('urlsafe-base64');
5
- import {b, d} from "./core";
6
-
7
-
8
- export class MatterCodex {
9
- Ed25519_Seed: string = 'A' // Ed25519 256 bit random seed for private key
10
- Ed25519N: string = 'B' // Ed25519 verification key non-transferable, basic derivation.
11
- X25519: string = 'C' // X25519 public encryption key, converted from Ed25519 or Ed25519N.
12
- Ed25519: string = 'D' // Ed25519 verification key basic derivation
13
- Blake3_256: string = 'E' // Blake3 256 bit digest self-addressing derivation.
14
- X25519_Private: string = 'O' // X25519 private decryption key converted from Ed25519
15
- X25519_Cipher_Seed: string = 'P' // X25519 124 char b64 Cipher of 44 char qb64 Seed
16
- X25519_Cipher_Salt: string = '1AAH' // X25519 100 char b64 Cipher of 24 char qb64 Salt
17
- Salt_128: string = '0A' // 128 bit random salt or 128 bit number (see Huge)
18
- Ed25519_Sig: string = '0B' // Ed25519 signature.
1
+ import { EmptyMaterialError } from './kering';
2
+
3
+ import { intToB64, readInt } from './core';
4
+ import Base64 from 'urlsafe-base64';
5
+ import { b, d } from './core';
6
+ import { Buffer } from 'buffer';
7
+
8
+ export class Codex {
9
+ has(prop: string): boolean {
10
+ const m = new Map(
11
+ Array.from(Object.entries(this), (v) => [v[1], v[0]])
12
+ );
13
+ return m.has(prop);
14
+ }
15
+ }
16
+
17
+ export class MatterCodex extends Codex {
18
+ Ed25519_Seed: string = 'A'; // Ed25519 256 bit random seed for private key
19
+ Ed25519N: string = 'B'; // Ed25519 verification key non-transferable, basic derivation.
20
+ X25519: string = 'C'; // X25519 public encryption key, converted from Ed25519 or Ed25519N.
21
+ Ed25519: string = 'D'; // Ed25519 verification key basic derivation
22
+ Blake3_256: string = 'E'; // Blake3 256 bit digest self-addressing derivation.
23
+ SHA3_256: string = 'H'; // SHA3 256 bit digest self-addressing derivation.
24
+ SHA2_256: string = 'I'; // SHA2 256 bit digest self-addressing derivation.
25
+ ECDSA_256k1_Seed: string = 'J'; // ECDSA secp256k1 256 bit random Seed for private key
26
+ X25519_Private: string = 'O'; // X25519 private decryption key converted from Ed25519
27
+ X25519_Cipher_Seed: string = 'P'; // X25519 124 char b64 Cipher of 44 char qb64 Seed
28
+ ECDSA_256r1_Seed: string = 'Q'; // ECDSA secp256r1 256 bit random Seed for private key
29
+ Salt_128: string = '0A'; // 128 bit random salt or 128 bit number (see Huge)
30
+ Ed25519_Sig: string = '0B'; // Ed25519 signature.
31
+ ECDSA_256k1_Sig: string = '0C'; // ECDSA secp256k1 signature.
32
+ ECDSA_256r1_Sig: string = '0I'; // ECDSA secp256r1 signature.
33
+ StrB64_L0: string = '4A'; // String Base64 Only Lead Size 0
34
+ StrB64_L1: string = '5A'; // String Base64 Only Lead Size 1
35
+ StrB64_L2: string = '6A'; // String Base64 Only Lead Size 2
36
+ ECDSA_256k1N: string = '1AAA'; // ECDSA secp256k1 verification key non-transferable, basic derivation.
37
+ ECDSA_256k1: string = '1AAB'; // ECDSA public verification or encryption key, basic derivation
38
+ X25519_Cipher_Salt: string = '1AAH'; // X25519 100 char b64 Cipher of 24 char qb64 Salt
39
+ ECDSA_256r1N: string = '1AAI'; // ECDSA secp256r1 verification key non-transferable, basic derivation.
40
+ ECDSA_256r1: string = '1AAJ'; // ECDSA secp256r1 verification or encryption key, basic derivation
41
+ StrB64_Big_L0: string = '7AAA'; // String Base64 Only Big Lead Size 0
42
+ StrB64_Big_L1: string = '8AAA'; // String Base64 Only Big Lead Size 1
43
+ StrB64_Big_L2: string = '9AAA'; // String Base64 Only Big Lead Size 2
44
+ }
45
+
46
+ export const MtrDex = new MatterCodex();
47
+
48
+ export class NonTransCodex extends Codex {
49
+ Ed25519N: string = 'B'; // Ed25519 verification key non-transferable, basic derivation.
50
+ ECDSA_256k1N: string = '1AAA'; // ECDSA secp256k1 verification key non-transferable, basic derivation.
51
+ Ed448N: string = '1AAC'; // Ed448 non-transferable prefix public signing verification key. Basic derivation.
52
+ ECDSA_256r1N: string = '1AAI'; // ECDSA secp256r1 verification key non-transferable, basic derivation.
19
53
  }
20
54
 
21
- export const MtrDex = new MatterCodex()
55
+ export const NonTransDex = new NonTransCodex();
56
+
57
+ export class DigiCodex extends Codex {
58
+ Blake3_256: string = 'E'; // Blake3 256 bit digest self-addressing derivation.
59
+ Blake2b_256: string = 'F'; // Blake2b 256 bit digest self-addressing derivation.
60
+ Blake2s_256: string = 'G'; // Blake2s 256 bit digest self-addressing derivation.
61
+ SHA3_256: string = 'H'; // SHA3 256 bit digest self-addressing derivation.
62
+ SHA2_256: string = 'I'; // SHA2 256 bit digest self-addressing derivation.
63
+ Blake3_512: string = '0D'; // Blake3 512 bit digest self-addressing derivation.
64
+ Blake2b_512: string = '0E'; // Blake2b 512 bit digest self-addressing derivation.
65
+ SHA3_512: string = '0F'; // SHA3 512 bit digest self-addressing derivation.
66
+ SHA2_512: string = '0G'; // SHA2 512 bit digest self-addressing derivation.
67
+ }
68
+
69
+ export const DigiDex = new DigiCodex();
70
+
71
+ export class NumCodex extends Codex {
72
+ Short: string = 'M'; // Short 2 byte b2 number
73
+ Long: string = '0H'; // Long 4 byte b2 number
74
+ Big: string = 'N'; // Big 8 byte b2 number
75
+ Huge: string = '0A'; // Huge 16 byte b2 number (same as Salt_128)
76
+ }
77
+
78
+ export const NumDex = new NumCodex();
79
+
80
+ export class BexCodex extends Codex {
81
+ StrB64_L0: string = '4A'; // String Base64 Only Leader Size 0
82
+ StrB64_L1: string = '5A'; // String Base64 Only Leader Size 1
83
+ StrB64_L2: string = '6A'; // String Base64 Only Leader Size 2
84
+ StrB64_Big_L0: string = '7AAA'; // String Base64 Only Big Leader Size 0
85
+ StrB64_Big_L1: string = '8AAA'; // String Base64 Only Big Leader Size 1
86
+ StrB64_Big_L2: string = '9AAA'; // String Base64 Only Big Leader Size 2
87
+ }
88
+
89
+ export const BexDex = new BexCodex();
90
+
91
+ class SmallVarRawSizeCodex extends Codex {
92
+ Lead0: string = '4'; // First Selector Character for all ls == 0 codes
93
+ Lead1: string = '5'; // First Selector Character for all ls == 1 codes
94
+ Lead2: string = '6'; // First Selector Character for all ls == 2 codes
95
+ }
96
+
97
+ export const SmallVrzDex = new SmallVarRawSizeCodex();
98
+
99
+ class LargeVarRawSizeCodex extends Codex {
100
+ Lead0_Big: string = '7'; // First Selector Character for all ls == 0 codes
101
+ Lead1_Big: string = '8'; // First Selector Character for all ls == 1 codes
102
+ Lead2_Big: string = '9'; // First Selector Character for all ls == 2 codes
103
+ }
104
+
105
+ export const LargeVrzDex = new LargeVarRawSizeCodex();
22
106
 
23
107
  export class Sizage {
24
108
  public hs: number;
25
109
  public ss: number;
26
- public ls: number;
27
- public fs: number;
110
+ public ls?: number;
111
+ public fs?: number;
28
112
 
29
- constructor(hs: number, ss: number, fs: number, ls: number) {
113
+ constructor(hs: number, ss: number, fs?: number, ls?: number) {
30
114
  this.hs = hs;
31
115
  this.ss = ss;
32
116
  this.fs = fs;
33
- this.ls = ls;
117
+ this.ls = ls!;
34
118
  }
35
119
  }
36
120
 
37
121
  export interface MatterArgs {
38
- raw?: Uint8Array | undefined
39
- code?: string
40
- qb64b?: Uint8Array | undefined
41
- qb64?: string
42
- qb2?: Uint8Array | undefined
122
+ raw?: Uint8Array | undefined;
123
+ code?: string;
124
+ qb64b?: Uint8Array | undefined;
125
+ qb64?: string;
126
+ qb2?: Uint8Array | undefined;
127
+ rize?: number;
43
128
  }
44
129
 
45
130
  export class Matter {
46
-
47
- static Sizes = new Map(Object.entries({
48
- 'A': new Sizage(1, 0, 44, 0),
49
- 'B': new Sizage(1, 0, 44, 0),
50
- 'C': new Sizage(1, 0, 44, 0),
51
- 'D': new Sizage(1, 0, 44, 0),
52
- 'E': new Sizage(1, 0, 44, 0),
53
- 'O': new Sizage(1, 0, 44, 0),
54
- 'P': new Sizage(1, 0, 124, 0),
55
- "0A": new Sizage(2, 0, 24, 0),
56
- '0B': new Sizage(2, 0, 88, 0),
57
- '1AAH': new Sizage(4, 0, 100, 0),
58
- }));
59
-
60
- static Hards = new Map<string, number>([['A', 1], ['B', 1], ['C', 1], ['D', 1], ['E', 1], ['F', 1], ['G', 1],
61
- ['H', 1], ['I', 1], ['J', 1], ['K', 1], ['L', 1], ['M', 1], ['N', 1], ['O', 1], ['P', 1], ['Q', 1], ['R', 1],
62
- ['S', 1], ['T', 1], ['U', 1], ['V', 1], ['W', 1], ['X', 1], ['Y', 1], ['Z', 1], ['a', 1], ['b', 1], ['c', 1],
63
- ['d', 1], ['e', 1], ['f', 1], ['g', 1], ['h', 1], ['i', 1], ['j', 1], ['k', 1], ['l', 1], ['m', 1], ['n', 1],
64
- ['o', 1], ['p', 1], ['q', 1], ['r', 1], ['s', 1], ['t', 1], ['u', 1], ['v', 1], ['w', 1], ['x', 1], ['y', 1],
65
- ['z', 1], ['0', 2], ['1', 4], ['2', 4], ['3', 4], ['4', 2], ['5', 2], ['6', 2], ['7', 4], ['8', 4], ['9', 4]])
66
-
67
-
68
- private _code: string = "";
131
+ static Sizes = new Map(
132
+ Object.entries({
133
+ A: new Sizage(1, 0, 44, 0),
134
+ B: new Sizage(1, 0, 44, 0),
135
+ C: new Sizage(1, 0, 44, 0),
136
+ D: new Sizage(1, 0, 44, 0),
137
+ E: new Sizage(1, 0, 44, 0),
138
+ F: new Sizage(1, 0, 44, 0),
139
+ G: new Sizage(1, 0, 44, 0),
140
+ H: new Sizage(1, 0, 44, 0),
141
+ I: new Sizage(1, 0, 44, 0),
142
+ J: new Sizage(1, 0, 44, 0),
143
+ K: new Sizage(1, 0, 76, 0),
144
+ L: new Sizage(1, 0, 76, 0),
145
+ M: new Sizage(1, 0, 4, 0),
146
+ N: new Sizage(1, 0, 12, 0),
147
+ O: new Sizage(1, 0, 44, 0),
148
+ P: new Sizage(1, 0, 124, 0),
149
+ Q: new Sizage(1, 0, 44, 0),
150
+ '0A': new Sizage(2, 0, 24, 0),
151
+ '0B': new Sizage(2, 0, 88, 0),
152
+ '0C': new Sizage(2, 0, 88, 0),
153
+ '0D': new Sizage(2, 0, 88, 0),
154
+ '0E': new Sizage(2, 0, 88, 0),
155
+ '0F': new Sizage(2, 0, 88, 0),
156
+ '0G': new Sizage(2, 0, 88, 0),
157
+ '0H': new Sizage(2, 0, 8, 0),
158
+ '0I': new Sizage(2, 0, 88, 0),
159
+ '1AAA': new Sizage(4, 0, 48, 0),
160
+ '1AAB': new Sizage(4, 0, 48, 0),
161
+ '1AAC': new Sizage(4, 0, 80, 0),
162
+ '1AAD': new Sizage(4, 0, 80, 0),
163
+ '1AAE': new Sizage(4, 0, 56, 0),
164
+ '1AAF': new Sizage(4, 0, 8, 0),
165
+ '1AAG': new Sizage(4, 0, 36, 0),
166
+ '1AAH': new Sizage(4, 0, 100, 0),
167
+ '1AAI': new Sizage(4, 0, 48, 0),
168
+ '1AAJ': new Sizage(4, 0, 48, 0),
169
+ '2AAA': new Sizage(4, 0, 8, 1),
170
+ '3AAA': new Sizage(4, 0, 8, 2),
171
+ '4A': new Sizage(2, 2, undefined, 0),
172
+ '5A': new Sizage(2, 2, undefined, 1),
173
+ '6A': new Sizage(2, 2, undefined, 2),
174
+ '7AAA': new Sizage(4, 4, undefined, 0),
175
+ '8AAA': new Sizage(4, 4, undefined, 1),
176
+ '9AAA': new Sizage(4, 4, undefined, 2),
177
+ '4B': new Sizage(2, 2, undefined, 0),
178
+ '5B': new Sizage(2, 2, undefined, 1),
179
+ '6B': new Sizage(2, 2, undefined, 2),
180
+ '7AAB': new Sizage(4, 4, undefined, 0),
181
+ '8AAB': new Sizage(4, 4, undefined, 1),
182
+ '9AAB': new Sizage(4, 4, undefined, 2),
183
+ })
184
+ );
185
+
186
+ static Hards = new Map<string, number>([
187
+ ['A', 1],
188
+ ['B', 1],
189
+ ['C', 1],
190
+ ['D', 1],
191
+ ['E', 1],
192
+ ['F', 1],
193
+ ['G', 1],
194
+ ['H', 1],
195
+ ['I', 1],
196
+ ['J', 1],
197
+ ['K', 1],
198
+ ['L', 1],
199
+ ['M', 1],
200
+ ['N', 1],
201
+ ['O', 1],
202
+ ['P', 1],
203
+ ['Q', 1],
204
+ ['R', 1],
205
+ ['S', 1],
206
+ ['T', 1],
207
+ ['U', 1],
208
+ ['V', 1],
209
+ ['W', 1],
210
+ ['X', 1],
211
+ ['Y', 1],
212
+ ['Z', 1],
213
+ ['a', 1],
214
+ ['b', 1],
215
+ ['c', 1],
216
+ ['d', 1],
217
+ ['e', 1],
218
+ ['f', 1],
219
+ ['g', 1],
220
+ ['h', 1],
221
+ ['i', 1],
222
+ ['j', 1],
223
+ ['k', 1],
224
+ ['l', 1],
225
+ ['m', 1],
226
+ ['n', 1],
227
+ ['o', 1],
228
+ ['p', 1],
229
+ ['q', 1],
230
+ ['r', 1],
231
+ ['s', 1],
232
+ ['t', 1],
233
+ ['u', 1],
234
+ ['v', 1],
235
+ ['w', 1],
236
+ ['x', 1],
237
+ ['y', 1],
238
+ ['z', 1],
239
+ ['0', 2],
240
+ ['1', 4],
241
+ ['2', 4],
242
+ ['3', 4],
243
+ ['4', 2],
244
+ ['5', 2],
245
+ ['6', 2],
246
+ ['7', 4],
247
+ ['8', 4],
248
+ ['9', 4],
249
+ ]);
250
+
251
+ private _code: string = '';
69
252
  private _size: number = -1;
70
253
  private _raw: Uint8Array = new Uint8Array(0);
71
254
 
72
- constructor({raw, code = MtrDex.Ed25519N, qb64b, qb64, qb2}: MatterArgs) {
73
-
74
- let size = -1
75
- if (raw !== undefined) {
76
- if (code.length === 0) {
77
- throw new Error("Improper initialization need either (raw and code) or qb64b or qb64 or qb2.")
255
+ constructor({
256
+ raw,
257
+ code = MtrDex.Ed25519N,
258
+ qb64b,
259
+ qb64,
260
+ qb2,
261
+ rize,
262
+ }: MatterArgs) {
263
+ let size = -1;
264
+ if (raw != undefined) {
265
+ if (code.length == 0) {
266
+ throw new Error(
267
+ 'Improper initialization need either (raw and code) or qb64b or qb64 or qb2.'
268
+ );
78
269
  }
79
270
 
80
- // Add support for variable size codes here if needed, this code only works for stable size codes
81
- let sizage = Matter.Sizes.get(code)
82
- if (sizage!.fs === -1) { // invalid
83
- throw new Error(`Unsupported variable size code=${code}`)
84
- }
271
+ if (SmallVrzDex.has(code[0]) || LargeVrzDex.has(code[0])) {
272
+ if (rize !== undefined) {
273
+ if (rize < 0)
274
+ throw new Error(
275
+ `missing var raw size for code=${code}`
276
+ );
277
+ } else {
278
+ rize = raw.length;
279
+ }
85
280
 
86
- let rize = Matter._rawSize(code)
87
- raw = raw.slice(0, rize) // copy only exact size from raw stream
88
- if (raw.length != rize) { // forbids shorter
89
- throw new Error(`Not enougth raw bytes for code=${code} expected ${rize} got ${raw.length}.`)
90
- }
281
+ const ls = (3 - (rize % 3)) % 3; // calc actual lead (pad) size
282
+ size = Math.floor((rize + ls) / 3); // calculate value of size in triplets
283
+ if (SmallVrzDex.has(code[0])) {
284
+ if (size <= 64 ** 2 - 1) {
285
+ const hs = 2;
286
+ const s = Object.values(SmallVrzDex)[ls];
287
+ code = `${s}${code.substring(1, hs)}`;
288
+ } else if (size <= 64 ** 4 - 1) {
289
+ const hs = 4;
290
+ const s = Object.values(LargeVrzDex)[ls];
291
+ code = `${s}${'AAAA'.substring(0, hs - 2)}${code[1]}`;
292
+ } else {
293
+ throw new Error(
294
+ `Unsupported raw size for code=${code}`
295
+ );
296
+ }
297
+ } else {
298
+ if (size <= 64 ** 4 - 1) {
299
+ const hs = 4;
300
+ const s = Object.values(LargeVrzDex)[ls];
301
+ code = `${s}${code.substring(1, hs)}`;
302
+ } else {
303
+ throw new Error(
304
+ `Unsupported raw size for code=${code}`
305
+ );
306
+ }
307
+ }
308
+ } else {
309
+ const sizage = Matter.Sizes.get(code);
310
+ if (sizage!.fs == -1) {
311
+ // invalid
312
+ throw new Error(`Unsupported variable size code=${code}`);
313
+ }
91
314
 
92
- this._code = code // hard value part of code
93
- this._size = size // soft value part of code in int
94
- this._raw = raw // crypto ops require bytes not bytearray
315
+ rize = Matter._rawSize(code);
316
+ }
317
+ raw = raw.slice(0, rize); // copy only exact size from raw stream
318
+ if (raw.length != rize) {
319
+ // forbids shorter
320
+ throw new Error(
321
+ `Not enougth raw bytes for code=${code} expected ${rize} got ${raw.length}.`
322
+ );
323
+ }
95
324
 
325
+ this._code = code; // hard value part of code
326
+ this._size = size; // soft value part of code in int
327
+ this._raw = raw; // crypto ops require bytes not bytearray
96
328
  } else if (qb64 !== undefined) {
97
- this._exfil(qb64)
329
+ this._exfil(qb64);
98
330
  } else if (qb64b !== undefined) {
99
- let qb64 = d(qb64b)
100
- this._exfil(qb64)
331
+ const qb64 = d(qb64b);
332
+ this._exfil(qb64);
101
333
  } else if (qb2 !== undefined) {
102
- this._bexfil(qb2)
334
+ this._bexfil(qb2);
103
335
  } else {
104
- throw new EmptyMaterialError("EmptyMaterialError");
336
+ throw new EmptyMaterialError('EmptyMaterialError');
105
337
  }
106
338
  }
107
339
 
@@ -114,7 +346,7 @@ export class Matter {
114
346
  }
115
347
 
116
348
  get raw(): Uint8Array {
117
- return this._raw
349
+ return this._raw;
118
350
  }
119
351
 
120
352
  get qb64() {
@@ -122,131 +354,168 @@ export class Matter {
122
354
  }
123
355
 
124
356
  get qb64b() {
125
- return b(this.qb64)
357
+ return b(this.qb64);
126
358
  }
127
359
 
128
360
  get transferable(): boolean {
129
- return !Array.from([MtrDex.Ed25519N]).includes(this.code)
361
+ return !NonTransDex.has(this.code);
362
+ }
363
+
364
+ get digestive(): boolean {
365
+ return DigiDex.has(this.code);
130
366
  }
131
367
 
132
368
  static _rawSize(code: string) {
133
- let sizage = this.Sizes.get(code) // get sizes
134
- let cs = sizage!.hs + sizage!.ss // both hard + soft code size
369
+ const sizage = this.Sizes.get(code); // get sizes
370
+ const cs = sizage!.hs + sizage!.ss; // both hard + soft code size
135
371
  if (sizage!.fs === -1) {
136
- throw Error(`Non-fixed raw size code ${code}.`)
372
+ throw Error(`Non-fixed raw size code ${code}.`);
137
373
  }
138
374
 
139
- return (Math.floor((sizage!.fs - cs) * 3 / 4)) - sizage!.ls
140
-
375
+ return Math.floor(((sizage!.fs! - cs) * 3) / 4) - sizage!.ls!;
141
376
  }
142
377
 
143
378
  static _leadSize(code: string) {
144
- let sizage = this.Sizes.get(code);
145
- return sizage!.ls
379
+ const sizage = this.Sizes.get(code);
380
+ return sizage!.ls;
146
381
  }
147
382
 
148
383
  get both() {
149
- let sizage = Matter.Sizes.get(this.code);
150
- return `${this.code}${intToB64(this.size, sizage!.ls)}`
384
+ const sizage = Matter.Sizes.get(this.code);
385
+ return `${this.code}${intToB64(this.size, sizage!.ss)}`;
151
386
  }
152
387
 
153
-
154
388
  private _infil() {
155
- let code = this.code;
156
- let raw = this.raw;
389
+ const code = this.code;
390
+ const size = this.size;
391
+ const raw = this.raw;
392
+
393
+ const ps = (3 - (raw.length % 3)) % 3; // pad size chars or lead size bytes
394
+ const sizage = Matter.Sizes.get(code);
395
+
396
+ if (sizage!.fs === undefined) {
397
+ // Variable size code, NOT SUPPORTED
398
+ const cs = sizage!.hs + sizage!.ss;
399
+ if (cs % 4) {
400
+ throw new Error(
401
+ `Whole code size not multiple of 4 for variable length material. cs=${cs}`
402
+ );
403
+ }
404
+ if (size < 0 || size > 64 ** sizage!.ss - 1) {
405
+ throw new Error(`Invalid size=${size} for code=${code}.`);
406
+ }
407
+
408
+ const both = `${code}${intToB64(size, sizage!.ss)}`;
409
+ if (both.length % 4 !== ps - sizage!.ls!) {
410
+ throw new Error(
411
+ `Invalid code=${both} for converted raw pad size=${ps}.`
412
+ );
413
+ }
157
414
 
158
- let ps = ((3 - (raw.length % 3)) % 3); // pad size chars or lead size bytes
159
- let sizage = Matter.Sizes.get(code);
415
+ const bytes = new Uint8Array(sizage!.ls! + raw.length);
416
+ for (let i = 0; i < sizage!.ls!; i++) {
417
+ bytes[i] = 0;
418
+ }
419
+ for (let i = 0; i < raw.length; i++) {
420
+ const odx = i + ps;
421
+ bytes[odx] = raw[i];
422
+ }
160
423
 
161
- if (sizage!.fs === -1) { // Variable size code, NOT SUPPORTED
162
- throw new Error("Variable sized codes not supported... yet");
424
+ return both + Base64.encode(Buffer.from(bytes));
163
425
  } else {
164
- let both = code
165
- let cs = both.length
166
- if ((cs % 4) != ps - sizage!.ls) { // adjusted pad given lead bytes
167
- throw new Error(`Invalid code=${both} for converted raw pad size=${ps}, ${raw.length}.`);
426
+ const both = code;
427
+ const cs = both.length;
428
+ if (cs % 4 != ps - sizage!.ls!) {
429
+ // adjusted pad given lead bytes
430
+ throw new Error(
431
+ `Invalid code=${both} for converted raw pad size=${ps}, ${raw.length}.`
432
+ );
168
433
  }
169
434
  // prepad, convert, and replace upfront
170
435
  // when fixed and ls != 0 then cs % 4 is zero and ps==ls
171
436
  // otherwise fixed and ls == 0 then cs % 4 == ps
172
- let bytes = new Uint8Array(ps + raw.length);
437
+ const bytes = new Uint8Array(ps + raw.length);
173
438
  for (let i = 0; i < ps; i++) {
174
439
  bytes[i] = 0;
175
440
  }
176
441
  for (let i = 0; i < raw.length; i++) {
177
- let odx = i + ps
442
+ const odx = i + ps;
178
443
  bytes[odx] = raw[i];
179
444
  }
180
445
 
181
- return both + Base64.encode(Buffer.from(bytes)).slice(cs % 4)
446
+ return both + Base64.encode(Buffer.from(bytes)).slice(cs % 4);
182
447
  }
183
448
  }
184
449
 
185
450
  private _exfil(qb64: string) {
186
451
  if (qb64.length == 0) {
187
- throw new Error("Empty Material")
452
+ throw new Error('Empty Material');
188
453
  }
189
454
 
190
- let first = qb64[0];
455
+ const first = qb64[0];
191
456
  if (!Array.from(Matter.Hards.keys()).includes(first)) {
192
- throw new Error(`Unexpected code ${first}`)
457
+ throw new Error(`Unexpected code ${first}`);
193
458
  }
194
459
 
195
- let hs = Matter.Hards.get(first);
460
+ const hs = Matter.Hards.get(first);
196
461
  if (qb64.length < hs!) {
197
- throw new Error(`Shortage Error`)
462
+ throw new Error(`Shortage Error`);
198
463
  }
199
464
 
200
- let hard = qb64.slice(0, hs);
465
+ const hard = qb64.slice(0, hs);
201
466
  if (!Array.from(Matter.Sizes.keys()).includes(hard)) {
202
- throw new Error(`Unsupported code ${hard}`)
467
+ throw new Error(`Unsupported code ${hard}`);
203
468
  }
204
469
 
205
- let sizage = Matter.Sizes.get(hard);
206
- let cs = sizage!.hs + sizage!.ss;
470
+ const sizage = Matter.Sizes.get(hard);
471
+ const cs = sizage!.hs + sizage!.ss;
207
472
  let size = -1;
208
- if (sizage!.fs == -1) { // Variable size code, Not supported
209
- throw new Error("Variable size codes not supported yet")
473
+ if (sizage!.fs == -1) {
474
+ // Variable size code, Not supported
475
+ throw new Error('Variable size codes not supported yet');
210
476
  } else {
211
- size = sizage!.fs
477
+ size = sizage!.fs!;
212
478
  }
213
479
 
214
- if (qb64.length < sizage!.fs) {
215
- throw new Error(`Need ${sizage!.fs - qb64.length} more chars.`)
480
+ if (qb64.length < sizage!.fs!) {
481
+ throw new Error(`Need ${sizage!.fs! - qb64.length} more chars.`);
216
482
  }
217
483
 
218
- qb64 = qb64.slice(0, sizage!.fs)
219
- let ps = cs % 4
220
- let pbs = 2 * (ps == 0 ? sizage!.ls : ps)
221
- let raw
484
+ qb64 = qb64.slice(0, sizage!.fs);
485
+ const ps = cs % 4;
486
+ const pbs = 2 * (ps == 0 ? sizage!.ls! : ps);
487
+ let raw;
222
488
  if (ps != 0) {
223
- let base = new Array(ps + 1).join('A') + qb64.slice(cs);
224
- let paw = Base64.decode(base) // decode base to leave prepadded raw
225
- let pi = (readInt(paw.slice(0, ps))) // prepad as int
226
- if (pi & (2 ** pbs - 1)) { // masked pad bits non-zero
227
- throw new Error(`Non zeroed prepad bits = {pi & (2 ** pbs - 1 ):<06b} in {qb64b[cs:cs+1]}.`)
489
+ const base = new Array(ps + 1).join('A') + qb64.slice(cs);
490
+ const paw = Base64.decode(base); // decode base to leave prepadded raw
491
+ const pi = readInt(paw.subarray(0, ps)); // prepad as int
492
+ if (pi & (2 ** pbs - 1)) {
493
+ // masked pad bits non-zero
494
+ throw new Error(
495
+ `Non zeroed prepad bits = {pi & (2 ** pbs - 1 ):<06b} in {qb64b[cs:cs+1]}.`
496
+ );
228
497
  }
229
- raw = paw.slice(ps) // strip off ps prepad paw bytes
498
+ raw = paw.subarray(ps); // strip off ps prepad paw bytes
230
499
  } else {
231
- let base = qb64.slice(cs);
232
- let paw = Base64.decode(base);
233
- let li = readInt(paw.slice(0, sizage!.ls))
500
+ const base = qb64.slice(cs);
501
+ const paw = Base64.decode(base);
502
+ const li = readInt(paw.subarray(0, sizage!.ls));
234
503
  if (li != 0) {
235
504
  if (li == 1) {
236
- throw new Error(`Non zeroed lead byte = 0x{li:02x}.`)
505
+ throw new Error(`Non zeroed lead byte = 0x{li:02x}.`);
237
506
  } else {
238
- throw new Error(`Non zeroed lead bytes = 0x{li:04x}`)
507
+ throw new Error(`Non zeroed lead bytes = 0x{li:04x}`);
239
508
  }
240
509
  }
241
- raw = paw.slice(sizage!.ls)
510
+ raw = paw.subarray(sizage!.ls);
242
511
  }
243
512
 
244
- this._code = hard // hard only
245
- this._size = size
246
- this._raw = Uint8Array.from(raw) // ensure bytes so immutable and for crypto ops
513
+ this._code = hard; // hard only
514
+ this._size = size;
515
+ this._raw = Uint8Array.from(raw); // ensure bytes so immutable and for crypto ops
247
516
  }
248
517
 
249
518
  private _bexfil(qb2: Uint8Array) {
250
- throw new Error(`qb2 not yet supported: ${qb2}`)
519
+ throw new Error(`qb2 not yet supported: ${qb2}`);
251
520
  }
252
521
  }