Skip to content

Commit

Permalink
account validation
Browse files Browse the repository at this point in the history
  • Loading branch information
CreatureDev committed Aug 21, 2023
1 parent 82ab9e6 commit c6bb99a
Show file tree
Hide file tree
Showing 9 changed files with 114 additions and 6 deletions.
18 changes: 12 additions & 6 deletions model/client/account/account_channels_request.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@ package account

import (
"encoding/json"
"errors"
"fmt"

"github.com/xyield/xrpl-go/model/client/common"
"github.com/xyield/xrpl-go/model/transactions/types"
Expand All @@ -21,12 +21,18 @@ func (*AccountChannelsRequest) Method() string {
return "account_channels"
}

// Validate method to be added to each request struct
func (a *AccountChannelsRequest) Validate() error {
if a.Account == "" {
return errors.New("no account ID specified")
func (r *AccountChannelsRequest) Validate() error {
if err := r.Account.Validate(); err != nil {
return err
}
if len(r.DestinationAccount) > 0 {
if err := r.DestinationAccount.Validate(); err != nil {
return err
}
}
if r.Limit != 0 && (r.Limit < 10 || r.Limit > 400) {
return fmt.Errorf("invalid limit, must be 10 <= limit <= 400")
}

return nil
}

Expand Down
8 changes: 8 additions & 0 deletions model/client/account/account_currencies_request.go
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,14 @@ func (*AccountCurrenciesRequest) Method() string {
return "account_currencies"
}

func (r *AccountCurrenciesRequest) Validate() error {
if err := r.Account.Validate(); err != nil {
return err
}

return nil
}

func (r *AccountCurrenciesRequest) UnmarshalJSON(data []byte) error {
type acrHelper struct {
Account types.Address `json:"account"`
Expand Down
8 changes: 8 additions & 0 deletions model/client/account/account_info_request.go
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,14 @@ func (*AccountInfoRequest) Method() string {
return "account_info"
}

func (r *AccountInfoRequest) Validate() error {
if err := r.Account.Validate(); err != nil {
return err
}

return nil
}

func (r *AccountInfoRequest) UnmarshalJSON(data []byte) error {
type airHelper struct {
Account types.Address `json:"account"`
Expand Down
12 changes: 12 additions & 0 deletions model/client/account/account_lines_request.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ package account

import (
"encoding/json"
"fmt"

"github.com/xyield/xrpl-go/model/client/common"
"github.com/xyield/xrpl-go/model/transactions/types"
Expand All @@ -20,6 +21,17 @@ func (*AccountLinesRequest) Method() string {
return "account_lines"
}

func (r *AccountLinesRequest) Validate() error {
if err := r.Account.Validate(); err != nil {
return err
}

if r.Limit != 0 && (r.Limit < 10 || r.Limit > 400) {
return fmt.Errorf("invalid limit, must be 10 <= limit <= 400")
}

return nil
}
func (r *AccountLinesRequest) UnmarshalJSON(data []byte) error {
type alrHelper struct {
Account types.Address `json:"account"`
Expand Down
13 changes: 13 additions & 0 deletions model/client/account/account_nfts_request.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ package account

import (
"encoding/json"
"fmt"

"github.com/xyield/xrpl-go/model/client/common"
"github.com/xyield/xrpl-go/model/transactions/types"
Expand All @@ -19,6 +20,18 @@ func (*AccountNFTsRequest) Method() string {
return "account_nfts"
}

func (r *AccountNFTsRequest) Validate() error {
if err := r.Account.Validate(); err != nil {
return err
}

if r.Limit != 0 && (r.Limit < 10 || r.Limit > 400) {
return fmt.Errorf("invalid limit, must be 10 <= limit <= 400")
}

return nil
}

func (r *AccountNFTsRequest) UnmarshalJSON(data []byte) error {
type anrHelper struct {
Account types.Address `json:"account"`
Expand Down
13 changes: 13 additions & 0 deletions model/client/account/account_objects_request.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ package account

import (
"encoding/json"
"fmt"

"github.com/xyield/xrpl-go/model/client/common"
"github.com/xyield/xrpl-go/model/transactions/types"
Expand Down Expand Up @@ -35,6 +36,18 @@ func (*AccountObjectsRequest) Method() string {
return "account_objects"
}

func (r *AccountObjectsRequest) Validate() error {
if err := r.Account.Validate(); err != nil {
return err
}

if r.Limit != 0 && (r.Limit < 10 || r.Limit > 400) {
return fmt.Errorf("invalid limit, must be 10 <= limit <= 400")
}

return nil
}

func (r *AccountObjectsRequest) UnmarshalJSON(data []byte) error {
type aorHelper struct {
Account types.Address `json:"account"`
Expand Down
13 changes: 13 additions & 0 deletions model/client/account/account_offers_request.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ package account

import (
"encoding/json"
"fmt"

"github.com/xyield/xrpl-go/model/client/common"
"github.com/xyield/xrpl-go/model/transactions/types"
Expand All @@ -20,6 +21,18 @@ func (*AccountOffersRequest) Method() string {
return "account_offers"
}

func (r *AccountOffersRequest) Validate() error {
if err := r.Account.Validate(); err != nil {
return err
}

if r.Limit != 0 && (r.Limit < 10 || r.Limit > 400) {
return fmt.Errorf("invalid limit, must be 10 <= limit <= 400")
}

return nil
}

func (r *AccountOffersRequest) UnmarshalJSON(data []byte) error {
type aorHelper struct {
Account types.Address `json:"account"`
Expand Down
13 changes: 13 additions & 0 deletions model/client/account/account_transactions_request.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ package account

import (
"encoding/json"
"fmt"

"github.com/xyield/xrpl-go/model/client/common"
"github.com/xyield/xrpl-go/model/transactions/types"
Expand All @@ -23,6 +24,18 @@ func (*AccountTransactionsRequest) Method() string {
return "account_tx"
}

func (r *AccountTransactionsRequest) Validate() error {
if err := r.Account.Validate(); err != nil {
return err
}

if r.Limit != 0 && (r.Limit < 10 || r.Limit > 400) {
return fmt.Errorf("invalid limit, must be 10 <= limit <= 400")
}

return nil
}

func (r *AccountTransactionsRequest) UnmarshalJSON(data []byte) error {
type atrHelper struct {
Account types.Address `json:"account"`
Expand Down
22 changes: 22 additions & 0 deletions model/transactions/types/address.go
Original file line number Diff line number Diff line change
@@ -1,3 +1,25 @@
package types

import (
"fmt"
"strings"
)

type Address string

func (a Address) Validate() error {
characters := []string{"0", "O", "I", "l"}
if len(a) < 25 || len(a) > 35 {
return fmt.Errorf("invalid xrpl address length")
}
if a[0] != 'r' {
return fmt.Errorf("invalid xrpl address prefix '%c'", a[0])
}
for _, c := range characters {
if strings.Contains(string(a), c) {
return fmt.Errorf("xrpl address contains invalid character '%s'", c)
}
}
// TODO checksum
return nil
}

0 comments on commit c6bb99a

Please sign in to comment.