@1tokenfe/onetoken-webln-provider 2.2.46
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/LICENSE.md +51 -0
- package/README.md +1 -0
- package/dist/ProviderWebln.d.ts +36 -0
- package/dist/ProviderWebln.js +206 -0
- package/dist/ProviderWeblnBase.d.ts +8 -0
- package/dist/ProviderWeblnBase.js +12 -0
- package/dist/cjs/ProviderWebln.js +209 -0
- package/dist/cjs/ProviderWeblnBase.js +15 -0
- package/dist/cjs/index.js +18 -0
- package/dist/cjs/type-utils.js +2 -0
- package/dist/cjs/types.js +8 -0
- package/dist/index.d.ts +2 -0
- package/dist/index.js +2 -0
- package/dist/type-utils.d.ts +4 -0
- package/dist/type-utils.js +1 -0
- package/dist/types.d.ts +85 -0
- package/dist/types.js +7 -0
- package/package.json +36 -0
package/LICENSE.md
ADDED
|
@@ -0,0 +1,51 @@
|
|
|
1
|
+
# ONETOKEN STANDARD SOURCE LICENSE (O-SSL)
|
|
2
|
+
|
|
3
|
+
This license governs use of the accompanying software. If you use the software,
|
|
4
|
+
you accept this license. If you do not accept the license, do not use the
|
|
5
|
+
software.
|
|
6
|
+
|
|
7
|
+
## 1. Definitions
|
|
8
|
+
|
|
9
|
+
The terms "reproduce," "reproduction" and "distribution" have the same meaning
|
|
10
|
+
here as under Hong Kong copyright law.
|
|
11
|
+
|
|
12
|
+
"You" means the licensee of the software.
|
|
13
|
+
|
|
14
|
+
"Your company" means the company you worked for when you downloaded the
|
|
15
|
+
software.
|
|
16
|
+
|
|
17
|
+
"Reference use" means use of the software within your company as a reference,
|
|
18
|
+
in read only form, for the sole purposes of debugging your products,
|
|
19
|
+
maintaining your products, or enhancing the interoperability of your products
|
|
20
|
+
with the software, and specifically excludes the right to distribute the
|
|
21
|
+
software outside of your company.
|
|
22
|
+
|
|
23
|
+
"Licensed patents" means any Licensor patent claims which read directly on the
|
|
24
|
+
software as distributed by the Licensor under this license.
|
|
25
|
+
|
|
26
|
+
## 2. Grant of Rights
|
|
27
|
+
|
|
28
|
+
(A) Copyright Grant - Subject to the terms of this license, the Licensor grants
|
|
29
|
+
you a non-transferable, non-exclusive, worldwide, royalty-free copyright
|
|
30
|
+
license to reproduce the software for reference use.
|
|
31
|
+
|
|
32
|
+
(B) Patent Grant - Subject to the terms of this license, the Licensor grants
|
|
33
|
+
you a non-transferable, non-exclusive, worldwide, royalty-free patent license
|
|
34
|
+
under licensed patents for reference use.
|
|
35
|
+
|
|
36
|
+
## 3. Limitations
|
|
37
|
+
|
|
38
|
+
(A) No Trademark License - This license does not grant you any rights to use
|
|
39
|
+
the Licensor's name, logo, or trademarks.
|
|
40
|
+
|
|
41
|
+
(B) If you begin patent litigation against the Licensor over patents that you
|
|
42
|
+
think may apply to the software (including a cross-claim or counterclaim in
|
|
43
|
+
a lawsuit), your license to the software ends automatically.
|
|
44
|
+
|
|
45
|
+
(C) The software is licensed "as-is." You bear the risk of using it. The
|
|
46
|
+
Licensor gives no express warranties, guarantees or conditions. You may have
|
|
47
|
+
additional consumer rights under your local laws which this license cannot
|
|
48
|
+
change. To the extent permitted under your local laws, the Licensor excludes
|
|
49
|
+
the implied warranties of merchantability, fitness for a particular purpose and
|
|
50
|
+
non-infringement.This license agreement is governed by the laws of Hong Kong,
|
|
51
|
+
and any disputes related to this license agreement shall be resolved in accordance with Hong Kong law.
|
package/README.md
ADDED
|
@@ -0,0 +1 @@
|
|
|
1
|
+
# cross-inpage-provider
|
|
@@ -0,0 +1,36 @@
|
|
|
1
|
+
import { IInpageProviderConfig } from "@1tokenfe/cross-inpage-provider-core";
|
|
2
|
+
import { ProviderWeblnBase } from "./ProviderWeblnBase";
|
|
3
|
+
import { WeblnProviderEventsMap, GetInfoResponse, IProviderWebln, RequestInvoiceArgs, RequestInvoiceResponse } from "./types";
|
|
4
|
+
declare class ProviderWebln extends ProviderWeblnBase implements IProviderWebln {
|
|
5
|
+
private states;
|
|
6
|
+
constructor(props: IInpageProviderConfig);
|
|
7
|
+
setExecuting(executing: boolean): void;
|
|
8
|
+
private checkEnabled;
|
|
9
|
+
on<E extends keyof WeblnProviderEventsMap>(event: E, listener: WeblnProviderEventsMap[E]): this;
|
|
10
|
+
emit<E extends keyof WeblnProviderEventsMap>(event: E, ...args: Parameters<WeblnProviderEventsMap[E]>): boolean;
|
|
11
|
+
private _callBridge;
|
|
12
|
+
enable(): Promise<void>;
|
|
13
|
+
getInfo(): Promise<GetInfoResponse>;
|
|
14
|
+
makeInvoice(args: RequestInvoiceArgs): Promise<RequestInvoiceResponse>;
|
|
15
|
+
sendPayment(paymentRequest: string): Promise<{
|
|
16
|
+
preimage: string;
|
|
17
|
+
}>;
|
|
18
|
+
signMessage(message: string): Promise<{
|
|
19
|
+
message: string;
|
|
20
|
+
signature: string;
|
|
21
|
+
}>;
|
|
22
|
+
verifyMessage(signature: string, message: string): Promise<void>;
|
|
23
|
+
getBalance(): Promise<{
|
|
24
|
+
balance: number;
|
|
25
|
+
currency?: "sats" | "EUR" | "USD" | undefined;
|
|
26
|
+
}>;
|
|
27
|
+
lnurl(lnurlString: string): Promise<{
|
|
28
|
+
status: "OK";
|
|
29
|
+
data?: unknown;
|
|
30
|
+
} | {
|
|
31
|
+
status: "ERROR";
|
|
32
|
+
reason: string;
|
|
33
|
+
}>;
|
|
34
|
+
handlerLnurl(): void;
|
|
35
|
+
}
|
|
36
|
+
export { ProviderWebln };
|
|
@@ -0,0 +1,206 @@
|
|
|
1
|
+
var __awaiter = (this && this.__awaiter) || function (thisArg, _arguments, P, generator) {
|
|
2
|
+
function adopt(value) { return value instanceof P ? value : new P(function (resolve) { resolve(value); }); }
|
|
3
|
+
return new (P || (P = Promise))(function (resolve, reject) {
|
|
4
|
+
function fulfilled(value) { try { step(generator.next(value)); } catch (e) { reject(e); } }
|
|
5
|
+
function rejected(value) { try { step(generator["throw"](value)); } catch (e) { reject(e); } }
|
|
6
|
+
function step(result) { result.done ? resolve(result.value) : adopt(result.value).then(fulfilled, rejected); }
|
|
7
|
+
step((generator = generator.apply(thisArg, _arguments || [])).next());
|
|
8
|
+
});
|
|
9
|
+
};
|
|
10
|
+
import { ProviderWeblnBase } from "./ProviderWeblnBase";
|
|
11
|
+
class ProviderWebln extends ProviderWeblnBase {
|
|
12
|
+
constructor(props) {
|
|
13
|
+
super(props);
|
|
14
|
+
this.states = {
|
|
15
|
+
enabled: false,
|
|
16
|
+
executing: false
|
|
17
|
+
};
|
|
18
|
+
this.handlerLnurl();
|
|
19
|
+
}
|
|
20
|
+
setExecuting(executing) {
|
|
21
|
+
this.states.executing = executing;
|
|
22
|
+
}
|
|
23
|
+
checkEnabled(method) {
|
|
24
|
+
if (!this.states.enabled) {
|
|
25
|
+
const message = `Please allow the connection request of webln before calling the ${method} method`;
|
|
26
|
+
alert(message);
|
|
27
|
+
throw new Error(message);
|
|
28
|
+
}
|
|
29
|
+
if (this.states.executing) {
|
|
30
|
+
const message = `window.webln call already executing`;
|
|
31
|
+
alert(message);
|
|
32
|
+
throw new Error(message);
|
|
33
|
+
}
|
|
34
|
+
}
|
|
35
|
+
on(event, listener) {
|
|
36
|
+
return super.on(event, listener);
|
|
37
|
+
}
|
|
38
|
+
emit(event, ...args) {
|
|
39
|
+
return super.emit(event, ...args);
|
|
40
|
+
}
|
|
41
|
+
_callBridge(params) {
|
|
42
|
+
return this.bridgeRequest(params);
|
|
43
|
+
}
|
|
44
|
+
enable() {
|
|
45
|
+
return __awaiter(this, void 0, void 0, function* () {
|
|
46
|
+
if (this.states.enabled) {
|
|
47
|
+
return;
|
|
48
|
+
}
|
|
49
|
+
if (this.states.executing) {
|
|
50
|
+
const message = `window.webln call already executing`;
|
|
51
|
+
alert(message);
|
|
52
|
+
throw new Error(message);
|
|
53
|
+
}
|
|
54
|
+
try {
|
|
55
|
+
this.setExecuting(true);
|
|
56
|
+
const result = yield this._callBridge({ method: "enable" });
|
|
57
|
+
if (typeof result.enabled === "boolean") {
|
|
58
|
+
this.states.enabled = true;
|
|
59
|
+
}
|
|
60
|
+
}
|
|
61
|
+
finally {
|
|
62
|
+
this.setExecuting(false);
|
|
63
|
+
}
|
|
64
|
+
});
|
|
65
|
+
}
|
|
66
|
+
getInfo() {
|
|
67
|
+
return __awaiter(this, void 0, void 0, function* () {
|
|
68
|
+
this.checkEnabled('getInfo');
|
|
69
|
+
try {
|
|
70
|
+
this.setExecuting(true);
|
|
71
|
+
const response = yield this._callBridge({ method: "getInfo" });
|
|
72
|
+
return response;
|
|
73
|
+
}
|
|
74
|
+
finally {
|
|
75
|
+
this.setExecuting(false);
|
|
76
|
+
}
|
|
77
|
+
});
|
|
78
|
+
}
|
|
79
|
+
makeInvoice(args) {
|
|
80
|
+
return __awaiter(this, void 0, void 0, function* () {
|
|
81
|
+
this.checkEnabled('makeInvoice');
|
|
82
|
+
try {
|
|
83
|
+
this.setExecuting(true);
|
|
84
|
+
const response = yield this._callBridge({ method: "makeInvoice", params: args });
|
|
85
|
+
return response;
|
|
86
|
+
}
|
|
87
|
+
finally {
|
|
88
|
+
this.setExecuting(false);
|
|
89
|
+
}
|
|
90
|
+
});
|
|
91
|
+
}
|
|
92
|
+
sendPayment(paymentRequest) {
|
|
93
|
+
return __awaiter(this, void 0, void 0, function* () {
|
|
94
|
+
this.checkEnabled('sendPayment');
|
|
95
|
+
try {
|
|
96
|
+
this.setExecuting(true);
|
|
97
|
+
const response = yield this._callBridge({ method: "sendPayment", params: paymentRequest });
|
|
98
|
+
return response;
|
|
99
|
+
}
|
|
100
|
+
finally {
|
|
101
|
+
this.setExecuting(false);
|
|
102
|
+
}
|
|
103
|
+
});
|
|
104
|
+
}
|
|
105
|
+
signMessage(message) {
|
|
106
|
+
return __awaiter(this, void 0, void 0, function* () {
|
|
107
|
+
this.checkEnabled('signMessage');
|
|
108
|
+
try {
|
|
109
|
+
this.setExecuting(true);
|
|
110
|
+
const response = yield this._callBridge({ method: "signMessage", params: message });
|
|
111
|
+
return response;
|
|
112
|
+
}
|
|
113
|
+
finally {
|
|
114
|
+
this.setExecuting(false);
|
|
115
|
+
}
|
|
116
|
+
});
|
|
117
|
+
}
|
|
118
|
+
verifyMessage(signature, message) {
|
|
119
|
+
return __awaiter(this, void 0, void 0, function* () {
|
|
120
|
+
this.checkEnabled('verifyMessage');
|
|
121
|
+
try {
|
|
122
|
+
this.setExecuting(true);
|
|
123
|
+
const response = yield this._callBridge({
|
|
124
|
+
method: "verifyMessage",
|
|
125
|
+
params: { signature, message },
|
|
126
|
+
});
|
|
127
|
+
return response;
|
|
128
|
+
}
|
|
129
|
+
finally {
|
|
130
|
+
this.setExecuting(false);
|
|
131
|
+
}
|
|
132
|
+
});
|
|
133
|
+
}
|
|
134
|
+
getBalance() {
|
|
135
|
+
if (!this.states.enabled) {
|
|
136
|
+
throw new Error("Please allow the connection request of webln before calling the getBalance method");
|
|
137
|
+
}
|
|
138
|
+
return this._callBridge({ method: "getBalance", params: undefined });
|
|
139
|
+
}
|
|
140
|
+
lnurl(lnurlString) {
|
|
141
|
+
return __awaiter(this, void 0, void 0, function* () {
|
|
142
|
+
this.checkEnabled('lnurl');
|
|
143
|
+
try {
|
|
144
|
+
this.setExecuting(true);
|
|
145
|
+
const response = yield this._callBridge({ method: "lnurl", params: lnurlString });
|
|
146
|
+
return response;
|
|
147
|
+
}
|
|
148
|
+
finally {
|
|
149
|
+
this.setExecuting(false);
|
|
150
|
+
}
|
|
151
|
+
});
|
|
152
|
+
}
|
|
153
|
+
handlerLnurl() {
|
|
154
|
+
if (document) {
|
|
155
|
+
window.addEventListener("click", (ev) => {
|
|
156
|
+
var _a, _b, _c, _d, _e;
|
|
157
|
+
const target = ev.composedPath()[0];
|
|
158
|
+
if (!target || !target.closest) {
|
|
159
|
+
return;
|
|
160
|
+
}
|
|
161
|
+
const lightningLink = target.closest('[href^="lightning:" i]');
|
|
162
|
+
const lnurlLink = target.closest('[href^="lnurl" i]');
|
|
163
|
+
const bitcoinLinkWithLighting = target.closest('[href*="lightning=ln" i]');
|
|
164
|
+
let href;
|
|
165
|
+
let paymentRequest;
|
|
166
|
+
let lnurl;
|
|
167
|
+
if (!lightningLink && !bitcoinLinkWithLighting && !lnurlLink) {
|
|
168
|
+
return;
|
|
169
|
+
}
|
|
170
|
+
ev.preventDefault();
|
|
171
|
+
if (lightningLink) {
|
|
172
|
+
href = (_a = lightningLink.getAttribute("href")) === null || _a === void 0 ? void 0 : _a.toLowerCase();
|
|
173
|
+
paymentRequest = href === null || href === void 0 ? void 0 : href.replace("lightning:", "");
|
|
174
|
+
}
|
|
175
|
+
else if (bitcoinLinkWithLighting) {
|
|
176
|
+
href = (_b = bitcoinLinkWithLighting.getAttribute("href")) === null || _b === void 0 ? void 0 : _b.toLowerCase();
|
|
177
|
+
const url = new URL(href !== null && href !== void 0 ? href : "");
|
|
178
|
+
const query = new URLSearchParams(url.search);
|
|
179
|
+
paymentRequest = query.get("lightning");
|
|
180
|
+
}
|
|
181
|
+
else if (lnurlLink) {
|
|
182
|
+
href = (_c = lnurlLink.getAttribute("href")) === null || _c === void 0 ? void 0 : _c.toLowerCase();
|
|
183
|
+
lnurl = href === null || href === void 0 ? void 0 : href.replace(/^lnurl[pwc]:/i, "");
|
|
184
|
+
}
|
|
185
|
+
if (!paymentRequest && !lnurl) {
|
|
186
|
+
return;
|
|
187
|
+
}
|
|
188
|
+
if (paymentRequest && paymentRequest.startsWith("lnurl")) {
|
|
189
|
+
lnurl = paymentRequest.replace(/^lnurl[pwc]:/i, "");
|
|
190
|
+
}
|
|
191
|
+
if (paymentRequest && paymentRequest.match(/(\S+@\S+)/)) {
|
|
192
|
+
lnurl = (_e = (_d = paymentRequest.match) === null || _d === void 0 ? void 0 : _d.call(paymentRequest, /(\S+@\S+)/)) === null || _e === void 0 ? void 0 : _e[1];
|
|
193
|
+
}
|
|
194
|
+
window.webln.enable().then(() => {
|
|
195
|
+
if (lnurl) {
|
|
196
|
+
return window.webln.lnurl(lnurl);
|
|
197
|
+
}
|
|
198
|
+
if (paymentRequest) {
|
|
199
|
+
return window.webln.sendPayment(paymentRequest);
|
|
200
|
+
}
|
|
201
|
+
});
|
|
202
|
+
}, { capture: true });
|
|
203
|
+
}
|
|
204
|
+
}
|
|
205
|
+
}
|
|
206
|
+
export { ProviderWebln };
|
|
@@ -0,0 +1,8 @@
|
|
|
1
|
+
import { IInjectedProviderNames } from '@1tokenfe/cross-inpage-provider-types';
|
|
2
|
+
import { ProviderBase, IInpageProviderConfig } from '@1tokenfe/cross-inpage-provider-core';
|
|
3
|
+
declare class ProviderWeblnBase extends ProviderBase {
|
|
4
|
+
constructor(props: IInpageProviderConfig);
|
|
5
|
+
protected readonly providerName = IInjectedProviderNames.webln;
|
|
6
|
+
request(data: unknown): Promise<unknown>;
|
|
7
|
+
}
|
|
8
|
+
export { ProviderWeblnBase };
|
|
@@ -0,0 +1,12 @@
|
|
|
1
|
+
import { IInjectedProviderNames } from '@1tokenfe/cross-inpage-provider-types';
|
|
2
|
+
import { ProviderBase } from '@1tokenfe/cross-inpage-provider-core';
|
|
3
|
+
class ProviderWeblnBase extends ProviderBase {
|
|
4
|
+
constructor(props) {
|
|
5
|
+
super(props);
|
|
6
|
+
this.providerName = IInjectedProviderNames.webln;
|
|
7
|
+
}
|
|
8
|
+
request(data) {
|
|
9
|
+
return this.bridgeRequest(data);
|
|
10
|
+
}
|
|
11
|
+
}
|
|
12
|
+
export { ProviderWeblnBase };
|
|
@@ -0,0 +1,209 @@
|
|
|
1
|
+
"use strict";
|
|
2
|
+
var __awaiter = (this && this.__awaiter) || function (thisArg, _arguments, P, generator) {
|
|
3
|
+
function adopt(value) { return value instanceof P ? value : new P(function (resolve) { resolve(value); }); }
|
|
4
|
+
return new (P || (P = Promise))(function (resolve, reject) {
|
|
5
|
+
function fulfilled(value) { try { step(generator.next(value)); } catch (e) { reject(e); } }
|
|
6
|
+
function rejected(value) { try { step(generator["throw"](value)); } catch (e) { reject(e); } }
|
|
7
|
+
function step(result) { result.done ? resolve(result.value) : adopt(result.value).then(fulfilled, rejected); }
|
|
8
|
+
step((generator = generator.apply(thisArg, _arguments || [])).next());
|
|
9
|
+
});
|
|
10
|
+
};
|
|
11
|
+
Object.defineProperty(exports, "__esModule", { value: true });
|
|
12
|
+
exports.ProviderWebln = void 0;
|
|
13
|
+
const ProviderWeblnBase_1 = require("./ProviderWeblnBase");
|
|
14
|
+
class ProviderWebln extends ProviderWeblnBase_1.ProviderWeblnBase {
|
|
15
|
+
constructor(props) {
|
|
16
|
+
super(props);
|
|
17
|
+
this.states = {
|
|
18
|
+
enabled: false,
|
|
19
|
+
executing: false
|
|
20
|
+
};
|
|
21
|
+
this.handlerLnurl();
|
|
22
|
+
}
|
|
23
|
+
setExecuting(executing) {
|
|
24
|
+
this.states.executing = executing;
|
|
25
|
+
}
|
|
26
|
+
checkEnabled(method) {
|
|
27
|
+
if (!this.states.enabled) {
|
|
28
|
+
const message = `Please allow the connection request of webln before calling the ${method} method`;
|
|
29
|
+
alert(message);
|
|
30
|
+
throw new Error(message);
|
|
31
|
+
}
|
|
32
|
+
if (this.states.executing) {
|
|
33
|
+
const message = `window.webln call already executing`;
|
|
34
|
+
alert(message);
|
|
35
|
+
throw new Error(message);
|
|
36
|
+
}
|
|
37
|
+
}
|
|
38
|
+
on(event, listener) {
|
|
39
|
+
return super.on(event, listener);
|
|
40
|
+
}
|
|
41
|
+
emit(event, ...args) {
|
|
42
|
+
return super.emit(event, ...args);
|
|
43
|
+
}
|
|
44
|
+
_callBridge(params) {
|
|
45
|
+
return this.bridgeRequest(params);
|
|
46
|
+
}
|
|
47
|
+
enable() {
|
|
48
|
+
return __awaiter(this, void 0, void 0, function* () {
|
|
49
|
+
if (this.states.enabled) {
|
|
50
|
+
return;
|
|
51
|
+
}
|
|
52
|
+
if (this.states.executing) {
|
|
53
|
+
const message = `window.webln call already executing`;
|
|
54
|
+
alert(message);
|
|
55
|
+
throw new Error(message);
|
|
56
|
+
}
|
|
57
|
+
try {
|
|
58
|
+
this.setExecuting(true);
|
|
59
|
+
const result = yield this._callBridge({ method: "enable" });
|
|
60
|
+
if (typeof result.enabled === "boolean") {
|
|
61
|
+
this.states.enabled = true;
|
|
62
|
+
}
|
|
63
|
+
}
|
|
64
|
+
finally {
|
|
65
|
+
this.setExecuting(false);
|
|
66
|
+
}
|
|
67
|
+
});
|
|
68
|
+
}
|
|
69
|
+
getInfo() {
|
|
70
|
+
return __awaiter(this, void 0, void 0, function* () {
|
|
71
|
+
this.checkEnabled('getInfo');
|
|
72
|
+
try {
|
|
73
|
+
this.setExecuting(true);
|
|
74
|
+
const response = yield this._callBridge({ method: "getInfo" });
|
|
75
|
+
return response;
|
|
76
|
+
}
|
|
77
|
+
finally {
|
|
78
|
+
this.setExecuting(false);
|
|
79
|
+
}
|
|
80
|
+
});
|
|
81
|
+
}
|
|
82
|
+
makeInvoice(args) {
|
|
83
|
+
return __awaiter(this, void 0, void 0, function* () {
|
|
84
|
+
this.checkEnabled('makeInvoice');
|
|
85
|
+
try {
|
|
86
|
+
this.setExecuting(true);
|
|
87
|
+
const response = yield this._callBridge({ method: "makeInvoice", params: args });
|
|
88
|
+
return response;
|
|
89
|
+
}
|
|
90
|
+
finally {
|
|
91
|
+
this.setExecuting(false);
|
|
92
|
+
}
|
|
93
|
+
});
|
|
94
|
+
}
|
|
95
|
+
sendPayment(paymentRequest) {
|
|
96
|
+
return __awaiter(this, void 0, void 0, function* () {
|
|
97
|
+
this.checkEnabled('sendPayment');
|
|
98
|
+
try {
|
|
99
|
+
this.setExecuting(true);
|
|
100
|
+
const response = yield this._callBridge({ method: "sendPayment", params: paymentRequest });
|
|
101
|
+
return response;
|
|
102
|
+
}
|
|
103
|
+
finally {
|
|
104
|
+
this.setExecuting(false);
|
|
105
|
+
}
|
|
106
|
+
});
|
|
107
|
+
}
|
|
108
|
+
signMessage(message) {
|
|
109
|
+
return __awaiter(this, void 0, void 0, function* () {
|
|
110
|
+
this.checkEnabled('signMessage');
|
|
111
|
+
try {
|
|
112
|
+
this.setExecuting(true);
|
|
113
|
+
const response = yield this._callBridge({ method: "signMessage", params: message });
|
|
114
|
+
return response;
|
|
115
|
+
}
|
|
116
|
+
finally {
|
|
117
|
+
this.setExecuting(false);
|
|
118
|
+
}
|
|
119
|
+
});
|
|
120
|
+
}
|
|
121
|
+
verifyMessage(signature, message) {
|
|
122
|
+
return __awaiter(this, void 0, void 0, function* () {
|
|
123
|
+
this.checkEnabled('verifyMessage');
|
|
124
|
+
try {
|
|
125
|
+
this.setExecuting(true);
|
|
126
|
+
const response = yield this._callBridge({
|
|
127
|
+
method: "verifyMessage",
|
|
128
|
+
params: { signature, message },
|
|
129
|
+
});
|
|
130
|
+
return response;
|
|
131
|
+
}
|
|
132
|
+
finally {
|
|
133
|
+
this.setExecuting(false);
|
|
134
|
+
}
|
|
135
|
+
});
|
|
136
|
+
}
|
|
137
|
+
getBalance() {
|
|
138
|
+
if (!this.states.enabled) {
|
|
139
|
+
throw new Error("Please allow the connection request of webln before calling the getBalance method");
|
|
140
|
+
}
|
|
141
|
+
return this._callBridge({ method: "getBalance", params: undefined });
|
|
142
|
+
}
|
|
143
|
+
lnurl(lnurlString) {
|
|
144
|
+
return __awaiter(this, void 0, void 0, function* () {
|
|
145
|
+
this.checkEnabled('lnurl');
|
|
146
|
+
try {
|
|
147
|
+
this.setExecuting(true);
|
|
148
|
+
const response = yield this._callBridge({ method: "lnurl", params: lnurlString });
|
|
149
|
+
return response;
|
|
150
|
+
}
|
|
151
|
+
finally {
|
|
152
|
+
this.setExecuting(false);
|
|
153
|
+
}
|
|
154
|
+
});
|
|
155
|
+
}
|
|
156
|
+
handlerLnurl() {
|
|
157
|
+
if (document) {
|
|
158
|
+
window.addEventListener("click", (ev) => {
|
|
159
|
+
var _a, _b, _c, _d, _e;
|
|
160
|
+
const target = ev.composedPath()[0];
|
|
161
|
+
if (!target || !target.closest) {
|
|
162
|
+
return;
|
|
163
|
+
}
|
|
164
|
+
const lightningLink = target.closest('[href^="lightning:" i]');
|
|
165
|
+
const lnurlLink = target.closest('[href^="lnurl" i]');
|
|
166
|
+
const bitcoinLinkWithLighting = target.closest('[href*="lightning=ln" i]');
|
|
167
|
+
let href;
|
|
168
|
+
let paymentRequest;
|
|
169
|
+
let lnurl;
|
|
170
|
+
if (!lightningLink && !bitcoinLinkWithLighting && !lnurlLink) {
|
|
171
|
+
return;
|
|
172
|
+
}
|
|
173
|
+
ev.preventDefault();
|
|
174
|
+
if (lightningLink) {
|
|
175
|
+
href = (_a = lightningLink.getAttribute("href")) === null || _a === void 0 ? void 0 : _a.toLowerCase();
|
|
176
|
+
paymentRequest = href === null || href === void 0 ? void 0 : href.replace("lightning:", "");
|
|
177
|
+
}
|
|
178
|
+
else if (bitcoinLinkWithLighting) {
|
|
179
|
+
href = (_b = bitcoinLinkWithLighting.getAttribute("href")) === null || _b === void 0 ? void 0 : _b.toLowerCase();
|
|
180
|
+
const url = new URL(href !== null && href !== void 0 ? href : "");
|
|
181
|
+
const query = new URLSearchParams(url.search);
|
|
182
|
+
paymentRequest = query.get("lightning");
|
|
183
|
+
}
|
|
184
|
+
else if (lnurlLink) {
|
|
185
|
+
href = (_c = lnurlLink.getAttribute("href")) === null || _c === void 0 ? void 0 : _c.toLowerCase();
|
|
186
|
+
lnurl = href === null || href === void 0 ? void 0 : href.replace(/^lnurl[pwc]:/i, "");
|
|
187
|
+
}
|
|
188
|
+
if (!paymentRequest && !lnurl) {
|
|
189
|
+
return;
|
|
190
|
+
}
|
|
191
|
+
if (paymentRequest && paymentRequest.startsWith("lnurl")) {
|
|
192
|
+
lnurl = paymentRequest.replace(/^lnurl[pwc]:/i, "");
|
|
193
|
+
}
|
|
194
|
+
if (paymentRequest && paymentRequest.match(/(\S+@\S+)/)) {
|
|
195
|
+
lnurl = (_e = (_d = paymentRequest.match) === null || _d === void 0 ? void 0 : _d.call(paymentRequest, /(\S+@\S+)/)) === null || _e === void 0 ? void 0 : _e[1];
|
|
196
|
+
}
|
|
197
|
+
window.webln.enable().then(() => {
|
|
198
|
+
if (lnurl) {
|
|
199
|
+
return window.webln.lnurl(lnurl);
|
|
200
|
+
}
|
|
201
|
+
if (paymentRequest) {
|
|
202
|
+
return window.webln.sendPayment(paymentRequest);
|
|
203
|
+
}
|
|
204
|
+
});
|
|
205
|
+
}, { capture: true });
|
|
206
|
+
}
|
|
207
|
+
}
|
|
208
|
+
}
|
|
209
|
+
exports.ProviderWebln = ProviderWebln;
|
|
@@ -0,0 +1,15 @@
|
|
|
1
|
+
"use strict";
|
|
2
|
+
Object.defineProperty(exports, "__esModule", { value: true });
|
|
3
|
+
exports.ProviderWeblnBase = void 0;
|
|
4
|
+
const cross_inpage_provider_types_1 = require("@1tokenfe/cross-inpage-provider-types");
|
|
5
|
+
const cross_inpage_provider_core_1 = require("@1tokenfe/cross-inpage-provider-core");
|
|
6
|
+
class ProviderWeblnBase extends cross_inpage_provider_core_1.ProviderBase {
|
|
7
|
+
constructor(props) {
|
|
8
|
+
super(props);
|
|
9
|
+
this.providerName = cross_inpage_provider_types_1.IInjectedProviderNames.webln;
|
|
10
|
+
}
|
|
11
|
+
request(data) {
|
|
12
|
+
return this.bridgeRequest(data);
|
|
13
|
+
}
|
|
14
|
+
}
|
|
15
|
+
exports.ProviderWeblnBase = ProviderWeblnBase;
|
|
@@ -0,0 +1,18 @@
|
|
|
1
|
+
"use strict";
|
|
2
|
+
var __createBinding = (this && this.__createBinding) || (Object.create ? (function(o, m, k, k2) {
|
|
3
|
+
if (k2 === undefined) k2 = k;
|
|
4
|
+
var desc = Object.getOwnPropertyDescriptor(m, k);
|
|
5
|
+
if (!desc || ("get" in desc ? !m.__esModule : desc.writable || desc.configurable)) {
|
|
6
|
+
desc = { enumerable: true, get: function() { return m[k]; } };
|
|
7
|
+
}
|
|
8
|
+
Object.defineProperty(o, k2, desc);
|
|
9
|
+
}) : (function(o, m, k, k2) {
|
|
10
|
+
if (k2 === undefined) k2 = k;
|
|
11
|
+
o[k2] = m[k];
|
|
12
|
+
}));
|
|
13
|
+
var __exportStar = (this && this.__exportStar) || function(m, exports) {
|
|
14
|
+
for (var p in m) if (p !== "default" && !Object.prototype.hasOwnProperty.call(exports, p)) __createBinding(exports, m, p);
|
|
15
|
+
};
|
|
16
|
+
Object.defineProperty(exports, "__esModule", { value: true });
|
|
17
|
+
__exportStar(require("./ProviderWeblnBase"), exports);
|
|
18
|
+
__exportStar(require("./ProviderWebln"), exports);
|
package/dist/index.d.ts
ADDED
package/dist/index.js
ADDED
|
@@ -0,0 +1 @@
|
|
|
1
|
+
export {};
|
package/dist/types.d.ts
ADDED
|
@@ -0,0 +1,85 @@
|
|
|
1
|
+
import { IJsonRpcRequest } from '@1tokenfe/cross-inpage-provider-types';
|
|
2
|
+
import { ProviderWeblnBase } from './ProviderWeblnBase';
|
|
3
|
+
import type * as TypeUtils from './type-utils';
|
|
4
|
+
export interface RequestArguments {
|
|
5
|
+
id?: number | string;
|
|
6
|
+
method: string;
|
|
7
|
+
params?: unknown[] | Record<string, unknown>;
|
|
8
|
+
}
|
|
9
|
+
export interface EnableResponse {
|
|
10
|
+
enabled: boolean;
|
|
11
|
+
}
|
|
12
|
+
export interface GetInfoResponse {
|
|
13
|
+
node: {
|
|
14
|
+
alias: string;
|
|
15
|
+
pubkey: string;
|
|
16
|
+
color?: string;
|
|
17
|
+
};
|
|
18
|
+
methods: string[];
|
|
19
|
+
}
|
|
20
|
+
export interface RequestInvoiceArgs {
|
|
21
|
+
amount?: string | number;
|
|
22
|
+
defaultAmount?: string | number;
|
|
23
|
+
minimumAmount?: string | number;
|
|
24
|
+
maximumAmount?: string | number;
|
|
25
|
+
defaultMemo?: string;
|
|
26
|
+
}
|
|
27
|
+
interface SendPaymentResponse {
|
|
28
|
+
preimage: string;
|
|
29
|
+
}
|
|
30
|
+
export interface RequestInvoiceResponse {
|
|
31
|
+
paymentRequest: string;
|
|
32
|
+
paymentHash: string;
|
|
33
|
+
rHash: string;
|
|
34
|
+
}
|
|
35
|
+
export interface SignMessageResponse {
|
|
36
|
+
message: string;
|
|
37
|
+
signature: string;
|
|
38
|
+
}
|
|
39
|
+
export interface VerifyMessageArgs {
|
|
40
|
+
signature: string;
|
|
41
|
+
message: string;
|
|
42
|
+
}
|
|
43
|
+
export type LNURLResponse = {
|
|
44
|
+
status: "OK";
|
|
45
|
+
data?: unknown;
|
|
46
|
+
} | {
|
|
47
|
+
status: "ERROR";
|
|
48
|
+
reason: string;
|
|
49
|
+
};
|
|
50
|
+
export type BalanceResponse = {
|
|
51
|
+
balance: number;
|
|
52
|
+
currency?: "sats" | "EUR" | "USD";
|
|
53
|
+
};
|
|
54
|
+
export type IProviderWebln = ProviderWeblnBase & Omit<WeblnRequeset, 'enable' | 'verifyMessage'> & {
|
|
55
|
+
enable: () => Promise<void>;
|
|
56
|
+
verifyMessage: (signature: string, message: string) => Promise<void>;
|
|
57
|
+
};
|
|
58
|
+
export type WeblnRequeset = {
|
|
59
|
+
enable: () => Promise<EnableResponse>;
|
|
60
|
+
getInfo: () => Promise<GetInfoResponse>;
|
|
61
|
+
makeInvoice: (args: RequestInvoiceArgs) => Promise<RequestInvoiceResponse>;
|
|
62
|
+
sendPayment: (paymentRequest: string) => Promise<SendPaymentResponse>;
|
|
63
|
+
signMessage: (message: string) => Promise<SignMessageResponse>;
|
|
64
|
+
verifyMessage: (args: VerifyMessageArgs) => Promise<void>;
|
|
65
|
+
lnurl: (lnurl: string) => Promise<LNURLResponse>;
|
|
66
|
+
getBalance: () => Promise<BalanceResponse>;
|
|
67
|
+
};
|
|
68
|
+
export type JsBridgeRequest = {
|
|
69
|
+
[K in keyof WeblnRequeset]: (params: Parameters<WeblnRequeset[K]>[0]) => Promise<TypeUtils.WireStringified<TypeUtils.ResolvePromise<ReturnType<WeblnRequeset[K]>>>>;
|
|
70
|
+
};
|
|
71
|
+
export type JsBridgeRequestParams<T extends keyof JsBridgeRequest> = Parameters<JsBridgeRequest[T]>[0];
|
|
72
|
+
export type JsBridgeRequestResponse<T extends keyof JsBridgeRequest> = ReturnType<JsBridgeRequest[T]>;
|
|
73
|
+
declare const PROVIDER_EVENTS: {
|
|
74
|
+
readonly connect: "connect";
|
|
75
|
+
readonly disconnect: "disconnect";
|
|
76
|
+
readonly accountChanged: "accountChanged";
|
|
77
|
+
readonly message_low_level: "message_low_level";
|
|
78
|
+
};
|
|
79
|
+
export type WeblnProviderEventsMap = {
|
|
80
|
+
[PROVIDER_EVENTS.connect]: (account: string) => void;
|
|
81
|
+
[PROVIDER_EVENTS.disconnect]: () => void;
|
|
82
|
+
[PROVIDER_EVENTS.accountChanged]: (account: string | null) => void;
|
|
83
|
+
[PROVIDER_EVENTS.message_low_level]: (payload: IJsonRpcRequest) => void;
|
|
84
|
+
};
|
|
85
|
+
export {};
|
package/dist/types.js
ADDED
package/package.json
ADDED
|
@@ -0,0 +1,36 @@
|
|
|
1
|
+
{
|
|
2
|
+
"name": "@1tokenfe/onetoken-webln-provider",
|
|
3
|
+
"version": "2.2.46",
|
|
4
|
+
"keywords": [
|
|
5
|
+
"cross-inpage-provider"
|
|
6
|
+
],
|
|
7
|
+
"author": "dev-fe@onetoken.so",
|
|
8
|
+
"repository": "https://github.com/OneTokenHQ/cross-inpage-provider",
|
|
9
|
+
"license": "Apache-2.0",
|
|
10
|
+
"publishConfig": {
|
|
11
|
+
"access": "public"
|
|
12
|
+
},
|
|
13
|
+
"files": [
|
|
14
|
+
"dist/*"
|
|
15
|
+
],
|
|
16
|
+
"exports": {
|
|
17
|
+
"types": "./dist/index.d.ts",
|
|
18
|
+
"import": "./dist/index.js",
|
|
19
|
+
"require": "./dist/cjs/index.js"
|
|
20
|
+
},
|
|
21
|
+
"types": "./dist/index.d.ts",
|
|
22
|
+
"module": "./dist/index.js",
|
|
23
|
+
"main": "./dist/cjs/index.js",
|
|
24
|
+
"scripts": {
|
|
25
|
+
"prebuild": "rm -rf dist",
|
|
26
|
+
"build": "tsc && tsc --project tsconfig.cjs.json",
|
|
27
|
+
"start": "tsc --watch"
|
|
28
|
+
},
|
|
29
|
+
"dependencies": {
|
|
30
|
+
"@1tokenfe/cross-inpage-provider-core": "2.2.46",
|
|
31
|
+
"@1tokenfe/cross-inpage-provider-errors": "2.2.46",
|
|
32
|
+
"@1tokenfe/cross-inpage-provider-types": "2.2.46",
|
|
33
|
+
"@1tokenfe/extension-bridge-injected": "2.2.46"
|
|
34
|
+
},
|
|
35
|
+
"gitHead": "dadd71e7e07ab7aaf87961aecc01255586848e16"
|
|
36
|
+
}
|