github.com/hashgraph/hedera-sdk-go/v2@v2.48.0/crypto_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  	"bytes"
    28  	"crypto/ed25519"
    29  	"encoding/hex"
    30  	"fmt"
    31  	"strings"
    32  	"testing"
    33  
    34  	"github.com/hashgraph/hedera-protobufs-go/services"
    35  
    36  	"github.com/ethereum/go-ethereum/crypto"
    37  	"github.com/stretchr/testify/assert"
    38  	"github.com/stretchr/testify/require"
    39  	"google.golang.org/protobuf/proto"
    40  )
    41  
    42  const _Ed25519PubKeyPrefix = "302a300506032b6570032100"
    43  const _ECDSAPubKeyPrefix = "3036301006072a8648ce3d020106052b8104000a0322000"
    44  const _ECDSAPrivatePrefix = "30540201010420"
    45  
    46  const testPrivateKeyStr = "302e020100300506032b657004220420db484b828e64b2d8f12ce3c0a0e93a0b8cce7af1bb8f39c97732394482538e10"
    47  
    48  const testPublicKeyStr = "302a300506032b6570032100e0c8ec2758a5879ffac226a13c0c516b799e72e35141a0dd828f94d37988a4b7"
    49  
    50  const testMnemonic3 = "obvious favorite remain caution remove laptop base vacant increase video erase pass sniff sausage knock grid argue salt romance way alone fever slush dune"
    51  
    52  // generated by hedera-keygen-java, not used anywhere
    53  const testMnemonic = "inmate flip alley wear offer often piece magnet surge toddler submit right radio absent pear floor belt raven price stove replace reduce plate home"
    54  const testMnemonicKey = "302e020100300506032b657004220420853f15aecd22706b105da1d709b4ac05b4906170c2b9c7495dff9af49e1391da"
    55  
    56  // backup phrase generated by the iOS wallet, not used anywhere
    57  const iosMnemonicString = "tiny denial casual grass skull spare awkward indoor ethics dash enough flavor good daughter early hard rug staff capable swallow raise flavor empty angle"
    58  
    59  // private key for "default account", should be index 0
    60  const iosDefaultPrivateKey = "5f66a51931e8c99089472e0d70516b6272b94dd772b967f8221e1077f966dbda2b60cf7ee8cf10ecd5a076bffad9a7c7b97df370ad758c0f1dd4ef738e04ceb6"
    61  
    62  // backup phrase generated by the Android wallet, also not used anywhere
    63  const androidMnemonicString = "ramp april job flavor surround pyramid fish sea good know blame gate village viable include mixed term draft among monitor swear swing novel track"
    64  
    65  // private key for "default account", should be index 0
    66  const androidDefaultPrivateKey = "c284c25b3a1458b59423bc289e83703b125c8eefec4d5aa1b393c2beb9f2bae66188a344ba75c43918ab12fa2ea4a92960eca029a2320d8c6a1c3b94e06c9985"
    67  
    68  // test pem key contests for the above testPrivateKeyStr
    69  const pemString = `-----BEGIN PRIVATE KEY-----
    70  MC4CAQAwBQYDK2VwBCIEINtIS4KOZLLY8SzjwKDpOguMznrxu485yXcyOUSCU44Q
    71  -----END PRIVATE KEY-----
    72  `
    73  
    74  // const encryptedPem = `-----BEGIN ENCRYPTED PRIVATE KEY-----
    75  // MIGbMFcGCSqGSIb3DQEFDTBKMCkGCSqGSIb3DQEFDDAcBAi8WY7Gy2tThQICCAAw
    76  // DAYIKoZIhvcNAgkFADAdBglghkgBZQMEAQIEEOq46NPss58chbjUn20NoK0EQG1x
    77  // R88hIXcWDOECttPTNlMXWJt7Wufm1YwBibrxmCq1QykIyTYhy1TZMyxyPxlYW6aV
    78  // 9hlo4YEh3uEaCmfJzWM=
    79  // -----END ENCRYPTED PRIVATE KEY-----`
    80  
    81  const encryptedPem = `-----BEGIN ENCRYPTED PRIVATE KEY-----
    82  MIGbMFcGCSqGSIb3DQEFDTBKMCkGCSqGSIb3DQEFDDAcBAi8WY7Gy2tThQICCAAw
    83  DAYIKoZIhvcNAgkFADAdBglghkgBZQMEAQIEEOq46NPss58chbjUn20NoK0EQG1x
    84  R88hIXcWDOECttPTNlMXWJt7Wufm1YwBibrxmCq1QykIyTYhy1TZMyxyPxlYW6aV
    85  9hlo4YEh3uEaCmfJzWM=
    86  -----END ENCRYPTED PRIVATE KEY-----
    87  `
    88  const pemPassphrase = "this is a passphrase"
    89  
    90  func TestUnitPrivateKeyGenerate(t *testing.T) {
    91  	t.Parallel()
    92  
    93  	key, err := GeneratePrivateKey()
    94  
    95  	require.NoError(t, err)
    96  	assert.True(t, strings.HasPrefix(key.String(), _Ed25519PrivateKeyPrefix))
    97  }
    98  
    99  func TestUnitPrivateEd25519KeyGenerate(t *testing.T) {
   100  	t.Parallel()
   101  
   102  	key, err := PrivateKeyGenerateEd25519()
   103  
   104  	require.NoError(t, err)
   105  	assert.True(t, strings.HasPrefix(key.String(), _Ed25519PrivateKeyPrefix))
   106  }
   107  
   108  func TestUnitPrivateECDSAKeyGenerate(t *testing.T) {
   109  	t.Parallel()
   110  
   111  	key, err := PrivateKeyGenerateEcdsa()
   112  
   113  	require.NoError(t, err)
   114  	assert.True(t, strings.HasPrefix(key.String(), _ECDSAPrivatePrefix))
   115  }
   116  
   117  func TestUnitPrivateKeyExternalSerialization(t *testing.T) {
   118  	t.Parallel()
   119  
   120  	key, err := PrivateKeyFromString(testPrivateKeyStr)
   121  
   122  	require.NoError(t, err)
   123  	assert.Equal(t, testPrivateKeyStr, key.String())
   124  }
   125  
   126  func TestUnitPrivateKeyExternalSerializationForConcatenatedHex(t *testing.T) {
   127  	t.Parallel()
   128  
   129  	keyStr := "db484b828e64b2d8f12ce3c0a0e93a0b8cce7af1bb8f39c97732394482538e10e0c8ec2758a5879ffac226a13c0c516b799e72e35141a0dd828f94d37988a4b7"
   130  	key, err := PrivateKeyFromStringEd25519(keyStr)
   131  
   132  	require.NoError(t, err)
   133  	assert.Equal(t, testPrivateKeyStr, key.String())
   134  }
   135  
   136  func TestUnitShouldMatchHbarWalletV1(t *testing.T) {
   137  	t.Parallel()
   138  
   139  	mnemonic, err := MnemonicFromString("jolly kidnap tom lawn drunk chick optic lust mutter mole bride galley dense member sage neural widow decide curb aboard margin manure")
   140  	require.NoError(t, err)
   141  
   142  	key, err := mnemonic.ToLegacyPrivateKey()
   143  	require.NoError(t, err)
   144  
   145  	deriveKey, err := key.LegacyDerive(1099511627775)
   146  	require.NoError(t, err)
   147  
   148  	assert.Equal(t, "302a300506032b657003210045f3a673984a0b4ee404a1f4404ed058475ecd177729daa042e437702f7791e9", deriveKey.PublicKey().String())
   149  }
   150  
   151  func TestUnitLegacyPrivateKeyFromMnemonicDerive(t *testing.T) {
   152  	t.Parallel()
   153  
   154  	mnemonic, err := MnemonicFromString("jolly kidnap tom lawn drunk chick optic lust mutter mole bride galley dense member sage neural widow decide curb aboard margin manure")
   155  	require.NoError(t, err)
   156  
   157  	key, err := mnemonic.ToLegacyPrivateKey()
   158  	require.NoError(t, err)
   159  
   160  	deriveKey, err := key.LegacyDerive(0)
   161  	require.NoError(t, err)
   162  	deriveKey2, err := key.LegacyDerive(-1)
   163  	require.NoError(t, err)
   164  
   165  	assert.Equal(t, "302e020100300506032b657004220420882a565ad8cb45643892b5366c1ee1c1ef4a730c5ce821a219ff49b6bf173ddf", deriveKey2.String())
   166  	assert.Equal(t, "302e020100300506032b657004220420fae0002d2716ea3a60c9cd05ee3c4bb88723b196341b68a02d20975f9d049dc6", deriveKey.String())
   167  }
   168  
   169  func TestUnitPrivateKeyExternalSerializationForRawHex(t *testing.T) {
   170  	t.Parallel()
   171  
   172  	keyStr := "db484b828e64b2d8f12ce3c0a0e93a0b8cce7af1bb8f39c97732394482538e10"
   173  	key, err := PrivateKeyFromStringEd25519(keyStr)
   174  
   175  	require.NoError(t, err)
   176  	assert.Equal(t, testPrivateKeyStr, key.String())
   177  }
   178  
   179  func TestUnitPublicKeyExternalSerializationForDerEncodedHex(t *testing.T) {
   180  	t.Parallel()
   181  
   182  	key, err := PublicKeyFromString(testPublicKeyStr)
   183  
   184  	require.NoError(t, err)
   185  	assert.Equal(t, testPublicKeyStr, key.String())
   186  }
   187  
   188  func TestUnitPublicKeyExternalSerializationForRawHex(t *testing.T) {
   189  	t.Parallel()
   190  
   191  	keyStr := "e0c8ec2758a5879ffac226a13c0c516b799e72e35141a0dd828f94d37988a4b7"
   192  	key, err := PublicKeyFromStringEd25519(keyStr)
   193  
   194  	require.NoError(t, err)
   195  	assert.Equal(t, testPublicKeyStr, key.String())
   196  }
   197  
   198  func TestUnitMnemonicToPrivateKey(t *testing.T) {
   199  	t.Parallel()
   200  
   201  	mnemonic, err := MnemonicFromString(testMnemonic)
   202  	require.NoError(t, err)
   203  
   204  	key, err := mnemonic.ToPrivateKey("")
   205  	require.NoError(t, err)
   206  
   207  	assert.Equal(t, testMnemonicKey, key.String())
   208  }
   209  
   210  func TestUnitIOSPrivateKeyFromMnemonic(t *testing.T) {
   211  	t.Parallel()
   212  
   213  	mnemonic, err := MnemonicFromString(iosMnemonicString)
   214  	require.NoError(t, err)
   215  
   216  	key, err := PrivateKeyFromMnemonic(mnemonic, "")
   217  	require.NoError(t, err)
   218  
   219  	derivedKey, err := key.Derive(0)
   220  	require.NoError(t, err)
   221  
   222  	expectedKey, err := PrivateKeyFromString(iosDefaultPrivateKey)
   223  	require.NoError(t, err)
   224  
   225  	assert.Equal(t, expectedKey.ed25519PrivateKey.keyData, derivedKey.ed25519PrivateKey.keyData)
   226  }
   227  
   228  func TestUnitAndroidPrivateKeyFromMnemonic(t *testing.T) {
   229  	t.Parallel()
   230  
   231  	mnemonic, err := MnemonicFromString(androidMnemonicString)
   232  	require.NoError(t, err)
   233  
   234  	key, err := PrivateKeyFromMnemonic(mnemonic, "")
   235  	require.NoError(t, err)
   236  
   237  	derivedKey, err := key.Derive(0)
   238  	require.NoError(t, err)
   239  
   240  	expectedKey, err := PrivateKeyFromString(androidDefaultPrivateKey)
   241  	require.NoError(t, err)
   242  
   243  	assert.Equal(t, expectedKey.ed25519PrivateKey.keyData, derivedKey.ed25519PrivateKey.keyData)
   244  }
   245  
   246  func TestUnitMnemonic3(t *testing.T) {
   247  	t.Parallel()
   248  
   249  	mnemonic, err := MnemonicFromString(testMnemonic3)
   250  	require.NoError(t, err)
   251  
   252  	key, err := mnemonic.ToLegacyPrivateKey()
   253  	require.NoError(t, err)
   254  
   255  	derivedKey, err := key.LegacyDerive(0)
   256  	require.NoError(t, err)
   257  	derivedKey2, err := key.LegacyDerive(-1)
   258  	require.NoError(t, err)
   259  
   260  	assert.Equal(t, "302e020100300506032b6570042204202b7345f302a10c2a6d55bf8b7af40f125ec41d780957826006d30776f0c441fb", derivedKey.String())
   261  	assert.Equal(t, "302e020100300506032b657004220420caffc03fdb9853e6a91a5b3c57a5c0031d164ce1c464dea88f3114786b5199e5", derivedKey2.String())
   262  }
   263  
   264  func TestUnitSigning(t *testing.T) {
   265  	t.Parallel()
   266  
   267  	priKey, err := PrivateKeyFromString(testPrivateKeyStr)
   268  	require.NoError(t, err)
   269  
   270  	pubKey, err := PublicKeyFromString(testPublicKeyStr)
   271  	require.NoError(t, err)
   272  
   273  	testSignData := []byte("this is the test data to sign")
   274  	signature := priKey.Sign(testSignData)
   275  
   276  	assert.True(t, ed25519.Verify(pubKey.Bytes(), []byte("this is the test data to sign"), signature))
   277  }
   278  
   279  func TestUnitGenerated24MnemonicToWorkingPrivateKey(t *testing.T) {
   280  	t.Parallel()
   281  
   282  	mnemonic, err := GenerateMnemonic24()
   283  
   284  	require.NoError(t, err)
   285  
   286  	privateKey, err := mnemonic.ToPrivateKey("")
   287  
   288  	require.NoError(t, err)
   289  
   290  	message := []byte("this is a test message")
   291  
   292  	signature := privateKey.Sign(message)
   293  
   294  	assert.True(t, ed25519.Verify(privateKey.PublicKey().Bytes(), message, signature))
   295  }
   296  
   297  func TestUnitGenerated12MnemonicToWorkingPrivateKey(t *testing.T) {
   298  	t.Parallel()
   299  
   300  	mnemonic, err := GenerateMnemonic12()
   301  
   302  	require.NoError(t, err)
   303  
   304  	privateKey, err := mnemonic.ToPrivateKey("")
   305  
   306  	require.NoError(t, err)
   307  
   308  	message := []byte("this is a test message")
   309  
   310  	signature := privateKey.Sign(message)
   311  
   312  	assert.True(t, ed25519.Verify(privateKey.PublicKey().Bytes(), message, signature))
   313  }
   314  
   315  func TestUnitPrivateKeyFromKeystore(t *testing.T) {
   316  	t.Parallel()
   317  
   318  	privatekey, err := PrivateKeyFromKeystore([]byte(testKeystore), passphrase)
   319  	require.NoError(t, err)
   320  
   321  	actualPrivateKey, err := PrivateKeyFromStringEd25519(testKeystoreKeyString)
   322  	require.NoError(t, err)
   323  
   324  	assert.Equal(t, actualPrivateKey.ed25519PrivateKey.keyData, privatekey.ed25519PrivateKey.keyData)
   325  }
   326  
   327  func TestUnitPrivateKeyKeystore(t *testing.T) {
   328  	t.Parallel()
   329  
   330  	privateKey, err := PrivateKeyFromString(testPrivateKeyStr)
   331  	require.NoError(t, err)
   332  
   333  	keystore, err := privateKey.Keystore(passphrase)
   334  	require.NoError(t, err)
   335  
   336  	ksPrivateKey, err := _ParseKeystore(keystore, passphrase)
   337  	require.NoError(t, err)
   338  
   339  	assert.Equal(t, privateKey.ed25519PrivateKey.keyData, ksPrivateKey.ed25519PrivateKey.keyData)
   340  }
   341  
   342  func TestUnitPrivateKeyReadKeystore(t *testing.T) {
   343  	t.Parallel()
   344  
   345  	actualPrivateKey, err := PrivateKeyFromStringEd25519(testKeystoreKeyString)
   346  	require.NoError(t, err)
   347  
   348  	keystoreReader := bytes.NewReader([]byte(testKeystore))
   349  
   350  	privateKey, err := PrivateKeyReadKeystore(keystoreReader, passphrase)
   351  	require.NoError(t, err)
   352  
   353  	assert.Equal(t, actualPrivateKey.ed25519PrivateKey.keyData, privateKey.ed25519PrivateKey.keyData)
   354  }
   355  
   356  func TestUnitPrivateKeyFromPem(t *testing.T) {
   357  	t.Parallel()
   358  
   359  	actualPrivateKey, err := PrivateKeyFromString(testPrivateKeyStr)
   360  	require.NoError(t, err)
   361  
   362  	privateKey, err := PrivateKeyFromPem([]byte(pemString), "")
   363  	require.NoError(t, err)
   364  
   365  	assert.Equal(t, actualPrivateKey, privateKey)
   366  }
   367  
   368  func TestUnitPrivateKeyFromPemInvalid(t *testing.T) {
   369  	t.Parallel()
   370  
   371  	_, err := PrivateKeyFromPem([]byte("invalid"), "")
   372  	assert.Error(t, err)
   373  }
   374  
   375  func TestUnitPrivateKeyFromPemWithPassphrase(t *testing.T) {
   376  	t.Parallel()
   377  
   378  	actualPrivateKey, err := PrivateKeyFromString(testPrivateKeyStr)
   379  	require.NoError(t, err)
   380  
   381  	privateKey, err := PrivateKeyFromPem([]byte(encryptedPem), pemPassphrase)
   382  	require.NoError(t, err)
   383  
   384  	assert.Equal(t, actualPrivateKey, privateKey)
   385  }
   386  
   387  func TestUnitECDSAPrivateKeyFromUnencryptedUncompressedPEM(t *testing.T) {
   388  	unencryptedUncompressed := "-----BEGIN EC PRIVATE KEY-----\n" +
   389  		"MHQCAQEEIG8I+jKi+iGVa7ttbfnlnML5AdvPugbgBWnseYjrle6qoAcGBSuBBAAK\n" +
   390  		"oUQDQgAEqf5BmMeBzkU1Ra9UAbZJo3tytVOlb7erTc36LRLP20mOLU7+mFY+3Cfe\n" +
   391  		"fAZgBtPXRAmDtRvYGODswAalW85GKA==\n" +
   392  		"-----END EC PRIVATE KEY-----"
   393  
   394  	privateKey, err := PrivateKeyFromPem([]byte(unencryptedUncompressed), "")
   395  	require.NoError(t, err)
   396  
   397  	assert.Equal(t, "6f08fa32a2fa21956bbb6d6df9e59cc2f901dbcfba06e00569ec7988eb95eeaa", privateKey.StringRaw())
   398  }
   399  func TestUnitECDSAPrivateKeyFromUnencryptedCompressedPEM(t *testing.T) {
   400  	unencryptedCompressed := "-----BEGIN EC PRIVATE KEY-----\n" +
   401  		"MFQCAQEEIOHyhclwHbha3f281Kvd884rhBzltxGJxCZyaQCagH9joAcGBSuBBAAK\n" +
   402  		"oSQDIgACREr6gFZa4K7hBP+bA25VdgQ+0ABFgM+g5RYw/W6T1Og=\n" +
   403  		"-----END EC PRIVATE KEY-----"
   404  
   405  	privateKey, err := PrivateKeyFromPem([]byte(unencryptedCompressed), "")
   406  	require.NoError(t, err)
   407  
   408  	assert.Equal(t, "e1f285c9701db85addfdbcd4abddf3ce2b841ce5b71189c4267269009a807f63", privateKey.StringRaw())
   409  }
   410  func TestUnitECDSAPrivateKeyFromEncryptedUncompressedPEM(t *testing.T) {
   411  	encryptedUncompressed := "-----BEGIN EC PRIVATE KEY-----\n" +
   412  		"Proc-Type: 4,ENCRYPTED\n" +
   413  		"DEK-Info: AES-128-CBC,0046A9EED8D16F0CAA66A197CE8BE8BD\n" +
   414  		"\n" +
   415  		"9VU9gReUmrn4XywjMx0F0A3oGzpHIksEXma72TCSdcxI7zHy0mtzuGq4Wd25O38s\n" +
   416  		"H9c6kvhTPS1N/c6iNhx154B0HUoND8jvAvfxbGR/R87vpZJsOoKCmRxGqrxG8HER\n" +
   417  		"FIHQ1jy16DrAbU95kDyLsiF1dy2vUY/HoqFZwxl/IVc=\n" +
   418  		"-----END EC PRIVATE KEY-----"
   419  
   420  	privateKey, err := PrivateKeyFromPem([]byte(encryptedUncompressed), "asdasd123")
   421  	require.NoError(t, err)
   422  
   423  	assert.Equal(t, "cf49eb5206c1b0468854d6ea7b370590619625514f71ff93608a18465e4012ad", privateKey.StringRaw())
   424  }
   425  func TestUnitECDSAPrivateKeyFromEncryptedCompressedPEM(t *testing.T) {
   426  	encryptedCompressed := "-----BEGIN EC PRIVATE KEY-----\n" +
   427  		"Proc-Type: 4,ENCRYPTED\n" +
   428  		"DEK-Info: AES-128-CBC,4A9B3B987EC2EFFA405818327D14FFF7\n" +
   429  		"\n" +
   430  		"Wh756RkK5fn1Ke2denR1OYfqE9Kr4BXhgrEMTU/6o0SNhMULUhWGHrCWvmNeEQwp\n" +
   431  		"ZVZYUxgYoTlJBeREzKAZithcvxIcTbQfLABo1NZbjA6YKqAqlGpM6owwL/f9e2ST\n" +
   432  		"-----END EC PRIVATE KEY-----"
   433  
   434  	privateKey, err := PrivateKeyFromPem([]byte(encryptedCompressed), "asdasd123")
   435  	require.NoError(t, err)
   436  
   437  	assert.Equal(t, "c0d3e16ba5a1abbeac4cd327a3c3c1cc10438431d0bac019054e573e67768bb5", privateKey.StringRaw())
   438  }
   439  
   440  func TestUnitPrivateKeyECDSASign(t *testing.T) {
   441  	t.Parallel()
   442  
   443  	key, err := PrivateKeyGenerateEcdsa()
   444  	require.NoError(t, err)
   445  
   446  	hash := crypto.Keccak256Hash([]byte("aaa"))
   447  	sig := key.Sign([]byte("aaa"))
   448  	s2 := crypto.VerifySignature(key.ecdsaPrivateKey._PublicKey()._BytesRaw(), hash.Bytes(), sig)
   449  	require.True(t, s2)
   450  }
   451  
   452  func DisabledTestUnitPrivateKeyECDSASign(t *testing.T) {
   453  	t.Parallel()
   454  
   455  	message := []byte("hello world")
   456  	key, err := PrivateKeyFromStringECDSA("8776c6b831a1b61ac10dac0304a2843de4716f54b1919bb91a2685d0fe3f3048")
   457  	require.NoError(t, err)
   458  
   459  	sig := key.Sign(message)
   460  
   461  	require.Equal(t, hex.EncodeToString(sig), "f3a13a555f1f8cd6532716b8f388bd4e9d8ed0b252743e923114c0c6cbfe414cf791c8e859afd3c12009ecf2cb20dacf01636d80823bcdbd9ec1ce59afe008f0")
   462  	require.True(t, key.PublicKey().Verify(message, sig))
   463  }
   464  
   465  func TestUnitPrivateKeyEd25519FromString(t *testing.T) {
   466  	t.Parallel()
   467  
   468  	key, err := PrivateKeyGenerateEd25519()
   469  	require.NoError(t, err)
   470  	key2, err := PrivateKeyFromString(key.String())
   471  	require.NoError(t, err)
   472  
   473  	require.Equal(t, key2.String(), key.String())
   474  }
   475  
   476  func TestUnitPrivateKeyEd25519FromStringRaw(t *testing.T) {
   477  	t.Parallel()
   478  
   479  	key, err := PrivateKeyGenerateEd25519()
   480  	require.NoError(t, err)
   481  	key2, err := PrivateKeyFromStringEd25519(key.StringRaw())
   482  	require.NoError(t, err)
   483  
   484  	require.Equal(t, key2.String(), key.String())
   485  }
   486  
   487  func TestUnitPrivateKeyEd25519FromStringDer(t *testing.T) {
   488  	t.Parallel()
   489  
   490  	key, err := PrivateKeyGenerateEd25519()
   491  	require.NoError(t, err)
   492  	key2, err := PrivateKeyFromStringEd25519(key.StringDer())
   493  	require.NoError(t, err)
   494  
   495  	require.Equal(t, key2.StringDer(), key.StringDer())
   496  }
   497  
   498  func TestUnitPublicKeyEd25519FromString(t *testing.T) {
   499  	t.Parallel()
   500  
   501  	key, err := PrivateKeyGenerateEd25519()
   502  	require.NoError(t, err)
   503  	publicKey := key.PublicKey()
   504  	publicKey2, err := PublicKeyFromStringEd25519(publicKey.String())
   505  	require.NoError(t, err)
   506  	require.Equal(t, publicKey2.String(), publicKey.String())
   507  }
   508  
   509  func TestUnitPublicKeyEd25519FromStringRaw(t *testing.T) {
   510  	t.Parallel()
   511  
   512  	key, err := PrivateKeyGenerateEd25519()
   513  	require.NoError(t, err)
   514  	publicKey := key.PublicKey()
   515  	publicKey2, err := PublicKeyFromStringEd25519(publicKey.StringRaw())
   516  	require.NoError(t, err)
   517  
   518  	require.Equal(t, publicKey2.String(), publicKey.String())
   519  }
   520  
   521  func TestUnitPublicKeyEd25519FromStringDer(t *testing.T) {
   522  	t.Parallel()
   523  
   524  	key, err := PrivateKeyGenerateEd25519()
   525  	require.NoError(t, err)
   526  	publicKey := key.PublicKey()
   527  	publicKey2, err := PublicKeyFromStringEd25519(publicKey.StringDer())
   528  	require.NoError(t, err)
   529  
   530  	require.Equal(t, publicKey2.StringDer(), publicKey.StringDer())
   531  }
   532  
   533  func TestUnitPrivateKeyECDSAFromString(t *testing.T) {
   534  	t.Parallel()
   535  
   536  	key, err := PrivateKeyGenerateEcdsa()
   537  	require.NoError(t, err)
   538  	key2, err := PrivateKeyFromString(key.String())
   539  	require.NoError(t, err)
   540  
   541  	require.Equal(t, key2.String(), key.String())
   542  }
   543  
   544  func TestUnitPrivateKeyECDSAFromStringWithPrefix(t *testing.T) {
   545  	t.Parallel()
   546  
   547  	_, err := PrivateKeyFromStringECDSA("0x7e18ba654522ff6ca5314d83354aaeca416abaef89ab27ffe01ecf5830ae8730")
   548  	require.NoError(t, err)
   549  }
   550  
   551  func TestUnitPrivateKeyECDSAFromStringRaw(t *testing.T) {
   552  	t.Parallel()
   553  
   554  	key, err := PrivateKeyGenerateEcdsa()
   555  	require.NoError(t, err)
   556  	key2, err := PrivateKeyFromStringECDSA(key.StringRaw())
   557  	require.NoError(t, err)
   558  
   559  	require.Equal(t, key2.String(), key.String())
   560  }
   561  
   562  func TestUnitPrivateKeyECDSAFromStringDer(t *testing.T) {
   563  	t.Parallel()
   564  
   565  	key, err := PrivateKeyGenerateEcdsa()
   566  	require.NoError(t, err)
   567  	key2, err := PrivateKeyFromStringECDSA(key.StringDer())
   568  	require.NoError(t, err)
   569  
   570  	require.Equal(t, key2.StringDer(), key.StringDer())
   571  }
   572  
   573  func TestUnitPrivateKeyECDSAFromStringLegacyDer(t *testing.T) {
   574  	key, err := PrivateKeyFromString("3030020100300706052b8104000a04220420c78aa6584228422d2edb027dec01ff374eb0720e32be2da827d51ed0711d4f8c")
   575  	require.NoError(t, err)
   576  	key2, err := PrivateKeyFromStringECDSA(key.StringDer())
   577  	require.NoError(t, err)
   578  
   579  	require.Equal(t, key2.StringDer(), key.StringDer())
   580  }
   581  
   582  func TestUnitPrivateKeyECDSAFromStringDerUncompressed(t *testing.T) {
   583  	key := "3074020101042052DDBE84838D865C18F043489C285C0B62041389569E8A62530" +
   584  		"56D0FFBEB7F5DA00706052B8104000AA144034200049D7DABF194F47CD5756BCC5231C821" +
   585  		"B34AFA03046EF7F645A2D026BF38D0332AF958B7840607EE4853DED1D372CF89FF131C788" +
   586  		"EE22644CF642503DF82EEF652"
   587  	expectedDerExport := "3054020101042052ddbe84838d865c18f043489c285c0b62041389569e8" +
   588  		"a6253056d0ffbeb7f5da00706052b8104000aa124032200029d7dabf194f47cd5756bcc5231c821b" +
   589  		"34afa03046ef7f645a2d026bf38d0332a"
   590  	key2, err := PrivateKeyFromStringECDSA(key)
   591  	require.NoError(t, err)
   592  
   593  	require.Equal(t, key2.StringDer(), expectedDerExport)
   594  }
   595  
   596  func TestUnitPublicKeyECDSAFromString(t *testing.T) {
   597  	t.Parallel()
   598  
   599  	key, err := PrivateKeyGenerateEcdsa()
   600  	require.NoError(t, err)
   601  	publicKey := key.PublicKey()
   602  	publicKey2, err := PublicKeyFromStringECDSA(publicKey.String())
   603  	require.NoError(t, err)
   604  	require.Equal(t, publicKey2.String(), publicKey.String())
   605  }
   606  
   607  func TestUnitPublicKeyECDSAFromStringRaw(t *testing.T) {
   608  	t.Parallel()
   609  
   610  	key, err := PrivateKeyGenerateEcdsa()
   611  	require.NoError(t, err)
   612  	publicKey := key.PublicKey()
   613  	publicKey2, err := PublicKeyFromStringECDSA(publicKey.StringRaw())
   614  	require.NoError(t, err)
   615  
   616  	require.Equal(t, publicKey2.String(), publicKey.String())
   617  }
   618  
   619  func TestUnitPublicKeyECDSAFromStringDer(t *testing.T) {
   620  	t.Parallel()
   621  
   622  	key, err := PrivateKeyGenerateEcdsa()
   623  	require.NoError(t, err)
   624  	publicKey := key.PublicKey()
   625  	publicKey2, err := PublicKeyFromStringECDSA(publicKey.StringDer())
   626  	require.NoError(t, err)
   627  
   628  	require.Equal(t, publicKey2.StringDer(), publicKey.StringDer())
   629  }
   630  
   631  func TestUnitPublicKeyECDSAFromStringLegacyDer(t *testing.T) {
   632  	publicKey, err := PublicKeyFromStringECDSA("302d300706052b8104000a0322000298c5d6efb814ead640467934b5ef9a02b81d3c483719675cb261cc5fde3edd57")
   633  	require.NoError(t, err)
   634  	publicKey2, err := PublicKeyFromStringECDSA(publicKey.StringDer())
   635  	require.NoError(t, err)
   636  
   637  	require.Equal(t, publicKey2.StringDer(), publicKey.StringDer())
   638  }
   639  
   640  func TestUnitPrivateKeyFromBytesDerECDSA(t *testing.T) {
   641  	t.Parallel()
   642  
   643  	key, err := PrivateKeyGenerateEcdsa()
   644  	require.NoError(t, err)
   645  	bytes := key.BytesDer()
   646  	key2, err := PrivateKeyFromBytesDer(bytes)
   647  	require.NoError(t, err)
   648  	require.True(t, strings.HasPrefix(key2.String(), _ECDSAPrivatePrefix))
   649  }
   650  
   651  func TestUnitPrivateKeyFromBytesDerECDSAUncompressed(t *testing.T) {
   652  	key := "3074020101042052DDBE84838D865C18F043489C285C0B62041389569E8A62530" +
   653  		"56D0FFBEB7F5DA00706052B8104000AA144034200049D7DABF194F47CD5756BCC5231C821" +
   654  		"B34AFA03046EF7F645A2D026BF38D0332AF958B7840607EE4853DED1D372CF89FF131C788" +
   655  		"EE22644CF642503DF82EEF652"
   656  	bytes, err := hex.DecodeString(key)
   657  	require.NoError(t, err)
   658  	key2, err := PrivateKeyFromBytesDer(bytes)
   659  	require.NoError(t, err)
   660  	fmt.Printf("key2.String(): %v\n", key2.String())
   661  	require.True(t, strings.HasPrefix(key2.String(), _ECDSAPrivatePrefix))
   662  }
   663  
   664  func TestUnitPrivateKeyFromBytesDerEd25519(t *testing.T) {
   665  	t.Parallel()
   666  
   667  	key, err := PrivateKeyGenerateEd25519()
   668  	require.NoError(t, err)
   669  	bytes := key.BytesDer()
   670  	key2, err := PrivateKeyFromBytesDer(bytes)
   671  	require.NoError(t, err)
   672  	require.True(t, strings.HasPrefix(key2.String(), _Ed25519PrivateKeyPrefix))
   673  }
   674  
   675  func TestUnitPublicKeyFromBytesDerECDSA(t *testing.T) {
   676  	t.Parallel()
   677  
   678  	key, err := PrivateKeyGenerateEcdsa()
   679  	require.NoError(t, err)
   680  	pkey := key.PublicKey()
   681  	bytes := pkey.BytesDer()
   682  	pkey2, err := PublicKeyFromBytesDer(bytes)
   683  	require.NoError(t, err)
   684  	require.True(t, strings.HasPrefix(pkey2.String(), _ECDSAPubKeyPrefix))
   685  }
   686  
   687  func TestUnitPublicKeyFromBytesDerEd25519(t *testing.T) {
   688  	t.Parallel()
   689  
   690  	key, err := PrivateKeyGenerateEd25519()
   691  	require.NoError(t, err)
   692  	pkey := key.PublicKey()
   693  	bytes := pkey.BytesDer()
   694  	pkey2, err := PublicKeyFromBytesDer(bytes)
   695  	require.NoError(t, err)
   696  	require.True(t, strings.HasPrefix(pkey2.String(), _Ed25519PubKeyPrefix))
   697  }
   698  
   699  func TestUnitPrivateKeyFromStringDerEd25519(t *testing.T) {
   700  	t.Parallel()
   701  
   702  	key, err := PrivateKeyGenerateEd25519()
   703  	require.NoError(t, err)
   704  	key2, err := PrivateKeyFromStringDer(key.StringDer())
   705  	require.NoError(t, err)
   706  	require.Equal(t, key2.String(), key.String())
   707  }
   708  
   709  func TestUnitPrivateKeyFromStringDerECDSA(t *testing.T) {
   710  	t.Parallel()
   711  
   712  	key, err := PrivateKeyGenerateEcdsa()
   713  	require.NoError(t, err)
   714  	key2, err := PrivateKeyFromStringDer(key.StringDer())
   715  	require.NoError(t, err)
   716  	require.Equal(t, key2.String(), key.String())
   717  }
   718  
   719  func TestUnitPrivateKeyECDSAFromBytes(t *testing.T) {
   720  	t.Parallel()
   721  
   722  	key, err := PrivateKeyGenerateEcdsa()
   723  	require.NoError(t, err)
   724  	key2, err := PrivateKeyFromBytes(key.Bytes())
   725  	require.NoError(t, err)
   726  	require.Equal(t, key2.String(), key.String())
   727  }
   728  
   729  func TestUnitPrivateKeyEd25519FromBytes(t *testing.T) {
   730  	t.Parallel()
   731  
   732  	key, err := PrivateKeyGenerateEd25519()
   733  	require.NoError(t, err)
   734  	key2, err := PrivateKeyFromBytes(key.Bytes())
   735  	require.NoError(t, err)
   736  	require.Equal(t, key2.String(), key.String())
   737  }
   738  
   739  func TestUnitPrivateKeyFromBytesECDSA(t *testing.T) {
   740  	t.Parallel()
   741  
   742  	key, err := PrivateKeyGenerateEcdsa()
   743  	require.NoError(t, err)
   744  	key2, err := PrivateKeyFromBytesECDSA(key.Bytes())
   745  	require.NoError(t, err)
   746  	require.Equal(t, key2.String(), key.String())
   747  }
   748  
   749  func TestUnitPrivateKeyFromBytesEd25519(t *testing.T) {
   750  	t.Parallel()
   751  
   752  	key, err := PrivateKeyGenerateEd25519()
   753  	require.NoError(t, err)
   754  	key2, err := PrivateKeyFromBytesEd25519(key.Bytes())
   755  	require.NoError(t, err)
   756  	require.Equal(t, key2.String(), key.String())
   757  }
   758  
   759  func TestUnitPublicKeyFromBytesECDSA(t *testing.T) {
   760  	t.Parallel()
   761  
   762  	key, err := PrivateKeyGenerateEcdsa()
   763  	require.NoError(t, err)
   764  	key2, err := PublicKeyFromBytesECDSA(key.PublicKey().Bytes())
   765  	require.NoError(t, err)
   766  	require.Equal(t, key.PublicKey().String(), key2.String())
   767  }
   768  
   769  func TestUnitPublicKeyFromBytesEd25519(t *testing.T) {
   770  	t.Parallel()
   771  
   772  	key, err := PrivateKeyGenerateEd25519()
   773  	require.NoError(t, err)
   774  	key2, err := PublicKeyFromBytesEd25519(key.PublicKey().Bytes())
   775  	require.NoError(t, err)
   776  	require.Equal(t, key.PublicKey().String(), key2.String())
   777  }
   778  
   779  func TestUnitPublicKeyECDSAFromBytes(t *testing.T) {
   780  	t.Parallel()
   781  
   782  	key, err := PrivateKeyGenerateEcdsa()
   783  	require.NoError(t, err)
   784  	key2, err := PublicKeyFromBytes(key.PublicKey().Bytes())
   785  	require.NoError(t, err)
   786  	require.Equal(t, key.PublicKey().String(), key2.String())
   787  }
   788  
   789  func TestUnitPublicKeyEd25519FromBytes(t *testing.T) {
   790  	t.Parallel()
   791  
   792  	key, err := PrivateKeyGenerateEd25519()
   793  	require.NoError(t, err)
   794  	key2, err := PublicKeyFromBytes(key.PublicKey().Bytes())
   795  	require.NoError(t, err)
   796  	require.Equal(t, key.PublicKey().String(), key2.String())
   797  }
   798  
   799  func TestUnitPrivateKeyBytesRawEd25519(t *testing.T) {
   800  	t.Parallel()
   801  
   802  	key, err := PrivateKeyGenerateEd25519()
   803  	require.NoError(t, err)
   804  	require.Equal(t, key.ed25519PrivateKey.keyData[0:32], key.BytesRaw())
   805  }
   806  
   807  func TestUnitPrivateKeyBytesRawECDSA(t *testing.T) {
   808  	t.Parallel()
   809  
   810  	key, err := PrivateKeyGenerateEcdsa()
   811  	require.NoError(t, err)
   812  	require.Equal(t, key.ecdsaPrivateKey.keyData.D.Bytes(), key.BytesRaw())
   813  }
   814  
   815  func TestUnitPublicKeyBytesRawEd25519(t *testing.T) {
   816  	t.Parallel()
   817  
   818  	key, err := PrivateKeyGenerateEd25519()
   819  	require.NoError(t, err)
   820  	require.Equal(t, key.PublicKey().ed25519PublicKey.keyData, key.PublicKey().BytesRaw())
   821  }
   822  
   823  func TestUnitPublicKeyBytesRawECDSA(t *testing.T) {
   824  	t.Parallel()
   825  
   826  	key, err := PrivateKeyGenerateEcdsa()
   827  	require.NoError(t, err)
   828  	require.Equal(t, crypto.CompressPubkey(&key.ecdsaPrivateKey.keyData.PublicKey), key.PublicKey().BytesRaw())
   829  }
   830  
   831  func TestUnitECDSAPrivateKeyFromBytesInvalidLength(t *testing.T) {
   832  	invalidPrivateKey := make([]byte, 31)
   833  	_, err := _ECDSAPrivateKeyFromBytes(invalidPrivateKey)
   834  	require.Error(t, err)
   835  	expectedError := fmt.Sprintf("invalid private key length: %v bytes", len(invalidPrivateKey))
   836  	if err.Error() != expectedError {
   837  		t.Errorf("expected error message %q, but got %q", expectedError, err.Error())
   838  	}
   839  }
   840  
   841  func TestUnitECDSAPrivateKeyFromBytesRawInvalidLength(t *testing.T) {
   842  	invalidPrivateKey := make([]byte, 31)
   843  	_, err := _ECDSAPrivateKeyFromBytesRaw(invalidPrivateKey)
   844  	require.Error(t, err)
   845  	expectedError := fmt.Sprintf("invalid private key length: %v bytes", len(invalidPrivateKey))
   846  	if err.Error() != expectedError {
   847  		t.Errorf("expected error message %q, but got %q", expectedError, err.Error())
   848  	}
   849  }
   850  
   851  func TestUnitECDSAPrivateKeyFromBytesRawInvalidKey(t *testing.T) {
   852  	invalidPrivateKey := make([]byte, 32)
   853  	_, err := _ECDSAPrivateKeyFromBytesRaw(invalidPrivateKey)
   854  	require.Error(t, err)
   855  	expectedError := "invalid private key, zero or negative"
   856  	if err.Error() != expectedError {
   857  		t.Errorf("expected error message %q, but got %q", expectedError, err.Error())
   858  	}
   859  }
   860  
   861  func TestUnit_ECDSAPrivateKeyFromBytesDerInvalidKey(t *testing.T) {
   862  	invalidPrivateKey := make([]byte, 32)
   863  	_, err := _ECDSAPrivateKeyFromBytesDer(invalidPrivateKey)
   864  	require.Error(t, err)
   865  }
   866  
   867  func Test_EcdsaPrivateKeyFromBytesDer_InvalidData(t *testing.T) {
   868  	key, err := PrivateKeyGenerateEcdsa()
   869  	require.NoError(t, err)
   870  	der := key.BytesDer()
   871  	trailingBytes := []byte{0x01, 0x02, 0x03}
   872  	derWithTrailingBytes := append(der, trailingBytes...)
   873  	_, err = _ECDSAPrivateKeyFromBytesDer(derWithTrailingBytes)
   874  	require.Error(t, err)
   875  	expectedError := "x509: trailing data after ASN.1 of public-key"
   876  	if err == nil || err.Error() != expectedError {
   877  		t.Fatalf("Expected error %q, got %q", expectedError, err)
   878  	}
   879  }
   880  
   881  func TestUnitECDSAPublicKeyFromBytesInvalidLength(t *testing.T) {
   882  	invalidPrivateKey := make([]byte, 31)
   883  	_, err := _ECDSAPublicKeyFromBytes(invalidPrivateKey)
   884  	require.Error(t, err)
   885  	expectedError := fmt.Sprintf("invalid compressed ECDSA public key length: %v bytes", len(invalidPrivateKey))
   886  	if err.Error() != expectedError {
   887  		t.Errorf("expected error message %q, but got %q", expectedError, err.Error())
   888  	}
   889  }
   890  
   891  func TestUnitECDSAPublicKeyFromBytesRawInvalidLength(t *testing.T) {
   892  	_, err := _ECDSAPublicKeyFromBytesRaw(nil)
   893  	require.Error(t, err)
   894  	invalidPrivateKey := make([]byte, 31)
   895  	_, err = _ECDSAPublicKeyFromBytesRaw(invalidPrivateKey)
   896  	require.Error(t, err)
   897  	expectedError := fmt.Sprintf("invalid public key length: %v bytes", len(invalidPrivateKey))
   898  	if err.Error() != expectedError {
   899  		t.Errorf("expected error message %q, but got %q", expectedError, err.Error())
   900  	}
   901  }
   902  
   903  func TestUnitECDSAPublicKeyFromBytesRawInvalidKey(t *testing.T) {
   904  	invalidPrivateKey := make([]byte, 33)
   905  	_, err := _ECDSAPublicKeyFromBytesRaw(invalidPrivateKey)
   906  	require.Error(t, err)
   907  	expectedError := "invalid public key"
   908  	if err.Error() != expectedError {
   909  		t.Errorf("expected error message %q, but got %q", expectedError, err.Error())
   910  	}
   911  }
   912  
   913  func TestUnitECDSAPublicKeyFromBytesDerInvalidKey(t *testing.T) {
   914  	_, err := _ECDSAPublicKeyFromBytesDer(nil)
   915  	require.Error(t, err)
   916  	invalidPrivateKey := make([]byte, 33)
   917  	_, err = _ECDSAPublicKeyFromBytesDer(invalidPrivateKey)
   918  	require.Error(t, err)
   919  }
   920  
   921  func TestUnitECDSAPublicKeyFromBytesDerWrongAlgorithmOID(t *testing.T) {
   922  	hexStr := "3036301006072a8648ce3d020206052b8104000a032200021ef6d7f710abb6f137f1c0dd87c6fd0200486c0754273d628cde6688e0bc9bf5"
   923  	bytes, err := hex.DecodeString(hexStr)
   924  	require.NoError(t, err)
   925  	_, err = _ECDSAPublicKeyFromBytesDer(bytes)
   926  	expectedError := "public key is not an ECDSA public key"
   927  	if err.Error() != expectedError {
   928  		t.Errorf("expected error message %q, but got %q", expectedError, err.Error())
   929  	}
   930  }
   931  
   932  func TestUnitECDSAPublicKeyFromBytesDerWrongCurve(t *testing.T) {
   933  	hexStr := "3036301006072a8648ce3d020106052b8104000b03220002f3c22171a370bd5eba014c4ffa2002c4de4e0d8545ab964cda10467f23cfa2e0"
   934  	bytes, err := hex.DecodeString(hexStr)
   935  	require.NoError(t, err)
   936  	_, err = _ECDSAPublicKeyFromBytesDer(bytes)
   937  	expectedError := "public key is not a secp256k1 public key"
   938  	if err.Error() != expectedError {
   939  		t.Errorf("expected error message %q, but got %q", expectedError, err.Error())
   940  	}
   941  }
   942  
   943  func TestUnitEd25519PrivateKeyFromBytesDerWrongLength(t *testing.T) {
   944  	invalidPrivateKey := make([]byte, 33)
   945  	_, err := _Ed25519PrivateKeyFromBytesDer(invalidPrivateKey)
   946  	require.Error(t, err)
   947  	expectedError := fmt.Sprintf("invalid private key length: %v byt", len(invalidPrivateKey))
   948  	if err.Error() != expectedError {
   949  		t.Errorf("expected error message %q, but got %q", expectedError, err.Error())
   950  	}
   951  }
   952  
   953  func TestUnitEd25519PrivateKeyFromBytesDerWrongKey(t *testing.T) {
   954  	invalidPrivateKey := make([]byte, 32)
   955  	_, err := _Ed25519PublicKeyFromBytesDer(invalidPrivateKey)
   956  	require.Error(t, err)
   957  }
   958  
   959  func TestUnitEd25519PublicKeyFromBytesDerWrongAlgorithmOID(t *testing.T) {
   960  	hexStr := "3036301006072a8648ce3d020206052b8104000a032200021ef6d7f710abb6f137f1c0dd87c6fd0200486c0754273d628cde6688e0bc9bf5"
   961  	bytes, err := hex.DecodeString(hexStr)
   962  	require.NoError(t, err)
   963  	_, err = _Ed25519PublicKeyFromBytesDer(bytes)
   964  	expectedError := "invalid algorithm identifier, expected Ed25519"
   965  	if err.Error() != expectedError {
   966  		t.Errorf("expected error message %q, but got %q", expectedError, err.Error())
   967  	}
   968  }
   969  
   970  func TestUnitEd25519PublicKeyFromBytesDerWrongCurve(t *testing.T) {
   971  	bytes, _ := hex.DecodeString("3053300506032b6570032100b6ecf351b0d0d8fce400647f4944621b411e97ae83a249fcadb33cf165f40ca2")
   972  	modifiedBytes := append(bytes[:14], bytes[15:]...)
   973  	_Ed25519PublicKeyFromBytesDer(modifiedBytes)
   974  }
   975  
   976  func TestUnitEd25519PublicKeyFromStringWrongLength(t *testing.T) {
   977  	_, err := _Ed25519PublicKeyFromString("303")
   978  	require.Error(t, err)
   979  	expectedError := "encoding/hex: odd length hex string"
   980  	if err.Error() != expectedError {
   981  		t.Errorf("expected error message %q, but got %q", expectedError, err.Error())
   982  	}
   983  }
   984  
   985  func TestUnitEd25519PublicKeyFromBytesRawWrongLength(t *testing.T) {
   986  	_, err := _Ed25519PublicKeyFromBytesRaw(nil)
   987  	require.Error(t, err)
   988  	expectedError := "byte array can't be null"
   989  	if err.Error() != expectedError {
   990  		t.Errorf("expected error message %q, but got %q", expectedError, err.Error())
   991  	}
   992  	invalidPrivateKey := make([]byte, 33)
   993  	_, err = _Ed25519PublicKeyFromBytesRaw(invalidPrivateKey)
   994  	require.Error(t, err)
   995  	expectedError = fmt.Sprintf("invalid public key length: %v bytes", len(invalidPrivateKey))
   996  	if err.Error() != expectedError {
   997  		t.Errorf("expected error message %q, but got %q", expectedError, err.Error())
   998  	}
   999  }
  1000  
  1001  func TestUnitPrivateKeyECDSASignTransaction(t *testing.T) {
  1002  	t.Parallel()
  1003  
  1004  	newKey, err := PrivateKeyGenerateEcdsa()
  1005  	require.NoError(t, err)
  1006  
  1007  	newBalance := NewHbar(2)
  1008  
  1009  	txID := TransactionIDGenerate(AccountID{Account: 123})
  1010  
  1011  	tx, err := NewAccountCreateTransaction().
  1012  		SetKey(newKey).
  1013  		SetNodeAccountIDs([]AccountID{{Account: 3}}).
  1014  		SetTransactionID(txID).
  1015  		SetInitialBalance(newBalance).
  1016  		SetMaxAutomaticTokenAssociations(100).
  1017  		Freeze()
  1018  	require.NoError(t, err)
  1019  
  1020  	_, err = newKey.SignTransaction(&tx.Transaction)
  1021  	require.NoError(t, err)
  1022  }
  1023  
  1024  func TestUnitPublicKeyFromPrivateKeyString(t *testing.T) {
  1025  	t.Parallel()
  1026  
  1027  	key, err := PrivateKeyFromStringECDSA("30540201010420ac318ea8ff8d991ab2f16172b4738e74dc35a56681199cfb1c0cb2e7cb560ffda00706052b8104000aa124032200036843f5cb338bbb4cdb21b0da4ea739d910951d6e8a5f703d313efe31afe788f4")
  1028  	require.NoError(t, err)
  1029  	require.Equal(t, "3036301006072a8648ce3d020106052b8104000a032200036843f5cb338bbb4cdb21b0da4ea739d910951d6e8a5f703d313efe31afe788f4", key.PublicKey().String())
  1030  }
  1031  
  1032  func TestUnitPublicKeyToEthereumAddress(t *testing.T) {
  1033  	t.Parallel()
  1034  
  1035  	byt, err := hex.DecodeString("03af80b90d25145da28c583359beb47b21796b2fe1a23c1511e443e7a64dfdb27d")
  1036  	require.NoError(t, err)
  1037  	key, err := PublicKeyFromBytesECDSA(byt)
  1038  	ethereumAddress := key.ToEthereumAddress()
  1039  	require.Equal(t, ethereumAddress, "627306090abab3a6e1400e9345bc60c78a8bef57")
  1040  }
  1041  
  1042  func TestSlip10Ed25519Vector1(t *testing.T) {
  1043  	t.Parallel()
  1044  
  1045  	// https://github.com/satoshilabs/slips/blob/master/slip-0010.md#test-vector-1-for-ed25519
  1046  	test1PrivateKey := "2b4be7f19ee27bbf30c667b642d5f4aa69fd169872f8fc3059c08ebae2eb19e7"
  1047  	test1PublicKey := "a4b2856bfec510abab89753fac1ac0e1112364e7d250545963f135f2a33188ed"
  1048  	test1ChainCode := "90046a93de5380a72b5e45010748567d5ea02bbf6522f979e05c0d8d8ca9fffb"
  1049  	test2PrivateKey := "68e0fe46dfb67e368c75379acec591dad19df3cde26e63b93a8e704f1dade7a3"
  1050  	test2PublicKey := "8c8a13df77a28f3445213a0f432fde644acaa215fc72dcdf300d5efaa85d350c"
  1051  	test2ChainCode := "8b59aa11380b624e81507a27fedda59fea6d0b779a778918a2fd3590e16e9c69"
  1052  	test3PrivateKey := "b1d0bad404bf35da785a64ca1ac54b2617211d2777696fbffaf208f746ae84f2"
  1053  	test3PublicKey := "1932a5270f335bed617d5b935c80aedb1a35bd9fc1e31acafd5372c30f5c1187"
  1054  	test3ChainCode := "a320425f77d1b5c2505a6b1b27382b37368ee640e3557c315416801243552f14"
  1055  	test4PrivateKey := "92a5b23c0b8a99e37d07df3fb9966917f5d06e02ddbd909c7e184371463e9fc9"
  1056  	test4PublicKey := "ae98736566d30ed0e9d2f4486a64bc95740d89c7db33f52121f8ea8f76ff0fc1"
  1057  	test4ChainCode := "2e69929e00b5ab250f49c3fb1c12f252de4fed2c1db88387094a0f8c4c9ccd6c"
  1058  	test5PrivateKey := "30d1dc7e5fc04c31219ab25a27ae00b50f6fd66622f6e9c913253d6511d1e662"
  1059  	test5PublicKey := "8abae2d66361c879b900d204ad2cc4984fa2aa344dd7ddc46007329ac76c429c"
  1060  	test5ChainCode := "8f6d87f93d750e0efccda017d662a1b31a266e4a6f5993b15f5c1f07f74dd5cc"
  1061  	test6PrivateKey := "8f94d394a8e8fd6b1bc2f3f49f5c47e385281d5c17e65324b0f62483e37e8793"
  1062  	test6PublicKey := "3c24da049451555d51a7014a37337aa4e12d41e485abccfa46b47dfb2af54b7a"
  1063  	test6ChainCode := "68789923a0cac2cd5a29172a475fe9e0fb14cd6adb5ad98a3fa70333e7afa230"
  1064  
  1065  	seed, err := hex.DecodeString("000102030405060708090a0b0c0d0e0f")
  1066  	assert.NoError(t, err)
  1067  
  1068  	//Chain m
  1069  	key1, err := PrivateKeyFromSeedEd25519(seed)
  1070  	assert.NoError(t, err)
  1071  
  1072  	assert.Equal(t, key1.StringRaw(), test1PrivateKey)
  1073  	assert.Equal(t, key1.PublicKey().StringRaw(), test1PublicKey)
  1074  	assert.Equal(t, hex.EncodeToString(key1.ed25519PrivateKey.chainCode), test1ChainCode)
  1075  
  1076  	// Chain m/0'
  1077  	key2, err := key1.Derive(0)
  1078  	assert.NoError(t, err)
  1079  
  1080  	assert.Equal(t, key2.StringRaw(), test2PrivateKey)
  1081  	assert.Equal(t, key2.PublicKey().StringRaw(), test2PublicKey)
  1082  	assert.Equal(t, hex.EncodeToString(key2.ed25519PrivateKey.chainCode), test2ChainCode)
  1083  
  1084  	// Chain m/0'/1'
  1085  	key3, err := key2.Derive(1)
  1086  	assert.NoError(t, err)
  1087  	assert.Equal(t, key3.StringRaw(), test3PrivateKey)
  1088  	assert.Equal(t, key3.PublicKey().StringRaw(), test3PublicKey)
  1089  	assert.Equal(t, hex.EncodeToString(key3.ed25519PrivateKey.chainCode), test3ChainCode)
  1090  
  1091  	// Chain m/0'/1'/2'
  1092  	key4, err := key3.Derive(2)
  1093  	assert.NoError(t, err)
  1094  	assert.Equal(t, key4.StringRaw(), test4PrivateKey)
  1095  	assert.Equal(t, key4.PublicKey().StringRaw(), test4PublicKey)
  1096  	assert.Equal(t, hex.EncodeToString(key4.ed25519PrivateKey.chainCode), test4ChainCode)
  1097  
  1098  	//Chain m/0'/1'/2'/2'
  1099  	key5, err := key4.Derive(2)
  1100  	assert.NoError(t, err)
  1101  	assert.Equal(t, key5.StringRaw(), test5PrivateKey)
  1102  	assert.Equal(t, key5.PublicKey().StringRaw(), test5PublicKey)
  1103  	assert.Equal(t, hex.EncodeToString(key5.ed25519PrivateKey.chainCode), test5ChainCode)
  1104  
  1105  	// Chain m/0'/1'/2'/2'/1000000000'
  1106  	key6, err := key5.Derive(1000000000)
  1107  	assert.NoError(t, err)
  1108  	assert.Equal(t, key6.StringRaw(), test6PrivateKey)
  1109  	assert.Equal(t, key6.PublicKey().StringRaw(), test6PublicKey)
  1110  	assert.Equal(t, hex.EncodeToString(key6.ed25519PrivateKey.chainCode), test6ChainCode)
  1111  }
  1112  
  1113  func TestSlip10Ed25519Vector2(t *testing.T) {
  1114  	t.Parallel()
  1115  
  1116  	// https://github.com/satoshilabs/slips/blob/master/slip-0010.md#test-vector-2-for-ed25519
  1117  	test1PrivateKey := "171cb88b1b3c1db25add599712e36245d75bc65a1a5c9e18d76f9f2b1eab4012"
  1118  	test1PublicKey := "8fe9693f8fa62a4305a140b9764c5ee01e455963744fe18204b4fb948249308a"
  1119  	test1ChainCode := "ef70a74db9c3a5af931b5fe73ed8e1a53464133654fd55e7a66f8570b8e33c3b"
  1120  	test2PrivateKey := "1559eb2bbec5790b0c65d8693e4d0875b1747f4970ae8b650486ed7470845635"
  1121  	test2PublicKey := "86fab68dcb57aa196c77c5f264f215a112c22a912c10d123b0d03c3c28ef1037"
  1122  	test2ChainCode := "0b78a3226f915c082bf118f83618a618ab6dec793752624cbeb622acb562862d"
  1123  	test3PrivateKey := "ea4f5bfe8694d8bb74b7b59404632fd5968b774ed545e810de9c32a4fb4192f4"
  1124  	test3PublicKey := "5ba3b9ac6e90e83effcd25ac4e58a1365a9e35a3d3ae5eb07b9e4d90bcf7506d"
  1125  	test3ChainCode := "138f0b2551bcafeca6ff2aa88ba8ed0ed8de070841f0c4ef0165df8181eaad7f"
  1126  	test4PrivateKey := "3757c7577170179c7868353ada796c839135b3d30554bbb74a4b1e4a5a58505c"
  1127  	test4PublicKey := "2e66aa57069c86cc18249aecf5cb5a9cebbfd6fadeab056254763874a9352b45"
  1128  	test4ChainCode := "73bd9fff1cfbde33a1b846c27085f711c0fe2d66fd32e139d3ebc28e5a4a6b90"
  1129  	test5PrivateKey := "5837736c89570de861ebc173b1086da4f505d4adb387c6a1b1342d5e4ac9ec72"
  1130  	test5PublicKey := "e33c0f7d81d843c572275f287498e8d408654fdf0d1e065b84e2e6f157aab09b"
  1131  	test5ChainCode := "0902fe8a29f9140480a00ef244bd183e8a13288e4412d8389d140aac1794825a"
  1132  	test6PrivateKey := "551d333177df541ad876a60ea71f00447931c0a9da16f227c11ea080d7391b8d"
  1133  	test6PublicKey := "47150c75db263559a70d5778bf36abbab30fb061ad69f69ece61a72b0cfa4fc0"
  1134  	test6ChainCode := "5d70af781f3a37b829f0d060924d5e960bdc02e85423494afc0b1a41bbe196d4"
  1135  
  1136  	seed, err := hex.DecodeString("fffcf9f6f3f0edeae7e4e1dedbd8d5d2cfccc9c6c3c0bdbab7b4b1aeaba8a5a29f9c999693908d8a8784817e7b7875726f6c696663605d5a5754514e4b484542")
  1137  	assert.NoError(t, err)
  1138  
  1139  	// Chain m
  1140  	key1, err := PrivateKeyFromSeedEd25519(seed)
  1141  	assert.NoError(t, err)
  1142  
  1143  	assert.Equal(t, key1.StringRaw(), test1PrivateKey)
  1144  	assert.Equal(t, key1.PublicKey().StringRaw(), test1PublicKey)
  1145  	assert.Equal(t, hex.EncodeToString(key1.ed25519PrivateKey.chainCode), test1ChainCode)
  1146  
  1147  	// Chain m/0'
  1148  	key2, err := key1.Derive(0)
  1149  	assert.NoError(t, err)
  1150  
  1151  	assert.Equal(t, key2.StringRaw(), test2PrivateKey)
  1152  	assert.Equal(t, key2.PublicKey().StringRaw(), test2PublicKey)
  1153  	assert.Equal(t, hex.EncodeToString(key2.ed25519PrivateKey.chainCode), test2ChainCode)
  1154  
  1155  	// Chain m/0'/2147483647'
  1156  	key3, err := key2.Derive(2147483647)
  1157  	assert.NoError(t, err)
  1158  	assert.Equal(t, key3.StringRaw(), test3PrivateKey)
  1159  	assert.Equal(t, key3.PublicKey().StringRaw(), test3PublicKey)
  1160  	assert.Equal(t, hex.EncodeToString(key3.ed25519PrivateKey.chainCode), test3ChainCode)
  1161  
  1162  	// Chain m/0'/2147483647'/1'
  1163  	key4, err := key3.Derive(1)
  1164  	assert.NoError(t, err)
  1165  	assert.Equal(t, key4.StringRaw(), test4PrivateKey)
  1166  	assert.Equal(t, key4.PublicKey().StringRaw(), test4PublicKey)
  1167  	assert.Equal(t, hex.EncodeToString(key4.ed25519PrivateKey.chainCode), test4ChainCode)
  1168  
  1169  	// Chain m/0'/2147483647'/1'/2147483646'
  1170  	key5, err := key4.Derive(2147483646)
  1171  	assert.NoError(t, err)
  1172  	assert.Equal(t, key5.StringRaw(), test5PrivateKey)
  1173  	assert.Equal(t, key5.PublicKey().StringRaw(), test5PublicKey)
  1174  	assert.Equal(t, hex.EncodeToString(key5.ed25519PrivateKey.chainCode), test5ChainCode)
  1175  
  1176  	// Chain m/0'/2147483647'/1'/2147483646'/2'
  1177  	key6, err := key5.Derive(2)
  1178  	assert.NoError(t, err)
  1179  	assert.Equal(t, key6.StringRaw(), test6PrivateKey)
  1180  	assert.Equal(t, key6.PublicKey().StringRaw(), test6PublicKey)
  1181  	assert.Equal(t, hex.EncodeToString(key6.ed25519PrivateKey.chainCode), test6ChainCode)
  1182  }
  1183  
  1184  func TestSlip10ECDSAVector1(t *testing.T) {
  1185  	t.Parallel()
  1186  
  1187  	// https://github.com/satoshilabs/slips/blob/master/slip-0010.md#test-vector-1-for-secp256k1
  1188  	test1PrivateKey := "e8f32e723decf4051aefac8e2c93c9c5b214313817cdb01a1494b917c8436b35"
  1189  	test1PublicKey := "0339a36013301597daef41fbe593a02cc513d0b55527ec2df1050e2e8ff49c85c2"
  1190  	test1ChainCode := "873dff81c02f525623fd1fe5167eac3a55a049de3d314bb42ee227ffed37d508"
  1191  	test2PrivateKey := "edb2e14f9ee77d26dd93b4ecede8d16ed408ce149b6cd80b0715a2d911a0afea"
  1192  	test2PublicKey := "035a784662a4a20a65bf6aab9ae98a6c068a81c52e4b032c0fb5400c706cfccc56"
  1193  	test2ChainCode := "47fdacbd0f1097043b78c63c20c34ef4ed9a111d980047ad16282c7ae6236141"
  1194  	test3PrivateKey := "3c6cb8d0f6a264c91ea8b5030fadaa8e538b020f0a387421a12de9319dc93368"
  1195  	test3PublicKey := "03501e454bf00751f24b1b489aa925215d66af2234e3891c3b21a52bedb3cd711c"
  1196  	test3ChainCode := "2a7857631386ba23dacac34180dd1983734e444fdbf774041578e9b6adb37c19"
  1197  	test4PrivateKey := "cbce0d719ecf7431d88e6a89fa1483e02e35092af60c042b1df2ff59fa424dca"
  1198  	test4PublicKey := "0357bfe1e341d01c69fe5654309956cbea516822fba8a601743a012a7896ee8dc2"
  1199  	test4ChainCode := "04466b9cc8e161e966409ca52986c584f07e9dc81f735db683c3ff6ec7b1503f"
  1200  	test5PrivateKey := "0f479245fb19a38a1954c5c7c0ebab2f9bdfd96a17563ef28a6a4b1a2a764ef4"
  1201  	test5PublicKey := "02e8445082a72f29b75ca48748a914df60622a609cacfce8ed0e35804560741d29"
  1202  	test5ChainCode := "cfb71883f01676f587d023cc53a35bc7f88f724b1f8c2892ac1275ac822a3edd"
  1203  	test6PrivateKey := "471b76e389e528d6de6d816857e012c5455051cad6660850e58372a6c3e6e7c8"
  1204  	test6PublicKey := "022a471424da5e657499d1ff51cb43c47481a03b1e77f951fe64cec9f5a48f7011"
  1205  	test6ChainCode := "c783e67b921d2beb8f6b389cc646d7263b4145701dadd2161548a8b078e65e9e"
  1206  
  1207  	seed, err := hex.DecodeString("000102030405060708090a0b0c0d0e0f")
  1208  	assert.NoError(t, err)
  1209  
  1210  	// Chain m
  1211  	key1, err := PrivateKeyFromSeedECDSAsecp256k1(seed)
  1212  	assert.NoError(t, err)
  1213  
  1214  	assert.Equal(t, key1.StringRaw(), test1PrivateKey)
  1215  	assert.Equal(t, key1.PublicKey().StringRaw(), test1PublicKey)
  1216  	assert.Equal(t, hex.EncodeToString(key1.ecdsaPrivateKey.chainCode), test1ChainCode)
  1217  
  1218  	// Chain m/0'
  1219  	key2, err := key1.Derive(ToHardenedIndex(0))
  1220  	assert.NoError(t, err)
  1221  
  1222  	assert.Equal(t, key2.StringRaw(), test2PrivateKey)
  1223  	assert.Equal(t, key2.PublicKey().StringRaw(), test2PublicKey)
  1224  	assert.Equal(t, hex.EncodeToString(key2.ecdsaPrivateKey.chainCode), test2ChainCode)
  1225  
  1226  	// Chain m/0'/1
  1227  	key3, err := key2.Derive(1)
  1228  	assert.NoError(t, err)
  1229  	assert.Equal(t, key3.StringRaw(), test3PrivateKey)
  1230  	assert.Equal(t, key3.PublicKey().StringRaw(), test3PublicKey)
  1231  	assert.Equal(t, hex.EncodeToString(key3.ecdsaPrivateKey.chainCode), test3ChainCode)
  1232  
  1233  	// Chain m/0'/1/2'
  1234  	key4, err := key3.Derive(ToHardenedIndex(2))
  1235  	assert.NoError(t, err)
  1236  	assert.Equal(t, key4.StringRaw(), test4PrivateKey)
  1237  	assert.Equal(t, key4.PublicKey().StringRaw(), test4PublicKey)
  1238  	assert.Equal(t, hex.EncodeToString(key4.ecdsaPrivateKey.chainCode), test4ChainCode)
  1239  
  1240  	// Chain m/0'/1/2'/2
  1241  	key5, err := key4.Derive(2)
  1242  	assert.NoError(t, err)
  1243  	assert.Equal(t, key5.StringRaw(), test5PrivateKey)
  1244  	assert.Equal(t, key5.PublicKey().StringRaw(), test5PublicKey)
  1245  	assert.Equal(t, hex.EncodeToString(key5.ecdsaPrivateKey.chainCode), test5ChainCode)
  1246  
  1247  	// Chain m/0'/1/2'/2/1000000000
  1248  	key6, err := key5.Derive(1000000000)
  1249  	assert.NoError(t, err)
  1250  	assert.Equal(t, key6.StringRaw(), test6PrivateKey)
  1251  	assert.Equal(t, key6.PublicKey().StringRaw(), test6PublicKey)
  1252  	assert.Equal(t, hex.EncodeToString(key6.ecdsaPrivateKey.chainCode), test6ChainCode)
  1253  }
  1254  
  1255  func TestSlip10ECDSAVector2(t *testing.T) {
  1256  	t.Parallel()
  1257  
  1258  	// https://github.com/satoshilabs/slips/blob/master/slip-0010.md#test-vector-2-for-secp256k1
  1259  	test1PrivateKey := "4b03d6fc340455b363f51020ad3ecca4f0850280cf436c70c727923f6db46c3e"
  1260  	test1PublicKey := "03cbcaa9c98c877a26977d00825c956a238e8dddfbd322cce4f74b0b5bd6ace4a7"
  1261  	test1ChainCode := "60499f801b896d83179a4374aeb7822aaeaceaa0db1f85ee3e904c4defbd9689"
  1262  	test2PrivateKey := "abe74a98f6c7eabee0428f53798f0ab8aa1bd37873999041703c742f15ac7e1e"
  1263  	test2PublicKey := "02fc9e5af0ac8d9b3cecfe2a888e2117ba3d089d8585886c9c826b6b22a98d12ea"
  1264  	test2ChainCode := "f0909affaa7ee7abe5dd4e100598d4dc53cd709d5a5c2cac40e7412f232f7c9c"
  1265  	test3PrivateKey := "877c779ad9687164e9c2f4f0f4ff0340814392330693ce95a58fe18fd52e6e93"
  1266  	test3PublicKey := "03c01e7425647bdefa82b12d9bad5e3e6865bee0502694b94ca58b666abc0a5c3b"
  1267  	test3ChainCode := "be17a268474a6bb9c61e1d720cf6215e2a88c5406c4aee7b38547f585c9a37d9"
  1268  	test4PrivateKey := "704addf544a06e5ee4bea37098463c23613da32020d604506da8c0518e1da4b7"
  1269  	test4PublicKey := "03a7d1d856deb74c508e05031f9895dab54626251b3806e16b4bd12e781a7df5b9"
  1270  	test4ChainCode := "f366f48f1ea9f2d1d3fe958c95ca84ea18e4c4ddb9366c336c927eb246fb38cb"
  1271  	test5PrivateKey := "f1c7c871a54a804afe328b4c83a1c33b8e5ff48f5087273f04efa83b247d6a2d"
  1272  	test5PublicKey := "02d2b36900396c9282fa14628566582f206a5dd0bcc8d5e892611806cafb0301f0"
  1273  	test5ChainCode := "637807030d55d01f9a0cb3a7839515d796bd07706386a6eddf06cc29a65a0e29"
  1274  	test6PrivateKey := "bb7d39bdb83ecf58f2fd82b6d918341cbef428661ef01ab97c28a4842125ac23"
  1275  	test6PublicKey := "024d902e1a2fc7a8755ab5b694c575fce742c48d9ff192e63df5193e4c7afe1f9c"
  1276  	test6ChainCode := "9452b549be8cea3ecb7a84bec10dcfd94afe4d129ebfd3b3cb58eedf394ed271"
  1277  
  1278  	seed, err := hex.DecodeString("fffcf9f6f3f0edeae7e4e1dedbd8d5d2cfccc9c6c3c0bdbab7b4b1aeaba8a5a29f9c999693908d8a8784817e7b7875726f6c696663605d5a5754514e4b484542")
  1279  	assert.NoError(t, err)
  1280  
  1281  	// Chain m
  1282  	key1, err := PrivateKeyFromSeedECDSAsecp256k1(seed)
  1283  	assert.NoError(t, err)
  1284  
  1285  	assert.Equal(t, key1.StringRaw(), test1PrivateKey)
  1286  	assert.Equal(t, key1.PublicKey().StringRaw(), test1PublicKey)
  1287  	assert.Equal(t, hex.EncodeToString(key1.ecdsaPrivateKey.chainCode), test1ChainCode)
  1288  
  1289  	// Chain m/0
  1290  	key2, err := key1.Derive(0)
  1291  	assert.NoError(t, err)
  1292  
  1293  	assert.Equal(t, key2.StringRaw(), test2PrivateKey)
  1294  	assert.Equal(t, key2.PublicKey().StringRaw(), test2PublicKey)
  1295  	assert.Equal(t, hex.EncodeToString(key2.ecdsaPrivateKey.chainCode), test2ChainCode)
  1296  
  1297  	// Chain m/0/2147483647'
  1298  	key3, err := key2.Derive(ToHardenedIndex(2147483647))
  1299  	assert.NoError(t, err)
  1300  	assert.Equal(t, key3.StringRaw(), test3PrivateKey)
  1301  	assert.Equal(t, key3.PublicKey().StringRaw(), test3PublicKey)
  1302  	assert.Equal(t, hex.EncodeToString(key3.ecdsaPrivateKey.chainCode), test3ChainCode)
  1303  
  1304  	// Chain m/0/2147483647'/1
  1305  	key4, err := key3.Derive(1)
  1306  	assert.NoError(t, err)
  1307  	assert.Equal(t, key4.StringRaw(), test4PrivateKey)
  1308  	assert.Equal(t, key4.PublicKey().StringRaw(), test4PublicKey)
  1309  	assert.Equal(t, hex.EncodeToString(key4.ecdsaPrivateKey.chainCode), test4ChainCode)
  1310  
  1311  	// Chain m/0/2147483647'/1/2147483646'
  1312  	key5, err := key4.Derive(ToHardenedIndex(2147483646))
  1313  	assert.NoError(t, err)
  1314  	assert.Equal(t, key5.StringRaw(), test5PrivateKey)
  1315  	assert.Equal(t, key5.PublicKey().StringRaw(), test5PublicKey)
  1316  	assert.Equal(t, hex.EncodeToString(key5.ecdsaPrivateKey.chainCode), test5ChainCode)
  1317  
  1318  	// Chain m/0/2147483647'/1/2147483646'/2
  1319  	key6, err := key5.Derive(2)
  1320  	assert.NoError(t, err)
  1321  	assert.Equal(t, key6.StringRaw(), test6PrivateKey)
  1322  	assert.Equal(t, key6.PublicKey().StringRaw(), test6PublicKey)
  1323  	assert.Equal(t, hex.EncodeToString(key6.ecdsaPrivateKey.chainCode), test6ChainCode)
  1324  }
  1325  
  1326  func TestFromBytesEd25519(t *testing.T) {
  1327  	keyBytes, _ := hex.DecodeString("0011223344556677889900112233445566778899001122334455667788990011")
  1328  	protoKey := &services.Key{
  1329  		Key: &services.Key_Ed25519{Ed25519: keyBytes},
  1330  	}
  1331  	bytes, err := proto.Marshal(protoKey)
  1332  	require.NoError(t, err)
  1333  
  1334  	cut, err := KeyFromBytes(bytes)
  1335  	require.NoError(t, err)
  1336  
  1337  	assert.IsType(t, PublicKey{}, cut)
  1338  	cutBytes, _ := KeyToBytes(cut.(PublicKey).ed25519PublicKey)
  1339  	assert.Equal(t, keyBytes, cutBytes[2:])
  1340  }
  1341  
  1342  func TestFromBytesECDSA(t *testing.T) {
  1343  	keyBytes, _ := hex.DecodeString("3a21034e0441201f2bf9c7d9873c2a9dc3fd451f64b7c05e17e4d781d916e3a11dfd99")
  1344  
  1345  	cut, err := KeyFromBytes(keyBytes)
  1346  	require.NoError(t, err)
  1347  
  1348  	assert.IsType(t, PublicKey{}, cut)
  1349  	cutBytes, _ := KeyToBytes(cut.(PublicKey).ecdsaPublicKey)
  1350  	assert.Equal(t, keyBytes, cutBytes)
  1351  }
  1352  
  1353  func TestFromBytesKeyList(t *testing.T) {
  1354  	keyBytes1, _ := hex.DecodeString("0011223344556677889900112233445566778899001122334455667788990011")
  1355  	keyBytes2, _ := hex.DecodeString("aa11223344556677889900112233445566778899001122334455667788990011")
  1356  
  1357  	protoKeyList := &services.KeyList{
  1358  		Keys: []*services.Key{
  1359  			{Key: &services.Key_Ed25519{Ed25519: keyBytes1}},
  1360  			{Key: &services.Key_Ed25519{Ed25519: keyBytes2}},
  1361  		},
  1362  	}
  1363  
  1364  	protoKey := &services.Key{
  1365  		Key: &services.Key_KeyList{KeyList: protoKeyList},
  1366  	}
  1367  	bytes, err := proto.Marshal(protoKey)
  1368  	require.NoError(t, err)
  1369  
  1370  	cut, err := KeyFromBytes(bytes)
  1371  	require.NoError(t, err)
  1372  
  1373  	assert.IsType(t, &KeyList{}, cut)
  1374  
  1375  	keyList := cut.(*KeyList)
  1376  	actual := keyList._ToProtoKey().GetKeyList()
  1377  
  1378  	assert.Len(t, actual.Keys, 2)
  1379  	assert.Equal(t, keyBytes1, actual.Keys[0].GetEd25519())
  1380  	assert.Equal(t, keyBytes2, actual.Keys[1].GetEd25519())
  1381  }
  1382  
  1383  func TestFromBytesThresholdKey(t *testing.T) {
  1384  	keyBytes1, _ := hex.DecodeString("0011223344556677889900112233445566778899001122334455667788990011")
  1385  	keyBytes2, _ := hex.DecodeString("aa11223344556677889900112233445566778899001122334455667788990011")
  1386  
  1387  	protoKeyList := &services.KeyList{
  1388  		Keys: []*services.Key{
  1389  			{Key: &services.Key_Ed25519{Ed25519: keyBytes1}},
  1390  			{Key: &services.Key_Ed25519{Ed25519: keyBytes2}},
  1391  		},
  1392  	}
  1393  
  1394  	protoThresholdKey := &services.ThresholdKey{
  1395  		Threshold: 1,
  1396  		Keys:      protoKeyList,
  1397  	}
  1398  
  1399  	protoKey := &services.Key{
  1400  		Key: &services.Key_ThresholdKey{ThresholdKey: protoThresholdKey},
  1401  	}
  1402  	bytes, err := proto.Marshal(protoKey)
  1403  	require.NoError(t, err)
  1404  
  1405  	cut, err := KeyFromBytes(bytes)
  1406  	require.NoError(t, err)
  1407  
  1408  	assert.IsType(t, &KeyList{}, cut)
  1409  
  1410  	thresholdKey := cut.(*KeyList)
  1411  	actual := thresholdKey._ToProtoKey().GetThresholdKey()
  1412  
  1413  	assert.Equal(t, uint32(1), actual.Threshold)
  1414  	assert.Len(t, actual.Keys.Keys, 2)
  1415  	assert.Equal(t, keyBytes1, actual.Keys.Keys[0].GetEd25519())
  1416  	assert.Equal(t, keyBytes2, actual.Keys.Keys[1].GetEd25519())
  1417  }
  1418  
  1419  func TestThrowsUnsupportedKeyFromBytes(t *testing.T) {
  1420  	keyBytes := []byte{0, 1, 2}
  1421  	protoKey := &services.Key{
  1422  		Key: &services.Key_RSA_3072{RSA_3072: keyBytes},
  1423  	}
  1424  	bytes, err := proto.Marshal(protoKey)
  1425  	require.NoError(t, err)
  1426  
  1427  	_, err = KeyFromBytes(bytes)
  1428  	assert.Error(t, err)
  1429  	assert.Equal(t, "key type not implemented: &{[0 1 2]}", err.Error())
  1430  }
  1431  
  1432  func TestStringMethod(t *testing.T) {
  1433  	ecdsaPrivateKey, _ := _GenerateECDSAPrivateKey()
  1434  	ecdsaPublicKey := ecdsaPrivateKey._PublicKey()
  1435  
  1436  	ed25519PrivateKey, _ := _GenerateEd25519PrivateKey()
  1437  	ed25519PublicKey := ed25519PrivateKey._PublicKey()
  1438  
  1439  	assert.Equal(t, ecdsaPrivateKey.String(), ecdsaPrivateKey.String())
  1440  	assert.Equal(t, ecdsaPublicKey.String(), ecdsaPublicKey.String())
  1441  	assert.Equal(t, ed25519PrivateKey.String(), ed25519PrivateKey.String())
  1442  	assert.Equal(t, ed25519PublicKey.String(), ed25519PublicKey.String())
  1443  
  1444  }