refactor: extract wallet system
continuous-integration/drone/push Build is running
Details
continuous-integration/drone/push Build is running
Details
This commit is contained in:
parent
d1095847d8
commit
8137317bfe
File diff suppressed because one or more lines are too long
File diff suppressed because one or more lines are too long
|
@ -1,4 +1,9 @@
|
||||||
yarnPath: .yarn/releases/yarn-3.6.3.cjs
|
compressionLevel: mixed
|
||||||
|
|
||||||
|
enableGlobalCache: false
|
||||||
|
|
||||||
npmScopes:
|
npmScopes:
|
||||||
"here":
|
here:
|
||||||
npmRegistryServer: "https://repo.platform.here.com/artifactory/api/npm/maps-api-for-javascript/"
|
npmRegistryServer: "https://repo.platform.here.com/artifactory/api/npm/maps-api-for-javascript/"
|
||||||
|
|
||||||
|
yarnPath: .yarn/releases/yarn-4.1.1.cjs
|
||||||
|
|
|
@ -4,7 +4,7 @@
|
||||||
"packages/*"
|
"packages/*"
|
||||||
],
|
],
|
||||||
"scripts": {
|
"scripts": {
|
||||||
"build": "yarn workspace @snort/shared build && yarn workspace @snort/worker-relay build && yarn workspace @snort/system build && yarn workspace @snort/system-web build && yarn workspace @snort/system-react build && yarn workspace @snort/app build",
|
"build": "yarn workspace @snort/shared build && yarn workspace @snort/wallet build && yarn workspace @snort/worker-relay build && yarn workspace @snort/system build && yarn workspace @snort/system-web build && yarn workspace @snort/system-react build && yarn workspace @snort/app build",
|
||||||
"start": "yarn build && yarn workspace @snort/app start",
|
"start": "yarn build && yarn workspace @snort/app start",
|
||||||
"test": "yarn build && yarn workspace @snort/app test && yarn workspace @snort/system test",
|
"test": "yarn build && yarn workspace @snort/app test && yarn workspace @snort/system test",
|
||||||
"pre:commit": "yarn workspace @snort/app intl-extract && yarn workspace @snort/app intl-compile && yarn prettier --write .",
|
"pre:commit": "yarn workspace @snort/app intl-extract && yarn workspace @snort/app intl-compile && yarn prettier --write .",
|
||||||
|
@ -18,7 +18,7 @@
|
||||||
"trailingComma": "all",
|
"trailingComma": "all",
|
||||||
"endOfLine": "lf"
|
"endOfLine": "lf"
|
||||||
},
|
},
|
||||||
"packageManager": "yarn@3.6.3",
|
"packageManager": "yarn@4.1.1",
|
||||||
"dependencies": {
|
"dependencies": {
|
||||||
"@cloudflare/workers-types": "^4.20230307.0",
|
"@cloudflare/workers-types": "^4.20230307.0",
|
||||||
"@tauri-apps/cli": "^1.2.3",
|
"@tauri-apps/cli": "^1.2.3",
|
||||||
|
|
|
@ -2,9 +2,7 @@
|
||||||
"name": "@snort/app",
|
"name": "@snort/app",
|
||||||
"version": "0.2.0",
|
"version": "0.2.0",
|
||||||
"dependencies": {
|
"dependencies": {
|
||||||
"@cashu/cashu-ts": "0.6.1",
|
|
||||||
"@here/maps-api-for-javascript": "^1.50.0",
|
"@here/maps-api-for-javascript": "^1.50.0",
|
||||||
"@lightninglabs/lnc-web": "^0.2.8-alpha",
|
|
||||||
"@noble/curves": "^1.0.0",
|
"@noble/curves": "^1.0.0",
|
||||||
"@noble/hashes": "^1.3.3",
|
"@noble/hashes": "^1.3.3",
|
||||||
"@scure/base": "^1.1.1",
|
"@scure/base": "^1.1.1",
|
||||||
|
@ -15,6 +13,7 @@
|
||||||
"@snort/system-react": "workspace:*",
|
"@snort/system-react": "workspace:*",
|
||||||
"@snort/system-wasm": "workspace:*",
|
"@snort/system-wasm": "workspace:*",
|
||||||
"@snort/system-web": "workspace:*",
|
"@snort/system-web": "workspace:*",
|
||||||
|
"@snort/wallet": "workspace:*",
|
||||||
"@snort/worker-relay": "workspace:*",
|
"@snort/worker-relay": "workspace:*",
|
||||||
"@szhsin/react-menu": "^3.3.1",
|
"@szhsin/react-menu": "^3.3.1",
|
||||||
"@uidotdev/usehooks": "^2.4.1",
|
"@uidotdev/usehooks": "^2.4.1",
|
||||||
|
@ -119,7 +118,7 @@
|
||||||
"tailwindcss": "^3.3.3",
|
"tailwindcss": "^3.3.3",
|
||||||
"tinybench": "^2.5.1",
|
"tinybench": "^2.5.1",
|
||||||
"typescript": "^5.2.2",
|
"typescript": "^5.2.2",
|
||||||
"vite": "^5.1.5",
|
"vite": "^5.2.8",
|
||||||
"vite-plugin-pwa": "^0.19.2",
|
"vite-plugin-pwa": "^0.19.2",
|
||||||
"vite-plugin-version-mark": "^0.0.10",
|
"vite-plugin-version-mark": "^0.0.10",
|
||||||
"vitest": "^0.34.6"
|
"vitest": "^0.34.6"
|
||||||
|
|
|
@ -1,5 +1,6 @@
|
||||||
import { LNURL } from "@snort/shared";
|
import { LNURL } from "@snort/shared";
|
||||||
import { NostrEvent } from "@snort/system";
|
import { NostrEvent } from "@snort/system";
|
||||||
|
import { WalletInvoiceState } from "@snort/wallet";
|
||||||
import { FormattedMessage, FormattedNumber } from "react-intl";
|
import { FormattedMessage, FormattedNumber } from "react-intl";
|
||||||
|
|
||||||
import { UserCache } from "@/Cache";
|
import { UserCache } from "@/Cache";
|
||||||
|
@ -10,7 +11,6 @@ import useEventPublisher from "@/Hooks/useEventPublisher";
|
||||||
import useLogin from "@/Hooks/useLogin";
|
import useLogin from "@/Hooks/useLogin";
|
||||||
import { dedupe, findTag, getDisplayName, hexToBech32 } from "@/Utils";
|
import { dedupe, findTag, getDisplayName, hexToBech32 } from "@/Utils";
|
||||||
import { useWallet } from "@/Wallet";
|
import { useWallet } from "@/Wallet";
|
||||||
import { WalletInvoiceState } from "@/Wallet";
|
|
||||||
|
|
||||||
export default function PubkeyList({ ev, className }: { ev: NostrEvent; className?: string }) {
|
export default function PubkeyList({ ev, className }: { ev: NostrEvent; className?: string }) {
|
||||||
const wallet = useWallet();
|
const wallet = useWallet();
|
||||||
|
|
|
@ -1,10 +1,10 @@
|
||||||
|
import { LNWallet } from "@snort/wallet";
|
||||||
import React, { ReactNode } from "react";
|
import React, { ReactNode } from "react";
|
||||||
import { FormattedMessage } from "react-intl";
|
import { FormattedMessage } from "react-intl";
|
||||||
|
|
||||||
import Copy from "@/Components/Copy/Copy";
|
import Copy from "@/Components/Copy/Copy";
|
||||||
import QrCode from "@/Components/QrCode";
|
import QrCode from "@/Components/QrCode";
|
||||||
import { ZapTargetResult } from "@/Utils/Zapper";
|
import { ZapTargetResult } from "@/Utils/Zapper";
|
||||||
import { LNWallet } from "@/Wallet";
|
|
||||||
|
|
||||||
export function ZapModalInvoice(props: {
|
export function ZapModalInvoice(props: {
|
||||||
invoice: Array<ZapTargetResult>;
|
invoice: Array<ZapTargetResult>;
|
||||||
|
|
|
@ -8,6 +8,7 @@ import BlueWallet from "@/Components/Icons/BlueWallet";
|
||||||
import Icon from "@/Components/Icons/Icon";
|
import Icon from "@/Components/Icons/Icon";
|
||||||
import NostrIcon from "@/Components/Icons/Nostrich";
|
import NostrIcon from "@/Components/Icons/Nostrich";
|
||||||
import { getAlbyOAuth } from "@/Pages/settings/wallet/utils";
|
import { getAlbyOAuth } from "@/Pages/settings/wallet/utils";
|
||||||
|
import CashuIcon from "@/Components/Icons/Cashu";
|
||||||
|
|
||||||
const WalletRow = (props: {
|
const WalletRow = (props: {
|
||||||
logo: ReactNode;
|
logo: ReactNode;
|
||||||
|
@ -70,12 +71,12 @@ const WalletSettings = () => {
|
||||||
url="/settings/wallet/lndhub"
|
url="/settings/wallet/lndhub"
|
||||||
desc={<FormattedMessage defaultMessage="Generic LNDHub wallet (BTCPayServer / Alby / LNBits)" id="0MndVW" />}
|
desc={<FormattedMessage defaultMessage="Generic LNDHub wallet (BTCPayServer / Alby / LNBits)" id="0MndVW" />}
|
||||||
/>
|
/>
|
||||||
{/*<WalletRow
|
<WalletRow
|
||||||
logo={<CashuIcon size={64} />}
|
logo={<CashuIcon size={64} />}
|
||||||
name="Cashu"
|
name="Cashu"
|
||||||
url="/settings/wallet/cashu"
|
url="/settings/wallet/cashu"
|
||||||
desc={<FormattedMessage defaultMessage="Cashu mint wallet" id="3natuV" />}
|
desc={<FormattedMessage defaultMessage="Cashu mint wallet" id="3natuV" />}
|
||||||
/>*/}
|
/>
|
||||||
{CONFIG.alby && (
|
{CONFIG.alby && (
|
||||||
<WalletRow
|
<WalletRow
|
||||||
logo={<AlbyIcon size={64} />}
|
logo={<AlbyIcon size={64} />}
|
||||||
|
|
|
@ -1,11 +1,11 @@
|
||||||
|
import { AlbyWallet, WalletKind } from "@snort/wallet";
|
||||||
import { useEffect, useState } from "react";
|
import { useEffect, useState } from "react";
|
||||||
import { useLocation, useNavigate } from "react-router-dom";
|
import { useLocation, useNavigate } from "react-router-dom";
|
||||||
import { v4 as uuid } from "uuid";
|
import { v4 as uuid } from "uuid";
|
||||||
|
|
||||||
import PageSpinner from "@/Components/PageSpinner";
|
import PageSpinner from "@/Components/PageSpinner";
|
||||||
import { getAlbyOAuth } from "@/Pages/settings/wallet/utils";
|
import { getAlbyOAuth } from "@/Pages/settings/wallet/utils";
|
||||||
import { WalletConfig, WalletKind, Wallets } from "@/Wallet";
|
import { WalletConfig, Wallets } from "@/Wallet";
|
||||||
import AlbyWallet from "@/Wallet/AlbyWallet";
|
|
||||||
|
|
||||||
export default function AlbyOAuth() {
|
export default function AlbyOAuth() {
|
||||||
const navigate = useNavigate();
|
const navigate = useNavigate();
|
||||||
|
@ -16,7 +16,7 @@ export default function AlbyOAuth() {
|
||||||
async function setupWallet(token: string) {
|
async function setupWallet(token: string) {
|
||||||
try {
|
try {
|
||||||
const auth = await alby.getToken(token);
|
const auth = await alby.getToken(token);
|
||||||
const connection = new AlbyWallet(auth, () => {});
|
const connection = new AlbyWallet(auth);
|
||||||
const info = await connection.getInfo();
|
const info = await connection.getInfo();
|
||||||
|
|
||||||
const newWallet = {
|
const newWallet = {
|
||||||
|
|
|
@ -1,3 +1,4 @@
|
||||||
|
import { CashuWallet, WalletKind } from "@snort/wallet";
|
||||||
import { useState } from "react";
|
import { useState } from "react";
|
||||||
import { FormattedMessage, useIntl } from "react-intl";
|
import { FormattedMessage, useIntl } from "react-intl";
|
||||||
import { useNavigate } from "react-router-dom";
|
import { useNavigate } from "react-router-dom";
|
||||||
|
@ -5,7 +6,7 @@ import { v4 as uuid } from "uuid";
|
||||||
|
|
||||||
import AsyncButton from "@/Components/Button/AsyncButton";
|
import AsyncButton from "@/Components/Button/AsyncButton";
|
||||||
import { unwrap } from "@/Utils";
|
import { unwrap } from "@/Utils";
|
||||||
import { WalletConfig, WalletKind, Wallets } from "@/Wallet";
|
import { WalletConfig, Wallets } from "@/Wallet";
|
||||||
|
|
||||||
const ConnectCashu = () => {
|
const ConnectCashu = () => {
|
||||||
const navigate = useNavigate();
|
const navigate = useNavigate();
|
||||||
|
@ -19,7 +20,6 @@ const ConnectCashu = () => {
|
||||||
throw new Error("Mint URL is required");
|
throw new Error("Mint URL is required");
|
||||||
}
|
}
|
||||||
|
|
||||||
const { CashuWallet } = await import("@/Wallet/Cashu");
|
|
||||||
const connection = new CashuWallet(
|
const connection = new CashuWallet(
|
||||||
{
|
{
|
||||||
url: config,
|
url: config,
|
||||||
|
@ -27,7 +27,6 @@ const ConnectCashu = () => {
|
||||||
proofs: [],
|
proofs: [],
|
||||||
keysets: [],
|
keysets: [],
|
||||||
},
|
},
|
||||||
() => {},
|
|
||||||
);
|
);
|
||||||
await connection.login();
|
await connection.login();
|
||||||
const info = await connection.getInfo();
|
const info = await connection.getInfo();
|
||||||
|
|
|
@ -1,3 +1,4 @@
|
||||||
|
import { LNCWallet, LNWallet, WalletInfo, WalletKind } from "@snort/wallet";
|
||||||
import { useState } from "react";
|
import { useState } from "react";
|
||||||
import { FormattedMessage, useIntl } from "react-intl";
|
import { FormattedMessage, useIntl } from "react-intl";
|
||||||
import { useNavigate } from "react-router-dom";
|
import { useNavigate } from "react-router-dom";
|
||||||
|
@ -5,7 +6,7 @@ import { v4 as uuid } from "uuid";
|
||||||
|
|
||||||
import AsyncButton from "@/Components/Button/AsyncButton";
|
import AsyncButton from "@/Components/Button/AsyncButton";
|
||||||
import { unwrap } from "@/Utils";
|
import { unwrap } from "@/Utils";
|
||||||
import { LNWallet, WalletInfo, WalletKind, Wallets } from "@/Wallet";
|
import { Wallets } from "@/Wallet";
|
||||||
|
|
||||||
const ConnectLNC = () => {
|
const ConnectLNC = () => {
|
||||||
const { formatMessage } = useIntl();
|
const { formatMessage } = useIntl();
|
||||||
|
@ -18,7 +19,6 @@ const ConnectLNC = () => {
|
||||||
|
|
||||||
async function tryConnect(cfg: string) {
|
async function tryConnect(cfg: string) {
|
||||||
try {
|
try {
|
||||||
const { LNCWallet } = await import("@/Wallet/LNCWallet");
|
|
||||||
const lnc = await LNCWallet.Initialize(cfg);
|
const lnc = await LNCWallet.Initialize(cfg);
|
||||||
const info = await lnc.getInfo();
|
const info = await lnc.getInfo();
|
||||||
|
|
||||||
|
|
|
@ -1,3 +1,4 @@
|
||||||
|
import { LNDHubWallet, WalletKind } from "@snort/wallet";
|
||||||
import { useState } from "react";
|
import { useState } from "react";
|
||||||
import { FormattedMessage, useIntl } from "react-intl";
|
import { FormattedMessage, useIntl } from "react-intl";
|
||||||
import { useNavigate } from "react-router-dom";
|
import { useNavigate } from "react-router-dom";
|
||||||
|
@ -5,8 +6,7 @@ import { v4 as uuid } from "uuid";
|
||||||
|
|
||||||
import AsyncButton from "@/Components/Button/AsyncButton";
|
import AsyncButton from "@/Components/Button/AsyncButton";
|
||||||
import { unwrap } from "@/Utils";
|
import { unwrap } from "@/Utils";
|
||||||
import { WalletConfig, WalletKind, Wallets } from "@/Wallet";
|
import { WalletConfig, Wallets } from "@/Wallet";
|
||||||
import LNDHubWallet from "@/Wallet/LNDHub";
|
|
||||||
|
|
||||||
const ConnectLNDHub = () => {
|
const ConnectLNDHub = () => {
|
||||||
const navigate = useNavigate();
|
const navigate = useNavigate();
|
||||||
|
@ -16,7 +16,7 @@ const ConnectLNDHub = () => {
|
||||||
|
|
||||||
async function tryConnect(config: string) {
|
async function tryConnect(config: string) {
|
||||||
try {
|
try {
|
||||||
const connection = new LNDHubWallet(config, () => {});
|
const connection = new LNDHubWallet(config);
|
||||||
await connection.login();
|
await connection.login();
|
||||||
const info = await connection.getInfo();
|
const info = await connection.getInfo();
|
||||||
|
|
||||||
|
|
|
@ -1,3 +1,4 @@
|
||||||
|
import { NostrConnectWallet, WalletKind } from "@snort/wallet";
|
||||||
import { useState } from "react";
|
import { useState } from "react";
|
||||||
import { FormattedMessage, useIntl } from "react-intl";
|
import { FormattedMessage, useIntl } from "react-intl";
|
||||||
import { Link, useNavigate } from "react-router-dom";
|
import { Link, useNavigate } from "react-router-dom";
|
||||||
|
@ -5,8 +6,7 @@ import { v4 as uuid } from "uuid";
|
||||||
|
|
||||||
import AsyncButton from "@/Components/Button/AsyncButton";
|
import AsyncButton from "@/Components/Button/AsyncButton";
|
||||||
import { unwrap } from "@/Utils";
|
import { unwrap } from "@/Utils";
|
||||||
import { WalletConfig, WalletKind, Wallets } from "@/Wallet";
|
import { WalletConfig, Wallets } from "@/Wallet";
|
||||||
import { NostrConnectWallet } from "@/Wallet/NostrWalletConnect";
|
|
||||||
|
|
||||||
const ConnectNostrWallet = () => {
|
const ConnectNostrWallet = () => {
|
||||||
const navigate = useNavigate();
|
const navigate = useNavigate();
|
||||||
|
@ -16,7 +16,7 @@ const ConnectNostrWallet = () => {
|
||||||
|
|
||||||
async function tryConnect(config: string) {
|
async function tryConnect(config: string) {
|
||||||
try {
|
try {
|
||||||
const connection = new NostrConnectWallet(config, () => {});
|
const connection = new NostrConnectWallet(config);
|
||||||
await connection.login();
|
await connection.login();
|
||||||
const info = await connection.getInfo();
|
const info = await connection.getInfo();
|
||||||
|
|
||||||
|
|
|
@ -59,7 +59,7 @@ export function getAlbyOAuth() {
|
||||||
|
|
||||||
const data = await req.json();
|
const data = await req.json();
|
||||||
if (req.ok) {
|
if (req.ok) {
|
||||||
return { ...data, created_at: unixNow() } as OAuthToken;
|
return { ...data, created_at: unixNow(), clientId, clientSecret } as OAuthToken;
|
||||||
} else {
|
} else {
|
||||||
throw new Error(data.error_description as string);
|
throw new Error(data.error_description as string);
|
||||||
}
|
}
|
||||||
|
@ -74,4 +74,6 @@ export interface OAuthToken {
|
||||||
refresh_token: string;
|
refresh_token: string;
|
||||||
scope: string;
|
scope: string;
|
||||||
token_type: string;
|
token_type: string;
|
||||||
|
clientId: string;
|
||||||
|
clientSecret: string;
|
||||||
}
|
}
|
||||||
|
|
|
@ -1,4 +1,5 @@
|
||||||
/* eslint-disable max-lines */
|
/* eslint-disable max-lines */
|
||||||
|
import { LNWallet,Sats, WalletInvoice } from "@snort/wallet";
|
||||||
import classNames from "classnames";
|
import classNames from "classnames";
|
||||||
import { useEffect, useState } from "react";
|
import { useEffect, useState } from "react";
|
||||||
import { FormattedMessage, FormattedNumber, useIntl } from "react-intl";
|
import { FormattedMessage, FormattedNumber, useIntl } from "react-intl";
|
||||||
|
@ -10,7 +11,7 @@ import NoteTime from "@/Components/Event/Note/NoteTime";
|
||||||
import Icon from "@/Components/Icons/Icon";
|
import Icon from "@/Components/Icons/Icon";
|
||||||
import { useRates } from "@/Hooks/useRates";
|
import { useRates } from "@/Hooks/useRates";
|
||||||
import { unwrap } from "@/Utils";
|
import { unwrap } from "@/Utils";
|
||||||
import { LNWallet, Sats, useWallet, WalletInvoice, Wallets } from "@/Wallet";
|
import { useWallet, Wallets } from "@/Wallet";
|
||||||
|
|
||||||
export default function WalletPage(props: { showHistory: boolean }) {
|
export default function WalletPage(props: { showHistory: boolean }) {
|
||||||
const navigate = useNavigate();
|
const navigate = useNavigate();
|
||||||
|
|
|
@ -1,11 +1,12 @@
|
||||||
import { ExternalStore, LNURL, unixNow } from "@snort/shared";
|
import { ExternalStore, LNURL, unixNow } from "@snort/shared";
|
||||||
|
import { LNWallet, WalletInvoiceState } from "@snort/wallet";
|
||||||
import debug from "debug";
|
import debug from "debug";
|
||||||
|
|
||||||
import { UserCache } from "@/Cache";
|
import { UserCache } from "@/Cache";
|
||||||
import { Toastore } from "@/Components/Toaster/Toaster";
|
import { Toastore } from "@/Components/Toaster/Toaster";
|
||||||
import { SnortPubKey } from "@/Utils/Const";
|
import { SnortPubKey } from "@/Utils/Const";
|
||||||
import { bech32ToHex, getDisplayName, trackEvent } from "@/Utils/index";
|
import { bech32ToHex, getDisplayName, trackEvent } from "@/Utils/index";
|
||||||
import { LNWallet, WalletInvoiceState, Wallets } from "@/Wallet";
|
import { Wallets } from "@/Wallet";
|
||||||
|
|
||||||
export enum ZapPoolRecipientType {
|
export enum ZapPoolRecipientType {
|
||||||
Generic = 0,
|
Generic = 0,
|
||||||
|
|
|
@ -1,8 +1,8 @@
|
||||||
import { isHex, LNURL } from "@snort/shared";
|
import { isHex, LNURL } from "@snort/shared";
|
||||||
import { EventPublisher, NostrEvent, NostrLink, SystemInterface } from "@snort/system";
|
import { EventPublisher, NostrEvent, NostrLink, SystemInterface } from "@snort/system";
|
||||||
|
import { LNWallet, WalletInvoiceState } from "@snort/wallet";
|
||||||
|
|
||||||
import { generateRandomKey } from "@/Utils/Login";
|
import { generateRandomKey } from "@/Utils/Login";
|
||||||
import { LNWallet, WalletInvoiceState } from "@/Wallet";
|
|
||||||
|
|
||||||
export interface ZapTarget {
|
export interface ZapTarget {
|
||||||
type: "lnurl" | "pubkey";
|
type: "lnurl" | "pubkey";
|
||||||
|
|
|
@ -1,288 +1,179 @@
|
||||||
import { decodeInvoice, ExternalStore } from "@snort/shared";
|
import { ExternalStore, unwrap } from "@snort/shared";
|
||||||
|
import { LNWallet, loadWallet, WalletInfo, WalletKind } from "@snort/wallet";
|
||||||
import { useEffect, useSyncExternalStore } from "react";
|
import { useEffect, useSyncExternalStore } from "react";
|
||||||
|
|
||||||
import { unwrap } from "@/Utils";
|
|
||||||
|
|
||||||
import AlbyWallet from "./AlbyWallet";
|
|
||||||
import LNDHubWallet from "./LNDHub";
|
|
||||||
import { NostrConnectWallet } from "./NostrWalletConnect";
|
|
||||||
import { WebLNWallet } from "./WebLN";
|
|
||||||
|
|
||||||
export enum WalletKind {
|
|
||||||
LNDHub = 1,
|
|
||||||
LNC = 2,
|
|
||||||
WebLN = 3,
|
|
||||||
NWC = 4,
|
|
||||||
Cashu = 5,
|
|
||||||
Alby = 6,
|
|
||||||
}
|
|
||||||
|
|
||||||
export enum WalletErrorCode {
|
|
||||||
BadAuth = 1,
|
|
||||||
NotEnoughBalance = 2,
|
|
||||||
BadPartner = 3,
|
|
||||||
InvalidInvoice = 4,
|
|
||||||
RouteNotFound = 5,
|
|
||||||
GeneralError = 6,
|
|
||||||
NodeFailure = 7,
|
|
||||||
}
|
|
||||||
|
|
||||||
export class WalletError extends Error {
|
|
||||||
code: WalletErrorCode;
|
|
||||||
|
|
||||||
constructor(c: WalletErrorCode, msg: string) {
|
|
||||||
super(msg);
|
|
||||||
this.code = c;
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
export const UnknownWalletError = {
|
|
||||||
code: WalletErrorCode.GeneralError,
|
|
||||||
message: "Unknown error",
|
|
||||||
} as WalletError;
|
|
||||||
|
|
||||||
export interface WalletInfo {
|
|
||||||
fee: number;
|
|
||||||
nodePubKey: string;
|
|
||||||
alias: string;
|
|
||||||
pendingChannels: number;
|
|
||||||
activeChannels: number;
|
|
||||||
peers: number;
|
|
||||||
blockHeight: number;
|
|
||||||
blockHash: string;
|
|
||||||
synced: boolean;
|
|
||||||
chains: string[];
|
|
||||||
version: string;
|
|
||||||
}
|
|
||||||
|
|
||||||
export interface Login {
|
|
||||||
service: string;
|
|
||||||
save: () => Promise<void>;
|
|
||||||
load: () => Promise<void>;
|
|
||||||
}
|
|
||||||
|
|
||||||
export interface InvoiceRequest {
|
|
||||||
amount: Sats;
|
|
||||||
memo?: string;
|
|
||||||
expiry?: number;
|
|
||||||
}
|
|
||||||
|
|
||||||
export enum WalletInvoiceState {
|
|
||||||
Pending = 0,
|
|
||||||
Paid = 1,
|
|
||||||
Expired = 2,
|
|
||||||
Failed = 3,
|
|
||||||
}
|
|
||||||
|
|
||||||
export interface WalletInvoice {
|
|
||||||
pr: string;
|
|
||||||
paymentHash: string;
|
|
||||||
memo: string;
|
|
||||||
amount: MilliSats;
|
|
||||||
fees: number;
|
|
||||||
timestamp: number;
|
|
||||||
preimage?: string;
|
|
||||||
state: WalletInvoiceState;
|
|
||||||
direction: "in" | "out";
|
|
||||||
}
|
|
||||||
|
|
||||||
export function prToWalletInvoice(pr: string) {
|
|
||||||
const parsedInvoice = decodeInvoice(pr);
|
|
||||||
if (parsedInvoice) {
|
|
||||||
return {
|
|
||||||
amount: parsedInvoice.amount ?? 0,
|
|
||||||
memo: parsedInvoice.description,
|
|
||||||
paymentHash: parsedInvoice.paymentHash ?? "",
|
|
||||||
timestamp: parsedInvoice.timestamp ?? 0,
|
|
||||||
state: parsedInvoice.expired ? WalletInvoiceState.Expired : WalletInvoiceState.Pending,
|
|
||||||
pr,
|
|
||||||
direction: "in",
|
|
||||||
} as WalletInvoice;
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
export type Sats = number;
|
|
||||||
export type MilliSats = number;
|
|
||||||
|
|
||||||
export interface LNWallet {
|
|
||||||
isReady(): boolean;
|
|
||||||
getInfo: () => Promise<WalletInfo>;
|
|
||||||
login: (password?: string) => Promise<boolean>;
|
|
||||||
close: () => Promise<boolean>;
|
|
||||||
getBalance: () => Promise<Sats>;
|
|
||||||
createInvoice: (req: InvoiceRequest) => Promise<WalletInvoice>;
|
|
||||||
payInvoice: (pr: string) => Promise<WalletInvoice>;
|
|
||||||
getInvoices: () => Promise<WalletInvoice[]>;
|
|
||||||
|
|
||||||
canAutoLogin: () => boolean;
|
|
||||||
canGetInvoices: () => boolean;
|
|
||||||
canGetBalance: () => boolean;
|
|
||||||
canCreateInvoice: () => boolean;
|
|
||||||
canPayInvoice: () => boolean;
|
|
||||||
}
|
|
||||||
|
|
||||||
export interface WalletConfig {
|
export interface WalletConfig {
|
||||||
id: string;
|
id: string;
|
||||||
kind: WalletKind;
|
kind: WalletKind;
|
||||||
active: boolean;
|
active: boolean;
|
||||||
info: WalletInfo;
|
info: WalletInfo;
|
||||||
data?: string;
|
|
||||||
|
/**
|
||||||
|
* Opaque string for wallet config
|
||||||
|
*/
|
||||||
|
data?: string;
|
||||||
}
|
}
|
||||||
|
|
||||||
export interface WalletStoreSnapshot {
|
export interface WalletStoreSnapshot {
|
||||||
configs: Array<WalletConfig>;
|
configs: Array<WalletConfig>;
|
||||||
config?: WalletConfig;
|
config?: WalletConfig;
|
||||||
wallet?: LNWallet;
|
wallet?: LNWallet;
|
||||||
}
|
}
|
||||||
|
|
||||||
export class WalletStore extends ExternalStore<WalletStoreSnapshot> {
|
export class WalletStore extends ExternalStore<WalletStoreSnapshot> {
|
||||||
#configs: Array<WalletConfig>;
|
#configs: Array<WalletConfig>;
|
||||||
#instance: Map<string, LNWallet>;
|
#instance: Map<string, LNWallet>;
|
||||||
|
|
||||||
constructor() {
|
constructor() {
|
||||||
super();
|
super();
|
||||||
this.#configs = [];
|
this.#configs = [];
|
||||||
this.#instance = new Map();
|
this.#instance = new Map();
|
||||||
this.load(false);
|
this.load(false);
|
||||||
this.notifyChange();
|
this.notifyChange();
|
||||||
}
|
|
||||||
|
|
||||||
list() {
|
|
||||||
return Object.freeze([...this.#configs]);
|
|
||||||
}
|
|
||||||
|
|
||||||
get() {
|
|
||||||
const activeConfig = this.#configs.find(a => a.active);
|
|
||||||
if (!activeConfig) {
|
|
||||||
if (this.#configs.length === 0) {
|
|
||||||
return undefined;
|
|
||||||
}
|
|
||||||
throw new Error("No active wallet config");
|
|
||||||
}
|
}
|
||||||
if (this.#instance.has(activeConfig.id)) {
|
|
||||||
return unwrap(this.#instance.get(activeConfig.id));
|
list() {
|
||||||
} else {
|
return Object.freeze([...this.#configs]);
|
||||||
const w = this.#activateWallet(activeConfig);
|
}
|
||||||
if (w) {
|
|
||||||
if ("then" in w) {
|
get() {
|
||||||
w.then(async wx => {
|
const activeConfig = this.#configs.find(a => a.active);
|
||||||
this.#instance.set(activeConfig.id, wx);
|
if (!activeConfig) {
|
||||||
this.notifyChange();
|
if (this.#configs.length === 0) {
|
||||||
});
|
return undefined;
|
||||||
return undefined;
|
}
|
||||||
|
throw new Error("No active wallet config");
|
||||||
|
}
|
||||||
|
if (this.#instance.has(activeConfig.id)) {
|
||||||
|
return unwrap(this.#instance.get(activeConfig.id));
|
||||||
} else {
|
} else {
|
||||||
this.#instance.set(activeConfig.id, w);
|
const w = this.#activateWallet(activeConfig);
|
||||||
this.notifyChange();
|
if (w) {
|
||||||
|
if ("then" in w) {
|
||||||
|
w.then(async wx => {
|
||||||
|
this.#instance.set(activeConfig.id, wx);
|
||||||
|
this.notifyChange();
|
||||||
|
});
|
||||||
|
return undefined;
|
||||||
|
} else {
|
||||||
|
this.#instance.set(activeConfig.id, w);
|
||||||
|
this.notifyChange();
|
||||||
|
}
|
||||||
|
return w;
|
||||||
|
} else {
|
||||||
|
throw new Error("Unable to activate wallet config");
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
add(cfg: WalletConfig) {
|
||||||
|
this.#configs.push(cfg);
|
||||||
|
this.save();
|
||||||
|
}
|
||||||
|
|
||||||
|
remove(id: string) {
|
||||||
|
const idx = this.#configs.findIndex(a => a.id === id);
|
||||||
|
if (idx === -1) {
|
||||||
|
throw new Error("Wallet not found");
|
||||||
|
}
|
||||||
|
const [removed] = this.#configs.splice(idx, 1);
|
||||||
|
if (removed.active && this.#configs.length > 0) {
|
||||||
|
this.#configs[0].active = true;
|
||||||
|
}
|
||||||
|
this.save();
|
||||||
|
}
|
||||||
|
|
||||||
|
switch(id: string) {
|
||||||
|
this.#configs.forEach(a => (a.active = a.id === id));
|
||||||
|
this.save();
|
||||||
|
}
|
||||||
|
|
||||||
|
save() {
|
||||||
|
const json = JSON.stringify(this.#configs);
|
||||||
|
window.localStorage.setItem("wallet-config", json);
|
||||||
|
this.notifyChange();
|
||||||
|
}
|
||||||
|
|
||||||
|
load(snapshot = true) {
|
||||||
|
const cfg = window.localStorage.getItem("wallet-config");
|
||||||
|
if (cfg) {
|
||||||
|
this.#configs = JSON.parse(cfg);
|
||||||
|
}
|
||||||
|
if (snapshot) {
|
||||||
|
this.notifyChange();
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
free() {
|
||||||
|
this.#instance.forEach(w => w.close());
|
||||||
|
}
|
||||||
|
|
||||||
|
takeSnapshot(): WalletStoreSnapshot {
|
||||||
|
return {
|
||||||
|
configs: [...this.#configs],
|
||||||
|
config: this.#configs.find(a => a.active),
|
||||||
|
wallet: this.get(),
|
||||||
|
} as WalletStoreSnapshot;
|
||||||
|
}
|
||||||
|
|
||||||
|
#activateWallet(cfg: WalletConfig): LNWallet | Promise<LNWallet> | undefined {
|
||||||
|
const w = loadWallet(cfg.kind, cfg.data);
|
||||||
|
if (w) {
|
||||||
|
w.on("change", d => this.#onWalletChange(cfg, d));
|
||||||
}
|
}
|
||||||
return w;
|
return w;
|
||||||
} else {
|
|
||||||
throw new Error("Unable to activate wallet config");
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
}
|
|
||||||
|
|
||||||
add(cfg: WalletConfig) {
|
#onWalletChange(cfg: WalletConfig, data?: string) {
|
||||||
this.#configs.push(cfg);
|
if (data) {
|
||||||
this.save();
|
const activeConfig = this.#configs.find(a => a.id === cfg.id);
|
||||||
}
|
if (activeConfig) {
|
||||||
|
activeConfig.data = data;
|
||||||
remove(id: string) {
|
}
|
||||||
const idx = this.#configs.findIndex(a => a.id === id);
|
this.save();
|
||||||
if (idx === -1) {
|
} else {
|
||||||
throw new Error("Wallet not found");
|
this.notifyChange();
|
||||||
|
}
|
||||||
}
|
}
|
||||||
const [removed] = this.#configs.splice(idx, 1);
|
|
||||||
if (removed.active && this.#configs.length > 0) {
|
|
||||||
this.#configs[0].active = true;
|
|
||||||
}
|
|
||||||
this.save();
|
|
||||||
}
|
|
||||||
|
|
||||||
switch(id: string) {
|
|
||||||
this.#configs.forEach(a => (a.active = a.id === id));
|
|
||||||
this.save();
|
|
||||||
}
|
|
||||||
|
|
||||||
save() {
|
|
||||||
const json = JSON.stringify(this.#configs);
|
|
||||||
window.localStorage.setItem("wallet-config", json);
|
|
||||||
this.notifyChange();
|
|
||||||
}
|
|
||||||
|
|
||||||
load(snapshot = true) {
|
|
||||||
const cfg = window.localStorage.getItem("wallet-config");
|
|
||||||
if (cfg) {
|
|
||||||
this.#configs = JSON.parse(cfg);
|
|
||||||
}
|
|
||||||
if (snapshot) {
|
|
||||||
this.notifyChange();
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
free() {
|
|
||||||
this.#instance.forEach(w => w.close());
|
|
||||||
}
|
|
||||||
|
|
||||||
takeSnapshot(): WalletStoreSnapshot {
|
|
||||||
return {
|
|
||||||
configs: [...this.#configs],
|
|
||||||
config: this.#configs.find(a => a.active),
|
|
||||||
wallet: this.get(),
|
|
||||||
} as WalletStoreSnapshot;
|
|
||||||
}
|
|
||||||
|
|
||||||
#activateWallet(cfg: WalletConfig): LNWallet | Promise<LNWallet> | undefined {
|
|
||||||
switch (cfg.kind) {
|
|
||||||
case WalletKind.LNC: {
|
|
||||||
return import("./LNCWallet").then(({ LNCWallet }) => LNCWallet.Empty());
|
|
||||||
}
|
|
||||||
case WalletKind.WebLN: {
|
|
||||||
return new WebLNWallet();
|
|
||||||
}
|
|
||||||
case WalletKind.LNDHub: {
|
|
||||||
return new LNDHubWallet(unwrap(cfg.data), d => this.#onWalletChange(cfg, d));
|
|
||||||
}
|
|
||||||
case WalletKind.NWC: {
|
|
||||||
return new NostrConnectWallet(unwrap(cfg.data), d => this.#onWalletChange(cfg, d));
|
|
||||||
}
|
|
||||||
case WalletKind.Alby: {
|
|
||||||
return new AlbyWallet(JSON.parse(unwrap(cfg.data)), d => this.#onWalletChange(cfg, d));
|
|
||||||
}
|
|
||||||
case WalletKind.Cashu: {
|
|
||||||
return import("./Cashu").then(
|
|
||||||
({ CashuWallet }) => new CashuWallet(JSON.parse(unwrap(cfg.data)), d => this.#onWalletChange(cfg, d)),
|
|
||||||
);
|
|
||||||
}
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
#onWalletChange(cfg: WalletConfig, data?: object) {
|
|
||||||
if (data) {
|
|
||||||
const activeConfig = this.#configs.find(a => a.id === cfg.id);
|
|
||||||
if (activeConfig) {
|
|
||||||
activeConfig.data = JSON.stringify(data);
|
|
||||||
}
|
|
||||||
this.save();
|
|
||||||
} else {
|
|
||||||
this.notifyChange();
|
|
||||||
}
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
|
|
||||||
export const Wallets = new WalletStore();
|
export const Wallets = new WalletStore();
|
||||||
window.document.addEventListener("close", () => {
|
window.document.addEventListener("close", () => {
|
||||||
Wallets.free();
|
Wallets.free();
|
||||||
});
|
});
|
||||||
|
|
||||||
export function useWallet() {
|
export function useWallet() {
|
||||||
const wallet = useSyncExternalStore<WalletStoreSnapshot>(
|
const wallet = useSyncExternalStore<WalletStoreSnapshot>(
|
||||||
h => Wallets.hook(h),
|
h => Wallets.hook(h),
|
||||||
() => Wallets.snapshot(),
|
() => Wallets.snapshot(),
|
||||||
);
|
);
|
||||||
useEffect(() => {
|
useEffect(() => {
|
||||||
if (wallet.wallet?.isReady() === false && wallet.wallet.canAutoLogin()) {
|
if (wallet.wallet?.isReady() === false && wallet.wallet.canAutoLogin()) {
|
||||||
wallet.wallet.login().catch(console.error);
|
wallet.wallet.login().catch(console.error);
|
||||||
}
|
}
|
||||||
}, [wallet]);
|
}, [wallet]);
|
||||||
return wallet;
|
return wallet;
|
||||||
|
}
|
||||||
|
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Adds a wallet config for WebLN if detected
|
||||||
|
*/
|
||||||
|
export function setupWebLNWalletConfig(store: WalletStore) {
|
||||||
|
const wallets = store.list();
|
||||||
|
|
||||||
|
const existing = wallets.find(a => a.kind === WalletKind.WebLN);
|
||||||
|
if (window.webln && !existing) {
|
||||||
|
const newConfig = {
|
||||||
|
id: "webln",
|
||||||
|
kind: WalletKind.WebLN,
|
||||||
|
active: wallets.length === 0,
|
||||||
|
info: {
|
||||||
|
alias: "WebLN",
|
||||||
|
},
|
||||||
|
} as WalletConfig;
|
||||||
|
store.add(newConfig);
|
||||||
|
} else if (existing) {
|
||||||
|
store.remove(existing.id);
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
|
@ -42,7 +42,7 @@ import { storeRefCode, unwrap } from "@/Utils";
|
||||||
import { LoginStore } from "@/Utils/Login";
|
import { LoginStore } from "@/Utils/Login";
|
||||||
import { hasWasm, wasmInit, WasmPath } from "@/Utils/wasm";
|
import { hasWasm, wasmInit, WasmPath } from "@/Utils/wasm";
|
||||||
import { Wallets } from "@/Wallet";
|
import { Wallets } from "@/Wallet";
|
||||||
import { setupWebLNWalletConfig } from "@/Wallet/WebLN";
|
import { setupWebLNWalletConfig } from "@/Wallet";
|
||||||
|
|
||||||
async function initSite() {
|
async function initSite() {
|
||||||
storeRefCode();
|
storeRefCode();
|
||||||
|
|
|
@ -109,7 +109,6 @@ export abstract class BackgroundLoader<T extends { loaded: number; created: numb
|
||||||
}
|
}
|
||||||
} catch (e) {
|
} catch (e) {
|
||||||
this.#log("Error: %O", e);
|
this.#log("Error: %O", e);
|
||||||
debugger;
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
|
@ -0,0 +1 @@
|
||||||
|
# wallet
|
|
@ -0,0 +1,35 @@
|
||||||
|
{
|
||||||
|
"name": "@snort/wallet",
|
||||||
|
"version": "0.1.0",
|
||||||
|
"description": "Snort wallet system package",
|
||||||
|
"type": "module",
|
||||||
|
"main": "dist/index.js",
|
||||||
|
"types": "dist/index.d.ts",
|
||||||
|
"module": "src/index.ts",
|
||||||
|
"repository": "https://git.v0l.io/Kieran/snort",
|
||||||
|
"author": "v0l",
|
||||||
|
"license": "MIT",
|
||||||
|
"scripts": {
|
||||||
|
"build": "rm -rf dist && tsc",
|
||||||
|
"test": "jest --runInBand"
|
||||||
|
},
|
||||||
|
"files": [
|
||||||
|
"src",
|
||||||
|
"dist"
|
||||||
|
],
|
||||||
|
"packageManager": "yarn@4.1.1",
|
||||||
|
"dependencies": {
|
||||||
|
"@cashu/cashu-ts": "^1.0.0-rc.3",
|
||||||
|
"@lightninglabs/lnc-web": "^0.3.1-alpha",
|
||||||
|
"@scure/base": "^1.1.6",
|
||||||
|
"@snort/shared": "workspace:^",
|
||||||
|
"@snort/system": "workspace:^",
|
||||||
|
"debug": "^4.3.4",
|
||||||
|
"eventemitter3": "^5.0.1"
|
||||||
|
},
|
||||||
|
"devDependencies": {
|
||||||
|
"@types/debug": "^4.1.12",
|
||||||
|
"@webbtc/webln-types": "^3.0.0",
|
||||||
|
"typescript": "^5.4.5"
|
||||||
|
}
|
||||||
|
}
|
|
@ -1,25 +1,34 @@
|
||||||
import { base64 } from "@scure/base";
|
import { base64 } from "@scure/base";
|
||||||
import { unixNow, unwrap } from "@snort/shared";
|
import { unixNow, unwrap } from "@snort/shared";
|
||||||
|
|
||||||
import { OAuthToken } from "@/Pages/settings/wallet/utils";
|
|
||||||
|
|
||||||
import {
|
import {
|
||||||
InvoiceRequest,
|
InvoiceRequest,
|
||||||
LNWallet,
|
LNWallet,
|
||||||
prToWalletInvoice,
|
prToWalletInvoice,
|
||||||
WalletError,
|
WalletError,
|
||||||
WalletErrorCode,
|
WalletErrorCode,
|
||||||
|
WalletEvents,
|
||||||
WalletInfo,
|
WalletInfo,
|
||||||
WalletInvoice,
|
WalletInvoice,
|
||||||
WalletInvoiceState,
|
WalletInvoiceState,
|
||||||
} from ".";
|
} from ".";
|
||||||
|
import EventEmitter from "eventemitter3";
|
||||||
|
|
||||||
export default class AlbyWallet implements LNWallet {
|
export interface OAuthToken {
|
||||||
|
access_token: string;
|
||||||
|
created_at: number;
|
||||||
|
expires_in: number;
|
||||||
|
refresh_token: string;
|
||||||
|
scope: string;
|
||||||
|
token_type: string;
|
||||||
|
clientId: string;
|
||||||
|
clientSecret: string;
|
||||||
|
}
|
||||||
|
|
||||||
|
export default class AlbyWallet extends EventEmitter<WalletEvents> implements LNWallet {
|
||||||
#token: OAuthToken;
|
#token: OAuthToken;
|
||||||
constructor(
|
constructor(token: OAuthToken) {
|
||||||
token: OAuthToken,
|
super();
|
||||||
readonly onChange: (data?: object) => void,
|
|
||||||
) {
|
|
||||||
this.#token = token;
|
this.#token = token;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -136,7 +145,7 @@ export default class AlbyWallet implements LNWallet {
|
||||||
accept: "application/json",
|
accept: "application/json",
|
||||||
"content-type": "application/x-www-form-urlencoded",
|
"content-type": "application/x-www-form-urlencoded",
|
||||||
authorization: `Basic ${base64.encode(
|
authorization: `Basic ${base64.encode(
|
||||||
new TextEncoder().encode(`${CONFIG.alby?.clientId}:${CONFIG.alby?.clientSecret}`),
|
new TextEncoder().encode(`${this.#token.clientId}:${this.#token.clientSecret}`),
|
||||||
)}`,
|
)}`,
|
||||||
},
|
},
|
||||||
});
|
});
|
||||||
|
@ -146,7 +155,7 @@ export default class AlbyWallet implements LNWallet {
|
||||||
...(json as OAuthToken),
|
...(json as OAuthToken),
|
||||||
created_at: unixNow(),
|
created_at: unixNow(),
|
||||||
};
|
};
|
||||||
this.onChange(this.#token);
|
this.emit("change", JSON.stringify(this.#token));
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
|
@ -1,6 +1,7 @@
|
||||||
import { CashuMint, Proof } from "@cashu/cashu-ts";
|
import { CashuMint, Proof } from "@cashu/cashu-ts";
|
||||||
|
|
||||||
import { InvoiceRequest, LNWallet, WalletInfo, WalletInvoice } from "@/Wallet";
|
import { InvoiceRequest, LNWallet, WalletEvents, WalletInfo, WalletInvoice } from ".";
|
||||||
|
import EventEmitter from "eventemitter3";
|
||||||
|
|
||||||
export type CashuWalletConfig = {
|
export type CashuWalletConfig = {
|
||||||
url: string;
|
url: string;
|
||||||
|
@ -9,14 +10,12 @@ export type CashuWalletConfig = {
|
||||||
proofs: Array<Proof>;
|
proofs: Array<Proof>;
|
||||||
};
|
};
|
||||||
|
|
||||||
export class CashuWallet implements LNWallet {
|
export class CashuWallet extends EventEmitter<WalletEvents> implements LNWallet {
|
||||||
#wallet: CashuWalletConfig;
|
#wallet: CashuWalletConfig;
|
||||||
#mint: CashuMint;
|
#mint: CashuMint;
|
||||||
|
|
||||||
constructor(
|
constructor(wallet: CashuWalletConfig) {
|
||||||
wallet: CashuWalletConfig,
|
super();
|
||||||
readonly onChange: (data?: object) => void,
|
|
||||||
) {
|
|
||||||
this.#wallet = wallet;
|
this.#wallet = wallet;
|
||||||
this.#mint = new CashuMint(this.#wallet.url);
|
this.#mint = new CashuMint(this.#wallet.url);
|
||||||
}
|
}
|
||||||
|
@ -99,7 +98,7 @@ export class CashuWallet implements LNWallet {
|
||||||
const filteredProofs = this.#wallet.proofs.filter((_, i) => checks.spendable[i]);
|
const filteredProofs = this.#wallet.proofs.filter((_, i) => checks.spendable[i]);
|
||||||
this.#wallet.proofs = filteredProofs;
|
this.#wallet.proofs = filteredProofs;
|
||||||
if (filteredProofs.length !== checks.spendable.length) {
|
if (filteredProofs.length !== checks.spendable.length) {
|
||||||
this.onChange(this.#wallet);
|
this.emit("change", JSON.stringify(this.#wallet));
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
|
@ -1,7 +1,6 @@
|
||||||
import LNC from "@lightninglabs/lnc-web";
|
import LNC from "@lightninglabs/lnc-web";
|
||||||
import debug from "debug";
|
import debug from "debug";
|
||||||
|
|
||||||
import { unwrap } from "@/Utils";
|
|
||||||
import {
|
import {
|
||||||
InvoiceRequest,
|
InvoiceRequest,
|
||||||
LNWallet,
|
LNWallet,
|
||||||
|
@ -9,10 +8,13 @@ import {
|
||||||
prToWalletInvoice,
|
prToWalletInvoice,
|
||||||
WalletError,
|
WalletError,
|
||||||
WalletErrorCode,
|
WalletErrorCode,
|
||||||
|
WalletEvents,
|
||||||
WalletInfo,
|
WalletInfo,
|
||||||
WalletInvoice,
|
WalletInvoice,
|
||||||
WalletInvoiceState,
|
WalletInvoiceState,
|
||||||
} from "@/Wallet";
|
} from ".";
|
||||||
|
import { unwrap } from "@snort/shared";
|
||||||
|
import EventEmitter from "eventemitter3";
|
||||||
|
|
||||||
enum Payment_PaymentStatus {
|
enum Payment_PaymentStatus {
|
||||||
UNKNOWN = "UNKNOWN",
|
UNKNOWN = "UNKNOWN",
|
||||||
|
@ -22,11 +24,12 @@ enum Payment_PaymentStatus {
|
||||||
UNRECOGNIZED = "UNRECOGNIZED",
|
UNRECOGNIZED = "UNRECOGNIZED",
|
||||||
}
|
}
|
||||||
|
|
||||||
export class LNCWallet implements LNWallet {
|
export class LNCWallet extends EventEmitter<WalletEvents> implements LNWallet {
|
||||||
#lnc: LNC;
|
#lnc: LNC;
|
||||||
readonly #log = debug("LNC");
|
readonly #log = debug("LNC");
|
||||||
|
|
||||||
private constructor(pairingPhrase?: string, password?: string) {
|
private constructor(pairingPhrase?: string, password?: string) {
|
||||||
|
super();
|
||||||
this.#lnc = new LNC({
|
this.#lnc = new LNC({
|
||||||
pairingPhrase,
|
pairingPhrase,
|
||||||
password,
|
password,
|
|
@ -7,27 +7,27 @@ import {
|
||||||
Sats,
|
Sats,
|
||||||
WalletError,
|
WalletError,
|
||||||
WalletErrorCode,
|
WalletErrorCode,
|
||||||
|
WalletEvents,
|
||||||
WalletInfo,
|
WalletInfo,
|
||||||
WalletInvoice,
|
WalletInvoice,
|
||||||
WalletInvoiceState,
|
WalletInvoiceState,
|
||||||
} from "@/Wallet";
|
} from ".";
|
||||||
|
import EventEmitter from "eventemitter3";
|
||||||
|
|
||||||
const defaultHeaders = {
|
const defaultHeaders = {
|
||||||
Accept: "application/json",
|
Accept: "application/json",
|
||||||
"Content-Type": "application/json",
|
"Content-Type": "application/json",
|
||||||
};
|
};
|
||||||
|
|
||||||
export default class LNDHubWallet implements LNWallet {
|
export default class LNDHubWallet extends EventEmitter<WalletEvents> implements LNWallet {
|
||||||
type: "lndhub";
|
type: "lndhub";
|
||||||
url: URL;
|
url: URL;
|
||||||
user: string;
|
user: string;
|
||||||
password: string;
|
password: string;
|
||||||
auth?: AuthResponse;
|
auth?: AuthResponse;
|
||||||
|
|
||||||
constructor(
|
constructor(url: string) {
|
||||||
url: string,
|
super();
|
||||||
readonly changed: (data?: object) => void,
|
|
||||||
) {
|
|
||||||
if (url.startsWith("lndhub://")) {
|
if (url.startsWith("lndhub://")) {
|
||||||
const regex = /^lndhub:\/\/([\S-]+):([\S-]+)@(.*)$/i;
|
const regex = /^lndhub:\/\/([\S-]+):([\S-]+)@(.*)$/i;
|
||||||
const parsedUrl = url.match(regex);
|
const parsedUrl = url.match(regex);
|
||||||
|
@ -84,7 +84,7 @@ export default class LNDHubWallet implements LNWallet {
|
||||||
password: this.password,
|
password: this.password,
|
||||||
});
|
});
|
||||||
this.auth = rsp as AuthResponse;
|
this.auth = rsp as AuthResponse;
|
||||||
this.changed();
|
this.emit("change");
|
||||||
return true;
|
return true;
|
||||||
}
|
}
|
||||||
|
|
|
@ -8,10 +8,12 @@ import {
|
||||||
LNWallet,
|
LNWallet,
|
||||||
WalletError,
|
WalletError,
|
||||||
WalletErrorCode,
|
WalletErrorCode,
|
||||||
|
WalletEvents,
|
||||||
WalletInfo,
|
WalletInfo,
|
||||||
WalletInvoice,
|
WalletInvoice,
|
||||||
WalletInvoiceState,
|
WalletInvoiceState,
|
||||||
} from "@/Wallet";
|
} from ".";
|
||||||
|
import EventEmitter from "eventemitter3";
|
||||||
|
|
||||||
interface WalletConnectConfig {
|
interface WalletConnectConfig {
|
||||||
relayUrl: string;
|
relayUrl: string;
|
||||||
|
@ -29,14 +31,14 @@ interface WalletConnectResponse<T> {
|
||||||
result?: T;
|
result?: T;
|
||||||
error?: {
|
error?: {
|
||||||
code:
|
code:
|
||||||
| "RATE_LIMITED"
|
| "RATE_LIMITED"
|
||||||
| "NOT_IMPLEMENTED"
|
| "NOT_IMPLEMENTED"
|
||||||
| "INSUFFICIENT_BALANCE"
|
| "INSUFFICIENT_BALANCE"
|
||||||
| "QUOTA_EXCEEDED"
|
| "QUOTA_EXCEEDED"
|
||||||
| "RESTRICTED"
|
| "RESTRICTED"
|
||||||
| "UNAUTHORIZED"
|
| "UNAUTHORIZED"
|
||||||
| "INTERNAL"
|
| "INTERNAL"
|
||||||
| "OTHER";
|
| "OTHER";
|
||||||
message: string;
|
message: string;
|
||||||
};
|
};
|
||||||
}
|
}
|
||||||
|
@ -75,7 +77,7 @@ interface MakeInvoiceResponse {
|
||||||
|
|
||||||
const DefaultSupported = ["get_info", "pay_invoice"];
|
const DefaultSupported = ["get_info", "pay_invoice"];
|
||||||
|
|
||||||
export class NostrConnectWallet implements LNWallet {
|
export class NostrConnectWallet extends EventEmitter<WalletEvents> implements LNWallet {
|
||||||
#log = debug("NWC");
|
#log = debug("NWC");
|
||||||
#config: WalletConnectConfig;
|
#config: WalletConnectConfig;
|
||||||
#conn?: Connection;
|
#conn?: Connection;
|
||||||
|
@ -83,10 +85,8 @@ export class NostrConnectWallet implements LNWallet {
|
||||||
#info?: WalletInfo;
|
#info?: WalletInfo;
|
||||||
#supported_methods: Array<string> = DefaultSupported;
|
#supported_methods: Array<string> = DefaultSupported;
|
||||||
|
|
||||||
constructor(
|
constructor(cfg: string) {
|
||||||
cfg: string,
|
super();
|
||||||
readonly changed: (data?: object) => void,
|
|
||||||
) {
|
|
||||||
this.#config = NostrConnectWallet.parseConfigUrl(cfg);
|
this.#config = NostrConnectWallet.parseConfigUrl(cfg);
|
||||||
this.#commandQueue = new Map();
|
this.#commandQueue = new Map();
|
||||||
}
|
}
|
||||||
|
@ -183,7 +183,7 @@ export class NostrConnectWallet implements LNWallet {
|
||||||
this.#conn.connect();
|
this.#conn.connect();
|
||||||
});
|
});
|
||||||
await this.getInfo();
|
await this.getInfo();
|
||||||
this.changed();
|
this.emit("change");
|
||||||
return true;
|
return true;
|
||||||
}
|
}
|
||||||
|
|
|
@ -5,42 +5,19 @@ import {
|
||||||
LNWallet,
|
LNWallet,
|
||||||
prToWalletInvoice,
|
prToWalletInvoice,
|
||||||
Sats,
|
Sats,
|
||||||
WalletConfig,
|
|
||||||
WalletError,
|
WalletError,
|
||||||
WalletErrorCode,
|
WalletErrorCode,
|
||||||
|
WalletEvents,
|
||||||
WalletInfo,
|
WalletInfo,
|
||||||
WalletInvoice,
|
WalletInvoice,
|
||||||
WalletInvoiceState,
|
WalletInvoiceState,
|
||||||
WalletKind,
|
} from ".";
|
||||||
WalletStore,
|
import EventEmitter from "eventemitter3";
|
||||||
} from "@/Wallet";
|
|
||||||
|
|
||||||
const WebLNQueue: Array<WorkQueueItem> = [];
|
const WebLNQueue: Array<WorkQueueItem> = [];
|
||||||
processWorkQueue(WebLNQueue);
|
processWorkQueue(WebLNQueue);
|
||||||
|
|
||||||
/**
|
export class WebLNWallet extends EventEmitter<WalletEvents> implements LNWallet {
|
||||||
* Adds a wallet config for WebLN if detected
|
|
||||||
*/
|
|
||||||
export function setupWebLNWalletConfig(store: WalletStore) {
|
|
||||||
const wallets = store.list();
|
|
||||||
|
|
||||||
const existing = wallets.find(a => a.kind === WalletKind.WebLN);
|
|
||||||
if (window.webln && !existing) {
|
|
||||||
const newConfig = {
|
|
||||||
id: "webln",
|
|
||||||
kind: WalletKind.WebLN,
|
|
||||||
active: wallets.length === 0,
|
|
||||||
info: {
|
|
||||||
alias: "WebLN",
|
|
||||||
},
|
|
||||||
} as WalletConfig;
|
|
||||||
store.add(newConfig);
|
|
||||||
} else if (existing) {
|
|
||||||
store.remove(existing.id);
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
export class WebLNWallet implements LNWallet {
|
|
||||||
isReady(): boolean {
|
isReady(): boolean {
|
||||||
return window.webln !== undefined && window.webln !== null;
|
return window.webln !== undefined && window.webln !== null;
|
||||||
}
|
}
|
|
@ -0,0 +1 @@
|
||||||
|
/// <reference types="@webbtc/webln-types" />
|
|
@ -0,0 +1,155 @@
|
||||||
|
import { decodeInvoice, unwrap } from "@snort/shared";
|
||||||
|
import AlbyWallet from "./AlbyWallet";
|
||||||
|
import { CashuWallet } from "./Cashu";
|
||||||
|
import { LNCWallet } from "./LNCWallet";
|
||||||
|
import LNDHubWallet from "./LNDHub";
|
||||||
|
import { NostrConnectWallet } from "./NostrWalletConnect";
|
||||||
|
import { WebLNWallet } from "./WebLN";
|
||||||
|
import EventEmitter from "eventemitter3";
|
||||||
|
|
||||||
|
export enum WalletKind {
|
||||||
|
LNDHub = 1,
|
||||||
|
LNC = 2,
|
||||||
|
WebLN = 3,
|
||||||
|
NWC = 4,
|
||||||
|
Cashu = 5,
|
||||||
|
Alby = 6,
|
||||||
|
}
|
||||||
|
|
||||||
|
export enum WalletErrorCode {
|
||||||
|
BadAuth = 1,
|
||||||
|
NotEnoughBalance = 2,
|
||||||
|
BadPartner = 3,
|
||||||
|
InvalidInvoice = 4,
|
||||||
|
RouteNotFound = 5,
|
||||||
|
GeneralError = 6,
|
||||||
|
NodeFailure = 7,
|
||||||
|
}
|
||||||
|
|
||||||
|
export class WalletError extends Error {
|
||||||
|
code: WalletErrorCode;
|
||||||
|
|
||||||
|
constructor(c: WalletErrorCode, msg: string) {
|
||||||
|
super(msg);
|
||||||
|
this.code = c;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
export const UnknownWalletError = {
|
||||||
|
code: WalletErrorCode.GeneralError,
|
||||||
|
message: "Unknown error",
|
||||||
|
} as WalletError;
|
||||||
|
|
||||||
|
export interface WalletInfo {
|
||||||
|
fee: number;
|
||||||
|
nodePubKey: string;
|
||||||
|
alias: string;
|
||||||
|
pendingChannels: number;
|
||||||
|
activeChannels: number;
|
||||||
|
peers: number;
|
||||||
|
blockHeight: number;
|
||||||
|
blockHash: string;
|
||||||
|
synced: boolean;
|
||||||
|
chains: string[];
|
||||||
|
version: string;
|
||||||
|
}
|
||||||
|
|
||||||
|
export interface Login {
|
||||||
|
service: string;
|
||||||
|
save: () => Promise<void>;
|
||||||
|
load: () => Promise<void>;
|
||||||
|
}
|
||||||
|
|
||||||
|
export interface InvoiceRequest {
|
||||||
|
amount: Sats;
|
||||||
|
memo?: string;
|
||||||
|
expiry?: number;
|
||||||
|
}
|
||||||
|
|
||||||
|
export enum WalletInvoiceState {
|
||||||
|
Pending = 0,
|
||||||
|
Paid = 1,
|
||||||
|
Expired = 2,
|
||||||
|
Failed = 3,
|
||||||
|
}
|
||||||
|
|
||||||
|
export interface WalletInvoice {
|
||||||
|
pr: string;
|
||||||
|
paymentHash: string;
|
||||||
|
memo: string;
|
||||||
|
amount: MilliSats;
|
||||||
|
fees: number;
|
||||||
|
timestamp: number;
|
||||||
|
preimage?: string;
|
||||||
|
state: WalletInvoiceState;
|
||||||
|
direction: "in" | "out";
|
||||||
|
}
|
||||||
|
|
||||||
|
export function prToWalletInvoice(pr: string) {
|
||||||
|
const parsedInvoice = decodeInvoice(pr);
|
||||||
|
if (parsedInvoice) {
|
||||||
|
return {
|
||||||
|
amount: parsedInvoice.amount ?? 0,
|
||||||
|
memo: parsedInvoice.description,
|
||||||
|
paymentHash: parsedInvoice.paymentHash ?? "",
|
||||||
|
timestamp: parsedInvoice.timestamp ?? 0,
|
||||||
|
state: parsedInvoice.expired ? WalletInvoiceState.Expired : WalletInvoiceState.Pending,
|
||||||
|
pr,
|
||||||
|
direction: "in",
|
||||||
|
} as WalletInvoice;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
export type Sats = number;
|
||||||
|
export type MilliSats = number;
|
||||||
|
|
||||||
|
export interface WalletEvents {
|
||||||
|
change: (data?: string) => void
|
||||||
|
}
|
||||||
|
|
||||||
|
export type LNWallet = EventEmitter<WalletEvents> & {
|
||||||
|
isReady(): boolean;
|
||||||
|
getInfo: () => Promise<WalletInfo>;
|
||||||
|
login: (password?: string) => Promise<boolean>;
|
||||||
|
close: () => Promise<boolean>;
|
||||||
|
getBalance: () => Promise<Sats>;
|
||||||
|
createInvoice: (req: InvoiceRequest) => Promise<WalletInvoice>;
|
||||||
|
payInvoice: (pr: string) => Promise<WalletInvoice>;
|
||||||
|
getInvoices: () => Promise<WalletInvoice[]>;
|
||||||
|
|
||||||
|
canAutoLogin: () => boolean;
|
||||||
|
canGetInvoices: () => boolean;
|
||||||
|
canGetBalance: () => boolean;
|
||||||
|
canCreateInvoice: () => boolean;
|
||||||
|
canPayInvoice: () => boolean;
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Load wallet by kind
|
||||||
|
* @param kind The wallet kind to create
|
||||||
|
* @param data Opaque data
|
||||||
|
*/
|
||||||
|
export function loadWallet(kind: WalletKind, data: string | undefined) {
|
||||||
|
switch (kind) {
|
||||||
|
case WalletKind.LNC: {
|
||||||
|
return LNCWallet.Empty();
|
||||||
|
}
|
||||||
|
case WalletKind.WebLN: {
|
||||||
|
return new WebLNWallet();
|
||||||
|
}
|
||||||
|
case WalletKind.LNDHub: {
|
||||||
|
return new LNDHubWallet(unwrap(data));
|
||||||
|
}
|
||||||
|
case WalletKind.NWC: {
|
||||||
|
return new NostrConnectWallet(unwrap(data));
|
||||||
|
}
|
||||||
|
case WalletKind.Alby: {
|
||||||
|
return new AlbyWallet(JSON.parse(unwrap(data)));
|
||||||
|
}
|
||||||
|
case WalletKind.Cashu: {
|
||||||
|
return new CashuWallet(JSON.parse(unwrap(data)));
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
export { LNCWallet, WebLNWallet, LNDHubWallet, NostrConnectWallet, AlbyWallet, CashuWallet }
|
|
@ -0,0 +1,18 @@
|
||||||
|
{
|
||||||
|
"compilerOptions": {
|
||||||
|
"baseUrl": "src",
|
||||||
|
"target": "ESNext",
|
||||||
|
"moduleResolution": "Bundler",
|
||||||
|
"esModuleInterop": true,
|
||||||
|
"noImplicitOverride": true,
|
||||||
|
"module": "ESNext",
|
||||||
|
"strict": true,
|
||||||
|
"declaration": true,
|
||||||
|
"declarationMap": true,
|
||||||
|
"inlineSourceMap": true,
|
||||||
|
"outDir": "dist",
|
||||||
|
"skipLibCheck": true
|
||||||
|
},
|
||||||
|
"include": ["./src/**/*.ts"],
|
||||||
|
"exclude": ["**/*.test.ts"]
|
||||||
|
}
|
|
@ -0,0 +1,3 @@
|
||||||
|
{
|
||||||
|
"entryPoints": ["src/index.ts"]
|
||||||
|
}
|
Loading…
Reference in New Issue