Skip to content

Commit

Permalink
feat: restore cashu wallet from seed
Browse files Browse the repository at this point in the history
  • Loading branch information
elnosh committed Jan 10, 2025
1 parent ddce78c commit c3c7f2c
Show file tree
Hide file tree
Showing 6 changed files with 47 additions and 14 deletions.
2 changes: 1 addition & 1 deletion frontend/src/lib/backendType.ts
Original file line number Diff line number Diff line change
Expand Up @@ -33,7 +33,7 @@ export const backendTypeConfigs: Record<BackendType, BackendTypeConfig> = {
hasNodeBackup: false,
},
CASHU: {
hasMnemonic: false,
hasMnemonic: true,
hasChannelManagement: false,
hasNodeBackup: false,
},
Expand Down
2 changes: 1 addition & 1 deletion frontend/src/screens/settings/DebugTools.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -304,7 +304,7 @@ export default function DebugTools() {
Get Network Graph
</Button>
</AlertDialogTrigger>
{info?.backendType === "LDK" && (
{(info?.backendType === "LDK" || info?.backendType === "CASHU") && (
<AlertDialogTrigger asChild>
<Button onClick={() => setDialog("resetRoutingData")}>
Clear Routing Data
Expand Down
2 changes: 1 addition & 1 deletion go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@ toolchain go1.23.3
require (
github.com/adrg/xdg v0.5.3
github.com/breez/breez-sdk-go v0.5.2
github.com/elnosh/gonuts v0.3.1-0.20250107155549-ae74d5ac3a23
github.com/elnosh/gonuts v0.3.1-0.20250109171605-ee81d0d42b97
github.com/getAlby/glalby-go v0.0.0-20240621192717-95673c864d59
github.com/getAlby/ldk-node-go v0.0.0-20250106052504-d4191410486f
github.com/go-gormigrate/gormigrate/v2 v2.1.3
Expand Down
4 changes: 2 additions & 2 deletions go.sum
Original file line number Diff line number Diff line change
Expand Up @@ -180,8 +180,8 @@ github.com/ebitengine/purego v0.6.0-alpha.5 h1:EYID3JOAdmQ4SNZYJHu9V6IqOeRQDBYxq
github.com/ebitengine/purego v0.6.0-alpha.5/go.mod h1:ah1In8AOtksoNK6yk5z1HTJeUkC1Ez4Wk2idgGslMwQ=
github.com/elnosh/btc-docker-test v0.0.0-20241223164556-146e52a0433b h1:JbZVAqKBVRkvHuZZJsf8MvO+I7HGaVNCMQvp7WMFGqs=
github.com/elnosh/btc-docker-test v0.0.0-20241223164556-146e52a0433b/go.mod h1:4PlP53czOHN+XvjyQZh+zgrzkI7BYFvJajxKK2zquyE=
github.com/elnosh/gonuts v0.3.1-0.20250107155549-ae74d5ac3a23 h1:L74EHKPpS5PDMHc/yhP24S7KaxHQBfCHudYpNXFmPI8=
github.com/elnosh/gonuts v0.3.1-0.20250107155549-ae74d5ac3a23/go.mod h1:s1lwjLf5HjrkgiMvdYPCz7HNAUjYS94C2WQcYaFbijc=
github.com/elnosh/gonuts v0.3.1-0.20250109171605-ee81d0d42b97 h1:m+BmGO3iJuX9QFbk1ClOPUgo758w1SF/OxQNONe62ow=
github.com/elnosh/gonuts v0.3.1-0.20250109171605-ee81d0d42b97/go.mod h1:s1lwjLf5HjrkgiMvdYPCz7HNAUjYS94C2WQcYaFbijc=
github.com/envoyproxy/go-control-plane v0.9.0/go.mod h1:YTl/9mNaCwkRvm6d1a2C3ymFceY/DCBVvsKhRF0iEA4=
github.com/envoyproxy/go-control-plane v0.9.1-0.20191026205805-5f8ba28d4473/go.mod h1:YTl/9mNaCwkRvm6d1a2C3ymFceY/DCBVvsKhRF0iEA4=
github.com/envoyproxy/go-control-plane v0.9.4/go.mod h1:6rpuAdCZL397s3pYoYcLgu1mIlRU8Am5FuJP05cCM98=
Expand Down
49 changes: 41 additions & 8 deletions lnclient/cashu/cashu.go
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@ import (
"github.com/elnosh/gonuts/cashu/nuts/nut05"
"github.com/elnosh/gonuts/wallet"
"github.com/elnosh/gonuts/wallet/storage"
"github.com/getAlby/hub/config"
"github.com/getAlby/hub/constants"
"github.com/getAlby/hub/lnclient"
"github.com/getAlby/hub/logger"
Expand All @@ -21,10 +22,11 @@ import (
)

type CashuService struct {
wallet *wallet.Wallet
wallet *wallet.Wallet
workDir string
}

func NewCashuService(workDir string, mintUrl string) (result lnclient.LNClient, err error) {
func NewCashuService(cfg config.Config, workDir, encryptionKey, mintUrl string) (result lnclient.LNClient, err error) {
if workDir == "" {
return nil, errors.New("one or more required cashu configuration are missing")
}
Expand All @@ -43,21 +45,31 @@ func NewCashuService(workDir string, mintUrl string) (result lnclient.LNClient,
logger.Logger.WithField("mintUrl", mintUrl).Info("Setting up cashu wallet")
config := wallet.Config{WalletPath: newpath, CurrentMintURL: mintUrl}

wallet, err := wallet.LoadWallet(config)
cashuWallet, err := wallet.LoadWallet(config)
if err != nil {
logger.Logger.WithError(err).Error("Failed to load cashu wallet")
return nil, err
}

cs := CashuService{
wallet: wallet,
wallet: cashuWallet,
workDir: workDir,
}

cashuWalletMnemonic := cashuWallet.Mnemonic()
mnemonic, _ := cfg.Get("Mnemonic", encryptionKey)
if mnemonic != cashuWalletMnemonic {
if err := cfg.SetUpdate("Mnemonic", cashuWalletMnemonic, encryptionKey); err != nil {
logger.Logger.WithError(err).Error("Failed to save mnemonic from cashu wallet")
return nil, err
}
}

return &cs, nil
}

func (cs *CashuService) Shutdown() error {
return nil
return cs.wallet.Shutdown()
}

func (cs *CashuService) SendPaymentSync(ctx context.Context, invoice string, amount *uint64) (response *lnclient.PayInvoiceResponse, err error) {
Expand Down Expand Up @@ -208,6 +220,25 @@ func (cs *CashuService) RedeemOnchainFunds(ctx context.Context, toAddress string
}

func (cs *CashuService) ResetRouter(key string) error {
mnemonic := cs.wallet.Mnemonic()
currentMint := cs.wallet.CurrentMint()

if err := cs.wallet.Shutdown(); err != nil {
return err
}

if err := os.RemoveAll(cs.workDir); err != nil {
logger.Logger.WithError(err).Error("Failed to remove wallet directory")
return err
}

amountRestored, err := wallet.Restore(cs.workDir, mnemonic, []string{currentMint})
if err != nil {
logger.Logger.WithError(err).Error("Failed restore cashu wallet")
return err
}

logger.Logger.WithField("amountRestored", amountRestored).Info("Successfully restored cashu wallet")
return nil
}

Expand Down Expand Up @@ -286,9 +317,11 @@ func (cs *CashuService) cashuMintQuoteToTransaction(mintQuote *storage.MintQuote
descriptionHash := paymentRequest.DescriptionHash

return &lnclient.Transaction{
Type: constants.TRANSACTION_TYPE_INCOMING,
Invoice: mintQuote.PaymentRequest,
PaymentHash: paymentRequest.PaymentHash,
Type: constants.TRANSACTION_TYPE_INCOMING,
Invoice: mintQuote.PaymentRequest,
PaymentHash: paymentRequest.PaymentHash,
// note: setting dummy preimage so that it gets marked as settled
Preimage: paymentRequest.PaymentHash,
Amount: paymentRequest.MSatoshi,
CreatedAt: int64(paymentRequest.CreatedAt),
ExpiresAt: expiresAt,
Expand Down
2 changes: 1 addition & 1 deletion service/start.go
Original file line number Diff line number Diff line change
Expand Up @@ -332,7 +332,7 @@ func (svc *service) launchLNBackend(ctx context.Context, encryptionKey string) e
cashuMintUrl, _ := svc.cfg.Get("CashuMintUrl", encryptionKey)
cashuWorkdir := path.Join(svc.cfg.GetEnv().Workdir, "cashu")

lnClient, err = cashu.NewCashuService(cashuWorkdir, cashuMintUrl)
lnClient, err = cashu.NewCashuService(svc.cfg, cashuWorkdir, encryptionKey, cashuMintUrl)
default:
logger.Logger.WithField("backend_type", lnBackend).Error("Unsupported LNBackendType")
return fmt.Errorf("unsupported backend type: %s", lnBackend)
Expand Down

0 comments on commit c3c7f2c

Please sign in to comment.