-
Notifications
You must be signed in to change notification settings - Fork 4
/
wallet.go
759 lines (640 loc) · 20.6 KB
/
wallet.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
package wallet
import (
"crypto/rand"
"encoding/hex"
"errors"
"fmt"
"net/url"
"slices"
"github.com/decred/dcrd/dcrec/secp256k1/v4"
"github.com/elnosh/gonuts/cashu"
"github.com/elnosh/gonuts/cashu/nuts/nut03"
"github.com/elnosh/gonuts/cashu/nuts/nut04"
"github.com/elnosh/gonuts/cashu/nuts/nut05"
"github.com/elnosh/gonuts/crypto"
"github.com/elnosh/gonuts/mint/lightning"
"github.com/elnosh/gonuts/wallet/storage"
decodepay "github.com/nbd-wtf/ln-decodepay"
)
type Wallet struct {
db storage.DB
// default mint
currentMint *walletMint
// array of mints that have been trusted
mints map[string]walletMint
proofs cashu.Proofs
}
type walletMint struct {
mintURL string
// active keysets from mint
activeKeysets map[string]crypto.Keyset
// list of inactive keysets (if any) from mint
inactiveKeysets map[string]crypto.Keyset
}
// get mint keysets
func mintInfo(mintURL string) (*walletMint, error) {
activeKeysets, err := GetMintActiveKeysets(mintURL)
if err != nil {
return nil, err
}
inactiveKeysets, err := GetMintInactiveKeysets(mintURL)
if err != nil {
return nil, err
}
return &walletMint{mintURL, activeKeysets, inactiveKeysets}, nil
}
// addMint adds the mint to the list of mints trusted by the wallet
func (w *Wallet) addMint(mint string) (*walletMint, error) {
url, err := url.Parse(mint)
if err != nil {
return nil, fmt.Errorf("invalid mint url: %v", err)
}
mintURL := url.String()
mintInfo, err := mintInfo(mintURL)
if err != nil {
return nil, err
}
for _, keyset := range mintInfo.activeKeysets {
w.db.SaveKeyset(&keyset)
}
for _, keyset := range mintInfo.inactiveKeysets {
w.db.SaveKeyset(&keyset)
}
w.mints[mintURL] = *mintInfo
return mintInfo, nil
}
func InitStorage(path string) (storage.DB, error) {
// bolt db atm
return storage.InitBolt(path)
}
func LoadWallet(config Config) (*Wallet, error) {
db, err := InitStorage(config.WalletPath)
if err != nil {
return nil, fmt.Errorf("InitStorage: %v", err)
}
wallet := &Wallet{db: db}
wallet.mints = wallet.getWalletMints()
url, err := url.Parse(config.CurrentMintURL)
if err != nil {
return nil, fmt.Errorf("invalid mint url: %v", err)
}
mintURL := url.String()
currentMint, err := mintInfo(mintURL)
if err != nil {
return nil, err
}
wallet.currentMint = currentMint
_, ok := wallet.mints[mintURL]
if !ok {
for _, keyset := range currentMint.activeKeysets {
db.SaveKeyset(&keyset)
}
for _, keyset := range currentMint.inactiveKeysets {
db.SaveKeyset(&keyset)
}
}
wallet.mints[mintURL] = *currentMint
wallet.proofs = wallet.db.GetProofs()
return wallet, nil
}
func GetMintActiveKeysets(mintURL string) (map[string]crypto.Keyset, error) {
keysetsResponse, err := GetActiveKeysets(mintURL)
if err != nil {
return nil, fmt.Errorf("error getting active keyset from mint: %v", err)
}
activeKeysets := make(map[string]crypto.Keyset)
for i, keyset := range keysetsResponse.Keysets {
activeKeyset := crypto.Keyset{MintURL: mintURL, Unit: keyset.Unit, Active: true}
keys := make(map[uint64]crypto.KeyPair)
for amount, key := range keysetsResponse.Keysets[i].Keys {
pkbytes, err := hex.DecodeString(key)
if err != nil {
return nil, err
}
pubkey, err := secp256k1.ParsePubKey(pkbytes)
if err != nil {
return nil, err
}
keys[amount] = crypto.KeyPair{PublicKey: pubkey}
}
activeKeyset.Keys = keys
id := crypto.DeriveKeysetId(activeKeyset.Keys)
activeKeyset.Id = id
activeKeysets[id] = activeKeyset
}
return activeKeysets, nil
}
func GetMintInactiveKeysets(mintURL string) (map[string]crypto.Keyset, error) {
keysetsResponse, err := GetAllKeysets(mintURL)
if err != nil {
return nil, fmt.Errorf("error getting keysets from mint: %v", err)
}
inactiveKeysets := make(map[string]crypto.Keyset)
for _, keysetRes := range keysetsResponse.Keysets {
if !keysetRes.Active {
keyset := crypto.Keyset{
Id: keysetRes.Id,
MintURL: mintURL,
Unit: keysetRes.Unit,
Active: keysetRes.Active,
}
inactiveKeysets[keyset.Id] = keyset
}
}
return inactiveKeysets, nil
}
// GetBalance returns the total balance aggregated from all proofs
func (w *Wallet) GetBalance() uint64 {
return w.proofs.Amount()
}
// GetBalanceByMints returns a map of string mint
// and a uint64 that represents the balance for that mint
func (w *Wallet) GetBalanceByMints() map[string]uint64 {
mintsBalances := make(map[string]uint64)
for _, mint := range w.mints {
var mintBalance uint64 = 0
for _, keyset := range mint.activeKeysets {
proofs := w.db.GetProofsByKeysetId(keyset.Id)
mintBalance += proofs.Amount()
}
for _, keyset := range mint.inactiveKeysets {
proofs := w.db.GetProofsByKeysetId(keyset.Id)
mintBalance += proofs.Amount()
}
mintsBalances[mint.mintURL] = mintBalance
}
return mintsBalances
}
// RequestMint requests a mint quote to the wallet's current mint
// for the specified amount
func (w *Wallet) RequestMint(amount uint64) (*nut04.PostMintQuoteBolt11Response, error) {
mintRequest := nut04.PostMintQuoteBolt11Request{Amount: amount, Unit: "sat"}
mintResponse, err := PostMintQuoteBolt11(w.currentMint.mintURL, mintRequest)
if err != nil {
return nil, err
}
bolt11, err := decodepay.Decodepay(mintResponse.Request)
if err != nil {
return nil, fmt.Errorf("error decoding bolt11 invoice: %v", err)
}
invoice := lightning.Invoice{
Id: mintResponse.Quote,
PaymentRequest: mintResponse.Request,
PaymentHash: bolt11.PaymentHash,
Amount: amount,
Expiry: mintResponse.Expiry,
}
err = w.db.SaveInvoice(invoice)
if err != nil {
return nil, err
}
return mintResponse, nil
}
// CheckQuotePaid reports whether the mint quote has been paid
func (w *Wallet) CheckQuotePaid(quoteId string) bool {
mintQuote, err := GetMintQuoteState(w.currentMint.mintURL, quoteId)
if err != nil {
return false
}
return mintQuote.Paid
}
// MintTokens will check whether if the mint quote has been paid.
// If yes, it will create blinded messages that will send to the mint
// to get the blinded signatures.
// If successful, it will unblind the signatures to generate proofs
// and store the proofs in the db.
func (w *Wallet) MintTokens(quoteId string) (cashu.Proofs, error) {
mintQuote, err := GetMintQuoteState(w.currentMint.mintURL, quoteId)
if err != nil {
return nil, err
}
if !mintQuote.Paid {
return nil, errors.New("invoice not paid")
}
invoice, err := w.GetInvoiceByPaymentRequest(mintQuote.Request)
if err != nil {
return nil, err
}
if invoice == nil {
return nil, errors.New("invoice not found")
}
// create blinded messages
activeKeyset := w.GetActiveSatKeyset()
blindedMessages, secrets, rs, err := w.CreateBlindedMessages(invoice.Amount, activeKeyset)
if err != nil {
return nil, fmt.Errorf("error creating blinded messages: %v", err)
}
// request mint to sign the blinded messages
postMintRequest := nut04.PostMintBolt11Request{Quote: quoteId, Outputs: blindedMessages}
mintResponse, err := PostMintBolt11(w.currentMint.mintURL, postMintRequest)
if err != nil {
return nil, err
}
// unblind the signatures from the promises and build the proofs
proofs, err := w.ConstructProofs(mintResponse.Signatures, secrets, rs, &activeKeyset)
if err != nil {
return nil, fmt.Errorf("error constructing proofs: %v", err)
}
// mark invoice as redeemed
invoice.Settled = true
invoice.Redeemed = true
err = w.db.SaveInvoice(*invoice)
if err != nil {
return nil, err
}
// store proofs in db
err = w.saveProofs(proofs)
if err != nil {
return nil, fmt.Errorf("error storing proofs: %v", err)
}
return proofs, nil
}
// Send will return a cashu token with proofs for the given amount
func (w *Wallet) Send(amount uint64, mintURL string) (*cashu.Token, error) {
proofsToSend, err := w.getProofsForAmount(amount, mintURL)
if err != nil {
return nil, err
}
token := cashu.NewToken(proofsToSend, mintURL, "sat")
return &token, nil
}
// Receives Cashu token. If swap is true, it will swap the funds to the configured default mint.
// If false, it will add the proofs from the mint and add that mint to the list of trusted mints.
func (w *Wallet) Receive(token cashu.Token, swap bool) (uint64, error) {
if swap {
trustedMintProofs, err := w.swapToTrusted(token)
if err != nil {
return 0, fmt.Errorf("error swapping token to trusted mint: %v", err)
}
return trustedMintProofs.Amount(), nil
} else {
proofsToSwap := make(cashu.Proofs, 0)
for _, tokenProof := range token.Token {
proofsToSwap = append(proofsToSwap, tokenProof.Proofs...)
}
// add mint to list of trusted mints
tokenMintURL := token.Token[0].Mint
mint, err := w.addMint(tokenMintURL)
if err != nil {
return 0, err
}
var activeSatKeyset crypto.Keyset
// for now just taking first keyset val from map and assign it as sat keyset
for _, k := range mint.activeKeysets {
activeSatKeyset = k
break
}
// create blinded messages
outputs, secrets, rs, err := w.CreateBlindedMessages(token.TotalAmount(), activeSatKeyset)
if err != nil {
return 0, fmt.Errorf("CreateBlindedMessages: %v", err)
}
// make swap request to mint
swapRequest := nut03.PostSwapRequest{Inputs: proofsToSwap, Outputs: outputs}
swapResponse, err := PostSwap(tokenMintURL, swapRequest)
if err != nil {
return 0, err
}
// unblind signatures to get proofs and save them to db
proofs, err := w.ConstructProofs(swapResponse.Signatures, secrets, rs, &activeSatKeyset)
if err != nil {
return 0, fmt.Errorf("wallet.ConstructProofs: %v", err)
}
w.saveProofs(proofs)
return proofs.Amount(), nil
}
}
// swapToTrusted will swap the proofs from mint in the token
// to the wallet's configured default mint
func (w *Wallet) swapToTrusted(token cashu.Token) (cashu.Proofs, error) {
invoicePct := 0.99
tokenAmount := token.TotalAmount()
tokenMintURL := token.Token[0].Mint
amount := float64(tokenAmount) * invoicePct
proofsToSwap := make(cashu.Proofs, 0)
for _, tokenProof := range token.Token {
proofsToSwap = append(proofsToSwap, tokenProof.Proofs...)
}
var mintResponse *nut04.PostMintQuoteBolt11Response
var meltQuoteResponse *nut05.PostMeltQuoteBolt11Response
var err error
for {
// request a mint quote from the configured default mint
// this will generate an invoice from the trusted mint
mintResponse, err = w.RequestMint(uint64(amount))
if err != nil {
return nil, fmt.Errorf("error requesting mint: %v", err)
}
// request melt quote from untrusted mint which will
// request mint to pay invoice generated from trusted mint in previous mint request
meltRequest := nut05.PostMeltQuoteBolt11Request{Request: mintResponse.Request, Unit: "sat"}
meltQuoteResponse, err = PostMeltQuoteBolt11(tokenMintURL, meltRequest)
if err != nil {
return nil, fmt.Errorf("error with melt request: %v", err)
}
// if amount in token is less than amount asked from mint in melt request,
// lower the amount for mint request
if meltQuoteResponse.Amount+meltQuoteResponse.FeeReserve > tokenAmount {
invoicePct -= 0.01
amount *= invoicePct
} else {
break
}
}
// request untrusted mint to pay invoice generated from trusted mint
meltBolt11Request := nut05.PostMeltBolt11Request{Quote: meltQuoteResponse.Quote, Inputs: proofsToSwap}
meltBolt11Response, err := PostMeltBolt11(tokenMintURL, meltBolt11Request)
if err != nil {
return nil, fmt.Errorf("error melting token: %v", err)
}
// if melt request was successful and untrusted mint paid the invoice,
// make mint request to trusted mint to get valid proofs
if meltBolt11Response.Paid {
proofs, err := w.MintTokens(mintResponse.Quote)
if err != nil {
return nil, fmt.Errorf("error minting tokens: %v", err)
}
return proofs, nil
} else {
return nil, errors.New("mint could not pay lightning invoice")
}
}
// Melt will request the mint to pay the given invoice
func (w *Wallet) Melt(invoice string, mint string) (*nut05.PostMeltBolt11Response, error) {
selectedMint, ok := w.mints[mint]
if !ok {
return nil, errors.New("mint does not exist")
}
meltRequest := nut05.PostMeltQuoteBolt11Request{Request: invoice, Unit: "sat"}
meltQuoteResponse, err := PostMeltQuoteBolt11(selectedMint.mintURL, meltRequest)
if err != nil {
return nil, err
}
amountNeeded := meltQuoteResponse.Amount + meltQuoteResponse.FeeReserve
proofs, err := w.getProofsForAmount(amountNeeded, mint)
if err != nil {
return nil, err
}
meltBolt11Request := nut05.PostMeltBolt11Request{Quote: meltQuoteResponse.Quote, Inputs: proofs}
meltBolt11Response, err := PostMeltBolt11(selectedMint.mintURL, meltBolt11Request)
if err != nil {
return nil, err
}
// only delete proofs after invoice has been paid
if meltBolt11Response.Paid {
for _, proof := range proofs {
w.db.DeleteProof(proof.Secret)
}
}
return meltBolt11Response, nil
}
// GetProofsByMint will return an array of proofs that are from
// the passed mint
func (w *Wallet) GetProofsByMint(mintURL string) (cashu.Proofs, error) {
selectedMint, ok := w.mints[mintURL]
if !ok {
return nil, errors.New("mint does not exist")
}
proofs := cashu.Proofs{}
for _, keyset := range selectedMint.activeKeysets {
keysetProofs := w.db.GetProofsByKeysetId(keyset.Id)
proofs = append(proofs, keysetProofs...)
}
for _, keyset := range selectedMint.inactiveKeysets {
keysetProofs := w.db.GetProofsByKeysetId(keyset.Id)
proofs = append(proofs, keysetProofs...)
}
return proofs, nil
}
// getProofsForAmount will return proofs from mint that equal to given amount.
// It returns error if wallet does not have enough proofs to fulfill amount
func (w *Wallet) getProofsForAmount(amount uint64, mintURL string) (cashu.Proofs, error) {
selectedMint, ok := w.mints[mintURL]
if !ok {
return nil, errors.New("mint does not exist")
}
balanceByMints := w.GetBalanceByMints()
balance := balanceByMints[mintURL]
if balance < amount {
return nil, errors.New("not enough funds in selected mint")
}
activeKeysetProofs := cashu.Proofs{}
inactiveKeysetProofs := cashu.Proofs{}
mintProofs, err := w.GetProofsByMint(mintURL)
if err != nil {
return nil, err
}
// use proofs from inactive keysets first
for _, proof := range mintProofs {
isInactive := false
for _, inactiveKeyset := range selectedMint.inactiveKeysets {
if proof.Id == inactiveKeyset.Id {
isInactive = true
break
}
}
if isInactive {
inactiveKeysetProofs = append(inactiveKeysetProofs, proof)
} else {
activeKeysetProofs = append(activeKeysetProofs, proof)
}
}
selectedProofs := cashu.Proofs{}
var currentProofsAmount uint64 = 0
addKeysetProofs := func(proofs cashu.Proofs) {
if currentProofsAmount < amount {
for _, proof := range proofs {
selectedProofs = append(selectedProofs, proof)
currentProofsAmount += proof.Amount
if currentProofsAmount == amount {
for _, proof := range selectedProofs {
w.db.DeleteProof(proof.Secret)
}
} else if currentProofsAmount > amount {
break
}
}
}
}
addKeysetProofs(inactiveKeysetProofs)
addKeysetProofs(activeKeysetProofs)
var activeSatKeyset crypto.Keyset
for _, k := range selectedMint.activeKeysets {
activeSatKeyset = k
break
}
// blinded messages for send amount
send, secrets, rs, err := w.CreateBlindedMessages(amount, activeSatKeyset)
if err != nil {
return nil, err
}
// blinded messages for change amount
change, changeSecrets, changeRs, err := w.CreateBlindedMessages(currentProofsAmount-amount, activeSatKeyset)
if err != nil {
return nil, err
}
blindedMessages := make(cashu.BlindedMessages, len(send))
copy(blindedMessages, send)
blindedMessages = append(blindedMessages, change...)
secrets = append(secrets, changeSecrets...)
rs = append(rs, changeRs...)
// sort messages, secrets and rs
for i := 0; i < len(blindedMessages)-1; i++ {
for j := i + 1; j < len(blindedMessages); j++ {
if blindedMessages[i].Amount > blindedMessages[j].Amount {
// Swap blinded messages
blindedMessages[i], blindedMessages[j] = blindedMessages[j], blindedMessages[i]
// Swap secrets
secrets[i], secrets[j] = secrets[j], secrets[i]
// Swap rs
rs[i], rs[j] = rs[j], rs[i]
}
}
}
swapRequest := nut03.PostSwapRequest{Inputs: selectedProofs, Outputs: blindedMessages}
swapResponse, err := PostSwap(selectedMint.mintURL, swapRequest)
if err != nil {
return nil, err
}
for _, proof := range selectedProofs {
w.db.DeleteProof(proof.Secret)
}
proofs, err := w.ConstructProofs(swapResponse.Signatures, secrets, rs, &activeSatKeyset)
if err != nil {
return nil, fmt.Errorf("wallet.ConstructProofs: %v", err)
}
proofsToSend := make(cashu.Proofs, len(send))
for i, sendmsg := range send {
for j, proof := range proofs {
if sendmsg.Amount == proof.Amount {
proofsToSend[i] = proof
proofs = slices.Delete(proofs, j, j+1)
break
}
}
}
// remaining proofs are change proofs to save to db
w.saveProofs(proofs)
return proofsToSend, nil
}
func NewBlindedMessage(id string, amount uint64, B_ *secp256k1.PublicKey) cashu.BlindedMessage {
B_str := hex.EncodeToString(B_.SerializeCompressed())
return cashu.BlindedMessage{Amount: amount, B_: B_str, Id: id}
}
// returns Blinded messages, secrets - [][]byte, and list of r
func (w *Wallet) CreateBlindedMessages(amount uint64, keyset crypto.Keyset) (cashu.BlindedMessages, []string, []*secp256k1.PrivateKey, error) {
splitAmounts := cashu.AmountSplit(amount)
splitLen := len(splitAmounts)
blindedMessages := make(cashu.BlindedMessages, splitLen)
secrets := make([]string, splitLen)
rs := make([]*secp256k1.PrivateKey, splitLen)
for i, amt := range splitAmounts {
// generate new private key r
r, err := secp256k1.GeneratePrivateKey()
if err != nil {
return nil, nil, nil, err
}
var B_ *secp256k1.PublicKey
var secret string
// generate random secret until it finds valid point
for {
secretBytes := make([]byte, 32)
_, err = rand.Read(secretBytes)
if err != nil {
return nil, nil, nil, err
}
secret = hex.EncodeToString(secretBytes)
B_, r, err = crypto.BlindMessage(secret, r)
if err == nil {
break
}
}
blindedMessage := NewBlindedMessage(keyset.Id, amt, B_)
blindedMessages[i] = blindedMessage
secrets[i] = secret
rs[i] = r
}
return blindedMessages, secrets, rs, nil
}
// ConstructProofs unblinds the blindedSignatures and returns the proofs
func (w *Wallet) ConstructProofs(blindedSignatures cashu.BlindedSignatures,
secrets []string, rs []*secp256k1.PrivateKey, keyset *crypto.Keyset) (cashu.Proofs, error) {
if len(blindedSignatures) != len(secrets) && len(blindedSignatures) != len(rs) {
return nil, errors.New("lengths do not match")
}
proofs := make(cashu.Proofs, len(blindedSignatures))
for i, blindedSignature := range blindedSignatures {
C_bytes, err := hex.DecodeString(blindedSignature.C_)
if err != nil {
return nil, err
}
C_, err := secp256k1.ParsePubKey(C_bytes)
if err != nil {
return nil, err
}
K := keyset.Keys[blindedSignature.Amount].PublicKey
C := crypto.UnblindSignature(C_, rs[i], K)
Cstr := hex.EncodeToString(C.SerializeCompressed())
proof := cashu.Proof{Amount: blindedSignature.Amount,
Secret: secrets[i], C: Cstr, Id: blindedSignature.Id}
proofs[i] = proof
}
return proofs, nil
}
func (w *Wallet) GetActiveSatKeyset() crypto.Keyset {
var activeKeyset crypto.Keyset
for _, keyset := range w.currentMint.activeKeysets {
if keyset.Unit == "sat" {
activeKeyset = keyset
break
}
}
return activeKeyset
}
func (w *Wallet) getWalletMints() map[string]walletMint {
walletMints := make(map[string]walletMint)
keysets := w.db.GetKeysets()
for k, mintKeysets := range keysets {
activeKeysets := make(map[string]crypto.Keyset)
inactiveKeysets := make(map[string]crypto.Keyset)
for _, keyset := range mintKeysets {
if keyset.Active {
activeKeysets[keyset.Id] = keyset
} else {
inactiveKeysets[keyset.Id] = keyset
}
}
walletMints[k] = walletMint{mintURL: k, activeKeysets: activeKeysets, inactiveKeysets: inactiveKeysets}
}
return walletMints
}
// CurrentMint returns the current mint url
func (w *Wallet) CurrentMint() string {
return w.currentMint.mintURL
}
func (w *Wallet) TrustedMints() map[string]walletMint {
return w.mints
}
func (w *Wallet) saveProofs(proofs cashu.Proofs) error {
for _, proof := range proofs {
err := w.db.SaveProof(proof)
if err != nil {
return err
}
}
w.proofs = append(w.proofs, proofs...)
return nil
}
func (w *Wallet) GetInvoiceByPaymentRequest(pr string) (*lightning.Invoice, error) {
bolt11, err := decodepay.Decodepay(pr)
if err != nil {
return nil, fmt.Errorf("invalid payment request: %v", err)
}
return w.db.GetInvoice(bolt11.PaymentHash), nil
}
func (w *Wallet) GetInvoiceByPaymentHash(hash string) *lightning.Invoice {
return w.db.GetInvoice(hash)
}
func (w *Wallet) GetAllInvoices() []lightning.Invoice {
return w.db.GetInvoices()
}