saro-dat 2.0.0 → 3.0.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/README.md +24 -66
- package/dist/crypto.d.ts.map +1 -1
- package/dist/crypto.js +3 -6
- package/dist/crypto.js.map +1 -1
- package/dist/dat.bank.d.ts.map +1 -1
- package/dist/dat.bank.js +53 -32
- package/dist/dat.bank.js.map +1 -1
- package/dist/dat.certificate.d.ts +16 -0
- package/dist/dat.certificate.d.ts.map +1 -0
- package/dist/dat.certificate.js +64 -0
- package/dist/dat.certificate.js.map +1 -0
- package/dist/dat.d.ts +7 -7
- package/dist/dat.d.ts.map +1 -1
- package/dist/dat.js +14 -14
- package/dist/dat.js.map +1 -1
- package/dist/dat.key.js +69 -56
- package/dist/dat.key.js.map +1 -1
- package/dist/dat.manager.d.ts +15 -0
- package/dist/dat.manager.d.ts.map +1 -0
- package/dist/dat.manager.js +76 -0
- package/dist/dat.manager.js.map +1 -0
- package/dist/index.d.ts +2 -3
- package/dist/index.d.ts.map +1 -1
- package/dist/index.js +2 -3
- package/dist/index.js.map +1 -1
- package/dist/signature.d.ts.map +1 -1
- package/dist/signature.js +8 -19
- package/dist/signature.js.map +1 -1
- package/package.json +1 -1
- package/dist/util.base64.d.ts +0 -6
- package/dist/util.base64.d.ts.map +0 -1
- package/dist/util.base64.js +0 -29
- package/dist/util.base64.js.map +0 -1
- package/dist/util.bytes.d.ts +0 -17
- package/dist/util.bytes.d.ts.map +0 -1
- package/dist/util.bytes.js +0 -85
- package/dist/util.bytes.js.map +0 -1
- package/dist/util.es6.d.ts +0 -7
- package/dist/util.es6.d.ts.map +0 -1
- package/dist/util.es6.js +0 -43
- package/dist/util.es6.js.map +0 -1
- package/dist/util.hex.d.ts +0 -4
- package/dist/util.hex.d.ts.map +0 -1
- package/dist/util.hex.js +0 -33
- package/dist/util.hex.js.map +0 -1
- package/dist/util.misc.d.ts +0 -4
- package/dist/util.misc.d.ts.map +0 -1
- package/dist/util.misc.js +0 -22
- package/dist/util.misc.js.map +0 -1
package/README.md
CHANGED
|
@@ -1,66 +1,12 @@
|
|
|
1
1
|
# DAT - Distributed Access Token
|
|
2
2
|
|
|
3
|
-
|
|
4
|
-
```
|
|
5
|
-
saro-dat
|
|
6
|
-
```
|
|
7
|
-
|
|
8
|
-
# DAT
|
|
9
|
-
```
|
|
10
|
-
# Example
|
|
11
|
-
signature_algorithm: P256
|
|
12
|
-
crypto_algorithm: AES128GCMN
|
|
13
|
-
plain: 123
|
|
14
|
-
secure: asdf
|
|
15
|
-
|
|
16
|
-
# DAT result Example
|
|
17
|
-
1776530737.11.MTIz.8yKUvzs7mg3tDwdeA9I2gNOliewpTgm9OVbEY3Qh6io.qfnqmXKuNE3MfRr576rxNMCchxbY1iqC07-woJcbCudt2O0BAyK_86ypaSfLJjkGq9FZxpGrsgBDkk-xQhGvmA
|
|
18
|
-
```
|
|
19
|
-
|
|
20
|
-
> ```expire```.```kid```.```plain```.```secure```.```sign```
|
|
3
|
+
## Document
|
|
21
4
|
|
|
22
|
-
|
|
23
|
-
- Unix-Timestamp (sec)
|
|
24
|
-
- ```kid```: stringifiable
|
|
25
|
-
- key id
|
|
26
|
-
- ```plain```: base64 url no pad
|
|
27
|
-
- Text Data
|
|
28
|
-
- ```secure```: base64 url no pad
|
|
29
|
-
- Encrypted Text Data
|
|
30
|
-
- ```sign```: base64 url no pad
|
|
31
|
-
- dat-bank\[kid\].sign(expire.kid.plain.secure)
|
|
5
|
+
### [DAT Run Online](https://dat.saro.me)
|
|
32
6
|
|
|
33
|
-
|
|
34
|
-
```
|
|
35
|
-
# Example
|
|
36
|
-
1.2.P256.DErFl-U5h4fdbnAXTTs2GikkJgZwYXV25v2EdFeXIXs.AES128GCMN.5VEziIzCu2LRsK1XS6OYxA.1776541326.1776544626.1800
|
|
37
|
-
```
|
|
38
|
-
> ```version```.```kid```.```signature-algorithm```.```signature-key```.```crypto-algorithm```.```crypto-key```.```issue-begin```.```issue-end```.```token-ttl```
|
|
39
|
-
|
|
40
|
-
- ```version```: number
|
|
41
|
-
- dat-key format version
|
|
42
|
-
- ```kid```: stringifiable
|
|
43
|
-
- key id
|
|
44
|
-
- ```signature-algorithm```: text
|
|
45
|
-
- sign algorithm
|
|
46
|
-
- ```signature-key```: base64 url no pad
|
|
47
|
-
> The signature-key is categorized into three types: FULL (signing-key~verifying-key), SIGNING (signing-key), and VERIFYING (~verifying-key).
|
|
48
|
-
>
|
|
49
|
-
> Whether a key is for sign or verify can be distinguished by the presence of a leading tilde (~). Generally, if you output the sign key alone, the public key can be derived from it (using the private key). However, depending on the platform, this derivation feature may not be available; in such cases, you should output the full key and parse it for use.
|
|
50
|
-
- FULL: \<signing key base64\>~\<verifying key base64\>
|
|
51
|
-
- SIGNING: \<signing key base64\>
|
|
52
|
-
- VERIFYING: ~\<verifying key base64\>
|
|
53
|
-
- ```crypto-algorithm```: text
|
|
54
|
-
- crypto algorithm,
|
|
55
|
-
- ```crypto-key```: base64 url no pad
|
|
56
|
-
- crypto key
|
|
57
|
-
- ```issue-begin```: number
|
|
58
|
-
- issue begin time
|
|
59
|
-
- ```issue-end```: number
|
|
60
|
-
- issue end time
|
|
61
|
-
- ```token-ttl```: number
|
|
62
|
-
- token(dat) TTL
|
|
7
|
+
### [What is DAT](https://dat.saro.me/--/intro)
|
|
63
8
|
|
|
9
|
+
### [Example](https://dat.saro.me/--/libs/npm-saro-dat)
|
|
64
10
|
|
|
65
11
|
## support signature algorithm
|
|
66
12
|
| name | algorithm |
|
|
@@ -76,11 +22,23 @@ secure: asdf
|
|
|
76
22
|
| AES256GCMN | aes-256-cbc n(nonce + body) |
|
|
77
23
|
|
|
78
24
|
|
|
79
|
-
|
|
80
|
-
-
|
|
81
|
-
|
|
82
|
-
|
|
83
|
-
|
|
84
|
-
|
|
85
|
-
|
|
86
|
-
|
|
25
|
+
# Performance
|
|
26
|
+
- random plain and secure test
|
|
27
|
+
- mac mini m4 2024 basic (10 core)
|
|
28
|
+
- [performance.test.ts](src/performance.test.ts)
|
|
29
|
+
```
|
|
30
|
+
plain: oPlAzKK2N62hHCIvJhvqkiBXwzt8eiS5vLavzL409GthN9ZeVZyr0uDFPfVEYwiC5BQ5Yuwp4z1mW5cFIZI161C8skuLmK9DrozH
|
|
31
|
+
secure: jNfQ5AVhjJVRfG3vmBF2NrpZLFpbOaeUbUMaTqlbOEnZ0cGtzLfDf1rLV9H4G6AstYn6QBaQhyiuJHcHLFTsunPyS6Bm3WKQE0Ih
|
|
32
|
+
P256/AES128GCMN Issue * 10000 : 455ms
|
|
33
|
+
P256/AES128GCMN Parse * 10000 : 700ms
|
|
34
|
+
P256/AES256GCMN Issue * 10000 : 443ms
|
|
35
|
+
P256/AES256GCMN Parse * 10000 : 688ms
|
|
36
|
+
P384/AES128GCMN Issue * 10000 : 4022ms
|
|
37
|
+
P384/AES128GCMN Parse * 10000 : 3466ms
|
|
38
|
+
P384/AES256GCMN Issue * 10000 : 4043ms
|
|
39
|
+
P384/AES256GCMN Parse * 10000 : 3448ms
|
|
40
|
+
P521/AES128GCMN Issue * 10000 : 9544ms
|
|
41
|
+
P521/AES128GCMN Parse * 10000 : 7433ms
|
|
42
|
+
P521/AES256GCMN Issue * 10000 : 9485ms
|
|
43
|
+
P521/AES256GCMN Parse * 10000 : 7419ms
|
|
44
|
+
```
|
package/dist/crypto.d.ts.map
CHANGED
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"crypto.d.ts","sourceRoot":"","sources":["../src/crypto.ts"],"names":[],"mappings":"AAIA,MAAM,MAAM,kBAAkB,GAAG,YAAY,GAAG,YAAY,CAAC;AAC7D,eAAO,MAAM,mBAAmB,EAAE,kBAAkB,EAAiC,CAAC;AAEtF,KAAK,YAAY,GAAG;IAAE,IAAI,EAAE,MAAM,CAAC;IAAC,MAAM,EAAE,MAAM,CAAA;CAAE,CAAC;
|
|
1
|
+
{"version":3,"file":"crypto.d.ts","sourceRoot":"","sources":["../src/crypto.ts"],"names":[],"mappings":"AAIA,MAAM,MAAM,kBAAkB,GAAG,YAAY,GAAG,YAAY,CAAC;AAC7D,eAAO,MAAM,mBAAmB,EAAE,kBAAkB,EAAiC,CAAC;AAEtF,KAAK,YAAY,GAAG;IAAE,IAAI,EAAE,MAAM,CAAC;IAAC,MAAM,EAAE,MAAM,CAAA;CAAE,CAAC;AAerD,qBAAa,YAAY;IACrB,OAAO,CAAC,QAAQ,CAAC,MAAM,CAAe;IACtC,SAAgB,SAAS,EAAE,kBAAkB,CAAC;IAC9C,SAAgB,GAAG,EAAE,SAAS,CAAC;gBAG3B,SAAS,EAAE,kBAAkB,EAC7B,GAAG,EAAE,SAAS,EACd,MAAM,GAAE,YAAyC;WAOxC,QAAQ,CAAC,SAAS,EAAE,kBAAkB,GAAG,OAAO,CAAC,YAAY,CAAC;WAQ9D,OAAO,CAAC,SAAS,EAAE,MAAM,EAAE,MAAM,EAAE,MAAM,GAAG,OAAO,CAAC,YAAY,CAAC;IASxE,OAAO,IAAI,OAAO,CAAC,MAAM,CAAC;IAI1B,OAAO,CAAC,IAAI,EAAE,eAAe,GAAC,UAAU,GAAC,MAAM,GAAC,IAAI,GAAC,SAAS,GAAG,OAAO,CAAC,WAAW,CAAC;IAiBrF,OAAO,CAAC,IAAI,EAAE,eAAe,GAAC,UAAU,GAAC,MAAM,GAAC,IAAI,GAAC,SAAS,GAAG,OAAO,CAAC,WAAW,CAAC;CAgB9F"}
|
package/dist/crypto.js
CHANGED
|
@@ -4,15 +4,12 @@ const CRYPTO_CONFIG = {
|
|
|
4
4
|
"AES128GCMN": { name: "AES-GCM", length: 128 },
|
|
5
5
|
"AES256GCMN": { name: "AES-GCM", length: 256 },
|
|
6
6
|
};
|
|
7
|
-
function unsupportedAlgorithm(algorithm) {
|
|
8
|
-
return new Error(`Unsupported DAT Crypto Algorithm: ${algorithm}`);
|
|
9
|
-
}
|
|
10
7
|
function getCryptoConfig(algorithm) {
|
|
11
8
|
const config = CRYPTO_CONFIG[algorithm];
|
|
12
9
|
if (config) {
|
|
13
10
|
return config;
|
|
14
11
|
}
|
|
15
|
-
throw
|
|
12
|
+
throw new Error(`Unsupported DAT Crypto Algorithm: ${algorithm}`);
|
|
16
13
|
}
|
|
17
14
|
export class DatCryptoKey {
|
|
18
15
|
config;
|
|
@@ -48,7 +45,7 @@ export class DatCryptoKey {
|
|
|
48
45
|
const encrypt = await crypto.subtle.encrypt({ name: this.config.name, iv: nonce }, this.key, buffer);
|
|
49
46
|
return DatArrayBuffer.concat(nonce, encrypt);
|
|
50
47
|
}
|
|
51
|
-
throw
|
|
48
|
+
throw new Error(`Unsupported DAT Crypto Algorithm: ${this.algorithm}`);
|
|
52
49
|
}
|
|
53
50
|
async decrypt(data) {
|
|
54
51
|
const buffer = DatArrayBuffer.fromBase64Url(data);
|
|
@@ -62,7 +59,7 @@ export class DatCryptoKey {
|
|
|
62
59
|
const nonce = buffer.slice(0, 12);
|
|
63
60
|
return await crypto.subtle.decrypt({ name: this.config.name, iv: nonce }, this.key, buffer.slice(12));
|
|
64
61
|
}
|
|
65
|
-
throw
|
|
62
|
+
throw new Error(`Unsupported DAT Crypto Algorithm: ${this.algorithm}`);
|
|
66
63
|
}
|
|
67
64
|
}
|
|
68
65
|
//# sourceMappingURL=crypto.js.map
|
package/dist/crypto.js.map
CHANGED
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"crypto.js","sourceRoot":"","sources":["../src/crypto.ts"],"names":[],"mappings":"AAAA,OAAO,EACH,cAAc,GACjB,MAAM,YAAY,CAAC;AAGpB,MAAM,CAAC,MAAM,mBAAmB,GAAyB,CAAC,YAAY,EAAE,YAAY,CAAC,CAAC;AAItF,MAAM,aAAa,GAAiC;IAChD,YAAY,EAAE,EAAE,IAAI,EAAE,SAAS,EAAE,MAAM,EAAE,GAAG,EAAE;IAC9C,YAAY,EAAE,EAAE,IAAI,EAAE,SAAS,EAAE,MAAM,EAAE,GAAG,EAAE;CACjD,CAAC;AAEF,SAAS,
|
|
1
|
+
{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CAAC,MAAM,CAAC,KAAK,EAAE,OAAO,CAAC,CAAC;QACjD,CAAC;QACD,MAAM,IAAI,KAAK,CAAC,qCAAqC,IAAI,CAAC,SAAS,EAAE,CAAC,CAAC;IAC3E,CAAC;IAED,KAAK,CAAC,OAAO,CAAC,IAAsD;QAChE,MAAM,MAAM,GAAgB,cAAc,CAAC,aAAa,CAAC,IAAI,CAAC,CAAC;QAC/D,IAAI,CAAC,MAAM,CAAC,UAAU,EAAE,CAAC;YACrB,OAAO,MAAM,CAAC;QAClB,CAAC;QACD,IAAI,IAAI,CAAC,MAAM,CAAC,IAAI,IAAI,SAAS,EAAE,CAAC;YAChC,IAAI,MAAM,CAAC,UAAU,IAAI,EAAE,EAAE,CAAC;gBAC1B,MAAM,IAAI,KAAK,CAAC,qBAAqB,CAAC,CAAC;YAC3C,CAAC;YACD,MAAM,KAAK,GAAG,MAAM,CAAC,KAAK,CAAC,CAAC,EAAE,EAAE,CAAC,CAAC;YAClC,OAAO,MAAM,MAAM,CAAC,MAAM,CAAC,OAAO,CAC9B,EAAE,IAAI,EAAE,IAAI,CAAC,MAAM,CAAC,IAAI,EAAE,EAAE,EAAE,KAAK,EAAE,EAAE,IAAI,CAAC,GAAG,EAAE,MAAM,CAAC,KAAK,CAAC,EAAE,CAAC,CACpE,CAAC;QACN,CAAC;QACD,MAAM,IAAI,KAAK,CAAC,qCAAqC,IAAI,CAAC,SAAS,EAAE,CAAC,CAAC;IAC3E,CAAC;CACJ"}
|
package/dist/dat.bank.d.ts.map
CHANGED
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"dat.bank.d.ts","sourceRoot":"","sources":["../src/dat.bank.ts"],"names":[],"mappings":"AAAA,OAAO,EAAC,GAAG,EAAE,MAAM,EAAE,UAAU,EAAE,wBAAwB,
|
|
1
|
+
{"version":3,"file":"dat.bank.d.ts","sourceRoot":"","sources":["../src/dat.bank.ts"],"names":[],"mappings":"AAAA,OAAO,EAAC,GAAG,EAAE,MAAM,EAAE,UAAU,EAAE,wBAAwB,EAAG,MAAM,YAAY,CAAC;AAE/E,qBAAa,OAAO;IAChB,OAAO,CAAC,QAAQ,CAAC,QAAQ,CAAgB;IACzC,OAAO,CAAC,QAAQ,CAAC,UAAU,CAAW;gBAE1B,QAAQ,GAAE,MAAM,GAAG,IAAW,EAAE,UAAU,GAAE,MAAM,EAAO;WAKxD,OAAO,CAAC,MAAM,EAAE,MAAM,GAAG,OAAO,CAAC,OAAO,CAAC;IA0BhD,OAAO,CAAC,wBAAwB,EAAE,wBAAwB,GAAG,OAAO,CAAC,MAAM,CAAC;IAIlF,IAAI,CAAC,GAAG,EAAE,MAAM,GAAG,MAAM,GAAG,IAAI;IAI1B,KAAK,CAAC,KAAK,EAAE,eAAe,GAAC,UAAU,GAAC,MAAM,GAAC,IAAI,GAAC,SAAS,EAAE,MAAM,EAAE,eAAe,GAAC,UAAU,GAAC,MAAM,GAAC,IAAI,GAAC,SAAS,GAAG,OAAO,CAAC,MAAM,CAAC;IAOzI,SAAS,CAAC,GAAG,EAAE,GAAG,GAAC,MAAM,GAAC,SAAS,GAAC,IAAI,GAAG,OAAO,CAAC,UAAU,CAAC;CAUvE"}
|
package/dist/dat.bank.js
CHANGED
|
@@ -1,47 +1,68 @@
|
|
|
1
|
-
|
|
1
|
+
var __awaiter = (this && this.__awaiter) || function (thisArg, _arguments, P, generator) {
|
|
2
|
+
function adopt(value) { return value instanceof P ? value : new P(function (resolve) { resolve(value); }); }
|
|
3
|
+
return new (P || (P = Promise))(function (resolve, reject) {
|
|
4
|
+
function fulfilled(value) { try { step(generator.next(value)); } catch (e) { reject(e); } }
|
|
5
|
+
function rejected(value) { try { step(generator["throw"](value)); } catch (e) { reject(e); } }
|
|
6
|
+
function step(result) { result.done ? resolve(result.value) : adopt(result.value).then(fulfilled, rejected); }
|
|
7
|
+
step((generator = generator.apply(thisArg, _arguments || [])).next());
|
|
8
|
+
});
|
|
9
|
+
};
|
|
10
|
+
import { Dat, DatKey, } from "./index.js";
|
|
2
11
|
export class DatBank {
|
|
3
|
-
issueKey;
|
|
4
|
-
verifyKeys;
|
|
5
12
|
constructor(issueKey = null, verifyKeys = []) {
|
|
6
13
|
this.issueKey = issueKey;
|
|
7
14
|
this.verifyKeys = verifyKeys;
|
|
8
15
|
}
|
|
9
|
-
static
|
|
10
|
-
|
|
11
|
-
|
|
12
|
-
|
|
13
|
-
|
|
14
|
-
|
|
15
|
-
|
|
16
|
-
|
|
16
|
+
static imports(format) {
|
|
17
|
+
return __awaiter(this, void 0, void 0, function* () {
|
|
18
|
+
const lines = format.split('\n').map(e => e.trim()).filter(e => !!e);
|
|
19
|
+
let keys = [];
|
|
20
|
+
for (const line of lines) {
|
|
21
|
+
const key = yield DatKey.imports(line);
|
|
22
|
+
if (keys.find(e => e.kid === key.kid)) {
|
|
23
|
+
throw new Error(`Invalid DAT KEYS - Duplicate KID ${key.kid}`);
|
|
24
|
+
}
|
|
25
|
+
keys.push(key);
|
|
17
26
|
}
|
|
18
|
-
keys.
|
|
19
|
-
|
|
20
|
-
|
|
21
|
-
|
|
22
|
-
|
|
27
|
+
keys.sort((a, b) => a.issueBegin - b.issueBegin);
|
|
28
|
+
let issueKey = null;
|
|
29
|
+
for (let i = keys.length - 1; i >= 0; i--) {
|
|
30
|
+
const key = keys[i];
|
|
31
|
+
if (key.isIssuable()) {
|
|
32
|
+
issueKey = key;
|
|
33
|
+
break;
|
|
34
|
+
}
|
|
35
|
+
}
|
|
36
|
+
return new DatBank(issueKey, keys);
|
|
37
|
+
});
|
|
23
38
|
}
|
|
24
|
-
|
|
25
|
-
return (
|
|
39
|
+
exports(datSignatureKeyOutOption) {
|
|
40
|
+
return __awaiter(this, void 0, void 0, function* () {
|
|
41
|
+
return (yield Promise.all(this.verifyKeys.map(e => e.exports(datSignatureKeyOutOption)))).join('\n');
|
|
42
|
+
});
|
|
26
43
|
}
|
|
27
44
|
find(kid) {
|
|
28
45
|
return this.verifyKeys.find(e => e.kid === kid) || null;
|
|
29
46
|
}
|
|
30
|
-
|
|
31
|
-
|
|
32
|
-
|
|
33
|
-
|
|
34
|
-
|
|
47
|
+
toDat(plain, secure) {
|
|
48
|
+
return __awaiter(this, void 0, void 0, function* () {
|
|
49
|
+
if (this.issueKey) {
|
|
50
|
+
return yield this.issueKey.toDat(plain, secure);
|
|
51
|
+
}
|
|
52
|
+
throw new Error("Invalid DAT: Signing Key Does Not Exist");
|
|
53
|
+
});
|
|
35
54
|
}
|
|
36
|
-
|
|
37
|
-
|
|
38
|
-
|
|
39
|
-
|
|
40
|
-
|
|
41
|
-
|
|
42
|
-
|
|
43
|
-
|
|
44
|
-
|
|
55
|
+
toPayload(dat) {
|
|
56
|
+
return __awaiter(this, void 0, void 0, function* () {
|
|
57
|
+
if (!(dat = Dat.from(dat)).format) {
|
|
58
|
+
throw new Error("Invalid DAT: Format");
|
|
59
|
+
}
|
|
60
|
+
const key = this.find(dat.kid);
|
|
61
|
+
if (key != null) {
|
|
62
|
+
return key.toPayload(dat);
|
|
63
|
+
}
|
|
64
|
+
throw new Error("Invalid DAT: KID Not Found");
|
|
65
|
+
});
|
|
45
66
|
}
|
|
46
67
|
}
|
|
47
68
|
//# sourceMappingURL=dat.bank.js.map
|
package/dist/dat.bank.js.map
CHANGED
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"dat.bank.js","sourceRoot":"","sources":["../src/dat.bank.ts"],"names":[],"mappings":"AAAA,OAAO,EAAC,GAAG,EAAE,MAAM,
|
|
1
|
+
{"version":3,"file":"dat.bank.js","sourceRoot":"","sources":["../src/dat.bank.ts"],"names":[],"mappings":";;;;;;;;;AAAA,OAAO,EAAC,GAAG,EAAE,MAAM,GAAyC,MAAM,YAAY,CAAC;AAE/E,MAAM,OAAO,OAAO;IAIhB,YAAY,WAA0B,IAAI,EAAE,aAAuB,EAAE;QACjE,IAAI,CAAC,QAAQ,GAAG,QAAQ,CAAC;QACzB,IAAI,CAAC,UAAU,GAAG,UAAU,CAAC;IACjC,CAAC;IAED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+
readonly _signatureKey: DatSignatureKey;
|
|
5
|
+
readonly _cryptoKey: DatCryptoKey;
|
|
6
|
+
readonly _datIssueBegin: number;
|
|
7
|
+
readonly _datIssueEnd: number;
|
|
8
|
+
readonly _datTtl: number;
|
|
9
|
+
constructor(certificateId: number, signatureKey: DatSignatureKey, cryptoKey: DatCryptoKey, datIssueBegin: number, datIssueEnd: number, datTtl: number);
|
|
10
|
+
exports(datSignatureKeyOutOption: DatSignatureKeyOutOption): Promise<string>;
|
|
11
|
+
static imports(format: string): Promise<DatCertificate>;
|
|
12
|
+
issuable(): boolean;
|
|
13
|
+
expired(): boolean;
|
|
14
|
+
hasSigningKey(): boolean;
|
|
15
|
+
}
|
|
16
|
+
//# sourceMappingURL=dat.certificate.d.ts.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"dat.certificate.d.ts","sourceRoot":"","sources":["../src/dat.certificate.ts"],"names":[],"mappings":"AAAA,OAAO,EAAC,YAAY,EAAc,eAAe,EAAE,wBAAwB,EAAe,MAAM,YAAY,CAAC;AAE7G,qBAAa,cAAc;IACvB,SAAgB,aAAa,EAAE,MAAM,CAAC;IACtC,SAAgB,aAAa,EAAE,eAAe,CAAC;IAC/C,SAAgB,UAAU,EAAE,YAAY,CAAC;IACzC,SAAgB,cAAc,EAAE,MAAM,CAAC;IACvC,SAAgB,YAAY,EAAE,MAAM,CAAC;IACrC,SAAgB,OAAO,EAAE,MAAM,CAAC;gBAG5B,aAAa,EAAE,MAAM,EACrB,YAAY,EAAE,eAAe,EAC7B,SAAS,EAAE,YAAY,EACvB,aAAa,EAAE,MAAM,EACrB,WAAW,EAAE,MAAM,EACnB,MAAM,EAAE,MAAM;IAsBZ,OAAO,CAAC,wBAAwB,EAAE,wBAAwB,GAAG,OAAO,CAAC,MAAM,CAAC;WAYrE,OAAO,CAAC,MAAM,EAAE,MAAM,GAAG,OAAO,CAAC,cAAc,CAAC;IAc7D,QAAQ,IAAI,OAAO;IAKnB,OAAO,IAAI,OAAO;IAIlB,aAAa,IAAI,OAAO;CAG3B"}
|
|
@@ -0,0 +1,64 @@
|
|
|
1
|
+
import { DatCryptoKey, DatInteger, DatSignatureKey, DatUnixtime, } from "./index.js";
|
|
2
|
+
export class DatCertificate {
|
|
3
|
+
certificateId;
|
|
4
|
+
_signatureKey;
|
|
5
|
+
_cryptoKey;
|
|
6
|
+
_datIssueBegin;
|
|
7
|
+
_datIssueEnd;
|
|
8
|
+
_datTtl;
|
|
9
|
+
constructor(certificateId, signatureKey, cryptoKey, datIssueBegin, datIssueEnd, datTtl) {
|
|
10
|
+
if (!DatInteger.isBetween(certificateId, 0)) {
|
|
11
|
+
throw new Error(`Invalid certificate id: certificate id is positive int or 0 ${datIssueBegin}`);
|
|
12
|
+
}
|
|
13
|
+
if (!DatInteger.isBetween(datIssueBegin, 0)) {
|
|
14
|
+
throw new Error(`Invalid issueBegin: issueBegin is positive int or 0 ${datIssueBegin}`);
|
|
15
|
+
}
|
|
16
|
+
if (!DatInteger.isBetween(datIssueEnd, 0)) {
|
|
17
|
+
throw new Error(`Invalid issueEnd: issueEnd is positive int or 0 ${datIssueEnd}`);
|
|
18
|
+
}
|
|
19
|
+
if (!DatInteger.isBetween(datTtl, 1)) {
|
|
20
|
+
throw new Error(`Invalid datTtl: datTtl is positive int ${datTtl}`);
|
|
21
|
+
}
|
|
22
|
+
this.certificateId = certificateId;
|
|
23
|
+
this._signatureKey = signatureKey;
|
|
24
|
+
this._cryptoKey = cryptoKey;
|
|
25
|
+
this._datIssueBegin = Math.floor(datIssueBegin);
|
|
26
|
+
this._datIssueEnd = Math.floor(datIssueEnd);
|
|
27
|
+
this._datTtl = Math.floor(datTtl);
|
|
28
|
+
}
|
|
29
|
+
async exports(datSignatureKeyOutOption) {
|
|
30
|
+
const certificateId = this.certificateId.toString(16);
|
|
31
|
+
const signatureAlgorithm = this._signatureKey.algorithm;
|
|
32
|
+
const signatureKey = await this._signatureKey.exports(datSignatureKeyOutOption);
|
|
33
|
+
const cryptoAlg = this._cryptoKey.algorithm;
|
|
34
|
+
const cryptoKey = await this._cryptoKey.exports();
|
|
35
|
+
const datIssueBegin = this._datIssueBegin;
|
|
36
|
+
const datIssueEnd = this._datIssueEnd;
|
|
37
|
+
const datTtl = this._datTtl;
|
|
38
|
+
return `${certificateId}.${signatureAlgorithm}.${signatureKey}.${cryptoAlg}.${cryptoKey}.${datIssueBegin}.${datIssueEnd}.${datTtl}`;
|
|
39
|
+
}
|
|
40
|
+
static async imports(format) {
|
|
41
|
+
const split = format.split(".");
|
|
42
|
+
if (split.length == 8) {
|
|
43
|
+
const certificateId = Number.parseInt(split[0], 16);
|
|
44
|
+
const signatureKey = await DatSignatureKey.imports(split[1], split[2]);
|
|
45
|
+
const cryptoKey = await DatCryptoKey.imports(split[3], split[4]);
|
|
46
|
+
const datIssueBegin = Number(split[5]);
|
|
47
|
+
const datIssueEnd = Number(split[6]);
|
|
48
|
+
const datTtl = Number(split[7]);
|
|
49
|
+
return new DatCertificate(certificateId, signatureKey, cryptoKey, datIssueBegin, datIssueEnd, datTtl);
|
|
50
|
+
}
|
|
51
|
+
throw new Error("Invalid Certificate format");
|
|
52
|
+
}
|
|
53
|
+
issuable() {
|
|
54
|
+
const now = DatUnixtime.now();
|
|
55
|
+
return this.hasSigningKey() && now >= this._datIssueBegin && now <= this._datIssueEnd;
|
|
56
|
+
}
|
|
57
|
+
expired() {
|
|
58
|
+
return DatUnixtime.isPast(this._datIssueEnd + this._datTtl);
|
|
59
|
+
}
|
|
60
|
+
hasSigningKey() {
|
|
61
|
+
return this._signatureKey.hasSigningKey();
|
|
62
|
+
}
|
|
63
|
+
}
|
|
64
|
+
//# sourceMappingURL=dat.certificate.js.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"dat.certificate.js","sourceRoot":"","sources":["../src/dat.certificate.ts"],"names":[],"mappings":"AAAA,OAAO,EAAC,YAAY,EAAE,UAAU,EAAE,eAAe,EAA4B,WAAW,GAAE,MAAM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vC,MAAM,WAAW,GAAG,MAAM,CAAC,KAAK,CAAC,CAAC,CAAC,CAAC,CAAC;YACrC,MAAM,MAAM,GAAG,MAAM,CAAC,KAAK,CAAC,CAAC,CAAC,CAAC,CAAC;YAChC,OAAO,IAAI,cAAc,CAAC,aAAa,EAAE,YAAY,EAAE,SAAS,EAAE,aAAa,EAAE,WAAW,EAAE,MAAM,CAAC,CAAA;QACzG,CAAC;QACD,MAAM,IAAI,KAAK,CAAC,4BAA4B,CAAC,CAAC;IAClD,CAAC;IAED,QAAQ;QACJ,MAAM,GAAG,GAAG,WAAW,CAAC,GAAG,EAAE,CAAC;QAC9B,OAAO,IAAI,CAAC,aAAa,EAAE,IAAI,GAAG,IAAI,IAAI,CAAC,cAAc,IAAI,GAAG,IAAI,IAAI,CAAC,YAAY,CAAA;IACzF,CAAC;IAED,OAAO;QACH,OAAO,WAAW,CAAC,MAAM,CAAC,IAAI,CAAC,YAAY,GAAG,IAAI,CAAC,OAAO,CAAC,CAAC;IAChE,CAAC;IAED,aAAa;QACT,OAAO,IAAI,CAAC,aAAa,CAAC,aAAa,EAAE,CAAC;IAC9C,CAAC;CACJ"}
|
package/dist/dat.d.ts
CHANGED
|
@@ -1,15 +1,15 @@
|
|
|
1
1
|
export declare class Dat {
|
|
2
|
-
readonly format: boolean;
|
|
3
2
|
readonly dat: string;
|
|
4
|
-
readonly
|
|
5
|
-
readonly
|
|
6
|
-
readonly
|
|
7
|
-
readonly
|
|
8
|
-
readonly
|
|
3
|
+
readonly _format: boolean;
|
|
4
|
+
readonly _expire: number;
|
|
5
|
+
readonly _certificateId: number;
|
|
6
|
+
readonly _plain: ArrayBuffer;
|
|
7
|
+
readonly _secure: ArrayBuffer;
|
|
8
|
+
readonly _signature: ArrayBuffer;
|
|
9
9
|
constructor(dat: string | undefined | null);
|
|
10
10
|
static from(dat: Dat | string | undefined | null): Dat;
|
|
11
11
|
expired(): boolean;
|
|
12
|
-
|
|
12
|
+
_body(): string;
|
|
13
13
|
}
|
|
14
14
|
export declare class DatPayload {
|
|
15
15
|
readonly expire: number;
|
package/dist/dat.d.ts.map
CHANGED
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"dat.d.ts","sourceRoot":"","sources":["../src/dat.ts"],"names":[],"mappings":"AAOA,qBAAa,GAAG;IACZ,QAAQ,CAAC,
|
|
1
|
+
{"version":3,"file":"dat.d.ts","sourceRoot":"","sources":["../src/dat.ts"],"names":[],"mappings":"AAOA,qBAAa,GAAG;IACZ,QAAQ,CAAC,GAAG,EAAE,MAAM,CAAM;IAC1B,QAAQ,CAAC,OAAO,EAAE,OAAO,CAAS;IAClC,QAAQ,CAAC,OAAO,EAAE,MAAM,CAAK;IAC7B,QAAQ,CAAC,cAAc,EAAE,MAAM,CAAK;IACpC,QAAQ,CAAC,MAAM,EAAE,WAAW,CAAsB;IAClD,QAAQ,CAAC,OAAO,EAAE,WAAW,CAAsB;IACnD,QAAQ,CAAC,UAAU,EAAE,WAAW,CAAsB;gBAE1C,GAAG,EAAE,MAAM,GAAC,SAAS,GAAC,IAAI;IAgBtC,MAAM,CAAC,IAAI,CAAC,GAAG,EAAE,GAAG,GAAC,MAAM,GAAC,SAAS,GAAC,IAAI,GAAG,GAAG;IAOhD,OAAO,IAAI,OAAO;IAIlB,KAAK,IAAI,MAAM;CAGlB;AAED,qBAAa,UAAU;IACnB,QAAQ,CAAC,MAAM,EAAE,MAAM,CAAC;IACxB,QAAQ,CAAC,UAAU,EAAE,WAAW,CAAC;IACjC,QAAQ,CAAC,WAAW,EAAE,WAAW,CAAC;gBAEtB,MAAM,EAAE,MAAM,EAAE,KAAK,EAAE,WAAW,EAAE,MAAM,EAAE,WAAW;IAMnE,IAAI,KAAK,IAAI,MAAM,CAElB;IACD,IAAI,MAAM,IAAI,MAAM,CAEnB;CACJ"}
|
package/dist/dat.js
CHANGED
|
@@ -1,23 +1,23 @@
|
|
|
1
1
|
import { DatBytes, DatArrayBuffer, DatInteger, DatUnixtime, } from "./index.js";
|
|
2
2
|
export class Dat {
|
|
3
|
-
format = false;
|
|
4
3
|
dat = '';
|
|
5
|
-
|
|
6
|
-
|
|
7
|
-
|
|
8
|
-
|
|
9
|
-
|
|
4
|
+
_format = false;
|
|
5
|
+
_expire = 0;
|
|
6
|
+
_certificateId = 0;
|
|
7
|
+
_plain = new ArrayBuffer(0);
|
|
8
|
+
_secure = new ArrayBuffer(0);
|
|
9
|
+
_signature = new ArrayBuffer(0);
|
|
10
10
|
constructor(dat) {
|
|
11
11
|
if (dat) {
|
|
12
12
|
const parts = (this.dat = dat || '').split('.');
|
|
13
13
|
if (dat && parts.length == 5) {
|
|
14
14
|
try {
|
|
15
|
-
this.
|
|
16
|
-
this.
|
|
17
|
-
this.
|
|
18
|
-
this.
|
|
19
|
-
this.
|
|
20
|
-
this.
|
|
15
|
+
this._expire = DatInteger.parse(parts[0]);
|
|
16
|
+
this._certificateId = Number.parseInt(parts[1], 16);
|
|
17
|
+
this._plain = DatArrayBuffer.fromBase64Url(parts[2]);
|
|
18
|
+
this._secure = DatArrayBuffer.fromBase64Url(parts[3]);
|
|
19
|
+
this._signature = DatArrayBuffer.fromBase64Url(parts[4]);
|
|
20
|
+
this._format = (this._signature.byteLength > 0 && this._expire >= 0);
|
|
21
21
|
}
|
|
22
22
|
catch (e) { }
|
|
23
23
|
}
|
|
@@ -30,9 +30,9 @@ export class Dat {
|
|
|
30
30
|
return new Dat(dat);
|
|
31
31
|
}
|
|
32
32
|
expired() {
|
|
33
|
-
return !this.
|
|
33
|
+
return !this._format || DatUnixtime.isPast(this._expire);
|
|
34
34
|
}
|
|
35
|
-
|
|
35
|
+
_body() {
|
|
36
36
|
return this.dat.substring(0, this.dat.lastIndexOf('.'));
|
|
37
37
|
}
|
|
38
38
|
}
|
package/dist/dat.js.map
CHANGED
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"dat.js","sourceRoot":"","sources":["../src/dat.ts"],"names":[],"mappings":"AAAA,OAAO,EACH,QAAQ,EACR,c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) || function (thisArg, _arguments, P, generator) {
|
|
2
|
+
function adopt(value) { return value instanceof P ? value : new P(function (resolve) { resolve(value); }); }
|
|
3
|
+
return new (P || (P = Promise))(function (resolve, reject) {
|
|
4
|
+
function fulfilled(value) { try { step(generator.next(value)); } catch (e) { reject(e); } }
|
|
5
|
+
function rejected(value) { try { step(generator["throw"](value)); } catch (e) { reject(e); } }
|
|
6
|
+
function step(result) { result.done ? resolve(result.value) : adopt(result.value).then(fulfilled, rejected); }
|
|
7
|
+
step((generator = generator.apply(thisArg, _arguments || [])).next());
|
|
8
|
+
});
|
|
9
|
+
};
|
|
1
10
|
import { Dat, DAT_VERSION, DatArrayBuffer, DatBytes, DatCryptoKey, DatInteger, DatPayload, DatSignatureKey, DatUnixtime, } from "./index.js";
|
|
2
11
|
export class DatKey {
|
|
3
|
-
kid;
|
|
4
|
-
signatureKey;
|
|
5
|
-
cryptoKey;
|
|
6
|
-
issueBegin;
|
|
7
|
-
issueEnd;
|
|
8
|
-
tokenTtl;
|
|
9
12
|
constructor(kid, signatureKey, cryptoKey, issueBegin, issueEnd, tokenTtl) {
|
|
10
13
|
if (kid.match(/[.\r\n]/) != null) {
|
|
11
14
|
throw new Error(`Invalid Kid: kid is not allowed ".", "\\r", "\\n"" ${kid}`);
|
|
@@ -26,66 +29,76 @@ export class DatKey {
|
|
|
26
29
|
this.issueEnd = Math.floor(issueEnd);
|
|
27
30
|
this.tokenTtl = Math.floor(tokenTtl);
|
|
28
31
|
}
|
|
29
|
-
|
|
30
|
-
|
|
31
|
-
|
|
32
|
-
|
|
33
|
-
|
|
34
|
-
|
|
35
|
-
|
|
36
|
-
|
|
37
|
-
|
|
38
|
-
|
|
32
|
+
exports(datSignatureKeyOutOption) {
|
|
33
|
+
return __awaiter(this, void 0, void 0, function* () {
|
|
34
|
+
const kid = this.kid;
|
|
35
|
+
const signAlg = this.signatureKey.algorithm;
|
|
36
|
+
const signKey = yield this.signatureKey.exports(datSignatureKeyOutOption);
|
|
37
|
+
const cryptoAlg = this.cryptoKey.algorithm;
|
|
38
|
+
const cryptoKey = yield this.cryptoKey.exports();
|
|
39
|
+
const issueBegin = this.issueBegin;
|
|
40
|
+
const issueEnd = this.issueEnd;
|
|
41
|
+
const tokenTtl = this.tokenTtl;
|
|
42
|
+
return `${DAT_VERSION}.${kid}.${signAlg}.${signKey}.${cryptoAlg}.${cryptoKey}.${issueBegin}.${issueEnd}.${tokenTtl}`;
|
|
43
|
+
});
|
|
39
44
|
}
|
|
40
|
-
static
|
|
41
|
-
|
|
42
|
-
|
|
43
|
-
|
|
44
|
-
|
|
45
|
-
|
|
46
|
-
|
|
47
|
-
|
|
48
|
-
|
|
49
|
-
|
|
45
|
+
static imports(format) {
|
|
46
|
+
return __awaiter(this, void 0, void 0, function* () {
|
|
47
|
+
const split = format.split(".");
|
|
48
|
+
switch (split[0] || '0') {
|
|
49
|
+
case '2':
|
|
50
|
+
case '1':
|
|
51
|
+
if (split.length == 9) {
|
|
52
|
+
return yield DatKey._import_ver_2(split);
|
|
53
|
+
}
|
|
54
|
+
}
|
|
55
|
+
throw new Error("Invalid format");
|
|
56
|
+
});
|
|
50
57
|
}
|
|
51
|
-
static
|
|
52
|
-
|
|
53
|
-
|
|
54
|
-
|
|
55
|
-
|
|
56
|
-
|
|
57
|
-
|
|
58
|
-
|
|
58
|
+
static _import_ver_2(split) {
|
|
59
|
+
return __awaiter(this, void 0, void 0, function* () {
|
|
60
|
+
const kid = split[1];
|
|
61
|
+
const signKey = yield DatSignatureKey.imports(split[2], split[3]);
|
|
62
|
+
const cryptoKey = yield DatCryptoKey.imports(split[4], split[5]);
|
|
63
|
+
const issueBegin = Number(split[6]);
|
|
64
|
+
const issueEnd = Number(split[7]);
|
|
65
|
+
const tokenTtl = Number(split[8]);
|
|
66
|
+
return new DatKey(kid, signKey, cryptoKey, issueBegin, issueEnd, tokenTtl);
|
|
67
|
+
});
|
|
59
68
|
}
|
|
60
|
-
|
|
61
|
-
|
|
62
|
-
|
|
63
|
-
|
|
64
|
-
|
|
65
|
-
|
|
66
|
-
|
|
67
|
-
|
|
68
|
-
|
|
69
|
+
toDat(plain, secure) {
|
|
70
|
+
return __awaiter(this, void 0, void 0, function* () {
|
|
71
|
+
const now = DatUnixtime.now();
|
|
72
|
+
const exp = now + this.tokenTtl;
|
|
73
|
+
const kid = this.kid;
|
|
74
|
+
const plainBase64 = DatBytes.toBase64Url(plain);
|
|
75
|
+
const securedBase64 = DatArrayBuffer.toBase64Url(yield this.cryptoKey.encrypt(DatArrayBuffer.from(secure)));
|
|
76
|
+
const body = `${exp}.${kid}.${plainBase64}.${securedBase64}`;
|
|
77
|
+
const sign = DatArrayBuffer.toBase64Url(yield this.signatureKey.sign(body));
|
|
78
|
+
return `${body}.${sign}`;
|
|
79
|
+
});
|
|
69
80
|
}
|
|
70
|
-
|
|
71
|
-
|
|
72
|
-
|
|
73
|
-
|
|
74
|
-
|
|
75
|
-
|
|
76
|
-
|
|
77
|
-
|
|
78
|
-
|
|
79
|
-
|
|
80
|
-
|
|
81
|
-
|
|
81
|
+
toPayload(dat) {
|
|
82
|
+
return __awaiter(this, void 0, void 0, function* () {
|
|
83
|
+
dat = Dat.from(dat);
|
|
84
|
+
if (!dat.format) {
|
|
85
|
+
throw new Error("Invalid DAT: Format");
|
|
86
|
+
}
|
|
87
|
+
if (dat.expired()) {
|
|
88
|
+
throw new Error("Invalid DAT: Expired");
|
|
89
|
+
}
|
|
90
|
+
if (!(yield this.signatureKey.verify(dat.body(), dat.signature))) {
|
|
91
|
+
throw new Error('Invalid DAT: Signature');
|
|
92
|
+
}
|
|
93
|
+
return new DatPayload(dat.expire, dat.plain, yield this.cryptoKey.decrypt(dat.secure));
|
|
94
|
+
});
|
|
82
95
|
}
|
|
83
96
|
isPreIssuance() {
|
|
84
97
|
return DatUnixtime.now() <= this.issueBegin;
|
|
85
98
|
}
|
|
86
99
|
isIssuable() {
|
|
87
100
|
const now = DatUnixtime.now();
|
|
88
|
-
return now > this.issueBegin && now <= this.issueEnd;
|
|
101
|
+
return this.hasSigningKey() && now > this.issueBegin && now <= this.issueEnd;
|
|
89
102
|
}
|
|
90
103
|
expiredVerifying() {
|
|
91
104
|
return DatUnixtime.isPast(this.issueEnd + this.tokenTtl);
|