Skip to content

Commit

Permalink
Merge pull request #172 from getAlby/feat/webln-provider-config
Browse files Browse the repository at this point in the history
feat: add webln provider config
  • Loading branch information
rolznz authored Jan 5, 2024
2 parents 0810e83 + 48bc0d7 commit ed30735
Show file tree
Hide file tree
Showing 11 changed files with 65 additions and 23 deletions.
8 changes: 8 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -227,12 +227,20 @@ init({
appName: 'My Lightning App', // your app name
// filters: ["nwc"],
// showBalance: true,
// providerConfig: {
// nwc: {
// authorizationUrlOptions: {
// requestMethods: ['get_balance', 'make_invoice', 'lookup_invoice'],
// },
// },
// }
});
```

- `appName` - Name of the app requesting access to wallet. Currently used for NWC connections (Alby and Mutiny)
- `filters` - Filter the type of connectors you want to show. Example: "nwc" (only show NWC connectors).
- `showBalance` - If false, do not request the connected wallet's balance
- `providerConfig` - **Experimental**: add provider-specific configuration (for NWC, LNC, LNbits etc). Currently only `nwc.authorizationUrlOptions` is supported. `NWCAuthorizationUrlOptions` can be found in the [Alby JS SDK](https://github.com/getAlby/js-sdk).

#### Requesting a provider

Expand Down
8 changes: 8 additions & 0 deletions dev/vite/index.html
Original file line number Diff line number Diff line change
Expand Up @@ -273,6 +273,14 @@ <h3>Make an invoice</h3>
appName,
filters: filters?.split(','),
showBalance,
// example of customizing WebLN provider
/*providerConfig: {
nwc: {
authorizationUrlOptions: {
requestMethods: ['get_balance', 'make_invoice', 'lookup_invoice'],
},
},
},*/
});

document
Expand Down
4 changes: 2 additions & 2 deletions package.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "@getalby/bitcoin-connect",
"version": "3.1.0",
"version": "3.2.0-beta.0",
"description": "Web components to connect to a lightning wallet and power a website with WebLN",
"type": "module",
"source": "src/index.ts",
Expand Down Expand Up @@ -48,7 +48,7 @@
},
"dependencies": {
"@getalby/lightning-tools": "^5.0.0",
"@getalby/sdk": "^3.2.2",
"@getalby/sdk": "^3.2.3",
"@lightninglabs/lnc-web": "^0.2.8-alpha",
"qrcode-generator": "^1.4.4",
"zustand": "^4.4.7"
Expand Down
4 changes: 2 additions & 2 deletions react/package.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "@getalby/bitcoin-connect-react",
"version": "3.1.1",
"version": "3.2.0-beta.0",
"type": "module",
"source": "src/index.ts",
"main": "./dist/index.cjs",
Expand All @@ -26,7 +26,7 @@
"build": "microbundle --globals react=React --jsx React.createElement --jsxFragment React.Fragment --jsxImportSource react"
},
"dependencies": {
"@getalby/bitcoin-connect": "^3.1.0"
"@getalby/bitcoin-connect": "^3.2.0-beta.0"
},
"devDependencies": {
"@types/react": "^18.2.21",
Expand Down
28 changes: 14 additions & 14 deletions react/yarn.lock
Original file line number Diff line number Diff line change
Expand Up @@ -1018,26 +1018,26 @@
"@babel/helper-validator-identifier" "^7.22.5"
to-fast-properties "^2.0.0"

"@getalby/bitcoin-connect@^3.1.0":
version "3.1.0"
resolved "https://registry.yarnpkg.com/@getalby/bitcoin-connect/-/bitcoin-connect-3.1.0.tgz#7546bf200943a665cc8d28dc6cbc3ed9828ff178"
integrity sha512-AH/iK1HbKeW0Jl29leQZa9wclGLAPEaCVLgohjfZ2W42mrCxmOXFnXbaYYQyMwhLdXgrt2M6u7yyDY1zWH43pw==
"@getalby/bitcoin-connect@^3.2.0-beta.0":
version "3.2.0-beta.0"
resolved "https://registry.yarnpkg.com/@getalby/bitcoin-connect/-/bitcoin-connect-3.2.0-beta.0.tgz#e8fbf46d4bc3d3794c6784ae0ea4ee5cfe94b142"
integrity sha512-/o+GJ1x+po5LuBaICjRIZ0Ph5xNyaxX7C+w/4ta8os8TkPIlSXGq8WjLEJK8XbQsHXPH1gMDxhD9bZTHCEvaDw==
dependencies:
"@getalby/lightning-tools" "^5.0.0"
"@getalby/sdk" "^3.0.0"
"@getalby/sdk" "^3.2.3"
"@lightninglabs/lnc-web" "^0.2.8-alpha"
qrcode-generator "^1.4.4"
zustand "^4.4.1"
zustand "^4.4.7"

"@getalby/lightning-tools@^5.0.0":
version "5.0.0"
resolved "https://registry.yarnpkg.com/@getalby/lightning-tools/-/lightning-tools-5.0.0.tgz#eefe1fc1ecf28582d6c922c9e22dc28bf3af27c6"
integrity sha512-j3x90CGclpyZTHJRVWvA95jQniu5A+xqvmJNPSkXyzYCdJiFG5oZFWNwC0N+q7Oa8cTvp7wB7FdDqxH+Le0/Nw==

"@getalby/sdk@^3.0.0":
version "3.0.0"
resolved "https://registry.yarnpkg.com/@getalby/sdk/-/sdk-3.0.0.tgz#3d87b3d388dd14355b8b49f2fd3cd7ab63533b32"
integrity sha512-v4CaQf8HYIvN7Ccp+NU09pROrgugwyOkaouMRrat0DJnbRLpkV8zssPeKEqI29k53USnLn5P3SaHFngM4Se/1A==
"@getalby/sdk@^3.2.3":
version "3.2.3"
resolved "https://registry.yarnpkg.com/@getalby/sdk/-/sdk-3.2.3.tgz#3031d64e935feea879b969da0a17ee05800fba1d"
integrity sha512-8eIqg1vEH2CdkNoHsIlL5q2bKLvLR+XiSWK2RQnTAB+Z0CNlVW2jRM8JxxeubHnKBDTiBrBMkPqBIAhQqc8now==
dependencies:
events "^3.3.0"
nostr-tools "^1.17.0"
Expand Down Expand Up @@ -3578,9 +3578,9 @@ yargs@^17.5.1:
y18n "^5.0.5"
yargs-parser "^21.1.1"

zustand@^4.4.1:
version "4.4.1"
resolved "https://registry.yarnpkg.com/zustand/-/zustand-4.4.1.tgz#0cd3a3e4756f21811bd956418fdc686877e8b3b0"
integrity sha512-QCPfstAS4EBiTQzlaGP1gmorkh/UL1Leaj2tdj+zZCZ/9bm0WS7sI2wnfD5lpOszFqWJ1DcPnGoY8RDL61uokw==
zustand@^4.4.7:
version "4.4.7"
resolved "https://registry.yarnpkg.com/zustand/-/zustand-4.4.7.tgz#355406be6b11ab335f59a66d2cf9815e8f24038c"
integrity sha512-QFJWJMdlETcI69paJwhSMJz7PPWjVP8Sjhclxmxmxv/RYI7ZOvR5BHX+ktH0we9gTWQMxcne8q1OY8xxz604gw==
dependencies:
use-sync-external-store "1.2.0"
7 changes: 7 additions & 0 deletions src/api.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
import {SendPaymentResponse, WebLNProvider} from '@webbtc/webln-types';
import store from './state/store';
import {ConnectorFilter} from './types/ConnectorFilter';
import {WebLNProviderConfig} from './types/WebLNProviderConfig';

type BitcoinConnectConfig = {
/**
Expand All @@ -16,6 +17,11 @@ type BitcoinConnectConfig = {
* Set to false to not request or show the user's wallet balance
*/
showBalance?: boolean;

/**
* Customize individual providers (NWC, LNC, LNbits etc)
*/
providerConfig?: WebLNProviderConfig;
};

type LaunchPaymentModalArgs = {
Expand Down Expand Up @@ -167,6 +173,7 @@ export function init(config: BitcoinConnectConfig = {}) {
store.getState().setAppName(config.appName);
store.getState().setFilters(config.filters);
store.getState().setShowBalance(config.showBalance);
store.getState().setProviderConfig(config.providerConfig);
}

/**
Expand Down
4 changes: 3 additions & 1 deletion src/components/connectors/bc-alby-nwc-connector.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ import {customElement} from 'lit/decorators.js';
import {albyIcon} from '../icons/connectors/albyIcon';
import {webln} from '@getalby/sdk';
import {ConnectorElement} from './ConnectorElement';
import store from '../../state/store';

@customElement('bc-alby-nwc-connector')
export class AlbyNWCConnector extends ConnectorElement {
Expand All @@ -16,8 +17,9 @@ export class AlbyNWCConnector extends ConnectorElement {

protected async _onClick() {
const nwc = webln.NostrWebLNProvider.withNewSecret();

const providerConfig = store.getState().providerConfig;
await nwc.initNWC({
...(providerConfig?.nwc?.authorizationUrlOptions || {}),
name: this._appName || 'Bitcoin Connect',
});

Expand Down
3 changes: 3 additions & 0 deletions src/components/pages/bc-mutiny.ts
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,9 @@ export class MutinyPage extends withTwind()(BitcoinConnectElement) {
@state()
private _nwcUrl = '';

// TODO: subscribe to store.providerConfig and pass extra options to mutiny URL below
// (also consider whether this should be replaced with NWC Wallet Auth - https://github.com/nostr-protocol/nips/pull/851)

override render() {
return html`<div class="w-full">
<bc-navbar
Expand Down
7 changes: 7 additions & 0 deletions src/state/store.ts
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@ import {Connector} from '../connectors/Connector';
import {Route} from '../components/routes';
import {ConnectorFilter} from '../types/ConnectorFilter';
import {WebLNProvider} from '@webbtc/webln-types';
import {WebLNProviderConfig} from '../types/WebLNProviderConfig';

