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"
|
|
|
|
"github.com/filecoin-project/lotus/chain/types"
|
|
|
|
"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")
|
|
|
|
|
2020-10-07 09:26:15 +00:00
|
|
|
type MpoolNonceAPI interface {
|
2021-04-15 16:36:04 +00:00
|
|
|
GetNonce(context.Context, address.Address, types.TipSetKey) (uint64, error)
|
2021-05-07 13:20:37 +00:00
|
|
|
GetActor(context.Context, address.Address, types.TipSetKey) (*types.Actor, 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
|
2020-10-07 09:26:15 +00:00
|
|
|
mpool MpoolNonceAPI
|
2020-09-23 16:28:11 +00:00
|
|
|
ds datastore.Batching
|
|
|
|
}
|
|
|
|
|
2021-03-23 12:42:56 +00:00
|
|
|
func NewMessageSigner(wallet api.Wallet, mpool 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
|
2020-10-05 12:17:40 +00:00
|
|
|
func (ms *MessageSigner) SignMessage(ctx context.Context, msg *types.Message, cb func(*types.SignedMessage) error) (*types.SignedMessage, error) {
|
|
|
|
ms.lk.Lock()
|
|
|
|
defer ms.lk.Unlock()
|
|
|
|
|
|
|
|
// Get the next message nonce
|
2021-04-16 15:18:54 +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
|
|
|
|
|
|
|
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{
|
|
|
|
Type: api.MTChainMsg,
|
|
|
|
Extra: mb.RawData(),
|
|
|
|
})
|
2020-09-23 16:28:11 +00:00
|
|
|
if err != nil {
|
|
|
|
return nil, xerrors.Errorf("failed to sign message: %w", err)
|
|
|
|
}
|
|
|
|
|
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
|
|
|
}
|
|
|
|
err = cb(smsg)
|
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
|
|
|
|
// If the callback executed successfully, write the nonce to the datastore
|
2021-12-11 21:03:00 +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)
|
|
|
|
}
|
|
|
|
|
2020-10-05 12:54:22 +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.
|
2021-04-16 15:18:54 +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
|
2020-10-05 12:17:40 +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
|
|
|
}
|
|
|
|
|
2020-10-05 12:54:22 +00:00
|
|
|
// saveNonce increments the nonce for this address and writes it to the
|
|
|
|
// datastore
|
2021-12-11 21:03:00 +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
|
2020-10-05 12:17:40 +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
|
|
|
|
2020-10-05 12:17:40 +00:00
|
|
|
func (ms *MessageSigner) dstoreKey(addr address.Address) datastore.Key {
|
|
|
|
return datastore.KeyWithNamespaces([]string{dsKeyActorNonce, addr.String()})
|
2020-09-23 16:28:11 +00:00
|
|
|
}
|