@branta-ops/branta 0.0.9 → 1.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.
@@ -6,21 +6,26 @@ class AesEncryption {
6
6
  * @returns Base64-encoded encrypted data (iv + ciphertext + tag)
7
7
  */
8
8
  static async encrypt(value, secret) {
9
- const encoder = new TextEncoder();
10
- const secretData = encoder.encode(secret);
11
- const keyData = await crypto.subtle.digest('SHA-256', secretData);
12
- const iv = crypto.getRandomValues(new Uint8Array(12));
13
- const key = await crypto.subtle.importKey('raw', keyData, { name: 'AES-GCM', length: 256 }, false, ['encrypt']);
14
- const plaintext = encoder.encode(value);
15
- const encrypted = await crypto.subtle.encrypt({ name: 'AES-GCM', iv: iv, tagLength: 128 }, key, plaintext);
16
- const encryptedArray = new Uint8Array(encrypted);
17
- const ciphertext = encryptedArray.slice(0, -16);
18
- const tag = encryptedArray.slice(-16);
19
- const result = new Uint8Array(iv.length + ciphertext.length + tag.length);
20
- result.set(iv, 0);
21
- result.set(ciphertext, iv.length);
22
- result.set(tag, iv.length + ciphertext.length);
23
- return btoa(String.fromCharCode(...result));
9
+ try {
10
+ const encoder = new TextEncoder();
11
+ const secretData = encoder.encode(secret);
12
+ const keyData = await crypto.subtle.digest('SHA-256', secretData);
13
+ const iv = crypto.getRandomValues(new Uint8Array(12));
14
+ const key = await crypto.subtle.importKey('raw', keyData, { name: 'AES-GCM', length: 256 }, false, ['encrypt']);
15
+ const plaintext = encoder.encode(value);
16
+ const encrypted = await crypto.subtle.encrypt({ name: 'AES-GCM', iv: iv, tagLength: 128 }, key, plaintext);
17
+ const encryptedArray = new Uint8Array(encrypted);
18
+ const ciphertext = encryptedArray.slice(0, -16);
19
+ const tag = encryptedArray.slice(-16);
20
+ const result = new Uint8Array(iv.length + ciphertext.length + tag.length);
21
+ result.set(iv, 0);
22
+ result.set(ciphertext, iv.length);
23
+ result.set(tag, iv.length + ciphertext.length);
24
+ return btoa(String.fromCharCode(...result));
25
+ }
26
+ catch (e) {
27
+ throw new Error('Encryption failed: ' + e);
28
+ }
24
29
  }
25
30
  /**
26
31
  * Decrypts an encrypted string using AES-GCM with a secret key
@@ -29,20 +34,28 @@ class AesEncryption {
29
34
  * @returns The decrypted plaintext
30
35
  */
31
36
  static async decrypt(encryptedValue, secret) {
32
- const encryptedData = Uint8Array.from(atob(encryptedValue), c => c.charCodeAt(0));
33
- const encoder = new TextEncoder();
34
- const secretData = encoder.encode(secret);
35
- const keyData = await crypto.subtle.digest('SHA-256', secretData);
36
- const iv = encryptedData.slice(0, 12);
37
- const tag = encryptedData.slice(-16);
38
- const ciphertext = encryptedData.slice(12, -16);
39
- const ciphertextWithTag = new Uint8Array(ciphertext.length + tag.length);
40
- ciphertextWithTag.set(ciphertext, 0);
41
- ciphertextWithTag.set(tag, ciphertext.length);
42
- const key = await crypto.subtle.importKey('raw', keyData, { name: 'AES-GCM', length: 256 }, false, ['decrypt']);
43
- const decrypted = await crypto.subtle.decrypt({ name: 'AES-GCM', iv: iv, tagLength: 128 }, key, ciphertextWithTag);
44
- const decoder = new TextDecoder();
45
- return decoder.decode(decrypted);
37
+ try {
38
+ const encryptedData = Uint8Array.from(atob(encryptedValue), c => c.charCodeAt(0));
39
+ if (encryptedData.length < 28) {
40
+ throw new Error('Invalid encrypted data: too short');
41
+ }
42
+ const encoder = new TextEncoder();
43
+ const secretData = encoder.encode(secret);
44
+ const keyData = await crypto.subtle.digest('SHA-256', secretData);
45
+ const iv = encryptedData.slice(0, 12);
46
+ const tag = encryptedData.slice(-16);
47
+ const ciphertext = encryptedData.slice(12, -16);
48
+ const ciphertextWithTag = new Uint8Array(ciphertext.length + tag.length);
49
+ ciphertextWithTag.set(ciphertext, 0);
50
+ ciphertextWithTag.set(tag, ciphertext.length);
51
+ const key = await crypto.subtle.importKey('raw', keyData, { name: 'AES-GCM', length: 256 }, false, ['decrypt']);
52
+ const decrypted = await crypto.subtle.decrypt({ name: 'AES-GCM', iv: iv, tagLength: 128 }, key, ciphertextWithTag);
53
+ const decoder = new TextDecoder();
54
+ return decoder.decode(decrypted);
55
+ }
56
+ catch (e) {
57
+ throw new Error('Decryption failed: ' + e);
58
+ }
46
59
  }
47
60
  }
