github.com/mysteriumnetwork/node@v0.0.0-20240516044423-365054f76801/identity/mover.go (about)

     1  /*
     2   * Copyright (C) 2021 The "MysteriumNetwork/node" Authors.
     3   *
     4   * This program is free software: you can redistribute it and/or modify
     5   * it under the terms of the GNU General Public License as published by
     6   * the Free Software Foundation, either version 3 of the License, or
     7   * (at your option) any later version.
     8   *
     9   * This program is distributed in the hope that it will be useful,
    10   * but WITHOUT ANY WARRANTY; without even the implied warranty of
    11   * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
    12   * GNU General Public License for more details.
    13   *
    14   * You should have received a copy of the GNU General Public License
    15   * along with this prograi.  If not, see <http://www.gnu.org/licenses/>.
    16   */
    17  
    18  package identity
    19  
    20  import (
    21  	"errors"
    22  
    23  	"github.com/mysteriumnetwork/node/eventbus"
    24  
    25  	"github.com/ethereum/go-ethereum/accounts"
    26  )
    27  
    28  // Mover is wrapper on both the Exporter and Importer
    29  // and can be used to manipulate private keys in to either direction.
    30  type Mover struct {
    31  	*Exporter
    32  	*Importer
    33  }
    34  
    35  type moverKeystore interface {
    36  	Unlock(a accounts.Account, passphrase string) error
    37  	Find(a accounts.Account) (accounts.Account, error)
    38  	Export(a accounts.Account, passphrase, newPassphrase string) ([]byte, error)
    39  	Import(keyJSON []byte, passphrase, newPassphrase string) (accounts.Account, error)
    40  }
    41  
    42  type moverIdentityHandler interface {
    43  	IdentityExists(Identity, Signer) (bool, error)
    44  }
    45  
    46  // NewMover returns a new mover object.
    47  func NewMover(ks moverKeystore, events eventbus.EventBus, signer SignerFactory) *Mover {
    48  	return &Mover{
    49  		Exporter: NewExporter(ks),
    50  		Importer: NewImporter(ks, events, signer),
    51  	}
    52  }
    53  
    54  // Importer exposes a way to import an private keys.
    55  type Importer struct {
    56  	ks            moverKeystore
    57  	eventBus      eventbus.EventBus
    58  	signerFactory SignerFactory
    59  }
    60  
    61  // NewImporter returns a new importer object.
    62  func NewImporter(ks moverKeystore, events eventbus.EventBus, signer SignerFactory) *Importer {
    63  	return &Importer{
    64  		ks:            ks,
    65  		eventBus:      events,
    66  		signerFactory: signer,
    67  	}
    68  }
    69  
    70  // Import imports a given blob as a new identity. It will return an
    71  // error if that identity was never registered.
    72  func (i *Importer) Import(blob []byte, currPass, newPass string) (Identity, error) {
    73  	acc, err := i.ks.Import(blob, currPass, newPass)
    74  	if err != nil {
    75  		return Identity{}, err
    76  	}
    77  
    78  	if err := i.ks.Unlock(acc, newPass); err != nil {
    79  		return Identity{}, err
    80  	}
    81  
    82  	identity := accountToIdentity(acc)
    83  	i.eventBus.Publish(AppTopicIdentityCreated, identity.Address)
    84  	return identity, nil
    85  }
    86  
    87  // Exporter exposes a way to export private keys.
    88  type Exporter struct {
    89  	ks moverKeystore
    90  }
    91  
    92  // NewExporter returns a new exporter object.
    93  func NewExporter(ks moverKeystore) *Exporter {
    94  	return &Exporter{
    95  		ks: ks,
    96  	}
    97  }
    98  
    99  // Export exports a given identity and returns it as json blob.
   100  func (e *Exporter) Export(address, currPass, newPass string) ([]byte, error) {
   101  	acc := addressToAccount(address)
   102  	_, err := e.ks.Find(acc)
   103  	if err != nil {
   104  		return nil, errors.New("identity not found")
   105  	}
   106  
   107  	return e.ks.Export(acc, currPass, newPass)
   108  }