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

     1  /*
     2   * Copyright (C) 2017 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 program.  If not, see <http://www.gnu.org/licenses/>.
    16   */
    17  
    18  package identity
    19  
    20  import (
    21  	"testing"
    22  
    23  	"github.com/ethereum/go-ethereum/accounts"
    24  	ethKs "github.com/ethereum/go-ethereum/accounts/keystore"
    25  	"github.com/ethereum/go-ethereum/common"
    26  	"github.com/ethereum/go-ethereum/crypto"
    27  	"github.com/stretchr/testify/assert"
    28  
    29  	"github.com/mysteriumnetwork/node/eventbus"
    30  )
    31  
    32  var (
    33  	signerAddress = "53a835143c0ef3bbcbfa796d7eb738ca7dd28f68"
    34  	signerAccount = accounts.Account{
    35  		Address: common.HexToAddress("53a835143c0ef3bbcbfa796d7eb738ca7dd28f68"),
    36  	}
    37  	signerChainID int64 = 1
    38  	signerKey, _        = crypto.HexToECDSA("6f88637b68ee88816e73f663aef709d7009836c98ae91ef31e3dfac7be3a1657")
    39  )
    40  
    41  func TestSigningMessageWithUnlockedAccount(t *testing.T) {
    42  	ks := NewKeystoreFilesystem("dir", &ethKeystoreMock{account: signerAccount})
    43  	ks.loadKey = func(addr common.Address, filename, auth string) (*ethKs.Key, error) {
    44  		return &ethKs.Key{Address: addr, PrivateKey: signerKey}, nil
    45  	}
    46  
    47  	bus := eventbus.New()
    48  	manager := NewIdentityManager(ks, bus, NewResidentCountry(bus, newMockLocationResolver("LT")))
    49  	err := manager.Unlock(signerChainID, signerAddress, "")
    50  	assert.NoError(t, err)
    51  
    52  	signer := NewSigner(ks, FromAddress(signerAddress))
    53  	message := []byte("MystVpnSessionId:Boop!")
    54  	signature, err := signer.Sign([]byte(message))
    55  	signatureBase64 := signature.Base64()
    56  	t.Logf("signature in base64: %s", signatureBase64)
    57  	assert.NoError(t, err)
    58  	assert.Equal(
    59  		t,
    60  		SignatureBase64("V6ifmvLuAT+hbtLBX/0xm3C0afywxTIdw1HqLmA4onpwmibHbxVhl50Gr3aRUZMqw1WxkfSIVdhpbCluHGBKsgE="),
    61  		signature,
    62  	)
    63  }