@branta-ops/branta 0.0.3 → 0.0.4

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/README.md CHANGED
@@ -72,7 +72,7 @@ await client.addPayment({
72
72
 
73
73
  - [X] Per Environment configuration
74
74
  - [X] V2 Get Payment by address
75
- - [ ] V2 Get Payment by QR Code
75
+ - [X] V2 Get Payment by QR Code
76
76
  - [X] V2 Get decrypted Zero Knowledge by address and secret
77
77
  - [X] V2 Add Payment
78
78
  - [X] V2 Payment by Parent Platform with HMAC
@@ -28,7 +28,10 @@ export declare class V2BrantaClient {
28
28
  getZKPayment(address: string, secret: string, options?: BrantaClientOptions | null): Promise<Payment[]>;
29
29
  addPayment(payment: Payment, options?: BrantaClientOptions | null): Promise<PaymentResult>;
30
30
  addZKPayment(payment: Payment, options?: BrantaClientOptions | null): Promise<ZKPaymentResult>;
31
+ getPaymentsByQRCode(qrText: string, options?: BrantaClientOptions | null): Promise<Payment[]>;
31
32
  isApiKeyValid(options?: BrantaClientOptions | null): Promise<boolean>;
33
+ private _resolveBaseUrl;
34
+ private _normalizeAddress;
32
35
  private _createClient;
33
36
  private _setApiKey;
34
37
  private _setHmacHeaders;
package/dist/v2/client.js CHANGED
@@ -49,12 +49,65 @@ export class V2BrantaClient {
49
49
  responsePayment.verifyLink = responsePayment.verifyLink.replace('verify', 'zk-verify') + "#secret=" + secret;
50
50
  return responsePayment;
51
51
  }
52
+ async getPaymentsByQRCode(qrText, options = null) {
53
+ const text = qrText.trim();
54
+ let url = null;
55
+ try {
56
+ url = new URL(text);
57
+ }
58
+ catch { /* not a URL */ }
59
+ if (url) {
60
+ const brantaId = url.searchParams.get('branta_id');
61
+ const brantaSecret = url.searchParams.get('branta_secret');
62
+ if (brantaId && brantaSecret) {
63
+ return this.getZKPayment(brantaId, brantaSecret, options);
64
+ }
65
+ if (url.protocol === 'http:' || url.protocol === 'https:') {
66
+ const baseUrl = this._resolveBaseUrl(options);
67
+ if (baseUrl && new URL(baseUrl).origin === url.origin) {
68
+ const segments = url.pathname.split('/').filter(Boolean);
69
+ const [version, type, id] = segments;
70
+ if (version === 'v2' && id) {
71
+ if (type === 'verify')
72
+ return this.getPayments(id, options);
73
+ if (type === 'zk-verify') {
74
+ const secret = new URLSearchParams(url.hash.slice(1)).get('secret');
75
+ return secret
76
+ ? this.getZKPayment(id, secret, options)
77
+ : this.getPayments(id, options);
78
+ }
79
+ }
80
+ const lastSegment = segments.at(-1);
81
+ if (lastSegment)
82
+ return this.getPayments(lastSegment, options);
83
+ }
84
+ }
85
+ }
86
+ return this.getPayments(this._normalizeAddress(text), options);
87
+ }
52
88
  async isApiKeyValid(options = null) {
53
89
  const httpClient = this._createClient(options);
54
90
  this._setApiKey(httpClient, options);
55
91
  const response = await httpClient.get("/v2/api-keys/health-check");
56
92
  return response.ok;
57
93
  }
94
+ _resolveBaseUrl(options) {
95
+ const baseUrl = options?.baseUrl ?? this._defaultOptions?.baseUrl;
96
+ return typeof baseUrl === 'string' ? baseUrl : baseUrl?.url ?? '';
97
+ }
98
+ _normalizeAddress(text) {
99
+ const lower = text.toLowerCase();
100
+ if (lower.startsWith('lightning:'))
101
+ return lower.slice('lightning:'.length);
102
+ if (lower.startsWith('bitcoin:')) {
103
+ const addr = text.slice('bitcoin:'.length);
104
+ const addrLower = addr.toLowerCase();
105
+ return addrLower.startsWith('bc1q') || addrLower.startsWith('bcrt') ? addrLower : addr;
106
+ }
107
+ if (lower.startsWith('lnbc') || lower.startsWith('bc1q'))
108
+ return lower;
109
+ return text;
110
+ }
58
111
  _createClient(options) {
59
112
  const baseUrl = options?.baseUrl ?? this._defaultOptions?.baseUrl;
60
113
  const timeout = options?.timeout ?? this._defaultOptions?.timeout ?? 10000;
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@branta-ops/branta",
3
- "version": "0.0.3",
3
+ "version": "0.0.4",
4
4
  "description": "A JavaScript SDK for the Branta API",
5
5
  "homepage": "https://github.com/BrantaOps/branta-js#readme",
6
6
  "bugs": {