github.com/ava-labs/avalanchego@v1.11.11/wallet/chain/x/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  	"context"
     8  
     9  	"github.com/ava-labs/avalanchego/ids"
    10  	"github.com/ava-labs/avalanchego/utils/crypto/keychain"
    11  	"github.com/ava-labs/avalanchego/vms/avm/txs"
    12  	"github.com/ava-labs/avalanchego/vms/components/avax"
    13  )
    14  
    15  var _ Signer = (*signer)(nil)
    16  
    17  type Signer interface {
    18  	// Sign adds as many missing signatures as possible to the provided
    19  	// transaction.
    20  	//
    21  	// If there are already some signatures on the transaction, those signatures
    22  	// will not be removed.
    23  	//
    24  	// If the signer doesn't have the ability to provide a required signature,
    25  	// the signature slot will be skipped without reporting an error.
    26  	Sign(ctx context.Context, tx *txs.Tx) error
    27  }
    28  
    29  type Backend interface {
    30  	GetUTXO(ctx context.Context, chainID, utxoID ids.ID) (*avax.UTXO, error)
    31  }
    32  
    33  type signer struct {
    34  	kc      keychain.Keychain
    35  	backend Backend
    36  }
    37  
    38  func New(kc keychain.Keychain, backend Backend) Signer {
    39  	return &signer{
    40  		kc:      kc,
    41  		backend: backend,
    42  	}
    43  }
    44  
    45  func (s *signer) Sign(ctx context.Context, tx *txs.Tx) error {
    46  	return tx.Unsigned.Visit(&visitor{
    47  		kc:      s.kc,
    48  		backend: s.backend,
    49  		ctx:     ctx,
    50  		tx:      tx,
    51  	})
    52  }
    53  
    54  func SignUnsigned(
    55  	ctx context.Context,
    56  	signer Signer,
    57  	utx txs.UnsignedTx,
    58  ) (*txs.Tx, error) {
    59  	tx := &txs.Tx{Unsigned: utx}
    60  	return tx, signer.Sign(ctx, tx)
    61  }