Skip to content
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

Minimize Signer interface and document Sign #2740

Merged
merged 3 commits into from
Feb 16, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
25 changes: 16 additions & 9 deletions wallet/chain/c/signer.go
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@ import (
"fmt"

"github.com/ava-labs/coreth/plugin/evm"
"github.com/ethereum/go-ethereum/common"

"github.com/ava-labs/avalanchego/database"
"github.com/ava-labs/avalanchego/ids"
Expand All @@ -20,7 +21,6 @@ import (
"github.com/ava-labs/avalanchego/vms/secp256k1fx"

stdcontext "context"
ethcommon "github.com/ethereum/go-ethereum/common"
)

const version = 0
Expand All @@ -37,16 +37,23 @@ var (
)

type Signer interface {
SignUnsignedAtomic(ctx stdcontext.Context, tx evm.UnsignedAtomicTx) (*evm.Tx, error)
// SignAtomic adds as many missing signatures as possible to the provided
// transaction.
//
// If there are already some signatures on the transaction, those signatures
// will not be removed.
//
// If the signer doesn't have the ability to provide a required signature,
// the signature slot will be skipped without reporting an error.
SignAtomic(ctx stdcontext.Context, tx *evm.Tx) error
}

type EthKeychain interface {
// The returned Signer can provide a signature for [addr]
GetEth(addr ethcommon.Address) (keychain.Signer, bool)
GetEth(addr common.Address) (keychain.Signer, bool)
// Returns the set of addresses for which the accessor keeps an associated
// signer
EthAddresses() set.Set[ethcommon.Address]
EthAddresses() set.Set[common.Address]
}

type SignerBackend interface {
Expand All @@ -67,11 +74,6 @@ func NewSigner(avaxKC keychain.Keychain, ethKC EthKeychain, backend SignerBacken
}
}

func (s *txSigner) SignUnsignedAtomic(ctx stdcontext.Context, utx evm.UnsignedAtomicTx) (*evm.Tx, error) {
tx := &evm.Tx{UnsignedAtomicTx: utx}
return tx, s.SignAtomic(ctx, tx)
}

func (s *txSigner) SignAtomic(ctx stdcontext.Context, tx *evm.Tx) error {
switch utx := tx.UnsignedAtomicTx.(type) {
case *evm.UnsignedImportTx:
Expand Down Expand Up @@ -150,6 +152,11 @@ func (s *txSigner) getExportSigners(ins []evm.EVMInput) [][]keychain.Signer {
return txSigners
}

func SignUnsignedAtomic(ctx stdcontext.Context, signer Signer, utx evm.UnsignedAtomicTx) (*evm.Tx, error) {
tx := &evm.Tx{UnsignedAtomicTx: utx}
return tx, signer.SignAtomic(ctx, tx)
}

// TODO: remove [signHash] after the ledger supports signing all transactions.
func sign(tx *evm.Tx, signHash bool, txSigners [][]keychain.Signer) error {
unsignedBytes, err := evm.Codec.Marshal(version, &tx.UnsignedAtomicTx)
Expand Down
2 changes: 1 addition & 1 deletion wallet/chain/c/wallet.go
Original file line number Diff line number Diff line change
Expand Up @@ -140,7 +140,7 @@ func (w *wallet) IssueUnsignedAtomicTx(
) (*evm.Tx, error) {
ops := common.NewOptions(options)
ctx := ops.Context()
tx, err := w.signer.SignUnsignedAtomic(ctx, utx)
tx, err := SignUnsignedAtomic(ctx, w.signer, utx)
if err != nil {
return nil, err
}
Expand Down
23 changes: 17 additions & 6 deletions wallet/chain/p/signer.go
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,14 @@ import (
var _ Signer = (*txSigner)(nil)

type Signer interface {
SignUnsigned(ctx stdcontext.Context, tx txs.UnsignedTx) (*txs.Tx, error)
// Sign adds as many missing signatures as possible to the provided
// transaction.
//
// If there are already some signatures on the transaction, those signatures
// will not be removed.
//
// If the signer doesn't have the ability to provide a required signature,
// the signature slot will be skipped without reporting an error.
Sign(ctx stdcontext.Context, tx *txs.Tx) error
}

Expand All @@ -37,11 +44,6 @@ func NewSigner(kc keychain.Keychain, backend SignerBackend) Signer {
}
}

func (s *txSigner) SignUnsigned(ctx stdcontext.Context, utx txs.UnsignedTx) (*txs.Tx, error) {
tx := &txs.Tx{Unsigned: utx}
return tx, s.Sign(ctx, tx)
}

func (s *txSigner) Sign(ctx stdcontext.Context, tx *txs.Tx) error {
return tx.Unsigned.Visit(&signerVisitor{
kc: s.kc,
Expand All @@ -50,3 +52,12 @@ func (s *txSigner) Sign(ctx stdcontext.Context, tx *txs.Tx) error {
tx: tx,
})
}

func SignUnsigned(
ctx stdcontext.Context,
signer Signer,
utx txs.UnsignedTx,
) (*txs.Tx, error) {
tx := &txs.Tx{Unsigned: utx}
return tx, signer.Sign(ctx, tx)
}
2 changes: 1 addition & 1 deletion wallet/chain/p/wallet.go
Original file line number Diff line number Diff line change
Expand Up @@ -495,7 +495,7 @@ func (w *wallet) IssueUnsignedTx(
) (*txs.Tx, error) {
ops := common.NewOptions(options)
ctx := ops.Context()
tx, err := w.signer.SignUnsigned(ctx, utx)
tx, err := SignUnsigned(ctx, w.signer, utx)
if err != nil {
return nil, err
}
Expand Down
23 changes: 17 additions & 6 deletions wallet/chain/x/signer.go
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,14 @@ import (
var _ Signer = (*signer)(nil)

type Signer interface {
SignUnsigned(ctx stdcontext.Context, tx txs.UnsignedTx) (*txs.Tx, error)
// Sign adds as many missing signatures as possible to the provided
// transaction.
//
// If there are already some signatures on the transaction, those signatures
// will not be removed.
//
// If the signer doesn't have the ability to provide a required signature,
// the signature slot will be skipped without reporting an error.
Sign(ctx stdcontext.Context, tx *txs.Tx) error
}

Expand All @@ -35,11 +42,6 @@ func NewSigner(kc keychain.Keychain, backend SignerBackend) Signer {
}
}

func (s *signer) SignUnsigned(ctx stdcontext.Context, utx txs.UnsignedTx) (*txs.Tx, error) {
tx := &txs.Tx{Unsigned: utx}
return tx, s.Sign(ctx, tx)
}

func (s *signer) Sign(ctx stdcontext.Context, tx *txs.Tx) error {
return tx.Unsigned.Visit(&signerVisitor{
kc: s.kc,
Expand All @@ -48,3 +50,12 @@ func (s *signer) Sign(ctx stdcontext.Context, tx *txs.Tx) error {
tx: tx,
})
}

func SignUnsigned(
ctx stdcontext.Context,
signer Signer,
utx txs.UnsignedTx,
) (*txs.Tx, error) {
tx := &txs.Tx{Unsigned: utx}
return tx, signer.Sign(ctx, tx)
}
2 changes: 1 addition & 1 deletion wallet/chain/x/wallet.go
Original file line number Diff line number Diff line change
Expand Up @@ -286,7 +286,7 @@ func (w *wallet) IssueUnsignedTx(
) (*txs.Tx, error) {
ops := common.NewOptions(options)
ctx := ops.Context()
tx, err := w.signer.SignUnsigned(ctx, utx)
tx, err := SignUnsigned(ctx, w.signer, utx)
if err != nil {
return nil, err
}
Expand Down
Loading