saro-dat 0.0.0 → 0.0.2

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 CHANGED
@@ -1,24 +1,86 @@
1
+ # DAT - Distributed Access Token
2
+
3
+ # NPM
4
+ ```
5
+ saro-dat
6
+ ```
7
+
1
8
  # DAT
2
- - Distributed Access Token
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
+ ```
3
19
 
4
- ## Package
5
- - https://www.npmjs.com/package/saro-dat
20
+ > ```expire```.```kid```.```plain```.```secure```.```sign```
6
21
 
22
+ - ```expire```: number
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)
32
+
33
+ # DAT KEY
7
34
  ```
8
- "saro-dat": "not-yet"
35
+ # Example
36
+ 1.2.P256.DErFl-U5h4fdbnAXTTs2GikkJgZwYXV25v2EdFeXIXs.AES128GCMN.5VEziIzCu2LRsK1XS6OYxA.1776541326.1776544626.1800
9
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
63
+
64
+
65
+ ## support signature algorithm
66
+ | name | algorithm |
67
+ |--------|------------|
68
+ | P256 | secp256r1 |
69
+ | P384 | secp384r1 |
70
+ | P521 | secp521r1 |
71
+
72
+ ## support crypto algorithm
73
+ | name | algorithm |
74
+ |------------|-----------------------------|
75
+ | AES128GCMN | aes-128-gcm n(nonce + body) |
76
+ | AES256GCMN | aes-256-cbc n(nonce + body) |
77
+
10
78
 
