Skip to content

Commit

Permalink
chore: extend from provider class
Browse files Browse the repository at this point in the history
  • Loading branch information
im-adithya authored and escapedcat committed Oct 5, 2022
1 parent 48f7b85 commit 0ac5519
Show file tree
Hide file tree
Showing 6 changed files with 107 additions and 179 deletions.
49 changes: 49 additions & 0 deletions src/extension/content-script/providers/WebBTC.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,49 @@
import Provider from ".";
import { RequestInvoiceArgs } from ".";

export default class WebBTCProvider extends Provider {
// TODO: return {version: "x.x.x", methods: [...]}
getInfo() {
if (!this.enabled) {
throw new Error("Provider must be enabled before calling getInfo");
}
return this.execute("getInfo");
}

// TODO: only return signature: string
signMessage(message: string) {
if (!this.enabled) {
throw new Error("Provider must be enabled before calling signMessage");
}

return this.execute("signMessageOrPrompt", { message });
}

// TODO: only return {paymentRequest: string}
makeInvoice(args: string | number | RequestInvoiceArgs) {
if (!this.enabled) {
throw new Error("Provider must be enabled before calling makeInvoice");
}
if (typeof args !== "object") {
args = { amount: args };
}

return this.execute("makeInvoice", args);
}

sendTransaction(address: string, amount: string) {
if (!this.enabled) {
throw new Error(
"Provider must be enabled before calling sendTransaction"
);
}
throw new Error("Alby does not support `sendTransaction`");
}

getAddress(index: number, num: number, change: boolean) {
if (!this.enabled) {
throw new Error("Provider must be enabled before calling getAddress");
}
throw new Error("Alby does not support `getAddress`");
}
}
50 changes: 50 additions & 0 deletions src/extension/content-script/providers/WebLN.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,50 @@
import Provider from ".";
import { RequestInvoiceArgs } from ".";

type KeysendArgs = {
destination: string;
customRecords?: Record<string, string>;
amount: string | number;
};

export default class WebLNProvider extends Provider {
getInfo() {
if (!this.enabled) {
throw new Error("Provider must be enabled before calling getInfo");
}
return this.execute("getInfo");
}

lnurl(lnurlEncoded: string) {
if (!this.enabled) {
throw new Error("Provider must be enabled before calling lnurl");
}
return this.execute("lnurl", { lnurlEncoded });
}

keysend(args: KeysendArgs) {
if (!this.enabled) {
throw new Error("Provider must be enabled before calling keysend");
}
return this.execute("keysendOrPrompt", args);
}

makeInvoice(args: string | number | RequestInvoiceArgs) {
if (!this.enabled) {
throw new Error("Provider must be enabled before calling makeInvoice");
}
if (typeof args !== "object") {
args = { amount: args };
}

return this.execute("makeInvoice", args);
}

signMessage(message: string) {
if (!this.enabled) {
throw new Error("Provider must be enabled before calling signMessage");
}

return this.execute("signMessageOrPrompt", { message });
}
}
Original file line number Diff line number Diff line change
@@ -1,12 +1,12 @@
type RequestInvoiceArgs = {
export type RequestInvoiceArgs = {
amount?: string | number;
defaultAmount?: string | number;
minimumAmount?: string | number;
maximumAmount?: string | number;
defaultMemo?: string;
};

export default class WebBTCProvider {
export default class Provider {
enabled: boolean;
isEnabled: boolean;
executing: boolean;
Expand All @@ -30,28 +30,11 @@ export default class WebBTCProvider {
});
}

// return {version: }
getInfo() {
if (!this.enabled) {
throw new Error("Provider must be enabled before calling getInfo");
}
return this.execute("getInfo");
}

getAddress(index: number, num: number, change: boolean) {
if (!this.enabled) {
throw new Error("Provider must be enabled before calling getAddress");
}
throw new Error("Alby does not support `getAddress`");
}

// return only (signature: string)
signMessage(message: string) {
sendPayment(paymentRequest: string) {
if (!this.enabled) {
throw new Error("Provider must be enabled before calling signMessage");
throw new Error("Provider must be enabled before calling sendPayment");
}

return this.execute("signMessageOrPrompt", { message });
return this.execute("sendPaymentOrPrompt", { paymentRequest });
}

verifyMessage(signature: string, message: string) {
Expand All @@ -61,24 +44,6 @@ export default class WebBTCProvider {
throw new Error("Alby does not support `verifyMessage`");
}

makeInvoice(args: string | number | RequestInvoiceArgs) {
if (!this.enabled) {
throw new Error("Provider must be enabled before calling makeInvoice");
}
if (typeof args !== "object") {
args = { amount: args };
}

return this.execute("makeInvoice", args);
}

sendPayment(paymentRequest: string) {
if (!this.enabled) {
throw new Error("Provider must be enabled before calling sendPayment");
}
return this.execute("sendPaymentOrPrompt", { paymentRequest });
}

// NOTE: new call `action`s must be specified also in the content script
execute(
action: string,
Expand Down
2 changes: 1 addition & 1 deletion src/extension/inpage-script/index.js
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
import { ABORT_PROMPT_ERROR, USER_REJECTED_ERROR } from "~/common/constants";

import WebLNProvider from "../ln/webln";
import WebLNProvider from "../content-script/providers/WebLN";

if (document) {
// window.webln is normally loaded onstart (see onstart.js)
Expand Down
4 changes: 2 additions & 2 deletions src/extension/inpage-script/webln.js
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
import WebLNProvider from "../ln/webln";
import WebBTCProvider from "../webbtc";
import WebBTCProvider from "../content-script/providers/WebBTC";
import WebLNProvider from "../content-script/providers/WebLN";

if (document) {
window.webln = new WebLNProvider();
Expand Down
136 changes: 0 additions & 136 deletions src/extension/ln/webln/index.ts

This file was deleted.

0 comments on commit 0ac5519

Please sign in to comment.