github.com/fibonacci-chain/fbc@v0.0.0-20231124064014-c7636198c1e9/x/wasm/types/address_adapter.go (about)

     1  package types
     2  
     3  import (
     4  	"crypto/sha256"
     5  
     6  	sdk "github.com/fibonacci-chain/fbc/libs/cosmos-sdk/types"
     7  	"github.com/fibonacci-chain/fbc/libs/cosmos-sdk/types/errors"
     8  )
     9  
    10  // Module is a specialized version of a composed address for modules. Each module account
    11  // is constructed from a module name and module account key.
    12  func Module(moduleName string, key []byte) []byte {
    13  	mKey := append([]byte(moduleName), 0)
    14  
    15  	return hash("module", append(mKey, key...))
    16  }
    17  
    18  // Hash creates a new address from address type and key
    19  func hash(typ string, key []byte) []byte {
    20  	hasher := sha256.New()
    21  	_, err := hasher.Write(sdk.UnsafeStrToBytes(typ))
    22  	// the error always nil, it's here only to satisfy the io.Writer interface
    23  	errors.AssertNil(err)
    24  	th := hasher.Sum(nil)
    25  
    26  	hasher.Reset()
    27  	_, err = hasher.Write(th)
    28  	errors.AssertNil(err)
    29  	_, err = hasher.Write(key)
    30  	errors.AssertNil(err)
    31  	return hasher.Sum(nil)
    32  }