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

refactor and bugfixes #101

Merged
merged 19 commits into from
Nov 4, 2024
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: 2 additions & 3 deletions api/cashu/keys.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,11 +3,10 @@ package cashu
import (
"crypto/sha256"
"encoding/hex"
"math"
"time"

"github.com/decred/dcrd/dcrec/secp256k1/v4"
"github.com/tyler-smith/go-bip32"
"math"
"time"
)

func DeriveKeysetId(keysets []Keyset) (string, error) {
Expand Down
53 changes: 53 additions & 0 deletions api/cashu/keys_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -47,3 +47,56 @@ func TestGenerateKeysetsAndIdGeneration(t *testing.T) {
}

}

func TestChangeProofsStateToPending(t *testing.T) {

proofs := Proofs{
Proof{
Amount: 1,
State: PROOF_UNSPENT,
},
Proof{
Amount: 2,
State: PROOF_UNSPENT,
},
}
proofs.SetProofsState(PROOF_PENDING)

if proofs[0].State != PROOF_PENDING {
t.Errorf("proof transformation not working, should be: %v ", proofs[1].State)
}
if proofs[1].State != PROOF_PENDING {
t.Errorf("proof transformation not working, should be: %v ", proofs[1].State)

}

}
func TestChangeProofsStateToPendingAndQuoteSet(t *testing.T) {

proofs := Proofs{
Proof{
Amount: 1,
State: PROOF_UNSPENT,
},
Proof{
Amount: 2,
State: PROOF_UNSPENT,
},
}
proofs.SetPendingAndQuoteRef("123")

if proofs[0].State != PROOF_PENDING {
t.Errorf("proof transformation not working, should be: %v ", proofs[1].State)
}
if proofs[0].Quote != "123" {
t.Errorf("proof transformation not working, should be: %v ", "123")
}
if proofs[1].State != PROOF_PENDING {
t.Errorf("proof transformation not working, should be: %v ", proofs[1].State)

}
if proofs[1].Quote != "123" {
t.Errorf("proof transformation not working, should be: %v ", "123")
}

}
51 changes: 36 additions & 15 deletions api/cashu/types.go
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,10 @@ var (
ErrCouldNotDecryptSeed = errors.New("Could not decrypt seed")
ErrKeysetNotFound = errors.New("Keyset not found")
ErrKeysetForProofNotFound = errors.New("Keyset for proof not found")

AlreadyActiveProof = errors.New("Proof already being spent")
AlreadyActiveQuote = errors.New("Quote already being spent")
UsingInactiveKeyset = errors.New("Trying to use an inactive keyset")
)

