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 #2 from getAlby/nostr-wallet-connect
Nostr wallet connect
- Loading branch information
Showing
11 changed files
with
366 additions
and
18 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 +1 @@ | ||
18.1.0 | ||
18.12.1 |
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,4 +1,4 @@ | ||
export * as auth from "./auth"; | ||
export * as types from './types' | ||
export { Client } from "./client"; | ||
export { WebLNProvider } from "./WeblnProvider"; | ||
export * as webln from "./webln"; | ||
export { Client } from "./client"; |
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,45 @@ | ||
import { useEffect, useState } from "react"; | ||
|
||
const AlbyOauthCallback = () => { | ||
const [error, setError] = useState(null); | ||
|
||
useEffect(() => { | ||
if (!window.opener) { | ||
alert("Something went wrong. Opener not available. Please contact support@getalby.com"); | ||
return; | ||
} | ||
const params = new URLSearchParams(window.location.search) | ||
const code = params.get("code"); | ||
const error = params.get("error"); | ||
|
||
if (!code) { | ||
setError("declined"); | ||
} | ||
if (error) { | ||
setError(error); | ||
alert(error); | ||
return; | ||
} | ||
|
||
window.opener.postMessage({ | ||
type: 'alby:oauth:success', | ||
payload: { code }, | ||
}); | ||
console.log("auth message published"); | ||
|
||
}, []); | ||
|
||
return ( | ||
<div> | ||
{error && ( | ||
<p>Authorization failed: {error}</p> | ||
)} | ||
{!error && ( | ||
<p>Connected. you can close this window.</p> | ||
)} | ||
</div> | ||
) | ||
|
||
} | ||
|
||
export default AlbyOauthCallback; |
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,190 @@ | ||
import { | ||
nip04, | ||
relayInit, | ||
signEvent, | ||
getEventHash, | ||
getPublicKey, | ||
nip19, | ||
Relay, | ||
Event, | ||
UnsignedEvent | ||
} from 'nostr-tools'; | ||
|
||
const DEFAULT_OPTIONS = { | ||
relayUrl: 'wss://relay.damus.io', | ||
walletPubkey: '69effe7b49a6dd5cf525bd0905917a5005ffe480b58eeb8e861418cf3ae760d9' // Alby | ||
}; | ||
|
||
interface Nostr { | ||
signEvent: (event: UnsignedEvent) => Promise<Event>; | ||
nip04: { | ||
decrypt: (pubkey: string, content: string) => Promise<string>; | ||
encrypt: (pubkey: string, content: string) => Promise<string>; | ||
}; | ||
} | ||
declare global { | ||
var nostr: Nostr | undefined; | ||
} | ||
|
||
interface NostrWebLNOptions { | ||
relayUrl: string; | ||
walletPubkey: string; | ||
privateKey?: string; | ||
} | ||
|
||
|
||
export class NostrWebLNProvider { | ||
relay: Relay; | ||
relayUrl: string; | ||
privateKey: string | undefined; | ||
walletPubkey: string; | ||
subscribers: Record<string, (payload: any) => void>; | ||
connected: boolean; | ||
|
||
static parseWalletConnectUrl(walletConnectUrl: string) { | ||
const url = new URL(walletConnectUrl); | ||
const options = {} as NostrWebLNOptions; | ||
options.walletPubkey = url.pathname.replace('//', ''); | ||
const privateKey = url.searchParams.get('secret'); | ||
const relayUrl = url.searchParams.get('relay'); | ||
if (privateKey) { | ||
options.privateKey = privateKey; | ||
} | ||
if (relayUrl) { | ||
options.relayUrl = relayUrl; | ||
} | ||
return options; | ||
} | ||
constructor(options: { relayUrl?: string, privateKey?: string, walletPubkey?: string, nostrWalletConnectUrl?: string }) { | ||
if (options.nostrWalletConnectUrl) { | ||
options = { | ||
...NostrWebLNProvider.parseWalletConnectUrl(options.nostrWalletConnectUrl), ...options | ||
}; | ||
} | ||
const _options = { ...DEFAULT_OPTIONS, ...options } as NostrWebLNOptions; | ||
this.relayUrl = _options.relayUrl; | ||
this.relay = relayInit(this.relayUrl); | ||
if (_options.privateKey) { | ||
this.privateKey = (_options.privateKey.toLowerCase().startsWith('nsec') ? nip19.decode(_options.privateKey).data : _options.privateKey) as string; | ||
} | ||
this.walletPubkey = (_options.walletPubkey.toLowerCase().startsWith('npub') ? nip19.decode(_options.walletPubkey).data : _options.walletPubkey) as string; | ||
this.subscribers = {}; | ||
this.connected = false; | ||
} | ||
|
||
on(name: string, callback: () => void) { | ||
this.subscribers[name] = callback; | ||
} | ||
|
||
notify(name: string, payload?: any) { | ||
const callback = this.subscribers[name]; | ||
if (callback) { | ||
callback(payload); | ||
} | ||
} | ||
|
||
async enable() { | ||
if (this.connected) { | ||
return Promise.resolve(); | ||
} | ||
this.relay.on('connect', () => { | ||
//console.debug(`connected to ${this.relay.url}`); | ||
this.connected = true; | ||
}) | ||
await this.relay.connect(); | ||
} | ||
|
||
async encrypt(pubkey: string, content: string) { | ||
let encrypted; | ||
if (globalThis.nostr && !this.privateKey) { | ||
encrypted = await globalThis.nostr.nip04.encrypt(pubkey, content); | ||
} else if (this.privateKey) { | ||
encrypted = await nip04.encrypt(this.privateKey, pubkey, content); | ||
} else { | ||
throw new Error("Missing private key"); | ||
} | ||
return encrypted; | ||
} | ||
|
||
async decrypt(pubkey: string, content: string) { | ||
let decrypted; | ||
if (globalThis.nostr && !this.privateKey) { | ||
decrypted = await globalThis.nostr.nip04.decrypt(pubkey, content); | ||
} else if (this.privateKey) { | ||
decrypted = await nip04.decrypt(this.privateKey, pubkey, content); | ||
} else { | ||
throw new Error("Missing private key"); | ||
} | ||
return decrypted; | ||
} | ||
|
||
sendPayment(invoice: string) { | ||
return new Promise(async (resolve, reject) => { | ||
const encryptedInvoice = await this.encrypt(this.walletPubkey, invoice); | ||
let event: any = { | ||
kind: 23194, | ||
created_at: Math.floor(Date.now() / 1000), | ||
tags: [['p', this.walletPubkey]], | ||
content: encryptedInvoice, | ||
}; | ||
|
||
if (globalThis.nostr && !this.privateKey) { | ||
event = await globalThis.nostr.signEvent(event); | ||
} else if (this.privateKey) { | ||
event.pubkey = getPublicKey(this.privateKey) | ||
event.id = getEventHash(event) | ||
event.sig = signEvent(event, this.privateKey) | ||
} else { | ||
throw new Error("Missing private key"); | ||
} | ||
|
||
let pub = this.relay.publish(event); | ||
|
||
function publishTimeout() { | ||
//console.error(`Publish timeout: event ${event.id}`); | ||
reject('Publish timeout'); | ||
} | ||
let publishTimeoutCheck = setTimeout(publishTimeout, 2000); | ||
|
||
// @ts-ignore | ||
pub.on('failed', (reason) => { | ||
//console.debug(`failed to publish to ${this.relay.url}: ${reason}`) | ||
clearTimeout(publishTimeoutCheck) | ||
reject(`Failed to publish request: ${reason}`); | ||
}); | ||
|
||
pub.on('ok', () => { | ||
//console.debug(`Event ${event.id} for ${invoice} published`); | ||
clearTimeout(publishTimeoutCheck); | ||
|
||
function replyTimeout() { | ||
//console.error(`Reply timeout: event ${event.id} `); | ||
reject('reply timeout'); | ||
} | ||
let replyTimeoutCheck = setTimeout(replyTimeout, 60000); | ||
|
||
if (!this.relay) { return; } // mainly for TS | ||
let sub = this.relay.sub([ | ||
{ | ||
kinds: [23195, 23196], | ||
authors: [this.walletPubkey], | ||
"#e": [event.id], | ||
} | ||
]); | ||
sub.on('event', async (event) => { | ||
//console.log(`Received reply event: `, event); | ||
clearTimeout(replyTimeoutCheck); | ||
sub.unsub(); | ||
const decryptedContent = await this.decrypt(this.walletPubkey, event.content); | ||
// @ts-ignore // event is still unknown in nostr-tools | ||
if (event.kind == 23195) { | ||
resolve({ preimage: decryptedContent }); | ||
this.notify('sendPayment', event.content); | ||
} else { | ||
reject({ error: decryptedContent }); | ||
} | ||
}); | ||
}); | ||
}); | ||
} | ||
} |
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,2 @@ | ||
export * from './NostrWeblnProvider'; | ||
export * from './OauthWeblnProvider'; |
Oops, something went wrong.