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.
- package/LICENSE +201 -0
- package/README.md +25 -0
- package/dist/index.d.ts +1 -0
- package/dist/index.js +8 -0
- package/dist/keri/core/cigar.d.ts +8 -0
- package/dist/keri/core/cipher.d.ts +5 -0
- package/dist/keri/core/core.d.ts +68 -0
- package/dist/keri/core/decrypter.d.ts +10 -0
- package/dist/keri/core/diger.d.ts +21 -0
- package/dist/keri/core/encrypter.d.ts +9 -0
- package/dist/keri/core/indexer.d.ts +88 -0
- package/dist/keri/core/keeping.d.ts +1 -0
- package/dist/keri/core/kering.d.ts +5 -0
- package/dist/keri/core/manager.d.ts +151 -0
- package/dist/keri/core/matter.d.ts +47 -0
- package/dist/keri/core/siger.d.ts +22 -0
- package/dist/keri/core/signer.d.ts +34 -0
- package/dist/keri/core/utils.d.ts +16 -0
- package/dist/signify-ts.cjs.development.js +13 -0
- package/dist/signify-ts.cjs.development.js.map +1 -0
- package/dist/signify-ts.cjs.production.min.js +2 -0
- package/dist/signify-ts.cjs.production.min.js.map +1 -0
- package/dist/signify-ts.esm.js +9 -0
- package/dist/signify-ts.esm.js.map +1 -0
- package/package.json +66 -0
- package/src/index.ts +6 -0
- package/src/keri/core/cigar.ts +19 -0
- package/src/keri/core/cipher.ts +25 -0
- package/src/keri/core/core.ts +174 -0
- package/src/keri/core/decrypter.ts +66 -0
- package/src/keri/core/diger.ts +82 -0
- package/src/keri/core/encrypter.ts +60 -0
- package/src/keri/core/indexer.ts +406 -0
- package/src/keri/core/keeping.ts +9 -0
- package/src/keri/core/kering.ts +12 -0
- package/src/keri/core/manager.ts +874 -0
- package/src/keri/core/matter.ts +252 -0
- package/src/keri/core/salter.ts +85 -0
- package/src/keri/core/siger.ts +39 -0
- package/src/keri/core/signer.ts +112 -0
- package/src/keri/core/tsconfig.json +100 -0
- package/src/keri/core/utils.ts +94 -0
- package/src/keri/core/verfer.ts +33 -0
|
@@ -0,0 +1,252 @@
|
|
|
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.
|
|
19
|
+
}
|
|
20
|
+
|
|
21
|
+
export const MtrDex = new MatterCodex()
|
|
22
|
+
|
|
23
|
+
export class Sizage {
|
|
24
|
+
public hs: number;
|
|
25
|
+
public ss: number;
|
|
26
|
+
public ls: number;
|
|
27
|
+
public fs: number;
|
|
28
|
+
|
|
29
|
+
constructor(hs: number, ss: number, fs: number, ls: number) {
|
|
30
|
+
this.hs = hs;
|
|
31
|
+
this.ss = ss;
|
|
32
|
+
this.fs = fs;
|
|
33
|
+
this.ls = ls;
|
|
34
|
+
}
|
|
35
|
+
}
|
|
36
|
+
|
|
37
|
+
export interface MatterArgs {
|
|
38
|
+
raw?: Uint8Array | undefined
|
|
39
|
+
code?: string
|
|
40
|
+
qb64b?: Uint8Array | undefined
|
|
41
|
+
qb64?: string
|
|
42
|
+
qb2?: Uint8Array | undefined
|
|
43
|
+
}
|
|
44
|
+
|
|
45
|
+
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 = "";
|
|
69
|
+
private _size: number = -1;
|
|
70
|
+
private _raw: Uint8Array = new Uint8Array(0);
|
|
71
|
+
|
|
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.")
|
|
78
|
+
}
|
|
79
|
+
|
|
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
|
+
}
|
|
85
|
+
|
|
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
|
+
}
|
|
91
|
+
|
|
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
|
|
95
|
+
|
|
96
|
+
} else if (qb64 !== undefined) {
|
|
97
|
+
this._exfil(qb64)
|
|
98
|
+
} else if (qb64b !== undefined) {
|
|
99
|
+
let qb64 = d(qb64b)
|
|
100
|
+
this._exfil(qb64)
|
|
101
|
+
} else if (qb2 !== undefined) {
|
|
102
|
+
this._bexfil(qb2)
|
|
103
|
+
} else {
|
|
104
|
+
throw new EmptyMaterialError("EmptyMaterialError");
|
|
105
|
+
}
|
|
106
|
+
}
|
|
107
|
+
|
|
108
|
+
get code(): string {
|
|
109
|
+
return this._code;
|
|
110
|
+
}
|
|
111
|
+
|
|
112
|
+
get size() {
|
|
113
|
+
return this._size;
|
|
114
|
+
}
|
|
115
|
+
|
|
116
|
+
get raw(): Uint8Array {
|
|
117
|
+
return this._raw
|
|
118
|
+
}
|
|
119
|
+
|
|
120
|
+
get qb64() {
|
|
121
|
+
return this._infil();
|
|
122
|
+
}
|
|
123
|
+
|
|
124
|
+
get qb64b() {
|
|
125
|
+
return b(this.qb64)
|
|
126
|
+
}
|
|
127
|
+
|
|
128
|
+
get transferable(): boolean {
|
|
129
|
+
return !Array.from([MtrDex.Ed25519N]).includes(this.code)
|
|
130
|
+
}
|
|
131
|
+
|
|
132
|
+
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
|
|
135
|
+
if (sizage!.fs === -1) {
|
|
136
|
+
throw Error(`Non-fixed raw size code ${code}.`)
|
|
137
|
+
}
|
|
138
|
+
|
|
139
|
+
return (Math.floor((sizage!.fs - cs) * 3 / 4)) - sizage!.ls
|
|
140
|
+
|
|
141
|
+
}
|
|
142
|
+
|
|
143
|
+
static _leadSize(code: string) {
|
|
144
|
+
let sizage = this.Sizes.get(code);
|
|
145
|
+
return sizage!.ls
|
|
146
|
+
}
|
|
147
|
+
|
|
148
|
+
get both() {
|
|
149
|
+
let sizage = Matter.Sizes.get(this.code);
|
|
150
|
+
return `${this.code}${intToB64(this.size, sizage!.ls)}`
|
|
151
|
+
}
|
|
152
|
+
|
|
153
|
+
|
|
154
|
+
private _infil() {
|
|
155
|
+
let code = this.code;
|
|
156
|
+
let raw = this.raw;
|
|
157
|
+
|
|
158
|
+
let ps = ((3 - (raw.length % 3)) % 3); // pad size chars or lead size bytes
|
|
159
|
+
let sizage = Matter.Sizes.get(code);
|
|
160
|
+
|
|
161
|
+
if (sizage!.fs === -1) { // Variable size code, NOT SUPPORTED
|
|
162
|
+
throw new Error("Variable sized codes not supported... yet");
|
|
163
|
+
} 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}.`);
|
|
168
|
+
}
|
|
169
|
+
// prepad, convert, and replace upfront
|
|
170
|
+
// when fixed and ls != 0 then cs % 4 is zero and ps==ls
|
|
171
|
+
// otherwise fixed and ls == 0 then cs % 4 == ps
|
|
172
|
+
let bytes = new Uint8Array(ps + raw.length);
|
|
173
|
+
for (let i = 0; i < ps; i++) {
|
|
174
|
+
bytes[i] = 0;
|
|
175
|
+
}
|
|
176
|
+
for (let i = 0; i < raw.length; i++) {
|
|
177
|
+
let odx = i + ps
|
|
178
|
+
bytes[odx] = raw[i];
|
|
179
|
+
}
|
|
180
|
+
|
|
181
|
+
return both + Base64.encode(Buffer.from(bytes)).slice(cs % 4)
|
|
182
|
+
}
|
|
183
|
+
}
|
|
184
|
+
|
|
185
|
+
private _exfil(qb64: string) {
|
|
186
|
+
if (qb64.length == 0) {
|
|
187
|
+
throw new Error("Empty Material")
|
|
188
|
+
}
|
|
189
|
+
|
|
190
|
+
let first = qb64[0];
|
|
191
|
+
if (!Array.from(Matter.Hards.keys()).includes(first)) {
|
|
192
|
+
throw new Error(`Unexpected code ${first}`)
|
|
193
|
+
}
|
|
194
|
+
|
|
195
|
+
let hs = Matter.Hards.get(first);
|
|
196
|
+
if (qb64.length < hs!) {
|
|
197
|
+
throw new Error(`Shortage Error`)
|
|
198
|
+
}
|
|
199
|
+
|
|
200
|
+
let hard = qb64.slice(0, hs);
|
|
201
|
+
if (!Array.from(Matter.Sizes.keys()).includes(hard)) {
|
|
202
|
+
throw new Error(`Unsupported code ${hard}`)
|
|
203
|
+
}
|
|
204
|
+
|
|
205
|
+
let sizage = Matter.Sizes.get(hard);
|
|
206
|
+
let cs = sizage!.hs + sizage!.ss;
|
|
207
|
+
let size = -1;
|
|
208
|
+
if (sizage!.fs == -1) { // Variable size code, Not supported
|
|
209
|
+
throw new Error("Variable size codes not supported yet")
|
|
210
|
+
} else {
|
|
211
|
+
size = sizage!.fs
|
|
212
|
+
}
|
|
213
|
+
|
|
214
|
+
if (qb64.length < sizage!.fs) {
|
|
215
|
+
throw new Error(`Need ${sizage!.fs - qb64.length} more chars.`)
|
|
216
|
+
}
|
|
217
|
+
|
|
218
|
+
qb64 = qb64.slice(0, sizage!.fs)
|
|
219
|
+
let ps = cs % 4
|
|
220
|
+
let pbs = 2 * (ps == 0 ? sizage!.ls : ps)
|
|
221
|
+
let raw
|
|
222
|
+
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]}.`)
|
|
228
|
+
}
|
|
229
|
+
raw = paw.slice(ps) // strip off ps prepad paw bytes
|
|
230
|
+
} else {
|
|
231
|
+
let base = qb64.slice(cs);
|
|
232
|
+
let paw = Base64.decode(base);
|
|
233
|
+
let li = readInt(paw.slice(0, sizage!.ls))
|
|
234
|
+
if (li != 0) {
|
|
235
|
+
if (li == 1) {
|
|
236
|
+
throw new Error(`Non zeroed lead byte = 0x{li:02x}.`)
|
|
237
|
+
} else {
|
|
238
|
+
throw new Error(`Non zeroed lead bytes = 0x{li:04x}`)
|
|
239
|
+
}
|
|
240
|
+
}
|
|
241
|
+
raw = paw.slice(sizage!.ls)
|
|
242
|
+
}
|
|
243
|
+
|
|
244
|
+
this._code = hard // hard only
|
|
245
|
+
this._size = size
|
|
246
|
+
this._raw = Uint8Array.from(raw) // ensure bytes so immutable and for crypto ops
|
|
247
|
+
}
|
|
248
|
+
|
|
249
|
+
private _bexfil(qb2: Uint8Array) {
|
|
250
|
+
throw new Error(`qb2 not yet supported: ${qb2}`)
|
|
251
|
+
}
|
|
252
|
+
}
|
|
@@ -0,0 +1,85 @@
|
|
|
1
|
+
import {Signer} from "./signer";
|
|
2
|
+
|
|
3
|
+
export {};
|
|
4
|
+
const { Matter, MtrDex } = require('./matter');
|
|
5
|
+
const { EmptyMaterialError } = require('./kering')
|
|
6
|
+
import libsodium from 'libsodium-wrappers-sumo';
|
|
7
|
+
|
|
8
|
+
export const enum Tier {
|
|
9
|
+
low = "low",
|
|
10
|
+
med = "med",
|
|
11
|
+
high = "high"
|
|
12
|
+
}
|
|
13
|
+
|
|
14
|
+
interface SalterArgs {
|
|
15
|
+
raw?: Uint8Array | undefined
|
|
16
|
+
code? :string
|
|
17
|
+
tier?:string
|
|
18
|
+
qb64b? :Uint8Array | undefined
|
|
19
|
+
qb64?: string
|
|
20
|
+
qb2?: Uint8Array | undefined
|
|
21
|
+
}
|
|
22
|
+
export class Salter extends Matter {
|
|
23
|
+
|
|
24
|
+
constructor({raw, code = MtrDex.Salt_128, tier=Tier.low, qb64, qb64b, qb2}:SalterArgs) {
|
|
25
|
+
try {
|
|
26
|
+
super({raw, code, qb64, qb64b, qb2});
|
|
27
|
+
} catch (e) {
|
|
28
|
+
if(e instanceof EmptyMaterialError) {
|
|
29
|
+
if (code == MtrDex.Salt_128) {
|
|
30
|
+
let salt = libsodium.randombytes_buf(libsodium.crypto_pwhash_SALTBYTES);
|
|
31
|
+
super({raw: salt, code: code})
|
|
32
|
+
} else {
|
|
33
|
+
throw new Error("invalid code for Salter, only Salt_128 accepted")
|
|
34
|
+
}
|
|
35
|
+
} else {
|
|
36
|
+
throw e;
|
|
37
|
+
}
|
|
38
|
+
}
|
|
39
|
+
|
|
40
|
+
if (this.code != MtrDex.Salt_128) {
|
|
41
|
+
throw new Error("invalid code for Salter, only Salt_128 accepted")
|
|
42
|
+
}
|
|
43
|
+
|
|
44
|
+
this.tier = tier !== null ? tier : Tier.low
|
|
45
|
+
}
|
|
46
|
+
|
|
47
|
+
private stretch(size: number = 32, path: string = "", tier: Tier | null = null, temp: boolean = false): Uint8Array {
|
|
48
|
+
tier = tier == null ? this.tier : tier;
|
|
49
|
+
|
|
50
|
+
let opslimit: number, memlimit: number
|
|
51
|
+
|
|
52
|
+
if (temp) {
|
|
53
|
+
opslimit = libsodium.crypto_pwhash_OPSLIMIT_MIN
|
|
54
|
+
memlimit = libsodium.crypto_pwhash_MEMLIMIT_MIN
|
|
55
|
+
|
|
56
|
+
} else {
|
|
57
|
+
switch (tier) {
|
|
58
|
+
case Tier.low:
|
|
59
|
+
opslimit = libsodium.crypto_pwhash_OPSLIMIT_INTERACTIVE
|
|
60
|
+
memlimit = libsodium.crypto_pwhash_MEMLIMIT_INTERACTIVE
|
|
61
|
+
break;
|
|
62
|
+
case Tier.med:
|
|
63
|
+
opslimit = libsodium.crypto_pwhash_OPSLIMIT_MODERATE
|
|
64
|
+
memlimit = libsodium.crypto_pwhash_MEMLIMIT_MODERATE
|
|
65
|
+
break;
|
|
66
|
+
case Tier.high:
|
|
67
|
+
opslimit = libsodium.crypto_pwhash_OPSLIMIT_SENSITIVE
|
|
68
|
+
memlimit = libsodium.crypto_pwhash_MEMLIMIT_SENSITIVE
|
|
69
|
+
break;
|
|
70
|
+
default:
|
|
71
|
+
throw new Error(`Unsupported security tier = ${tier}.`)
|
|
72
|
+
}
|
|
73
|
+
}
|
|
74
|
+
|
|
75
|
+
return libsodium.crypto_pwhash(size, path, this.raw, opslimit, memlimit, libsodium.crypto_pwhash_ALG_DEFAULT)
|
|
76
|
+
}
|
|
77
|
+
|
|
78
|
+
signer(code: string=MtrDex.Ed25519_Seed, transferable: boolean=true, path: string = "",
|
|
79
|
+
tier: Tier | null = null, temp:boolean = false): Signer {
|
|
80
|
+
let seed = this.stretch(Matter._rawSize(code), path, tier, temp)
|
|
81
|
+
|
|
82
|
+
return new Signer({raw: seed, code: code, transferable: transferable})
|
|
83
|
+
}
|
|
84
|
+
|
|
85
|
+
}
|
|
@@ -0,0 +1,39 @@
|
|
|
1
|
+
import {IdxSigDex, Indexer, IndexerArgs} from "./indexer";
|
|
2
|
+
import {Verfer} from "./verfer";
|
|
3
|
+
|
|
4
|
+
/**
|
|
5
|
+
Siger is subclass of Indexer, indexed signature material,
|
|
6
|
+
Adds .verfer property which is instance of Verfer that provides
|
|
7
|
+
associated signature verifier.
|
|
8
|
+
|
|
9
|
+
See Indexer for inherited attributes and properties:
|
|
10
|
+
|
|
11
|
+
Attributes:
|
|
12
|
+
|
|
13
|
+
Properties:
|
|
14
|
+
.verfer is Verfer object instance
|
|
15
|
+
|
|
16
|
+
Methods:
|
|
17
|
+
**/
|
|
18
|
+
|
|
19
|
+
export class Siger extends Indexer {
|
|
20
|
+
|
|
21
|
+
private _verfer?: Verfer
|
|
22
|
+
constructor({raw, code, qb64, qb64b, qb2}: IndexerArgs, verfer?: Verfer) {
|
|
23
|
+
super({raw, code, qb64, qb64b, qb2});
|
|
24
|
+
|
|
25
|
+
if (!IdxSigDex.has(this.code)) {
|
|
26
|
+
throw new Error(`Invalid code = ${this.code} for Siger.`)
|
|
27
|
+
}
|
|
28
|
+
this._verfer = verfer
|
|
29
|
+
}
|
|
30
|
+
|
|
31
|
+
get verfer(): Verfer | undefined {
|
|
32
|
+
return this._verfer;
|
|
33
|
+
}
|
|
34
|
+
|
|
35
|
+
set verfer(verfer: Verfer | undefined) {
|
|
36
|
+
this._verfer = verfer;
|
|
37
|
+
}
|
|
38
|
+
|
|
39
|
+
}
|
|
@@ -0,0 +1,112 @@
|
|
|
1
|
+
import {EmptyMaterialError} from "./kering";
|
|
2
|
+
|
|
3
|
+
export {};
|
|
4
|
+
import libsodium from 'libsodium-wrappers-sumo';
|
|
5
|
+
import {Matter} from './matter';
|
|
6
|
+
import {MtrDex} from './matter';
|
|
7
|
+
import {Verfer} from "./verfer";
|
|
8
|
+
import {Cigar} from "./cigar";
|
|
9
|
+
import {Siger} from "./siger";
|
|
10
|
+
import {IdrDex} from "./indexer";
|
|
11
|
+
|
|
12
|
+
|
|
13
|
+
/**
|
|
14
|
+
* @description Signer is Matter subclass with method to create signature of serialization
|
|
15
|
+
* It will use .raw as signing (private) key seed
|
|
16
|
+
* .code as cipher suite for signing and new property .verfer whose property
|
|
17
|
+
* .raw is public key for signing.
|
|
18
|
+
* If not provided .verfer is generated from private key seed using .code
|
|
19
|
+
as cipher suite for creating key-pair.
|
|
20
|
+
*/
|
|
21
|
+
interface SignerArgs {
|
|
22
|
+
raw?: Uint8Array | undefined
|
|
23
|
+
code?: string
|
|
24
|
+
qb64b?: Uint8Array | undefined
|
|
25
|
+
qb64?: string
|
|
26
|
+
qb2?: Uint8Array | undefined
|
|
27
|
+
transferable?: boolean
|
|
28
|
+
}
|
|
29
|
+
|
|
30
|
+
export class Signer extends Matter {
|
|
31
|
+
private readonly _sign
|
|
32
|
+
private readonly _verfer: Verfer
|
|
33
|
+
|
|
34
|
+
constructor({raw, code = MtrDex.Ed25519_Seed, qb64, qb64b, qb2, transferable = true}: SignerArgs) {
|
|
35
|
+
try {
|
|
36
|
+
super({raw, code, qb64, qb64b, qb2})
|
|
37
|
+
} catch (e) {
|
|
38
|
+
if (e instanceof EmptyMaterialError) {
|
|
39
|
+
if (code == MtrDex.Ed25519_Seed) {
|
|
40
|
+
let raw = libsodium.randombytes_buf(libsodium.crypto_sign_SEEDBYTES);
|
|
41
|
+
super({raw, code, qb64, qb64b, qb2})
|
|
42
|
+
} else {
|
|
43
|
+
throw new Error(`Unsupported signer code = ${code}.`)
|
|
44
|
+
}
|
|
45
|
+
} else {
|
|
46
|
+
throw e
|
|
47
|
+
}
|
|
48
|
+
}
|
|
49
|
+
let verfer
|
|
50
|
+
if (this.code == MtrDex.Ed25519_Seed) {
|
|
51
|
+
this._sign = this._ed25519
|
|
52
|
+
const keypair = libsodium.crypto_sign_seed_keypair(this.raw)
|
|
53
|
+
verfer = new Verfer({raw: keypair.publicKey, code: transferable ? MtrDex.Ed25519 : MtrDex.Ed25519N})
|
|
54
|
+
|
|
55
|
+
} else {
|
|
56
|
+
throw new Error(`Unsupported signer code = ${this.code}.`)
|
|
57
|
+
}
|
|
58
|
+
|
|
59
|
+
this._verfer = verfer
|
|
60
|
+
|
|
61
|
+
}
|
|
62
|
+
|
|
63
|
+
|
|
64
|
+
/**
|
|
65
|
+
* @description Property verfer:
|
|
66
|
+
Returns Verfer instance
|
|
67
|
+
Assumes ._verfer is correctly assigned
|
|
68
|
+
*/
|
|
69
|
+
get verfer(): Verfer {
|
|
70
|
+
return this._verfer;
|
|
71
|
+
}
|
|
72
|
+
|
|
73
|
+
sign(ser: Uint8Array, index: number | null = null, only: boolean = false, ondex: number | undefined = undefined) {
|
|
74
|
+
return this._sign(ser, this.raw, this.verfer, index, only, ondex)
|
|
75
|
+
}
|
|
76
|
+
|
|
77
|
+
_ed25519(ser: Uint8Array, seed: Uint8Array, verfer: Verfer, index: number | null,
|
|
78
|
+
only: boolean = false, ondex: number | undefined) {
|
|
79
|
+
|
|
80
|
+
let sig = libsodium.crypto_sign_detached(
|
|
81
|
+
ser,
|
|
82
|
+
Buffer.concat([seed, verfer.raw]),
|
|
83
|
+
);
|
|
84
|
+
|
|
85
|
+
if (index == null) {
|
|
86
|
+
return new Cigar({raw: sig, code: MtrDex.Ed25519_Sig}, verfer);
|
|
87
|
+
} else {
|
|
88
|
+
let code;
|
|
89
|
+
if (only) {
|
|
90
|
+
ondex = undefined
|
|
91
|
+
if (index <= 63) {
|
|
92
|
+
code = IdrDex.Ed25519_Crt_Sig
|
|
93
|
+
} else {
|
|
94
|
+
code = IdrDex.Ed25519_Big_Crt_Sig
|
|
95
|
+
}
|
|
96
|
+
} else {
|
|
97
|
+
if(ondex == undefined) {
|
|
98
|
+
ondex = index
|
|
99
|
+
}
|
|
100
|
+
|
|
101
|
+
if (ondex == index && index <= 63) // both same and small
|
|
102
|
+
code = IdrDex.Ed25519_Sig // use small both same
|
|
103
|
+
else // otherwise big or both not same so use big both
|
|
104
|
+
code = IdrDex.Ed25519_Big_Sig // use use big both
|
|
105
|
+
|
|
106
|
+
}
|
|
107
|
+
|
|
108
|
+
return new Siger({raw: sig, code: code, index: index, ondex: ondex}, verfer)
|
|
109
|
+
}
|
|
110
|
+
|
|
111
|
+
}
|
|
112
|
+
}
|
|
@@ -0,0 +1,100 @@
|
|
|
1
|
+
{
|
|
2
|
+
"compilerOptions": {
|
|
3
|
+
/* Visit https://aka.ms/tsconfig.json to read more about this file */
|
|
4
|
+
|
|
5
|
+
/* Projects */
|
|
6
|
+
// "incremental": true, /* Enable incremental compilation */
|
|
7
|
+
// "composite": true, /* Enable constraints that allow a TypeScript project to be used with project references. */
|
|
8
|
+
// "tsBuildInfoFile": "./", /* Specify the folder for .tsbuildinfo incremental compilation files. */
|
|
9
|
+
// "disableSourceOfProjectReferenceRedirect": true, /* Disable preferring source files instead of declaration files when referencing composite projects */
|
|
10
|
+
// "disableSolutionSearching": true, /* Opt a project out of multi-project reference checking when editing. */
|
|
11
|
+
// "disableReferencedProjectLoad": true, /* Reduce the number of projects loaded automatically by TypeScript. */
|
|
12
|
+
|
|
13
|
+
/* Language and Environment */
|
|
14
|
+
"target": "es6", /* Set the JavaScript language version for emitted JavaScript and include compatible library declarations. */
|
|
15
|
+
// "lib": [], /* Specify a set of bundled library declaration files that describe the target runtime environment. */
|
|
16
|
+
// "jsx": "preserve", /* Specify what JSX code is generated. */
|
|
17
|
+
// "experimentalDecorators": true, /* Enable experimental support for TC39 stage 2 draft decorators. */
|
|
18
|
+
// "emitDecoratorMetadata": true, /* Emit design-type metadata for decorated declarations in source files. */
|
|
19
|
+
// "jsxFactory": "", /* Specify the JSX factory function used when targeting React JSX emit, e.g. 'React.createElement' or 'h' */
|
|
20
|
+
// "jsxFragmentFactory": "", /* Specify the JSX Fragment reference used for fragments when targeting React JSX emit e.g. 'React.Fragment' or 'Fragment'. */
|
|
21
|
+
// "jsxImportSource": "", /* Specify module specifier used to import the JSX factory functions when using `jsx: react-jsx*`.` */
|
|
22
|
+
// "reactNamespace": "", /* Specify the object invoked for `createElement`. This only applies when targeting `react` JSX emit. */
|
|
23
|
+
// "noLib": true, /* Disable including any library files, including the default lib.d.ts. */
|
|
24
|
+
// "useDefineForClassFields": true, /* Emit ECMAScript-standard-compliant class fields. */
|
|
25
|
+
|
|
26
|
+
/* Modules */
|
|
27
|
+
"module": "commonjs", /* Specify what module code is generated. */
|
|
28
|
+
// "rootDir": "./", /* Specify the root folder within your source files. */
|
|
29
|
+
// "moduleResolution": "node", /* Specify how TypeScript looks up a file from a given module specifier. */
|
|
30
|
+
// "baseUrl": "./", /* Specify the base directory to resolve non-relative module names. */
|
|
31
|
+
// "paths": {}, /* Specify a set of entries that re-map imports to additional lookup locations. */
|
|
32
|
+
// "rootDirs": [], /* Allow multiple folders to be treated as one when resolving modules. */
|
|
33
|
+
// "typeRoots": [], /* Specify multiple folders that act like `./node_modules/@types`. */
|
|
34
|
+
// "types": [], /* Specify type package names to be included without being referenced in a source file. */
|
|
35
|
+
// "allowUmdGlobalAccess": true, /* Allow accessing UMD globals from modules. */
|
|
36
|
+
// "resolveJsonModule": true, /* Enable importing .json files */
|
|
37
|
+
// "noResolve": true, /* Disallow `import`s, `require`s or `<reference>`s from expanding the number of files TypeScript should add to a project. */
|
|
38
|
+
|
|
39
|
+
/* JavaScript Support */
|
|
40
|
+
// "allowJs": true, /* Allow JavaScript files to be a part of your program. Use the `checkJS` option to get errors from these files. */
|
|
41
|
+
// "checkJs": true, /* Enable error reporting in type-checked JavaScript files. */
|
|
42
|
+
// "maxNodeModuleJsDepth": 1, /* Specify the maximum folder depth used for checking JavaScript files from `node_modules`. Only applicable with `allowJs`. */
|
|
43
|
+
|
|
44
|
+
/* Emit */
|
|
45
|
+
// "declaration": true, /* Generate .d.ts files from TypeScript and JavaScript files in your project. */
|
|
46
|
+
// "declarationMap": true, /* Create sourcemaps for d.ts files. */
|
|
47
|
+
// "emitDeclarationOnly": true, /* Only output d.ts files and not JavaScript files. */
|
|
48
|
+
// "sourceMap": true, /* Create source map files for emitted JavaScript files. */
|
|
49
|
+
// "outFile": "./", /* Specify a file that bundles all outputs into one JavaScript file. If `declaration` is true, also designates a file that bundles all .d.ts output. */
|
|
50
|
+
// "outDir": "./", /* Specify an output folder for all emitted files. */
|
|
51
|
+
// "removeComments": true, /* Disable emitting comments. */
|
|
52
|
+
// "noEmit": true, /* Disable emitting files from a compilation. */
|
|
53
|
+
// "importHelpers": true, /* Allow importing helper functions from tslib once per project, instead of including them per-file. */
|
|
54
|
+
// "importsNotUsedAsValues": "remove", /* Specify emit/checking behavior for imports that are only used for types */
|
|
55
|
+
// "downlevelIteration": true, /* Emit more compliant, but verbose and less performant JavaScript for iteration. */
|
|
56
|
+
// "sourceRoot": "", /* Specify the root path for debuggers to find the reference source code. */
|
|
57
|
+
// "mapRoot": "", /* Specify the location where debugger should locate map files instead of generated locations. */
|
|
58
|
+
// "inlineSourceMap": true, /* Include sourcemap files inside the emitted JavaScript. */
|
|
59
|
+
// "inlineSources": true, /* Include source code in the sourcemaps inside the emitted JavaScript. */
|
|
60
|
+
// "emitBOM": true, /* Emit a UTF-8 Byte Order Mark (BOM) in the beginning of output files. */
|
|
61
|
+
// "newLine": "crlf", /* Set the newline character for emitting files. */
|
|
62
|
+
// "stripInternal": true, /* Disable emitting declarations that have `@internal` in their JSDoc comments. */
|
|
63
|
+
// "noEmitHelpers": true, /* Disable generating custom helper functions like `__extends` in compiled output. */
|
|
64
|
+
// "noEmitOnError": true, /* Disable emitting files if any type checking errors are reported. */
|
|
65
|
+
// "preserveConstEnums": true, /* Disable erasing `const enum` declarations in generated code. */
|
|
66
|
+
// "declarationDir": "./", /* Specify the output directory for generated declaration files. */
|
|
67
|
+
|
|
68
|
+
/* Interop Constraints */
|
|
69
|
+
// "isolatedModules": true, /* Ensure that each file can be safely transpiled without relying on other imports. */
|
|
70
|
+
// "allowSyntheticDefaultImports": true, /* Allow 'import x from y' when a module doesn't have a default export. */
|
|
71
|
+
"esModuleInterop": true, /* Emit additional JavaScript to ease support for importing CommonJS modules. This enables `allowSyntheticDefaultImports` for type compatibility. */
|
|
72
|
+
// "preserveSymlinks": true, /* Disable resolving symlinks to their realpath. This correlates to the same flag in node. */
|
|
73
|
+
"forceConsistentCasingInFileNames": true, /* Ensure that casing is correct in imports. */
|
|
74
|
+
|
|
75
|
+
/* Type Checking */
|
|
76
|
+
"strict": true, /* Enable all strict type-checking options. */
|
|
77
|
+
// "noImplicitAny": true, /* Enable error reporting for expressions and declarations with an implied `any` type.. */
|
|
78
|
+
// "strictNullChecks": true, /* When type checking, take into account `null` and `undefined`. */
|
|
79
|
+
// "strictFunctionTypes": true, /* When assigning functions, check to ensure parameters and the return values are subtype-compatible. */
|
|
80
|
+
// "strictBindCallApply": true, /* Check that the arguments for `bind`, `call`, and `apply` methods match the original function. */
|
|
81
|
+
// "strictPropertyInitialization": true, /* Check for class properties that are declared but not set in the constructor. */
|
|
82
|
+
// "noImplicitThis": true, /* Enable error reporting when `this` is given the type `any`. */
|
|
83
|
+
// "useUnknownInCatchVariables": true, /* Type catch clause variables as 'unknown' instead of 'any'. */
|
|
84
|
+
// "alwaysStrict": true, /* Ensure 'use strict' is always emitted. */
|
|
85
|
+
// "noUnusedLocals": true, /* Enable error reporting when a local variables aren't read. */
|
|
86
|
+
// "noUnusedParameters": true, /* Raise an error when a function parameter isn't read */
|
|
87
|
+
// "exactOptionalPropertyTypes": true, /* Interpret optional property types as written, rather than adding 'undefined'. */
|
|
88
|
+
// "noImplicitReturns": true, /* Enable error reporting for codepaths that do not explicitly return in a function. */
|
|
89
|
+
// "noFallthroughCasesInSwitch": true, /* Enable error reporting for fallthrough cases in switch statements. */
|
|
90
|
+
// "noUncheckedIndexedAccess": true, /* Include 'undefined' in index signature results */
|
|
91
|
+
// "noImplicitOverride": true, /* Ensure overriding members in derived classes are marked with an override modifier. */
|
|
92
|
+
// "noPropertyAccessFromIndexSignature": true, /* Enforces using indexed accessors for keys declared using an indexed type */
|
|
93
|
+
// "allowUnusedLabels": true, /* Disable error reporting for unused labels. */
|
|
94
|
+
// "allowUnreachableCode": true, /* Disable error reporting for unreachable code. */
|
|
95
|
+
|
|
96
|
+
/* Completeness */
|
|
97
|
+
// "skipDefaultLibCheck": true, /* Skip type checking .d.ts files that are included with TypeScript. */
|
|
98
|
+
"skipLibCheck": true /* Skip type checking all .d.ts files. */
|
|
99
|
+
}
|
|
100
|
+
}
|