From 522e96f01638debc4bcaad53faf57e99aeb4bd49 Mon Sep 17 00:00:00 2001 From: Shrenuj Bansal <108157875+shrenujbansal@users.noreply.github.com> Date: Thu, 19 Jan 2023 17:57:48 -0500 Subject: [PATCH] fix: msg signing with delegated keys and send cli changes (#10056) * fix msg signing with delegated keys and send cli changes * make gen and docsgen * address comments --- chain/messagesigner/messagesigner.go | 30 +++++++++++++++++++++++++++- cli/send.go | 17 ++++++++++++++++ documentation/en/cli-lotus.md | 19 +++++++++--------- lib/sigs/delegated/init.go | 2 +- node/impl/full/wallet.go | 12 ++++++++++- 5 files changed, 68 insertions(+), 12 deletions(-) diff --git a/chain/messagesigner/messagesigner.go b/chain/messagesigner/messagesigner.go index 96457e9f8..6a622dd57 100644 --- a/chain/messagesigner/messagesigner.go +++ b/chain/messagesigner/messagesigner.go @@ -13,10 +13,13 @@ import ( "golang.org/x/xerrors" "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/messagepool" "github.com/filecoin-project/lotus/chain/types" + "github.com/filecoin-project/lotus/chain/types/ethtypes" + "github.com/filecoin-project/lotus/chain/wallet/key" "github.com/filecoin-project/lotus/node/modules/dtypes" ) @@ -66,15 +69,24 @@ func (ms *MessageSigner) SignMessage(ctx context.Context, msg *types.Message, sp // Sign the message with the nonce msg.Nonce = nonce + keyInfo, err := ms.wallet.WalletExport(ctx, msg.From) + if err != nil { + return nil, err + } + sb, err := SigningBytes(msg, key.ActSigType(keyInfo.Type)) + if err != nil { + return nil, err + } mb, err := msg.ToStorageBlock() if err != nil { return nil, xerrors.Errorf("serializing message: %w", err) } - sig, err := ms.wallet.WalletSign(ctx, msg.From, mb.Cid().Bytes(), api.MsgMeta{ + sig, err := ms.wallet.WalletSign(ctx, msg.From, sb, api.MsgMeta{ Type: api.MTChainMsg, Extra: mb.RawData(), }) + if err != nil { return nil, xerrors.Errorf("failed to sign message: %w, addr=%s", err, msg.From) } @@ -187,3 +199,19 @@ func (ms *MessageSigner) SaveNonce(ctx context.Context, addr address.Address, no func (ms *MessageSigner) dstoreKey(addr address.Address) datastore.Key { return datastore.KeyWithNamespaces([]string{dsKeyActorNonce, addr.String()}) } + +func SigningBytes(msg *types.Message, sigType crypto.SigType) ([]byte, error) { + if sigType == crypto.SigTypeDelegated { + txArgs, err := ethtypes.EthTxArgsFromMessage(msg) + if err != nil { + return nil, xerrors.Errorf("failed to reconstruct eth transaction: %w", err) + } + rlpEncodedMsg, err := txArgs.ToRlpUnsignedMsg() + if err != nil { + return nil, xerrors.Errorf("failed to repack eth rlp message: %w", err) + } + return rlpEncodedMsg, nil + } + + return msg.Cid().Bytes(), nil +} diff --git a/cli/send.go b/cli/send.go index 4268f8eb2..3e390584d 100644 --- a/cli/send.go +++ b/cli/send.go @@ -13,6 +13,7 @@ import ( "github.com/filecoin-project/lotus/chain/actors/builtin" "github.com/filecoin-project/lotus/chain/types" + "github.com/filecoin-project/lotus/chain/types/ethtypes" ) var sendCmd = &cli.Command{ @@ -24,6 +25,10 @@ var sendCmd = &cli.Command{ Name: "from", Usage: "optionally specify the account to send funds from", }, + &cli.StringFlag{ + Name: "from-eth-addr", + Usage: "optionally specify the eth addr to send funds from", + }, &cli.StringFlag{ Name: "gas-premium", Usage: "specify gas price to use in AttoFIL", @@ -98,6 +103,18 @@ var sendCmd = &cli.Command{ } params.From = addr + } else if from := cctx.String("from-eth-addr"); from != "" { + eaddr, err := ethtypes.ParseEthAddress(from) + if err != nil { + return err + } + faddr, err := eaddr.ToFilecoinAddress() + if err != nil { + fmt.Println("error on conversion to faddr") + return err + } + fmt.Println("f4 addr: ", faddr) + params.From = faddr } if cctx.IsSet("gas-premium") { diff --git a/documentation/en/cli-lotus.md b/documentation/en/cli-lotus.md index 616a4a356..5e0c7a305 100644 --- a/documentation/en/cli-lotus.md +++ b/documentation/en/cli-lotus.md @@ -181,15 +181,16 @@ CATEGORY: BASIC OPTIONS: - --force Deprecated: use global 'force-send' (default: false) - --from value optionally specify the account to send funds from - --gas-feecap value specify gas fee cap to use in AttoFIL (default: "0") - --gas-limit value specify gas limit (default: 0) - --gas-premium value specify gas price to use in AttoFIL (default: "0") - --method value specify method to invoke (default: 0) - --nonce value specify the nonce to use (default: 0) - --params-hex value specify invocation parameters in hex - --params-json value specify invocation parameters in json + --force Deprecated: use global 'force-send' (default: false) + --from value optionally specify the account to send funds from + --from-eth-addr value optionally specify the eth addr to send funds from + --gas-feecap value specify gas fee cap to use in AttoFIL (default: "0") + --gas-limit value specify gas limit (default: 0) + --gas-premium value specify gas price to use in AttoFIL (default: "0") + --method value specify method to invoke (default: 0) + --nonce value specify the nonce to use (default: 0) + --params-hex value specify invocation parameters in hex + --params-json value specify invocation parameters in json ``` diff --git a/lib/sigs/delegated/init.go b/lib/sigs/delegated/init.go index 4db83b3e7..81886ceaa 100644 --- a/lib/sigs/delegated/init.go +++ b/lib/sigs/delegated/init.go @@ -68,7 +68,7 @@ func (delegatedSigner) Verify(sig []byte, a address.Address, msg []byte) error { } if maybeaddr != a { - return fmt.Errorf("signature did not match") + return fmt.Errorf("signature did not match maybeaddr: %s, signer: %s", maybeaddr, a) } return nil diff --git a/node/impl/full/wallet.go b/node/impl/full/wallet.go index 67bb96101..0927e5aca 100644 --- a/node/impl/full/wallet.go +++ b/node/impl/full/wallet.go @@ -11,9 +11,11 @@ import ( "github.com/filecoin-project/go-state-types/crypto" "github.com/filecoin-project/lotus/api" + "github.com/filecoin-project/lotus/chain/messagesigner" "github.com/filecoin-project/lotus/chain/stmgr" "github.com/filecoin-project/lotus/chain/types" "github.com/filecoin-project/lotus/chain/wallet" + "github.com/filecoin-project/lotus/chain/wallet/key" "github.com/filecoin-project/lotus/lib/sigs" ) @@ -51,12 +53,20 @@ func (a *WalletAPI) WalletSignMessage(ctx context.Context, k address.Address, ms return nil, xerrors.Errorf("failed to resolve ID address: %w", keyAddr) } + keyInfo, err := a.Wallet.WalletExport(ctx, k) + if err != nil { + return nil, err + } + sb, err := messagesigner.SigningBytes(msg, key.ActSigType(keyInfo.Type)) + if err != nil { + return nil, err + } mb, err := msg.ToStorageBlock() if err != nil { return nil, xerrors.Errorf("serializing message: %w", err) } - sig, err := a.Wallet.WalletSign(ctx, keyAddr, mb.Cid().Bytes(), api.MsgMeta{ + sig, err := a.Wallet.WalletSign(ctx, keyAddr, sb, api.MsgMeta{ Type: api.MTChainMsg, Extra: mb.RawData(), })