Skip to content

Commit

Permalink
Merge pull request #186 from getAlby/feat/get-connector-config
Browse files Browse the repository at this point in the history
feat: add api function to get connector config
  • Loading branch information
rolznz authored Jan 9, 2024
2 parents 838a194 + 31c367a commit 66aae43
Show file tree
Hide file tree
Showing 8 changed files with 47 additions and 56 deletions.
13 changes: 13 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -314,6 +314,19 @@ import {disconnect} from '@getalby/bitcoin-connect';
disconnect();
```

#### Get connector config

Returns the saved configuration of the currently-connected connector (if connected)

```ts
import {getConnectorConfig} from '@getalby/bitcoin-connect';

const connectorConfig = getConnectorConfig();
if (connectorConfig) {
// can now access e.g. connectorConfig.connectorName
}
```

#### Events

##### onConnected
Expand Down
9 changes: 9 additions & 0 deletions dev/vite/index.html
Original file line number Diff line number Diff line change
Expand Up @@ -245,6 +245,8 @@ <h3>Pay an invoice</h3>
<button id="pay-invoice">Pay 1 sat to Alby team</button>
<h3>Make an invoice</h3>
<button id="make-invoice">Make a 1 sat invoice</button>
<h3>Connector config</h3>
<button id="get-connector-config">Get connector config</button>
<h3>Try it yourself</h3>
<p>
Open devtools and use <b>window.bitcoinConnectSandbox</b> to access the
Expand All @@ -258,6 +260,7 @@ <h3>Try it yourself</h3>
launchModal,
launchPaymentModal,
requestProvider,
getConnectorConfig,
} from '../../src/index.ts';

const appName = localStorage.getItem('app-name');
Expand Down Expand Up @@ -386,6 +389,12 @@ <h3>Try it yourself</h3>
}
});

document
.getElementById('get-connector-config')
.addEventListener('click', async () => {
alert(JSON.stringify(getConnectorConfig(), null, 2));
});

document.addEventListener('bc:onpaid', (e) => console.log('PAID!', e));
</script>
<script type="module">
Expand Down
2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "@getalby/bitcoin-connect",
"version": "3.2.0-beta.0",
"version": "3.2.0-beta.1",
"description": "Web components to connect to a lightning wallet and power a website with WebLN",
"type": "module",
"source": "src/index.ts",
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.2.0-beta.0",
"version": "3.2.0-beta.1",
"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.2.0-beta.0"
"@getalby/bitcoin-connect": "^3.2.0-beta.1"
},
"devDependencies": {
"@types/react": "^18.2.21",
Expand Down
1 change: 1 addition & 0 deletions react/src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -15,5 +15,6 @@ export {
onDisconnected,
onModalOpened,
onModalClosed,
getConnectorConfig,
WebLNProviders,
} from '@getalby/bitcoin-connect';
8 changes: 4 additions & 4 deletions react/yarn.lock
Original file line number Diff line number Diff line change
Expand Up @@ -1018,10 +1018,10 @@
"@babel/helper-validator-identifier" "^7.22.5"
to-fast-properties "^2.0.0"

"@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==
"@getalby/bitcoin-connect@^3.2.0-beta.1":
version "3.2.0-beta.1"
resolved "https://registry.yarnpkg.com/@getalby/bitcoin-connect/-/bitcoin-connect-3.2.0-beta.1.tgz#95a25b89747602b4bdeacc4025e7219e4c31dd54"
integrity sha512-54I6YYbaBh5kEugJKObemVy8NSrg5QBtysuLIBGlsvKRNCqOQwotSX3CjtVXWR+IHF/AvPgcGO4HuUvzf8mj1w==
dependencies:
"@getalby/lightning-tools" "^5.0.0"
"@getalby/sdk" "^3.2.3"
Expand Down
8 changes: 8 additions & 0 deletions src/api.ts
Original file line number Diff line number Diff line change
Expand Up @@ -67,6 +67,7 @@ export function onConnected(callback: (provider: WebLNProvider) => void) {
}

/**
* Listen to onConnecting events which will fire when a user is connecting to their wallet
* Subscribe to onConnecting events which will fire when a user is connecting to their wallet
*
* If a provider is already being connected to when the subscription is created, the callback will be immediately fired.
Expand Down Expand Up @@ -297,3 +298,10 @@ export function closeModal() {
export function disconnect() {
store.getState().disconnect();
}

/**
* @returns the configuration of the current connector (if connected)
*/
export function getConnectorConfig() {
return store.getState().config;
}
58 changes: 9 additions & 49 deletions src/state/store.ts
Original file line number Diff line number Diff line change
Expand Up @@ -7,24 +7,6 @@ import {ConnectorFilter} from '../types/ConnectorFilter';
import {WebLNProvider} from '@webbtc/webln-types';
import {WebLNProviderConfig} from '../types/WebLNProviderConfig';

interface PrivateStore {
readonly connector: Connector | undefined;
readonly config: ConnectorConfig | undefined;
setConfig(config: ConnectorConfig | undefined): void;
setConnector(connector: Connector | undefined): void;
}

const privateStore = createStore<PrivateStore>((set) => ({
connector: undefined,
config: undefined,
setConfig: (config) => {
set({config});
},
setConnector: (connector) => {
set({connector});
},
}));

interface Store {
readonly route: Route;
readonly routeHistory: Route[];
Expand All @@ -39,6 +21,8 @@ interface Store {
readonly provider: WebLNProvider | undefined;
readonly currency: string | undefined;
readonly providerConfig: WebLNProviderConfig | undefined;
readonly connector: Connector | undefined;
readonly config: ConnectorConfig | undefined;

connect(config: ConnectorConfig): void;
disconnect(): void;
Expand Down Expand Up @@ -75,6 +59,8 @@ const store = createStore<Store>((set, get) => ({
invoice: undefined,
provider: undefined,
providerConfig: undefined,
connector: undefined,
config: undefined,
connect: async (config: ConnectorConfig) => {
set({
connecting: true,
Expand All @@ -84,9 +70,9 @@ const store = createStore<Store>((set, get) => ({
const connector = new connectors[config.connectorType](config);
const provider = await connector.init();
await provider.enable();
privateStore.getState().setConfig(config);
privateStore.getState().setConnector(connector);
set({
config,
connector,
connected: true,
connecting: false,
provider,
Expand All @@ -105,18 +91,17 @@ const store = createStore<Store>((set, get) => ({
}
},
disconnect: () => {
privateStore.getState().connector?.unload();
privateStore.getState().setConfig(undefined);
privateStore.getState().setConnector(undefined);
get().connector?.unload();
set({
config: undefined,
connector: undefined,
connected: false,
connectorName: undefined,
provider: undefined,
modalOpen: false,
});
deleteConfig();
},
getConnectorName: () => privateStore.getState().config?.connectorName,
// TODO: support passing route parameters as a second argument
pushRoute: (route: Route) => {
if (get().route === route) {
Expand Down Expand Up @@ -164,31 +149,6 @@ const store = createStore<Store>((set, get) => ({
}
set({currency});
},

/*async getBalance() {
try {
if (!window.webln) {
throw new Error('webln not found');
}
const balanceResponse = await window.webln.getBalance?.();
return balanceResponse?.balance;
} catch (error) {
console.error('Failed to get balance', error);
}
return undefined;
},
async getAlias() {
try {
if (!window.webln) {
throw new Error('webln not found');
}
const info = await window.webln.getInfo();
return info.node.alias;
} catch (error) {
console.error('Failed to get alias', error);
return undefined;
}
},*/
}));

export default store;
Expand Down

0 comments on commit 66aae43

Please sign in to comment.