-
Notifications
You must be signed in to change notification settings - Fork 195
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
1 parent
3210e83
commit f8e57b8
Showing
2 changed files
with
147 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,5 +1,7 @@ | ||
import WebLNProvider from "../ln/webln"; | ||
import WebBTCProvider from "../webbtc"; | ||
|
||
if (document) { | ||
window.webln = new WebLNProvider(); | ||
window.webbtc = new WebBTCProvider(); | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,145 @@ | ||
type RequestInvoiceArgs = { | ||
amount?: string | number; | ||
defaultAmount?: string | number; | ||
minimumAmount?: string | number; | ||
maximumAmount?: string | number; | ||
defaultMemo?: string; | ||
}; | ||
|
||
export default class WebBTCProvider { | ||
enabled: boolean; | ||
isEnabled: boolean; | ||
executing: boolean; | ||
|
||
constructor() { | ||
this.enabled = false; | ||
this.isEnabled = false; | ||
this.executing = false; | ||
} | ||
|
||
enable() { | ||
if (this.enabled) { | ||
return Promise.resolve({ enabled: true }); | ||
} | ||
return this.execute("enable").then((result) => { | ||
if (typeof result.enabled === "boolean") { | ||
this.enabled = result.enabled; | ||
this.isEnabled = result.enabled; | ||
} | ||
return result; | ||
}); | ||
} | ||
|
||
getInfo() { | ||
if (!this.enabled) { | ||
throw new Error("Provider must be enabled before calling getInfo"); | ||
} | ||
return { | ||
version: "stable", | ||
supports: ["lightning"], | ||
methods: [ | ||
"enable", | ||
"getInfo", | ||
"signMessage", | ||
"verifyMessage", | ||
"makeInvoice", | ||
"sendPayment", | ||
"keysend", | ||
], | ||
}; | ||
} | ||
|
||
signMessage(message: string) { | ||
if (!this.enabled) { | ||
throw new Error("Provider must be enabled before calling signMessage"); | ||
} | ||
|
||
return this.execute("signMessageOrPrompt", { message }); | ||
} | ||
|
||
verifyMessage(signature: string, message: string) { | ||
if (!this.enabled) { | ||
throw new Error("Provider must be enabled before calling verifyMessage"); | ||
} | ||
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 }); | ||
} | ||
|
||
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`"); | ||
} | ||
|
||
// NOTE: new call `action`s must be specified also in the content script | ||
execute( | ||
action: string, | ||
args?: Record<string, unknown> | ||
): Promise<Record<string, unknown>> { | ||
return new Promise((resolve, reject) => { | ||
// post the request to the content script. from there it gets passed to the background script and back | ||
// in page script can not directly connect to the background script | ||
window.postMessage( | ||
{ | ||
application: "LBE", | ||
prompt: true, | ||
action: `webln/${action}`, | ||
scope: "webln", | ||
args, | ||
}, | ||
"*" // TODO use origin | ||
); | ||
|
||
function handleWindowMessage(messageEvent: MessageEvent) { | ||
// check if it is a relevant message | ||
// there are some other events happening | ||
if ( | ||
!messageEvent.data || | ||
!messageEvent.data.response || | ||
messageEvent.data.application !== "LBE" | ||
) { | ||
return; | ||
} | ||
if (messageEvent.data.data.error) { | ||
reject(new Error(messageEvent.data.data.error)); | ||
} else { | ||
// 1. data: the message data | ||
// 2. data: the data passed as data to the message | ||
// 3. data: the actual response data | ||
resolve(messageEvent.data.data.data); | ||
} | ||
// For some reason must happen only at the end of this function | ||
window.removeEventListener("message", handleWindowMessage); | ||
} | ||
|
||
window.addEventListener("message", handleWindowMessage); | ||
}); | ||
} | ||
} |