From d80ae475831ff3fba712f9c2b3d95219524c25d7 Mon Sep 17 00:00:00 2001 From: Will O'Beirne Date: Mon, 3 Dec 2018 21:33:11 -0500 Subject: [PATCH] Update interface for makeInvoice. Update README. --- README.md | 33 +++++++++++++++++++++++++++------ src/provider.ts | 10 +++++++++- 2 files changed, 36 insertions(+), 7 deletions(-) diff --git a/README.md b/README.md index 941fd1b..8791da3 100644 --- a/README.md +++ b/README.md @@ -16,7 +16,9 @@ Apps that want to enable WebLN payments can use the client library in ### requestProvider() -Attempts to acquire and enable a WebLN provider. +Attempts to acquire and enable a WebLN provider. It's recommended +you wait to run this until after `DOMContentLoaded` to ensure that +any client providers have had time to inject the WebLN instance. #### Arguments @@ -31,6 +33,24 @@ Promise (see below) that's already been `enable()`d. * If no providers are available * If the provider rejects the `enable()` call (e.g. user doesn't confirm) +#### Example + +```ts +import { requestProvider } from 'webln/lib/client'; + +let webln; +try { + webln = await requestProvider(); +} catch (err) { + // Handle users without WebLN +} + +// Elsewhere in the code... +if (webln) { + // Call webln functions +} +``` + ## Provider Spec @@ -38,7 +58,7 @@ Promise (see below) that's already been `enable()`d. Providers must implement the interface provided in `webln/lib/provider`. The spec is as follows: -```.ts +```ts export interface WebLNProvider { /* Determines if the WebLNProvider will allow the page to use it */ enable(): Promise; @@ -50,7 +70,7 @@ export interface WebLNProvider { sendPayment(paymentRequest: string): Promise; /* Prompts the user to provide the page with an invoice */ - makeInvoice(amount: string): Promise; + makeInvoice(amount: string | number | RequestInvoiceArgs): Promise; /* Prompts the user to sign a message with their private key */ signMessage(message: string): Promise; @@ -60,9 +80,10 @@ export interface WebLNProvider { } ``` -See the typescript definitions for more detail about response shapes. The spec -is far from complete, and will need more functions to be fully-fledged, but -these methods should cover most use-cases. +See the [typescript definitions](https://github.com/wbobeirne/webln/blob/master/src/provider.ts) +for more detail about request objects and response shapes. The spec +is far from complete, and will need more functions to be fully-fledged, +but these methods should cover most use-cases. ## Contributing diff --git a/src/provider.ts b/src/provider.ts index 50d5c0d..4b782bc 100644 --- a/src/provider.ts +++ b/src/provider.ts @@ -16,6 +16,14 @@ export interface SendPaymentResponse { preimage: string; } +export interface RequestInvoiceArgs { + amount?: string | number; + defaultAmount?: string | number; + minimumAmount?: string | number; + maximumAmount?: string | number; + defaultMemo?: string; +} + export interface RequestInvoiceResponse { paymentRequest: string; } @@ -28,7 +36,7 @@ export interface WebLNProvider { enable(): Promise; getInfo(): Promise; sendPayment(paymentRequest: string): Promise; - makeInvoice(amount: string): Promise; + makeInvoice(args: string | number | RequestInvoiceArgs): Promise; signMessage(message: string): Promise; verifyMessage(signedMessage: string, rawMessage: string): Promise; }