-
Notifications
You must be signed in to change notification settings - Fork 194
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #2218 from getAlby/fix/race-condition
fix: race conditions
- Loading branch information
Showing
15 changed files
with
187 additions
and
215 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
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
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
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
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
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
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
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
This file was deleted.
Oops, something went wrong.
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,70 @@ | ||
import { postMessage } from "../postMessage"; | ||
import { Event } from "./types"; | ||
|
||
declare global { | ||
interface Window { | ||
nostr: NostrProvider; | ||
} | ||
} | ||
|
||
export default class NostrProvider { | ||
nip04 = new Nip04(this); | ||
enabled: boolean; | ||
|
||
constructor() { | ||
this.enabled = false; | ||
} | ||
|
||
async enable() { | ||
if (this.enabled) { | ||
return { enabled: true }; | ||
} | ||
return await this.execute("enable"); | ||
} | ||
|
||
async getPublicKey() { | ||
await this.enable(); | ||
return await this.execute("getPublicKeyOrPrompt"); | ||
} | ||
|
||
async signEvent(event: Event) { | ||
await this.enable(); | ||
return this.execute("signEventOrPrompt", { event }); | ||
} | ||
|
||
async signSchnorr(sigHash: string) { | ||
await this.enable(); | ||
return this.execute("signSchnorrOrPrompt", { sigHash }); | ||
} | ||
|
||
async getRelays() { | ||
await this.enable(); | ||
return this.execute("getRelays"); | ||
} | ||
|
||
// 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 postMessage("nostr", action, args); | ||
} | ||
} | ||
|
||
class Nip04 { | ||
provider: NostrProvider; | ||
|
||
constructor(provider: NostrProvider) { | ||
this.provider = provider; | ||
} | ||
|
||
async encrypt(peer: string, plaintext: string) { | ||
await this.provider.enable(); | ||
return this.provider.execute("encryptOrPrompt", { peer, plaintext }); | ||
} | ||
|
||
async decrypt(peer: string, ciphertext: string) { | ||
await this.provider.enable(); | ||
return this.provider.execute("decryptOrPrompt", { peer, ciphertext }); | ||
} | ||
} |
File renamed without changes.
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,51 @@ | ||
export function postMessage<T>( | ||
scope: string, | ||
action: string, | ||
args: T | undefined | ||
): Promise<T> { | ||
return new Promise((resolve, reject) => { | ||
const id = Math.random().toString().slice(4); | ||
|
||
// 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( | ||
{ | ||
id: id, | ||
application: "LBE", | ||
prompt: true, | ||
action: `${scope}/${action}`, | ||
scope: scope, | ||
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" || | ||
messageEvent.data.scope !== scope || | ||
messageEvent.data.id !== id | ||
) { | ||
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); | ||
}); | ||
} |
Oops, something went wrong.