diff --git a/model/client/account/account_lines_request.go b/model/client/account/account_lines_request.go index 825466f0..c5bbf6c3 100644 --- a/model/client/account/account_lines_request.go +++ b/model/client/account/account_lines_request.go @@ -23,13 +23,19 @@ func (*AccountLinesRequest) Method() string { func (r *AccountLinesRequest) Validate() error { if err := r.Account.Validate(); err != nil { - return fmt.Errorf("account lines request: %w", err) + return fmt.Errorf("account lines request account: %w", err) } if r.Limit != 0 && (r.Limit < 10 || r.Limit > 400) { return fmt.Errorf("account lines request: invalid limit, must be 10 <= limit <= 400") } + if r.Peer != "" { + if err := r.Peer.Validate(); err != nil { + return fmt.Errorf("account lines request peer: %w", err) + } + } + return nil } func (r *AccountLinesRequest) UnmarshalJSON(data []byte) error { diff --git a/model/client/admin/data/download_shard_request.go b/model/client/admin/data/download_shard_request.go index 4ad3d0db..b4cdfb3d 100644 --- a/model/client/admin/data/download_shard_request.go +++ b/model/client/admin/data/download_shard_request.go @@ -1,6 +1,10 @@ package data -import "github.com/xyield/xrpl-go/model/client/common" +import ( + "fmt" + + "github.com/xyield/xrpl-go/model/client/common" +) type DownloadShardRequest struct { Shards []ShardDescriptor `json:"shards"` @@ -15,6 +19,9 @@ func (*DownloadShardRequest) Method() string { return "download_shard" } -func (*DownloadShardRequest) Validate() error { +func (d *DownloadShardRequest) Validate() error { + if len(d.Shards) == 0 { + return fmt.Errorf("download shard request: no shard descriptors provided") + } return nil } diff --git a/model/client/admin/key/wallet_propose_request.go b/model/client/admin/key/wallet_propose_request.go index e61e6fa7..27f36e0e 100644 --- a/model/client/admin/key/wallet_propose_request.go +++ b/model/client/admin/key/wallet_propose_request.go @@ -1,5 +1,7 @@ package key +import "fmt" + type WalletProposeRequest struct { KeyType string `json:"key_type,omitempty"` Passphrase string `json:"passphrase,omitempty"` @@ -11,6 +13,19 @@ func (*WalletProposeRequest) Method() string { return "wallet_propose" } -func (*WalletProposeRequest) Validate() error { +func (p *WalletProposeRequest) Validate() error { + cnt := 0 + if p.Passphrase != "" { + cnt++ + } + if p.Seed != "" { + cnt++ + } + if p.SeedHex != "" { + cnt++ + } + if cnt > 1 { + return fmt.Errorf("wallet propose request: only one of (passphrase, seed, seedhex) may be set") + } return nil } diff --git a/model/client/admin/peer/connect_request.go b/model/client/admin/peer/connect_request.go index c8acf829..177eaf42 100644 --- a/model/client/admin/peer/connect_request.go +++ b/model/client/admin/peer/connect_request.go @@ -1,5 +1,7 @@ package peer +import "fmt" + type ConnectRequest struct { IP string `json:"ip"` Port int `json:"port,omitempty"` @@ -9,6 +11,9 @@ func (*ConnectRequest) Method() string { return "connect" } -func (*ConnectRequest) Validate() error { +func (c *ConnectRequest) Validate() error { + if c.IP == "" { + return fmt.Errorf("connect request: missing ip") + } return nil } diff --git a/model/client/admin/peer/peer_reservations_add_request.go b/model/client/admin/peer/peer_reservations_add_request.go index e763ec87..a8da95fd 100644 --- a/model/client/admin/peer/peer_reservations_add_request.go +++ b/model/client/admin/peer/peer_reservations_add_request.go @@ -1,5 +1,7 @@ package peer +import "fmt" + type PeerReservationAddRequest struct { PublicKey string `json:"public_key"` Description string `json:"description,omitempty"` @@ -9,6 +11,9 @@ func (*PeerReservationAddRequest) Method() string { return "peer_reservations_add" } -func (*PeerReservationAddRequest) Validate() error { +func (r *PeerReservationAddRequest) Validate() error { + if r.PublicKey == "" { + return fmt.Errorf("peer reservation add request: missing publickey") + } return nil } diff --git a/model/client/admin/peer/peer_reservations_del_request.go b/model/client/admin/peer/peer_reservations_del_request.go index f2262fbc..34e9f49e 100644 --- a/model/client/admin/peer/peer_reservations_del_request.go +++ b/model/client/admin/peer/peer_reservations_del_request.go @@ -1,5 +1,7 @@ package peer +import "fmt" + type PeerReservationDelRequest struct { PublicKey string `json:"public_key"` } @@ -8,6 +10,9 @@ func (*PeerReservationDelRequest) Method() string { return "peer_reservations_del" } -func (*PeerReservationDelRequest) Validate() error { +func (r *PeerReservationDelRequest) Validate() error { + if r.PublicKey == "" { + return fmt.Errorf("peer reservation del request: missing publickey") + } return nil } diff --git a/model/client/admin/signing/sign_for_request.go b/model/client/admin/signing/sign_for_request.go index b4f56219..523641e4 100644 --- a/model/client/admin/signing/sign_for_request.go +++ b/model/client/admin/signing/sign_for_request.go @@ -31,6 +31,23 @@ func (r *SignForRequest) Validate() error { return fmt.Errorf("sign for request: empty tx") } + cnt := 0 + if r.Secret != "" { + cnt++ + } + if r.Seed != "" { + cnt++ + } + if r.SeedHex != "" { + cnt++ + } + if r.Passphrase != "" { + cnt++ + } + if cnt != 1 { + return fmt.Errorf("sign for request: must provide one of (secret, seed, seedhex, passphrase)") + } + return nil } diff --git a/model/client/channel/channel_authorize_request.go b/model/client/channel/channel_authorize_request.go index 26513d26..9ec03f41 100644 --- a/model/client/channel/channel_authorize_request.go +++ b/model/client/channel/channel_authorize_request.go @@ -24,7 +24,25 @@ func (r *ChannelAuthorizeRequest) Validate() error { if r.ChannelID == "" { return fmt.Errorf("channel authorize request: missing channel id") } - + var set []string + if r.Secret != "" { + set = append(set, "secret") + } + if r.Seed != "" { + set = append(set, "seed") + } + if r.SeedHex != "" { + set = append(set, "seed_hex") + } + if r.Passphrase != "" { + set = append(set, "passphrase") + } + if len(set) == 0 { + return fmt.Errorf("channel authorize request: at least one of (secret, seed, seed_hex, passphrase) must be set") + } + if len(set) > 1 { + return fmt.Errorf("channel authorize request: only one signing method required, currently set %v", set) + } return nil } diff --git a/model/client/channel/channel_authorize_test.go b/model/client/channel/channel_authorize_test.go index 907bb8ab..13d09dc2 100644 --- a/model/client/channel/channel_authorize_test.go +++ b/model/client/channel/channel_authorize_test.go @@ -50,5 +50,15 @@ func TestChannelAuthorizeValidate(t *testing.T) { ChannelID: "abc", } err = s.Validate() + assert.ErrorContains(t, err, "seed") + s = ChannelAuthorizeRequest{ + ChannelID: "abc", + Seed: "123", + } + err = s.Validate() assert.Nil(t, err) + s.Secret = "def" + err = s.Validate() + assert.ErrorContains(t, err, "seed") + assert.ErrorContains(t, err, "secret") } diff --git a/model/client/channel/channel_verify_request.go b/model/client/channel/channel_verify_request.go index 4a4b18a8..f3bede1b 100644 --- a/model/client/channel/channel_verify_request.go +++ b/model/client/channel/channel_verify_request.go @@ -1,10 +1,14 @@ package channel -import "github.com/xyield/xrpl-go/model/transactions/types" +import ( + "fmt" + + "github.com/xyield/xrpl-go/model/transactions/types" +) type ChannelVerifyRequest struct { Amount types.XRPCurrencyAmount `json:"amount"` - ChannelID string `json:"channel_id"` + ChannelID types.Hash256 `json:"channel_id"` PublicKey string `json:"public_key"` Signature string `json:"signature"` } @@ -13,6 +17,15 @@ func (*ChannelVerifyRequest) Method() string { return "channel_verify" } -func (*ChannelVerifyRequest) Validate() error { +func (r *ChannelVerifyRequest) Validate() error { + if err := r.ChannelID.Validate(); err != nil { + return fmt.Errorf("channel verify request: channel id: %w", err) + } + if r.PublicKey == "" { + return fmt.Errorf("channel verify request: public key not set") + } + if r.Signature == "" { + return fmt.Errorf("channel verify request: signature not set") + } return nil } diff --git a/model/client/clio/nft_info_request.go b/model/client/clio/nft_info_request.go index c12f2369..962327e7 100644 --- a/model/client/clio/nft_info_request.go +++ b/model/client/clio/nft_info_request.go @@ -2,6 +2,7 @@ package clio import ( "encoding/json" + "fmt" "github.com/xyield/xrpl-go/model/client/common" "github.com/xyield/xrpl-go/model/transactions/types" @@ -17,7 +18,10 @@ func (*NFTInfoRequest) Method() string { return "nft_info" } -func (*NFTInfoRequest) Validate() error { +func (r *NFTInfoRequest) Validate() error { + if err := r.NFTokenID.Validate(); err != nil { + return fmt.Errorf("nft info request: %w", err) + } return nil } diff --git a/model/client/ledger/ledger_entry_request.go b/model/client/ledger/ledger_entry_request.go index f85915e9..d7f1ab05 100644 --- a/model/client/ledger/ledger_entry_request.go +++ b/model/client/ledger/ledger_entry_request.go @@ -22,6 +22,7 @@ type LedgerEntryRequest struct { PaymentChannel string `json:"payment_channel,omitempty"` DepositPreauth EntryRequestOrString `json:"deposit_preauth,omitempty"` Ticket EntryRequestOrString `json:"ticket,omitempty"` + NFTPage string `json:"nft_page,omitempty"` } func (*LedgerEntryRequest) Method() string { @@ -58,7 +59,7 @@ func (r *LedgerEntryRequest) Validate() error { if r.RippleState != nil { setCount++ - if err := r.Offer.Validate(); err != nil { + if err := r.RippleState.Validate(); err != nil { return fmt.Errorf("ledger entry ripple state: %w", err) } } @@ -92,6 +93,10 @@ func (r *LedgerEntryRequest) Validate() error { } } + if r.NFTPage != "" { + setCount++ + } + if setCount != 1 { return fmt.Errorf("ledger entry: exactly one ledger entry object may be requested, found %d", setCount) } diff --git a/model/client/server/manifest_request.go b/model/client/server/manifest_request.go index 2aae6dca..a2da5ad5 100644 --- a/model/client/server/manifest_request.go +++ b/model/client/server/manifest_request.go @@ -1,5 +1,7 @@ package server +import "fmt" + type ManifestRequest struct { PublicKey string `json:"public_key"` } @@ -8,6 +10,9 @@ func (*ManifestRequest) Method() string { return "manifest" } -func (*ManifestRequest) Validate() error { +func (r *ManifestRequest) Validate() error { + if r.PublicKey == "" { + return fmt.Errorf("manifest request: public key not set") + } return nil } diff --git a/model/client/transactions/submit_multisigned_request.go b/model/client/transactions/submit_multisigned_request.go index e6d6d6dc..ca4a9ee2 100644 --- a/model/client/transactions/submit_multisigned_request.go +++ b/model/client/transactions/submit_multisigned_request.go @@ -2,6 +2,7 @@ package transactions import ( "encoding/json" + "fmt" "github.com/xyield/xrpl-go/model/transactions" ) @@ -36,6 +37,9 @@ func (r *SubmitMultisignedRequest) UnmarshalJSON(data []byte) error { return nil } -func (*SubmitMultisignedRequest) Validate() error { +func (s *SubmitMultisignedRequest) Validate() error { + if s.Tx == nil { + return fmt.Errorf("submit multisigned request: missing tx") + } return nil } diff --git a/model/client/transactions/submit_request.go b/model/client/transactions/submit_request.go index 45cea96f..c3fc30fd 100644 --- a/model/client/transactions/submit_request.go +++ b/model/client/transactions/submit_request.go @@ -1,5 +1,7 @@ package transactions +import "fmt" + type SubmitRequest struct { TxBlob string `json:"tx_blob"` FailHard bool `json:"fail_hard,omitempty"` @@ -9,6 +11,9 @@ func (*SubmitRequest) Method() string { return "submit" } -func (*SubmitRequest) Validate() error { +func (s *SubmitRequest) Validate() error { + if s.TxBlob == "" { + return fmt.Errorf("submit request: missing txblob") + } return nil } diff --git a/model/client/transactions/transaction_entry_request.go b/model/client/transactions/transaction_entry_request.go index 835aaa3b..5ebf9cd6 100644 --- a/model/client/transactions/transaction_entry_request.go +++ b/model/client/transactions/transaction_entry_request.go @@ -2,6 +2,7 @@ package transactions import ( "encoding/json" + "fmt" "github.com/xyield/xrpl-go/model/client/common" ) @@ -39,6 +40,9 @@ func (t *TransactionEntryRequest) UnmarshalJSON(data []byte) error { return nil } -func (*TransactionEntryRequest) Validate() error { +func (t *TransactionEntryRequest) Validate() error { + if t.TxHash == "" { + return fmt.Errorf("transaction entry request: missing txhash") + } return nil } diff --git a/model/client/transactions/tx_request.go b/model/client/transactions/tx_request.go index 70b2366a..ebc5ffce 100644 --- a/model/client/transactions/tx_request.go +++ b/model/client/transactions/tx_request.go @@ -1,6 +1,10 @@ package transactions -import "github.com/xyield/xrpl-go/model/client/common" +import ( + "fmt" + + "github.com/xyield/xrpl-go/model/client/common" +) type TxRequest struct { Transaction string `json:"transaction"` @@ -13,6 +17,9 @@ func (*TxRequest) Method() string { return "tx" } -func (*TxRequest) Validate() error { +func (t *TxRequest) Validate() error { + if t.Transaction == "" { + return fmt.Errorf("transaction request: missing transaction") + } return nil } diff --git a/model/transactions/types/hash256.go b/model/transactions/types/hash256.go index df068aa3..e63df929 100644 --- a/model/transactions/types/hash256.go +++ b/model/transactions/types/hash256.go @@ -1,3 +1,15 @@ package types +import "fmt" + type Hash256 string + +func (h Hash256) Validate() error { + if h == "" { + return fmt.Errorf("hash256 value not set") + } + if len(h) != 64 { + return fmt.Errorf("hash256 length was not expected 64 characters") + } + return nil +} diff --git a/model/transactions/types/nftoken_id.go b/model/transactions/types/nftoken_id.go index 438a0a42..c682b473 100644 --- a/model/transactions/types/nftoken_id.go +++ b/model/transactions/types/nftoken_id.go @@ -1,3 +1,13 @@ package types +import "fmt" + type NFTokenID Hash256 + +func (id NFTokenID) Validate() error { + h := Hash256(id) + if err := h.Validate(); err != nil { + return fmt.Errorf("nftoken id: %w", err) + } + return nil +}