Fix web extension after de3f223c
This commit is contained in:
parent
77e2ebfbd6
commit
3fe32d8d24
1 changed files with 19 additions and 2 deletions
|
|
@ -56,6 +56,23 @@ function makeNsecSigner(secretKey: Uint8Array): Signer {
|
||||||
};
|
};
|
||||||
}
|
}
|
||||||
|
|
||||||
|
// Always reads window.nostr at call time. This avoids two issues:
|
||||||
|
// 1. window.nostr may not be injected yet when restoreSession runs.
|
||||||
|
// 2. Storing window.nostr in a $state wraps it in a reactive proxy, which
|
||||||
|
// rebinds `this` and confuses some extensions (request never resolves).
|
||||||
|
function makeExtensionSigner(): Signer {
|
||||||
|
return {
|
||||||
|
async getPublicKey() {
|
||||||
|
if (!window.nostr) throw new Error("No Nostr extension found");
|
||||||
|
return window.nostr.getPublicKey();
|
||||||
|
},
|
||||||
|
async signEvent(template) {
|
||||||
|
if (!window.nostr) throw new Error("No Nostr extension found");
|
||||||
|
return window.nostr.signEvent(template);
|
||||||
|
},
|
||||||
|
};
|
||||||
|
}
|
||||||
|
|
||||||
async function setUser(pubkey: string) {
|
async function setUser(pubkey: string) {
|
||||||
const { loadNostrUser } = await import("@nostr/gadgets/metadata");
|
const { loadNostrUser } = await import("@nostr/gadgets/metadata");
|
||||||
user = await loadNostrUser(pubkey);
|
user = await loadNostrUser(pubkey);
|
||||||
|
|
@ -67,7 +84,7 @@ export async function loginWithExtension() {
|
||||||
throw new Error("No Nostr extension found");
|
throw new Error("No Nostr extension found");
|
||||||
}
|
}
|
||||||
const pubkey = await window.nostr.getPublicKey();
|
const pubkey = await window.nostr.getPublicKey();
|
||||||
signer = window.nostr;
|
signer = makeExtensionSigner();
|
||||||
localStorage.setItem(PUBKEY_KEY, pubkey);
|
localStorage.setItem(PUBKEY_KEY, pubkey);
|
||||||
localStorage.setItem(METHOD_KEY, "extension");
|
localStorage.setItem(METHOD_KEY, "extension");
|
||||||
localStorage.removeItem(NSEC_KEY);
|
localStorage.removeItem(NSEC_KEY);
|
||||||
|
|
@ -129,7 +146,7 @@ export async function restoreSession() {
|
||||||
return;
|
return;
|
||||||
}
|
}
|
||||||
} else {
|
} else {
|
||||||
signer = window.nostr ?? null;
|
signer = makeExtensionSigner();
|
||||||
}
|
}
|
||||||
await setUser(pubkey);
|
await setUser(pubkey);
|
||||||
}
|
}
|
||||||
|
|
|
||||||
Loading…
Add table
Add a link
Reference in a new issue