saro-dat 3.3.0 → 4.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/dist/crypto.d.ts +4 -4
- package/dist/crypto.d.ts.map +1 -1
- package/dist/crypto.js +6 -6
- package/dist/crypto.js.map +1 -1
- package/dist/dat.certificate.d.ts +10 -9
- package/dist/dat.certificate.d.ts.map +1 -1
- package/dist/dat.certificate.js +40 -36
- package/dist/dat.certificate.js.map +1 -1
- package/dist/dat.d.ts +7 -7
- package/dist/dat.d.ts.map +1 -1
- package/dist/dat.js +15 -15
- package/dist/dat.js.map +1 -1
- package/dist/dat.manager.d.ts +2 -2
- package/dist/dat.manager.d.ts.map +1 -1
- package/dist/dat.manager.js +15 -15
- package/dist/dat.manager.js.map +1 -1
- package/dist/signature.d.ts +15 -11
- package/dist/signature.d.ts.map +1 -1
- package/dist/signature.js +87 -101
- package/dist/signature.js.map +1 -1
- package/package.json +2 -3
package/dist/crypto.d.ts
CHANGED
|
@@ -1,16 +1,16 @@
|
|
|
1
|
-
export type DatCryptoAlgorithm = "
|
|
1
|
+
export type DatCryptoAlgorithm = "IV-AES128-GCM" | "IV-AES256-GCM";
|
|
2
2
|
export declare const DatCryptoAlgorithms: DatCryptoAlgorithm[];
|
|
3
3
|
type CryptoConfig = {
|
|
4
4
|
name: string;
|
|
5
5
|
length: number;
|
|
6
6
|
};
|
|
7
|
-
export declare class
|
|
7
|
+
export declare class DatCrypto {
|
|
8
8
|
private readonly config;
|
|
9
9
|
readonly algorithm: DatCryptoAlgorithm;
|
|
10
10
|
readonly key: CryptoKey;
|
|
11
11
|
constructor(algorithm: DatCryptoAlgorithm, key: CryptoKey, config?: CryptoConfig);
|
|
12
|
-
static generate(algorithm: DatCryptoAlgorithm): Promise<
|
|
13
|
-
static imports(algorithm: string, base64: string): Promise<
|
|
12
|
+
static generate(algorithm: DatCryptoAlgorithm): Promise<DatCrypto>;
|
|
13
|
+
static imports(algorithm: string, base64: string): Promise<DatCrypto>;
|
|
14
14
|
exports(): Promise<string>;
|
|
15
15
|
encrypt(data: ArrayBufferLike | Uint8Array | string | null | undefined): Promise<ArrayBuffer>;
|
|
16
16
|
decrypt(data: ArrayBufferLike | Uint8Array | string | null | undefined): Promise<ArrayBuffer>;
|
package/dist/crypto.d.ts.map
CHANGED
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"crypto.d.ts","sourceRoot":"","sources":["../src/crypto.ts"],"names":[],"mappings":"AAEA,MAAM,MAAM,kBAAkB,GAAG,
|
|
1
|
+
{"version":3,"file":"crypto.d.ts","sourceRoot":"","sources":["../src/crypto.ts"],"names":[],"mappings":"AAEA,MAAM,MAAM,kBAAkB,GAAG,eAAe,GAAG,eAAe,CAAC;AACnE,eAAO,MAAM,mBAAmB,EAAE,kBAAkB,EAAuC,CAAC;AAE5F,KAAK,YAAY,GAAG;IAAE,IAAI,EAAE,MAAM,CAAC;IAAC,MAAM,EAAE,MAAM,CAAA;CAAE,CAAC;AAerD,qBAAa,SAAS;IAClB,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,SAAS,CAAC;WAQ3D,OAAO,CAAC,SAAS,EAAE,MAAM,EAAE,MAAM,EAAE,MAAM,GAAG,OAAO,CAAC,SAAS,CAAC;IASrE,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
|
@@ -1,8 +1,8 @@
|
|
|
1
1
|
import { DatArrayBuffer, } from "./index.js";
|
|
2
|
-
export const DatCryptoAlgorithms = ["
|
|
2
|
+
export const DatCryptoAlgorithms = ["IV-AES128-GCM", "IV-AES256-GCM"];
|
|
3
3
|
const CRYPTO_CONFIG = {
|
|
4
|
-
"
|
|
5
|
-
"
|
|
4
|
+
"IV-AES128-GCM": { name: "AES-GCM", length: 128 },
|
|
5
|
+
"IV-AES256-GCM": { name: "AES-GCM", length: 256 },
|
|
6
6
|
};
|
|
7
7
|
function getCryptoConfig(algorithm) {
|
|
8
8
|
const config = CRYPTO_CONFIG[algorithm];
|
|
@@ -11,7 +11,7 @@ function getCryptoConfig(algorithm) {
|
|
|
11
11
|
}
|
|
12
12
|
throw new Error(`Unsupported DAT Crypto Algorithm: ${algorithm}`);
|
|
13
13
|
}
|
|
14
|
-
export class
|
|
14
|
+
export class DatCrypto {
|
|
15
15
|
config;
|
|
16
16
|
algorithm;
|
|
17
17
|
key;
|
|
@@ -23,13 +23,13 @@ export class DatCryptoKey {
|
|
|
23
23
|
static async generate(algorithm) {
|
|
24
24
|
const config = getCryptoConfig(algorithm);
|
|
25
25
|
const key = await crypto.subtle.generateKey({ name: config.name, length: config.length }, true, ["encrypt", "decrypt"]);
|
|
26
|
-
return new
|
|
26
|
+
return new DatCrypto(algorithm, key, config);
|
|
27
27
|
}
|
|
28
28
|
static async imports(algorithm, base64) {
|
|
29
29
|
const config = getCryptoConfig(algorithm);
|
|
30
30
|
const bytes = DatArrayBuffer.fromBase64Url(base64);
|
|
31
31
|
const key = await crypto.subtle.importKey("raw", bytes, { name: config.name }, true, ["encrypt", "decrypt"]);
|
|
32
|
-
return new
|
|
32
|
+
return new DatCrypto(algorithm, key, config);
|
|
33
33
|
}
|
|
34
34
|
async exports() {
|
|
35
35
|
return DatArrayBuffer.toBase64Url(await crypto.subtle.exportKey("raw", this.key));
|
package/dist/crypto.js.map
CHANGED
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"crypto.js","sourceRoot":"","sources":["../src/crypto.ts"],"names":[],"mappings":"AAAA,OAAO,EAAC,cAAc,GAAE,MAAM,YAAY,CAAC;AAG3C,MAAM,CAAC,MAAM,mBAAmB,GAAyB,CAAC,
|
|
1
|
+
{"version":3,"file":"crypto.js","sourceRoot":"","sources":["../src/crypto.ts"],"names":[],"mappings":"AAAA,OAAO,EAAC,cAAc,GAAE,MAAM,YAAY,CAAC;AAG3C,MAAM,CAAC,MAAM,mBAAmB,GAAyB,CAAC,eAAe,EAAE,eAAe,CAAC,CAAC;AAI5F,MAAM,aAAa,GAAiC;IAChD,eAAe,EAAE,EAAE,IAAI,EAAE,SAAS,EAAE,MAAM,EAAE,GAAG,EAAE;IACjD,eAAe,EAAE,EAAE,IAAI,EAAE,SAAS,EAAE,MAAM,EAAE,GAAG,EAAE;CACpD,CAAC;AAEF,SAAS,eAAe,CAAC,SAAiB;IACtC,MAAM,MAAM,GAAG,aAAa,CAAC,SAAS,CAAC,CAAC;IACxC,IAAI,MAAM,EAAE,CAAC;QACT,OAAO,MAAM,CAAC;IAClB,CAAC;IACD,MAAM,IAAI,KAAK,CAAC,qCAAqC,SAAS,EAAE,CAAC,CAAC;AACtE,CAAC;AAED,MAAM,OAAO,SAAS;IACD,MAAM,CAAe;IACtB,SAAS,CAAqB;IAC9B,GAAG,CAAY;IAE/B,YACI,SAA6B,EAC7B,GAAc,EACd,SAAuB,eAAe,CAAC,SAAS,CAAC;QAEjD,IAAI,CAAC,SAAS,GAAG,SAAS,CAAC;QAC3B,IAAI,CAAC,GAAG,GAAG,GAAG,CAAC;QACf,IAAI,CAAC,MAAM,GAAG,MAAM,CAAC;IACzB,CAAC;IAED,MAAM,CAAC,KAAK,CAAC,QAAQ,CAAC,SAA6B;QAC/C,MAAM,MAAM,GAAG,eAAe,CAAC,SAAS,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DatCrypto, DatSignature } from "./index.js";
|
|
2
2
|
export declare class DatCertificate {
|
|
3
3
|
readonly cid: bigint;
|
|
4
|
-
readonly
|
|
5
|
-
readonly
|
|
6
|
-
readonly
|
|
7
|
-
readonly
|
|
8
|
-
readonly
|
|
9
|
-
constructor(cid: bigint | number, signatureKey:
|
|
10
|
-
exports(
|
|
4
|
+
readonly signatureKey: DatSignature;
|
|
5
|
+
readonly cryptoKey: DatCrypto;
|
|
6
|
+
readonly datIssueBegin: bigint;
|
|
7
|
+
readonly datIssueEnd: bigint;
|
|
8
|
+
readonly datTtl: bigint;
|
|
9
|
+
constructor(cid: bigint | number | string, signatureKey: DatSignature, cryptoKey: DatCrypto, datIssueBegin: number | bigint | string, datIssueEnd: number | bigint | string, datTtl: number | bigint | string);
|
|
10
|
+
exports(verifyOnly?: boolean): Promise<string>;
|
|
11
11
|
static imports(format: string): Promise<DatCertificate>;
|
|
12
12
|
issuable(): boolean;
|
|
13
13
|
expired(): boolean;
|
|
14
|
-
|
|
14
|
+
signable(): boolean;
|
|
15
|
+
pair(): boolean;
|
|
15
16
|
}
|
|
16
17
|
//# sourceMappingURL=dat.certificate.d.ts.map
|
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"dat.certificate.d.ts","sourceRoot":"","sources":["../src/dat.certificate.ts"],"names":[],"mappings":"AAAA,OAAO,EAAC,
|
|
1
|
+
{"version":3,"file":"dat.certificate.d.ts","sourceRoot":"","sources":["../src/dat.certificate.ts"],"names":[],"mappings":"AAAA,OAAO,EAAC,SAAS,EAAc,YAAY,EAAE,MAAM,YAAY,CAAC;AAGhE,qBAAa,cAAc;IACvB,SAAgB,GAAG,EAAE,MAAM,CAAC;IAC5B,SAAgB,YAAY,EAAE,YAAY,CAAC;IAC3C,SAAgB,SAAS,EAAE,SAAS,CAAC;IACrC,SAAgB,aAAa,EAAE,MAAM,CAAC;IACtC,SAAgB,WAAW,EAAE,MAAM,CAAC;IACpC,SAAgB,MAAM,EAAE,MAAM,CAAC;gBAG3B,GAAG,EAAE,MAAM,GAAC,MAAM,GAAC,MAAM,EACzB,YAAY,EAAE,YAAY,EAC1B,SAAS,EAAE,SAAS,EACpB,aAAa,EAAE,MAAM,GAAC,MAAM,GAAC,MAAM,EACnC,WAAW,EAAE,MAAM,GAAC,MAAM,GAAC,MAAM,EACjC,MAAM,EAAE,MAAM,GAAC,MAAM,GAAC,MAAM;IAU1B,OAAO,CAAC,UAAU,GAAE,OAAe,GAAG,OAAO,CAAC,MAAM,CAAC;WAY9C,OAAO,CAAC,MAAM,EAAE,MAAM,GAAG,OAAO,CAAC,cAAc,CAAC;IAiB7D,QAAQ,IAAI,OAAO;IAInB,OAAO,IAAI,OAAO;IAIlB,QAAQ,IAAI,OAAO;IAInB,IAAI,IAAI,OAAO;CAGlB"}
|
package/dist/dat.certificate.js
CHANGED
|
@@ -1,53 +1,57 @@
|
|
|
1
|
-
import {
|
|
1
|
+
import { DatCrypto, DatInteger, DatSignature, } from "./index.js";
|
|
2
2
|
import { Unixtime } from "infinite-unixtime";
|
|
3
3
|
export class DatCertificate {
|
|
4
4
|
cid;
|
|
5
|
-
|
|
6
|
-
|
|
7
|
-
|
|
8
|
-
|
|
9
|
-
|
|
5
|
+
signatureKey;
|
|
6
|
+
cryptoKey;
|
|
7
|
+
datIssueBegin;
|
|
8
|
+
datIssueEnd;
|
|
9
|
+
datTtl;
|
|
10
10
|
constructor(cid, signatureKey, cryptoKey, datIssueBegin, datIssueEnd, datTtl) {
|
|
11
|
-
cid = DatInteger.toCid(cid, `Invalid cid(Certificate ID) is HEX ${cid}`);
|
|
12
|
-
this.
|
|
13
|
-
this.
|
|
14
|
-
this.
|
|
15
|
-
this.
|
|
16
|
-
this.
|
|
17
|
-
this._datTtl = DatInteger.toBigInt(datTtl, `Invalid: datTtl is positive int or 0 ${datTtl}`, 0n);
|
|
11
|
+
this.cid = DatInteger.toCid(cid, `Invalid cid(Certificate ID) is HEX ${cid}`);
|
|
12
|
+
this.signatureKey = signatureKey;
|
|
13
|
+
this.cryptoKey = cryptoKey;
|
|
14
|
+
this.datIssueBegin = DatInteger.toBigInt(datIssueBegin, `Invalid: datIssueBegin is positive int or 0 ${datIssueBegin}`, 0n);
|
|
15
|
+
this.datIssueEnd = DatInteger.toBigInt(datIssueEnd, `Invalid: datIssueEnd is positive int or 0 ${datIssueEnd}`, 0n);
|
|
16
|
+
this.datTtl = DatInteger.toBigInt(datTtl, `Invalid: datTtl is positive int or 0 ${datTtl}`, 0n);
|
|
18
17
|
}
|
|
19
|
-
async exports(
|
|
18
|
+
async exports(verifyOnly = false) {
|
|
20
19
|
const cid = this.cid.toString(16);
|
|
21
|
-
const
|
|
22
|
-
const
|
|
23
|
-
const
|
|
24
|
-
const
|
|
25
|
-
const
|
|
26
|
-
const
|
|
27
|
-
const
|
|
28
|
-
return `${cid}.${
|
|
20
|
+
const issuedAt = this.datIssueBegin.toString();
|
|
21
|
+
const issuanceDuration = (this.datIssueEnd - this.datIssueBegin).toString();
|
|
22
|
+
const ttl = this.datTtl.toString();
|
|
23
|
+
const sigAlgo = this.signatureKey.algorithm;
|
|
24
|
+
const cryptoAlgo = this.cryptoKey.algorithm;
|
|
25
|
+
const sigKey = await this.signatureKey.exports(verifyOnly);
|
|
26
|
+
const cryptoKey = await this.cryptoKey.exports();
|
|
27
|
+
return `${cid}.${issuedAt}.${issuanceDuration}.${ttl}.${sigAlgo}.${cryptoAlgo}.${sigKey}.${cryptoKey}`;
|
|
29
28
|
}
|
|
30
29
|
static async imports(format) {
|
|
31
|
-
const
|
|
32
|
-
if (
|
|
33
|
-
|
|
34
|
-
const signatureKey = await DatSignatureKey.imports(split[1], split[2]);
|
|
35
|
-
const cryptoKey = await DatCryptoKey.imports(split[3], split[4]);
|
|
36
|
-
const datIssueBegin = Number(split[5]);
|
|
37
|
-
const datIssueEnd = Number(split[6]);
|
|
38
|
-
const datTtl = Number(split[7]);
|
|
39
|
-
return new DatCertificate(cid, signatureKey, cryptoKey, datIssueBegin, datIssueEnd, datTtl);
|
|
30
|
+
const parts = format.split(".");
|
|
31
|
+
if (parts.length !== 8) {
|
|
32
|
+
throw new Error("Invalid Certificate format");
|
|
40
33
|
}
|
|
41
|
-
|
|
34
|
+
const cid = DatInteger.toCid(parts[0], `Invalid cid(Certificate ID) is HEX ${parts[0]}`);
|
|
35
|
+
const issuedAt = DatInteger.toBigInt(parts[1]);
|
|
36
|
+
const issuanceDuration = DatInteger.toBigInt(parts[2]);
|
|
37
|
+
const ttl = DatInteger.toBigInt(parts[3]);
|
|
38
|
+
const sigAlgo = parts[4];
|
|
39
|
+
const cryptoAlgo = parts[5];
|
|
40
|
+
const signatureKey = await DatSignature.imports(sigAlgo, parts[6]);
|
|
41
|
+
const cryptoKey = await DatCrypto.imports(cryptoAlgo, parts[7]);
|
|
42
|
+
return new DatCertificate(cid, signatureKey, cryptoKey, issuedAt, issuedAt + issuanceDuration, ttl);
|
|
42
43
|
}
|
|
43
44
|
issuable() {
|
|
44
|
-
return this.
|
|
45
|
+
return this.signable() && Unixtime.now().between(this.datIssueBegin, this.datIssueEnd, true);
|
|
45
46
|
}
|
|
46
47
|
expired() {
|
|
47
|
-
return Unixtime.now().after(this.
|
|
48
|
+
return Unixtime.now().after(this.datIssueEnd + this.datTtl, true);
|
|
48
49
|
}
|
|
49
|
-
|
|
50
|
-
return this.
|
|
50
|
+
signable() {
|
|
51
|
+
return this.signatureKey.signable();
|
|
52
|
+
}
|
|
53
|
+
pair() {
|
|
54
|
+
return this.signatureKey.pair();
|
|
51
55
|
}
|
|
52
56
|
}
|
|
53
57
|
//# sourceMappingURL=dat.certificate.js.map
|
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"dat.certificate.js","sourceRoot":"","sources":["../src/dat.certificate.ts"],"names":[],"mappings":"AAAA,OAAO,EAAC,
|
|
1
|
+
{"version":3,"file":"dat.certificate.js","sourceRoot":"","sources":["../src/dat.certificate.ts"],"names":[],"mappings":"AAAA,OAAO,EAAC,SAAS,EAAE,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;QACnE,MAAM,SAAS,GAAG,MAAM,SAAS,CAAC,OAAO,CAAC,UAAU,EAAE,KAAK,CAAC,CAAC,CAAC,CAAC,CAAC;QAEhE,OAAO,IAAI,cAAc,CAAC,GAAG,EAAE,YAAY,EAAE,SAAS,EAAE,QAAQ,EAAE,QAAQ,GAAG,gBAAgB,EAAE,GAAG,CAAC,CAAC;IACxG,CAAC;IAED,QAAQ;QACJ,OAAO,IAAI,CAAC,QAAQ,EAAE,IAAI,QAAQ,CAAC,GAAG,EAAE,CAAC,OAAO,CAAC,IAAI,CAAC,aAAa,EAAE,IAAI,CAAC,WAAW,EAAE,IAAI,CAAC,CAAC;IACjG,CAAC;IAED,OAAO;QACH,OAAO,QAAQ,CAAC,GAAG,EAAE,CAAC,KAAK,CAAC,IAAI,CAAC,WAAW,GAAG,IAAI,CAAC,MAAM,EAAE,IAAI,CAAC,CAAC;IACtE,CAAC;IAED,QAAQ;QACJ,OAAO,IAAI,CAAC,YAAY,CAAC,QAAQ,EAAE,CAAC;IACxC,CAAC;IAED,IAAI;QACA,OAAO,IAAI,CAAC,YAAY,CAAC,IAAI,EAAE,CAAC;IACpC,CAAC;CACJ"}
|
package/dist/dat.d.ts
CHANGED
|
@@ -1,15 +1,15 @@
|
|
|
1
1
|
export declare class Dat {
|
|
2
2
|
readonly dat: string;
|
|
3
|
-
readonly
|
|
4
|
-
readonly
|
|
5
|
-
readonly
|
|
6
|
-
readonly
|
|
7
|
-
readonly
|
|
8
|
-
readonly
|
|
3
|
+
readonly format: boolean;
|
|
4
|
+
readonly expire: number;
|
|
5
|
+
readonly cid: bigint;
|
|
6
|
+
readonly plainBytes: ArrayBuffer;
|
|
7
|
+
readonly secureBytes: 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 plainBytes: ArrayBuffer;
|
package/dist/dat.d.ts.map
CHANGED
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"dat.d.ts","sourceRoot":"","sources":["../src/dat.ts"],"names":[],"mappings":"AAGA,qBAAa,GAAG;IACZ,
|
|
1
|
+
{"version":3,"file":"dat.d.ts","sourceRoot":"","sources":["../src/dat.ts"],"names":[],"mappings":"AAGA,qBAAa,GAAG;IACZ,SAAgB,GAAG,EAAE,MAAM,CAAM;IACjC,SAAgB,MAAM,EAAE,OAAO,CAAS;IACxC,SAAgB,MAAM,EAAE,MAAM,CAAK;IACnC,SAAgB,GAAG,EAAE,MAAM,CAAM;IACjC,SAAgB,UAAU,EAAE,WAAW,CAAsB;IAC7D,SAAgB,WAAW,EAAE,WAAW,CAAsB;IAC9D,SAAgB,SAAS,EAAE,WAAW,CAAsB;gBAEhD,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,IAAI,IAAI,MAAM;CAGjB;AAED,qBAAa,UAAU;IACnB,SAAgB,UAAU,EAAE,WAAW,CAAC;IACxC,SAAgB,WAAW,EAAE,WAAW,CAAC;gBAE7B,KAAK,EAAE,WAAW,EAAE,MAAM,EAAE,WAAW;IAKnD,IAAI,KAAK,IAAI,MAAM,CAElB;IACD,IAAI,MAAM,IAAI,MAAM,CAEnB;IAED,QAAQ,IAAI,MAAM;IAIlB,cAAc,IAAI,MAAM;CAG3B"}
|
package/dist/dat.js
CHANGED
|
@@ -2,23 +2,23 @@ import { DatArrayBuffer, DatBytes, DatInteger, } from "./index.js";
|
|
|
2
2
|
import { Unixtime } from "infinite-unixtime";
|
|
3
3
|
export class Dat {
|
|
4
4
|
dat = '';
|
|
5
|
-
|
|
6
|
-
|
|
7
|
-
|
|
8
|
-
|
|
9
|
-
|
|
10
|
-
|
|
5
|
+
format = false;
|
|
6
|
+
expire = 0;
|
|
7
|
+
cid = 0n;
|
|
8
|
+
plainBytes = new ArrayBuffer(0);
|
|
9
|
+
secureBytes = new ArrayBuffer(0);
|
|
10
|
+
signature = new ArrayBuffer(0);
|
|
11
11
|
constructor(dat) {
|
|
12
12
|
if (dat) {
|
|
13
13
|
const parts = (this.dat = dat || '').split('.');
|
|
14
14
|
if (dat && parts.length == 5) {
|
|
15
15
|
try {
|
|
16
|
-
this.
|
|
17
|
-
this.
|
|
18
|
-
this.
|
|
19
|
-
this.
|
|
20
|
-
this.
|
|
21
|
-
this.
|
|
16
|
+
this.expire = DatInteger.parse(parts[0]);
|
|
17
|
+
this.cid = DatInteger.toCid(parts[1]);
|
|
18
|
+
this.plainBytes = DatArrayBuffer.fromBase64Url(parts[2]);
|
|
19
|
+
this.secureBytes = DatArrayBuffer.fromBase64Url(parts[3]);
|
|
20
|
+
this.signature = DatArrayBuffer.fromBase64Url(parts[4]);
|
|
21
|
+
this.format = (this.signature.byteLength > 0 && this.expire >= 0);
|
|
22
22
|
}
|
|
23
23
|
catch (e) { }
|
|
24
24
|
}
|
|
@@ -31,9 +31,9 @@ export class Dat {
|
|
|
31
31
|
return new Dat(dat);
|
|
32
32
|
}
|
|
33
33
|
expired() {
|
|
34
|
-
return !this.
|
|
34
|
+
return !this.format || Unixtime.now().after(this.expire, true);
|
|
35
35
|
}
|
|
36
|
-
|
|
36
|
+
body() {
|
|
37
37
|
return this.dat.substring(0, this.dat.lastIndexOf('.'));
|
|
38
38
|
}
|
|
39
39
|
}
|
|
@@ -51,7 +51,7 @@ export class DatPayload {
|
|
|
51
51
|
return DatBytes.toUtf8(this.secureBytes);
|
|
52
52
|
}
|
|
53
53
|
toString() {
|
|
54
|
-
return `${
|
|
54
|
+
return `${DatArrayBuffer.toBase64Url(this.plainBytes)} ${DatArrayBuffer.toBase64Url(this.secureBytes)}`;
|
|
55
55
|
}
|
|
56
56
|
toUnsafeString() {
|
|
57
57
|
return `${this.plain} ${this.secure}`;
|
package/dist/dat.js.map
CHANGED
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"dat.js","sourceRoot":"","sources":["../src/dat.ts"],"names":[],"mappings":"AAAA,OAAO,EAAC,cAAc,EAAE,QAAQ,EAAE,UAAU,GAAE,MAAM,YAAY,CAAC;AACjE,OAAO,EAAC,QAAQ,EAAC,MAAM,mBAAmB,CAAC;AAE3C,MAAM,OAAO,GAAG;
|
|
1
|
+
{"version":3,"file":"dat.js","sourceRoot":"","sources":["../src/dat.ts"],"names":[],"mappings":"AAAA,OAAO,EAAC,cAAc,EAAE,QAAQ,EAAE,UAAU,GAAE,MAAM,YAAY,CAAC;AACjE,OAAO,EAAC,QAAQ,EAAC,MAAM,mBAAmB,CAAC;AAE3C,MAAM,OAAO,GAAG;IACI,GAAG,GAAW,EAAE,CAAC;IACjB,MAAM,GAAY,KAAK,CAAC;IACxB,MAAM,GAAW,CAAC,CAAC;IACnB,GAAG,GAAW,EAAE,CAAC;IACjB,UAAU,GAAgB,IAAI,WAAW,CAAC,CAAC,CAAC,CAAC;IAC7C,WAAW,GAAgB,IAAI,WAAW,CAAC,CAAC,CAAC,CAAC;IAC9C,SAAS,GAAgB,IAAI,WAAW,CAAC,CAAC,CAAC,CAAC;IAE5D,YAAY,GAA0B;QAClC,IAAI,GAAG,EAAE,CAAC;YACN,MAAM,KAAK,GAAG,CAAC,IAAI,CAAC,GAAG,GAAG,GAAG,IAAI,EAAE,CAAC,CAAC,KAAK,CAAC,GAAG,CAAC,CAAC;YAChD,IAAI,GAAG,IAAI,KAAK,CAAC,MAAM,IAAI,CAAC,EAAE,CAAC;gBAC3B,IAAI,CAAC;oBACD,IAAI,CAAC,MAAM,GAAG,UAAU,CAAC,KAAK,CAAC,KAAK,CAAC,CAAC,CAAC,CAAC,CAAC;oBACzC,IAAI,CAAC,GAAG,GAAG,UAAU,C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O,QAAQ,CAAC,MAAM,CAAC,IAAI,CAAC,UAAU,CAAC,CAAC;IAC5C,CAAC;IACD,IAAI,MAAM;QACN,OAAO,QAAQ,CAAC,MAAM,CAAC,IAAI,CAAC,WAAW,CAAC,CAAC;IAC7C,CAAC;IAED,QAAQ;QACJ,OAAO,GAAG,cAAc,CAAC,WAAW,CAAC,IAAI,CAAC,UAAU,CAAC,IAAI,cAAc,CAAC,WAAW,CAAC,IAAI,CAAC,WAAW,CAAC,EAAE,CAAC;IAC5G,CAAC;IAED,cAAc;QACV,OAAO,GAAG,IAAI,CAAC,KAAK,IAAI,IAAI,CAAC,MAAM,EAAE,CAAC;IAC1C,CAAC;CACJ"}
|
package/dist/dat.manager.d.ts
CHANGED
|
@@ -1,11 +1,11 @@
|
|
|
1
|
-
import { Dat, DatCertificate, DatPayload
|
|
1
|
+
import { Dat, DatCertificate, DatPayload } from "./index.js";
|
|
2
2
|
export declare class DatManager {
|
|
3
3
|
private readonly issuer;
|
|
4
4
|
private readonly certificates;
|
|
5
5
|
constructor(issuer?: DatCertificate | null, certificates?: DatCertificate[]);
|
|
6
6
|
static from(inputCertificates: DatCertificate[]): DatManager;
|
|
7
7
|
static imports(format: string): Promise<DatManager>;
|
|
8
|
-
exports(
|
|
8
|
+
exports(verifyOnly?: boolean): Promise<string>;
|
|
9
9
|
find(cid: bigint): DatCertificate | null;
|
|
10
10
|
issue(plain: ArrayBufferLike | Uint8Array | string | null | undefined, secure: ArrayBufferLike | Uint8Array | string | null | undefined): Promise<string>;
|
|
11
11
|
parse(dat: Dat | string | undefined | null): Promise<DatPayload>;
|
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"dat.manager.d.ts","sourceRoot":"","sources":["../src/dat.manager.ts"],"names":[],"mappings":"AAAA,OAAO,
|
|
1
|
+
{"version":3,"file":"dat.manager.d.ts","sourceRoot":"","sources":["../src/dat.manager.ts"],"names":[],"mappings":"AAAA,OAAO,EAAC,GAAG,EAA4B,cAAc,EAAE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sort((a, b) => {
|
|
22
|
-
if (a.
|
|
22
|
+
if (a.datIssueEnd == b.datIssueEnd) {
|
|
23
23
|
return 0;
|
|
24
24
|
}
|
|
25
25
|
else {
|
|
26
|
-
return a.
|
|
26
|
+
return a.datIssueEnd < b.datIssueEnd ? -1 : 1;
|
|
27
27
|
}
|
|
28
28
|
});
|
|
29
29
|
const issuer = certificates.findLast(e => e.issuable()) || null;
|
|
30
30
|
return new DatManager(issuer, certificates);
|
|
31
31
|
}
|
|
32
32
|
static async imports(format) {
|
|
33
|
-
const
|
|
34
|
-
|
|
35
|
-
.filter(e => !!e));
|
|
33
|
+
const lines = format.split('\n').map(e => e.trim()).filter(e => e !== '');
|
|
34
|
+
const certificates = await Promise.all(lines.map(async (e) => await DatCertificate.imports(e)));
|
|
36
35
|
return DatManager.from(certificates);
|
|
37
36
|
}
|
|
38
|
-
async exports(
|
|
39
|
-
return (await Promise.all(this.certificates.map(e => e.exports(
|
|
37
|
+
async exports(verifyOnly = false) {
|
|
38
|
+
return (await Promise.all(this.certificates.map(e => e.exports(verifyOnly)))).join('\n');
|
|
40
39
|
}
|
|
41
40
|
find(cid) {
|
|
42
41
|
return this.certificates.find(e => e.cid === cid) || null;
|
|
@@ -48,10 +47,11 @@ export class DatManager {
|
|
|
48
47
|
throw new Error("Invalid DAT: Signing Key Does Not Exist");
|
|
49
48
|
}
|
|
50
49
|
async parse(dat) {
|
|
51
|
-
|
|
50
|
+
dat = Dat.from(dat);
|
|
51
|
+
if (!dat.format) {
|
|
52
52
|
throw new Error("Invalid DAT: Format");
|
|
53
53
|
}
|
|
54
|
-
const certificate = this.find(dat.
|
|
54
|
+
const certificate = this.find(dat.cid);
|
|
55
55
|
if (certificate != null) {
|
|
56
56
|
return DatManager.parse(certificate, dat);
|
|
57
57
|
}
|
|
@@ -59,26 +59,26 @@ export class DatManager {
|
|
|
59
59
|
}
|
|
60
60
|
static async issue(certificate, plain, secure) {
|
|
61
61
|
const now = Unixtime.now().time;
|
|
62
|
-
const expire = now + certificate.
|
|
62
|
+
const expire = now + certificate.datTtl;
|
|
63
63
|
const cid = certificate.cid.toString(16);
|
|
64
64
|
const plainBase64 = DatBytes.toBase64Url(plain);
|
|
65
|
-
const securedBase64 = DatArrayBuffer.toBase64Url(await certificate.
|
|
65
|
+
const securedBase64 = DatArrayBuffer.toBase64Url(await certificate.cryptoKey.encrypt(DatArrayBuffer.from(secure)));
|
|
66
66
|
const body = `${expire}.${cid}.${plainBase64}.${securedBase64}`;
|
|
67
|
-
const signature = DatArrayBuffer.toBase64Url(await certificate.
|
|
67
|
+
const signature = DatArrayBuffer.toBase64Url(await certificate.signatureKey.sign(body));
|
|
68
68
|
return `${body}.${signature}`;
|
|
69
69
|
}
|
|
70
70
|
static async parse(certificate, dat) {
|
|
71
71
|
dat = Dat.from(dat);
|
|
72
|
-
if (!dat.
|
|
72
|
+
if (!dat.format) {
|
|
73
73
|
throw new Error("Invalid DAT: Format");
|
|
74
74
|
}
|
|
75
75
|
if (dat.expired()) {
|
|
76
76
|
throw new Error("Invalid DAT: Expired");
|
|
77
77
|
}
|
|
78
|
-
if (!await certificate.
|
|
78
|
+
if (!await certificate.signatureKey.verify(dat.body(), dat.signature)) {
|
|
79
79
|
throw new Error('Invalid DAT: Signature');
|
|
80
80
|
}
|
|
81
|
-
return new DatPayload(dat.
|
|
81
|
+
return new DatPayload(dat.plainBytes, await certificate.cryptoKey.decrypt(dat.secureBytes));
|
|
82
82
|
}
|
|
83
83
|
}
|
|
84
84
|
//# sourceMappingURL=dat.manager.js.map
|
package/dist/dat.manager.js.map
CHANGED
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"dat.manager.js","sourceRoot":"","sources":["../src/dat.manager.ts"],"names":[],"mappings":"AAAA,OAAO,
|
|
1
|
+
{"version":3,"file":"dat.manager.js","sourceRoot":"","sources":["../src/dat.manager.ts"],"names":[],"mappings":"AAAA,OAAO,EAAC,GAAG,EAAE,cAAc,EAAE,QAAQ,EAAE,cAAc,E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KAAK,CAAC,KAAK,CAAC,WAA2B,EAAE,GAA8B;QAC1E,GAAG,GAAG,GAAG,CAAC,IAAI,CAAC,GAAG,CAAC,CAAC;QACpB,IAAI,CAAC,GAAG,CAAC,MAAM,EAAE,CAAC;YACd,MAAM,IAAI,KAAK,CAAC,qBAAqB,CAAC,CAAC;QAC3C,CAAC;QACD,IAAI,GAAG,CAAC,OAAO,EAAE,EAAE,CAAC;YAChB,MAAM,IAAI,KAAK,CAAC,sBAAsB,CAAC,CAAC;QAC5C,CAAC;QACD,IAAI,CAAC,MAAM,WAAW,CAAC,YAAY,CAAC,MAAM,CAAC,GAAG,CAAC,IAAI,EAAE,EAAE,GAAG,CAAC,SAAS,CAAC,EAAE,CAAC;YACpE,MAAM,IAAI,KAAK,CAAC,wBAAwB,CAAC,CAAC;QAC9C,CAAC;QACD,OAAO,IAAI,UAAU,CAAC,GAAG,CAAC,UAAU,EAAE,MAAM,WAAW,CAAC,SAAS,CAAC,OAAO,CAAC,GAAG,CAAC,WAAW,CAAC,CAAC,CAAA;IAC/F,CAAC;CACJ"}
|
package/dist/signature.d.ts
CHANGED
|
@@ -1,24 +1,28 @@
|
|
|
1
|
-
export type DatSignatureAlgorithm = "P256" | "P384" | "P521";
|
|
1
|
+
export type DatSignatureAlgorithm = "HMAC-SHA256-MFS" | "HMAC-SHA384-MFS" | "HMAC-SHA512-MFS" | "ECDSA-P256" | "ECDSA-P384" | "ECDSA-P521";
|
|
2
2
|
export declare const DatSignatureAlgorithms: DatSignatureAlgorithm[];
|
|
3
|
-
export type DatSignatureKeyOutOption = "FULL" | "SIGNING" | "VERIFYING";
|
|
4
|
-
export declare const DatSignatureKeyOutOptions: DatSignatureKeyOutOption[];
|
|
5
3
|
type SignatureConfig = {
|
|
6
4
|
name: string;
|
|
7
|
-
|
|
8
|
-
|
|
5
|
+
hash: {
|
|
6
|
+
name: string;
|
|
7
|
+
};
|
|
8
|
+
namedCurve?: string;
|
|
9
|
+
privateLen?: number;
|
|
10
|
+
publicLen?: number;
|
|
11
|
+
hmacLen?: number;
|
|
9
12
|
};
|
|
10
|
-
export declare class
|
|
13
|
+
export declare class DatSignature {
|
|
11
14
|
private readonly config;
|
|
12
15
|
readonly algorithm: DatSignatureAlgorithm;
|
|
13
16
|
private readonly signingKey;
|
|
14
17
|
private readonly verifyingKey;
|
|
15
|
-
constructor(algorithm: DatSignatureAlgorithm,
|
|
16
|
-
static generate(algorithm: DatSignatureAlgorithm): Promise<
|
|
17
|
-
static imports(algorithm: string,
|
|
18
|
-
exports(
|
|
18
|
+
constructor(algorithm: DatSignatureAlgorithm, signingKey: CryptoKey | null, verifyingKey: CryptoKey, config?: SignatureConfig);
|
|
19
|
+
static generate(algorithm: DatSignatureAlgorithm): Promise<DatSignature>;
|
|
20
|
+
static imports(algorithm: string, base64: string): Promise<DatSignature>;
|
|
21
|
+
exports(verifyOnly?: boolean): Promise<string>;
|
|
19
22
|
sign(body: ArrayBufferLike | Uint8Array | string | null | undefined): Promise<ArrayBuffer>;
|
|
20
23
|
verify(body: ArrayBufferLike | Uint8Array | string | null | undefined, signature: ArrayBufferLike | Uint8Array | string | null | undefined): Promise<boolean>;
|
|
21
|
-
|
|
24
|
+
signable(): boolean;
|
|
25
|
+
pair(): boolean;
|
|
22
26
|
}
|
|
23
27
|
export {};
|
|
24
28
|
//# sourceMappingURL=signature.d.ts.map
|
package/dist/signature.d.ts.map
CHANGED
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"signature.d.ts","sourceRoot":"","sources":["../src/signature.ts"],"names":[],"mappings":"
|
|
1
|
+
{"version":3,"file":"signature.d.ts","sourceRoot":"","sources":["../src/signature.ts"],"names":[],"mappings":"AAEA,MAAM,MAAM,qBAAqB,GAC7B,iBAAiB,GAAG,iBAAiB,GAAG,iBAAiB,GACzD,YAAY,GAAG,YAAY,GAAG,YAAY,CAAC;AAE/C,eAAO,MAAM,sBAAsB,EAAE,qBAAqB,EAGzD,CAAC;AAEF,KAAK,eAAe,GAAG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8Array, } from "./index.js";
|
|
2
|
-
|
|
3
|
-
|
|
4
|
-
|
|
2
|
+
export const DatSignatureAlgorithms = [
|
|
3
|
+
"HMAC-SHA256-MFS", "HMAC-SHA384-MFS", "HMAC-SHA512-MFS",
|
|
4
|
+
"ECDSA-P256", "ECDSA-P384", "ECDSA-P521"
|
|
5
|
+
];
|
|
5
6
|
const SIGNATURE_CONFIG = {
|
|
6
|
-
"
|
|
7
|
-
"
|
|
8
|
-
"
|
|
7
|
+
"HMAC-SHA256-MFS": { name: "HMAC", hash: { name: "SHA-256" }, hmacLen: 32 },
|
|
8
|
+
"HMAC-SHA384-MFS": { name: "HMAC", hash: { name: "SHA-384" }, hmacLen: 48 },
|
|
9
|
+
"HMAC-SHA512-MFS": { name: "HMAC", hash: { name: "SHA-512" }, hmacLen: 64 },
|
|
10
|
+
"ECDSA-P256": { name: "ECDSA", namedCurve: "P-256", hash: { name: "SHA-256" }, privateLen: 32, publicLen: 65 },
|
|
11
|
+
"ECDSA-P384": { name: "ECDSA", namedCurve: "P-384", hash: { name: "SHA-384" }, privateLen: 48, publicLen: 97 },
|
|
12
|
+
"ECDSA-P521": { name: "ECDSA", namedCurve: "P-521", hash: { name: "SHA-512" }, privateLen: 66, publicLen: 133 },
|
|
9
13
|
};
|
|
10
|
-
function toDatSignatureKeyOutOption(option) {
|
|
11
|
-
switch (option) {
|
|
12
|
-
case "FULL":
|
|
13
|
-
case "SIGNING":
|
|
14
|
-
case "VERIFYING": return option;
|
|
15
|
-
}
|
|
16
|
-
throw new Error(`Unsupported DAT Signature Key Out Option: ${option}`);
|
|
17
|
-
}
|
|
18
14
|
function getCryptoConfig(algorithm) {
|
|
19
15
|
const config = SIGNATURE_CONFIG[algorithm];
|
|
20
16
|
if (config) {
|
|
@@ -22,64 +18,88 @@ function getCryptoConfig(algorithm) {
|
|
|
22
18
|
}
|
|
23
19
|
throw new Error(`Unsupported DAT Signature Algorithm: ${algorithm}`);
|
|
24
20
|
}
|
|
25
|
-
export class
|
|
21
|
+
export class DatSignature {
|
|
26
22
|
config;
|
|
27
23
|
algorithm;
|
|
28
24
|
signingKey;
|
|
29
25
|
verifyingKey;
|
|
30
|
-
constructor(algorithm,
|
|
26
|
+
constructor(algorithm, signingKey, verifyingKey, config = getCryptoConfig(algorithm)) {
|
|
31
27
|
this.algorithm = algorithm;
|
|
32
|
-
this.signingKey =
|
|
33
|
-
this.verifyingKey =
|
|
28
|
+
this.signingKey = signingKey;
|
|
29
|
+
this.verifyingKey = verifyingKey;
|
|
34
30
|
this.config = config;
|
|
35
31
|
}
|
|
36
32
|
static async generate(algorithm) {
|
|
37
33
|
const config = getCryptoConfig(algorithm);
|
|
38
|
-
|
|
39
|
-
|
|
34
|
+
if (config.name === "HMAC") {
|
|
35
|
+
const key = await crypto.subtle.generateKey({ name: "HMAC", hash: config.hash, length: config.hmacLen * 8 }, true, ["sign", "verify"]);
|
|
36
|
+
return new DatSignature(algorithm, key, key, config);
|
|
37
|
+
}
|
|
38
|
+
else {
|
|
39
|
+
const { publicKey, privateKey } = await crypto.subtle.generateKey({ name: "ECDSA", namedCurve: config.namedCurve }, true, ["sign", "verify"]);
|
|
40
|
+
return new DatSignature(algorithm, privateKey, publicKey, config);
|
|
41
|
+
}
|
|
40
42
|
}
|
|
41
|
-
static async imports(algorithm,
|
|
43
|
+
static async imports(algorithm, base64) {
|
|
42
44
|
const config = getCryptoConfig(algorithm);
|
|
43
|
-
|
|
44
|
-
|
|
45
|
-
|
|
46
|
-
|
|
47
|
-
let signingKeyBase64 = null;
|
|
48
|
-
let verifyingKeyBase64 = null;
|
|
49
|
-
if (parts.length == 1) { // signing key only
|
|
50
|
-
if (parts[0]) {
|
|
51
|
-
signingKeyBase64 = parts[0] || null;
|
|
45
|
+
const bytes = DatUint8Array.fromBase64Url(base64);
|
|
46
|
+
if (config.name === "HMAC") {
|
|
47
|
+
if (bytes.length !== config.hmacLen) {
|
|
48
|
+
throw new Error(`Invalid HMAC key length: expected ${config.hmacLen}, got ${bytes.length}`);
|
|
52
49
|
}
|
|
50
|
+
const key = await crypto.subtle.importKey("raw", bytes, { name: "HMAC", hash: config.hash }, true, ["sign", "verify"]);
|
|
51
|
+
return new DatSignature(algorithm, key, key, config);
|
|
53
52
|
}
|
|
54
|
-
else
|
|
55
|
-
|
|
56
|
-
|
|
53
|
+
else {
|
|
54
|
+
const privateLen = config.privateLen;
|
|
55
|
+
const publicLen = config.publicLen;
|
|
56
|
+
let signingKey = null;
|
|
57
|
+
let verifyingKey;
|
|
58
|
+
if (bytes.length === privateLen + publicLen) {
|
|
59
|
+
const privateBytes = bytes.slice(0, privateLen);
|
|
60
|
+
const publicBytes = bytes.slice(privateLen);
|
|
61
|
+
// Import public key
|
|
62
|
+
verifyingKey = await crypto.subtle.importKey("raw", publicBytes, { name: "ECDSA", namedCurve: config.namedCurve }, true, ["verify"]);
|
|
63
|
+
// Import private key using JWK to include X, Y from publicBytes
|
|
64
|
+
// uncompressed point format: 0x04 || X || Y
|
|
65
|
+
const xBytes = publicBytes.slice(1, 1 + (publicLen - 1) / 2);
|
|
66
|
+
const yBytes = publicBytes.slice(1 + (publicLen - 1) / 2);
|
|
67
|
+
const jwk = {
|
|
68
|
+
kty: "EC",
|
|
69
|
+
crv: config.namedCurve,
|
|
70
|
+
d: DatUint8Array.toBase64Url(privateBytes),
|
|
71
|
+
x: DatUint8Array.toBase64Url(xBytes),
|
|
72
|
+
y: DatUint8Array.toBase64Url(yBytes),
|
|
73
|
+
ext: true,
|
|
74
|
+
};
|
|
75
|
+
signingKey = await crypto.subtle.importKey("jwk", jwk, { name: "ECDSA", namedCurve: config.namedCurve }, true, ["sign"]);
|
|
57
76
|
}
|
|
58
|
-
if (
|
|
59
|
-
|
|
77
|
+
else if (bytes.length === publicLen) {
|
|
78
|
+
verifyingKey = await crypto.subtle.importKey("raw", bytes, { name: "ECDSA", namedCurve: config.namedCurve }, true, ["verify"]);
|
|
60
79
|
}
|
|
80
|
+
else {
|
|
81
|
+
throw new Error(`Invalid ECDSA key length`);
|
|
82
|
+
}
|
|
83
|
+
return new DatSignature(algorithm, signingKey, verifyingKey, config);
|
|
61
84
|
}
|
|
62
|
-
// 현재는 EC 계열밖에 없어서 따로 예외처리 하진 않는다.
|
|
63
|
-
// 검증키가 추가되면 이부분도 바뀌어야 한다.
|
|
64
|
-
let keyPair = await toEcdsaKeyPair(config, signingKeyBase64, verifyingKeyBase64);
|
|
65
|
-
return new DatSignatureKey(algorithm, keyPair.privateKey, keyPair.publicKey, config);
|
|
66
85
|
}
|
|
67
|
-
async exports(
|
|
68
|
-
|
|
69
|
-
|
|
70
|
-
|
|
71
|
-
|
|
72
|
-
|
|
73
|
-
|
|
86
|
+
async exports(verifyOnly = false) {
|
|
87
|
+
if (this.config.name === "HMAC") {
|
|
88
|
+
const bytes = await crypto.subtle.exportKey("raw", this.verifyingKey);
|
|
89
|
+
return DatArrayBuffer.toBase64Url(bytes);
|
|
90
|
+
}
|
|
91
|
+
else {
|
|
92
|
+
if (verifyOnly || !this.signingKey) {
|
|
93
|
+
const bytes = await crypto.subtle.exportKey("raw", this.verifyingKey);
|
|
94
|
+
return DatArrayBuffer.toBase64Url(bytes);
|
|
74
95
|
}
|
|
75
96
|
else {
|
|
76
|
-
|
|
97
|
+
// Export private (raw) + public (raw uncompressed)
|
|
98
|
+
const jwk = await crypto.subtle.exportKey("jwk", this.signingKey);
|
|
99
|
+
const key = DatArrayBuffer.concat(DatArrayBuffer.fromBase64Url(jwk.d), await crypto.subtle.exportKey("raw", this.verifyingKey));
|
|
100
|
+
return DatArrayBuffer.toBase64Url(key);
|
|
77
101
|
}
|
|
78
102
|
}
|
|
79
|
-
if (option == "FULL" || option == "VERIFYING") {
|
|
80
|
-
rv += '~' + DatArrayBuffer.toBase64Url(await crypto.subtle.exportKey("raw", this.verifyingKey));
|
|
81
|
-
}
|
|
82
|
-
return rv;
|
|
83
103
|
}
|
|
84
104
|
async sign(body) {
|
|
85
105
|
if (!this.signingKey) {
|
|
@@ -89,65 +109,31 @@ export class DatSignatureKey {
|
|
|
89
109
|
if (!bytes.byteLength) {
|
|
90
110
|
throw new Error(`Sign Error - body is empty`);
|
|
91
111
|
}
|
|
92
|
-
|
|
112
|
+
if (this.config.name === "HMAC") {
|
|
113
|
+
return crypto.subtle.sign({ name: "HMAC" }, this.signingKey, bytes);
|
|
114
|
+
}
|
|
115
|
+
else {
|
|
116
|
+
return crypto.subtle.sign({ name: "ECDSA", hash: this.config.hash }, this.signingKey, bytes);
|
|
117
|
+
}
|
|
93
118
|
}
|
|
94
119
|
async verify(body, signature) {
|
|
95
120
|
const bodyBytes = DatArrayBuffer.from(body);
|
|
96
121
|
if (!bodyBytes.byteLength) {
|
|
97
122
|
return false;
|
|
98
123
|
}
|
|
99
|
-
|
|
124
|
+
const sigBytes = DatArrayBuffer.fromBase64Url(signature);
|
|
125
|
+
if (this.config.name === "HMAC") {
|
|
126
|
+
return crypto.subtle.verify({ name: "HMAC" }, this.verifyingKey, sigBytes, bodyBytes);
|
|
127
|
+
}
|
|
128
|
+
else {
|
|
129
|
+
return crypto.subtle.verify({ name: "ECDSA", hash: this.config.hash }, this.verifyingKey, sigBytes, bodyBytes);
|
|
130
|
+
}
|
|
100
131
|
}
|
|
101
|
-
|
|
132
|
+
signable() {
|
|
102
133
|
return this.signingKey !== null;
|
|
103
134
|
}
|
|
104
|
-
|
|
105
|
-
|
|
106
|
-
let ecdsa;
|
|
107
|
-
switch (config.curve) {
|
|
108
|
-
case "P-256":
|
|
109
|
-
ecdsa = p256;
|
|
110
|
-
break;
|
|
111
|
-
case "P-384":
|
|
112
|
-
ecdsa = p384;
|
|
113
|
-
break;
|
|
114
|
-
case "P-521":
|
|
115
|
-
ecdsa = p521;
|
|
116
|
-
break;
|
|
117
|
-
default:
|
|
118
|
-
throw new Error(`Unsupported DAT Signature Algorithm: ${config.curve}`);
|
|
119
|
-
}
|
|
120
|
-
let privateKey = null;
|
|
121
|
-
let privateKeyBytes = null;
|
|
122
|
-
if (privateKeyBase64) {
|
|
123
|
-
privateKeyBytes = DatUint8Array.fromBase64Url(privateKeyBase64);
|
|
124
|
-
// d로부터 공개 키(Point) 계산
|
|
125
|
-
const publicPoint = ecdsa.getPublicKey(privateKeyBytes, false);
|
|
126
|
-
// 곡선별 좌표 길이 계산: (전체 길이 - 1) / 2 - 좌표 추출
|
|
127
|
-
const coordinateLen = (publicPoint.length - 1) / 2;
|
|
128
|
-
const xBytes = publicPoint.slice(1, 1 + coordinateLen);
|
|
129
|
-
const yBytes = publicPoint.slice(1 + coordinateLen, 1 + 2 * coordinateLen);
|
|
130
|
-
const jwk = {
|
|
131
|
-
kty: "EC",
|
|
132
|
-
crv: config.curve,
|
|
133
|
-
d: privateKeyBase64,
|
|
134
|
-
x: DatUint8Array.toBase64Url(xBytes),
|
|
135
|
-
y: DatUint8Array.toBase64Url(yBytes),
|
|
136
|
-
ext: true,
|
|
137
|
-
};
|
|
138
|
-
privateKey = await crypto.subtle.importKey("jwk", jwk, { name: "ECDSA", namedCurve: config.curve }, true, ["sign"]);
|
|
139
|
-
}
|
|
140
|
-
let publicKeyBytes;
|
|
141
|
-
if (publicKeyBase64) {
|
|
142
|
-
publicKeyBytes = DatArrayBuffer.fromBase64Url(publicKeyBase64);
|
|
143
|
-
}
|
|
144
|
-
else if (privateKeyBytes?.byteLength) {
|
|
145
|
-
publicKeyBytes = DatArrayBuffer.from(ecdsa.getPublicKey(privateKeyBytes, false));
|
|
146
|
-
}
|
|
147
|
-
else {
|
|
148
|
-
throw new Error(`Invalid DAT Signature Key Format`);
|
|
135
|
+
pair() {
|
|
136
|
+
return this.config.name === "ECDSA";
|
|
149
137
|
}
|
|
150
|
-
let publicKey = await crypto.subtle.importKey("raw", publicKeyBytes, { name: config.name, namedCurve: config.curve }, true, ["verify"]);
|
|
151
|
-
return { privateKey, publicKey };
|
|
152
138
|
}
|
|
153
139
|
//# sourceMappingURL=signature.js.map
|
package/dist/signature.js.map
CHANGED
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"signature.js","sourceRoot":"","sources":["../src/signature.ts"],"names":[],"mappings":"AAAA,OAAO,EAAC,cAAc,EAAE,aAAa,GAAE,MAAM,YAAY,CAAC;
|
|
1
|
+
{"version":3,"file":"signature.js","sourceRoot":"","sources":["../src/signature.ts"],"names":[],"mappings":"AAAA,OAAO,EAAC,cAAc,EAAE,aAAa,GAAE,MAAM,YAAY,CAAC;AAM1D,MAAM,CAAC,MAAM,sBAAsB,GAA4B;IAC3D,iBAAiB,EAAE,iBAAiB,EAAE,iBAAiB;IACvD,YAAY,EAAE,YAAY,EAAE,YAAY;CAC3C,CAAC;AAWF,MAAM,gBAAgB,GAAoC;IACtD,iBAAiB,EAAE,EAAE,IAAI,EAAE,MAAM,EAAE,IAAI,EAAE,EAAE,IAAI,EAAE,SAAS,EAAE,EAAE,OAAO,EAAE,EAAE,EAAE;IAC3E,iBAAiB,EAAE,EAAE,IAAI,EAAE,MAAM,EAAE,IAAI,EAAE,EAAE,IAAI,EAAE,SAAS,EAAE,EAAE,OAAO,EAAE,EAAE,EAAE;IAC3E,iBAAiB,EAAE,EAAE,IAAI,EAAE,MAAM,EAAE,IAAI,EAAE,EAAE,IAAI,EAAE,SAAS,EAAE,EAAE,OAAO,EAAE,EAAE,EAAE;IAC3E,YAAY,EAAE,EAAE,IAAI,EAAE,OAAO,EAAE,UAAU,EAAE,OAAO,EAAE,IAAI,EAAE,EAAE,IAAI,EAAE,SAAS,EAAE,EAAE,UAAU,EAAE,EAAE,EAAE,SAAS,EAAE,EAAE,EAAE;IAC9G,YAAY,EAAE,EAAE,IAAI,EAAE,OAAO,EAAE,UAAU,EAAE,OAAO,EAAE,IAAI,EAAE,EAAE,IAAI,EAAE,SAAS,EAAE,EAAE,UAAU,EAAE,EAAE,EAAE,SAAS,EAAE,EAAE,EAAE;IAC9G,YAAY,EAAE,EAAE,IAAI,EAAE,OAAO,EAAE,UAAU,EAAE,OAAO,EAAE,IAAI,EAAE,EAAE,IAAI,EAAE,SAAS,EAAE,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EAChB,IAAI,CAAC,YAAY,EACjB,QAAQ,EAAE,SAAS,CACtB,CAAC;QACN,CAAC;aAAM,CAAC;YACJ,OAAO,MAAM,CAAC,MAAM,CAAC,MAAM,CACvB,EAAE,IAAI,EAAE,OAAO,EAAE,IAAI,EAAE,IAAI,CAAC,MAAM,CAAC,IAAI,EAAE,EACzC,IAAI,CAAC,YAAY,EACjB,QAAQ,EAAE,SAAS,CACtB,CAAC;QACN,CAAC;IACL,CAAC;IAED,QAAQ;QACJ,OAAO,IAAI,CAAC,UAAU,KAAK,IAAI,CAAC;IACpC,CAAC;IAED,IAAI;QACA,OAAO,IAAI,CAAC,MAAM,CAAC,IAAI,KAAK,OAAO,CAAC;IACxC,CAAC;CACJ"}
|
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "saro-dat",
|
|
3
|
-
"version": "
|
|
3
|
+
"version": "4.0.0",
|
|
4
4
|
"description": "Distributed Access Token",
|
|
5
5
|
"keywords": [
|
|
6
6
|
"dat",
|
|
@@ -41,8 +41,7 @@
|
|
|
41
41
|
"prepublishOnly": "npm run build"
|
|
42
42
|
},
|
|
43
43
|
"dependencies": {
|
|
44
|
-
"
|
|
45
|
-
"infinite-unixtime": "^1.1.3"
|
|
44
|
+
"infinite-unixtime": "^1.1.4"
|
|
46
45
|
},
|
|
47
46
|
"devDependencies": {
|
|
48
47
|
"@types/node": "^25.7.0",
|