github.com/klaytn/klaytn@v1.12.1/accounts/keystore/key2335_test.go (about)

     1  // Copyright 2018 The klaytn Authors
     2  // This file is part of the klaytn library.
     3  //
     4  // The klaytn library is free software: you can redistribute it and/or modify
     5  // it under the terms of the GNU Lesser 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  // The klaytn library 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 Lesser General Public License for more details.
    13  //
    14  // You should have received a copy of the GNU Lesser General Public License
    15  // along with the klaytn library. If not, see <http://www.gnu.org/licenses/>.
    16  
    17  package keystore
    18  
    19  import (
    20  	"os"
    21  	"strings"
    22  	"testing"
    23  
    24  	"github.com/klaytn/klaytn/common/hexutil"
    25  	"github.com/klaytn/klaytn/crypto/bls"
    26  	"github.com/stretchr/testify/assert"
    27  	"github.com/stretchr/testify/require"
    28  )
    29  
    30  func TestEncryptDecryptEIP2335(t *testing.T) {
    31  	password := "password"
    32  	sk, err := bls.RandKey()
    33  	require.Nil(t, err)
    34  
    35  	plain := NewKeyEIP2335(sk)
    36  
    37  	encrypted, err := EncryptKeyEIP2335(plain, password, LightScryptN, LightScryptP)
    38  	require.Nil(t, err)
    39  
    40  	decrypted, err := DecryptKeyEIP2335(encrypted, password)
    41  	require.Nil(t, err)
    42  
    43  	assert.Equal(t, plain.ID, decrypted.ID)
    44  	assert.Equal(t, plain.SecretKey.Marshal(), decrypted.SecretKey.Marshal())
    45  	assert.Equal(t, plain.PublicKey.Marshal(), decrypted.PublicKey.Marshal())
    46  }
    47  
    48  func TestDecryptEIP2335(t *testing.T) {
    49  	var (
    50  		// https://eips.ethereum.org/EIPS/eip-2335 test vectors
    51  		passwordBytes, _ = os.ReadFile("testdata/eip2335_password.txt")
    52  		password         = strings.TrimSpace(string(passwordBytes))
    53  		keyBytes         = hexutil.MustDecode("0x000000000019d6689c085ae165831e934ff763ae46a2a6c172b3f1b60a8ce26f")
    54  
    55  		scryptJSON, _ = os.ReadFile("testdata/eip2335_scrypt.json")
    56  		pbkdf2JSON, _ = os.ReadFile("testdata/eip2335_pbkdf2.json")
    57  	)
    58  
    59  	k, err := DecryptKeyEIP2335(scryptJSON, password)
    60  	require.Nil(t, err)
    61  	assert.Equal(t, keyBytes, k.SecretKey.Marshal())
    62  
    63  	k, err = DecryptKeyEIP2335(pbkdf2JSON, password)
    64  	require.Nil(t, err)
    65  	assert.Equal(t, keyBytes, k.SecretKey.Marshal())
    66  }