const (
Expand Down Expand Up @@ -144,14 +148,37 @@ const PROOF_UNSPENT ProofState = "UNSPENT"
const PROOF_SPENT ProofState = "SPENT"
const PROOF_PENDING ProofState = "PENDING"

type Proofs []Proof

func (p *Proofs) SetPendingAndQuoteRef(quote string) {
for i := 0; i < len(*p); i++ {
(*p)[i].State = PROOF_PENDING
(*p)[i].Quote = quote
}
}

func (p *Proofs) SetProofsState(state ProofState) {
for i := 0; i < len(*p); i++ {
(*p)[i].State = state
}
}

func (p *Proofs) SetQuoteReference(quote string) {
for i := 0; i < len(*p); i++ {
(*p)[i].Quote = quote
}
}

type Proof struct {
Amount uint64 `json:"amount"`
Id string `json:"id"`
Secret string `json:"secret"`
C string `json:"C" db:"c"`
Y string `json:"Y" db:"y"`
Witness string `json:"witness" db:"witness"`
SeenAt int64 `json:"seen_at"`
Amount uint64 `json:"amount"`
Id string `json:"id"`
Secret string `json:"secret"`
C string `json:"C" db:"c"`
Y string `json:"Y" db:"y"`
Witness string `json:"witness" db:"witness"`
SeenAt int64 `json:"seen_at"`
State ProofState `json:"state"`
Quote string `json:"quote" db:"quote"`
}

func (p Proof) VerifyWitness(spendCondition *SpendCondition, witness *Witness, pubkeysFromProofs *map[*btcec.PublicKey]bool) (bool, error) {
Expand Down Expand Up @@ -484,7 +511,7 @@ type PostMeltQuoteBolt11Response struct {
}

type PostSwapRequest struct {
Inputs []Proof `json:"inputs"`
Inputs Proofs `json:"inputs"`
Outputs []BlindedMessage `json:"outputs"`
}

Expand All @@ -494,7 +521,7 @@ type PostSwapResponse struct {

type PostMeltBolt11Request struct {
Quote string `json:"quote"`
Inputs []Proof `json:"inputs"`
Inputs Proofs `json:"inputs"`
Outputs []BlindedMessage `json:"outputs"`
}

Expand Down Expand Up @@ -721,9 +748,3 @@ func (b *BlindSignature) VerifyDLEQ(
return hashed_keys_priv.Key.Negate().String() == e.Key.String(), nil

}

type NostrLoginAuth struct {
Nonce string
Activated bool
Expiry int
}
37 changes: 33 additions & 4 deletions cmd/nutmix/htlc_route_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -248,7 +248,17 @@ func TestRoutesHTLCSwapMelt(t *testing.T) {
t.Fatalf("Expected status code 403, got %d", w.Code)
}

if w.Body.String() != `"Invalid preimage"` {
var errorRes cashu.ErrorResponse
err = json.Unmarshal(w.Body.Bytes(), &errorRes)
if err != nil {
t.Fatalf("json.Unmarshal(w.Body.Bytes(), &errorRes): %v", err)
}

if errorRes.Code != cashu.UNKNOWN {
t.Errorf("Expected Invalid Proof, got %s", w.Body.String())
}

if *errorRes.Detail != `Invalid preimage` {
t.Fatalf("Expected response Invalid preimage, got %s", w.Body.String())
}

Expand Down Expand Up @@ -557,7 +567,17 @@ func TestHTLCMultisigSigning(t *testing.T) {
t.Fatalf("Expected status code 403, got %d", w.Code)
}

if w.Body.String() != `"Locktime has passed and no refund key was found"` {
var errorRes cashu.ErrorResponse
err = json.Unmarshal(w.Body.Bytes(), &errorRes)
if err != nil {
t.Fatalf("json.Unmarshal(w.Body.Bytes(), &errorRes): %v", err)
}

if errorRes.Code != cashu.UNKNOWN {
t.Errorf("Expected Invalid Proof, got %s", w.Body.String())
}

if *errorRes.Detail != `Locktime has passed and no refund key was found` {
t.Fatalf("Expected response No valid signatures, got %s", w.Body.String())
}

Expand Down Expand Up @@ -710,8 +730,17 @@ func TestHTLCMultisigSigning(t *testing.T) {
t.Fatalf("Expected status code 403, got %d", w.Code)
}

if w.Body.String() != `"Invalid preimage"` {
t.Fatalf("Expected response No valid signatures, got %s", w.Body.String())
err = json.Unmarshal(w.Body.Bytes(), &errorRes)
if err != nil {
t.Fatalf("json.Unmarshal(w.Body.Bytes(), &errorRes): %v", err)
}

if errorRes.Code != cashu.UNKNOWN {
t.Errorf("Expected Invalid Proof, got %s", w.Body.String())
}

if *errorRes.Detail != `Invalid preimage` {
t.Fatalf("Expected response Invalid preimage, got %s", w.Body.String())
}

// Try swapping with correct signatures and correct preimage
Expand Down
16 changes: 8 additions & 8 deletions cmd/nutmix/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@ import (
"github.com/gin-gonic/gin"
"github.com/joho/godotenv"
"github.com/lescuer97/nutmix/api/cashu"
"github.com/lescuer97/nutmix/internal/database"
"github.com/lescuer97/nutmix/internal/database/postgresql"
"github.com/lescuer97/nutmix/internal/mint"
"github.com/lescuer97/nutmix/internal/routes"
"github.com/lescuer97/nutmix/internal/routes/admin"
Expand Down Expand Up @@ -84,15 +84,15 @@ func main() {
logger.Info("Running in Release mode")
}

pool, err := database.DatabaseSetup(ctx, "migrations")
defer pool.Close()
db, err := postgresql.DatabaseSetup(ctx, "migrations")
defer db.Close()

if err != nil {
logger.Error(fmt.Sprintf("Error conecting to db %+v", err))
log.Panic()
}

seeds, err := database.GetAllSeeds(pool)
seeds, err := db.GetAllSeeds()

if err != nil {
logger.Error(fmt.Sprintf("Could not GetAllSeeds: %v", err))
Expand Down Expand Up @@ -127,7 +127,7 @@ func main() {
log.Panic()
}

err = database.SaveNewSeeds(pool, []cashu.Seed{newSeed})
err = db.SaveNewSeeds([]cashu.Seed{newSeed})

seeds = append(seeds, newSeed)

Expand All @@ -143,7 +143,7 @@ func main() {
}

// remove mint private key from variable
mint, err := mint.SetUpMint(ctx, parsedPrivateKey, seeds, config)
mint, err := mint.SetUpMint(ctx, parsedPrivateKey, seeds, config, db)

// clear mint seeds and privatekey
seeds = []cashu.Seed{}
Expand All @@ -164,9 +164,9 @@ func main() {

r.Use(cors.Default())

routes.V1Routes(r, pool, mint, logger)
routes.V1Routes(r, mint, logger)

admin.AdminRoutes(ctx, r, pool, mint, logger)
admin.AdminRoutes(ctx, r, mint, logger)

logger.Info("Nutmix started in port 8080")

Expand Down
Loading
Loading