Skip to content
New issue

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

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

Already on GitHub? Sign in to your account

feat: phoenixd subwallets #1035

Merged
merged 2 commits into from
Jan 30, 2025
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
5 changes: 3 additions & 2 deletions api/api.go
Original file line number Diff line number Diff line change
Expand Up @@ -60,8 +60,9 @@ func NewAPI(svc service.Service, gormDB *gorm.DB, config config.Config, keys key
func (api *api) CreateApp(createAppRequest *CreateAppRequest) (*CreateAppResponse, error) {
backendType, _ := api.cfg.Get("LNBackendType", "")
if createAppRequest.Isolated &&
backendType != "LDK" &&
backendType != "LND" {
backendType != config.LDKBackendType &&
backendType != config.LNDBackendType &&
backendType != config.PhoenixBackendType {
return nil, fmt.Errorf(
"sub-wallets are currently not supported on your node backend. Try LDK or LND")
}
Expand Down
104 changes: 41 additions & 63 deletions lnclient/phoenixd/phoenixd.go
Original file line number Diff line number Diff line change
Expand Up @@ -166,23 +166,12 @@ func (svc *PhoenixService) ListTransactions(ctx context.Context, from, until, li
}
transactions = []lnclient.Transaction{}
for _, invoice := range incomingPayments {
var settledAt *int64
if invoice.CompletedAt != 0 {
settledAtUnix := time.UnixMilli(invoice.CompletedAt).Unix()
settledAt = &settledAtUnix
}
transaction := lnclient.Transaction{
Type: "incoming",
Invoice: invoice.Invoice,
Preimage: invoice.Preimage,
PaymentHash: invoice.PaymentHash,
Amount: invoice.ReceivedSat * 1000,
FeesPaid: invoice.Fees * 1000,
CreatedAt: time.UnixMilli(invoice.CreatedAt).Unix(),
Description: invoice.Description,
SettledAt: settledAt,
transaction, err := phoenixInvoiceToTransaction(&invoice)
if err != nil {
return nil, err
}
transactions = append(transactions, transaction)

transactions = append(transactions, *transaction)
}

// get outgoing payments
Expand Down Expand Up @@ -320,31 +309,12 @@ func (svc *PhoenixService) MakeInvoice(ctx context.Context, amount int64, descri
return nil, err
}

paymentRequest, err := decodepay.Decodepay(invoiceRes.Serialized)
tx, err := svc.LookupInvoice(ctx, invoiceRes.PaymentHash)
if err != nil {
logger.Logger.WithFields(logrus.Fields{
"bolt11": invoiceRes.Serialized,
}).Errorf("Failed to decode bolt11 invoice: %v", err)

logger.Logger.WithError(err).Error("failed to lookup newly created invoice")
return nil, err
}

expiresAt := time.UnixMilli(int64(paymentRequest.CreatedAt) * 1000).Add(time.Duration(paymentRequest.Expiry) * time.Second).Unix()

tx := &lnclient.Transaction{
Type: "incoming",
Invoice: invoiceRes.Serialized,
Preimage: "", // TODO: set preimage to enable self-payments
PaymentHash: invoiceRes.PaymentHash,
Amount: amount,
FeesPaid: 0,
CreatedAt: time.Now().Unix(),
ExpiresAt: &expiresAt,
SettledAt: nil,
Metadata: nil,
Description: paymentRequest.Description,
DescriptionHash: paymentRequest.DescriptionHash,
}
return tx, nil
}

Expand All @@ -366,36 +336,11 @@ func (svc *PhoenixService) LookupInvoice(ctx context.Context, paymentHash string
return nil, err
}

var settledAt *int64
if invoiceRes.CompletedAt != 0 {
settledAtUnix := time.UnixMilli(invoiceRes.CompletedAt).Unix()
settledAt = &settledAtUnix
}

paymentRequest, err := decodepay.Decodepay(invoiceRes.Invoice)
transaction, err = phoenixInvoiceToTransaction(&invoiceRes)
if err != nil {
logger.Logger.WithFields(logrus.Fields{
"bolt11": invoiceRes.Invoice,
}).Errorf("Failed to decode bolt11 invoice: %v", err)

return nil, err
}

expiresAt := time.UnixMilli(int64(paymentRequest.CreatedAt) * 1000).Add(time.Duration(paymentRequest.Expiry) * time.Second).Unix()

transaction = &lnclient.Transaction{
Type: "incoming",
Invoice: invoiceRes.Invoice,
Preimage: invoiceRes.Preimage,
PaymentHash: invoiceRes.PaymentHash,
Amount: invoiceRes.ReceivedSat * 1000,
FeesPaid: invoiceRes.Fees * 1000,
CreatedAt: time.UnixMilli(invoiceRes.CreatedAt).Unix(),
Description: invoiceRes.Description,
SettledAt: settledAt,
ExpiresAt: &expiresAt,
DescriptionHash: paymentRequest.DescriptionHash,
}
return transaction, nil
}

Expand Down Expand Up @@ -543,6 +488,39 @@ func (svc *PhoenixService) GetPubkey() string {
return svc.pubkey
}

func phoenixInvoiceToTransaction(invoiceRes *InvoiceResponse) (*lnclient.Transaction, error) {
var settledAt *int64
if invoiceRes.CompletedAt != 0 {
settledAtUnix := time.UnixMilli(invoiceRes.CompletedAt).Unix()
settledAt = &settledAtUnix
}

paymentRequest, err := decodepay.Decodepay(invoiceRes.Invoice)
if err != nil {
logger.Logger.WithFields(logrus.Fields{
"bolt11": invoiceRes.Invoice,
}).Errorf("Failed to decode bolt11 invoice: %v", err)

return nil, err
}

expiresAt := time.UnixMilli(int64(paymentRequest.CreatedAt) * 1000).Add(time.Duration(paymentRequest.Expiry) * time.Second).Unix()

return &lnclient.Transaction{
Type: "incoming",
Invoice: invoiceRes.Invoice,
Preimage: invoiceRes.Preimage,
PaymentHash: invoiceRes.PaymentHash,
Amount: paymentRequest.MSatoshi,
FeesPaid: invoiceRes.Fees * 1000,
CreatedAt: time.UnixMilli(invoiceRes.CreatedAt).Unix(),
Description: invoiceRes.Description,
SettledAt: settledAt,
ExpiresAt: &expiresAt,
DescriptionHash: paymentRequest.DescriptionHash,
}, nil
}

func (svc *PhoenixService) GetCustomNodeCommandDefinitions() []lnclient.CustomNodeCommandDef {
return nil
}
Expand Down
Loading