@branta-ops/branta 0.0.3 → 0.0.5
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 +1 -1
- package/dist/v2/client.d.ts +5 -0
- package/dist/v2/client.js +70 -0
- package/package.json +1 -1
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
|
-
- [
|
|
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
|
package/dist/v2/client.d.ts
CHANGED
|
@@ -8,6 +8,7 @@ export interface Payment {
|
|
|
8
8
|
ttl?: number;
|
|
9
9
|
description?: string;
|
|
10
10
|
metadata?: Record<string, string>;
|
|
11
|
+
verify_url?: string;
|
|
11
12
|
}
|
|
12
13
|
interface PaymentResponse extends Payment {
|
|
13
14
|
createdAt: Date;
|
|
@@ -28,7 +29,11 @@ export declare class V2BrantaClient {
|
|
|
28
29
|
getZKPayment(address: string, secret: string, options?: BrantaClientOptions | null): Promise<Payment[]>;
|
|
29
30
|
addPayment(payment: Payment, options?: BrantaClientOptions | null): Promise<PaymentResult>;
|
|
30
31
|
addZKPayment(payment: Payment, options?: BrantaClientOptions | null): Promise<ZKPaymentResult>;
|
|
32
|
+
getPaymentsByQRCode(qrText: string, options?: BrantaClientOptions | null): Promise<Payment[]>;
|
|
31
33
|
isApiKeyValid(options?: BrantaClientOptions | null): Promise<boolean>;
|
|
34
|
+
private _buildVerifyUrl;
|
|
35
|
+
private _resolveBaseUrl;
|
|
36
|
+
private _normalizeAddress;
|
|
32
37
|
private _createClient;
|
|
33
38
|
private _setApiKey;
|
|
34
39
|
private _setHmacHeaders;
|
package/dist/v2/client.js
CHANGED
|
@@ -11,6 +11,10 @@ export class V2BrantaClient {
|
|
|
11
11
|
return [];
|
|
12
12
|
}
|
|
13
13
|
const data = await response.json();
|
|
14
|
+
const baseUrl = this._resolveBaseUrl(options);
|
|
15
|
+
for (const payment of data) {
|
|
16
|
+
payment.verify_url = this._buildVerifyUrl(baseUrl, address);
|
|
17
|
+
}
|
|
14
18
|
return data;
|
|
15
19
|
}
|
|
16
20
|
async getZKPayment(address, secret, options = null) {
|
|
@@ -22,6 +26,10 @@ export class V2BrantaClient {
|
|
|
22
26
|
destination.value = await AesEncryption.decrypt(destination.value, secret);
|
|
23
27
|
}
|
|
24
28
|
}
|
|
29
|
+
const baseUrl = this._resolveBaseUrl(options);
|
|
30
|
+
for (const payment of payments) {
|
|
31
|
+
payment.verify_url = this._buildVerifyUrl(baseUrl, address, secret);
|
|
32
|
+
}
|
|
25
33
|
return payments;
|
|
26
34
|
}
|
|
27
35
|
async addPayment(payment, options = null) {
|
|
@@ -34,6 +42,7 @@ export class V2BrantaClient {
|
|
|
34
42
|
}
|
|
35
43
|
const responseBody = await response.text();
|
|
36
44
|
const paymentResponse = JSON.parse(responseBody);
|
|
45
|
+
paymentResponse.verify_url = this._buildVerifyUrl(httpClient.baseURL, payment.destinations[0].value);
|
|
37
46
|
const verifyLink = httpClient.baseURL + "/v2/verify/" + encodeURIComponent(payment.destinations[0].value);
|
|
38
47
|
return { payment: paymentResponse, verifyLink };
|
|
39
48
|
}
|
|
@@ -47,14 +56,75 @@ export class V2BrantaClient {
|
|
|
47
56
|
const responsePayment = (await this.addPayment(payment, options));
|
|
48
57
|
responsePayment.secret = secret;
|
|
49
58
|
responsePayment.verifyLink = responsePayment.verifyLink.replace('verify', 'zk-verify') + "#secret=" + secret;
|
|
59
|
+
responsePayment.payment.verify_url = this._buildVerifyUrl(this._resolveBaseUrl(options), payment.destinations[0].value, secret);
|
|
50
60
|
return responsePayment;
|
|
51
61
|
}
|
|
62
|
+
async getPaymentsByQRCode(qrText, options = null) {
|
|
63
|
+
const text = qrText.trim();
|
|
64
|
+
let url = null;
|
|
65
|
+
try {
|
|
66
|
+
url = new URL(text);
|
|
67
|
+
}
|
|
68
|
+
catch { /* not a URL */ }
|
|
69
|
+
if (url) {
|
|
70
|
+
const brantaId = url.searchParams.get('branta_id');
|
|
71
|
+
const brantaSecret = url.searchParams.get('branta_secret');
|
|
72
|
+
if (brantaId && brantaSecret) {
|
|
73
|
+
return this.getZKPayment(brantaId, brantaSecret, options);
|
|
74
|
+
}
|
|
75
|
+
if (url.protocol === 'http:' || url.protocol === 'https:') {
|
|
76
|
+
const baseUrl = this._resolveBaseUrl(options);
|
|
77
|
+
if (baseUrl && new URL(baseUrl).origin === url.origin) {
|
|
78
|
+
const segments = url.pathname.split('/').filter(Boolean);
|
|
79
|
+
const [version, type, id] = segments;
|
|
80
|
+
if (version === 'v2' && id) {
|
|
81
|
+
if (type === 'verify')
|
|
82
|
+
return this.getPayments(id, options);
|
|
83
|
+
if (type === 'zk-verify') {
|
|
84
|
+
const secret = new URLSearchParams(url.hash.slice(1)).get('secret');
|
|
85
|
+
return secret
|
|
86
|
+
? this.getZKPayment(id, secret, options)
|
|
87
|
+
: this.getPayments(id, options);
|
|
88
|
+
}
|
|
89
|
+
}
|
|
90
|
+
const lastSegment = segments.at(-1);
|
|
91
|
+
if (lastSegment)
|
|
92
|
+
return this.getPayments(lastSegment, options);
|
|
93
|
+
}
|
|
94
|
+
}
|
|
95
|
+
}
|
|
96
|
+
return this.getPayments(this._normalizeAddress(text), options);
|
|
97
|
+
}
|
|
52
98
|
async isApiKeyValid(options = null) {
|
|
53
99
|
const httpClient = this._createClient(options);
|
|
54
100
|
this._setApiKey(httpClient, options);
|
|
55
101
|
const response = await httpClient.get("/v2/api-keys/health-check");
|
|
56
102
|
return response.ok;
|
|
57
103
|
}
|
|
104
|
+
_buildVerifyUrl(baseUrl, address, secret) {
|
|
105
|
+
const encoded = encodeURIComponent(address);
|
|
106
|
+
if (secret) {
|
|
107
|
+
return `${baseUrl}/v2/zk-verify/${encoded}#secret=${secret}`;
|
|
108
|
+
}
|
|
109
|
+
return `${baseUrl}/v2/verify/${encoded}`;
|
|
110
|
+
}
|
|
111
|
+
_resolveBaseUrl(options) {
|
|
112
|
+
const baseUrl = options?.baseUrl ?? this._defaultOptions?.baseUrl;
|
|
113
|
+
return typeof baseUrl === 'string' ? baseUrl : baseUrl?.url ?? '';
|
|
114
|
+
}
|
|
115
|
+
_normalizeAddress(text) {
|
|
116
|
+
const lower = text.toLowerCase();
|
|
117
|
+
if (lower.startsWith('lightning:'))
|
|
118
|
+
return lower.slice('lightning:'.length);
|
|
119
|
+
if (lower.startsWith('bitcoin:')) {
|
|
120
|
+
const addr = text.slice('bitcoin:'.length);
|
|
121
|
+
const addrLower = addr.toLowerCase();
|
|
122
|
+
return addrLower.startsWith('bc1q') || addrLower.startsWith('bcrt') ? addrLower : addr;
|
|
123
|
+
}
|
|
124
|
+
if (lower.startsWith('lnbc') || lower.startsWith('bc1q'))
|
|
125
|
+
return lower;
|
|
126
|
+
return text;
|
|
127
|
+
}
|
|
58
128
|
_createClient(options) {
|
|
59
129
|
const baseUrl = options?.baseUrl ?? this._defaultOptions?.baseUrl;
|
|
60
130
|
const timeout = options?.timeout ?? this._defaultOptions?.timeout ?? 10000;
|