github.com/ethersphere/bee/v2@v2.2.0/pkg/accesscontrol/mock/session.go (about)

     1  // Copyright 2024 The Swarm Authors. All rights reserved.
     2  // Use of this source code is governed by a BSD-style
     3  // license that can be found in the LICENSE file.
     4  
     5  package mock
     6  
     7  import (
     8  	"crypto/ecdsa"
     9  
    10  	"github.com/ethersphere/bee/v2/pkg/crypto"
    11  	"github.com/ethersphere/bee/v2/pkg/keystore"
    12  )
    13  
    14  type SessionMock struct {
    15  	KeyFunc func(publicKey *ecdsa.PublicKey, nonces [][]byte) ([][]byte, error)
    16  	key     *ecdsa.PrivateKey
    17  }
    18  
    19  func (s *SessionMock) Key(publicKey *ecdsa.PublicKey, nonces [][]byte) ([][]byte, error) {
    20  	if s.KeyFunc == nil {
    21  		return nil, nil
    22  	}
    23  	return s.KeyFunc(publicKey, nonces)
    24  }
    25  
    26  func NewSessionMock(key *ecdsa.PrivateKey) *SessionMock {
    27  	return &SessionMock{key: key}
    28  }
    29  
    30  func NewFromKeystore(
    31  	ks keystore.Service,
    32  	tag,
    33  	password string,
    34  	keyFunc func(publicKey *ecdsa.PublicKey, nonces [][]byte) ([][]byte, error),
    35  ) *SessionMock {
    36  	key, created, err := ks.Key(tag, password, crypto.EDGSecp256_K1)
    37  	if !created || err != nil {
    38  		return nil
    39  	}
    40  	return &SessionMock{
    41  		key:     key,
    42  		KeyFunc: keyFunc,
    43  	}
    44  }