@libp2p/keychain 0.6.1

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.
@@ -0,0 +1,33 @@
1
+ import 'node-forge/lib/pkcs7.js';
2
+ import 'node-forge/lib/pbe.js';
3
+ import type { KeyChain } from './index.js';
4
+ /**
5
+ * Cryptographic Message Syntax (aka PKCS #7)
6
+ *
7
+ * CMS describes an encapsulation syntax for data protection. It
8
+ * is used to digitally sign, digest, authenticate, or encrypt
9
+ * arbitrary message content.
10
+ *
11
+ * See RFC 5652 for all the details.
12
+ */
13
+ export declare class CMS {
14
+ private readonly keychain;
15
+ /**
16
+ * Creates a new instance with a keychain
17
+ */
18
+ constructor(keychain: KeyChain, dek: string);
19
+ /**
20
+ * Creates some protected data.
21
+ *
22
+ * The output Uint8Array contains the PKCS #7 message in DER.
23
+ */
24
+ encrypt(name: string, plain: Uint8Array): Promise<Uint8Array>;
25
+ /**
26
+ * Reads some protected data.
27
+ *
28
+ * The keychain must contain one of the keys used to encrypt the data. If none of the keys
29
+ * exists, an Error is returned with the property 'missingKeys'. It is array of key ids.
30
+ */
31
+ decrypt(cmsData: Uint8Array): Promise<Uint8Array>;
32
+ }
33
+ //# sourceMappingURL=cms.d.ts.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"cms.d.ts","sourceRoot":"","sources":["../../src/cms.ts"],"names":[],"mappings":"AAAA,OAAO,yBAAyB,CAAA;AAChC,OAAO,uBAAuB,CAAA;AAS9B,OAAO,KAAK,EAAE,QAAQ,EAAE,MAAM,YAAY,CAAA;AAM1C;;;;;;;;GAQG;AACH,qBAAa,GAAG;IACd,OAAO,CAAC,QAAQ,CAAC,QAAQ,CAAU;IAEnC;;OAEG;gBACU,QAAQ,EAAE,QAAQ,EAAE,GAAG,EAAE,MAAM;IAS5C;;;;OAIG;IACG,OAAO,CAAE,IAAI,EAAE,MAAM,EAAE,KAAK,EAAE,UAAU,GAAG,OAAO,CAAC,UAAU,CAAC;IA4BpE;;;;;OAKG;IACG,OAAO,CAAE,OAAO,EAAE,UAAU,GAAG,OAAO,CAAC,UAAU,CAAC;CAsEzD"}
@@ -0,0 +1,129 @@
1
+ import 'node-forge/lib/pkcs7.js';
2
+ import 'node-forge/lib/pbe.js';
3
+ // @ts-expect-error types are missing
4
+ import forge from 'node-forge/lib/forge.js';
5
+ import { certificateForKey, findAsync } from './util.js';
6
+ import errCode from 'err-code';
7
+ import { fromString as uint8ArrayFromString } from 'uint8arrays/from-string';
8
+ import { toString as uint8ArrayToString } from 'uint8arrays/to-string';
9
+ import { codes } from './errors.js';
10
+ import { logger } from '@libp2p/logger';
11
+ const log = logger('libp2p:keychain:cms');
12
+ const privates = new WeakMap();
13
+ /**
14
+ * Cryptographic Message Syntax (aka PKCS #7)
15
+ *
16
+ * CMS describes an encapsulation syntax for data protection. It
17
+ * is used to digitally sign, digest, authenticate, or encrypt
18
+ * arbitrary message content.
19
+ *
20
+ * See RFC 5652 for all the details.
21
+ */
22
+ export class CMS {
23
+ /**
24
+ * Creates a new instance with a keychain
25
+ */
26
+ constructor(keychain, dek) {
27
+ if (keychain == null) {
28
+ throw errCode(new Error('keychain is required'), codes.ERR_KEYCHAIN_REQUIRED);
29
+ }
30
+ this.keychain = keychain;
31
+ privates.set(this, { dek });
32
+ }
33
+ /**
34
+ * Creates some protected data.
35
+ *
36
+ * The output Uint8Array contains the PKCS #7 message in DER.
37
+ */
38
+ async encrypt(name, plain) {
39
+ if (!(plain instanceof Uint8Array)) {
40
+ throw errCode(new Error('Plain data must be a Uint8Array'), codes.ERR_INVALID_PARAMETERS);
41
+ }
42
+ const key = await this.keychain.findKeyByName(name);
43
+ const pem = await this.keychain.getPrivateKey(name);
44
+ const cached = privates.get(this);
45
+ if (cached == null) {
46
+ throw errCode(new Error('dek missing'), codes.ERR_INVALID_PARAMETERS);
47
+ }
48
+ const dek = cached.dek;
49
+ const privateKey = forge.pki.decryptRsaPrivateKey(pem, dek);
50
+ const certificate = await certificateForKey(key, privateKey);
51
+ // create a p7 enveloped message
52
+ const p7 = forge.pkcs7.createEnvelopedData();
53
+ p7.addRecipient(certificate);
54
+ p7.content = forge.util.createBuffer(plain);
55
+ p7.encrypt();
56
+ // convert message to DER
57
+ const der = forge.asn1.toDer(p7.toAsn1()).getBytes();
58
+ return uint8ArrayFromString(der, 'ascii');
59
+ }
60
+ /**
61
+ * Reads some protected data.
62
+ *
63
+ * The keychain must contain one of the keys used to encrypt the data. If none of the keys
64
+ * exists, an Error is returned with the property 'missingKeys'. It is array of key ids.
65
+ */
66
+ async decrypt(cmsData) {
67
+ if (!(cmsData instanceof Uint8Array)) {
68
+ throw errCode(new Error('CMS data is required'), codes.ERR_INVALID_PARAMETERS);
69
+ }
70
+ let cms;
71
+ try {
72
+ const buf = forge.util.createBuffer(uint8ArrayToString(cmsData, 'ascii'));
73
+ const obj = forge.asn1.fromDer(buf);
74
+ cms = forge.pkcs7.messageFromAsn1(obj);
75
+ }
76
+ catch (err) {
77
+ log.error(err);
78
+ throw errCode(new Error('Invalid CMS'), codes.ERR_INVALID_CMS);
79
+ }
80
+ // Find a recipient whose key we hold. We only deal with recipient certs
81
+ // issued by ipfs (O=ipfs).
82
+ const recipients = cms.recipients
83
+ // @ts-expect-error cms types not defined
84
+ .filter(r => r.issuer.find(a => a.shortName === 'O' && a.value === 'ipfs'))
85
+ // @ts-expect-error cms types not defined
86
+ .filter(r => r.issuer.find(a => a.shortName === 'CN'))
87
+ // @ts-expect-error cms types not defined
88
+ .map(r => {
89
+ return {
90
+ recipient: r,
91
+ // @ts-expect-error cms types not defined
92
+ keyId: r.issuer.find(a => a.shortName === 'CN').value
93
+ };
94
+ });
95
+ const r = await findAsync(recipients, async (recipient) => {
96
+ try {
97
+ const key = await this.keychain.findKeyById(recipient.keyId);
98
+ if (key != null) {
99
+ return true;
100
+ }
101
+ }
102
+ catch (err) {
103
+ return false;
104
+ }
105
+ return false;
106
+ });
107
+ if (r == null) {
108
+ // @ts-expect-error cms types not defined
109
+ const missingKeys = recipients.map(r => r.keyId);
110
+ throw errCode(new Error(`Decryption needs one of the key(s): ${missingKeys.join(', ')}`), codes.ERR_MISSING_KEYS, {
111
+ missingKeys
112
+ });
113
+ }
114
+ const key = await this.keychain.findKeyById(r.keyId);
115
+ if (key == null) {
116
+ throw errCode(new Error('No key available to decrypto'), codes.ERR_NO_KEY);
117
+ }
118
+ const pem = await this.keychain.getPrivateKey(key.name);
119
+ const cached = privates.get(this);
120
+ if (cached == null) {
121
+ throw errCode(new Error('dek missing'), codes.ERR_INVALID_PARAMETERS);
122
+ }
123
+ const dek = cached.dek;
124
+ const privateKey = forge.pki.decryptRsaPrivateKey(pem, dek);
125
+ cms.decrypt(r.recipient, privateKey);
126
+ return uint8ArrayFromString(cms.content.getBytes(), 'ascii');
127
+ }
128
+ }
129
+ //# sourceMappingURL=cms.js.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"cms.js","sourceRoot":"","sources":["../../src/cms.ts"],"names":[],"mappings":"AAAA,OAAO,yBAAyB,CAAA;AAChC,OAAO,uBAAuB,CAAA;AAC9B,qCAAqC;AACrC,OAAO,KAAK,MAAM,yBAAyB,CAAA;AAC3C,OAAO,EAAE,iBAAiB,EAAE,SAAS,EAAE,MAAM,WAAW,CAAA;AACxD,OAAO,OAAO,MAAM,UAAU,CAAA;AAC9B,OAAO,EAAE,UAAU,IAAI,oBAAoB,EAAE,MAAM,yBAAyB,CAAA;AAC5E,OAAO,EAAE,QAAQ,IAAI,kBAAkB,EAAE,MAAM,uBAAuB,CAAA;AACtE,OAAO,EAAE,KAAK,EAAE,MAAM,aAAa,CAAA;AACnC,OAAO,EAAE,MAAM,EAAE,MAAM,gBAAgB,CAAA;AAGvC,MAAM,GAAG,GAAG,MAAM,CAAC,qBAAqB,CAAC,CAAA;AAEzC,MAAM,QAAQ,GAAG,IAAI,OAAO,EAA2B,CAAA;AAEvD;;;;;;;;GAQG;AACH,MAAM,OAAO,GAAG;IAGd;;OAEG;IACH,YAAa,Q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KAAK,CAAC,CAAA;QAEpD,IAAI,GAAG,IAAI,IAAI,EAAE;YACf,MAAM,OAAO,CAAC,IAAI,KAAK,CAAC,8BAA8B,CAAC,EAAE,KAAK,CAAC,UAAU,CAAC,CAAA;SAC3E;QAED,MAAM,GAAG,GAAG,MAAM,IAAI,CAAC,QAAQ,CAAC,aAAa,CAAC,GAAG,CAAC,IAAI,CAAC,CAAA;QACvD,MAAM,MAAM,GAAG,QAAQ,CAAC,GAAG,CAAC,IAAI,CAAC,CAAA;QAEjC,IAAI,MAAM,IAAI,IAAI,EAAE;YAClB,MAAM,OAAO,CAAC,IAAI,KAAK,CAAC,aAAa,CAAC,EAAE,KAAK,CAAC,sBAAsB,CAAC,CAAA;SACtE;QAED,MAAM,GAAG,GAAG,MAAM,CAAC,GAAG,CAAA;QACtB,MAAM,UAAU,GAAG,KAAK,CAAC,GAAG,CAAC,oBAAoB,CAAC,GAAG,EAAE,GAAG,CAAC,CAAA;QAC3D,GAAG,CAAC,OAAO,CAAC,CAAC,CAAC,SAAS,EAAE,UAAU,CAAC,CAAA;QACpC,OAAO,oBAAoB,CAAC,GAAG,CAAC,OAAO,CAAC,QAAQ,EAAE,EAAE,OAAO,CAAC,CAAA;IAC9D,CAAC;CACF"}
@@ -0,0 +1,22 @@
1
+ export declare enum codes {
2
+ ERR_KEYCHAIN_REQUIRED = "ERR_KEYCHAIN_REQUIRED",
3
+ ERR_INVALID_PARAMETERS = "ERR_INVALID_PARAMETERS",
4
+ ERR_INVALID_CMS = "ERR_INVALID_CMS",
5
+ ERR_MISSING_KEYS = "ERR_MISSING_KEYS",
6
+ ERR_NO_KEY = "ERR_NO_KEY",
7
+ ERR_INVALID_KEY_NAME = "ERR_INVALID_KEY_NAME",
8
+ ERR_INVALID_KEY_TYPE = "ERR_INVALID_KEY_TYPE",
9
+ ERR_KEY_ALREADY_EXISTS = "ERR_KEY_ALREADY_EXISTS",
10
+ ERR_INVALID_KEY_SIZE = "ERR_INVALID_KEY_SIZE",
11
+ ERR_KEY_NOT_FOUND = "ERR_KEY_NOT_FOUND",
12
+ ERR_OLD_KEY_NAME_INVALID = "ERR_OLD_KEY_NAME_INVALID",
13
+ ERR_NEW_KEY_NAME_INVALID = "ERR_NEW_KEY_NAME_INVALID",
14
+ ERR_PASSWORD_REQUIRED = "ERR_PASSWORD_REQUIRED",
15
+ ERR_PEM_REQUIRED = "ERR_PEM_REQUIRED",
16
+ ERR_CANNOT_READ_KEY = "ERR_CANNOT_READ_KEY",
17
+ ERR_MISSING_PRIVATE_KEY = "ERR_MISSING_PRIVATE_KEY",
18
+ ERR_INVALID_OLD_PASS_TYPE = "ERR_INVALID_OLD_PASS_TYPE",
19
+ ERR_INVALID_NEW_PASS_TYPE = "ERR_INVALID_NEW_PASS_TYPE",
20
+ ERR_INVALID_PASS_LENGTH = "ERR_INVALID_PASS_LENGTH"
21
+ }
22
+ //# sourceMappingURL=errors.d.ts.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"errors.d.ts","sourceRoot":"","sources":["../../src/errors.ts"],"names":[],"mappings":"AACA,oBAAY,KAAK;IACf,qBAAqB,0BAA0B;IAC/C,sBAAsB,2BAA2B;IACjD,eAAe,oBAAoB;IACnC,gBAAgB,qBAAqB;IACrC,UAAU,eAAe;IACzB,oBAAoB,yBAAyB;IAC7C,oBAAoB,yBAAyB;IAC7C,sBAAsB,2BAA2B;IACjD,oBAAoB,yBAAyB;IAC7C,iBAAiB,sBAAsB;IACvC,wBAAwB,6BAA6B;IACrD,wBAAwB,6BAA6B;IACrD,qBAAqB,0BAA0B;IAC/C,gBAAgB,qBAAqB;IACrC,mBAAmB,wBAAwB;IAC3C,uBAAuB,4BAA4B;IACnD,yBAAyB,8BAA8B;IACvD,yBAAyB,8BAA8B;IACvD,uBAAuB,4BAA4B;CACpD"}
@@ -0,0 +1,23 @@
1
+ export var codes;
2
+ (function (codes) {
3
+ codes["ERR_KEYCHAIN_REQUIRED"] = "ERR_KEYCHAIN_REQUIRED";
4
+ codes["ERR_INVALID_PARAMETERS"] = "ERR_INVALID_PARAMETERS";
5
+ codes["ERR_INVALID_CMS"] = "ERR_INVALID_CMS";
6
+ codes["ERR_MISSING_KEYS"] = "ERR_MISSING_KEYS";
7
+ codes["ERR_NO_KEY"] = "ERR_NO_KEY";
8
+ codes["ERR_INVALID_KEY_NAME"] = "ERR_INVALID_KEY_NAME";
9
+ codes["ERR_INVALID_KEY_TYPE"] = "ERR_INVALID_KEY_TYPE";
10
+ codes["ERR_KEY_ALREADY_EXISTS"] = "ERR_KEY_ALREADY_EXISTS";
11
+ codes["ERR_INVALID_KEY_SIZE"] = "ERR_INVALID_KEY_SIZE";
12
+ codes["ERR_KEY_NOT_FOUND"] = "ERR_KEY_NOT_FOUND";
13
+ codes["ERR_OLD_KEY_NAME_INVALID"] = "ERR_OLD_KEY_NAME_INVALID";
14
+ codes["ERR_NEW_KEY_NAME_INVALID"] = "ERR_NEW_KEY_NAME_INVALID";
15
+ codes["ERR_PASSWORD_REQUIRED"] = "ERR_PASSWORD_REQUIRED";
16
+ codes["ERR_PEM_REQUIRED"] = "ERR_PEM_REQUIRED";
17
+ codes["ERR_CANNOT_READ_KEY"] = "ERR_CANNOT_READ_KEY";
18
+ codes["ERR_MISSING_PRIVATE_KEY"] = "ERR_MISSING_PRIVATE_KEY";
19
+ codes["ERR_INVALID_OLD_PASS_TYPE"] = "ERR_INVALID_OLD_PASS_TYPE";
20
+ codes["ERR_INVALID_NEW_PASS_TYPE"] = "ERR_INVALID_NEW_PASS_TYPE";
21
+ codes["ERR_INVALID_PASS_LENGTH"] = "ERR_INVALID_PASS_LENGTH";
22
+ })(codes || (codes = {}));
23
+ //# sourceMappingURL=errors.js.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"errors.js","sourceRoot":"","sources":["../../src/errors.ts"],"names":[],"mappings":"AACA,MAAM,CAAN,IAAY,KAoBX;AApBD,WAAY,KAAK;IACf,wDAA+C,CAAA;IAC/C,0DAAiD,CAAA;IACjD,4CAAmC,CAAA;IACnC,8CAAqC,CAAA;IACrC,kCAAyB,CAAA;IACzB,sDAA6C,CAAA;IAC7C,sDAA6C,CAAA;IAC7C,0DAAiD,CAAA;IACjD,sDAA6C,CAAA;IAC7C,gDAAuC,CAAA;IACvC,8DAAqD,CAAA;IACrD,8DAAqD,CAAA;IACrD,wDAA+C,CAAA;IAC/C,8CAAqC,CAAA;IACrC,oDAA2C,CAAA;IAC3C,4DAAmD,CAAA;IACnD,gEAAuD,CAAA;IACvD,gEAAuD,CAAA;IACvD,4DAAmD,CAAA;AACrD,CAAC,EApBW,KAAK,KAAL,KAAK,QAoBhB"}
@@ -0,0 +1,145 @@
1
+ import { CMS } from './cms.js';
2
+ import type { PeerId } from '@libp2p/interface-peer-id';
3
+ import type { Datastore } from 'interface-datastore';
4
+ import type { KeyTypes } from '@libp2p/crypto/keys';
5
+ export interface DEKConfig {
6
+ hash: string;
7
+ salt: string;
8
+ iterationCount: number;
9
+ keyLength: number;
10
+ }
11
+ export interface KeyChainInit {
12
+ pass?: string;
13
+ dek?: DEKConfig;
14
+ }
15
+ /**
16
+ * Information about a key.
17
+ */
18
+ export interface KeyInfo {
19
+ /**
20
+ * The universally unique key id
21
+ */
22
+ id: string;
23
+ /**
24
+ * The local key name.
25
+ */
26
+ name: string;
27
+ }
28
+ export interface KeyChainComponents {
29
+ datastore: Datastore;
30
+ }
31
+ /**
32
+ * Manages the lifecycle of a key. Keys are encrypted at rest using PKCS #8.
33
+ *
34
+ * A key in the store has two entries
35
+ * - '/info/*key-name*', contains the KeyInfo for the key
36
+ * - '/pkcs8/*key-name*', contains the PKCS #8 for the key
37
+ *
38
+ */
39
+ export declare class KeyChain {
40
+ private readonly components;
41
+ private readonly init;
42
+ /**
43
+ * Creates a new instance of a key chain
44
+ */
45
+ constructor(components: KeyChainComponents, init: KeyChainInit);
46
+ /**
47
+ * Gets an object that can encrypt/decrypt protected data
48
+ * using the Cryptographic Message Syntax (CMS).
49
+ *
50
+ * CMS describes an encapsulation syntax for data protection. It
51
+ * is used to digitally sign, digest, authenticate, or encrypt
52
+ * arbitrary message content
53
+ */
54
+ get cms(): CMS;
55
+ /**
56
+ * Generates the options for a keychain. A random salt is produced.
57
+ *
58
+ * @returns {object}
59
+ */
60
+ static generateOptions(): KeyChainInit;
61
+ /**
62
+ * Gets an object that can encrypt/decrypt protected data.
63
+ * The default options for a keychain.
64
+ *
65
+ * @returns {object}
66
+ */
67
+ static get options(): {
68
+ dek: {
69
+ keyLength: number;
70
+ iterationCount: number;
71
+ salt: string;
72
+ hash: string;
73
+ };
74
+ };
75
+ /**
76
+ * Create a new key.
77
+ *
78
+ * @param {string} name - The local key name; cannot already exist.
79
+ * @param {string} type - One of the key types; 'rsa'.
80
+ * @param {number} [size = 2048] - The key size in bits. Used for rsa keys only
81
+ */
82
+ createKey(name: string, type: KeyTypes, size?: number): Promise<KeyInfo>;
83
+ /**
84
+ * List all the keys.
85
+ *
86
+ * @returns {Promise<KeyInfo[]>}
87
+ */
88
+ listKeys(): Promise<any[]>;
89
+ /**
90
+ * Find a key by it's id
91
+ */
92
+ findKeyById(id: string): Promise<KeyInfo>;
93
+ /**
94
+ * Find a key by it's name.
95
+ *
96
+ * @param {string} name - The local key name.
97
+ * @returns {Promise<KeyInfo>}
98
+ */
99
+ findKeyByName(name: string): Promise<KeyInfo>;
100
+ /**
101
+ * Remove an existing key.
102
+ *
103
+ * @param {string} name - The local key name; must already exist.
104
+ * @returns {Promise<KeyInfo>}
105
+ */
106
+ removeKey(name: string): Promise<KeyInfo>;
107
+ /**
108
+ * Rename a key
109
+ *
110
+ * @param {string} oldName - The old local key name; must already exist.
111
+ * @param {string} newName - The new local key name; must not already exist.
112
+ * @returns {Promise<KeyInfo>}
113
+ */
114
+ renameKey(oldName: string, newName: string): Promise<KeyInfo>;
115
+ /**
116
+ * Export an existing key as a PEM encrypted PKCS #8 string
117
+ */
118
+ exportKey(name: string, password: string): Promise<string>;
119
+ /**
120
+ * Export an existing key as a PeerId
121
+ */
122
+ exportPeerId(name: string): Promise<PeerId>;
123
+ /**
124
+ * Import a new key from a PEM encoded PKCS #8 string
125
+ *
126
+ * @param {string} name - The local key name; must not already exist.
127
+ * @param {string} pem - The PEM encoded PKCS #8 string
128
+ * @param {string} password - The password.
129
+ * @returns {Promise<KeyInfo>}
130
+ */
131
+ importKey(name: string, pem: string, password: string): Promise<KeyInfo>;
132
+ /**
133
+ * Import a peer key
134
+ */
135
+ importPeer(name: string, peer: PeerId): Promise<KeyInfo>;
136
+ /**
137
+ * Gets the private key as PEM encoded PKCS #8 string
138
+ */
139
+ getPrivateKey(name: string): Promise<string>;
140
+ /**
141
+ * Rotate keychain password and re-encrypt all associated keys
142
+ */
143
+ rotateKeychainPass(oldPass: string, newPass: string): Promise<void>;
144
+ }
145
+ //# sourceMappingURL=index.d.ts.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"index.d.ts","sourceRoot":"","sources":["../../src/index.ts"],"names":[],"mappings":"AAMA,OAAO,EAAE,GAAG,EAAE,MAAM,UAAU,CAAA;AAM9B,OAAO,KAAK,EAAE,MAAM,EAAE,MAAM,2BAA2B,CAAA;AAEvD,OAAO,KAAK,EAAE,SAAS,EAAE,MAAM,qBAAqB,CAAA;AAEpD,OAAO,KAAK,EAAE,QAAQ,EAAE,MAAM,qBAAqB,CAAA;AAInD,MAAM,WAAW,SAAS;IACxB,IAAI,EAAE,MAAM,CAAA;IACZ,IAAI,EAAE,MAAM,CAAA;IACZ,cAAc,EAAE,MAAM,CAAA;IACtB,SAAS,EAAE,MAAM,CAAA;CAClB;AAED,MAAM,WAAW,YAAY;IAC3B,IAAI,CAAC,EAAE,MAAM,CAAA;IACb,GAAG,CAAC,EAAE,SAAS,CAAA;CAChB;AAED;;GAEG;AACH,MAAM,WAAW,OAAO;IACtB;;OAEG;IACH,EAAE,EAAE,MAAM,CAAA;IAEV;;OAEG;IACH,IAAI,EAAE,MAAM,CAAA;CACb;AA6DD,MAAM,WAAW,kBAAkB;IACjC,SAAS,EAAE,S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