Skip to content

Commit ae112e1

Browse files
authored
Merge pull request #433 from getAlby/connectors-typescript
Typed connectors
2 parents 791a3a4 + 4153c67 commit ae112e1

File tree

9 files changed

+305
-127
lines changed

9 files changed

+305
-127
lines changed

src/extension/background-script/connectors/base.js

-11
This file was deleted.
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,21 @@
1+
interface Config {
2+
adminkey: string;
3+
login?: string;
4+
macaroon?: string;
5+
password?: string;
6+
url?: string;
7+
}
8+
9+
class Base {
10+
config: Config;
11+
12+
constructor(config: Config) {
13+
this.config = config;
14+
}
15+
16+
init() {
17+
return Promise.resolve();
18+
}
19+
}
20+
21+
export default Base;
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,41 @@
1+
import Base from "./base";
2+
import Connector, {
3+
SendPaymentArgs,
4+
SendPaymentResponse,
5+
GetInfoResponse,
6+
GetBalanceResponse,
7+
MakeInvoiceArgs,
8+
MakeInvoiceResponse,
9+
SignMessageArgs,
10+
SignMessageResponse,
11+
VerifyMessageArgs,
12+
VerifyMessageResponse,
13+
} from "./connector.interface";
14+
15+
class ConnectorExample extends Base implements Connector {
16+
getInfo(): Promise<GetInfoResponse> {
17+
// Add your own implementation.
18+
}
19+
20+
getBalance(): Promise<GetBalanceResponse> {
21+
// Add your own implementation.
22+
}
23+
24+
sendPayment(args: SendPaymentArgs): Promise<SendPaymentResponse> {
25+
// Add your own implementation.
26+
}
27+
28+
signMessage(args: SignMessageArgs): Promise<SignMessageResponse> {
29+
// Add your own implementation.
30+
}
31+
32+
verifyMessage(args: VerifyMessageArgs): Promise<VerifyMessageResponse> {
33+
// Add your own implementation.
34+
}
35+
36+
makeInvoice(args: MakeInvoiceArgs): Promise<MakeInvoiceResponse> {
37+
// Add your own implementation.
38+
}
39+
}
40+
41+
export default ConnectorExample;
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,76 @@
1+
interface WebLNNode {
2+
alias: string;
3+
pubkey?: string;
4+
color?: string;
5+
}
6+
7+
interface Route {
8+
total_amt: number;
9+
total_fees: number;
10+
}
11+
12+
export interface MakeInvoiceArgs {
13+
amount: number;
14+
memo: string;
15+
}
16+
17+
export interface MakeInvoiceResponse {
18+
data: {
19+
paymentRequest: string;
20+
rHash: string;
21+
};
22+
}
23+
24+
export interface GetInfoResponse {
25+
data: WebLNNode;
26+
}
27+
28+
export interface GetBalanceResponse {
29+
data: {
30+
balance: number;
31+
};
32+
}
33+
34+
export type SendPaymentResponse =
35+
| {
36+
data: {
37+
preimage: string;
38+
paymentHash: string;
39+
route: Route;
40+
};
41+
}
42+
| { error: string };
43+
44+
export interface SendPaymentArgs {
45+
paymentRequest: string;
46+
}
47+
48+
export interface SignMessageArgs {
49+
message: string;
50+
}
51+
52+
export interface SignMessageResponse {
53+
data: {
54+
signature: string;
55+
};
56+
}
57+
58+
export interface VerifyMessageArgs {
59+
message: string;
60+
signature: string;
61+
}
62+
63+
export interface VerifyMessageResponse {
64+
data: {
65+
valid: boolean;
66+
};
67+
}
68+
69+
export default interface Connector {
70+
getInfo(): Promise<GetInfoResponse>;
71+
getBalance(): Promise<GetBalanceResponse>;
72+
makeInvoice(args: MakeInvoiceArgs): Promise<MakeInvoiceResponse>;
73+
sendPayment(args: SendPaymentArgs): Promise<SendPaymentResponse>;
74+
signMessage(args: SignMessageArgs): Promise<SignMessageResponse>;
75+
verifyMessage(args: VerifyMessageArgs): Promise<VerifyMessageResponse>;
76+
}

src/extension/background-script/connectors/index.ts

