@dynamic-labs/utils 4.0.0-alpha.9 → 4.0.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.
- package/CHANGELOG.md +496 -1
- package/package.cjs +1 -1
- package/package.js +1 -1
- package/package.json +5 -5
- package/src/bufferToBase64/index.d.ts +1 -0
- package/src/eip6963/eip6963Provider.cjs +15 -4
- package/src/eip6963/eip6963Provider.d.ts +8 -3
- package/src/eip6963/eip6963Provider.js +14 -5
- package/src/encryption/convertPublicKeyCryptoKeyToHex/convertPublicKeyCryptoKeyToHex.cjs +31 -0
- package/src/encryption/convertPublicKeyCryptoKeyToHex/convertPublicKeyCryptoKeyToHex.d.ts +11 -0
- package/src/encryption/convertPublicKeyCryptoKeyToHex/convertPublicKeyCryptoKeyToHex.js +27 -0
- package/src/encryption/convertPublicKeyCryptoKeyToHex/index.d.ts +1 -0
- package/src/encryption/convertPublicKeyHexToCryptoKey/convertPublicKeyHexToCryptoKey.cjs +35 -0
- package/src/encryption/convertPublicKeyHexToCryptoKey/convertPublicKeyHexToCryptoKey.d.ts +8 -0
- package/src/encryption/convertPublicKeyHexToCryptoKey/convertPublicKeyHexToCryptoKey.js +31 -0
- package/src/encryption/convertPublicKeyHexToCryptoKey/index.d.ts +1 -0
- package/src/encryption/createKeyPair/createKeyPair.cjs +27 -0
- package/src/encryption/createKeyPair/createKeyPair.d.ts +9 -0
- package/src/encryption/createKeyPair/createKeyPair.js +23 -0
- package/src/encryption/createKeyPair/index.d.ts +1 -0
- package/src/encryption/decryptMessage/decryptMessage.cjs +48 -0
- package/src/encryption/decryptMessage/decryptMessage.d.ts +9 -0
- package/src/encryption/decryptMessage/decryptMessage.js +44 -0
- package/src/encryption/decryptMessage/index.d.ts +1 -0
- package/src/encryption/deriveSharedSecret/deriveSharedSecret.cjs +28 -0
- package/src/encryption/deriveSharedSecret/deriveSharedSecret.d.ts +10 -0
- package/src/encryption/deriveSharedSecret/deriveSharedSecret.js +24 -0
- package/src/encryption/deriveSharedSecret/index.d.ts +1 -0
- package/src/encryption/encryptMessage/encryptMessage.cjs +42 -0
- package/src/encryption/encryptMessage/encryptMessage.d.ts +13 -0
- package/src/encryption/encryptMessage/encryptMessage.js +38 -0
- package/src/encryption/encryptMessage/index.d.ts +1 -0
- package/src/encryption/index.d.ts +7 -0
- package/src/encryption/isEncryptedMessage/index.d.ts +1 -0
- package/src/encryption/isEncryptedMessage/isEncryptedMessage.cjs +28 -0
- package/src/encryption/isEncryptedMessage/isEncryptedMessage.d.ts +7 -0
- package/src/encryption/isEncryptedMessage/isEncryptedMessage.js +24 -0
- package/src/eventTimeline/eventTimeline.cjs +41 -0
- package/src/eventTimeline/eventTimeline.d.ts +18 -0
- package/src/eventTimeline/eventTimeline.js +37 -0
- package/src/eventTimeline/index.d.ts +1 -0
- package/src/getProvidersFromWindow/index.d.ts +1 -0
- package/src/handleMobileWalletRedirect/handleMobileWalletRedirect.cjs +1 -1
- package/src/handleMobileWalletRedirect/handleMobileWalletRedirect.js +1 -1
- package/src/index.cjs +85 -49
- package/src/index.d.ts +33 -24
- package/src/index.js +41 -25
- package/src/isInIframe/index.d.ts +1 -0
- package/src/isInIframe/isInIframe.cjs +8 -0
- package/src/isInIframe/isInIframe.d.ts +1 -0
- package/src/isInIframe/isInIframe.js +4 -0
- package/src/isLedgerAddressViaVerifiedCredentials/index.d.ts +1 -0
- package/src/isMobile/index.d.ts +1 -0
- package/src/isString/index.d.ts +1 -0
- package/src/isString/isString.cjs +8 -0
- package/src/isString/isString.d.ts +1 -0
- package/src/isString/isString.js +4 -0
- package/src/last/index.d.ts +1 -0
- package/src/localStorageAsync/index.d.ts +1 -0
- package/src/{localStorageAsync.cjs → localStorageAsync/localStorageAsync.cjs} +1 -1
- package/src/{localStorageAsync.js → localStorageAsync/localStorageAsync.js} +1 -1
- package/src/parseChainId/index.d.ts +1 -0
- package/src/parseIntSafe/index.d.ts +1 -0
- package/src/parseIntSafe/parseIntSafe.cjs +23 -0
- package/src/parseIntSafe/parseIntSafe.d.ts +1 -0
- package/src/parseIntSafe/parseIntSafe.js +19 -0
- package/src/parseNetworks/index.d.ts +1 -0
- package/src/{parseEvmNetworks.cjs → parseNetworks/parseNetworks.cjs} +3 -1
- package/src/parseNetworks/parseNetworks.d.ts +3 -0
- package/src/{parseEvmNetworks.js → parseNetworks/parseNetworks.js} +3 -2
- package/src/retryableFn/index.d.ts +1 -0
- package/src/{retryableFn.cjs → retryableFn/retryableFn.cjs} +3 -18
- package/src/{retryableFn.d.ts → retryableFn/retryableFn.d.ts} +0 -3
- package/src/{retryableFn.js → retryableFn/retryableFn.js} +3 -18
- package/src/sanitizeName/index.d.ts +1 -0
- package/src/sanitizeName/sanitizeName.cjs +8 -0
- package/src/sanitizeName/sanitizeName.d.ts +1 -0
- package/src/sanitizeName/sanitizeName.js +4 -0
- package/src/services/Oauth2Service/Oauth2Service.d.ts +0 -1
- package/src/services/Oauth2Service/createWindowOauth2Service/createWindowOauth2Service.cjs +9 -1
- package/src/services/Oauth2Service/createWindowOauth2Service/createWindowOauth2Service.js +9 -1
- package/src/services/StorageService/StorageService.d.ts +1 -1
- package/src/services/StorageService/applyPostfixToStorageService/applyPostfixToStorageService.cjs +11 -0
- package/src/services/StorageService/applyPostfixToStorageService/applyPostfixToStorageService.d.ts +7 -0
- package/src/services/StorageService/applyPostfixToStorageService/applyPostfixToStorageService.js +7 -0
- package/src/services/StorageService/applyPostfixToStorageService/index.d.ts +1 -0
- package/src/services/StorageService/createStorageService/createStorageService.cjs +4 -5
- package/src/services/StorageService/createStorageService/createStorageService.d.ts +1 -2
- package/src/services/StorageService/createStorageService/createStorageService.js +4 -5
- package/src/services/StorageService/index.d.ts +1 -0
- package/src/services/StorageService/types.d.ts +1 -1
- package/src/solToLamports/index.d.ts +1 -0
- package/src/solToLamports/solToLamports.cjs +11 -0
- package/src/solToLamports/solToLamports.d.ts +1 -0
- package/src/solToLamports/solToLamports.js +7 -0
- package/src/uint8ArrayToBase64/index.d.ts +1 -0
- package/src/uint8ArrayToBase64/uint8ArrayToBase64.cjs +32 -0
- package/src/uint8ArrayToBase64/uint8ArrayToBase64.d.ts +24 -0
- package/src/uint8ArrayToBase64/uint8ArrayToBase64.js +27 -0
- package/src/parseEvmNetworks.d.ts +0 -2
- /package/src/{bufferPolyfill.cjs → bufferToBase64/bufferPolyfill.cjs} +0 -0
- /package/src/{bufferPolyfill.d.ts → bufferToBase64/bufferPolyfill.d.ts} +0 -0
- /package/src/{bufferPolyfill.js → bufferToBase64/bufferPolyfill.js} +0 -0
- /package/src/{bufferToBase64.cjs → bufferToBase64/bufferToBase64.cjs} +0 -0
- /package/src/{bufferToBase64.d.ts → bufferToBase64/bufferToBase64.d.ts} +0 -0
- /package/src/{bufferToBase64.js → bufferToBase64/bufferToBase64.js} +0 -0
- /package/src/{getProvidersFromWindow.cjs → getProvidersFromWindow/getProvidersFromWindow.cjs} +0 -0
- /package/src/{getProvidersFromWindow.d.ts → getProvidersFromWindow/getProvidersFromWindow.d.ts} +0 -0
- /package/src/{getProvidersFromWindow.js → getProvidersFromWindow/getProvidersFromWindow.js} +0 -0
- /package/src/{isLedgerAddressViaVerifiedCredentials.cjs → isLedgerAddressViaVerifiedCredentials/isLedgerAddressViaVerifiedCredentials.cjs} +0 -0
- /package/src/{isLedgerAddressViaVerifiedCredentials.d.ts → isLedgerAddressViaVerifiedCredentials/isLedgerAddressViaVerifiedCredentials.d.ts} +0 -0
- /package/src/{isLedgerAddressViaVerifiedCredentials.js → isLedgerAddressViaVerifiedCredentials/isLedgerAddressViaVerifiedCredentials.js} +0 -0
- /package/src/{isMobile.cjs → isMobile/isMobile.cjs} +0 -0
- /package/src/{isMobile.d.ts → isMobile/isMobile.d.ts} +0 -0
- /package/src/{isMobile.js → isMobile/isMobile.js} +0 -0
- /package/src/{last.cjs → last/last.cjs} +0 -0
- /package/src/{last.d.ts → last/last.d.ts} +0 -0
- /package/src/{last.js → last/last.js} +0 -0
- /package/src/{localStorageAsync.d.ts → localStorageAsync/localStorageAsync.d.ts} +0 -0
- /package/src/{parseChainId.cjs → parseChainId/parseChainId.cjs} +0 -0
- /package/src/{parseChainId.d.ts → parseChainId/parseChainId.d.ts} +0 -0
- /package/src/{parseChainId.js → parseChainId/parseChainId.js} +0 -0
|
@@ -1,6 +1,18 @@
|
|
|
1
1
|
'use client'
|
|
2
2
|
const isEip9693Event = (event) => event.type === 'eip6963:announceProvider' &&
|
|
3
3
|
event.detail !== undefined;
|
|
4
|
+
const listenToEip6963Events = (handler) => {
|
|
5
|
+
const eventHandler = (event) => {
|
|
6
|
+
if (!isEip9693Event(event)) {
|
|
7
|
+
return;
|
|
8
|
+
}
|
|
9
|
+
handler(event);
|
|
10
|
+
};
|
|
11
|
+
window.addEventListener('eip6963:announceProvider', eventHandler);
|
|
12
|
+
return () => {
|
|
13
|
+
window.removeEventListener('eip6963:announceProvider', eventHandler);
|
|
14
|
+
};
|
|
15
|
+
};
|
|
4
16
|
class Eip6963Provider {
|
|
5
17
|
constructor() {
|
|
6
18
|
this.providers = [];
|
|
@@ -9,10 +21,7 @@ class Eip6963Provider {
|
|
|
9
21
|
if (typeof window === 'undefined') {
|
|
10
22
|
return;
|
|
11
23
|
}
|
|
12
|
-
|
|
13
|
-
if (!isEip9693Event(event)) {
|
|
14
|
-
return;
|
|
15
|
-
}
|
|
24
|
+
listenToEip6963Events((event) => {
|
|
16
25
|
this.providers.push(event.detail);
|
|
17
26
|
});
|
|
18
27
|
window.dispatchEvent(new Event('eip6963:requestProvider'));
|
|
@@ -32,4 +41,4 @@ class Eip6963ProviderSingleton {
|
|
|
32
41
|
}
|
|
33
42
|
}
|
|
34
43
|
|
|
35
|
-
export { Eip6963Provider, Eip6963ProviderSingleton };
|
|
44
|
+
export { Eip6963Provider, Eip6963ProviderSingleton, isEip9693Event, listenToEip6963Events };
|
|
@@ -0,0 +1,31 @@
|
|
|
1
|
+
'use client'
|
|
2
|
+
'use strict';
|
|
3
|
+
|
|
4
|
+
Object.defineProperty(exports, '__esModule', { value: true });
|
|
5
|
+
|
|
6
|
+
var _tslib = require('../../../_virtual/_tslib.cjs');
|
|
7
|
+
|
|
8
|
+
/**
|
|
9
|
+
* Converts a CryptoKey public key to its hexadecimal string representation.
|
|
10
|
+
*
|
|
11
|
+
* @param publicKey - The public CryptoKey to convert. Must be extractable.
|
|
12
|
+
* @returns A Promise that resolves to the hexadecimal string representation of the public key.
|
|
13
|
+
*
|
|
14
|
+
* @example
|
|
15
|
+
* const pair = await createKeyPair(...);
|
|
16
|
+
* const hexString = await convertPublicKeyCryptoKeyToHex(pair.publicKey);
|
|
17
|
+
*/
|
|
18
|
+
const convertPublicKeyCryptoKeyToHex = (publicKey) => _tslib.__awaiter(void 0, void 0, void 0, function* () {
|
|
19
|
+
if (!publicKey.extractable) {
|
|
20
|
+
throw new Error('CryptoKey must be extractable to convert to hex.');
|
|
21
|
+
}
|
|
22
|
+
// Export the CryptoKey to a raw ArrayBuffer
|
|
23
|
+
const exportedKey = yield crypto.subtle.exportKey('raw', publicKey);
|
|
24
|
+
// Convert the ArrayBuffer to a hex string
|
|
25
|
+
const byteArray = new Uint8Array(exportedKey);
|
|
26
|
+
return encodeURIComponent(Array.from(byteArray)
|
|
27
|
+
.map((byte) => byte.toString(16).padStart(2, '0'))
|
|
28
|
+
.join(''));
|
|
29
|
+
});
|
|
30
|
+
|
|
31
|
+
exports.convertPublicKeyCryptoKeyToHex = convertPublicKeyCryptoKeyToHex;
|
|
@@ -0,0 +1,11 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* Converts a CryptoKey public key to its hexadecimal string representation.
|
|
3
|
+
*
|
|
4
|
+
* @param publicKey - The public CryptoKey to convert. Must be extractable.
|
|
5
|
+
* @returns A Promise that resolves to the hexadecimal string representation of the public key.
|
|
6
|
+
*
|
|
7
|
+
* @example
|
|
8
|
+
* const pair = await createKeyPair(...);
|
|
9
|
+
* const hexString = await convertPublicKeyCryptoKeyToHex(pair.publicKey);
|
|
10
|
+
*/
|
|
11
|
+
export declare const convertPublicKeyCryptoKeyToHex: (publicKey: CryptoKey) => Promise<string>;
|
|
@@ -0,0 +1,27 @@
|
|
|
1
|
+
'use client'
|
|
2
|
+
import { __awaiter } from '../../../_virtual/_tslib.js';
|
|
3
|
+
|
|
4
|
+
/**
|
|
5
|
+
* Converts a CryptoKey public key to its hexadecimal string representation.
|
|
6
|
+
*
|
|
7
|
+
* @param publicKey - The public CryptoKey to convert. Must be extractable.
|
|
8
|
+
* @returns A Promise that resolves to the hexadecimal string representation of the public key.
|
|
9
|
+
*
|
|
10
|
+
* @example
|
|
11
|
+
* const pair = await createKeyPair(...);
|
|
12
|
+
* const hexString = await convertPublicKeyCryptoKeyToHex(pair.publicKey);
|
|
13
|
+
*/
|
|
14
|
+
const convertPublicKeyCryptoKeyToHex = (publicKey) => __awaiter(void 0, void 0, void 0, function* () {
|
|
15
|
+
if (!publicKey.extractable) {
|
|
16
|
+
throw new Error('CryptoKey must be extractable to convert to hex.');
|
|
17
|
+
}
|
|
18
|
+
// Export the CryptoKey to a raw ArrayBuffer
|
|
19
|
+
const exportedKey = yield crypto.subtle.exportKey('raw', publicKey);
|
|
20
|
+
// Convert the ArrayBuffer to a hex string
|
|
21
|
+
const byteArray = new Uint8Array(exportedKey);
|
|
22
|
+
return encodeURIComponent(Array.from(byteArray)
|
|
23
|
+
.map((byte) => byte.toString(16).padStart(2, '0'))
|
|
24
|
+
.join(''));
|
|
25
|
+
});
|
|
26
|
+
|
|
27
|
+
export { convertPublicKeyCryptoKeyToHex };
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
export { convertPublicKeyCryptoKeyToHex } from './convertPublicKeyCryptoKeyToHex';
|
|
@@ -0,0 +1,35 @@
|
|
|
1
|
+
'use client'
|
|
2
|
+
'use strict';
|
|
3
|
+
|
|
4
|
+
Object.defineProperty(exports, '__esModule', { value: true });
|
|
5
|
+
|
|
6
|
+
var _tslib = require('../../../_virtual/_tslib.cjs');
|
|
7
|
+
|
|
8
|
+
/**
|
|
9
|
+
* Converts a hexadecimal string representation of a public key into a CryptoKey object
|
|
10
|
+
*
|
|
11
|
+
* @param hex - The hexadecimal string representation of the public key
|
|
12
|
+
* @returns A Promise that resolves to a CryptoKey object suitable for ECDH operations
|
|
13
|
+
* @throws Error if the hex string length is invalid or doesn't represent a valid public key
|
|
14
|
+
*/
|
|
15
|
+
const convertPublicKeyHexToCryptoKey = (hex) => _tslib.__awaiter(void 0, void 0, void 0, function* () {
|
|
16
|
+
var _a, _b;
|
|
17
|
+
// Validate hex string length (65 bytes = 130 hex characters)
|
|
18
|
+
if (hex.length !== 130) {
|
|
19
|
+
throw new Error('Invalid public key length. Expected 130 hex characters.');
|
|
20
|
+
}
|
|
21
|
+
// Validate hex string format (should start with '04' for uncompressed point)
|
|
22
|
+
if (!hex.startsWith('04')) {
|
|
23
|
+
throw new Error('Invalid public key format. Should start with "04".');
|
|
24
|
+
}
|
|
25
|
+
// Convert the hex string to an ArrayBuffer
|
|
26
|
+
const byteArray = new Uint8Array((_b = (_a = decodeURIComponent(hex)
|
|
27
|
+
.match(/.{1,2}/g)) === null || _a === void 0 ? void 0 : _a.map((byte) => parseInt(byte, 16))) !== null && _b !== void 0 ? _b : []);
|
|
28
|
+
// Import the ArrayBuffer as a CryptoKey
|
|
29
|
+
return crypto.subtle.importKey('raw', byteArray.buffer, {
|
|
30
|
+
name: 'ECDH',
|
|
31
|
+
namedCurve: 'P-256',
|
|
32
|
+
}, true, []);
|
|
33
|
+
});
|
|
34
|
+
|
|
35
|
+
exports.convertPublicKeyHexToCryptoKey = convertPublicKeyHexToCryptoKey;
|
|
@@ -0,0 +1,8 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* Converts a hexadecimal string representation of a public key into a CryptoKey object
|
|
3
|
+
*
|
|
4
|
+
* @param hex - The hexadecimal string representation of the public key
|
|
5
|
+
* @returns A Promise that resolves to a CryptoKey object suitable for ECDH operations
|
|
6
|
+
* @throws Error if the hex string length is invalid or doesn't represent a valid public key
|
|
7
|
+
*/
|
|
8
|
+
export declare const convertPublicKeyHexToCryptoKey: (hex: string) => Promise<CryptoKey>;
|
|
@@ -0,0 +1,31 @@
|
|
|
1
|
+
'use client'
|
|
2
|
+
import { __awaiter } from '../../../_virtual/_tslib.js';
|
|
3
|
+
|
|
4
|
+
/**
|
|
5
|
+
* Converts a hexadecimal string representation of a public key into a CryptoKey object
|
|
6
|
+
*
|
|
7
|
+
* @param hex - The hexadecimal string representation of the public key
|
|
8
|
+
* @returns A Promise that resolves to a CryptoKey object suitable for ECDH operations
|
|
9
|
+
* @throws Error if the hex string length is invalid or doesn't represent a valid public key
|
|
10
|
+
*/
|
|
11
|
+
const convertPublicKeyHexToCryptoKey = (hex) => __awaiter(void 0, void 0, void 0, function* () {
|
|
12
|
+
var _a, _b;
|
|
13
|
+
// Validate hex string length (65 bytes = 130 hex characters)
|
|
14
|
+
if (hex.length !== 130) {
|
|
15
|
+
throw new Error('Invalid public key length. Expected 130 hex characters.');
|
|
16
|
+
}
|
|
17
|
+
// Validate hex string format (should start with '04' for uncompressed point)
|
|
18
|
+
if (!hex.startsWith('04')) {
|
|
19
|
+
throw new Error('Invalid public key format. Should start with "04".');
|
|
20
|
+
}
|
|
21
|
+
// Convert the hex string to an ArrayBuffer
|
|
22
|
+
const byteArray = new Uint8Array((_b = (_a = decodeURIComponent(hex)
|
|
23
|
+
.match(/.{1,2}/g)) === null || _a === void 0 ? void 0 : _a.map((byte) => parseInt(byte, 16))) !== null && _b !== void 0 ? _b : []);
|
|
24
|
+
// Import the ArrayBuffer as a CryptoKey
|
|
25
|
+
return crypto.subtle.importKey('raw', byteArray.buffer, {
|
|
26
|
+
name: 'ECDH',
|
|
27
|
+
namedCurve: 'P-256',
|
|
28
|
+
}, true, []);
|
|
29
|
+
});
|
|
30
|
+
|
|
31
|
+
export { convertPublicKeyHexToCryptoKey };
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
export { convertPublicKeyHexToCryptoKey } from './convertPublicKeyHexToCryptoKey';
|
|
@@ -0,0 +1,27 @@
|
|
|
1
|
+
'use client'
|
|
2
|
+
'use strict';
|
|
3
|
+
|
|
4
|
+
Object.defineProperty(exports, '__esModule', { value: true });
|
|
5
|
+
|
|
6
|
+
var _tslib = require('../../../_virtual/_tslib.cjs');
|
|
7
|
+
|
|
8
|
+
/**
|
|
9
|
+
* Generates an ECDH key pair for cryptographic operations.
|
|
10
|
+
* Uses the P-256 curve for key generation, which provides 128-bit security level.
|
|
11
|
+
*
|
|
12
|
+
* @returns {Promise<{publicKey: CryptoKey, privateKey: CryptoKey}>} A promise that resolves to an object containing:
|
|
13
|
+
* - publicKey: The generated public key
|
|
14
|
+
* - privateKey: The generated private key
|
|
15
|
+
*/
|
|
16
|
+
const createKeyPair = () => _tslib.__awaiter(void 0, void 0, void 0, function* () {
|
|
17
|
+
const keyPair = yield crypto.subtle.generateKey({
|
|
18
|
+
name: 'ECDH',
|
|
19
|
+
namedCurve: 'P-256',
|
|
20
|
+
}, true, ['deriveKey', 'deriveBits']);
|
|
21
|
+
return {
|
|
22
|
+
privateKey: keyPair.privateKey,
|
|
23
|
+
publicKey: keyPair.publicKey,
|
|
24
|
+
};
|
|
25
|
+
});
|
|
26
|
+
|
|
27
|
+
exports.createKeyPair = createKeyPair;
|
|
@@ -0,0 +1,9 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* Generates an ECDH key pair for cryptographic operations.
|
|
3
|
+
* Uses the P-256 curve for key generation, which provides 128-bit security level.
|
|
4
|
+
*
|
|
5
|
+
* @returns {Promise<{publicKey: CryptoKey, privateKey: CryptoKey}>} A promise that resolves to an object containing:
|
|
6
|
+
* - publicKey: The generated public key
|
|
7
|
+
* - privateKey: The generated private key
|
|
8
|
+
*/
|
|
9
|
+
export declare const createKeyPair: () => Promise<CryptoKeyPair>;
|
|
@@ -0,0 +1,23 @@
|
|
|
1
|
+
'use client'
|
|
2
|
+
import { __awaiter } from '../../../_virtual/_tslib.js';
|
|
3
|
+
|
|
4
|
+
/**
|
|
5
|
+
* Generates an ECDH key pair for cryptographic operations.
|
|
6
|
+
* Uses the P-256 curve for key generation, which provides 128-bit security level.
|
|
7
|
+
*
|
|
8
|
+
* @returns {Promise<{publicKey: CryptoKey, privateKey: CryptoKey}>} A promise that resolves to an object containing:
|
|
9
|
+
* - publicKey: The generated public key
|
|
10
|
+
* - privateKey: The generated private key
|
|
11
|
+
*/
|
|
12
|
+
const createKeyPair = () => __awaiter(void 0, void 0, void 0, function* () {
|
|
13
|
+
const keyPair = yield crypto.subtle.generateKey({
|
|
14
|
+
name: 'ECDH',
|
|
15
|
+
namedCurve: 'P-256',
|
|
16
|
+
}, true, ['deriveKey', 'deriveBits']);
|
|
17
|
+
return {
|
|
18
|
+
privateKey: keyPair.privateKey,
|
|
19
|
+
publicKey: keyPair.publicKey,
|
|
20
|
+
};
|
|
21
|
+
});
|
|
22
|
+
|
|
23
|
+
export { createKeyPair };
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
export { createKeyPair } from './createKeyPair';
|
|
@@ -0,0 +1,48 @@
|
|
|
1
|
+
'use client'
|
|
2
|
+
'use strict';
|
|
3
|
+
|
|
4
|
+
Object.defineProperty(exports, '__esModule', { value: true });
|
|
5
|
+
|
|
6
|
+
var _tslib = require('../../../_virtual/_tslib.cjs');
|
|
7
|
+
|
|
8
|
+
/**
|
|
9
|
+
* Decrypts an encrypted message using AES-GCM encryption with a shared secret key.
|
|
10
|
+
*
|
|
11
|
+
* @param sharedSecret - The shared secret key in hexadecimal format
|
|
12
|
+
* @param message - The encrypted message in base64 format
|
|
13
|
+
* @param iv - The initialization vector in hexadecimal format
|
|
14
|
+
* @returns A Promise that resolves to the decrypted message as a string
|
|
15
|
+
*/
|
|
16
|
+
const decryptMessage = (sharedSecret, message, iv) => _tslib.__awaiter(void 0, void 0, void 0, function* () {
|
|
17
|
+
var _a, _b;
|
|
18
|
+
const decoder = new TextDecoder();
|
|
19
|
+
const sharedSecretByteArray = new Uint8Array((_b = (_a = decodeURIComponent(sharedSecret)
|
|
20
|
+
.match(/.{1,2}/g)) === null || _a === void 0 ? void 0 : _a.map((byte) => parseInt(byte, 16))) !== null && _b !== void 0 ? _b : []);
|
|
21
|
+
// Import the ArrayBuffer as a CryptoKey
|
|
22
|
+
const sharedSecretCryptoKey = yield crypto.subtle.importKey('raw', sharedSecretByteArray.buffer, 'AES-GCM', true, ['encrypt', 'decrypt']);
|
|
23
|
+
const decryptedData = yield crypto.subtle.decrypt({ iv: hexToUint8Array(decodeURIComponent(iv)), name: 'AES-GCM' }, sharedSecretCryptoKey, base64ToArrayBuffer(decodeURIComponent(message)));
|
|
24
|
+
return decoder.decode(new Uint8Array(decryptedData));
|
|
25
|
+
});
|
|
26
|
+
const hexToUint8Array = (hex) => {
|
|
27
|
+
if (hex.length % 2 !== 0) {
|
|
28
|
+
throw new Error('Hex string must have an even number of characters');
|
|
29
|
+
}
|
|
30
|
+
if (!/^[0-9A-Fa-f]+$/.test(hex)) {
|
|
31
|
+
throw new Error('Invalid hex characters');
|
|
32
|
+
}
|
|
33
|
+
const bytes = new Uint8Array(hex.length / 2);
|
|
34
|
+
for (let i = 0; i < hex.length; i += 2) {
|
|
35
|
+
bytes[i / 2] = parseInt(hex.slice(i, i + 2), 16);
|
|
36
|
+
}
|
|
37
|
+
return bytes;
|
|
38
|
+
};
|
|
39
|
+
const base64ToArrayBuffer = (base64) => {
|
|
40
|
+
const binaryString = atob(base64);
|
|
41
|
+
const bytes = new Uint8Array(binaryString.length);
|
|
42
|
+
for (let i = 0; i < binaryString.length; i++) {
|
|
43
|
+
bytes[i] = binaryString.charCodeAt(i);
|
|
44
|
+
}
|
|
45
|
+
return bytes.buffer;
|
|
46
|
+
};
|
|
47
|
+
|
|
48
|
+
exports.decryptMessage = decryptMessage;
|
|
@@ -0,0 +1,9 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* Decrypts an encrypted message using AES-GCM encryption with a shared secret key.
|
|
3
|
+
*
|
|
4
|
+
* @param sharedSecret - The shared secret key in hexadecimal format
|
|
5
|
+
* @param message - The encrypted message in base64 format
|
|
6
|
+
* @param iv - The initialization vector in hexadecimal format
|
|
7
|
+
* @returns A Promise that resolves to the decrypted message as a string
|
|
8
|
+
*/
|
|
9
|
+
export declare const decryptMessage: (sharedSecret: string, message: string, iv: string) => Promise<string>;
|
|
@@ -0,0 +1,44 @@
|
|
|
1
|
+
'use client'
|
|
2
|
+
import { __awaiter } from '../../../_virtual/_tslib.js';
|
|
3
|
+
|
|
4
|
+
/**
|
|
5
|
+
* Decrypts an encrypted message using AES-GCM encryption with a shared secret key.
|
|
6
|
+
*
|
|
7
|
+
* @param sharedSecret - The shared secret key in hexadecimal format
|
|
8
|
+
* @param message - The encrypted message in base64 format
|
|
9
|
+
* @param iv - The initialization vector in hexadecimal format
|
|
10
|
+
* @returns A Promise that resolves to the decrypted message as a string
|
|
11
|
+
*/
|
|
12
|
+
const decryptMessage = (sharedSecret, message, iv) => __awaiter(void 0, void 0, void 0, function* () {
|
|
13
|
+
var _a, _b;
|
|
14
|
+
const decoder = new TextDecoder();
|
|
15
|
+
const sharedSecretByteArray = new Uint8Array((_b = (_a = decodeURIComponent(sharedSecret)
|
|
16
|
+
.match(/.{1,2}/g)) === null || _a === void 0 ? void 0 : _a.map((byte) => parseInt(byte, 16))) !== null && _b !== void 0 ? _b : []);
|
|
17
|
+
// Import the ArrayBuffer as a CryptoKey
|
|
18
|
+
const sharedSecretCryptoKey = yield crypto.subtle.importKey('raw', sharedSecretByteArray.buffer, 'AES-GCM', true, ['encrypt', 'decrypt']);
|
|
19
|
+
const decryptedData = yield crypto.subtle.decrypt({ iv: hexToUint8Array(decodeURIComponent(iv)), name: 'AES-GCM' }, sharedSecretCryptoKey, base64ToArrayBuffer(decodeURIComponent(message)));
|
|
20
|
+
return decoder.decode(new Uint8Array(decryptedData));
|
|
21
|
+
});
|
|
22
|
+
const hexToUint8Array = (hex) => {
|
|
23
|
+
if (hex.length % 2 !== 0) {
|
|
24
|
+
throw new Error('Hex string must have an even number of characters');
|
|
25
|
+
}
|
|
26
|
+
if (!/^[0-9A-Fa-f]+$/.test(hex)) {
|
|
27
|
+
throw new Error('Invalid hex characters');
|
|
28
|
+
}
|
|
29
|
+
const bytes = new Uint8Array(hex.length / 2);
|
|
30
|
+
for (let i = 0; i < hex.length; i += 2) {
|
|
31
|
+
bytes[i / 2] = parseInt(hex.slice(i, i + 2), 16);
|
|
32
|
+
}
|
|
33
|
+
return bytes;
|
|
34
|
+
};
|
|
35
|
+
const base64ToArrayBuffer = (base64) => {
|
|
36
|
+
const binaryString = atob(base64);
|
|
37
|
+
const bytes = new Uint8Array(binaryString.length);
|
|
38
|
+
for (let i = 0; i < binaryString.length; i++) {
|
|
39
|
+
bytes[i] = binaryString.charCodeAt(i);
|
|
40
|
+
}
|
|
41
|
+
return bytes.buffer;
|
|
42
|
+
};
|
|
43
|
+
|
|
44
|
+
export { decryptMessage };
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
export { decryptMessage } from './decryptMessage';
|
|
@@ -0,0 +1,28 @@
|
|
|
1
|
+
'use client'
|
|
2
|
+
'use strict';
|
|
3
|
+
|
|
4
|
+
Object.defineProperty(exports, '__esModule', { value: true });
|
|
5
|
+
|
|
6
|
+
var _tslib = require('../../../_virtual/_tslib.cjs');
|
|
7
|
+
var convertPublicKeyCryptoKeyToHex = require('../convertPublicKeyCryptoKeyToHex/convertPublicKeyCryptoKeyToHex.cjs');
|
|
8
|
+
|
|
9
|
+
/**
|
|
10
|
+
* Derives a shared secret key using ECDH (Elliptic Curve Diffie-Hellman) key agreement protocol.
|
|
11
|
+
* The resulting shared secret can be used for AES-GCM encryption/decryption operations.
|
|
12
|
+
*
|
|
13
|
+
* @param ownPrivateKey - The private key of the current party (CryptoKey format)
|
|
14
|
+
* @param peerPublicKey - The public key of the peer party (CryptoKey format)
|
|
15
|
+
* @returns A Promise that resolves to a hex string representing the derived shared secret
|
|
16
|
+
* configured for AES-GCM encryption/decryption operations
|
|
17
|
+
*/
|
|
18
|
+
const deriveSharedSecret = (ownPrivateKey, peerPublicKey) => _tslib.__awaiter(void 0, void 0, void 0, function* () {
|
|
19
|
+
return convertPublicKeyCryptoKeyToHex.convertPublicKeyCryptoKeyToHex(yield crypto.subtle.deriveKey({
|
|
20
|
+
name: 'ECDH',
|
|
21
|
+
public: peerPublicKey,
|
|
22
|
+
}, ownPrivateKey, {
|
|
23
|
+
length: 256,
|
|
24
|
+
name: 'AES-GCM',
|
|
25
|
+
}, true, ['encrypt', 'decrypt']));
|
|
26
|
+
});
|
|
27
|
+
|
|
28
|
+
exports.deriveSharedSecret = deriveSharedSecret;
|
|
@@ -0,0 +1,10 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* Derives a shared secret key using ECDH (Elliptic Curve Diffie-Hellman) key agreement protocol.
|
|
3
|
+
* The resulting shared secret can be used for AES-GCM encryption/decryption operations.
|
|
4
|
+
*
|
|
5
|
+
* @param ownPrivateKey - The private key of the current party (CryptoKey format)
|
|
6
|
+
* @param peerPublicKey - The public key of the peer party (CryptoKey format)
|
|
7
|
+
* @returns A Promise that resolves to a hex string representing the derived shared secret
|
|
8
|
+
* configured for AES-GCM encryption/decryption operations
|
|
9
|
+
*/
|
|
10
|
+
export declare const deriveSharedSecret: (ownPrivateKey: CryptoKey, peerPublicKey: CryptoKey) => Promise<string>;
|
|
@@ -0,0 +1,24 @@
|
|
|
1
|
+
'use client'
|
|
2
|
+
import { __awaiter } from '../../../_virtual/_tslib.js';
|
|
3
|
+
import { convertPublicKeyCryptoKeyToHex } from '../convertPublicKeyCryptoKeyToHex/convertPublicKeyCryptoKeyToHex.js';
|
|
4
|
+
|
|
5
|
+
/**
|
|
6
|
+
* Derives a shared secret key using ECDH (Elliptic Curve Diffie-Hellman) key agreement protocol.
|
|
7
|
+
* The resulting shared secret can be used for AES-GCM encryption/decryption operations.
|
|
8
|
+
*
|
|
9
|
+
* @param ownPrivateKey - The private key of the current party (CryptoKey format)
|
|
10
|
+
* @param peerPublicKey - The public key of the peer party (CryptoKey format)
|
|
11
|
+
* @returns A Promise that resolves to a hex string representing the derived shared secret
|
|
12
|
+
* configured for AES-GCM encryption/decryption operations
|
|
13
|
+
*/
|
|
14
|
+
const deriveSharedSecret = (ownPrivateKey, peerPublicKey) => __awaiter(void 0, void 0, void 0, function* () {
|
|
15
|
+
return convertPublicKeyCryptoKeyToHex(yield crypto.subtle.deriveKey({
|
|
16
|
+
name: 'ECDH',
|
|
17
|
+
public: peerPublicKey,
|
|
18
|
+
}, ownPrivateKey, {
|
|
19
|
+
length: 256,
|
|
20
|
+
name: 'AES-GCM',
|
|
21
|
+
}, true, ['encrypt', 'decrypt']));
|
|
22
|
+
});
|
|
23
|
+
|
|
24
|
+
export { deriveSharedSecret };
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
export { deriveSharedSecret } from './deriveSharedSecret';
|
|
@@ -0,0 +1,42 @@
|
|
|
1
|
+
'use client'
|
|
2
|
+
'use strict';
|
|
3
|
+
|
|
4
|
+
Object.defineProperty(exports, '__esModule', { value: true });
|
|
5
|
+
|
|
6
|
+
var _tslib = require('../../../_virtual/_tslib.cjs');
|
|
7
|
+
|
|
8
|
+
/**
|
|
9
|
+
* Encrypts a message using AES-GCM encryption with a shared secret key
|
|
10
|
+
* @param sharedSecret - Hexadecimal string representing the shared secret key
|
|
11
|
+
* @param message - The plaintext message to encrypt
|
|
12
|
+
* @returns {Promise<{encryptedMessage: string, iv: string}>} Object containing:
|
|
13
|
+
* - encryptedMessage: Base64 encoded encrypted message
|
|
14
|
+
* - iv: Hexadecimal initialization vector used for encryption
|
|
15
|
+
*/
|
|
16
|
+
const encryptMessage = (sharedSecret, message) => _tslib.__awaiter(void 0, void 0, void 0, function* () {
|
|
17
|
+
var _a, _b;
|
|
18
|
+
const encoder = new TextEncoder();
|
|
19
|
+
const iv = crypto.getRandomValues(new Uint8Array(12));
|
|
20
|
+
const data = encoder.encode(message);
|
|
21
|
+
const sharedSecretByteArray = new Uint8Array((_b = (_a = sharedSecret.match(/.{1,2}/g)) === null || _a === void 0 ? void 0 : _a.map((byte) => parseInt(byte, 16))) !== null && _b !== void 0 ? _b : []);
|
|
22
|
+
// Import the ArrayBuffer as a CryptoKey
|
|
23
|
+
const sharedSecretCryptoKey = yield crypto.subtle.importKey('raw', sharedSecretByteArray.buffer, 'AES-GCM', true, ['encrypt', 'decrypt']);
|
|
24
|
+
const encryptedMessage = yield crypto.subtle.encrypt({ iv, name: 'AES-GCM' }, sharedSecretCryptoKey, data);
|
|
25
|
+
return {
|
|
26
|
+
encryptedMessage: encodeURIComponent(arrayBufferToBase64(encryptedMessage)),
|
|
27
|
+
iv: encodeURIComponent(uint8ArrayToHex(iv)),
|
|
28
|
+
};
|
|
29
|
+
});
|
|
30
|
+
const arrayBufferToBase64 = (buffer) => {
|
|
31
|
+
const bytes = new Uint8Array(buffer);
|
|
32
|
+
let binary = '';
|
|
33
|
+
for (let i = 0; i < bytes.byteLength; i++) {
|
|
34
|
+
binary += String.fromCharCode(bytes[i]);
|
|
35
|
+
}
|
|
36
|
+
return btoa(binary);
|
|
37
|
+
};
|
|
38
|
+
const uint8ArrayToHex = (arr) => Array.from(arr)
|
|
39
|
+
.map((b) => b.toString(16).padStart(2, '0'))
|
|
40
|
+
.join('');
|
|
41
|
+
|
|
42
|
+
exports.encryptMessage = encryptMessage;
|
|
@@ -0,0 +1,13 @@
|
|
|
1
|
+
export type EncryptedMessage = {
|
|
2
|
+
encryptedMessage: string;
|
|
3
|
+
iv: string;
|
|
4
|
+
};
|
|
5
|
+
/**
|
|
6
|
+
* Encrypts a message using AES-GCM encryption with a shared secret key
|
|
7
|
+
* @param sharedSecret - Hexadecimal string representing the shared secret key
|
|
8
|
+
* @param message - The plaintext message to encrypt
|
|
9
|
+
* @returns {Promise<{encryptedMessage: string, iv: string}>} Object containing:
|
|
10
|
+
* - encryptedMessage: Base64 encoded encrypted message
|
|
11
|
+
* - iv: Hexadecimal initialization vector used for encryption
|
|
12
|
+
*/
|
|
13
|
+
export declare const encryptMessage: (sharedSecret: string, message: string) => Promise<EncryptedMessage>;
|
|
@@ -0,0 +1,38 @@
|
|
|
1
|
+
'use client'
|
|
2
|
+
import { __awaiter } from '../../../_virtual/_tslib.js';
|
|
3
|
+
|
|
4
|
+
/**
|
|
5
|
+
* Encrypts a message using AES-GCM encryption with a shared secret key
|
|
6
|
+
* @param sharedSecret - Hexadecimal string representing the shared secret key
|
|
7
|
+
* @param message - The plaintext message to encrypt
|
|
8
|
+
* @returns {Promise<{encryptedMessage: string, iv: string}>} Object containing:
|
|
9
|
+
* - encryptedMessage: Base64 encoded encrypted message
|
|
10
|
+
* - iv: Hexadecimal initialization vector used for encryption
|
|
11
|
+
*/
|
|
12
|
+
const encryptMessage = (sharedSecret, message) => __awaiter(void 0, void 0, void 0, function* () {
|
|
13
|
+
var _a, _b;
|
|
14
|
+
const encoder = new TextEncoder();
|
|
15
|
+
const iv = crypto.getRandomValues(new Uint8Array(12));
|
|
16
|
+
const data = encoder.encode(message);
|
|
17
|
+
const sharedSecretByteArray = new Uint8Array((_b = (_a = sharedSecret.match(/.{1,2}/g)) === null || _a === void 0 ? void 0 : _a.map((byte) => parseInt(byte, 16))) !== null && _b !== void 0 ? _b : []);
|
|
18
|
+
// Import the ArrayBuffer as a CryptoKey
|
|
19
|
+
const sharedSecretCryptoKey = yield crypto.subtle.importKey('raw', sharedSecretByteArray.buffer, 'AES-GCM', true, ['encrypt', 'decrypt']);
|
|
20
|
+
const encryptedMessage = yield crypto.subtle.encrypt({ iv, name: 'AES-GCM' }, sharedSecretCryptoKey, data);
|
|
21
|
+
return {
|
|
22
|
+
encryptedMessage: encodeURIComponent(arrayBufferToBase64(encryptedMessage)),
|
|
23
|
+
iv: encodeURIComponent(uint8ArrayToHex(iv)),
|
|
24
|
+
};
|
|
25
|
+
});
|
|
26
|
+
const arrayBufferToBase64 = (buffer) => {
|
|
27
|
+
const bytes = new Uint8Array(buffer);
|
|
28
|
+
let binary = '';
|
|
29
|
+
for (let i = 0; i < bytes.byteLength; i++) {
|
|
30
|
+
binary += String.fromCharCode(bytes[i]);
|
|
31
|
+
}
|
|
32
|
+
return btoa(binary);
|
|
33
|
+
};
|
|
34
|
+
const uint8ArrayToHex = (arr) => Array.from(arr)
|
|
35
|
+
.map((b) => b.toString(16).padStart(2, '0'))
|
|
36
|
+
.join('');
|
|
37
|
+
|
|
38
|
+
export { encryptMessage };
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
export { encryptMessage, type EncryptedMessage } from './encryptMessage';
|
|
@@ -0,0 +1,7 @@
|
|
|
1
|
+
export { createKeyPair } from './createKeyPair';
|
|
2
|
+
export { deriveSharedSecret } from './deriveSharedSecret';
|
|
3
|
+
export { convertPublicKeyCryptoKeyToHex } from './convertPublicKeyCryptoKeyToHex';
|
|
4
|
+
export { convertPublicKeyHexToCryptoKey } from './convertPublicKeyHexToCryptoKey';
|
|
5
|
+
export { decryptMessage } from './decryptMessage';
|
|
6
|
+
export { encryptMessage, type EncryptedMessage } from './encryptMessage';
|
|
7
|
+
export { isEncryptedMessage } from './isEncryptedMessage';
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
export { isEncryptedMessage } from './isEncryptedMessage';
|
|
@@ -0,0 +1,28 @@
|
|
|
1
|
+
'use client'
|
|
2
|
+
'use strict';
|
|
3
|
+
|
|
4
|
+
Object.defineProperty(exports, '__esModule', { value: true });
|
|
5
|
+
|
|
6
|
+
/**
|
|
7
|
+
* Type guard to check if a value is an encrypted message
|
|
8
|
+
* @param value - Value to check
|
|
9
|
+
* @returns boolean indicating if the value matches the EncryptedMessage format
|
|
10
|
+
*/
|
|
11
|
+
const isEncryptedMessage = (value) => {
|
|
12
|
+
if (!value ||
|
|
13
|
+
typeof value !== 'object' ||
|
|
14
|
+
Array.isArray(value) ||
|
|
15
|
+
value === null ||
|
|
16
|
+
typeof value === 'string') {
|
|
17
|
+
return false;
|
|
18
|
+
}
|
|
19
|
+
const candidate = value;
|
|
20
|
+
const keys = Object.keys(candidate);
|
|
21
|
+
return (keys.length === 2 &&
|
|
22
|
+
keys.includes('encryptedMessage') &&
|
|
23
|
+
keys.includes('iv') &&
|
|
24
|
+
typeof candidate['encryptedMessage'] === 'string' &&
|
|
25
|
+
typeof candidate['iv'] === 'string');
|
|
26
|
+
};
|
|
27
|
+
|
|
28
|
+
exports.isEncryptedMessage = isEncryptedMessage;
|
|
@@ -0,0 +1,7 @@
|
|
|
1
|
+
import { EncryptedMessage } from '../encryptMessage';
|
|
2
|
+
/**
|
|
3
|
+
* Type guard to check if a value is an encrypted message
|
|
4
|
+
* @param value - Value to check
|
|
5
|
+
* @returns boolean indicating if the value matches the EncryptedMessage format
|
|
6
|
+
*/
|
|
7
|
+
export declare const isEncryptedMessage: (value: unknown) => value is EncryptedMessage;
|
|
@@ -0,0 +1,24 @@
|
|
|
1
|
+
'use client'
|
|
2
|
+
/**
|
|
3
|
+
* Type guard to check if a value is an encrypted message
|
|
4
|
+
* @param value - Value to check
|
|
5
|
+
* @returns boolean indicating if the value matches the EncryptedMessage format
|
|
6
|
+
*/
|
|
7
|
+
const isEncryptedMessage = (value) => {
|
|
8
|
+
if (!value ||
|
|
9
|
+
typeof value !== 'object' ||
|
|
10
|
+
Array.isArray(value) ||
|
|
11
|
+
value === null ||
|
|
12
|
+
typeof value === 'string') {
|
|
13
|
+
return false;
|
|
14
|
+
}
|
|
15
|
+
const candidate = value;
|
|
16
|
+
const keys = Object.keys(candidate);
|
|
17
|
+
return (keys.length === 2 &&
|
|
18
|
+
keys.includes('encryptedMessage') &&
|
|
19
|
+
keys.includes('iv') &&
|
|
20
|
+
typeof candidate['encryptedMessage'] === 'string' &&
|
|
21
|
+
typeof candidate['iv'] === 'string');
|
|
22
|
+
};
|
|
23
|
+
|
|
24
|
+
export { isEncryptedMessage };
|