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

     1  package hedera
     2  
     3  /*-
     4   *
     5   * Hedera Go SDK
     6   *
     7   * Copyright (C) 2020 - 2024 Hedera Hashgraph, LLC
     8   *
     9   * Licensed under the Apache License, Version 2.0 (the "License");
    10   * you may not use this file except in compliance with the License.
    11   * You may obtain a copy of the License at
    12   *
    13   *      http://www.apache.org/licenses/LICENSE-2.0
    14   *
    15   * Unless required by applicable law or agreed to in writing, software
    16   * distributed under the License is distributed on an "AS IS" BASIS,
    17   * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
    18   * See the License for the specific language governing permissions and
    19   * limitations under the License.
    20   *
    21   */
    22  
    23  import (
    24  	"crypto/hmac"
    25  	"crypto/sha512"
    26  	"encoding/binary"
    27  	"encoding/hex"
    28  	"io"
    29  	"math/big"
    30  	"strings"
    31  
    32  	"github.com/ethereum/go-ethereum/crypto"
    33  	"github.com/hashgraph/hedera-protobufs-go/services"
    34  	"github.com/pkg/errors"
    35  	"golang.org/x/crypto/pbkdf2"
    36  	protobuf "google.golang.org/protobuf/proto"
    37  )
    38  
    39  const _Ed25519PrivateKeyPrefix = "302e020100300506032b657004220420"
    40  
    41  type Key interface {
    42  	_ToProtoKey() *services.Key
    43  	String() string
    44  }
    45  
    46  func KeyFromBytes(bytes []byte) (Key, error) {
    47  	protoKey := &services.Key{}
    48  
    49  	err := protobuf.Unmarshal(bytes, protoKey)
    50  	if err != nil {
    51  		return nil, err
    52  	}
    53  
    54  	return _KeyFromProtobuf(protoKey)
    55  }
    56  
    57  func KeyToBytes(key Key) ([]byte, error) {
    58  	protoKey := key._ToProtoKey()
    59  	return protobuf.Marshal(protoKey)
    60  }
    61  
    62  func _KeyFromProtobuf(pbKey *services.Key) (Key, error) {
    63  	if pbKey == nil {
    64  		return PublicKey{}, errParameterNull
    65  	}
    66  	switch key := pbKey.GetKey().(type) {
    67  	case *services.Key_Ed25519:
    68  		return PublicKeyFromBytesEd25519(key.Ed25519)
    69  
    70  	case *services.Key_ThresholdKey:
    71  		threshold := int(key.ThresholdKey.GetThreshold())
    72  		keys, err := _KeyListFromProtobuf(key.ThresholdKey.GetKeys())
    73  		if err != nil {
    74  			return nil, err
    75  		}
    76  		keys.threshold = threshold
    77  
    78  		return &keys, nil
    79  
    80  	case *services.Key_KeyList:
    81  		keys, err := _KeyListFromProtobuf(key.KeyList)
    82  		if err != nil {
    83  			return nil, err
    84  		}
    85  
    86  		return &keys, nil
    87  
    88  	case *services.Key_ContractID:
    89  		return _ContractIDFromProtobuf(key.ContractID), nil
    90  
    91  	case *services.Key_ECDSASecp256K1:
    92  		return PublicKeyFromBytesECDSA(key.ECDSASecp256K1)
    93  
    94  	case *services.Key_DelegatableContractId:
    95  		return _DelegatableContractIDFromProtobuf(key.DelegatableContractId), nil
    96  
    97  	default:
    98  		return nil, _NewErrBadKeyf("key type not implemented: %v", key)
    99  	}
   100  }
   101  
   102  type PrivateKey struct {
   103  	ecdsaPrivateKey   *_ECDSAPrivateKey
   104  	ed25519PrivateKey *_Ed25519PrivateKey
   105  }
   106  
   107  type PublicKey struct {
   108  	ecdsaPublicKey   *_ECDSAPublicKey
   109  	ed25519PublicKey *_Ed25519PublicKey
   110  }
   111  
   112  /**
   113   *  SDK needs to provide  a way to set an unusable key such as an Ed25519 all-zeros
   114   *  key, since it is (presumably) impossible to find the 32-byte string whose SHA-512 hash begins with 32 bytes
   115   *  of zeros. We recommend using all-zeros to clearly advertise any unusable keys.
   116   */
   117  func ZeroKey() (PublicKey, error) {
   118  	return PublicKeyFromString("0000000000000000000000000000000000000000000000000000000000000000")
   119  }
   120  
   121  // PrivateKeyGenerateEcdsa Generates a new ECDSASecp256K1 key
   122  func PrivateKeyGenerateEcdsa() (PrivateKey, error) {
   123  	key, err := _GenerateECDSAPrivateKey()
   124  	if err != nil {
   125  		return PrivateKey{}, err
   126  	}
   127  	return PrivateKey{
   128  		ecdsaPrivateKey: key,
   129  	}, nil
   130  }
   131  
   132  // Deprecated: use `PrivateKeyGenerateEd25519()` instead
   133  func PrivateKeyGenerate() (PrivateKey, error) {
   134  	return PrivateKeyGenerateEd25519()
   135  }
   136  
   137  // PrivateKeyGenerateEd25519 Generates a new Ed25519 key
   138  func PrivateKeyGenerateEd25519() (PrivateKey, error) {
   139  	key, err := _GenerateEd25519PrivateKey()
   140  	if err != nil {
   141  		return PrivateKey{}, err
   142  	}
   143  	return PrivateKey{
   144  		ed25519PrivateKey: key,
   145  	}, nil
   146  }
   147  
   148  // Deprecated the use of raw bytes for a Ed25519 private key is deprecated; use PrivateKeyFromBytesEd25519() instead.
   149  func PrivateKeyFromBytes(bytes []byte) (PrivateKey, error) {
   150  	key, err := _Ed25519PrivateKeyFromBytes(bytes)
   151  	if err != nil {
   152  		key2, err2 := _ECDSAPrivateKeyFromBytes(bytes)
   153  		if err2 != nil {
   154  			return PrivateKey{}, err2
   155  		}
   156  
   157  		return PrivateKey{
   158  			ecdsaPrivateKey: key2,
   159  		}, nil
   160  	}
   161  
   162  	return PrivateKey{
   163  		ed25519PrivateKey: key,
   164  	}, nil
   165  }
   166  
   167  func PrivateKeyFromBytesDer(bytes []byte) (PrivateKey, error) {
   168  	key, err := _Ed25519PrivateKeyFromBytes(bytes)
   169  	if err != nil {
   170  		key2, err2 := _ECDSAPrivateKeyFromBytes(bytes)
   171  		if err2 != nil {
   172  			return PrivateKey{}, err2
   173  		}
   174  
   175  		return PrivateKey{
   176  			ecdsaPrivateKey: key2,
   177  		}, nil
   178  	}
   179  
   180  	return PrivateKey{
   181  		ed25519PrivateKey: key,
   182  	}, nil
   183  }
   184  
   185  func PrivateKeyFromBytesEd25519(bytes []byte) (PrivateKey, error) {
   186  	key, err := _Ed25519PrivateKeyFromBytes(bytes)
   187  	if err != nil {
   188  		return PrivateKey{}, err
   189  	}
   190  
   191  	return PrivateKey{
   192  		ed25519PrivateKey: key,
   193  	}, nil
   194  }
   195  
   196  func PrivateKeyFromBytesECDSA(bytes []byte) (PrivateKey, error) {
   197  	key, err := _ECDSAPrivateKeyFromBytes(bytes)
   198  	if err != nil {
   199  		return PrivateKey{}, err
   200  	}
   201  
   202  	return PrivateKey{
   203  		ecdsaPrivateKey: key,
   204  	}, nil
   205  }
   206  
   207  func PublicKeyFromBytesEd25519(bytes []byte) (PublicKey, error) {
   208  	key, err := _Ed25519PublicKeyFromBytes(bytes)
   209  	if err != nil {
   210  		return PublicKey{}, err
   211  	}
   212  
   213  	return PublicKey{
   214  		ed25519PublicKey: key,
   215  	}, nil
   216  }
   217  
   218  func PublicKeyFromBytesECDSA(bytes []byte) (PublicKey, error) {
   219  	key, err := _ECDSAPublicKeyFromBytes(bytes)
   220  	if err != nil {
   221  		return PublicKey{}, err
   222  	}
   223  
   224  	return PublicKey{
   225  		ecdsaPublicKey: key,
   226  	}, nil
   227  }
   228  
   229  // Deprecated the use of raw bytes for a Ed25519 private key is deprecated; use PublicKeyFromBytesEd25519() instead.
   230  func PublicKeyFromBytes(bytes []byte) (PublicKey, error) {
   231  	key, err := _Ed25519PublicKeyFromBytes(bytes)
   232  	if err != nil {
   233  		key2, err2 := _ECDSAPublicKeyFromBytes(bytes)
   234  		if err2 != nil {
   235  			return PublicKey{}, err2
   236  		}
   237  
   238  		return PublicKey{
   239  			ecdsaPublicKey: key2,
   240  		}, nil
   241  	}
   242  
   243  	return PublicKey{
   244  		ed25519PublicKey: key,
   245  	}, nil
   246  }
   247  
   248  func PublicKeyFromBytesDer(bytes []byte) (PublicKey, error) {
   249  	key, err := _Ed25519PublicKeyFromBytes(bytes)
   250  	if err != nil {
   251  		key2, err2 := _ECDSAPublicKeyFromBytes(bytes)
   252  		if err2 != nil {
   253  			return PublicKey{}, err2
   254  		}
   255  
   256  		return PublicKey{
   257  			ecdsaPublicKey: key2,
   258  		}, nil
   259  	}
   260  
   261  	return PublicKey{
   262  		ed25519PublicKey: key,
   263  	}, nil
   264  }
   265  
   266  // Deprecated
   267  // PrivateKeyFromMnemonic recovers an _Ed25519PrivateKey from a valid 24 word length mnemonic phrase and a
   268  // passphrase.
   269  //
   270  // An empty string can be passed for passPhrase If the mnemonic phrase wasn't generated with a passphrase. This is
   271  // required to recover a private key from a mnemonic generated by the Android and iOS wallets.
   272  func PrivateKeyFromMnemonic(mnemonic Mnemonic, passPhrase string) (PrivateKey, error) {
   273  	key, err := _Ed25519PrivateKeyFromMnemonic(mnemonic, passPhrase)
   274  	if err != nil {
   275  		return PrivateKey{}, err
   276  	}
   277  
   278  	return PrivateKey{
   279  		ed25519PrivateKey: key,
   280  	}, nil
   281  }
   282  
   283  // The use of raw bytes for a Ed25519 private key is deprecated; use PrivateKeyFromStringEd25519() instead.
   284  func PrivateKeyFromString(s string) (PrivateKey, error) {
   285  	byt, err := hex.DecodeString(s)
   286  	if err != nil {
   287  		return PrivateKey{}, err
   288  	}
   289  
   290  	return PrivateKeyFromBytes(byt)
   291  }
   292  
   293  // PrivateKeyFromStringDer Creates PrivateKey from hex string with a der prefix
   294  func PrivateKeyFromStringDer(s string) (PrivateKey, error) {
   295  	KeyEd25519, err := _Ed25519PrivateKeyFromString(s)
   296  	if err == nil {
   297  		return PrivateKey{ed25519PrivateKey: KeyEd25519}, nil
   298  	}
   299  
   300  	keyECDSA, err := _ECDSAPrivateKeyFromString(s)
   301  	if err == nil {
   302  		return PrivateKey{ecdsaPrivateKey: keyECDSA}, nil
   303  	}
   304  
   305  	return PrivateKey{}, errors.New("invalid private key format")
   306  }
   307  
   308  func PrivateKeyFromStringEd25519(s string) (PrivateKey, error) {
   309  	key, err := _Ed25519PrivateKeyFromString(s)
   310  	if err != nil {
   311  		return PrivateKey{}, err
   312  	}
   313  
   314  	return PrivateKey{
   315  		ed25519PrivateKey: key,
   316  	}, nil
   317  }
   318  
   319  // Deprecated: use PrivateKeyFromStringECDSA() instead
   320  func PrivateKeyFromStringECSDA(s string) (PrivateKey, error) {
   321  	return PrivateKeyFromStringECDSA(s)
   322  }
   323  
   324  func PrivateKeyFromStringECDSA(s string) (PrivateKey, error) {
   325  	trimmedKey := strings.TrimPrefix(s, "0x")
   326  	key, err := _ECDSAPrivateKeyFromString(trimmedKey)
   327  	if err != nil {
   328  		return PrivateKey{}, err
   329  	}
   330  
   331  	return PrivateKey{
   332  		ecdsaPrivateKey: key,
   333  	}, nil
   334  }
   335  
   336  func PrivateKeyFromSeedEd25519(seed []byte) (PrivateKey, error) {
   337  	key, err := _Ed25519PrivateKeyFromSeed(seed)
   338  	if err != nil {
   339  		return PrivateKey{}, err
   340  	}
   341  
   342  	return PrivateKey{
   343  		ed25519PrivateKey: key,
   344  	}, nil
   345  }
   346  
   347  func PrivateKeyFromSeedECDSAsecp256k1(seed []byte) (PrivateKey, error) {
   348  	key, err := _ECDSAPrivateKeyFromSeed(seed)
   349  	if err != nil {
   350  		return PrivateKey{}, err
   351  	}
   352  
   353  	return PrivateKey{
   354  		ecdsaPrivateKey: key,
   355  	}, nil
   356  }
   357  
   358  func PrivateKeyFromKeystore(ks []byte, passphrase string) (PrivateKey, error) {
   359  	key, err := _Ed25519PrivateKeyFromKeystore(ks, passphrase)
   360  	if err != nil {
   361  		return PrivateKey{}, err
   362  	}
   363  
   364  	return PrivateKey{
   365  		ed25519PrivateKey: key,
   366  	}, nil
   367  }
   368  
   369  // PrivateKeyReadKeystore recovers an _Ed25519PrivateKey from an encrypted _Keystore file.
   370  func PrivateKeyReadKeystore(source io.Reader, passphrase string) (PrivateKey, error) {
   371  	key, err := _Ed25519PrivateKeyReadKeystore(source, passphrase)
   372  	if err != nil {
   373  		return PrivateKey{}, err
   374  	}
   375  
   376  	return PrivateKey{
   377  		ed25519PrivateKey: key,
   378  	}, nil
   379  }
   380  
   381  func PrivateKeyFromPem(bytes []byte, passphrase string) (PrivateKey, error) {
   382  	key, err := _Ed25519PrivateKeyFromPem(bytes, passphrase)
   383  	if err != nil {
   384  		key, err := _ECDSAPrivateKeyFromPem(bytes, passphrase)
   385  		if err != nil {
   386  			return PrivateKey{}, err
   387  		}
   388  		return PrivateKey{
   389  			ecdsaPrivateKey: key,
   390  		}, nil
   391  	}
   392  
   393  	return PrivateKey{
   394  		ed25519PrivateKey: key,
   395  	}, nil
   396  }
   397  
   398  func PrivateKeyReadPem(source io.Reader, passphrase string) (PrivateKey, error) {
   399  	// note: Passphrases are currently not supported, but included in the function definition to avoid breaking
   400  	// changes in the future.
   401  
   402  	key, err := _Ed25519PrivateKeyReadPem(source, passphrase)
   403  	if err != nil {
   404  		key, err := _ECDSAPrivateKeyReadPem(source, passphrase)
   405  		if err != nil {
   406  			return PrivateKey{}, err
   407  		}
   408  		return PrivateKey{
   409  			ecdsaPrivateKey: key,
   410  		}, nil
   411  	}
   412  
   413  	return PrivateKey{
   414  		ed25519PrivateKey: key,
   415  	}, nil
   416  }
   417  
   418  // The use of raw bytes for a Ed25519 public key is deprecated; use PublicKeyFromStringEd25519/ECDSA() instead.
   419  func PublicKeyFromString(s string) (PublicKey, error) {
   420  	byt, err := hex.DecodeString(s)
   421  	if err != nil {
   422  		return PublicKey{}, err
   423  	}
   424  
   425  	return PublicKeyFromBytes(byt)
   426  }
   427  
   428  func PublicKeyFromStringECDSA(s string) (PublicKey, error) {
   429  	key, err := _ECDSAPublicKeyFromString(s)
   430  	if err != nil {
   431  		return PublicKey{}, err
   432  	}
   433  
   434  	return PublicKey{
   435  		ecdsaPublicKey: key,
   436  	}, nil
   437  }
   438  
   439  func PublicKeyFromStringEd25519(s string) (PublicKey, error) {
   440  	key, err := _Ed25519PublicKeyFromString(s)
   441  	if err != nil {
   442  		return PublicKey{}, err
   443  	}
   444  
   445  	return PublicKey{
   446  		ed25519PublicKey: key,
   447  	}, nil
   448  }
   449  
   450  func _DeriveEd25519ChildKey(parentKey []byte, chainCode []byte, index uint32) ([]byte, []byte, error) {
   451  	if IsHardenedIndex(index) {
   452  		return nil, nil, errors.New("the index should not be pre-hardened")
   453  	}
   454  
   455  	h := hmac.New(sha512.New, chainCode)
   456  
   457  	input := make([]byte, 37)
   458  
   459  	// 0x00 + parentKey + _Index(BE)
   460  	input[0] = 0
   461  
   462  	copy(input[1:37], parentKey)
   463  
   464  	binary.BigEndian.PutUint32(input[33:37], index)
   465  
   466  	// harden the input
   467  	input[33] |= 128
   468  
   469  	if _, err := h.Write(input); err != nil {
   470  		return nil, nil, err
   471  	}
   472  
   473  	digest := h.Sum(nil)
   474  
   475  	return digest[0:32], digest[32:], nil
   476  }
   477  
   478  func _DeriveECDSAChildKey(parentKey []byte, chainCode []byte, index uint32) ([]byte, []byte, error) {
   479  	h := hmac.New(sha512.New, chainCode)
   480  
   481  	isHardened := IsHardenedIndex(index)
   482  	input := make([]byte, 37)
   483  	key, err := crypto.ToECDSA(parentKey)
   484  	if err != nil {
   485  		return nil, nil, err
   486  	}
   487  
   488  	if isHardened {
   489  		offset := 33 - len(parentKey)
   490  		copy(input[offset:], parentKey)
   491  	} else {
   492  		pubKey := crypto.CompressPubkey(&key.PublicKey)
   493  		copy(input, pubKey)
   494  	}
   495  
   496  	binary.BigEndian.PutUint32(input[33:37], index)
   497  
   498  	if _, err := h.Write(input); err != nil {
   499  		return nil, nil, err
   500  	}
   501  
   502  	i := h.Sum(nil)
   503  
   504  	il := new(big.Int)
   505  	il.SetBytes(i[0:32])
   506  	ir := i[32:]
   507  
   508  	ki := new(big.Int)
   509  	ki.Add(key.D, il)
   510  	ki.Mod(ki, key.Curve.Params().N)
   511  
   512  	return ki.Bytes(), ir, nil
   513  }
   514  
   515  func _DeriveLegacyChildKey(parentKey []byte, index int64) ([]byte, error) {
   516  	in := make([]uint8, 8)
   517  
   518  	switch switchIndex := index; {
   519  	case switchIndex == int64(0xffffffffff):
   520  		in = []uint8{0, 0, 0, 0xff, 0xff, 0xff, 0xff, 0xff}
   521  	case switchIndex > 0xffffffff:
   522  		return nil, errors.New("derive index is out of range")
   523  	default:
   524  		if switchIndex < 0 {
   525  			for i := 0; i < 4; i++ {
   526  				in[i] = uint8(0xff)
   527  			}
   528  		}
   529  
   530  		for i := 4; i < len(in); i++ {
   531  			in[i] = uint8(switchIndex)
   532  		}
   533  	}
   534  
   535  	password := make([]uint8, len(parentKey))
   536  	copy(password, parentKey)
   537  	password = append(password, in...)
   538  
   539  	salt := []byte{0xFF}
   540  
   541  	return pbkdf2.Key(password, salt, 2048, 32, sha512.New), nil
   542  }
   543  
   544  func (sk PrivateKey) PublicKey() PublicKey {
   545  	if sk.ecdsaPrivateKey != nil {
   546  		return PublicKey{
   547  			ecdsaPublicKey: sk.ecdsaPrivateKey._PublicKey(),
   548  		}
   549  	}
   550  
   551  	if sk.ed25519PrivateKey != nil {
   552  		return PublicKey{
   553  			ed25519PublicKey: sk.ed25519PrivateKey._PublicKey(),
   554  		}
   555  	}
   556  
   557  	return PublicKey{}
   558  }
   559  
   560  func (sk PrivateKey) ToAccountID(shard uint64, realm uint64) *AccountID {
   561  	return sk.PublicKey().ToAccountID(shard, realm)
   562  }
   563  
   564  func (pk PublicKey) ToAccountID(shard uint64, realm uint64) *AccountID {
   565  	temp := pk
   566  
   567  	return &AccountID{
   568  		Shard:    shard,
   569  		Realm:    realm,
   570  		Account:  0,
   571  		AliasKey: &temp,
   572  		checksum: nil,
   573  	}
   574  }
   575  
   576  // String returns the text-encoded representation of the PrivateKey.
   577  func (sk PrivateKey) String() string {
   578  	if sk.ecdsaPrivateKey != nil {
   579  		return sk.ecdsaPrivateKey._StringDer()
   580  	}
   581  
   582  	if sk.ed25519PrivateKey != nil {
   583  		return sk.ed25519PrivateKey._StringDer()
   584  	}
   585  
   586  	return ""
   587  }
   588  
   589  func (sk PrivateKey) StringRaw() string {
   590  	if sk.ecdsaPrivateKey != nil {
   591  		return sk.ecdsaPrivateKey._StringRaw()
   592  	}
   593  
   594  	if sk.ed25519PrivateKey != nil {
   595  		return sk.ed25519PrivateKey._StringRaw()
   596  	}
   597  
   598  	return ""
   599  }
   600  
   601  func (sk PrivateKey) StringDer() string {
   602  	if sk.ecdsaPrivateKey != nil {
   603  		return sk.ecdsaPrivateKey._StringDer()
   604  	}
   605  
   606  	if sk.ed25519PrivateKey != nil {
   607  		return sk.ed25519PrivateKey._StringDer()
   608  	}
   609  
   610  	return ""
   611  }
   612  
   613  func (pk PublicKey) String() string {
   614  	if pk.ecdsaPublicKey != nil {
   615  		return pk.ecdsaPublicKey._StringDer()
   616  	}
   617  
   618  	if pk.ed25519PublicKey != nil {
   619  		return pk.ed25519PublicKey._StringDer()
   620  	}
   621  
   622  	return ""
   623  }
   624  
   625  func (pk PublicKey) StringDer() string {
   626  	if pk.ecdsaPublicKey != nil {
   627  		return pk.ecdsaPublicKey._StringDer()
   628  	}
   629  
   630  	if pk.ed25519PublicKey != nil {
   631  		return pk.ed25519PublicKey._StringDer()
   632  	}
   633  
   634  	return ""
   635  }
   636  
   637  func (pk PublicKey) StringRaw() string {
   638  	if pk.ecdsaPublicKey != nil {
   639  		return pk.ecdsaPublicKey._StringRaw()
   640  	}
   641  
   642  	if pk.ed25519PublicKey != nil {
   643  		return pk.ed25519PublicKey._StringRaw()
   644  	}
   645  
   646  	return ""
   647  }
   648  
   649  // `Deprecated: Use ToEvmAddress instead`
   650  func (pk PublicKey) ToEthereumAddress() string {
   651  	if pk.ecdsaPublicKey != nil {
   652  		return pk.ecdsaPublicKey._ToEthereumAddress()
   653  	}
   654  
   655  	panic("unsupported operation on Ed25519PublicKey")
   656  }
   657  
   658  func (pk PublicKey) ToEvmAddress() string {
   659  	if pk.ecdsaPublicKey != nil {
   660  		return pk.ecdsaPublicKey._ToEthereumAddress()
   661  	}
   662  
   663  	panic("unsupported operation on Ed25519PublicKey")
   664  }
   665  
   666  /*
   667   * For `Ed25519` the result of this method call is identical to `toBytesRaw()` while for `ECDSA`
   668   * this method is identical to `toBytesDer()`.
   669   *
   670   * We strongly recommend using `toBytesRaw()` or `toBytesDer()` instead.
   671   */
   672  func (sk PrivateKey) Bytes() []byte {
   673  	if sk.ecdsaPrivateKey != nil {
   674  		return sk.ecdsaPrivateKey._BytesDer()
   675  	}
   676  
   677  	if sk.ed25519PrivateKey != nil {
   678  		return sk.ed25519PrivateKey._BytesRaw()
   679  	}
   680  
   681  	return []byte{}
   682  }
   683  
   684  func (sk PrivateKey) BytesDer() []byte {
   685  	if sk.ecdsaPrivateKey != nil {
   686  		return sk.ecdsaPrivateKey._BytesDer()
   687  	}
   688  
   689  	if sk.ed25519PrivateKey != nil {
   690  		return sk.ed25519PrivateKey._BytesDer()
   691  	}
   692  
   693  	return []byte{}
   694  }
   695  
   696  func (sk PrivateKey) BytesRaw() []byte {
   697  	if sk.ecdsaPrivateKey != nil {
   698  		return sk.ecdsaPrivateKey._BytesRaw()
   699  	}
   700  
   701  	if sk.ed25519PrivateKey != nil {
   702  		return sk.ed25519PrivateKey._BytesRaw()
   703  	}
   704  
   705  	return []byte{}
   706  }
   707  
   708  /*
   709   * For `Ed25519` the result of this method call is identical to `toBytesRaw()` while for `ECDSA`
   710   * this method is identical to `toBytesDer()`.
   711   *
   712   * We strongly recommend using `toBytesRaw()` or `toBytesDer()` instead.
   713   */
   714  func (pk PublicKey) Bytes() []byte {
   715  	if pk.ecdsaPublicKey != nil {
   716  		return pk.ecdsaPublicKey._BytesDer()
   717  	}
   718  
   719  	if pk.ed25519PublicKey != nil {
   720  		return pk.ed25519PublicKey._BytesRaw()
   721  	}
   722  
   723  	return []byte{}
   724  }
   725  
   726  func (pk PublicKey) BytesRaw() []byte {
   727  	if pk.ecdsaPublicKey != nil {
   728  		return pk.ecdsaPublicKey._BytesRaw()
   729  	}
   730  
   731  	if pk.ed25519PublicKey != nil {
   732  		return pk.ed25519PublicKey._BytesRaw()
   733  	}
   734  
   735  	return []byte{}
   736  }
   737  
   738  func (pk PublicKey) BytesDer() []byte {
   739  	if pk.ecdsaPublicKey != nil {
   740  		return pk.ecdsaPublicKey._BytesDer()
   741  	}
   742  
   743  	if pk.ed25519PublicKey != nil {
   744  		return pk.ed25519PublicKey._BytesDer()
   745  	}
   746  
   747  	return []byte{}
   748  }
   749  
   750  func (sk PrivateKey) Keystore(passphrase string) ([]byte, error) {
   751  	if sk.ed25519PrivateKey != nil {
   752  		return sk.ed25519PrivateKey._Keystore(passphrase)
   753  	}
   754  
   755  	return []byte{}, errors.New("only ed25519 keystore is supported right now")
   756  }
   757  
   758  func (sk PrivateKey) WriteKeystore(destination io.Writer, passphrase string) error {
   759  	if sk.ed25519PrivateKey != nil {
   760  		return sk.ed25519PrivateKey._WriteKeystore(destination, passphrase)
   761  	}
   762  
   763  	return errors.New("only writing ed25519 keystore is supported right now")
   764  }
   765  
   766  // Sign signs the provided message with the Ed25519PrivateKey.
   767  func (sk PrivateKey) Sign(message []byte) []byte {
   768  	if sk.ed25519PrivateKey != nil {
   769  		return sk.ed25519PrivateKey._Sign(message)
   770  	}
   771  	if sk.ecdsaPrivateKey != nil {
   772  		return sk.ecdsaPrivateKey._Sign(message)
   773  	}
   774  
   775  	return []byte{}
   776  }
   777  
   778  func (sk PrivateKey) SupportsDerivation() bool {
   779  	if sk.ed25519PrivateKey != nil {
   780  		return sk.ed25519PrivateKey._SupportsDerivation()
   781  	}
   782  
   783  	return false
   784  }
   785  
   786  func (sk PrivateKey) Derive(index uint32) (PrivateKey, error) {
   787  	if sk.ed25519PrivateKey != nil {
   788  		key, err := sk.ed25519PrivateKey._Derive(index)
   789  		if err != nil {
   790  			return PrivateKey{}, err
   791  		}
   792  
   793  		return PrivateKey{
   794  			ed25519PrivateKey: key,
   795  		}, nil
   796  	}
   797  	if sk.ecdsaPrivateKey != nil {
   798  		key, err := sk.ecdsaPrivateKey._Derive(index)
   799  		if err != nil {
   800  			return PrivateKey{}, err
   801  		}
   802  
   803  		return PrivateKey{
   804  			ecdsaPrivateKey: key,
   805  		}, nil
   806  	}
   807  
   808  	return PrivateKey{}, nil
   809  }
   810  
   811  func (sk PrivateKey) LegacyDerive(index int64) (PrivateKey, error) {
   812  	if sk.ed25519PrivateKey != nil {
   813  		key, err := sk.ed25519PrivateKey._LegacyDerive(index)
   814  		if err != nil {
   815  			return PrivateKey{}, err
   816  		}
   817  
   818  		return PrivateKey{
   819  			ed25519PrivateKey: key,
   820  		}, nil
   821  	}
   822  
   823  	return PrivateKey{}, errors.New("only ed25519 legacy derivation is supported")
   824  }
   825  
   826  func (sk PrivateKey) _ToProtoKey() *services.Key {
   827  	if sk.ecdsaPrivateKey != nil {
   828  		return sk.ecdsaPrivateKey._ToProtoKey()
   829  	}
   830  
   831  	if sk.ed25519PrivateKey != nil {
   832  		return sk.ed25519PrivateKey._ToProtoKey()
   833  	}
   834  
   835  	return &services.Key{}
   836  }
   837  
   838  func (pk PublicKey) _ToProtoKey() *services.Key {
   839  	if pk.ecdsaPublicKey != nil {
   840  		return pk.ecdsaPublicKey._ToProtoKey()
   841  	}
   842  
   843  	if pk.ed25519PublicKey != nil {
   844  		return pk.ed25519PublicKey._ToProtoKey()
   845  	}
   846  
   847  	return &services.Key{}
   848  }
   849  
   850  func (pk PublicKey) _ToSignaturePairProtobuf(signature []byte) *services.SignaturePair {
   851  	if pk.ecdsaPublicKey != nil {
   852  		return pk.ecdsaPublicKey._ToSignaturePairProtobuf(signature)
   853  	}
   854  
   855  	if pk.ed25519PublicKey != nil {
   856  		return pk.ed25519PublicKey._ToSignaturePairProtobuf(signature)
   857  	}
   858  
   859  	return &services.SignaturePair{}
   860  }
   861  
   862  // SignTransaction signes the transaction and adds the signature to the transaction
   863  func (sk PrivateKey) SignTransaction(tx *Transaction) ([]byte, error) {
   864  	if sk.ecdsaPrivateKey != nil {
   865  		b, err := sk.ecdsaPrivateKey._SignTransaction(tx)
   866  		if err != nil {
   867  			return []byte{}, err
   868  		}
   869  
   870  		return b, nil
   871  	}
   872  
   873  	if sk.ed25519PrivateKey != nil {
   874  		b, err := sk.ed25519PrivateKey._SignTransaction(tx)
   875  		if err != nil {
   876  			return []byte{}, err
   877  		}
   878  
   879  		return b, nil
   880  	}
   881  
   882  	return []byte{}, errors.New("key type not supported, only ed25519 and ECDSASecp256K1 are supported right now")
   883  }
   884  
   885  func (pk PublicKey) Verify(message []byte, signature []byte) bool {
   886  	if pk.ecdsaPublicKey != nil {
   887  		return pk.ecdsaPublicKey._Verify(message, signature)
   888  	}
   889  
   890  	if pk.ed25519PublicKey != nil {
   891  		return pk.ed25519PublicKey._Verify(message, signature)
   892  	}
   893  
   894  	return false
   895  }
   896  
   897  func (pk PublicKey) VerifyTransaction(transaction Transaction) bool {
   898  	if pk.ecdsaPublicKey != nil {
   899  		return pk.ecdsaPublicKey._VerifyTransaction(transaction)
   900  	}
   901  
   902  	if pk.ed25519PublicKey != nil {
   903  		return pk.ed25519PublicKey._VerifyTransaction(transaction)
   904  	}
   905  
   906  	return false
   907  }