github.com/hashgraph/hedera-sdk-go/v2@v2.48.0/keystore_unit_test.go (about)

     1  //go:build all || unit
     2  // +build all unit
     3  
     4  package hedera
     5  
     6  /*-
     7   *
     8   * Hedera Go SDK
     9   *
    10   * Copyright (C) 2020 - 2024 Hedera Hashgraph, LLC
    11   *
    12   * Licensed under the Apache License, Version 2.0 (the "License");
    13   * you may not use this file except in compliance with the License.
    14   * You may obtain a copy of the License at
    15   *
    16   *      http://www.apache.org/licenses/LICENSE-2.0
    17   *
    18   * Unless required by applicable law or agreed to in writing, software
    19   * distributed under the License is distributed on an "AS IS" BASIS,
    20   * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
    21   * See the License for the specific language governing permissions and
    22   * limitations under the License.
    23   *
    24   */
    25  
    26  import (
    27  	"testing"
    28  
    29  	"github.com/stretchr/testify/assert"
    30  
    31  	"github.com/stretchr/testify/require"
    32  )
    33  
    34  const passphrase string = "HelloHashgraph!"
    35  
    36  const testKeystoreKeyString string = "45e512c479dc40bc47561c507daa962aa17b6133f8433c459adb481e06cbdafb"
    37  
    38  // generated with the JS SDK
    39  const testKeystore string = `{"version":1,"crypto":{"ciphertext":"9dfa728ba59e50d745f76a5fb4cab6918d4096869ab436a879731095f621456f5add51715ce81383a7996f3a75359e8216102285238a3ad8fac4dfa17894a6aa","cipherparams":{"iv":"c3198c3529fef9c5e2886f19c479683e"},"cipher":"aes-128-ctr","kdf":"pbkdf2","kdfparams":{"dkLen":32,"salt":"c87aba46b7db247694763cff3f2ec18bad1006590c6bb9befc14f05b2b2af479","c":262144,"prf":"hmac-sha256"},"mac":"f6f7a1552b3618209073feebe0109f57a3df57d8b11f07ff44aad82bafffbd15636263b96cfd2328b122d2851771c7b4"}}`
    40  
    41  func TestUnitDecryptKeyStore(t *testing.T) {
    42  	t.Parallel()
    43  
    44  	privateKey, err := PrivateKeyFromString(testKeystoreKeyString)
    45  	require.NoError(t, err)
    46  
    47  	ksPrivateKey, err := _ParseKeystore([]byte(testKeystore), passphrase)
    48  	require.NoError(t, err)
    49  
    50  	assert.Equal(t, privateKey.ed25519PrivateKey.keyData, ksPrivateKey.ed25519PrivateKey.keyData)
    51  }
    52  
    53  func TestUnitEncryptAndDecryptKeyStore(t *testing.T) {
    54  	t.Parallel()
    55  
    56  	privateKey, err := PrivateKeyFromString(testPrivateKeyStr)
    57  	require.NoError(t, err)
    58  
    59  	keyStore, err := _NewKeystore(privateKey.Bytes(), passphrase)
    60  	require.NoError(t, err)
    61  
    62  	ksPrivateKey, err := _ParseKeystore(keyStore, passphrase)
    63  	require.NoError(t, err)
    64  
    65  	assert.Equal(t, privateKey.ed25519PrivateKey.keyData, ksPrivateKey.ed25519PrivateKey.keyData)
    66  }