Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat: add webln.request #56

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
22 changes: 14 additions & 8 deletions src/client.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
import { WebLNProvider } from './provider';
import { MissingProviderError } from './errors';
import { WebLNProvider } from "./provider";
import { MissingProviderError } from "./errors";

/**
* Everything needed to get and set providers on the client.
Expand All @@ -12,19 +12,25 @@ export interface GetProviderParameters {
pubkey?: string;
}

export function requestProvider(_: GetProviderParameters = {}): Promise<WebLNProvider> {
// TODO: expose webln.enable() response in return type
export function requestProvider(
_: GetProviderParameters = {}
): Promise<WebLNProvider> {
return new Promise((resolve, reject) => {
if (typeof window === 'undefined') {
return reject(new Error('Must be called in a browser context'));
if (typeof window === "undefined") {
return reject(new Error("Must be called in a browser context"));
}

const webln: WebLNProvider = (window as any).webln;
if (!webln) {
return reject(new MissingProviderError('Your browser has no WebLN provider'));
return reject(
new MissingProviderError("Your browser has no WebLN provider")
);
}

webln.enable()
webln
.enable()
.then(() => resolve(webln))
.catch(err => reject(err));
.catch((err) => reject(err));
});
}
7 changes: 4 additions & 3 deletions src/index.ts
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
export * from './client';
export * from './provider';
export * from './errors';
export * from "./client";
export * from "./provider";
export * from "./errors";
export * from "./request-method";
19 changes: 17 additions & 2 deletions src/provider.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,9 @@
* Everything needed to implement your own provider.
*/

import { RequestMethod } from "./request-method";
import { AdditionalString } from "./types";

export interface WebLNNode {
alias: string;
pubkey: string;
Expand All @@ -10,6 +13,9 @@ export interface WebLNNode {

export interface GetInfoResponse {
node: WebLNNode;
version: string;
supports: ("lightning" | AdditionalString)[];
methods: RequestMethod[];
}

export interface SendPaymentResponse {
Expand Down Expand Up @@ -39,12 +45,21 @@ export interface SignMessageResponse {
signature: string;
}

export interface EnableResponse {
enabled: boolean;
remember: boolean;
}

export interface WebLNProvider {
enable(): Promise<void>;
enable(): Promise<EnableResponse>;
getInfo(): Promise<GetInfoResponse>;
sendPayment(paymentRequest: string): Promise<SendPaymentResponse>;
keysend(args: KeysendArgs): Promise<SendPaymentResponse>;
makeInvoice(args: string | number | RequestInvoiceArgs): Promise<RequestInvoiceResponse>;
makeInvoice(
args: string | number | RequestInvoiceArgs
): Promise<RequestInvoiceResponse>;
signMessage(message: string): Promise<SignMessageResponse>;
verifyMessage(signature: string, message: string): Promise<void>;
// TODO: improve request typings
request(method: RequestMethod, args?: unknown): Promise<unknown>;
}
26 changes: 26 additions & 0 deletions src/request-method.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,26 @@
import { AdditionalString } from "./types";

export type RequestMethod =
| "getinfo"
| "listchannels"
| "listinvoices"
| "channelbalance"
| "walletbalance"
| "openchannel"
| "connectpeer"
| "disconnectpeer"
| "estimatefee"
| "getchaninfo"
| "getnetworkinfo"
| "getnodeinfo"
| "gettransactions"
| "listpayments"
| "listpeers"
| "lookupinvoice"
| "queryroutes"
| "verifymessage"
| "sendtoroute"
| "decodepayreq"
| "routermc"
| "addinvoice"
| AdditionalString;
3 changes: 3 additions & 0 deletions src/types.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
// Workaround for code completion to account for values this library doesn't know about yet.
// This allows TypeScript to understand that AdditionalString is a string that is distinct from the string literal type.
export type AdditionalString = string & { _additionalString?: never };