-
Notifications
You must be signed in to change notification settings - Fork 30
/
request.go
51 lines (42 loc) · 1.15 KB
/
request.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
package types
import (
"encoding/hex"
"fmt"
sdk "github.com/cosmos/cosmos-sdk/types"
sdkerrors "github.com/cosmos/cosmos-sdk/types/errors"
)
// NewRequest constructs a request
func NewRequest(
height int64,
consumer string,
txHash string,
oracle bool,
serviceFeeCap sdk.Coins,
serviceContextID string,
) Request {
return Request{
Height: height,
Consumer: consumer,
TxHash: txHash,
Oracle: oracle,
ServiceFeeCap: serviceFeeCap,
ServiceContextID: serviceContextID,
}
}
// GenerateRequestID generate a request id
func GenerateRequestID(r Request) []byte {
reqID := make([]byte, 0)
reqID = append(reqID, sdk.Uint64ToBigEndian(uint64(r.Height))...)
reqID = append(reqID, []byte(r.Consumer)...)
return SHA256(reqID)
}
// CheckReqID checks if the given request id is valid
func CheckReqID(reqID string) error {
if len(reqID) != 64 {
return sdkerrors.Wrap(sdkerrors.ErrInvalidRequest, fmt.Sprintf("invalid request id: %s", reqID))
}
if _, err := hex.DecodeString(reqID); err != nil {
return sdkerrors.Wrap(sdkerrors.ErrInvalidRequest, fmt.Sprintf("invalid request id: %s", reqID))
}
return nil
}