-
Notifications
You must be signed in to change notification settings - Fork 586
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
imp: allow memo strings instead of keys for transfer authorizations #6268
Changes from 2 commits
a41d67e
af98008
ecaf37f
772c7e6
dd89517
99f46d8
34735f0
388b7ef
3b72af7
7e80b22
b44d371
7241686
c0f920a
19e5453
d8be449
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,10 +1,10 @@ | ||
package types | ||
|
||
import ( | ||
"bytes" | ||
"context" | ||
"encoding/json" | ||
"math/big" | ||
"sort" | ||
"strings" | ||
|
||
"github.com/cosmos/gogoproto/proto" | ||
|
@@ -131,6 +131,16 @@ | |
} | ||
found[allocation.AllowList[i]] = true | ||
} | ||
|
||
if len(allocation.AllowedPacketData) > 0 && allocation.AllowedPacketData[0] != AllowAllPacketDataKeys { | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Should we also put a limit on how many items we allow in |
||
jsonObject := make(map[string]interface{}) | ||
for _, elem := range allocation.AllowedPacketData { | ||
err := json.Unmarshal([]byte(elem), &jsonObject) | ||
if err != nil { | ||
return errorsmod.Wrapf(ErrInvalidAuthorization, "allowed packet data contains a non JSON-encoded string: %s", elem) | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Why do we require them to be JSON encoded? Can we remove this requirement please and just use any string? There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Sure, I can remove it. Although I thought that, even though it's not enforced, the consensus was that the memo should a JSON-encoded string. Do you know of use cases where that would not be the case? There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Ok, I changed it here. If during the PR review we decide to go back to enforcing JSON, we can just revert that commit. |
||
} | ||
} | ||
} | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. This checks that if the wildcard is not used, then all elements should be JSON-encoded strings. |
||
} | ||
|
||
return nil | ||
|
@@ -166,39 +176,30 @@ | |
} | ||
|
||
// if allowedPacketDataList has only 1 element and it equals AllowAllPacketDataKeys | ||
// then accept all the packet data keys | ||
// then accept all the memo strings | ||
if len(allowedPacketDataList) == 1 && allowedPacketDataList[0] == AllowAllPacketDataKeys { | ||
return nil | ||
} | ||
|
||
jsonObject := make(map[string]interface{}) | ||
err := json.Unmarshal([]byte(memo), &jsonObject) | ||
if err != nil { | ||
return err | ||
} | ||
|
||
gasCostPerIteration := ctx.KVGasConfig().IterNextCostFlat | ||
|
||
for _, key := range allowedPacketDataList { | ||
for _, allowedMemo := range allowedPacketDataList { | ||
crodriguezvega marked this conversation as resolved.
Show resolved
Hide resolved
|
||
ctx.GasMeter().ConsumeGas(gasCostPerIteration, "transfer authorization") | ||
|
||
_, ok := jsonObject[key] | ||
if ok { | ||
delete(jsonObject, key) | ||
} | ||
} | ||
dst := &bytes.Buffer{} | ||
json.Compact(dst, []byte(allowedMemo)) | ||
compactAllowedMemo := dst.String() | ||
|
||
keys := make([]string, 0, len(jsonObject)) | ||
for k := range jsonObject { | ||
keys = append(keys, k) | ||
} | ||
sort.Strings(keys) | ||
dst = &bytes.Buffer{} | ||
json.Compact(dst, []byte(memo)) | ||
compactMemo := dst.String() | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Maybe this is over engineering it, but this compacts the JSON strings to prevent a mismatch due to empty spaces. |
||
|
||
if len(jsonObject) != 0 { | ||
return errorsmod.Wrapf(ErrInvalidAuthorization, "not allowed packet data keys: %s", keys) | ||
if compactMemo == compactAllowedMemo { | ||
return nil | ||
} | ||
} | ||
|
||
return nil | ||
return errorsmod.Wrapf(ErrInvalidAuthorization, "not allowed memo: %s", memo) | ||
crodriguezvega marked this conversation as resolved.
Show resolved
Hide resolved
|
||
} | ||
|
||
// UnboundedSpendLimit returns the sentinel value that can be used | ||
|
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -19,8 +19,8 @@ message Allocation { | |
[(gogoproto.nullable) = false, (gogoproto.castrepeated) = "github.com/cosmos/cosmos-sdk/types.Coins"]; | ||
// allow list of receivers, an empty allow list permits any receiver address | ||
repeated string allow_list = 4; | ||
// allow list of packet data keys, an empty list prohibits all packet data keys; | ||
// a list only with "*" permits any packet data key | ||
// allow list of (JSON-encoded) memo strings, an empty list prohibits all memo strings; | ||
// a list only with "*" permits any memo string | ||
repeated string allowed_packet_data = 5; | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Maybe in v9 we can rename this field? |
||
} | ||
|
||
|
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Maybe this constant can also be renamed in v9? We could also add a new constant with a more accurate name for the back ports.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Yeah, maybe you can rename it on this PR and then change it back in the backport
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I am going to rename it in a followup PR, just to make the backport easier.