forked from Kieran/snort
refactor: extract wallet system
This commit is contained in:
parent
d1095847d8
commit
8137317bfe
874
.yarn/releases/yarn-3.6.3.cjs
vendored
874
.yarn/releases/yarn-3.6.3.cjs
vendored
File diff suppressed because one or more lines are too long
893
.yarn/releases/yarn-4.1.1.cjs
vendored
Executable file
893
.yarn/releases/yarn-4.1.1.cjs
vendored
Executable file
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:
|
||||
"here":
|
||||
here:
|
||||
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/*"
|
||||
],
|
||||
"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",
|
||||
"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 .",
|
||||
@ -18,7 +18,7 @@
|
||||
"trailingComma": "all",
|
||||
"endOfLine": "lf"
|
||||
},
|
||||
"packageManager": "yarn@3.6.3",
|
||||
"packageManager": "yarn@4.1.1",
|
||||
"dependencies": {
|
||||
"@cloudflare/workers-types": "^4.20230307.0",
|
||||
"@tauri-apps/cli": "^1.2.3",
|
||||
|
@ -2,9 +2,7 @@
|
||||
"name": "@snort/app",
|
||||
"version": "0.2.0",
|
||||
"dependencies": {
|
||||
"@cashu/cashu-ts": "0.6.1",
|
||||
"@here/maps-api-for-javascript": "^1.50.0",
|
||||
"@lightninglabs/lnc-web": "^0.2.8-alpha",
|
||||
"@noble/curves": "^1.0.0",
|
||||
"@noble/hashes": "^1.3.3",
|
||||
"@scure/base": "^1.1.1",
|
||||
@ -15,6 +13,7 @@
|
||||
"@snort/system-react": "workspace:*",
|
||||
"@snort/system-wasm": "workspace:*",
|
||||
"@snort/system-web": "workspace:*",
|
||||
"@snort/wallet": "workspace:*",
|
||||
"@snort/worker-relay": "workspace:*",
|
||||
"@szhsin/react-menu": "^3.3.1",
|
||||
"@uidotdev/usehooks": "^2.4.1",
|
||||
@ -119,7 +118,7 @@
|
||||
"tailwindcss": "^3.3.3",
|
||||
"tinybench": "^2.5.1",
|
||||
"typescript": "^5.2.2",
|
||||
"vite": "^5.1.5",
|
||||
"vite": "^5.2.8",
|
||||
"vite-plugin-pwa": "^0.19.2",
|
||||
"vite-plugin-version-mark": "^0.0.10",
|
||||
"vitest": "^0.34.6"
|
||||
|
@ -1,5 +1,6 @@
|
||||
import { LNURL } from "@snort/shared";
|
||||
import { NostrEvent } from "@snort/system";
|
||||
import { WalletInvoiceState } from "@snort/wallet";
|
||||
import { FormattedMessage, FormattedNumber } from "react-intl";
|
||||
|
||||
import { UserCache } from "@/Cache";
|
||||
@ -10,7 +11,6 @@ import useEventPublisher from "@/Hooks/useEventPublisher";
|
||||
import useLogin from "@/Hooks/useLogin";
|
||||
import { dedupe, findTag, getDisplayName, hexToBech32 } from "@/Utils";
|
||||
import { useWallet } from "@/Wallet";
|
||||
import { WalletInvoiceState } from "@/Wallet";
|
||||
|
||||
export default function PubkeyList({ ev, className }: { ev: NostrEvent; className?: string }) {
|
||||
const wallet = useWallet();
|
||||
|
@ -1,10 +1,10 @@
|
||||
import { LNWallet } from "@snort/wallet";
|
||||
import React, { ReactNode } from "react";
|
||||
import { FormattedMessage } from "react-intl";
|
||||
|
||||
import Copy from "@/Components/Copy/Copy";
|
||||
import QrCode from "@/Components/QrCode";
|
||||
import { ZapTargetResult } from "@/Utils/Zapper";
|
||||
import { LNWallet } from "@/Wallet";
|
||||
|
||||
export function ZapModalInvoice(props: {
|
||||
invoice: Array<ZapTargetResult>;
|
||||
|
@ -8,6 +8,7 @@ import BlueWallet from "@/Components/Icons/BlueWallet";
|
||||
import Icon from "@/Components/Icons/Icon";
|
||||
import NostrIcon from "@/Components/Icons/Nostrich";
|
||||
import { getAlbyOAuth } from "@/Pages/settings/wallet/utils";
|
||||
import CashuIcon from "@/Components/Icons/Cashu";
|
||||
|
||||
const WalletRow = (props: {
|
||||
logo: ReactNode;
|
||||
@ -70,12 +71,12 @@ const WalletSettings = () => {
|
||||
url="/settings/wallet/lndhub"
|
||||
desc={<FormattedMessage defaultMessage="Generic LNDHub wallet (BTCPayServer / Alby / LNBits)" id="0MndVW" />}
|
||||
/>
|
||||
{/*<WalletRow
|
||||
<WalletRow
|
||||
logo={<CashuIcon size={64} />}
|
||||
name="Cashu"
|
||||
url="/settings/wallet/cashu"
|
||||
desc={<FormattedMessage defaultMessage="Cashu mint wallet" id="3natuV" />}
|
||||
/>*/}
|
||||
/>
|
||||
{CONFIG.alby && (
|
||||
<WalletRow
|
||||
logo={<AlbyIcon size={64} />}
|
||||
|
@ -1,11 +1,11 @@
|
||||
import { AlbyWallet, WalletKind } from "@snort/wallet";
|
||||
import { useEffect, useState } from "react";
|
||||
import { useLocation, useNavigate } from "react-router-dom";
|
||||
import { v4 as uuid } from "uuid";
|
||||
|
||||
import PageSpinner from "@/Components/PageSpinner";
|
||||
import { getAlbyOAuth } from "@/Pages/settings/wallet/utils";
|
||||
import { WalletConfig, WalletKind, Wallets } from "@/Wallet";
|
||||
import AlbyWallet from "@/Wallet/AlbyWallet";
|
||||
import { WalletConfig, Wallets } from "@/Wallet";
|
||||
|
||||
export default function AlbyOAuth() {
|
||||
const navigate = useNavigate();
|
||||
@ -16,7 +16,7 @@ export default function AlbyOAuth() {
|
||||
async function setupWallet(token: string) {
|
||||
try {
|
||||
const auth = await alby.getToken(token);
|
||||
const connection = new AlbyWallet(auth, () => {});
|
||||
const connection = new AlbyWallet(auth);
|
||||
const info = await connection.getInfo();
|
||||
|
||||
const newWallet = {
|
||||
|
@ -1,3 +1,4 @@
|
||||
import { CashuWallet, WalletKind } from "@snort/wallet";
|
||||
import { useState } from "react";
|
||||
import { FormattedMessage, useIntl } from "react-intl";
|
||||
import { useNavigate } from "react-router-dom";
|
||||
@ -5,7 +6,7 @@ import { v4 as uuid } from "uuid";
|
||||
|
||||
import AsyncButton from "@/Components/Button/AsyncButton";
|
||||
import { unwrap } from "@/Utils";
|
||||
import { WalletConfig, WalletKind, Wallets } from "@/Wallet";
|
||||
import { WalletConfig, Wallets } from "@/Wallet";
|
||||
|
||||
const ConnectCashu = () => {
|
||||
const navigate = useNavigate();
|
||||
@ -19,7 +20,6 @@ const ConnectCashu = () => {
|
||||
throw new Error("Mint URL is required");
|
||||
}
|
||||
|
||||
const { CashuWallet } = await import("@/Wallet/Cashu");
|
||||
const connection = new CashuWallet(
|
||||
{
|
||||
url: config,
|
||||
@ -27,7 +27,6 @@ const ConnectCashu = () => {
|
||||
proofs: [],
|
||||
keysets: [],
|
||||
},
|
||||
() => {},
|
||||
);
|
||||
await connection.login();
|
||||
const info = await connection.getInfo();
|
||||
|
@ -1,3 +1,4 @@
|
||||
import { LNCWallet, LNWallet, WalletInfo, WalletKind } from "@snort/wallet";
|
||||
import { useState } from "react";
|
||||
import { FormattedMessage, useIntl } from "react-intl";
|
||||
import { useNavigate } from "react-router-dom";
|
||||
@ -5,7 +6,7 @@ import { v4 as uuid } from "uuid";
|
||||
|
||||
import AsyncButton from "@/Components/Button/AsyncButton";
|
||||
import { unwrap } from "@/Utils";
|
||||
import { LNWallet, WalletInfo, WalletKind, Wallets } from "@/Wallet";
|
||||
import { Wallets } from "@/Wallet";
|
||||
|
||||
const ConnectLNC = () => {
|
||||
const { formatMessage } = useIntl();
|
||||
@ -18,7 +19,6 @@ const ConnectLNC = () => {
|
||||
|
||||
async function tryConnect(cfg: string) {
|
||||
try {
|
||||
const { LNCWallet } = await import("@/Wallet/LNCWallet");
|
||||
const lnc = await LNCWallet.Initialize(cfg);
|
||||
const info = await lnc.getInfo();
|
||||
|
||||
|
@ -1,3 +1,4 @@
|
||||
import { LNDHubWallet, WalletKind } from "@snort/wallet";
|
||||
import { useState } from "react";
|
||||
import { FormattedMessage, useIntl } from "react-intl";
|
||||
import { useNavigate } from "react-router-dom";
|
||||
@ -5,8 +6,7 @@ import { v4 as uuid } from "uuid";
|
||||
|
||||
import AsyncButton from "@/Components/Button/AsyncButton";
|
||||
import { unwrap } from "@/Utils";
|
||||
import { WalletConfig, WalletKind, Wallets } from "@/Wallet";
|
||||
import LNDHubWallet from "@/Wallet/LNDHub";
|
||||
import { WalletConfig, Wallets } from "@/Wallet";
|
||||
|
||||
const ConnectLNDHub = () => {
|
||||
const navigate = useNavigate();
|
||||
@ -16,7 +16,7 @@ const ConnectLNDHub = () => {
|
||||
|
||||
async function tryConnect(config: string) {
|
||||
try {
|
||||
const connection = new LNDHubWallet(config, () => {});
|
||||
const connection = new LNDHubWallet(config);
|
||||
await connection.login();
|
||||
const info = await connection.getInfo();
|
||||
|
||||
|
@ -1,3 +1,4 @@
|
||||
import { NostrConnectWallet, WalletKind } from "@snort/wallet";
|
||||
import { useState } from "react";
|
||||
import { FormattedMessage, useIntl } from "react-intl";
|
||||
import { Link, useNavigate } from "react-router-dom";
|
||||
@ -5,8 +6,7 @@ import { v4 as uuid } from "uuid";
|
||||
|
||||
import AsyncButton from "@/Components/Button/AsyncButton";
|
||||
import { unwrap } from "@/Utils";
|
||||
import { WalletConfig, WalletKind, Wallets } from "@/Wallet";
|
||||
import { NostrConnectWallet } from "@/Wallet/NostrWalletConnect";
|
||||
import { WalletConfig, Wallets } from "@/Wallet";
|
||||
|
||||
const ConnectNostrWallet = () => {
|
||||
const navigate = useNavigate();
|
||||
@ -16,7 +16,7 @@ const ConnectNostrWallet = () => {
|
||||
|
||||
async function tryConnect(config: string) {
|
||||
try {
|
||||
const connection = new NostrConnectWallet(config, () => {});
|
||||
const connection = new NostrConnectWallet(config);
|
||||
await connection.login();
|
||||
const info = await connection.getInfo();
|
||||
|
||||
|
@ -59,7 +59,7 @@ export function getAlbyOAuth() {
|
||||
|
||||
const data = await req.json();
|
||||
if (req.ok) {
|
||||
return { ...data, created_at: unixNow() } as OAuthToken;
|
||||
return { ...data, created_at: unixNow(), clientId, clientSecret } as OAuthToken;
|
||||
} else {
|
||||
throw new Error(data.error_description as string);
|
||||
}
|
||||
@ -74,4 +74,6 @@ export interface OAuthToken {
|
||||
refresh_token: string;
|
||||
scope: string;
|
||||
token_type: string;
|
||||
clientId: string;
|
||||
clientSecret: string;
|
||||
}
|
||||
|
@ -1,4 +1,5 @@
|
||||
/* eslint-disable max-lines */
|
||||
import { LNWallet,Sats, WalletInvoice } from "@snort/wallet";
|
||||
import classNames from "classnames";
|
||||
import { useEffect, useState } from "react";
|
||||
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 { useRates } from "@/Hooks/useRates";
|
||||
import { unwrap } from "@/Utils";
|
||||
import { LNWallet, Sats, useWallet, WalletInvoice, Wallets } from "@/Wallet";
|
||||
import { useWallet, Wallets } from "@/Wallet";
|
||||
|
||||
export default function WalletPage(props: { showHistory: boolean }) {
|
||||
const navigate = useNavigate();
|
||||
|
@ -1,11 +1,12 @@
|
||||
import { ExternalStore, LNURL, unixNow } from "@snort/shared";
|
||||
import { LNWallet, WalletInvoiceState } from "@snort/wallet";
|
||||
import debug from "debug";
|
||||
|
||||
import { UserCache } from "@/Cache";
|
||||
import { Toastore } from "@/Components/Toaster/Toaster";
|
||||
import { SnortPubKey } from "@/Utils/Const";
|
||||
import { bech32ToHex, getDisplayName, trackEvent } from "@/Utils/index";
|
||||
import { LNWallet, WalletInvoiceState, Wallets } from "@/Wallet";
|
||||
import { Wallets } from "@/Wallet";
|
||||
|
||||
export enum ZapPoolRecipientType {
|
||||
Generic = 0,
|
||||
|
@ -1,8 +1,8 @@
|
||||
import { isHex, LNURL } from "@snort/shared";
|
||||
import { EventPublisher, NostrEvent, NostrLink, SystemInterface } from "@snort/system";
|
||||
import { LNWallet, WalletInvoiceState } from "@snort/wallet";
|
||||
|
||||
import { generateRandomKey } from "@/Utils/Login";
|
||||
import { LNWallet, WalletInvoiceState } from "@/Wallet";
|
||||
|
||||
export interface ZapTarget {
|
||||
type: "lnurl" | "pubkey";
|
||||
|
@ -1,131 +1,16 @@
|
||||
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 { 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 {
|
||||
id: string;
|
||||
kind: WalletKind;
|
||||
active: boolean;
|
||||
info: WalletInfo;
|
||||
|
||||
/**
|
||||
* Opaque string for wallet config
|
||||
*/
|
||||
data?: string;
|
||||
}
|
||||
|
||||
@ -232,35 +117,18 @@ export class WalletStore extends ExternalStore<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)),
|
||||
);
|
||||
}
|
||||
const w = loadWallet(cfg.kind, cfg.data);
|
||||
if (w) {
|
||||
w.on("change", d => this.#onWalletChange(cfg, d));
|
||||
}
|
||||
return w;
|
||||
}
|
||||
|
||||
#onWalletChange(cfg: WalletConfig, data?: object) {
|
||||
#onWalletChange(cfg: WalletConfig, data?: string) {
|
||||
if (data) {
|
||||
const activeConfig = this.#configs.find(a => a.id === cfg.id);
|
||||
if (activeConfig) {
|
||||
activeConfig.data = JSON.stringify(data);
|
||||
activeConfig.data = data;
|
||||
}
|
||||
this.save();
|
||||
} else {
|
||||
@ -286,3 +154,26 @@ export function useWallet() {
|
||||
}, [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 { hasWasm, wasmInit, WasmPath } from "@/Utils/wasm";
|
||||
import { Wallets } from "@/Wallet";
|
||||
import { setupWebLNWalletConfig } from "@/Wallet/WebLN";
|
||||
import { setupWebLNWalletConfig } from "@/Wallet";
|
||||
|
||||
async function initSite() {
|
||||
storeRefCode();
|
||||
|
@ -109,7 +109,6 @@ export abstract class BackgroundLoader<T extends { loaded: number; created: numb
|
||||
}
|
||||
} catch (e) {
|
||||
this.#log("Error: %O", e);
|
||||
debugger;
|
||||
}
|
||||
}
|
||||
|
||||
|
1
packages/wallet/README.md
Normal file
1
packages/wallet/README.md
Normal file
@ -0,0 +1 @@
|
||||
# wallet
|
35
packages/wallet/package.json
Normal file
35
packages/wallet/package.json
Normal file
@ -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 { unixNow, unwrap } from "@snort/shared";
|
||||
|
||||
import { OAuthToken } from "@/Pages/settings/wallet/utils";
|
||||
|
||||
import {
|
||||
InvoiceRequest,
|
||||
LNWallet,
|
||||
prToWalletInvoice,
|
||||
WalletError,
|
||||
WalletErrorCode,
|
||||
WalletEvents,
|
||||
WalletInfo,
|
||||
WalletInvoice,
|
||||
WalletInvoiceState,
|
||||
} 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;
|
||||
constructor(
|
||||
token: OAuthToken,
|
||||
readonly onChange: (data?: object) => void,
|
||||
) {
|
||||
constructor(token: OAuthToken) {
|
||||
super();
|
||||
this.#token = token;
|
||||
}
|
||||
|
||||
@ -136,7 +145,7 @@ export default class AlbyWallet implements LNWallet {
|
||||
accept: "application/json",
|
||||
"content-type": "application/x-www-form-urlencoded",
|
||||
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),
|
||||
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 { InvoiceRequest, LNWallet, WalletInfo, WalletInvoice } from "@/Wallet";
|
||||
import { InvoiceRequest, LNWallet, WalletEvents, WalletInfo, WalletInvoice } from ".";
|
||||
import EventEmitter from "eventemitter3";
|
||||
|
||||
export type CashuWalletConfig = {
|
||||
url: string;
|
||||
@ -9,14 +10,12 @@ export type CashuWalletConfig = {
|
||||
proofs: Array<Proof>;
|
||||
};
|
||||
|
||||
export class CashuWallet implements LNWallet {
|
||||
export class CashuWallet extends EventEmitter<WalletEvents> implements LNWallet {
|
||||
#wallet: CashuWalletConfig;
|
||||
#mint: CashuMint;
|
||||
|
||||
constructor(
|
||||
wallet: CashuWalletConfig,
|
||||
readonly onChange: (data?: object) => void,
|
||||
) {
|
||||
constructor(wallet: CashuWalletConfig) {
|
||||
super();
|
||||
this.#wallet = wallet;
|
||||
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]);
|
||||
this.#wallet.proofs = filteredProofs;
|
||||
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 debug from "debug";
|
||||
|
||||
import { unwrap } from "@/Utils";
|
||||
import {
|
||||
InvoiceRequest,
|
||||
LNWallet,
|
||||
@ -9,10 +8,13 @@ import {
|
||||
prToWalletInvoice,
|
||||
WalletError,
|
||||
WalletErrorCode,
|
||||
WalletEvents,
|
||||
WalletInfo,
|
||||
WalletInvoice,
|
||||
WalletInvoiceState,
|
||||
} from "@/Wallet";
|
||||
} from ".";
|
||||
import { unwrap } from "@snort/shared";
|
||||
import EventEmitter from "eventemitter3";
|
||||
|
||||
enum Payment_PaymentStatus {
|
||||
UNKNOWN = "UNKNOWN",
|
||||
@ -22,11 +24,12 @@ enum Payment_PaymentStatus {
|
||||
UNRECOGNIZED = "UNRECOGNIZED",
|
||||
}
|
||||
|
||||
export class LNCWallet implements LNWallet {
|
||||
export class LNCWallet extends EventEmitter<WalletEvents> implements LNWallet {
|
||||
#lnc: LNC;
|
||||
readonly #log = debug("LNC");
|
||||
|
||||
private constructor(pairingPhrase?: string, password?: string) {
|
||||
super();
|
||||
this.#lnc = new LNC({
|
||||
pairingPhrase,
|
||||
password,
|
@ -7,27 +7,27 @@ import {
|
||||
Sats,
|
||||
WalletError,
|
||||
WalletErrorCode,
|
||||
WalletEvents,
|
||||
WalletInfo,
|
||||
WalletInvoice,
|
||||
WalletInvoiceState,
|
||||
} from "@/Wallet";
|
||||
} from ".";
|
||||
import EventEmitter from "eventemitter3";
|
||||
|
||||
const defaultHeaders = {
|
||||
Accept: "application/json",
|
||||
"Content-Type": "application/json",
|
||||
};
|
||||
|
||||
export default class LNDHubWallet implements LNWallet {
|
||||
export default class LNDHubWallet extends EventEmitter<WalletEvents> implements LNWallet {
|
||||
type: "lndhub";
|
||||
url: URL;
|
||||
user: string;
|
||||
password: string;
|
||||
auth?: AuthResponse;
|
||||
|
||||
constructor(
|
||||
url: string,
|
||||
readonly changed: (data?: object) => void,
|
||||
) {
|
||||
constructor(url: string) {
|
||||
super();
|
||||
if (url.startsWith("lndhub://")) {
|
||||
const regex = /^lndhub:\/\/([\S-]+):([\S-]+)@(.*)$/i;
|
||||
const parsedUrl = url.match(regex);
|
||||
@ -84,7 +84,7 @@ export default class LNDHubWallet implements LNWallet {
|
||||
password: this.password,
|
||||
});
|
||||
this.auth = rsp as AuthResponse;
|
||||
this.changed();
|
||||
this.emit("change");
|
||||
return true;
|
||||
}
|
||||
|
@ -8,10 +8,12 @@ import {
|
||||
LNWallet,
|
||||
WalletError,
|
||||
WalletErrorCode,
|
||||
WalletEvents,
|
||||
WalletInfo,
|
||||
WalletInvoice,
|
||||
WalletInvoiceState,
|
||||
} from "@/Wallet";
|
||||
} from ".";
|
||||
import EventEmitter from "eventemitter3";
|
||||
|
||||
interface WalletConnectConfig {
|
||||
relayUrl: string;
|
||||
@ -75,7 +77,7 @@ interface MakeInvoiceResponse {
|
||||
|
||||
const DefaultSupported = ["get_info", "pay_invoice"];
|
||||
|
||||
export class NostrConnectWallet implements LNWallet {
|
||||
export class NostrConnectWallet extends EventEmitter<WalletEvents> implements LNWallet {
|
||||
#log = debug("NWC");
|
||||
#config: WalletConnectConfig;
|
||||
#conn?: Connection;
|
||||
@ -83,10 +85,8 @@ export class NostrConnectWallet implements LNWallet {
|
||||
#info?: WalletInfo;
|
||||
#supported_methods: Array<string> = DefaultSupported;
|
||||
|
||||
constructor(
|
||||
cfg: string,
|
||||
readonly changed: (data?: object) => void,
|
||||
) {
|
||||
constructor(cfg: string) {
|
||||
super();
|
||||
this.#config = NostrConnectWallet.parseConfigUrl(cfg);
|
||||
this.#commandQueue = new Map();
|
||||
}
|
||||
@ -183,7 +183,7 @@ export class NostrConnectWallet implements LNWallet {
|
||||
this.#conn.connect();
|
||||
});
|
||||
await this.getInfo();
|
||||
this.changed();
|
||||
this.emit("change");
|
||||
return true;
|
||||
}
|
||||
|
@ -5,42 +5,19 @@ import {
|
||||
LNWallet,
|
||||
prToWalletInvoice,
|
||||
Sats,
|
||||
WalletConfig,
|
||||
WalletError,
|
||||
WalletErrorCode,
|
||||
WalletEvents,
|
||||
WalletInfo,
|
||||
WalletInvoice,
|
||||
WalletInvoiceState,
|
||||
WalletKind,
|
||||
WalletStore,
|
||||
} from "@/Wallet";
|
||||
} from ".";
|
||||
import EventEmitter from "eventemitter3";
|
||||
|
||||
const WebLNQueue: Array<WorkQueueItem> = [];
|
||||
processWorkQueue(WebLNQueue);
|
||||
|
||||
/**
|
||||
* 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 {
|
||||
export class WebLNWallet extends EventEmitter<WalletEvents> implements LNWallet {
|
||||
isReady(): boolean {
|
||||
return window.webln !== undefined && window.webln !== null;
|
||||
}
|
1
packages/wallet/src/custom.d.ts
vendored
Normal file
1
packages/wallet/src/custom.d.ts
vendored
Normal file
@ -0,0 +1 @@
|
||||
/// <reference types="@webbtc/webln-types" />
|
155
packages/wallet/src/index.ts
Normal file
155
packages/wallet/src/index.ts
Normal file
@ -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 }
|
18
packages/wallet/tsconfig.json
Normal file
18
packages/wallet/tsconfig.json
Normal file
@ -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"]
|
||||
}
|
3
packages/wallet/typedoc.json
Normal file
3
packages/wallet/typedoc.json
Normal file
@ -0,0 +1,3 @@
|
||||
{
|
||||
"entryPoints": ["src/index.ts"]
|
||||
}
|
Loading…
Reference in New Issue
Block a user