code.vegaprotocol.io/vega@v0.79.0/core/nodewallets/vega/wallet.go (about)

     1  // Copyright (C) 2023 Gobalsky Labs Limited
     2  //
     3  // This program is free software: you can redistribute it and/or modify
     4  // it under the terms of the GNU Affero General Public License as
     5  // published by the Free Software Foundation, either version 3 of the
     6  // License, or (at your option) any later version.
     7  //
     8  // This program is distributed in the hope that it will be useful,
     9  // but WITHOUT ANY WARRANTY; without even the implied warranty of
    10  // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
    11  // GNU Affero General Public License for more details.
    12  //
    13  // You should have received a copy of the GNU Affero General Public License
    14  // along with this program.  If not, see <http://www.gnu.org/licenses/>.
    15  
    16  package vega
    17  
    18  import (
    19  	"fmt"
    20  	"sync"
    21  
    22  	"code.vegaprotocol.io/vega/core/nodewallets/registry"
    23  	"code.vegaprotocol.io/vega/libs/crypto"
    24  	"code.vegaprotocol.io/vega/wallet/wallet"
    25  )
    26  
    27  type loader interface {
    28  	Load(walletName, passphrase string) (*Wallet, error)
    29  }
    30  
    31  type Wallet struct {
    32  	loader   loader
    33  	name     string
    34  	keyPair  wallet.KeyPair
    35  	pubKey   crypto.PublicKey
    36  	walletID crypto.PublicKey
    37  	mut      sync.Mutex
    38  }
    39  
    40  func (w *Wallet) Name() string {
    41  	return w.name
    42  }
    43  
    44  func (w *Wallet) Chain() string {
    45  	return "vega"
    46  }
    47  
    48  func (w *Wallet) Sign(data []byte) ([]byte, error) {
    49  	return w.keyPair.SignAny(data)
    50  }
    51  
    52  func (w *Wallet) Algo() string {
    53  	return w.keyPair.AlgorithmName()
    54  }
    55  
    56  func (w *Wallet) Version() uint32 {
    57  	return w.keyPair.AlgorithmVersion()
    58  }
    59  
    60  func (w *Wallet) PubKey() crypto.PublicKey {
    61  	return w.pubKey
    62  }
    63  
    64  func (w *Wallet) Index() uint32 {
    65  	return w.keyPair.Index()
    66  }
    67  
    68  func (w *Wallet) ID() crypto.PublicKey {
    69  	return w.walletID
    70  }
    71  
    72  func (w *Wallet) Reload(rw registry.RegisteredVegaWallet) error {
    73  	nW, err := w.loader.Load(rw.Name, rw.Passphrase)
    74  	if err != nil {
    75  		return fmt.Errorf("failed to load wallet: %w", err)
    76  	}
    77  
    78  	w.mut.Lock()
    79  	defer w.mut.Unlock()
    80  
    81  	w.name = nW.name
    82  	w.keyPair = nW.keyPair
    83  	w.pubKey = nW.pubKey
    84  	w.walletID = nW.walletID
    85  
    86  	return nil
    87  }