@bandeira-tech/b3nd-web 0.2.1 → 0.2.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.
@@ -1,327 +0,0 @@
1
- import {
2
- __export
3
- } from "./chunk-MLKGABMK.js";
4
-
5
- // encrypt/mod.ts
6
- var mod_exports = {};
7
- __export(mod_exports, {
8
- createAuthenticatedMessage: () => createAuthenticatedMessage,
9
- createSignedEncryptedMessage: () => createSignedEncryptedMessage,
10
- decrypt: () => decrypt,
11
- decryptWithHex: () => decryptWithHex,
12
- encrypt: () => encrypt,
13
- generateEncryptionKeyPair: () => generateEncryptionKeyPair,
14
- generateNonce: () => generateNonce,
15
- generateRandomData: () => generateRandomData,
16
- generateSigningKeyPair: () => generateSigningKeyPair,
17
- sign: () => sign,
18
- signWithHex: () => signWithHex,
19
- verify: () => verify,
20
- verifyAndDecrypt: () => verifyAndDecrypt
21
- });
22
-
23
- // shared/encoding.ts
24
- function encodeHex(bytes) {
25
- return Array.from(bytes).map((b) => b.toString(16).padStart(2, "0")).join("");
26
- }
27
- function decodeHex(hex) {
28
- if (hex.length % 2 !== 0) {
29
- throw new Error("Invalid hex input");
30
- }
31
- const buffer = new ArrayBuffer(hex.length / 2);
32
- const bytes = new Uint8Array(buffer);
33
- for (let i = 0; i < hex.length; i += 2) {
34
- bytes[i / 2] = parseInt(hex.slice(i, i + 2), 16);
35
- }
36
- return bytes;
37
- }
38
- function encodeBase64(bytes) {
39
- if (typeof Buffer !== "undefined") {
40
- return Buffer.from(bytes).toString("base64");
41
- }
42
- let binary = "";
43
- bytes.forEach((b) => binary += String.fromCharCode(b));
44
- return btoa(binary);
45
- }
46
- function decodeBase64(b64) {
47
- if (typeof Buffer !== "undefined") {
48
- return new Uint8Array(Buffer.from(b64, "base64"));
49
- }
50
- const binary = atob(b64);
51
- const bytes = new Uint8Array(binary.length);
52
- for (let i = 0; i < binary.length; i++) {
53
- bytes[i] = binary.charCodeAt(i);
54
- }
55
- return bytes;
56
- }
57
-
58
- // encrypt/mod.ts
59
- async function generateSigningKeyPair() {
60
- const keyPair = await crypto.subtle.generateKey(
61
- {
62
- name: "Ed25519",
63
- namedCurve: "Ed25519"
64
- },
65
- true,
66
- ["sign", "verify"]
67
- );
68
- const publicKeyBytes = await crypto.subtle.exportKey("raw", keyPair.publicKey);
69
- const privateKeyBytes = await crypto.subtle.exportKey(
70
- "pkcs8",
71
- keyPair.privateKey
72
- );
73
- return {
74
- publicKey: keyPair.publicKey,
75
- privateKey: keyPair.privateKey,
76
- publicKeyHex: encodeHex(new Uint8Array(publicKeyBytes)),
77
- privateKeyHex: encodeHex(new Uint8Array(privateKeyBytes))
78
- };
79
- }
80
- async function generateEncryptionKeyPair() {
81
- const keyPair = await crypto.subtle.generateKey(
82
- {
83
- name: "X25519",
84
- namedCurve: "X25519"
85
- },
86
- true,
87
- ["deriveBits"]
88
- );
89
- const publicKeyBytes = await crypto.subtle.exportKey("raw", keyPair.publicKey);
90
- return {
91
- publicKey: keyPair.publicKey,
92
- privateKey: keyPair.privateKey,
93
- publicKeyHex: encodeHex(new Uint8Array(publicKeyBytes))
94
- };
95
- }
96
- async function sign(privateKey, payload) {
97
- const encoder = new TextEncoder();
98
- const data = encoder.encode(JSON.stringify(payload));
99
- const signature = await crypto.subtle.sign("Ed25519", privateKey, data);
100
- return encodeHex(new Uint8Array(signature));
101
- }
102
- async function signWithHex(privateKeyHex, payload) {
103
- const privateKeyBytes = decodeHex(privateKeyHex).buffer;
104
- const privateKey = await crypto.subtle.importKey(
105
- "pkcs8",
106
- privateKeyBytes,
107
- {
108
- name: "Ed25519",
109
- namedCurve: "Ed25519"
110
- },
111
- false,
112
- ["sign"]
113
- );
114
- return await sign(privateKey, payload);
115
- }
116
- async function verify(publicKeyHex, signatureHex, payload) {
117
- try {
118
- const publicKeyBytes = decodeHex(publicKeyHex).buffer;
119
- const publicKey = await crypto.subtle.importKey(
120
- "raw",
121
- publicKeyBytes,
122
- {
123
- name: "Ed25519",
124
- namedCurve: "Ed25519"
125
- },
126
- false,
127
- ["verify"]
128
- );
129
- const encoder = new TextEncoder();
130
- const data = encoder.encode(JSON.stringify(payload));
131
- const signatureBytes = decodeHex(signatureHex).buffer;
132
- return await crypto.subtle.verify(
133
- "Ed25519",
134
- publicKey,
135
- signatureBytes,
136
- data
137
- );
138
- } catch (error) {
139
- console.error("Verification error:", error);
140
- return false;
141
- }
142
- }
143
- async function encrypt(data, recipientPublicKeyHex) {
144
- const ephemeralKeyPair = await generateEncryptionKeyPair();
145
- const recipientPublicKeyBytes = decodeHex(recipientPublicKeyHex).buffer;
146
- const recipientPublicKey = await crypto.subtle.importKey(
147
- "raw",
148
- recipientPublicKeyBytes,
149
- {
150
- name: "X25519",
151
- namedCurve: "X25519"
152
- },
153
- false,
154
- []
155
- );
156
- const sharedSecret = await crypto.subtle.deriveBits(
157
- {
158
- name: "X25519",
159
- public: recipientPublicKey
160
- },
161
- ephemeralKeyPair.privateKey,
162
- 256
163
- );
164
- const aesKey = await crypto.subtle.importKey(
165
- "raw",
166
- sharedSecret,
167
- {
168
- name: "AES-GCM",
169
- length: 256
170
- },
171
- false,
172
- ["encrypt"]
173
- );
174
- const nonce = crypto.getRandomValues(new Uint8Array(12));
175
- const encoder = new TextEncoder();
176
- const plaintext = encoder.encode(JSON.stringify(data));
177
- const ciphertext = await crypto.subtle.encrypt(
178
- {
179
- name: "AES-GCM",
180
- iv: nonce
181
- },
182
- aesKey,
183
- plaintext
184
- );
185
- return {
186
- data: encodeBase64(new Uint8Array(ciphertext)),
187
- nonce: encodeBase64(nonce),
188
- ephemeralPublicKey: ephemeralKeyPair.publicKeyHex
189
- };
190
- }
191
- async function decrypt(encryptedPayload, recipientPrivateKey) {
192
- if (!encryptedPayload.ephemeralPublicKey) {
193
- throw new Error("Missing ephemeral public key");
194
- }
195
- const ephemeralPublicKeyBytes = decodeHex(
196
- encryptedPayload.ephemeralPublicKey
197
- ).buffer;
198
- const ephemeralPublicKey = await crypto.subtle.importKey(
199
- "raw",
200
- ephemeralPublicKeyBytes,
201
- {
202
- name: "X25519",
203
- namedCurve: "X25519"
204
- },
205
- false,
206
- []
207
- );
208
- const sharedSecret = await crypto.subtle.deriveBits(
209
- {
210
- name: "X25519",
211
- public: ephemeralPublicKey
212
- },
213
- recipientPrivateKey,
214
- 256
215
- );
216
- const aesKey = await crypto.subtle.importKey(
217
- "raw",
218
- sharedSecret,
219
- {
220
- name: "AES-GCM",
221
- length: 256
222
- },
223
- false,
224
- ["decrypt"]
225
- );
226
- const ciphertext = new Uint8Array(decodeBase64(encryptedPayload.data));
227
- const nonce = new Uint8Array(decodeBase64(encryptedPayload.nonce));
228
- const plaintext = await crypto.subtle.decrypt(
229
- {
230
- name: "AES-GCM",
231
- iv: nonce
232
- },
233
- aesKey,
234
- ciphertext
235
- );
236
- const decoder = new TextDecoder();
237
- const json = decoder.decode(plaintext);
238
- return JSON.parse(json);
239
- }
240
- async function decryptWithHex(encryptedPayload, recipientPrivateKeyHex) {
241
- const privateKeyBytes = decodeHex(recipientPrivateKeyHex).buffer;
242
- const privateKey = await crypto.subtle.importKey(
243
- "raw",
244
- privateKeyBytes,
245
- {
246
- name: "X25519",
247
- namedCurve: "X25519"
248
- },
249
- false,
250
- ["deriveBits"]
251
- );
252
- return await decrypt(encryptedPayload, privateKey);
253
- }
254
- async function createAuthenticatedMessage(payload, signers) {
255
- const auth = await Promise.all(
256
- signers.map(async (signer) => {
257
- const signature = await sign(signer.privateKey, payload);
258
- return {
259
- pubkey: signer.publicKeyHex,
260
- signature
261
- };
262
- })
263
- );
264
- return {
265
- auth,
266
- payload
267
- };
268
- }
269
- async function createSignedEncryptedMessage(data, signers, recipientPublicKeyHex) {
270
- const encrypted = await encrypt(data, recipientPublicKeyHex);
271
- const auth = await Promise.all(
272
- signers.map(async (signer) => {
273
- const signature = await sign(signer.privateKey, encrypted);
274
- return {
275
- pubkey: signer.publicKeyHex,
276
- signature
277
- };
278
- })
279
- );
280
- return {
281
- auth,
282
- payload: encrypted
283
- };
284
- }
285
- async function verifyAndDecrypt(message, recipientPrivateKey) {
286
- const verificationResults = await Promise.all(
287
- message.auth.map(async (authEntry) => {
288
- const verified2 = await verify(
289
- authEntry.pubkey,
290
- authEntry.signature,
291
- message.payload
292
- );
293
- return { pubkey: authEntry.pubkey, verified: verified2 };
294
- })
295
- );
296
- const verified = verificationResults.every((r) => r.verified);
297
- const signers = verificationResults.filter((r) => r.verified).map((r) => r.pubkey);
298
- const data = await decrypt(message.payload, recipientPrivateKey);
299
- return {
300
- data,
301
- verified,
302
- signers
303
- };
304
- }
305
- function generateNonce(length = 12) {
306
- return crypto.getRandomValues(new Uint8Array(length));
307
- }
308
- function generateRandomData(size) {
309
- return crypto.getRandomValues(new Uint8Array(size));
310
- }
311
-
312
- export {
313
- generateSigningKeyPair,
314
- generateEncryptionKeyPair,
315
- sign,
316
- signWithHex,
317
- verify,
318
- encrypt,
319
- decrypt,
320
- decryptWithHex,
321
- createAuthenticatedMessage,
322
- createSignedEncryptedMessage,
323
- verifyAndDecrypt,
324
- generateNonce,
325
- generateRandomData,
326
- mod_exports
327
- };
@@ -1,111 +0,0 @@
1
- interface KeyPair {
2
- publicKey: CryptoKey;
3
- privateKey: CryptoKey;
4
- publicKeyHex: string;
5
- privateKeyHex: string;
6
- }
7
- interface EncryptionKeyPair {
8
- publicKey: CryptoKey;
9
- privateKey: CryptoKey;
10
- publicKeyHex: string;
11
- }
12
- interface EncryptedPayload {
13
- data: string;
14
- nonce: string;
15
- ephemeralPublicKey?: string;
16
- }
17
- interface AuthenticatedMessage<T = unknown> {
18
- auth: Array<{
19
- pubkey: string;
20
- signature: string;
21
- }>;
22
- payload: T;
23
- }
24
- interface SignedEncryptedMessage {
25
- auth: Array<{
26
- pubkey: string;
27
- signature: string;
28
- }>;
29
- payload: EncryptedPayload;
30
- }
31
- /**
32
- * Generate an Ed25519 keypair for signing
33
- */
34
- declare function generateSigningKeyPair(): Promise<KeyPair>;
35
- /**
36
- * Generate an X25519 keypair for encryption (ECDH)
37
- */
38
- declare function generateEncryptionKeyPair(): Promise<EncryptionKeyPair>;
39
- /**
40
- * Sign a payload with an Ed25519 private key
41
- */
42
- declare function sign<T>(privateKey: CryptoKey, payload: T): Promise<string>;
43
- /**
44
- * Sign a payload with an Ed25519 private key from hex
45
- */
46
- declare function signWithHex<T>(privateKeyHex: string, payload: T): Promise<string>;
47
- /**
48
- * Verify a signature using Ed25519 public key
49
- */
50
- declare function verify<T>(publicKeyHex: string, signatureHex: string, payload: T): Promise<boolean>;
51
- /**
52
- * Encrypt data using X25519 ECDH + AES-GCM
53
- * Uses ephemeral keypair for forward secrecy
54
- */
55
- declare function encrypt(data: unknown, recipientPublicKeyHex: string): Promise<EncryptedPayload>;
56
- /**
57
- * Decrypt data using X25519 ECDH + AES-GCM
58
- */
59
- declare function decrypt(encryptedPayload: EncryptedPayload, recipientPrivateKey: CryptoKey): Promise<unknown>;
60
- /**
61
- * Decrypt data using private key from hex
62
- */
63
- declare function decryptWithHex(encryptedPayload: EncryptedPayload, recipientPrivateKeyHex: string): Promise<unknown>;
64
- /**
65
- * Create an authenticated message (signed but not encrypted)
66
- */
67
- declare function createAuthenticatedMessage<T>(payload: T, signers: Array<{
68
- privateKey: CryptoKey;
69
- publicKeyHex: string;
70
- }>): Promise<AuthenticatedMessage<T>>;
71
- /**
72
- * Create a signed and encrypted message
73
- */
74
- declare function createSignedEncryptedMessage(data: unknown, signers: Array<{
75
- privateKey: CryptoKey;
76
- publicKeyHex: string;
77
- }>, recipientPublicKeyHex: string): Promise<SignedEncryptedMessage>;
78
- /**
79
- * Verify and decrypt a signed encrypted message
80
- */
81
- declare function verifyAndDecrypt(message: SignedEncryptedMessage, recipientPrivateKey: CryptoKey): Promise<{
82
- data: unknown;
83
- verified: boolean;
84
- signers: string[];
85
- }>;
86
- declare function generateNonce(length?: number): Uint8Array;
87
- declare function generateRandomData(size: number): Uint8Array;
88
-
89
- type mod_AuthenticatedMessage<T = unknown> = AuthenticatedMessage<T>;
90
- type mod_EncryptedPayload = EncryptedPayload;
91
- type mod_EncryptionKeyPair = EncryptionKeyPair;
92
- type mod_KeyPair = KeyPair;
93
- type mod_SignedEncryptedMessage = SignedEncryptedMessage;
94
- declare const mod_createAuthenticatedMessage: typeof createAuthenticatedMessage;
95
- declare const mod_createSignedEncryptedMessage: typeof createSignedEncryptedMessage;
96
- declare const mod_decrypt: typeof decrypt;
97
- declare const mod_decryptWithHex: typeof decryptWithHex;
98
- declare const mod_encrypt: typeof encrypt;
99
- declare const mod_generateEncryptionKeyPair: typeof generateEncryptionKeyPair;
100
- declare const mod_generateNonce: typeof generateNonce;
101
- declare const mod_generateRandomData: typeof generateRandomData;
102
- declare const mod_generateSigningKeyPair: typeof generateSigningKeyPair;
103
- declare const mod_sign: typeof sign;
104
- declare const mod_signWithHex: typeof signWithHex;
105
- declare const mod_verify: typeof verify;
106
- declare const mod_verifyAndDecrypt: typeof verifyAndDecrypt;
107
- declare namespace mod {
108
- export { type mod_AuthenticatedMessage as AuthenticatedMessage, type mod_EncryptedPayload as EncryptedPayload, type mod_EncryptionKeyPair as EncryptionKeyPair, type mod_KeyPair as KeyPair, type mod_SignedEncryptedMessage as SignedEncryptedMessage, mod_createAuthenticatedMessage as createAuthenticatedMessage, mod_createSignedEncryptedMessage as createSignedEncryptedMessage, mod_decrypt as decrypt, mod_decryptWithHex as decryptWithHex, mod_encrypt as encrypt, mod_generateEncryptionKeyPair as generateEncryptionKeyPair, mod_generateNonce as generateNonce, mod_generateRandomData as generateRandomData, mod_generateSigningKeyPair as generateSigningKeyPair, mod_sign as sign, mod_signWithHex as signWithHex, mod_verify as verify, mod_verifyAndDecrypt as verifyAndDecrypt };
109
- }
110
-
111
- export { type AuthenticatedMessage as A, type EncryptionKeyPair as E, type KeyPair as K, type SignedEncryptedMessage as S, type EncryptedPayload as a, generateEncryptionKeyPair as b, signWithHex as c, decrypt as d, encrypt as e, decryptWithHex as f, generateSigningKeyPair as g, createAuthenticatedMessage as h, createSignedEncryptedMessage as i, verifyAndDecrypt as j, generateNonce as k, generateRandomData as l, mod as m, sign as s, verify as v };