Skip to content

Commit

Permalink
mint - include DLEQ proofs in blind signatures
Browse files Browse the repository at this point in the history
  • Loading branch information
elnosh committed Sep 20, 2024
1 parent d595f5a commit 7270867
Show file tree
Hide file tree
Showing 5 changed files with 67 additions and 13 deletions.
18 changes: 14 additions & 4 deletions mint/mint.go
Original file line number Diff line number Diff line change
Expand Up @@ -831,12 +831,22 @@ func (m *Mint) signBlindedMessages(blindedMessages cashu.BlindedMessages) (cashu
C_ := crypto.SignBlindedMessage(B_, k)
C_hex := hex.EncodeToString(C_.SerializeCompressed())

blindedSignature := cashu.BlindedSignature{Amount: msg.Amount,
C_: C_hex, Id: keyset.Id}
// DLEQ proof
e, s := crypto.GenerateDLEQ(k, B_, C_)

blindedSignature := cashu.BlindedSignature{
Amount: msg.Amount,
C_: C_hex,
Id: keyset.Id,
DLEQ: &cashu.DLEQProof{
E: hex.EncodeToString(e.Serialize()),
S: hex.EncodeToString(s.Serialize()),
},
}

blindedSignatures[i] = blindedSignature

if err := m.db.SaveBlindSignature(msg.B_, C_hex, msg.Id, msg.Amount); err != nil {
if err := m.db.SaveBlindSignature(msg.B_, blindedSignature); err != nil {
msg := fmt.Sprintf("error saving signatures: %v", err)
return nil, cashu.BuildCashuError(msg, cashu.DBErrCode)
}
Expand All @@ -845,7 +855,7 @@ func (m *Mint) signBlindedMessages(blindedMessages cashu.BlindedMessages) (cashu
return blindedSignatures, nil
}

// requestInvoices requests an invoice from the Lightning backend
// requestInvoice requests an invoice from the Lightning backend
// for the given amount
func (m *Mint) requestInvoice(amount uint64) (*lightning.Invoice, error) {
invoice, err := m.lightningClient.CreateInvoice(amount)
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@

ALTER TABLE blind_signatures DROP COLUMN e;
ALTER TABLE blind_signatures DROP COLUMN s;
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@

ALTER TABLE blind_signatures ADD COLUMN e TEXT;
ALTER TABLE blind_signatures ADD COLUMN s TEXT;
54 changes: 46 additions & 8 deletions mint/storage/sqlite/sqlite.go
Original file line number Diff line number Diff line change
Expand Up @@ -320,33 +320,52 @@ func (sqlite *SQLiteDB) UpdateMeltQuote(quoteId, preimage string, state nut05.St
return nil
}

func (sqlite *SQLiteDB) SaveBlindSignature(B_, C_, keysetId string, amount uint64) error {
func (sqlite *SQLiteDB) SaveBlindSignature(B_ string, blindSignature cashu.BlindedSignature) error {
_, err := sqlite.db.Exec(`
INSERT INTO blind_signatures (b_, c_, keyset_id, amount) VALUES (?, ?, ?, ?)`,
B_, C_, keysetId, amount,
INSERT INTO blind_signatures (b_, c_, keyset_id, amount, e, s) VALUES (?, ?, ?, ?, ?, ?)`,
B_,
blindSignature.C_,
blindSignature.Id,
blindSignature.Amount,
blindSignature.DLEQ.E,
blindSignature.DLEQ.S,
)
return err
}

func (sqlite *SQLiteDB) GetBlindSignature(B_ string) (cashu.BlindedSignature, error) {
row := sqlite.db.QueryRow("SELECT amount, c_, keyset_id FROM blind_signatures WHERE b_ = ?", B_)
row := sqlite.db.QueryRow("SELECT amount, c_, keyset_id, e, s FROM blind_signatures WHERE b_ = ?", B_)

var signature cashu.BlindedSignature
var e sql.NullString
var s sql.NullString

err := row.Scan(
&signature.Amount,
&signature.C_,
&signature.Id,
&e,
&s,
)
if err != nil {
return cashu.BlindedSignature{}, err
}

if !e.Valid || !s.Valid {
signature.DLEQ = nil
} else {
signature.DLEQ = &cashu.DLEQProof{
E: e.String,
S: s.String,
}
}

return signature, nil
}

func (sqlite *SQLiteDB) GetBlindSignatures(B_s []string) (cashu.BlindedSignatures, error) {
signatures := cashu.BlindedSignatures{}
query := `SELECT amount, c_, keyset_id FROM blind_signatures WHERE b_ in (?` + strings.Repeat(",?", len(B_s)-1) + `)`
query := `SELECT amount, c_, keyset_id, e, s FROM blind_signatures WHERE b_ in (?` + strings.Repeat(",?", len(B_s)-1) + `)`

args := make([]any, len(B_s))
for i, B_ := range B_s {
Expand All @@ -360,12 +379,31 @@ func (sqlite *SQLiteDB) GetBlindSignatures(B_s []string) (cashu.BlindedSignature
defer rows.Close()

for rows.Next() {
var blindSignature cashu.BlindedSignature
err := rows.Scan(&blindSignature.Amount, &blindSignature.C_, &blindSignature.Id)
var signature cashu.BlindedSignature
var e sql.NullString
var s sql.NullString

err := rows.Scan(
&signature.Amount,
&signature.C_,
&signature.Id,
&e,
&s,
)
if err != nil {
return nil, err
}
signatures = append(signatures, blindSignature)

if !e.Valid || !s.Valid {
signature.DLEQ = nil
} else {
signature.DLEQ = &cashu.DLEQProof{
E: e.String,
S: s.String,
}
}

signatures = append(signatures, signature)
}

return signatures, nil
Expand Down
2 changes: 1 addition & 1 deletion mint/storage/storage.go
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,7 @@ type MintDB interface {
GetMeltQuote(string) (MeltQuote, error)
UpdateMeltQuote(quoteId string, preimage string, state nut05.State) error

SaveBlindSignature(B_, C_, keysetId string, amount uint64) error
SaveBlindSignature(B_ string, blindSignature cashu.BlindedSignature) error
GetBlindSignature(B_ string) (cashu.BlindedSignature, error)
GetBlindSignatures(B_s []string) (cashu.BlindedSignatures, error)

Expand Down

0 comments on commit 7270867

Please sign in to comment.