-8
Original file line numberDiff line numberDiff line change
@@ -1,17 +1,9 @@
11
// eslint-disable-next-line @typescript-eslint/ban-ts-comment
22
// @ts-ignore: implicitly has 'any' type error
33
import Native from "./native";
4-
// eslint-disable-next-line @typescript-eslint/ban-ts-comment
5-
// @ts-ignore: implicitly has 'any' type error
64
import Lnd from "./lnd";
7-
// eslint-disable-next-line @typescript-eslint/ban-ts-comment
8-
// @ts-ignore: implicitly has 'any' type error
95
import LndHub from "./lndhub";
10-
// eslint-disable-next-line @typescript-eslint/ban-ts-comment
11-
// @ts-ignore: implicitly has 'any' type error
126
import LnBits from "./lnbits";
13-
// eslint-disable-next-line @typescript-eslint/ban-ts-comment
14-
// @ts-ignore: implicitly has 'any' type error
157
import Base from "./base";
168

179
/*

src/extension/background-script/connectors/lnbits.js renamed to src/extension/background-script/connectors/lnbits.ts

+28-10
Original file line numberDiff line numberDiff line change
@@ -4,9 +4,21 @@ import { parsePaymentRequest } from "invoices";
44
import Base from "./base";
55
import utils from "../../../common/lib/utils";
66
import HashKeySigner from "../../../common/utils/signer";
7+
import Connector, {
8+
SendPaymentArgs,
9+
SendPaymentResponse,
10+
GetInfoResponse,
11+
GetBalanceResponse,
12+
MakeInvoiceArgs,
13+
MakeInvoiceResponse,
14+
SignMessageArgs,
15+
SignMessageResponse,
16+
VerifyMessageArgs,
17+
VerifyMessageResponse,
18+
} from "./connector.interface";
719

8-
class LnBits extends Base {
9-
getInfo() {
20+
class LnBits extends Base implements Connector {
21+
getInfo(): Promise<GetInfoResponse> {
1022
return this.request(
1123
"GET",
1224
"/api/v1/wallet",
@@ -22,7 +34,7 @@ class LnBits extends Base {
2234
});
2335
}
2436

25-
getBalance() {
37+
getBalance(): Promise<GetBalanceResponse> {
2638
return this.request(
2739
"GET",
2840
"/api/v1/wallet",
@@ -40,11 +52,11 @@ class LnBits extends Base {
4052
});
4153
}
4254

43-
sendPayment(args) {
55+
sendPayment(args: SendPaymentArgs): Promise<SendPaymentResponse> {
4456
const paymentRequestDetails = parsePaymentRequest({
4557
request: args.paymentRequest,
4658
});
47-
const amountInSats = parseInt(paymentRequestDetails.tokens);
59+
const amountInSats = paymentRequestDetails.tokens;
4860
return this.request("POST", "/api/v1/payments", this.config.adminkey, {
4961
bolt11: args.paymentRequest,
5062
out: true,
@@ -66,15 +78,15 @@ class LnBits extends Base {
6678
});
6779
}
6880

69-
checkPayment(paymentHash) {
81+
checkPayment(paymentHash: string) {
7082
return this.request(
7183
"GET",
7284
`/api/v1/payments/${paymentHash}`,
7385
this.config.adminkey
7486
);
7587
}
7688

77-
signMessage(args) {
89+
signMessage(args: SignMessageArgs): Promise<SignMessageResponse> {
7890
// make sure we got the config to create a new key
7991
if (!this.config.url || !this.config.adminkey) {
8092
return Promise.reject(new Error("Missing config"));
@@ -104,7 +116,7 @@ class LnBits extends Base {
104116
});
105117
}
106118

107-
verifyMessage(args) {
119+
verifyMessage(args: VerifyMessageArgs): Promise<VerifyMessageResponse> {
108120
// create a signing key from the lnbits URL and the adminkey
109121
const keyHex = sha256(
110122
`LBE-LNBITS-${this.config.url}-${this.config.adminkey}`
@@ -121,7 +133,7 @@ class LnBits extends Base {
121133
});
122134
}
123135

124-
makeInvoice(args) {
136+
makeInvoice(args: MakeInvoiceArgs): Promise<MakeInvoiceResponse> {
125137
return this.request("POST", "/api/v1/payments", this.config.adminkey, {
126138
amount: args.amount,
127139
memo: args.memo,
@@ -136,7 +148,13 @@ class LnBits extends Base {
136148
});
137149
}
138150

139-
async request(method, path, apiKey, args, defaultValues) {
151+
async request(
152+
method: string,
153+
path: string,
154+
apiKey: string,
155+
args?: any,
156+
defaultValues?: any
157+
) {
140158
let body = null;
141159
let query = "";
142160
const headers = new Headers();

src/extension/background-script/connectors/lnd.js renamed to src/extension/background-script/connectors/lnd.ts

+22-13
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,21 @@
11
import Base64 from "crypto-js/enc-base64";
22
import UTF8 from "crypto-js/enc-utf8";
33
import Base from "./base";
4+
import Connector, {
5+
SendPaymentArgs,
6+
SendPaymentResponse,
7+
GetInfoResponse,
8+
GetBalanceResponse,
9+
MakeInvoiceArgs,
10+
MakeInvoiceResponse,
11+
SignMessageArgs,
12+
SignMessageResponse,
13+
VerifyMessageArgs,
14+
VerifyMessageResponse,
15+
} from "./connector.interface";
416

5-
class Lnd extends Base {
6-
getInfo() {
17+
class Lnd extends Base implements Connector {
18+
getInfo(): Promise<GetInfoResponse> {
719
return this.request("GET", "/v1/getinfo", undefined, {}).then((res) => {
820
return {
921
data: {
@@ -15,11 +27,11 @@ class Lnd extends Base {
1527
});
1628
}
1729

18-
getBalance() {
30+
getBalance(): Promise<GetBalanceResponse> {
1931
return this.getChannelsBalance();
2032
}
2133

22-
sendPayment(args) {
34+
sendPayment(args: SendPaymentArgs): Promise<SendPaymentResponse> {
2335
return this.request(
2436
"POST",
2537
"/v1/channels/transactions",
@@ -41,7 +53,7 @@ class Lnd extends Base {
4153
});
4254
}
4355

44-
signMessage(args) {
56+
signMessage(args: SignMessageArgs): Promise<SignMessageResponse> {
4557
// use v2 to use the key locator (key_loc)
4658
// return this.request("POST", "/v2/signer/signmessage", {
4759
return this.request("POST", "/v1/signmessage", {
@@ -56,7 +68,7 @@ class Lnd extends Base {
5668
});
5769
}
5870

59-
verifyMessage(args) {
71+
verifyMessage(args: VerifyMessageArgs): Promise<VerifyMessageResponse> {
6072
return this.request("POST", "/v1/verifymessage", {
6173
msg: Base64.stringify(UTF8.parse(args.message)),
6274
signature: args.signature,
@@ -69,7 +81,7 @@ class Lnd extends Base {
6981
});
7082
}
7183

72-
makeInvoice(args) {
84+
makeInvoice(args: MakeInvoiceArgs): Promise<MakeInvoiceResponse> {
7385
return this.request("POST", "/v1/invoices", {
7486
memo: args.memo,
7587
value: args.amount,
@@ -115,7 +127,7 @@ class Lnd extends Base {
115127
});
116128
};
117129

118-
async request(method, path, args, defaultValues) {
130+
async request(method: string, path: string, args: any, defaultValues?: any) {
119131
let body = null;
120132
let query = "";
121133
const headers = new Headers();
@@ -143,10 +155,7 @@ class Lnd extends Base {
143155
throw new Error();
144156
}
145157
} catch (err) {
146-
throw new Error({
147-
statusText: res.statusText,
148-
status: res.status,
149-
});
158+
throw new Error(res.statusText);
150159
}
151160
console.log("errBody", errBody);
152161
throw errBody;
@@ -156,7 +165,7 @@ class Lnd extends Base {
156165
data = Object.assign(Object.assign({}, defaultValues), data);
157166
}
158167
return { data };
159-
} catch (err) {
168+
} catch (err: any) {
160169
console.error(`API error calling ${method} ${path}`, err);
161170
// Thrown errors must be JSON serializable, so include metadata if possible
162171
if (err.code || err.status || !err.message) {

0 commit comments

Comments
 (0)