Skip to content
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
7 changes: 6 additions & 1 deletion .github/workflows/golangci-lint.yml
Original file line number Diff line number Diff line change
Expand Up @@ -29,5 +29,10 @@ jobs:
just gen-templ
just web-install
just web-build-prod
- name: golangci-lint
- name: Install golangci-lint
uses: golangci/golangci-lint-action@v9
with:
version: v2.6.1
install-only: true
- name: Run lint
run: just lint
11 changes: 11 additions & 0 deletions .golangci.yml
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,8 @@ linters:
- bidichk # Checks for dangerous unicode character sequences
- bodyclose # Checks HTTP response body is closed
- contextcheck # Check for non-inherited context
- musttag # enforces field tags in (un)marshaled structs
- staticcheck # is a go vet on steroids, applying a ton of static analysis checks

# Bug detection
- staticcheck # Comprehensive static analysis
Expand All @@ -26,6 +28,15 @@ linters:
- unparam # Unused function parameters
- misspell # Commonly misspelled words
- exhaustruct # [highly recommend to enable] checks if all structure fields are initialized
- iface # checks the incorrect use of interfaces, helping developers avoid interface pollution
- sqlclosecheck # checks that sql.Rows and sql.Stmt are closed
- sloglint # ensure consistent code style when using log/slog
- unconvert # removes unnecessary type conversions
- unparam # reports unused function parameters
- unqueryvet # detects SELECT * in SQL queries and SQL builders, encouraging explicit column selection
- whitespace # detects leading and trailing whitespace
# - noinlineerr # disallows inline error handling `if err := ...; err != nil {`
- prealloc # [premature optimization, but can be used in some cases] finds slice declarations that could potentially be preallocated

settings:
gosec:
Expand Down
2 changes: 0 additions & 2 deletions api/cashu/auth.go
Original file line number Diff line number Diff line change
Expand Up @@ -80,7 +80,6 @@ type Nut22Info struct {
}

func ConvertRouteListToProtectedRouteList(list []string) []ProtectedRoute {

routes := []ProtectedRoute{}

for _, v := range list {
Expand All @@ -92,7 +91,6 @@ func ConvertRouteListToProtectedRouteList(list []string) []ProtectedRoute {
Path: v,
},
)

}
return routes
}
Expand Down
49 changes: 34 additions & 15 deletions api/cashu/errors.go
Original file line number Diff line number Diff line change
Expand Up @@ -11,27 +11,42 @@ var (
ErrDifferentInputOutputUnit = errors.New("different input output unit")
ErrNotEnoughtProofs = errors.New("not enough proofs")
ErrProofSpent = errors.New("proof already spent")
ErrProofPending = errors.New("Proofs are pending")
ErrBlindMessageAlreadySigned = errors.New("blind message already signed")
ErrCommonSecretNotCorrectSize = errors.New("proof secret is not correct size")
ErrUnknown = errors.New("unknown error")
ErrPaymentMethodNotSupported = errors.New("payment method not supported")

ErrMintRequestAlreadyIssued = errors.New("mint request already issued")
ErrAmountNotEqualToInvoice = errors.New("Amount in request does not equal invoice")

ErrMintintDisabled = errors.New("minting is disabled")
ErrAmountOutsideLimit = errors.New("amount is outside the limit")
ErrRequestNotPaid = errors.New("request not paid yet")

ErrAmountlessInvoiceNotSupported = errors.New("Amount less invoices not supported")

ErrKeysetNotKnow = errors.New("keyset not known")
)

type ErrorCode uint