11
- ## Document
12
- - Class
13
- - Date Format
14
- - Locate
15
- - SingleLock
16
- - [Scroll Loader](./document/scroll-loader.md)
17
- - Instance
18
- - cookies
19
- - ko
79
+ ### See Also
80
+ - Libraries
81
+ - [Rust](https://github.com/saro-lab/dat-rust)
82
+ - [Java, Kotlin](https://github.com/saro-lab/dat-maven)
83
+ - DatKey Generate Service
84
+ - [DAT Bank: Binary Docker, Kubernetes](https://github.com/saro-lab/dat-bank)
20
85
 
21
86
 
22
- ## Project Info
23
- - typescript
24
- - yarn 3.x
@@ -0,0 +1,13 @@
1
+ export type DatCryptoAlgorithm = "AES128GCMN" | "AES256GCMN";
2
+ export declare class DatCryptoKey {
3
+ private readonly config;
4
+ readonly algorithm: DatCryptoAlgorithm;
5
+ readonly key: CryptoKey;
6
+ constructor(algorithm: DatCryptoAlgorithm, key: CryptoKey);
7
+ static generate(algorithm: DatCryptoAlgorithm): Promise<DatCryptoKey>;
8
+ static imports(algorithmString: string, base64: string): Promise<DatCryptoKey>;
9
+ exports(): Promise<string>;
10
+ encrypt(data: ArrayBuffer): Promise<ArrayBuffer>;
11
+ decrypt(data: ArrayBuffer): Promise<ArrayBuffer>;
12
+ }
13
+ //# sourceMappingURL=crypto.d.ts.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"crypto.d.ts","sourceRoot":"","sources":["../src/crypto.ts"],"names":[],"mappings":"AAEA,MAAM,MAAM,kBAAkB,GAAG,YAAY,GAAG,YAAY,CAAC;AAW7D,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;WAOL,QAAQ,CAAC,SAAS,EAAE,kBAAkB,GAAG,OAAO,CAAC,YAAY,CAAC;WAU9D,OAAO,CAAC,eAAe,EAAE,MAAM,EAAE,MAAM,EAAE,MAAM,GAAG,OAAO,CAAC,YAAY,CAAC;IAe9E,OAAO,IAAI,OAAO,CAAC,MAAM,CAAC;IAI1B,OAAO,CAAC,IAAI,EAAE,WAAW,GAAG,OAAO,CAAC,WAAW,CAAC;IAehD,OAAO,CAAC,IAAI,EAAE,WAAW,GAAG,OAAO,CAAC,WAAW,CAAC;CAezD"}
package/dist/crypto.js ADDED
@@ -0,0 +1,64 @@
1
+ import { concatArrayBufferLike, decodeBase64, encodeBase64Url } from "./index.js";
2
+ const algorithms = ["AES128GCMN", "AES256GCMN"];
3
+ const CRYPTO_CONFIG = {
4
+ AES128GCMN: { name: "AES-GCM", length: 128 },
5
+ AES256GCMN: { name: "AES-GCM", length: 256 },
6
+ };
7
+ export class DatCryptoKey {
8
+ config;
9
+ algorithm;
10
+ key;
11
+ constructor(algorithm, key) {
12
+ this.algorithm = algorithm;
13
+ this.key = key;
14
+ this.config = CRYPTO_CONFIG[algorithm];
15
+ }
16
+ static async generate(algorithm) {
17
+ const config = CRYPTO_CONFIG[algorithm];
18
+ if (!config)
19
+ throw new Error(`Unsupported algorithm: ${algorithm}`);
20
+ const key = await crypto.subtle.generateKey({ name: config.name, length: config.length }, true, ["encrypt", "decrypt"]);
21
+ return new DatCryptoKey(algorithm, key);
22
+ }
23
+ static async imports(algorithmString, base64) {
24
+ if (!algorithms.includes(algorithmString)) {
25
+ throw new Error(`Unsupported algorithm: ${algorithmString}`);
26
+ }
27
+ const algorithm = algorithmString;
28
+ const config = CRYPTO_CONFIG[algorithm];
29
+ if (!config)
30
+ throw new Error(`Unsupported algorithm: ${algorithm}`);
31
+ const bytes = new Uint8Array(decodeBase64(base64));
32
+ const key = await crypto.subtle.importKey("raw", bytes, { name: config.name }, true, ["encrypt", "decrypt"]);
33
+ return new DatCryptoKey(algorithm, key);
34
+ }
35
+ async exports() {
36
+ return encodeBase64Url(await crypto.subtle.exportKey("raw", this.key));
37
+ }
38
+ async encrypt(data) {
39
+ if (!data) {
40
+ return new ArrayBuffer(0);
41
+ }
42
+ if (this.config.name == "AES-GCM") {
43
+ const nonce = new Uint8Array(12);
44
+ crypto.getRandomValues(nonce);
45
+ const encrypt = await crypto.subtle.encrypt({ name: this.config.name, iv: nonce }, this.key, data);
46
+ return concatArrayBufferLike(nonce.buffer, encrypt);
47
+ }
48
+ throw new Error("Unsupported algorithm");
49
+ }
50
+ async decrypt(data) {
51
+ if (!data) {
52
+ return new ArrayBuffer(0);
53
+ }
54
+ if (this.config.name == "AES-GCM") {
55
+ if (data.byteLength <= 12) {
56
+ throw new Error("Invalid data length");
57
+ }
58
+ const nonce = data.slice(0, 12);
59
+ return await crypto.subtle.decrypt({ name: this.config.name, iv: nonce }, this.key, data.slice(12));
60
+ }
61
+ throw new Error("Unsupported algorithm");
62
+ }
63
+ }
64
+ //# sourceMappingURL=crypto.js.map
@@ -0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AE,EAAE,IAAI,CAAC,GAAG,EAAE,IAAI,CACxD,CAAC;YACF,OAAO,qBAAqB,CAAC,KAAK,CAAC,MAAM,EAAE,OAAO,CAAC,CAAC;QACxD,CAAC;QACD,MAAM,IAAI,KAAK,CAAC,uBAAuB,CAAC,CAAC;IAC7C,CAAC;IAED,KAAK,CAAC,OAAO,CAAC,IAAiB;QAC3B,IAAI,CAAC,IAAI,EAAE,CAAC;YACR,OAAO,IAAI,WAAW,CAAC,CAAC,CAAC,CAAC;QAC9B,CAAC;QACD,IAAI,IAAI,CAAC,MAAM,CAAC,IAAI,IAAI,SAAS,EAAE,CAAC;YAChC,IAAI,IAAI,CAAC,UAAU,IAAI,EAAE,EAAE,CAAC;gBACxB,MAAM,IAAI,KAAK,CAAC,qBAAqB,CAAC,CAAC;YAC3C,CAAC;YACD,MAAM,KAAK,GAAG,IAAI,CAAC,KAAK,CAAC,CAAC,EAAE,EAAE,CAAC,CAAC;YAChC,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,IAAI,CAAC,KAAK,CAAC,EAAE,CAAC,CAClE,CAAC;QACN,CAAC;QACD,MAAM,IAAI,KAAK,CAAC,uBAAuB,CAAC,CAAC;IAC7C,CAAC;CACJ"}
@@ -0,0 +1,13 @@
1
+ import { DatKey, DatPayload, DatPayloadBuffer, DatSignatureKeyOutOption } from "./index.js";
2
+ export declare class DatBank {
3
+ private readonly issueKey;
4
+ private readonly verifyKeys;
5
+ constructor(issueKey?: DatKey | null, verifyKeys?: DatKey[]);
6
+ static imports(format: string): Promise<DatBank>;
7
+ exports(datSignatureKeyOutOption: DatSignatureKeyOutOption): Promise<string>;
8
+ find(kid: string): DatKey | null;
9
+ toDat(plain: string | ArrayBuffer, secure: string | ArrayBuffer): Promise<string>;
10
+ toPayloadBuffer(dat: string): Promise<DatPayloadBuffer>;
11
+ toPayload(dat: string): Promise<DatPayload>;
12
+ }
13
+ //# sourceMappingURL=dat.bank.d.ts.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"dat.bank.d.ts","sourceRoot":"","sources":["../src/dat.bank.ts"],"names":[],"mappings":"AAAA,OAAO,EACH,MAAM,EACN,UAAU,EACV,gBAAgB,EAChB,wBAAwB,EAI3B,MAAM,YAAY,CAAC;AAEpB,qBAAa,OAAO;IAChB,OAAO,CAAC,QAAQ,CAAC,QAAQ,CAAgB;IACzC,OAAO,CAAC,QAAQ,CAAC,UAAU,CAAW;gBAC1B,QAAQ,GAAE,MAAM,GAAG,IAAW,EAAE,UAAU,GAAE,MAAM,EAAO;WAKxD,OAAO,CAAC,MAAM,EAAE,MAAM,GAAG,OAAO,CAAC,OAAO,CAAC;IAoBhD,OAAO,CAAC,wBAAwB,EAAE,wBAAwB,GAAG,OAAO,CAAC,MAAM,CAAC;IAQlF,IAAI,CAAC,GAAG,EAAE,MAAM,GAAG,MAAM,GAAG,IAAI;IAI1B,KAAK,CAAC,KAAK,EAAE,MAAM,GAAC,WAAW,EAAE,MAAM,EAAE,MAAM,GAAC,WAAW,GAAG,OAAO,CAAC,MAAM,CAAC;IAO7E,eAAe,CAAC,GAAG,EAAE,MAAM,GAAG,OAAO,CAAC,gBAAgB,CAAC;IAYvD,SAAS,CAAC,GAAG,EAAE,MAAM,GAAG,OAAO,CAAC,UAAU,CAAC;CAGpD"}
@@ -0,0 +1,54 @@
1
+ import { DatKey, serializePayload, splitDat, toArrayBuffer } from "./index.js";
2
+ export class DatBank {
3
+ issueKey;
4
+ verifyKeys;
5
+ constructor(issueKey = null, verifyKeys = []) {
6
+ this.issueKey = issueKey;
7
+ this.verifyKeys = verifyKeys;
8
+ }
9
+ static async imports(format) {
10
+ const now = Math.floor(new Date().getTime() / 1000);
11
+ const lines = format.split('\n').map(e => e.trim()).filter(e => !!e);
12
+ let keys = [];
13
+ for (const line of lines) {
14
+ keys.push(await DatKey.import(line));
15
+ }
16
+ if (keys.length != new Set(keys.map(e => e.kid)).size) {
17
+ throw new Error('duplicate kid');
18
+ }
19
+ keys.sort((a, b) => a.issueBegin - b.issueBegin);
20
+ const issueKey = keys.findLast(e => e.hasSigningKey() && e.issueBegin <= now && e.issueEnd > now) || null;
21
+ return new DatBank(issueKey, keys);
22
+ }
23
+ async exports(datSignatureKeyOutOption) {
24
+ let lines = [];
25
+ for (const key of this.verifyKeys) {
26
+ lines.push(await key.export(datSignatureKeyOutOption));
27
+ }
28
+ return lines.join('\n');
29
+ }
30
+ find(kid) {
31
+ return this.verifyKeys.find(e => e.kid === kid) || null;
32
+ }
33
+ async toDat(plain, secure) {
34
+ if (this.issueKey) {
35
+ return await this.issueKey.toDat(toArrayBuffer(plain), toArrayBuffer(secure));
36
+ }
37
+ throw new Error("issue key does not exist");
38
+ }
39
+ async toPayloadBuffer(dat) {
40
+ const parts = splitDat(dat);
41
+ if (parts.error) {
42
+ return Promise.reject("invalid dat format");
43
+ }
44
+ const key = this.find(parts.kid);
45
+ if (key != null) {
46
+ return key.toPayloadBuffer(parts);
47
+ }
48
+ return Promise.reject("invalid dat");
49
+ }
50
+ async toPayload(dat) {
51
+ return this.toPayloadBuffer(dat).then(serializePayload);
52
+ }
53
+ }
54
+ //# sourceMappingURL=dat.bank.js.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"dat.bank.js","sourceRoot":"","sources":["../src/dat.bank.ts"],"names":[],"mappings":"AAAA,OAAO,EACH,MAAM,EAIN,gBAAgB,EAChB,QAAQ,EACR,aAAa,EAChB,MAAM,YAAY,CAAC;AAEpB,MAAM,OAAO,OAAO;IACC,QAAQ,CAAgB;IACxB,UAAU,CAAW;IACtC,YAAY,WAA0B,IAAI,EAAE,aAAuB,EAAE;QACjE,IAAI,CAAC,QAAQ,GAAG,QAAQ,CAAC;QACzB,IAAI,CAAC,UAAU,GAAG,UAAU,CAAC;IACjC,CAAC;IAED,MAAM,CAAC,KAA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,CAAC;CACJ"}
@@ -0,0 +1,18 @@
1
+ import { DatCryptoKey, type DatParts, type DatPayload, type DatPayloadBuffer, DatSignatureKey, type DatSignatureKeyOutOption } from "./index.js";
2
+ export declare class DatKey {
3
+ readonly kid: string;
4
+ readonly signatureKey: DatSignatureKey;
5
+ readonly cryptoKey: DatCryptoKey;
6
+ readonly issueBegin: number;
7
+ readonly issueEnd: number;
8
+ readonly tokenTtl: number;
9
+ constructor(kid: string, signatureKey: DatSignatureKey, cryptoKey: DatCryptoKey, issueBegin: number, issueEnd: number, tokenTtl: number);
10
+ export(datSignatureKeyOutOption: DatSignatureKeyOutOption): Promise<string>;
11
+ static import(format: string): Promise<DatKey>;
12
+ private static _import_ver_2;
13
+ toDat(plainIn: ArrayBuffer, secureIn: ArrayBuffer): Promise<string>;
14
+ toPayload(parts: DatParts | string): Promise<DatPayload>;
15
+ toPayloadBuffer(parts: DatParts | string): Promise<DatPayloadBuffer>;
16
+ hasSigningKey(): boolean;
17
+ }
18
+ //# sourceMappingURL=dat.key.d.ts.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"dat.key.d.ts","sourceRoot":"","sources":["../src/dat.key.ts"],"names":[],"mappings":"AAAA,OAAO,EAEH,YAAY,EACZ,KAAK,QAAQ,EACb,KAAK,UAAU,EACf,KAAK,gBAAgB,EACrB,eAAe,EACf,KAAK,wBAAwB,EAMhC,MAAM,YAAY,CAAC;AAGpB,qBAAa,MAAM;IACf,SAAgB,GAAG,EAAE,MAAM,CAAC;IAC5B,SAAgB,YAAY,EAAE,eAAe,CAAC;IAC9C,SAAgB,SAAS,EAAE,YAAY,CAAC;IACxC,SAAgB,UAAU,EAAE,MAAM,CAAC;IACnC,SAAgB,QAAQ,EAAE,MAAM,CAAC;IACjC,SAAgB,QAAQ,EAAE,MAAM,CAAC;gBAG7B,GAAG,EAAE,MAAM,EACX,YAAY,EAAE,eAAe,EAC7B,SAAS,EAAE,YAAY,EACvB,UAAU,EAAE,MAAM,EAClB,QAAQ,EAAE,MAAM,EAChB,QAAQ,EAAE,MAAM;IAgBd,MAAM,CAAC,wBAAwB,EAAE,wBAAwB,GAAG,OAAO,CAAC,MAAM,CAAC;WAYpE,MAAM,CAAC,MAAM,EAAE,MAAM,GAAG,OAAO,CAAC,MAAM,CAAC;mBAW/B,aAAa;IAU5B,KAAK,CAAC,OAAO,EAAE,WAAW,EAAE,QAAQ,EAAE,WAAW,GAAG,OAAO,CAAC,MAAM,CAAC;IAWnE,SAAS,CAAC,KAAK,EAAE,QAAQ,GAAC,MAAM,GAAG,OAAO,CAAC,UAAU,CAAC;IAItD,eAAe,CAAC,KAAK,EAAE,QAAQ,GAAC,MAAM,GAAG,OAAO,CAAC,gBAAgB,CAAC;IAwBxE,aAAa,IAAI,OAAO;CAG3B"}
@@ -0,0 +1,93 @@
1
+ import { DAT_VERSION, DatCryptoKey, DatSignatureKey, decodeBase64, encodeBase64Url, serializePayload, splitDat, toArrayBuffer } from "./index.js";
2
+ export class DatKey {
3
+ kid;
4
+ signatureKey;
5
+ cryptoKey;
6
+ issueBegin;
7
+ issueEnd;
8
+ tokenTtl;
9
+ constructor(kid, signatureKey, cryptoKey, issueBegin, issueEnd, tokenTtl) {
10
+ if (kid.match(/[.\r\n]/) != null) {
11
+ throw new Error(`Invalid kid: ${kid}`);
12
+ }
13
+ if (isNaN(issueBegin) || isNaN(issueEnd) || isNaN(tokenTtl)) {
14
+ throw new Error(`Invalid issueBegin, issueEnd, tokenTtl: ${issueBegin}, ${issueEnd}, ${tokenTtl}`);
15
+ }
16
+ this.kid = kid;
17
+ this.signatureKey = signatureKey;
18
+ this.cryptoKey = cryptoKey;
19
+ this.issueBegin = Math.floor(issueBegin);
20
+ this.issueEnd = Math.floor(issueEnd);
21
+ this.tokenTtl = Math.floor(tokenTtl);
22
+ }
23
+ async export(datSignatureKeyOutOption) {
24
+ const kid = this.kid;
25
+ const signAlg = this.signatureKey.algorithm;
26
+ const signKey = await this.signatureKey.exports(datSignatureKeyOutOption);
27
+ const cryptoAlg = this.cryptoKey.algorithm;
28
+ const cryptoKey = await this.cryptoKey.exports();
29
+ const issueBegin = this.issueBegin;
30
+ const issueEnd = this.issueEnd;
31
+ const tokenTtl = this.tokenTtl;
32
+ return `${DAT_VERSION}.${kid}.${signAlg}.${signKey}.${cryptoAlg}.${cryptoKey}.${issueBegin}.${issueEnd}.${tokenTtl}`;
33
+ }
34
+ static async import(format) {
35
+ const split = format.split(".");
36
+ switch (split[0] || '0') {
37
+ case '2':
38
+ case '1':
39
+ if (split.length == 9) {
40
+ return await DatKey._import_ver_2(split);
41
+ }
42
+ }
43
+ throw new Error("Invalid format");
44
+ }
45
+ static async _import_ver_2(split) {
46
+ const kid = split[1];
47
+ const signKey = await DatSignatureKey.imports(split[2], split[3]);
48
+ const cryptoKey = await DatCryptoKey.imports(split[4], split[5]);
49
+ const issueBegin = Number(split[6]);
50
+ const issueEnd = Number(split[7]);
51
+ const tokenTtl = Number(split[8]);
52
+ return new DatKey(kid, signKey, cryptoKey, issueBegin, issueEnd, tokenTtl);
53
+ }
54
+ async toDat(plainIn, secureIn) {
55
+ const now = Math.floor(new Date().getTime() / 1000);
56
+ const exp = now + this.tokenTtl;
57
+ const kid = this.kid;
58
+ const plain = encodeBase64Url(plainIn || new ArrayBuffer(0));
59
+ const secure = encodeBase64Url(await this.cryptoKey.encrypt(secureIn || new ArrayBuffer(0)));
60
+ const body = `${exp}.${kid}.${plain}.${secure}`;
61
+ const sign = encodeBase64Url(await this.signatureKey.sign(toArrayBuffer(body)));
62
+ return `${body}.${sign}`;
63
+ }
64
+ async toPayload(parts) {
65
+ return this.toPayloadBuffer(parts).then(serializePayload);
66
+ }
67
+ async toPayloadBuffer(parts) {
68
+ if (typeof parts === 'string') {
69
+ parts = splitDat(parts);
70
+ }
71
+ if (parts.error) {
72
+ return Promise.reject("invalid format");
73
+ }
74
+ const exp = parts.expire;
75
+ if (exp < (new Date().getTime() / 1000)) {
76
+ return Promise.reject('expired');
77
+ }
78
+ if (parts.kid != this.kid) {
79
+ return Promise.reject('invalid kid');
80
+ }
81
+ const body = toArrayBuffer(parts.dat.substring(0, parts.dat.lastIndexOf('.')));
82
+ if (!await this.signatureKey.verify(body, toArrayBuffer(decodeBase64(parts.signature)))) {
83
+ return Promise.reject('invalid signature');
84
+ }
85
+ const plain = decodeBase64(parts.plain);
86
+ const secure = Buffer.from(await this.cryptoKey.decrypt(toArrayBuffer(decodeBase64(parts.secure))));
87
+ return ({ expire: exp, plain, secure, });
88
+ }
89
+ hasSigningKey() {
90
+ return this.signatureKey.hasSigningKey();
91
+ }
92
+ }
93
+ //# sourceMappingURL=dat.key.js.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"dat.key.js","sourceRoot":"","sources":["../src/dat.key.ts"],"names":[],"mappings":"AAAA,OAAO,EACH,WAAW,EACX,YAAY,EAIZ,eAAe,EAEf,YAAY,EACZ,eAAe,EACf,gBAAgB,EAChB,QAAQ,EACR,aAAa,EAChB,MAAM,YAAY,CAAC;AAGpB,MAAM,OAAO,MAAM;IACC,GAAG,CAAS;IACZ,YAAY,CAAkB;IAC9B,SAAS,CAAe;IACxB,UAAU,CAAS;IACnB,QAAQ,CAAS;IACjB,QAAQ,CAAS;IAEjC,YACI,GAAW,EACX,YAA6B,EAC7B,SAAuB,EACvB,UAAkB,EAClB,QAAgB,EAChB,QAAgB;QAEhB,IAAI,GAAG,CAAC,KAAK,CAAC,SAAS,CAAC,IAAI,IAAI,EAAE,CAAC;YAC/B,MAAM,IAAI,KAAK,CAAC,gBAAgB,GAAG,EAAE,CAAC,CAAC;QAC3C,CAAC;QACD,IAAI,KAAK,CAAC,UAAU,CAAC,IAAI,KAAK,CAAC,QAAQ,CAAC,IAAI,KAAK,CAAC,QAAQ,CAAC,EAAE,CAAC;YAC1D,MAAM,IAAI,KAAK,CAAC,2CAA2C,UAAU,KAAK,QAAQ,KAAK,QAAQ,EAAE,CAAC,CAAC;QACvG,CAAC;QACD,IAAI,CAAC,GAAG,GAAG,GAAG,C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G,CAAC,WAAW,CAAC,GAAG,CAAC,CAAC,CAAC,CAAC;QAC/E,IAAI,CAAC,MAAM,IAAI,CAAC,YAAY,CAAC,MAAM,CAAC,IAAI,EAAE,aAAa,CAAC,YAAY,CAAC,KAAK,CAAC,SAAS,CAAC,CAAC,CAAC,EAAE,CAAC;YACtF,OAAO,OAAO,CAAC,MAAM,CAAC,mBAAmB,CAAC,CAAC;QAC/C,CAAC;QACD,MAAM,KAAK,GAAG,YAAY,CAAC,KAAK,CAAC,KAAK,CAAC,CAAC;QACxC,MAAM,MAAM,GAAG,MAAM,CAAC,IAAI,CAAC,MAAM,IAAI,CAAC,SAAS,CAAC,OAAO,CAAC,aAAa,CAAC,YAAY,CAAC,KAAK,CAAC,MAAM,CAAC,CAAC,CAAC,CAAC,CAAC;QAEpG,OAAO,CAAC,EAAC,MAAM,EAAE,GAAG,EAAE,KAAK,EAAE,MAAM,GAAE,CAAC,CAAC;IAC3C,CAAC;IAED,aAAa;QACT,OAAO,IAAI,CAAC,YAAY,CAAC,aAAa,EAAE,CAAC;IAC7C,CAAC;CACJ"}
@@ -0,0 +1,7 @@
1
+ export declare const DAT_VERSION = "2";
2
+ export * from "./crypto.js";
3
+ export * from "./dat.key.js";
4
+ export * from "./dat.bank.js";
5
+ export * from "./signature.js";
6
+ export * from "./util.js";
7
+ //# sourceMappingURL=index.d.ts.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"index.d.ts","sourceRoot":"","sources":["../src/index.ts"],"names":[],"mappings":"AAAA,eAAO,MAAM,WAAW,MAAM,CAAC;AAE/B,cAAc,aAAa,CAAC;AAC5B,cAAc,cAAc,CAAC;AAC7B,cAAc,eAAe,CAAC;AAC9B,cAAc,gBAAgB,CAAC;AAC/B,cAAc,WAAW,CAAC"}
package/dist/index.js ADDED
@@ -0,0 +1,7 @@
1
+ export const DAT_VERSION = "2";
2
+ export * from "./crypto.js";
3
+ export * from "./dat.key.js";
4
+ export * from "./dat.bank.js";
5
+ export * from "./signature.js";
6
+ export * from "./util.js";
7
+ //# sourceMappingURL=index.js.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"index.js","sourceRoot":"","sources":["../src/index.ts"],"names":[],"mappings":"AAAA,MAAM,CAAC,MAAM,WAAW,GAAG,GAAG,CAAC;AAE/B,cAAc,aAAa,CAAC;AAC5B,cAAc,cAAc,CAAC;AAC7B,cAAc,eAAe,CAAC;AAC9B,cAAc,gBAAgB,CAAC;AAC/B,cAAc,WAAW,CAAC"}
@@ -0,0 +1,16 @@
1
+ export type DatSignatureAlgorithm = "P256" | "P384" | "P521";
2
+ export type DatSignatureKeyOutOption = "FULL" | "SIGNING" | "VERIFYING";
3
+ export declare class DatSignatureKey {
4
+ private readonly config;
5
+ readonly algorithm: DatSignatureAlgorithm;
6
+ private readonly signingKey;
7
+ private readonly verifyingKey;
8
+ constructor(algorithm: DatSignatureAlgorithm, SigningKey: CryptoKey | null, VerifyingKey: CryptoKey);
9
+ static generate(algorithm: DatSignatureAlgorithm): Promise<DatSignatureKey>;
10
+ static imports(algorithmString: string, format: string): Promise<DatSignatureKey>;
11
+ exports(option: DatSignatureKeyOutOption): Promise<string>;
12
+ sign(data: ArrayBuffer): Promise<ArrayBuffer>;
13
+ verify(body: ArrayBuffer, signature: ArrayBuffer): Promise<boolean>;
14
+ hasSigningKey(): boolean;
15
+ }
16
+ //# sourceMappingURL=signature.d.ts.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"signature.d.ts","sourceRoot":"","sources":["../src/signature.ts"],"names":[],"mappings":"AAKA,MAAM,MAAM,qBAAqB,GAAG,MAAM,GAAG,MAAM,GAAG,MAAM,CAAC;AAC7D,MAAM,MAAM,wBAAwB,GAAG,MAAM,GAAG,SAAS,GAAG,WAAW,CAAC;AAYxE,qBAAa,eAAe;IACxB,OAAO,CAAC,QAAQ,CAAC,MAAM,CAAkB;IACzC,SAAgB,SAAS,EAAE,qBAAqB,CAAC;IACjD,OAAO,CAAC,QAAQ,CAAC,UAAU,CAAmB;IAC9C,OAAO,CAAC,QAAQ,CAAC,YAAY,CAAY;gBAGrC,SAAS,EAAE,qBAAqB,EAChC,UAAU,EAAE,SAAS,GAAG,IAAI,EAC5B,YAAY,EAAE,SAAS;WAQd,QAAQ,CAAC,SAAS,EAAE,qBAAqB,GAAG,OAAO,CAAC,eAAe,CAAC;WAYpE,OAAO,CAAC,eAAe,EAAE,MAAM,EAAE,MAAM,EAAE,MAAM,GAAG,OAAO,CAAC,eAAe,CAAC;IAoFjF,OAAO,CAAC,MAAM,EAAE,wBAAwB,GAAG,OAAO,CAAC,MAAM,CAAC;IAkB1D,IAAI,CAAC,IAAI,EAAE,WAAW,GAAG,OAAO,CAAC,WAAW,CAAC;IAgB7C,MAAM,CAAC,IAAI,EAAE,WAAW,EAAE,SAAS,EAAE,WAAW,GAAG,OAAO,CAAC,OAAO,CAAC;IAezE,aAAa,IAAI,OAAO;CAG3B"}
@@ -0,0 +1,140 @@
1
+ import { decodeBase64, encodeBase64Url } from "./index.js";
2
+ import { p256, p384, p521 } from '@noble/curves/nist.js';
3
+ const algorithms = ["P256", "P384", "P521"];
4
+ const SIGNATURE_CONFIG = {
5
+ P256: { name: "ECDSA", curve: 'P-256', hash: 'SHA-256' },
6
+ P384: { name: "ECDSA", curve: 'P-384', hash: 'SHA-384' },
7
+ P521: { name: "ECDSA", curve: 'P-521', hash: 'SHA-512' },
8
+ };
9
+ export class DatSignatureKey {
10
+ config;
11
+ algorithm;
12
+ signingKey;
13
+ verifyingKey;
14
+ constructor(algorithm, SigningKey, VerifyingKey) {
15
+ this.algorithm = algorithm;
16
+ this.signingKey = SigningKey;
17
+ this.verifyingKey = VerifyingKey;
18
+ this.config = SIGNATURE_CONFIG[algorithm];
19
+ }
20
+ static async generate(algorithm) {
21
+ const config = SIGNATURE_CONFIG[algorithm];
22
+ if (!config)
23
+ throw new Error(`Unsupported algorithm: ${algorithm}`);
24
+ const { publicKey, privateKey } = await crypto.subtle.generateKey({ name: config.name, namedCurve: config.curve, }, true, ["sign", "verify"]);
25
+ return new DatSignatureKey(algorithm, privateKey, publicKey);
26
+ }
27
+ static async imports(algorithmString, format) {
28
+ if (!algorithms.includes(algorithmString)) {
29
+ throw new Error(`Unsupported algorithm: ${algorithmString}`);
30
+ }
31
+ const algorithm = algorithmString;
32
+ const config = SIGNATURE_CONFIG[algorithm];
33
+ if (!config)
34
+ throw new Error(`Unsupported algorithm: ${algorithm}`);
35
+ if (!format) {
36
+ throw new Error("Invalid format");
37
+ }
38
+ const split = format.split("~");
39
+ let signingKeyBase64 = '';
40
+ let signingKey = null;
41
+ let verifyingKeyBytes = null;
42
+ let verifyingKey = null;
43
+ let ecdsa;
44
+ switch (algorithm) {
45
+ case "P256":
46
+ ecdsa = p256;
47
+ break;
48
+ case "P384":
49
+ ecdsa = p384;
50
+ break;
51
+ case "P521":
52
+ ecdsa = p521;
53
+ break;
54
+ default:
55
+ throw new Error(`Unsupported algorithm: ${algorithm}`);
56
+ }
57
+ if (split.length == 1) {
58
+ if (split[0]) {
59
+ signingKeyBase64 = split[0];
60
+ }
61
+ }
62
+ else if (split.length == 2) {
63
+ if (split[0]) {
64
+ signingKeyBase64 = split[0];
65
+ }
66
+ if (split[1]) {
67
+ verifyingKeyBytes = new Uint8Array(decodeBase64(split[1]));
68
+ }
69
+ }
70
+ if (signingKeyBase64) {
71
+ const dBytes = decodeBase64(signingKeyBase64);
72
+ // d로부터 공개 키(Point) 계산
73
+ const publicPoint = ecdsa.getPublicKey(dBytes, false);
74
+ // 곡선별 좌표 길이 계산: (전체 길이 - 1) / 2 - 좌표 추출
75
+ const coordinateLen = (publicPoint.length - 1) / 2;
76
+ const xBytes = publicPoint.slice(1, 1 + coordinateLen);
77
+ const yBytes = publicPoint.slice(1 + coordinateLen, 1 + 2 * coordinateLen);
78
+ const jwk = {
79
+ kty: "EC",
80
+ crv: config.curve,
81
+ d: signingKeyBase64,
82
+ x: Buffer.from(xBytes).toString('base64'),
83
+ y: Buffer.from(yBytes).toString('base64'),
84
+ ext: true,
85
+ };
86
+ signingKey = await crypto.subtle.importKey("jwk", jwk, { name: "ECDSA", namedCurve: config.curve }, true, ["sign"]);
87
+ }
88
+ if (!verifyingKeyBytes && signingKeyBase64) {
89
+ verifyingKeyBytes = ecdsa.getPublicKey(decodeBase64(signingKeyBase64), false);
90
+ }
91
+ if (verifyingKeyBytes) {
92
+ verifyingKey = await crypto.subtle.importKey("raw", verifyingKeyBytes, { name: config.name, namedCurve: config.curve }, true, ["verify"]);
93
+ }
94
+ else {
95
+ throw new Error("Invalid format");
96
+ }
97
+ return new DatSignatureKey(algorithm, signingKey, verifyingKey);
98
+ }
99
+ async exports(option) {
100
+ let rv = "";
101
+ if (option == "FULL" || option == "SIGNING") {
102
+ if (this.signingKey) {
103
+ let jwk = await crypto.subtle.exportKey("jwk", this.signingKey);
104
+ rv += jwk.d;
105
+ }
106
+ else {
107
+ throw new Error("this key is verifying only");
108
+ }
109
+ }
110
+ if (option == "FULL" || option == "VERIFYING") {
111
+ rv += '~' + encodeBase64Url(await crypto.subtle.exportKey("raw", this.verifyingKey));
112
+ }
113
+ return rv;
114
+ }
115
+ async sign(data) {
116
+ if (!this.signingKey) {
117
+ throw new Error("this key is verifying only");
118
+ }
119
+ if (!data) {
120
+ throw new Error("data is required");
121
+ }
122
+ return crypto.subtle.sign({
123
+ name: this.config.name,
124
+ hash: { name: this.config.hash },
125
+ }, this.signingKey, data);
126
+ }
127
+ async verify(body, signature) {
128
+ if (!body || !signature) {
129
+ return false;
130
+ }
131
+ return crypto.subtle.verify({
132
+ name: this.config.name,
133
+ hash: { name: this.config.hash },
134
+ }, this.verifyingKey, signature, body);
135
+ }
136
+ hasSigningKey() {
137
+ return this.signingKey !== null;
138
+ }
139
+ }
140
+ //# sourceMappingURL=signature.js.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"signature.js","sourceRoot":"","sources":["../src/signature.ts"],"names":[],"mappings":"AAAA,OAAO,EAAC,YAAY,EAAE,eAAe,EAAC,MAAM,YAAY,CA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4
+ expire: number;
5
+ kid: string;
6
+ plain: string;
7
+ secure: string;
8
+ signature: string;
9
+ };
10
+ export type DatPayload = {
11
+ expire: number;
12
+ plain: string;
13
+ secure: string;
14
+ };
15
+ export type DatPayloadBuffer = {
16
+ expire: number;
17
+ plain: Buffer;
18
+ secure: Buffer;
19
+ };
20
+ export declare function encodeBase64Url(data: any): string;
21
+ export declare function decodeBase64String(b64: any): string;
22
+ export declare function decodeBase64(b64: any): Buffer;
23
+ export declare function concatArrayBufferLike(arr1: ArrayBufferLike, arr2: ArrayBufferLike): ArrayBuffer;
24
+ export declare function toArrayBuffer(data: string | ArrayBuffer | Uint8Array | SharedArrayBuffer | Buffer | null | undefined): ArrayBuffer;
25
+ export declare function randomString(length: number, mold: string): string;
26
+ export declare function randomBase62(length: number): string;
27
+ export declare function splitDat(dat: string | null | undefined): DatParts;
28
+ export declare function serializePayload(payloadBuffer: DatPayloadBuffer): DatPayload;
29
+ //# sourceMappingURL=util.d.ts.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"util.d.ts","sourceRoot":"","sources":["../src/util.ts"],"names":[],"mappings":"AAIA,MAAM,MAAM,QAAQ,GAAG;IACnB,KAAK,EAAE,OAAO,CAAC;IACf,GAAG,EAAE,MAAM,CAAC;IACZ,MAAM,EAAE,MAAM,CAAC;IACf,GAAG,EAAE,MAAM,CAAC;IACZ,KAAK,EAAE,MAAM,CAAC;IACd,MAAM,EAAE,MAAM,CAAC;IACf,SAAS,EAAE,MAAM,CAAC;CACrB,CAAC;AAEF,MAAM,MAAM,UAAU,GAAG;IACrB,MAAM,EAAE,MAAM,CAAC;IACf,KAAK,EAAE,MAAM,CAAC;IACd,MAAM,EAAE,MAAM,CAAC;CAClB,CAAC;AAEF,MAAM,MAAM,gBAAgB,GAAG;IAC3B,MAAM,EAAE,MAAM,CAAC;IACf,KAAK,EAAE,MAAM,CAAC;IACd,MAAM,EAAE,MAAM,CAAC;CAClB,CAAC;AAaF,wBAAgB,eAAe,CAAC,IAAI,EAAE,GAAG,GAAG,MAAM,CAcjD;AAED,wBAAgB,kBAAkB,CAAC,GAAG,EAAE,GAAG,GAAG,MAAM,CAEnD;AAED,wBAAgB,YAAY,CAAC,GAAG,EAAE,GAAG,GAAG,MAAM,CAa7C;AAED,wBAAgB,qBAAqB,CAAC,IAAI,EAAE,eAAe,EAAE,IAAI,EAAE,eAAe,GAAG,WAAW,CAK/F;AAED,wBAAgB,aAAa,CAAC,IAAI,EAAE,MAAM,GAAG,WAAW,GAAG,UAAU,GAAG,iBAAiB,GAAG,MAAM,GAAG,IAAI,GAAG,SAAS,GAAG,WAAW,CA0BlI;AAED,wBAAgB,YAAY,CAAC,MAAM,EAAE,MAAM,EAAE,IAAI,EAAE,MAAM,GAAG,MAAM,CAIjE;AAED,wBAAgB,YAAY,CAAC,MAAM,EAAE,MAAM,GAAG,MAAM,CAEnD;AAED,wBAAgB,QAAQ,CAAC,GAAG,EAAE,MAAM,GAAG,IAAI,GAAG,SAAS,GAAG,QAAQ,CAejE;AAED,wBAAgB,gBAAgB,CAAC,aAAa,EAAE,gBAAgB,GAAG,UAAU,CAM5E"}
package/dist/util.js ADDED
@@ -0,0 +1,106 @@
1
+ const asciiDecoder = new TextDecoder('ascii');
2
+ const textEncoder = new TextEncoder();
3
+ const base62arr = '0123456789ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz';
4
+ // function normalizeBase64Url(base64: string): string {
5
+ // return base64.replace(/[+\/=]/g, (ch: string) => {
6
+ // switch (ch) {
7
+ // case '+': return '-';
8
+ // case '/': return '_';
9
+ // }
10
+ // return '';
11
+ // });
12
+ // }
13
+ export function encodeBase64Url(data) {
14
+ if (!data) {
15
+ return '';
16
+ }
17
+ if (typeof data === 'string') {
18
+ return Buffer.from(data).toString('base64url');
19
+ }
20
+ else if (data instanceof ArrayBuffer) {
21
+ return Buffer.from(data).toString('base64url');
22
+ }
23
+ else if (data instanceof Uint8Array) {
24
+ return Buffer.from(data).toString('base64url');
25
+ }
26
+ else if (data instanceof Buffer) {
27
+ return data.toString('base64url');
28
+ }
29
+ throw new Error('Unsupported data type');
30
+ }
31
+ export function decodeBase64String(b64) {
32
+ return decodeBase64(b64).toString('utf-8');
33
+ }
34
+ export function decodeBase64(b64) {
35
+ if (!b64) {
36
+ return Buffer.alloc(0);
37
+ }
38
+ if (typeof b64 === 'string') {
39
+ return Buffer.from(b64, 'base64');
40
+ }
41
+ if (b64 instanceof ArrayBuffer) {
42
+ return Buffer.from(asciiDecoder.decode(b64), 'base64');
43
+ }
44
+ throw new Error('Unsupported data type');
45
+ }
46
+ export function concatArrayBufferLike(arr1, arr2) {
47
+ const buf = new Uint8Array(arr1.byteLength + arr2.byteLength);
48
+ buf.set(new Uint8Array(arr1), 0);
49
+ buf.set(new Uint8Array(arr2), arr1.byteLength);
50
+ return buf.buffer;
51
+ }
52
+ export function toArrayBuffer(data) {
53
+ if (!data) {
54
+ return new ArrayBuffer(0);
55
+ }
56
+ if (typeof data === 'string') {
57
+ return textEncoder.encode(data).buffer;
58
+ }
59
+ if (Buffer.isBuffer(data)) {
60
+ return data.buffer.slice(data.byteOffset, data.byteOffset + data.byteLength);
61
+ }
62
+ if (data instanceof Uint8Array) {
63
+ return data.buffer.slice(data.byteOffset, data.byteOffset + data.byteLength);
64
+ }
65
+ if (data instanceof ArrayBuffer) {
66
+ return data;
67
+ }
68
+ if (data instanceof SharedArrayBuffer) {
69
+ const buffer = new Uint8Array(data.byteLength);
70
+ buffer.set(new Uint8Array(data));
71
+ return buffer.buffer;
72
+ }
73
+ throw new Error('Unsupported data type');
74
+ }
75
+ export function randomString(length, mold) {
76
+ return [...Array(length)]
77
+ .map(() => mold[Math.floor(Math.random() * mold.length)])
78
+ .join('');
79
+ }
80
+ export function randomBase62(length) {
81
+ return randomString(length, base62arr);
82
+ }
83
+ export function splitDat(dat) {
84
+ const parts = dat?.split('.') || [];
85
+ if (dat && parts.length == 5) {
86
+ let exp = Number(parts[0] || 'x');
87
+ let kid = parts[1];
88
+ let plain = parts[2];
89
+ let secure = parts[3];
90
+ let sign = parts[4];
91
+ if (Number.isInteger(exp) && sign) {
92
+ return ({
93
+ error: false, dat, expire: exp, kid, plain, secure, signature: sign
94
+ });
95
+ }
96
+ }
97
+ return ({ error: true, dat: dat || '', expire: 0, kid: '', plain: '', secure: '', signature: '' });
98
+ }
99
+ export function serializePayload(payloadBuffer) {
100
+ return ({
101
+ expire: payloadBuffer.expire,
102
+ plain: payloadBuffer.plain.toString('utf-8'),
103
+ secure: payloadBuffer.secure.toString('utf-8'),
104
+ });
105
+ }
106
+ //# sourceMappingURL=util.js.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"util.js","sourceRoot":"","sources":["../src/util.ts"],"names":[],"mappings":"AAAA,MAAM,YAAY,GAAG,IAAI,WAAW,CAAC,OAAO,CAAC,CAAC;AAC9C,MAAM,WAAW,GAAG,IAAI,WAAW,EAAE,CAAC;AACtC,MAAM,SAAS,GAAG,gEAAgE,CAAC;AAyBnF,wDAAwD;AACxD,yDAAyD;AACzD,wBAAwB;AACxB,oCAAoC;AACpC,oCAAoC;AACpC,YAAY;AACZ,qBAAqB;AACrB,UAAU;AACV,IAAI;AAEJ,MAAM,UAAU,eAAe,CAAC,IAAS;IACrC,IAAI,CAAC,IAAI,EAAE,CAAC;QACR,OAAO,EAAE,CAAC;IACd,CAAC;IACD,IAAI,OAAO,IAAI,KAAK,QAAQ,EAAE,CAAC;QAC3B,OAAO,MAAM,CAAC,IAAI,CAAC,IAAI,CAAC,CAAC,QAAQ,CAAC,WAAW,CAAC,CAAC;IACnD,CAAC;SAAM,IAAI,IAAI,YAAY,WAAW,EAAE,CAAC;QACrC,OAAO,MAAM,CAAC,IAAI,CAAC,IAAI,CAAC,CAAC,QAAQ,CAAC,WAAW,CAAC,CAAC;IACnD,CAAC;SAAM,IAAI,IAAI,YAAY,UAAU,EAAE,CAAC;QACpC,OAAO,MAAM,CAAC,IAAI,CAAC,IAAI,CAAC,CAAC,QAAQ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K,CAAC,QAAQ,CAAC,OAAO,CAAC;QAC5C,MAAM,EAAE,aAAa,CAAC,MAAM,CAAC,QAAQ,CAAC,OAAO,CAAC;KACjD,CAAC,CAAA;AACN,CAAC"}
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "saro-dat",
3
- "version": "0.0.0",
3
+ "version": "0.0.2",
4
4
  "description": "Distributed Access Token",
5
5
  "keywords": [
6
6
  "dat",
@@ -8,23 +8,41 @@
8
8
  "access",
9
9
  "token"
10
10
  ],
11
- "homepage": "https://github.com/saro-lab/dat-npm#readme",
11
+ "author": "marker",
12
+ "license": "MIT",
13
+ "homepage": "https://dat.saro.me/",
12
14
  "bugs": {
13
15
  "url": "https://github.com/saro-lab/dat-npm/issues"
14
16
  },
15
17
  "repository": {
16
18
  "type": "git",
17
- "url": "git+https://github.com/saro-lab/dat-npm.git"
19
+ "url": "https://github.com/saro-lab/dat-npm"
18
20
  },
19
- "license": "MIT",
20
- "author": "marker",
21
- "type": "commonjs",
22
- "main": "index.js",
21
+ "type": "module",
22
+ "main": "./dist/index.cjs",
23
+ "module": "./dist/index.js",
24
+ "types": "./dist/index.d.ts",
25
+ "exports": {
26
+ ".": {
27
+ "types": "./dist/index.d.ts",
28
+ "import": "./dist/index.js",
29
+ "require": "./dist/index.cjs"
30
+ }
31
+ },
32
+ "files": [
33
+ "dist"
34
+ ],
23
35
  "scripts": {
24
- "build": "tsc",
25
- "test": "echo \"Error: no test specified\" && exit 1"
36
+ "build": "tsc && tsc -p tsconfig.json",
37
+ "test": "vitest run",
38
+ "prepublishOnly": "npm run build"
39
+ },
40
+ "dependencies": {
41
+ "@noble/curves": "^2.2.0"
26
42
  },
27
43
  "devDependencies": {
28
- "typescript": "^6.0.3"
44
+ "@types/node": "^25.6.0",
45
+ "typescript": "^6.0.3",
46
+ "vitest": "^4.1.5"
29
47
  }
30
48
  }
package/.editorconfig DELETED
@@ -1,10 +0,0 @@
1
- root = true
2
-
3
- [*]
4
- end_of_line = lf
5
- insert_final_newline = true
6
-
7
- [*.{js,json,yml}]
8
- charset = utf-8
9
- indent_style = space
10
- indent_size = 2
package/PUBLISH.md DELETED
@@ -1,22 +0,0 @@
1
- # build & publish
2
-
3
- ## build
4
- ``` shell
5
- npm install
6
- ```
7
-
8
- ``` shell
9
- npm run build
10
- ```
11
-
12
- ## login
13
- ``` shell
14
- npm login
15
- ```
16
-
17
- ## publish
18
- 1. build
19
- 2. update next version in README.md and commit
20
- ``` shell
21
- npm publish
22
- ```
package/src/index.ts DELETED
File without changes
package/tsconfig.json DELETED
@@ -1,11 +0,0 @@
1
- {
2
- "compilerOptions": {
3
- "target": "ES6",
4
- "allowJs": true,
5
- "declaration": true,
6
- "strict": true,
7
- "outDir": "dist",
8
- "rootDir": "src",
9
- },
10
- "include": ["src/**/*"]
11
- }