github.com/ava-labs/avalanchego@v1.11.11/wallet/chain/p/signer/signer.go (about)

     1  // Copyright (C) 2019-2024, Ava Labs, Inc. All rights reserved.
     2  // See the file LICENSE for licensing terms.
     3  
     4  package signer
     5  
     6  import (
     7  	"github.com/ava-labs/avalanchego/ids"
     8  	"github.com/ava-labs/avalanchego/utils/crypto/keychain"
     9  	"github.com/ava-labs/avalanchego/vms/components/avax"
    10  	"github.com/ava-labs/avalanchego/vms/platformvm/fx"
    11  	"github.com/ava-labs/avalanchego/vms/platformvm/txs"
    12  
    13  	stdcontext "context"
    14  )
    15  
    16  var _ Signer = (*txSigner)(nil)
    17  
    18  type Signer interface {
    19  	// Sign adds as many missing signatures as possible to the provided
    20  	// transaction.
    21  	//
    22  	// If there are already some signatures on the transaction, those signatures
    23  	// will not be removed.
    24  	//
    25  	// If the signer doesn't have the ability to provide a required signature,
    26  	// the signature slot will be skipped without reporting an error.
    27  	Sign(ctx stdcontext.Context, tx *txs.Tx) error
    28  }
    29  
    30  type Backend interface {
    31  	GetUTXO(ctx stdcontext.Context, chainID, utxoID ids.ID) (*avax.UTXO, error)
    32  	GetSubnetOwner(ctx stdcontext.Context, subnetID ids.ID) (fx.Owner, error)
    33  }
    34  
    35  type txSigner struct {
    36  	kc      keychain.Keychain
    37  	backend Backend
    38  }
    39  
    40  func New(kc keychain.Keychain, backend Backend) Signer {
    41  	return &txSigner{
    42  		kc:      kc,
    43  		backend: backend,
    44  	}
    45  }
    46  
    47  func (s *txSigner) Sign(ctx stdcontext.Context, tx *txs.Tx) error {
    48  	return tx.Unsigned.Visit(&visitor{
    49  		kc:      s.kc,
    50  		backend: s.backend,
    51  		ctx:     ctx,
    52  		tx:      tx,
    53  	})
    54  }
    55  
    56  func SignUnsigned(
    57  	ctx stdcontext.Context,
    58  	signer Signer,
    59  	utx txs.UnsignedTx,
    60  ) (*txs.Tx, error) {
    61  	tx := &txs.Tx{Unsigned: utx}
    62  	return tx, signer.Sign(ctx, tx)
    63  }