github.com/fibonacci-chain/fbc@v0.0.0-20231124064014-c7636198c1e9/libs/cosmos-sdk/types/address_exchain.go (about)

     1  package types
     2  
     3  import (
     4  	"crypto/sha256"
     5  
     6  	"github.com/fibonacci-chain/fbc/libs/cosmos-sdk/types/errors"
     7  	"github.com/fibonacci-chain/fbc/libs/tendermint/crypto"
     8  )
     9  
    10  // MustBech32ifyAccPub returns the result of Bech32ifyAccPub panicing on failure.
    11  func MustBech32ifyAccPub(pub crypto.PubKey) string {
    12  	return MustBech32ifyPubKey(Bech32PubKeyTypeAccPub, pub)
    13  }
    14  
    15  // Derive derives a new address from the main `address` and a derivation `key`.
    16  func Derive(address []byte, key []byte) []byte {
    17  	return Hash(UnsafeBytesToStr(address), key)
    18  }
    19  
    20  // Hash creates a new address from address type and key
    21  func Hash(typ string, key []byte) []byte {
    22  	hasher := sha256.New()
    23  	_, err := hasher.Write(UnsafeStrToBytes(typ))
    24  	// the error always nil, it's here only to satisfy the io.Writer interface
    25  	errors.AssertNil(err)
    26  	th := hasher.Sum(nil)
    27  
    28  	hasher.Reset()
    29  	_, err = hasher.Write(th)
    30  	errors.AssertNil(err)
    31  	_, err = hasher.Write(key)
    32  	errors.AssertNil(err)
    33  	return hasher.Sum(nil)
    34  }