2020-09-23 16:28:11 +00:00
|
|
|
package messagesigner
|
|
|
|
|
|
|
|
import (
|
|
|
|
"bytes"
|
|
|
|
"context"
|
2020-10-05 12:17:40 +00:00
|
|
|
"sync"
|
2020-09-23 16:28:11 +00:00
|
|
|
|
2022-08-16 19:56:32 +00:00
|
|
|
"github.com/google/uuid"
|
2020-09-23 16:28:11 +00:00
|
|
|
"github.com/ipfs/go-datastore"
|
|
|
|
"github.com/ipfs/go-datastore/namespace"
|
2020-09-29 10:19:04 +00:00
|
|
|
logging "github.com/ipfs/go-log/v2"
|
2020-09-23 16:28:11 +00:00
|
|
|
cbg "github.com/whyrusleeping/cbor-gen"
|
|
|
|
"golang.org/x/xerrors"
|
2020-10-08 23:53:12 +00:00
|
|
|
|
|
|
|
"github.com/filecoin-project/go-address"
|
|
|
|
|
|
|
|
"github.com/filecoin-project/lotus/api"
|
2022-09-29 10:56:57 +00:00
|
|
|
"github.com/filecoin-project/lotus/chain/messagepool"
|
2020-10-08 23:53:12 +00:00
|
|
|
"github.com/filecoin-project/lotus/chain/types"
|
2023-01-19 22:57:48 +00:00
|
|
|
"github.com/filecoin-project/lotus/chain/types/ethtypes"
|
2020-10-08 23:53:12 +00:00
|
|
|
"github.com/filecoin-project/lotus/node/modules/dtypes"
|
2020-09-23 16:28:11 +00:00
|
|
|
)
|
|
|
|
|
2020-10-05 12:54:22 +00:00
|
|
|
const dsKeyActorNonce = "ActorNextNonce"
|
2022-08-16 20:25:42 +00:00
|
|
|
const dsKeyMsgUUIDSet = "MsgUuidSet"
|
2020-09-23 16:28:11 +00:00
|
|
|
|
2020-09-29 10:19:04 +00:00
|
|
|
var log = logging.Logger("messagesigner")
|
|
|
|
|
2022-09-08 18:20:05 +00:00
|
|
|
type MsgSigner interface {
|
|
|
|
SignMessage(ctx context.Context, msg *types.Message, spec *api.MessageSendSpec, cb func(*types.SignedMessage) error) (*types.SignedMessage, error)
|
|
|
|
GetSignedMessage(ctx context.Context, uuid uuid.UUID) (*types.SignedMessage, error)
|
|
|
|
StoreSignedMessage(ctx context.Context, uuid uuid.UUID, message *types.SignedMessage) error
|
|
|
|
NextNonce(ctx context.Context, addr address.Address) (uint64, error)
|
|
|
|
SaveNonce(ctx context.Context, addr address.Address, nonce uint64) error
|
|
|
|
}
|
|
|
|
|
2020-09-23 16:28:11 +00:00
|
|
|
// MessageSigner keeps track of nonces per address, and increments the nonce
|
|
|
|
// when signing a message
|
|
|
|
type MessageSigner struct {
|
2021-03-23 12:42:56 +00:00
|
|
|
wallet api.Wallet
|
2020-10-05 12:17:40 +00:00
|
|
|
lk sync.Mutex
|
2022-09-29 10:56:57 +00:00
|
|
|
mpool messagepool.MpoolNonceAPI
|
2020-09-23 16:28:11 +00:00
|
|
|
ds datastore.Batching
|
|
|
|
}
|
|
|
|
|
2022-09-29 10:56:57 +00:00
|
|
|
func NewMessageSigner(wallet api.Wallet, mpool messagepool.MpoolNonceAPI, ds dtypes.MetadataDS) *MessageSigner {
|
2020-09-23 16:28:11 +00:00
|
|
|
ds = namespace.Wrap(ds, datastore.NewKey("/message-signer/"))
|
|
|
|
return &MessageSigner{
|
|
|
|
wallet: wallet,
|
|
|
|
mpool: mpool,
|
|
|
|
ds: ds,
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
// SignMessage increments the nonce for the message From address, and signs
|
|
|
|
// the message
|
2022-09-08 18:20:05 +00:00
|
|
|
func (ms *MessageSigner) SignMessage(ctx context.Context, msg *types.Message, spec *api.MessageSendSpec, cb func(*types.SignedMessage) error) (*types.SignedMessage, error) {
|
2020-10-05 12:17:40 +00:00
|
|
|
ms.lk.Lock()
|
|
|
|
defer ms.lk.Unlock()
|
|
|
|
|
|
|
|
// Get the next message nonce
|
2022-09-08 18:20:05 +00:00
|
|
|
nonce, err := ms.NextNonce(ctx, msg.From)
|
2020-09-23 16:28:11 +00:00
|
|
|
if err != nil {
|
|
|
|
return nil, xerrors.Errorf("failed to create nonce: %w", err)
|
|
|
|
}
|
|
|
|
|
2020-10-05 12:17:40 +00:00
|
|
|
// Sign the message with the nonce
|
2020-09-23 16:28:11 +00:00
|
|
|
msg.Nonce = nonce
|
2020-10-08 23:27:38 +00:00
|
|
|
|
2023-02-09 22:51:35 +00:00
|
|
|
sb, err := SigningBytes(msg, msg.From.Protocol())
|
2023-01-19 22:57:48 +00:00
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
2020-10-08 23:27:38 +00:00
|
|
|
mb, err := msg.ToStorageBlock()
|
|
|
|
if err != nil {
|
|
|
|
return nil, xerrors.Errorf("serializing message: %w", err)
|
|
|
|
}
|
|
|
|
|
2023-01-19 22:57:48 +00:00
|
|
|
sig, err := ms.wallet.WalletSign(ctx, msg.From, sb, api.MsgMeta{
|
2020-10-08 23:27:38 +00:00
|
|
|
Type: api.MTChainMsg,
|
|
|
|
Extra: mb.RawData(),
|
|
|
|
})
|
2023-01-19 22:57:48 +00:00
|
|
|
|
2020-09-23 16:28:11 +00:00
|
|
|
if err != nil {
|
2022-09-08 18:20:05 +00:00
|
|
|
return nil, xerrors.Errorf("failed to sign message: %w, addr=%s", err, msg.From)
|
2020-09-23 16:28:11 +00:00
|
|
|
}
|
|
|
|
|
2020-10-05 12:17:40 +00:00
|
|
|
// Callback with the signed message
|
|
|
|
smsg := &types.SignedMessage{
|
2020-09-23 16:28:11 +00:00
|
|
|
Message: *msg,
|
|
|
|
Signature: *sig,
|
2020-10-05 12:17:40 +00:00
|
|
|
}
|
2022-10-17 02:52:22 +00:00
|
|
|
|
2020-10-05 12:17:40 +00:00
|
|
|
err = cb(smsg)
|
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
|
|
|
|
// If the callback executed successfully, write the nonce to the datastore
|
2022-09-08 18:20:05 +00:00
|
|
|
if err := ms.SaveNonce(ctx, msg.From, nonce); err != nil {
|
2020-10-05 12:17:40 +00:00
|
|
|
return nil, xerrors.Errorf("failed to save nonce: %w", err)
|
|
|
|
}
|
|
|
|
|
|
|
|
return smsg, nil
|
2020-09-23 16:28:11 +00:00
|
|
|
}
|
|
|
|
|
2022-08-16 19:39:06 +00:00
|
|
|
func (ms *MessageSigner) GetSignedMessage(ctx context.Context, uuid uuid.UUID) (*types.SignedMessage, error) {
|
|
|
|
|
2022-08-16 20:44:14 +00:00
|
|
|
key := datastore.KeyWithNamespaces([]string{dsKeyMsgUUIDSet, uuid.String()})
|
2022-08-16 19:39:06 +00:00
|
|
|
bytes, err := ms.ds.Get(ctx, key)
|
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
return types.DecodeSignedMessage(bytes)
|
|
|
|
}
|
|
|
|
|
|
|
|
func (ms *MessageSigner) StoreSignedMessage(ctx context.Context, uuid uuid.UUID, message *types.SignedMessage) error {
|
|
|
|
|
2022-08-16 20:44:14 +00:00
|
|
|
key := datastore.KeyWithNamespaces([]string{dsKeyMsgUUIDSet, uuid.String()})
|
2022-08-16 19:39:06 +00:00
|
|
|
serializedMsg, err := message.Serialize()
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
return ms.ds.Put(ctx, key, serializedMsg)
|
|
|
|
}
|
|
|
|
|
2022-09-08 18:20:05 +00:00
|
|
|
// NextNonce gets the next nonce for the given address.
|
2020-09-23 16:28:11 +00:00
|
|
|
// If there is no nonce in the datastore, gets the nonce from the message pool.
|
2022-09-08 18:20:05 +00:00
|
|
|
func (ms *MessageSigner) NextNonce(ctx context.Context, addr address.Address) (uint64, error) {
|
2020-09-29 10:19:04 +00:00
|
|
|
// Nonces used to be created by the mempool and we need to support nodes
|
|
|
|
// that have mempool nonces, so first check the mempool for a nonce for
|
|
|
|
// this address. Note that the mempool returns the actor state's nonce
|
|
|
|
// by default.
|
2021-04-16 15:18:54 +00:00
|
|
|
nonce, err := ms.mpool.GetNonce(ctx, addr, types.EmptyTSK)
|
2020-09-29 10:19:04 +00:00
|
|
|
if err != nil {
|
|
|
|
return 0, xerrors.Errorf("failed to get nonce from mempool: %w", err)
|
|
|
|
}
|
2020-09-23 16:28:11 +00:00
|
|
|
|
2020-10-05 12:54:22 +00:00
|
|
|
// Get the next nonce for this address from the datastore
|
2022-09-21 19:41:10 +00:00
|
|
|
addrNonceKey := ms.dstoreKey(addr)
|
2021-12-11 21:03:00 +00:00
|
|
|
dsNonceBytes, err := ms.ds.Get(ctx, addrNonceKey)
|
2020-09-23 16:28:11 +00:00
|
|
|
|
|
|
|
switch {
|
|
|
|
case xerrors.Is(err, datastore.ErrNotFound):
|
|
|
|
// If a nonce for this address hasn't yet been created in the
|
2020-09-29 10:19:04 +00:00
|
|
|
// datastore, just use the nonce from the mempool
|
2020-10-05 12:54:22 +00:00
|
|
|
return nonce, nil
|
2020-09-23 16:28:11 +00:00
|
|
|
|
|
|
|
case err != nil:
|
|
|
|
return 0, xerrors.Errorf("failed to get nonce from datastore: %w", err)
|
|
|
|
|
|
|
|
default:
|
2020-10-05 12:54:22 +00:00
|
|
|
// There is a nonce in the datastore, so unmarshall it
|
|
|
|
maj, dsNonce, err := cbg.CborReadHeader(bytes.NewReader(dsNonceBytes))
|
2020-09-23 16:28:11 +00:00
|
|
|
if err != nil {
|
|
|
|
return 0, xerrors.Errorf("failed to parse nonce from datastore: %w", err)
|
|
|
|
}
|
|
|
|
if maj != cbg.MajUnsignedInt {
|
|
|
|
return 0, xerrors.Errorf("bad cbor type parsing nonce from datastore")
|
|
|
|
}
|
|
|
|
|
2020-09-29 10:19:04 +00:00
|
|
|
// The message pool nonce should be <= than the datastore nonce
|
|
|
|
if nonce <= dsNonce {
|
|
|
|
nonce = dsNonce
|
|
|
|
} else {
|
|
|
|
log.Warnf("mempool nonce was larger than datastore nonce (%d > %d)", nonce, dsNonce)
|
|
|
|
}
|
2020-09-23 16:28:11 +00:00
|
|
|
|
2020-10-05 12:54:22 +00:00
|
|
|
return nonce, nil
|
|
|
|
}
|
2020-10-05 12:17:40 +00:00
|
|
|
}
|
|
|
|
|
2022-09-08 18:20:05 +00:00
|
|
|
// SaveNonce increments the nonce for this address and writes it to the
|
2020-10-05 12:54:22 +00:00
|
|
|
// datastore
|
2022-09-08 18:20:05 +00:00
|
|
|
func (ms *MessageSigner) SaveNonce(ctx context.Context, addr address.Address, nonce uint64) error {
|
2020-10-05 12:54:22 +00:00
|
|
|
// Increment the nonce
|
|
|
|
nonce++
|
|
|
|
|
|
|
|
// Write the nonce to the datastore
|
2022-09-21 19:41:10 +00:00
|
|
|
addrNonceKey := ms.dstoreKey(addr)
|
2020-09-23 16:28:11 +00:00
|
|
|
buf := bytes.Buffer{}
|
2020-10-05 12:17:40 +00:00
|
|
|
_, err := buf.Write(cbg.CborEncodeMajorType(cbg.MajUnsignedInt, nonce))
|
2020-09-23 16:28:11 +00:00
|
|
|
if err != nil {
|
2020-10-05 12:17:40 +00:00
|
|
|
return xerrors.Errorf("failed to marshall nonce: %w", err)
|
2020-09-23 16:28:11 +00:00
|
|
|
}
|
2021-12-11 21:03:00 +00:00
|
|
|
err = ms.ds.Put(ctx, addrNonceKey, buf.Bytes())
|
2020-09-23 16:28:11 +00:00
|
|
|
if err != nil {
|
2020-10-05 12:17:40 +00:00
|
|
|
return xerrors.Errorf("failed to write nonce to datastore: %w", err)
|
2020-09-23 16:28:11 +00:00
|
|
|
}
|
2020-10-05 12:17:40 +00:00
|
|
|
return nil
|
|
|
|
}
|
2020-09-23 16:28:11 +00:00
|
|
|
|
2022-09-21 19:41:10 +00:00
|
|
|
func (ms *MessageSigner) dstoreKey(addr address.Address) datastore.Key {
|
2020-10-05 12:17:40 +00:00
|
|
|
return datastore.KeyWithNamespaces([]string{dsKeyActorNonce, addr.String()})
|
2020-09-23 16:28:11 +00:00
|
|
|
}
|
2023-01-19 22:57:48 +00:00
|
|
|
|
2023-02-09 22:51:35 +00:00
|
|
|
func SigningBytes(msg *types.Message, sigType address.Protocol) ([]byte, error) {
|
|
|
|
if sigType == address.Delegated {
|
2023-01-30 18:40:14 +00:00
|
|
|
txArgs, err := ethtypes.EthTxArgsFromUnsignedEthMessage(msg)
|
2023-01-19 22:57:48 +00:00
|
|
|
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
|
|
|
|
}
|