-
Notifications
You must be signed in to change notification settings - Fork 1.3k
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
feat: wallet: per-token filtering rules #8428
Draft
magik6k
wants to merge
10
commits into
master
Choose a base branch
from
feat/wallet-rules
base: master
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Draft
Changes from all commits
Commits
Show all changes
10 commits
Select commit
Hold shift + click to select a range
1a91c4f
feat: wallet: per-token filtering rules
magik6k 0cd9bcf
wallet: Add rule tests
magik6k 0d98c08
wallet: Fip filter rule integration
magik6k 5401a4f
wallet: Integrate filter rules
magik6k aab8073
wallet: Document filtering rules
magik6k f498821
wallet: Support reading rules from a file
magik6k 15cd30b
wallet: Interpret rule arrays as AnyAccepts
magik6k 454239f
wallet Rename AnyAccepts to more obvious First
magik6k ae3f69e
wallet: Fix lint
magik6k fd10d44
wallet: Note that rules are experimental for now
magik6k File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,185 @@ | ||
package main | ||
|
||
import ( | ||
"bytes" | ||
"context" | ||
|
||
"github.com/ipfs/go-cid" | ||
"golang.org/x/xerrors" | ||
|
||
market0 "github.com/filecoin-project/specs-actors/actors/builtin/market" | ||
|
||
"github.com/filecoin-project/go-address" | ||
"github.com/filecoin-project/go-state-types/crypto" | ||
"github.com/filecoin-project/lotus/api" | ||
"github.com/filecoin-project/lotus/chain/types" | ||
) | ||
|
||
type FilteredWallet struct { | ||
under api.Wallet | ||
mustAccept bool | ||
} | ||
|
||
func (c *FilteredWallet) applyRules(ctx context.Context, filterParams map[FilterParam]interface{}) error { | ||
p, ok := ctx.Value(tokenKey).(jwtPayload) | ||
if !ok { | ||
return xerrors.Errorf("jwt payload not set on request context") | ||
} | ||
if c.mustAccept && p.Rules == nil { | ||
return xerrors.Errorf("token with no rules") | ||
} | ||
if p.Rules != nil { | ||
f, err := ParseRule(ctx, *p.Rules) | ||
if err != nil { | ||
return xerrors.Errorf("parsing rules: %w", err) | ||
} | ||
|
||
fres := f(filterParams) | ||
switch fres { | ||
case nil: | ||
if c.mustAccept { | ||
return xerrors.Errorf("filter didn't accept") | ||
} | ||
fallthrough | ||
case ErrAccept: | ||
|
||
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. Add comment that we explicitly "do nothing" here? |
||
default: | ||
return xerrors.Errorf("filter error: %w", fres) | ||
} | ||
} | ||
|
||
return nil | ||
} | ||
|
||
func (c *FilteredWallet) WalletNew(ctx context.Context, typ types.KeyType) (address.Address, error) { | ||
filterParams := map[FilterParam]interface{}{ | ||
ParamAction: ActionNew, | ||
} | ||
if err := c.applyRules(ctx, filterParams); err != nil { | ||
return address.Undef, err | ||
} | ||
|
||
return c.under.WalletNew(ctx, typ) | ||
} | ||
|
||
func (c *FilteredWallet) WalletHas(ctx context.Context, addr address.Address) (bool, error) { | ||
filterParams := map[FilterParam]interface{}{ | ||
ParamAction: ActionHas, | ||
} | ||
if err := c.applyRules(ctx, filterParams); err != nil { | ||
return false, err | ||
} | ||
|
||
return c.under.WalletHas(ctx, addr) | ||
} | ||
|
||
func (c *FilteredWallet) WalletList(ctx context.Context) ([]address.Address, error) { | ||
filterParams := map[FilterParam]interface{}{ | ||
ParamAction: ActionList, | ||
} | ||
if err := c.applyRules(ctx, filterParams); err != nil { | ||
return nil, err | ||
} | ||
|
||
return c.under.WalletList(ctx) | ||
} | ||
|
||
func (c *FilteredWallet) WalletSign(ctx context.Context, k address.Address, msg []byte, meta api.MsgMeta) (*crypto.Signature, error) { | ||
var filterParams map[FilterParam]interface{} | ||
|
||
switch meta.Type { | ||
case api.MTChainMsg: | ||
var cmsg types.Message | ||
if err := cmsg.UnmarshalCBOR(bytes.NewReader(meta.Extra)); err != nil { | ||
return nil, xerrors.Errorf("unmarshalling message: %w", err) | ||
} | ||
|
||
_, bc, err := cid.CidFromBytes(msg) | ||
if err != nil { | ||
return nil, xerrors.Errorf("getting cid from signing bytes: %w", err) | ||
} | ||
|
||
if !cmsg.Cid().Equals(bc) { | ||
return nil, xerrors.Errorf("cid(meta.Extra).bytes() != msg") | ||
} | ||
|
||
filterParams = map[FilterParam]interface{}{ | ||
ParamAction: ActionSign, | ||
ParamSignType: api.MTChainMsg, | ||
|
||
ParamSource: cmsg.From, | ||
ParamDestination: cmsg.To, | ||
ParamValue: cmsg.Value, | ||
ParamMethod: cmsg.Method, | ||
ParamMaxFee: cmsg.RequiredFunds(), | ||
} | ||
case api.MTBlock: | ||
bh, err := types.DecodeBlock(msg) | ||
if err != nil { | ||
return nil, xerrors.Errorf("decode block header: %w", err) | ||
} | ||
|
||
filterParams = map[FilterParam]interface{}{ | ||
ParamAction: ActionSign, | ||
ParamSignType: api.MTBlock, | ||
|
||
ParamMiner: bh.Miner, | ||
} | ||
case api.MTDealProposal: | ||
var dp market0.DealProposal | ||
if err := dp.UnmarshalCBOR(bytes.NewReader(msg)); err != nil { | ||
return nil, xerrors.Errorf("unmarshalling deal proposal: %w", err) | ||
} | ||
|
||
filterParams = map[FilterParam]interface{}{ | ||
ParamAction: ActionSign, | ||
ParamSignType: api.MTDealProposal, | ||
} | ||
default: | ||
filterParams = map[FilterParam]interface{}{ | ||
ParamAction: ActionSign, | ||
ParamSignType: api.MTUnknown, | ||
} | ||
} | ||
|
||
filterParams[ParamSigner] = k | ||
|
||
if err := c.applyRules(ctx, filterParams); err != nil { | ||
return nil, err | ||
} | ||
|
||
return c.under.WalletSign(ctx, k, msg, meta) | ||
} | ||
|
||
func (c *FilteredWallet) WalletExport(ctx context.Context, a address.Address) (*types.KeyInfo, error) { | ||
filterParams := map[FilterParam]interface{}{ | ||
ParamAction: ActionExport, | ||
} | ||
if err := c.applyRules(ctx, filterParams); err != nil { | ||
return nil, err | ||
} | ||
|
||
return c.under.WalletExport(ctx, a) | ||
} | ||
|
||
func (c *FilteredWallet) WalletImport(ctx context.Context, ki *types.KeyInfo) (address.Address, error) { | ||
filterParams := map[FilterParam]interface{}{ | ||
ParamAction: ActionImport, | ||
} | ||
if err := c.applyRules(ctx, filterParams); err != nil { | ||
return address.Undef, err | ||
} | ||
|
||
return c.under.WalletImport(ctx, ki) | ||
} | ||
|
||
func (c *FilteredWallet) WalletDelete(ctx context.Context, addr address.Address) error { | ||
filterParams := map[FilterParam]interface{}{ | ||
ParamAction: ActionDelete, | ||
} | ||
if err := c.applyRules(ctx, filterParams); err != nil { | ||
return err | ||
} | ||
|
||
return c.under.WalletDelete(ctx, addr) | ||
} |
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
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.