const (
PROOF_VERIFICATION_FAILED ErrorCode = 10001

PROOF_ALREADY_SPENT ErrorCode = 11001
PROOFS_PENDING ErrorCode = 11002
OUTPUTS_ALREADY_SIGNED ErrorCode = 11003
OUTPUTS_PENDING ErrorCode = 11004
TRANSACTION_NOT_BALANCED ErrorCode = 11005
INSUFICIENT_FEE ErrorCode = 11006
DUPLICATE_INPUTS ErrorCode = 11007
DUPLICATE_OUTPUTS ErrorCode = 11008
MULTIPLE_UNITS_OUTPUT_INPUT ErrorCode = 11009
INPUT_OUTPUT_NOT_SAME_UNIT ErrorCode = 11010
UNIT_NOT_SUPPORTED ErrorCode = 11013
PROOF_ALREADY_SPENT ErrorCode = 11001
PROOFS_PENDING ErrorCode = 11002
OUTPUTS_ALREADY_SIGNED ErrorCode = 11003
OUTPUTS_PENDING ErrorCode = 11004
TRANSACTION_NOT_BALANCED ErrorCode = 11005
INSUFICIENT_OUTSIDE_LIMIT ErrorCode = 11006
DUPLICATE_INPUTS ErrorCode = 11007
DUPLICATE_OUTPUTS ErrorCode = 11008
MULTIPLE_UNITS_OUTPUT_INPUT ErrorCode = 11009
INPUT_OUTPUT_NOT_SAME_UNIT ErrorCode = 11010
AMOUNT_LESS_INVOICE_NOT_SUPPORTED ErrorCode = 11011
AMOUNT_NOT_EQUAL_TO_INVOICE ErrorCode = 11012
UNIT_NOT_SUPPORTED ErrorCode = 11013

KEYSET_NOT_KNOW ErrorCode = 12001
INACTIVE_KEYSET ErrorCode = 12002
Expand All @@ -58,7 +73,6 @@ const (
)

func (e ErrorCode) String() string {

error := ""
switch e {
case OUTPUTS_ALREADY_SIGNED:
Expand All @@ -76,8 +90,8 @@ func (e ErrorCode) String() string {
error = "Transaction is not balanced (inputs != outputs)"
case UNIT_NOT_SUPPORTED:
error = "Unit in request is not supported"
case INSUFICIENT_FEE:
error = "Insufficient fee"
case INSUFICIENT_OUTSIDE_LIMIT:
error = "Amount outside limit"
case DUPLICATE_INPUTS:
error = "Duplicate inputs provided"
case DUPLICATE_OUTPUTS:
Expand All @@ -86,6 +100,10 @@ func (e ErrorCode) String() string {
error = "Inputs/Outputs of multiple units"
case INPUT_OUTPUT_NOT_SAME_UNIT:
error = "Inputs and outputs are not same unit"
case AMOUNT_NOT_EQUAL_TO_INVOICE:
error = "Amount in request does not equal invoice"
case AMOUNT_LESS_INVOICE_NOT_SUPPORTED:
error = "Amountless invoices are not supported"

case KEYSET_NOT_KNOW:
error = "Keyset is not known"
Expand Down Expand Up @@ -120,6 +138,8 @@ func (e ErrorCode) String() string {
error = "Maximum Blind auth token amounts execeeded"
case MAXIMUM_BAT_RATE_LIMIT_EXCEEDED:
error = "Maximum BAT rate limit execeeded"
case UNKNOWN:
error = "Unknown error"
}

return error
Expand All @@ -132,7 +152,6 @@ type ErrorResponse struct {
}

func ErrorCodeToResponse(code ErrorCode, detail *string) ErrorResponse {

return ErrorResponse{
Code: code,
Error: code.String(),
Expand Down
6 changes: 2 additions & 4 deletions api/cashu/erros_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,15 +3,13 @@ package cashu
import "testing"

func TestCreatingAnErrorResponse(t *testing.T) {

response := ErrorCodeToResponse(INSUFICIENT_FEE, nil)
response := ErrorCodeToResponse(INSUFICIENT_OUTSIDE_LIMIT, nil)

if response.Code != 11006 {
t.Errorf("Did not get the correct error node.")
}

if response.Error != "Insufficient fee" {
if response.Error != "Amount outside limit" {
t.Errorf("Incorrect error string")
}

}
5 changes: 2 additions & 3 deletions api/cashu/keys.go
Original file line number Diff line number Diff line change
Expand Up @@ -51,7 +51,6 @@ func sortPubkeyMapToOrganizedArray(pubkeyMap map[uint64]*secp256k1.PublicKey) []
return int(a.Amount) - int(b.Amount)
})
return arrayPubkeys

}

func generateKeysetV2Preimage(sortedPubkeyArray []pubkeyWithAmount, unit string, fee uint, finalExpiry *time.Time) string {
Expand Down Expand Up @@ -83,7 +82,7 @@ func DeriveKeysetIdV2(pubKeysMap map[uint64]*secp256k1.PublicKey, unit string, f
}

func GenerateKeysets(versionKey *bip32.Key, values []uint64, seed Seed) ([]MintKey, error) {
var keysets []MintKey
var keysets = make([]MintKey, len(values))

// Get the current time
currentTime := time.Now()
Expand All @@ -109,7 +108,7 @@ func GenerateKeysets(versionKey *bip32.Key, values []uint64, seed Seed) ([]MintK
FinalExpiry: seed.FinalExpiry,
}

keysets = append(keysets, keyset)
keysets[i] = keyset
}

return keysets, nil
Expand Down
4 changes: 1 addition & 3 deletions api/cashu/melt.go
Original file line number Diff line number Diff line change
Expand Up @@ -42,7 +42,6 @@ func (meltRequest *MeltRequestDB) GetPostMeltQuoteResponse() PostMeltQuoteBolt11
Unit: meltRequest.Unit,
Change: []BlindSignature{},
}

}

type PostMeltQuoteBolt11Options struct {
Expand Down Expand Up @@ -77,7 +76,7 @@ type PostMeltQuoteBolt11Response struct {
type PostMeltBolt11Request struct {
Quote string `json:"quote"`
Inputs Proofs `json:"inputs"`
Outputs []BlindedMessage `json:"outputs"`
Outputs BlindedMessages `json:"outputs"`
}

func (p *PostMeltBolt11Request) ValidateSigflag() error {
Expand Down Expand Up @@ -156,7 +155,6 @@ func (p *PostMeltBolt11Request) verifyConditions() error {
if spendCondition.Data.Tags.originalTag != firstSpendCondition.Data.Tags.originalTag {
return fmt.Errorf("not same tags %w", ErrInvalidSpendCondition)
}

}
return nil
}
Expand Down
2 changes: 0 additions & 2 deletions api/cashu/melt_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -44,7 +44,6 @@ func TestMeltRequestMsg(t *testing.T) {
if hex.EncodeToString(hashMessage[:]) != "9efa1067cc7dc870f4074f695115829c3cd817a6866c3b84e9814adf3c3cf262" {
t.Errorf("hash message is wrong %v", msg)
}

}

func TestMeltRequestValidSignature(t *testing.T) {
Expand Down Expand Up @@ -110,6 +109,5 @@ func TestMeltRequestValidMultiSig(t *testing.T) {
err = meltRequest.ValidateSigflag()
if err != nil {
t.Errorf("there should not have been any error on multisig! %+v ", err)

}
}
5 changes: 0 additions & 5 deletions api/cashu/proofs.go
Original file line number Diff line number Diff line change
Expand Up @@ -326,13 +326,11 @@ func (p Proof) IsProofSpendConditioned() (bool, *SpendCondition, error) {
spendCondition, err := p.parseSpendCondition()
if err != nil {
return false, nil, fmt.Errorf("p.parseSpendCondition(). %w", err)

}
return true, spendCondition, nil
}

func (p Proof) HashSecretToCurve() (Proof, error) {

// Get Hash to curve of secret
parsedProof := []byte(p.Secret)

Expand Down Expand Up @@ -379,7 +377,6 @@ func (p *Proof) Sign(privkey *secp256k1.PrivateKey) error {
return nil
}
func (p *Proof) AddPreimage(preimage string) error {

var witness Witness
if p.Witness == "" {
witness = Witness{
Expand Down Expand Up @@ -446,7 +443,6 @@ func VerifyProofsSpendConditions(proofs Proofs) error {
return fmt.Errorf("proof.IsProofSpendConditioned(). %+v", err)
}
if isLocked {

err = spendCondition.CheckValid()
if err != nil {
return fmt.Errorf("spendCondition.CheckValid(). %w", err)
Expand All @@ -469,7 +465,6 @@ func VerifyProofsSpendConditions(proofs Proofs) error {
return ErrInvalidSpendCondition
}
}

}
if !isLocked {
if len(proof.Secret) != 64 {
Expand Down
5 changes: 0 additions & 5 deletions api/cashu/proofs_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -106,13 +106,10 @@ func TestCheckP2PKProof(t *testing.T) {
valid, err := proof.VerifyP2PK(spendCondition)
if err != nil {
t.Errorf("should not have errored. %+v", err)

}
if !valid {

t.Errorf("proof should have been valid")
}

}

func TestCheckP2PKProofInvalidSignature(t *testing.T) {
Expand Down Expand Up @@ -164,7 +161,6 @@ func TestCheckP2PKProofValidMultisig2of2(t *testing.T) {
valid, err := proof.VerifyP2PK(spendCondition)
if err != nil {
t.Errorf("should not have errored. %+v", err)

}
if !valid {
t.Errorf("proof should have been valid")
Expand Down Expand Up @@ -219,7 +215,6 @@ func TestCheckP2PKProofWithSpendableLocktime(t *testing.T) {
valid, err := proof.VerifyP2PK(spendCondition)
if err != nil {
t.Errorf("should not have errored. %+v", err)

}
if !valid {
t.Errorf("proof should have been valid")
Expand Down
16 changes: 4 additions & 12 deletions api/cashu/spend_condition.go
Original file line number Diff line number Diff line change
Expand Up @@ -182,7 +182,6 @@ func (tags *TagsInfo) MarshalJSON() ([]byte, error) {
}

func (tags *TagsInfo) UnmarshalJSON(b []byte) error {

var arrayToCheck [][]string

err := json.Unmarshal(b, &arrayToCheck)
Expand All @@ -192,7 +191,6 @@ func (tags *TagsInfo) UnmarshalJSON(b []byte) error {
}

for _, tag := range arrayToCheck {

if len(tag) < 2 {
return fmt.Errorf("%w: %s", ErrMalformedTag, tag)
}
Expand All @@ -205,7 +203,6 @@ func (tags *TagsInfo) UnmarshalJSON(b []byte) error {

tagInfo := tag[1:]
switch tagName {

case Sigflag:
if len(tagInfo) != 1 {
return fmt.Errorf("%w: %s", ErrMalformedTag, tag)
Expand Down Expand Up @@ -241,7 +238,6 @@ func (tags *TagsInfo) UnmarshalJSON(b []byte) error {
case Refund:
tags.Refund = append(tags.Refund, parsedPubkey)
}

}

case NSigs:
Expand Down Expand Up @@ -280,7 +276,6 @@ func (tags *TagsInfo) UnmarshalJSON(b []byte) error {

tags.Locktime = uint(locktime)
}

}
tags.originalTag = string(b)
return nil
Expand Down Expand Up @@ -319,7 +314,6 @@ func (sc *SpendCondition) VerifyPreimage(witness *Witness) error {
}

return nil

}

type Tags int
Expand Down Expand Up @@ -405,8 +399,8 @@ type Witness struct {

func (wit *Witness) String() (string, error) {
var witness = struct {
Preimage string
Signatures []string
Preimage string `json:"preimage,omitempty"`
Signatures []string `json:"signatures,omitempty"`
}{
Preimage: "",
Signatures: []string{},
Expand All @@ -429,8 +423,8 @@ func (wit *Witness) String() (string, error) {

func (wit *Witness) UnmarshalJSON(b []byte) error {
var sigs = struct {
Preimage string
Signatures []string
Preimage string `json:"preimage,omitempty"`
Signatures []string `json:"signatures,omitempty"`
}{
Preimage: "",
Signatures: []string{},
Expand Down Expand Up @@ -461,13 +455,11 @@ func (wit *Witness) UnmarshalJSON(b []byte) error {
}

witness.Signatures = append(witness.Signatures, signature)

}

*wit = witness

return nil

}

type SigflagValidation struct {
Expand Down
Loading
Loading