48
61
  export default AesEncryption;
package/dist/index.d.ts CHANGED
@@ -1,6 +1,6 @@
1
1
  import V2BrantaClient from "./v2/client.js";
2
- import { Payment, Destination } from "./v2/client.js";
2
+ import { Payment, Destination, DestinationType } from "./v2/client.js";
3
3
  import BrantaClientOptions from "./classes/brantaClientOptions.js";
4
4
  import BrantaServerBaseUrl from "./classes/brantaServerBaseUrl.js";
5
- export { V2BrantaClient, BrantaClientOptions, BrantaServerBaseUrl, Payment, Destination };
5
+ export { V2BrantaClient, BrantaClientOptions, BrantaServerBaseUrl, Payment, Destination, DestinationType };
6
6
  export default V2BrantaClient;
@@ -1,6 +1,8 @@
1
1
  import BrantaClientOptions from "../classes/brantaClientOptions.js";
2
+ export type DestinationType = 'bitcoin_address' | 'ln_address' | 'bolt11' | 'bolt12' | 'ln_url' | 'tether_address' | 'ark_address';
2
3
  export interface Destination {
3
4
  value: string;
5
+ type?: DestinationType;
4
6
  zk?: boolean;
5
7
  }
6
8
  export interface Payment {
@@ -8,12 +10,12 @@ export interface Payment {
8
10
  ttl?: number;
9
11
  description?: string;
10
12
  metadata?: Record<string, string>;
11
- verify_url?: string;
13
+ verifyUrl?: string;
14
+ platformLogoUrl?: string;
12
15
  }
13
16
  interface PaymentResponse extends Payment {
14
17
  createdAt: Date;
15
18
  platform: string;
16
- platformLogoUrl: string;
17
19
  }
18
20
  interface PaymentResult {
19
21
  payment: PaymentResponse;
package/dist/v2/client.js CHANGED
@@ -10,11 +10,16 @@ export class V2BrantaClient {
10
10
  if (!response.ok || response.headers.get("content-length") === "0") {
11
11
  return [];
12
12
  }
13
- const data = await response.json();
13
+ const raw = await response.json();
14
+ const data = raw.map(({ platform_logo_url: platformLogoUrl, verify_url: verifyUrl, ...rest }) => ({
15
+ ...rest,
16
+ platformLogoUrl,
17
+ verifyUrl,
18
+ }));
14
19
  const baseUrl = this._resolveBaseUrl(options);
15
20
  const baseOrigin = new URL(baseUrl).origin;
16
21
  for (const payment of data) {
17
- payment.verify_url = this._buildVerifyUrl(baseUrl, address);
22
+ payment.verifyUrl = this._buildVerifyUrl(baseUrl, address);
18
23
  if (payment.platformLogoUrl) {
19
24
  let valid = false;
20
25
  try {
@@ -38,7 +43,7 @@ export class V2BrantaClient {
38
43
  }
39
44
  const baseUrl = this._resolveBaseUrl(options);
40
45
  for (const payment of payments) {
41
- payment.verify_url = this._buildVerifyUrl(baseUrl, address, secret);
46
+ payment.verifyUrl = this._buildVerifyUrl(baseUrl, address, secret);
42
47
  }
43
48
  return payments;
44
49
  }
@@ -52,7 +57,7 @@ export class V2BrantaClient {
52
57
  }
53
58
  const responseBody = await response.text();
54
59
  const paymentResponse = JSON.parse(responseBody);
55
- paymentResponse.verify_url = this._buildVerifyUrl(httpClient.baseURL, payment.destinations[0].value);
60
+ paymentResponse.verifyUrl = this._buildVerifyUrl(httpClient.baseURL, payment.destinations[0].value);
56
61
  const verifyLink = httpClient.baseURL + "/v2/verify/" + encodeURIComponent(payment.destinations[0].value);
57
62
  return { payment: paymentResponse, verifyLink };
58
63
  }
@@ -66,7 +71,7 @@ export class V2BrantaClient {
66
71
  const responsePayment = (await this.addPayment(payment, options));
67
72
  responsePayment.secret = secret;
68
73
  responsePayment.verifyLink = responsePayment.verifyLink.replace('verify', 'zk-verify') + "#secret=" + secret;
69
- responsePayment.payment.verify_url = this._buildVerifyUrl(this._resolveBaseUrl(options), payment.destinations[0].value, secret);
74
+ responsePayment.payment.verifyUrl = this._buildVerifyUrl(this._resolveBaseUrl(options), payment.destinations[0].value, secret);
70
75
  return responsePayment;
71
76
  }
72
77
  async getPaymentsByQRCode(qrText, options = null) {
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@branta-ops/branta",
3
- "version": "0.0.9",
3
+ "version": "1.0.1",
4
4
  "description": "A JavaScript SDK for the Branta API",
5
5
  "homepage": "https://github.com/BrantaOps/branta-js#readme",
6
6
  "bugs": {