interface PrivateStore {
readonly connector: Connector | undefined;
Expand Down Expand Up @@ -37,13 +38,15 @@ interface Store {
readonly modalOpen: boolean;
readonly provider: WebLNProvider | undefined;
readonly currency: string | undefined;
readonly providerConfig: WebLNProviderConfig | undefined;

connect(config: ConnectorConfig): void;
disconnect(): void;
pushRoute(route: Route): void;
popRoute(): void;
setAppName(appName: string | undefined): void;
setShowBalance(showBalance: boolean | undefined): void;
setProviderConfig(providerConfig: WebLNProviderConfig | undefined): void;
setFilters(filters: ConnectorFilter[] | undefined): void;
setError(error: string | undefined): void;
clearRouteHistory(): void;
Expand Down Expand Up @@ -71,6 +74,7 @@ const store = createStore<Store>((set, get) => ({
filters: undefined,
invoice: undefined,
provider: undefined,
providerConfig: undefined,
connect: async (config: ConnectorConfig) => {
set({
connecting: true,
Expand Down Expand Up @@ -146,6 +150,9 @@ const store = createStore<Store>((set, get) => ({
setFilters: (filters) => {
set({filters});
},
setProviderConfig: (providerConfig) => {
set({providerConfig});
},
setError: (error) => {
set({error});
},
Expand Down
7 changes: 7 additions & 0 deletions src/types/WebLNProviderConfig.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
import {types} from '@getalby/sdk';

export type WebLNProviderConfig = {
nwc?: {
authorizationUrlOptions: types.GetNWCAuthorizationUrlOptions;
};
};
8 changes: 4 additions & 4 deletions yarn.lock
Original file line number Diff line number Diff line change
Expand Up @@ -1309,10 +1309,10 @@
resolved "https://registry.yarnpkg.com/@getalby/lightning-tools/-/lightning-tools-5.0.0.tgz#eefe1fc1ecf28582d6c922c9e22dc28bf3af27c6"
integrity sha512-j3x90CGclpyZTHJRVWvA95jQniu5A+xqvmJNPSkXyzYCdJiFG5oZFWNwC0N+q7Oa8cTvp7wB7FdDqxH+Le0/Nw==

"@getalby/sdk@^3.2.2":
version "3.2.2"
resolved "https://registry.yarnpkg.com/@getalby/sdk/-/sdk-3.2.2.tgz#a640fef78f4462fd8924eab9ab8a8f9a0339a11e"
integrity sha512-G4Ooteo/5D6SXB+y8OK8gxXWALGh4HFgq8ZqT3rBMo3FV7U/fDjf+/jn/SMsJ7ub/nEzUBBTGdfARdVoYqMvSQ==
"@getalby/sdk@^3.2.3":
version "3.2.3"
resolved "https://registry.yarnpkg.com/@getalby/sdk/-/sdk-3.2.3.tgz#3031d64e935feea879b969da0a17ee05800fba1d"
integrity sha512-8eIqg1vEH2CdkNoHsIlL5q2bKLvLR+XiSWK2RQnTAB+Z0CNlVW2jRM8JxxeubHnKBDTiBrBMkPqBIAhQqc8now==
dependencies:
events "^3.3.0"
nostr-tools "^1.17.0"
Expand Down

0 comments on commit ed30735

Please sign in to comment.