-
Notifications
You must be signed in to change notification settings - Fork 4
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
add: state_root query; update: docs, get gist proof by state root, no…
…t block number, split business logic and user request data
- Loading branch information
Showing
5 changed files
with
62 additions
and
70 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,30 +1,72 @@ | ||
package requests | ||
|
||
import ( | ||
"math/big" | ||
"net/http" | ||
|
||
validation "github.com/go-ozzo/ozzo-validation/v4" | ||
"github.com/go-ozzo/ozzo-validation/v4/is" | ||
"github.com/iden3/go-iden3-core/v2/w3c" | ||
"gitlab.com/distributed_lab/logan/v3" | ||
"gitlab.com/distributed_lab/logan/v3/errors" | ||
"gitlab.com/distributed_lab/urlval" | ||
"net/http" | ||
) | ||
|
||
type GetGistDataRequest struct { | ||
UserDID string `url:"user_did"` | ||
BlockNumber uint64 `url:"block_number"` | ||
UserDID *w3c.DID | ||
StateRoot *big.Int | ||
} | ||
|
||
type getGistDataQuery struct { | ||
UserDID string `url:"user_did"` | ||
StateRoot string `url:"state_root"` | ||
} | ||
|
||
func NewGetGistDataRequest(r *http.Request) (GetGistDataRequest, error) { | ||
var req GetGistDataRequest | ||
var query getGistDataQuery | ||
|
||
err := urlval.Decode(r.URL.Query(), &req) | ||
err := urlval.Decode(r.URL.Query(), &query) | ||
if err != nil { | ||
return GetGistDataRequest{}, errors.Wrap(err, "failed to decode url") | ||
return GetGistDataRequest{}, validation.Errors{ | ||
"url": errors.Wrap(err, "failed to decode url"), | ||
} | ||
} | ||
|
||
return req, validateGetGistDataRequest(req) | ||
return parseGistDataQuery(query) | ||
} | ||
|
||
func validateGetGistDataRequest(r GetGistDataRequest) error { | ||
return validation.Errors{ | ||
"/user_did": validation.Validate(r.UserDID, validation.Required), | ||
func parseGistDataQuery(query getGistDataQuery) (GetGistDataRequest, error) { | ||
var ( | ||
err error | ||
ok bool | ||
req GetGistDataRequest | ||
) | ||
|
||
err = validation.Errors{ | ||
"/user_did": validation.Validate(query.UserDID, validation.Required), | ||
"/state_root": validation.Validate(query.StateRoot, validation.Required, is.Hexadecimal), | ||
}.Filter() | ||
if err != nil { | ||
return req, err | ||
} | ||
|
||
req.UserDID, err = w3c.ParseDID(query.UserDID) | ||
if err != nil { | ||
return req, validation.Errors{ | ||
"/user_did": errors.Wrap(err, "failed to parse user DID", logan.F{ | ||
"user_did": req.UserDID, | ||
}), | ||
}.Filter() | ||
} | ||
|
||
req.StateRoot, ok = new(big.Int).SetString(query.StateRoot, 16) | ||
if !ok { | ||
return req, validation.Errors{ | ||
"/state_root": errors.From(errors.New("failed to parse state root"), logan.F{ | ||
"state_root": req.StateRoot, | ||
}), | ||
} | ||
} | ||
|
||
return req, nil | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters