Skip to content

Commit 48e091a

Browse files
committed
refactor(sdk/js): unify browser and node sources
after moving to @noble/hashes and @noble/curves the .browser.ts variants were 95% identical to the node ones — only Buffer vs manual byte helpers and \"import crypto\" vs globalThis.crypto. rewrite both subpaths to use the universal pattern and drop the duplicates in the next commit. - encrypt-env-vars.ts: drop \`import crypto from 'crypto'\` and use the global Web Crypto (works on Node 18+ and browsers) - verify-env-encrypt-public-key.ts: drop Buffer (use manual hex / bigint helpers); make the function bodies stay synchronous like the previous node version, so the public signature does not change both still return the same types and behave identically.
1 parent 7536c6c commit 48e091a

7 files changed

Lines changed: 118 additions & 520 deletions

sdk/js/src/encrypt-env-vars.browser.ts

Lines changed: 0 additions & 66 deletions
This file was deleted.

sdk/js/src/encrypt-env-vars.ts

Lines changed: 29 additions & 31 deletions
Original file line numberDiff line numberDiff line change
@@ -3,65 +3,63 @@
33
// SPDX-License-Identifier: Apache-2.0
44

55
import { x25519 } from "@noble/curves/ed25519"
6-
import crypto from 'crypto'
76

8-
// Convert hex string to Uint8Array
9-
function hexToUint8Array(hex: string) {
10-
hex = hex.startsWith("0x") ? hex.slice(2) : hex;
7+
function hexToUint8Array(hex: string): Uint8Array {
8+
hex = hex.startsWith("0x") ? hex.slice(2) : hex
119
return new Uint8Array(
12-
hex.match(/.{1,2}/g)?.map((byte: string) => parseInt(byte, 16)) ?? [],
13-
);
10+
hex.match(/.{1,2}/g)?.map((byte) => parseInt(byte, 16)) ?? [],
11+
)
1412
}
1513

16-
function uint8ArrayToHex(buffer: Uint8Array) {
14+
function uint8ArrayToHex(buffer: Uint8Array): string {
1715
return Array.from(buffer)
18-
.map((byte: number) => byte.toString(16).padStart(2, "0"))
19-
.join("");
16+
.map((byte) => byte.toString(16).padStart(2, "0"))
17+
.join("")
2018
}
2119

2220
export interface EnvVar {
2321
key: string
2422
value: string
2523
}
2624

27-
// Encrypt environment variables
28-
export async function encryptEnvVars(envs: EnvVar[], publicKeyHex: string) {
29-
// Prepare environment data
30-
const envsJson = JSON.stringify({ env: envs });
25+
/**
26+
* ECIES-encrypt a set of environment variables against a recipient's x25519
27+
* public key. Works on Node 18+ and modern browsers — uses `globalThis.crypto`
28+
* (Web Crypto API) and @noble/curves.
29+
*/
30+
export async function encryptEnvVars(
31+
envs: EnvVar[],
32+
publicKeyHex: string,
33+
): Promise<string> {
34+
const envsJson = JSON.stringify({ env: envs })
3135

32-
// Generate private key and derive public key
33-
const privateKey = x25519.utils.randomPrivateKey();
34-
const publicKey = x25519.getPublicKey(privateKey);
36+
const privateKey = x25519.utils.randomPrivateKey()
37+
const publicKey = x25519.getPublicKey(privateKey)
3538

36-
// Generate shared key
37-
const remotePubkey = hexToUint8Array(publicKeyHex);
38-
const shared = x25519.getSharedSecret(privateKey, remotePubkey);
39+
const remotePubkey = hexToUint8Array(publicKeyHex)
40+
const shared = x25519.getSharedSecret(privateKey, remotePubkey)
3941

40-
// Import shared key for AES-GCM
4142
const importedShared = await crypto.subtle.importKey(
4243
"raw",
4344
new Uint8Array(shared),
4445
{ name: "AES-GCM", length: 256 },
4546
true,
4647
["encrypt"],
47-
);
48+
)
4849

49-
// Encrypt the data
50-
const iv = crypto.getRandomValues(new Uint8Array(12));
50+
const iv = crypto.getRandomValues(new Uint8Array(12))
5151
const encrypted = await crypto.subtle.encrypt(
5252
{ name: "AES-GCM", iv },
5353
importedShared,
5454
new TextEncoder().encode(envsJson),
55-
);
55+
)
5656

57-
// Combine all components
5857
const result = new Uint8Array(
5958
publicKey.length + iv.length + encrypted.byteLength,
60-
);
59+
)
60+
result.set(publicKey)
61+
result.set(iv, publicKey.length)
62+
result.set(new Uint8Array(encrypted), publicKey.length + iv.length)
6163

62-
result.set(publicKey);
63-
result.set(iv, publicKey.length);
64-
result.set(new Uint8Array(encrypted), publicKey.length + iv.length);
65-
66-
return uint8ArrayToHex(result);
64+
return uint8ArrayToHex(result)
6765
}

sdk/js/src/get-compose-hash.browser.ts

Lines changed: 0 additions & 49 deletions
This file was deleted.

0 commit comments

Comments
 (0)