Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat: lnc connector (WIP) #74

Merged
merged 3 commits into from
Oct 20, 2023
Merged
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
21 changes: 21 additions & 0 deletions CONTRIBUTING.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
# Contributing

## Adding a new Connector component

A connector component is a web component that will be displayed in the modal. You can customize the name, color, icon and functionality that will happen when the component is clicked.

See [existing connector components](src/components/connectors/index.ts)

Make sure to add your new connector component to the [connector list](src/components/bc-connector-list.ts)

## Adding a new connector

A connector exposes a WebLN object for the web application.

See [existing connectors](src/connectors/index.ts)

## Adding a new wrapper package

A wrapper package simplifies integration of Bitcoin Connect with different web frameworks.

See [React](/react)
1 change: 1 addition & 0 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -46,6 +46,7 @@
"license": "MIT",
"dependencies": {
"@getalby/sdk": "^2.4.0",
"@lightninglabs/lnc-web": "^0.2.6-alpha",
"lit": "^2.2.4",
"zustand": "^4.4.1"
},
Expand Down
1 change: 1 addition & 0 deletions src/components/bc-connector-list.ts
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@ export class ConnectorList extends withTwind()(BitcoinConnectElement) {
: null}
<bc-alby-nwc-connector></bc-alby-nwc-connector>
<bc-nwc-connector></bc-nwc-connector>
<bc-lnc-connector></bc-lnc-connector>
</div>`;
}
}
Expand Down
32 changes: 32 additions & 0 deletions src/components/connectors/bc-lnc-connector.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,32 @@
import {customElement} from 'lit/decorators.js';
import {puzzleIcon} from '../icons/puzzleIcon';
import {ConnectorElement} from './ConnectorElement';

@customElement('bc-lnc-connector')
export class LNCConnector extends ConnectorElement {
constructor() {
super(
'lnc',
'LNC',
'linear-gradient(180deg, #FF0000 63.72%, #00FFFF 95.24%)', // TODO: change
puzzleIcon // TODO: change
);
}

protected _onClick() {
// TODO: improve UX for entering pairing phrase, allow scanning QR code?
const pairingPhrase = window.prompt('Enter pairing phrase');
if (!pairingPhrase) {
return;
}
this._connect({
pairingPhrase,
});
}
}

declare global {
interface HTMLElementTagNameMap {
'bc-lnc-connector': LNCConnector;
}
}
1 change: 1 addition & 0 deletions src/components/connectors/index.ts
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
export * from './bc-extension-connector';
export * from './bc-alby-nwc-connector';
export * from './bc-generic-nwc-connector';
export * from './bc-lnc-connector';
2 changes: 2 additions & 0 deletions src/connectors/Connector.ts
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,8 @@ export abstract class Connector {
await window.webln.enable();
}

async unload() {}

async getBalance() {
try {
if (!window.webln) {
Expand Down
103 changes: 103 additions & 0 deletions src/connectors/LNCConnector.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,103 @@
import {Connector} from './Connector';
import {ConnectorConfig} from '../types/ConnectorConfig';
import {
GetBalanceResponse,
GetInfoResponse,
KeysendArgs,
LookupInvoiceArgs,
LookupInvoiceResponse,
MakeInvoiceResponse,
RequestInvoiceArgs,
SendPaymentResponse,
SignMessageResponse,
WebLNProvider,
WebLNRequestMethod,
} from '@webbtc/webln-types';
import LNC from '@lightninglabs/lnc-web';

export class LNCConnector extends Connector {
private _lnc: LNC;

constructor(config: ConnectorConfig) {
super(config);

if (!config.pairingPhrase) {
throw new Error('no pairing phrase provided');
}
this._lnc = new LNC({
pairingPhrase: this._config.pairingPhrase,
// TODO: add encryption password?
});
}

override async init() {
window.webln = new LNCWebLNProvider(this._lnc);

try {
await this._lnc.connect();
} catch (error) {
console.error(error);
this._lnc.disconnect();
throw error;
}

super.init();
}

override async unload(): Promise<void> {
this._lnc.disconnect();
await super.unload();
}
}

class LNCWebLNProvider implements WebLNProvider {
private _lnc: LNC;

constructor(lnc: LNC) {
this._lnc = lnc;
}
enable(): Promise<void> {
return Promise.resolve();
}
getInfo(): Promise<GetInfoResponse> {
throw new Error('Method not implemented.');
}
makeInvoice(
_args: string | number | RequestInvoiceArgs
): Promise<MakeInvoiceResponse> {
throw new Error('Method not implemented.');
}
sendPayment(_paymentRequest: string): Promise<SendPaymentResponse> {
throw new Error('Method not implemented.');
}
isEnabled(): boolean {
throw new Error('Method not implemented.');
}
async getBalance(): Promise<GetBalanceResponse> {
const balance = await this._lnc.lnd.lightning.channelBalance();
return {
balance: parseInt(balance.localBalance?.sat || '0'),
};
}

keysend(_args: KeysendArgs): Promise<SendPaymentResponse> {
throw new Error('Method not implemented.');
}
lnurl(
_lnurl: string
): Promise<{status: 'OK'} | {status: 'ERROR'; reason: string}> {
throw new Error('Method not implemented.');
}
lookupInvoice(_args: LookupInvoiceArgs): Promise<LookupInvoiceResponse> {
throw new Error('Method not implemented.');
}
request:
| ((method: WebLNRequestMethod, args?: unknown) => Promise<unknown>)
| undefined;
signMessage(_message: string): Promise<SignMessageResponse> {
throw new Error('Method not implemented.');
}
verifyMessage(_signature: string, _message: string): Promise<void> {
throw new Error('Method not implemented.');
}
}
2 changes: 2 additions & 0 deletions src/connectors/index.ts
Original file line number Diff line number Diff line change
@@ -1,8 +1,10 @@
import {ExtensionConnector} from './ExtensionConnector';
import {LNCConnector} from './LNCConnector';
import {NWCConnector} from './NWCConnector';

export const connectors = {
'extension.generic': ExtensionConnector,
'nwc.alby': NWCConnector,
'nwc.generic': NWCConnector,
lnc: LNCConnector,
};
12 changes: 7 additions & 5 deletions src/state/store.ts
Original file line number Diff line number Diff line change
Expand Up @@ -40,7 +40,7 @@ interface Store {
setAppName(appName: string): void;
}

const store = createStore<Store>((set) => ({
const store = createStore<Store>((set, get) => ({
route: '/start',
connected: false,
connecting: false,
Expand All @@ -53,13 +53,13 @@ const store = createStore<Store>((set) => ({
set({
connecting: true,
});
const connector = new connectors[config.connectorType](config);
privateStore.getState().setConnector(connector);
privateStore.getState().setConfig(config);
try {
const connector = new connectors[config.connectorType](config);
await connector.init();
const balance = await connector.getBalance();
const alias = await connector.getAlias();
privateStore.getState().setConfig(config);
privateStore.getState().setConnector(connector);
set({
connected: true,
connecting: false,
Expand All @@ -68,15 +68,17 @@ const store = createStore<Store>((set) => ({
connectorName: config.connectorName,
});
dispatchEvent('bc:connected');
saveConfig(config);
} catch (error) {
console.error(error);
set({
connecting: false,
});
get().disconnect();
}
saveConfig(config);
},
disconnect: () => {
privateStore.getState().connector?.unload();
privateStore.getState().setConfig(undefined);
privateStore.getState().setConnector(undefined);
set({
Expand Down
1 change: 1 addition & 0 deletions src/types/ConnectorConfig.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,4 +4,5 @@ export type ConnectorConfig = {
connectorName: string;
connectorType: ConnectorType;
nwcUrl?: string;
pairingPhrase?: string;
};
15 changes: 14 additions & 1 deletion yarn.lock
Original file line number Diff line number Diff line change
Expand Up @@ -1482,6 +1482,19 @@
"@jridgewell/resolve-uri" "3.1.0"
"@jridgewell/sourcemap-codec" "1.4.14"

"@lightninglabs/lnc-core@0.2.6-alpha":
version "0.2.6-alpha"
resolved "https://registry.yarnpkg.com/@lightninglabs/lnc-core/-/lnc-core-0.2.6-alpha.tgz#1b93d5aeefb09bb3dedcb82988368b15e223f8fd"
integrity sha512-bw2EQG78pPKMZMFwV+TR99RUbYgPVUKQYMLGGKIOvhPds3dBWSDZpMoqOyW/WidWGXF/ugPHzud8lDbKKhNXgA==

"@lightninglabs/lnc-web@^0.2.6-alpha":
version "0.2.6-alpha"
resolved "https://registry.yarnpkg.com/@lightninglabs/lnc-web/-/lnc-web-0.2.6-alpha.tgz#34f54f65691ff8cdef6eb159321b70c94f198f9e"
integrity sha512-SrqR8xaDnFLgNzPe5om7REOAhSOP95jQNIHP0GY0Lv895eDjrI6CPkfCFcX97INoDWYHBvDT8DZeYkBvlznVNA==
dependencies:
"@lightninglabs/lnc-core" "0.2.6-alpha"
crypto-js "4.1.1"

"@lit-labs/ssr-dom-shim@^1.0.0", "@lit-labs/ssr-dom-shim@^1.1.0":
version "1.1.1"
resolved "https://registry.yarnpkg.com/@lit-labs/ssr-dom-shim/-/ssr-dom-shim-1.1.1.tgz#64df34e2f12e68e78ac57e571d25ec07fa460ca9"
Expand Down Expand Up @@ -3128,7 +3141,7 @@ cross-spawn@^7.0.2:
shebang-command "^2.0.0"
which "^2.0.1"

crypto-js@^4.1.1:
crypto-js@4.1.1, crypto-js@^4.1.1:
version "4.1.1"
resolved "https://registry.yarnpkg.com/crypto-js/-/crypto-js-4.1.1.tgz#9e485bcf03521041bd85844786b83fb7619736cf"
integrity sha512-o2JlM7ydqd3Qk9CA0L4NL6mTzU2sdx96a+oOfPu8Mkl/PK51vSyoi8/rQ8NknZtk44vq15lmhAj9CIAGwgeWKw==
Expand Down