The core protocol of WoopChain
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
woop/crypto/pki/utils.go

51 lines
1.3 KiB

package pki
import (
"crypto/sha256"
6 years ago
"encoding/binary"
6 years ago
"github.com/harmony-one/bls/ffi/go/bls"
)
func init() {
bls.Init(bls.BLS12_381)
}
6 years ago
// GetAddressFromPublicKey returns address given a public key.
6 years ago
func GetAddressFromPublicKey(pubKey *bls.PublicKey) [20]byte {
bytes := pubKey.Serialize()
address := [20]byte{}
hash := sha256.Sum256(bytes)
copy(address[:], hash[12:])
return address
}
6 years ago
// GetAddressFromPrivateKey returns address given a private key.
6 years ago
func GetAddressFromPrivateKey(priKey *bls.SecretKey) [20]byte {
return GetAddressFromPublicKey(priKey.GetPublicKey())
}
6 years ago
// GetAddressFromPrivateKeyBytes returns address from private key in bytes.
func GetAddressFromPrivateKeyBytes(priKey [32]byte) [20]byte {
6 years ago
var privateKey bls.SecretKey
privateKey.SetLittleEndian(priKey[:])
return GetAddressFromPublicKey(privateKey.GetPublicKey())
}
6 years ago
// GetAddressFromInt is the temporary helper function for benchmark use
func GetAddressFromInt(value int) [20]byte {
6 years ago
priKey := [32]byte{}
binary.LittleEndian.PutUint32(priKey[:], uint32(value))
return GetAddressFromPrivateKeyBytes(priKey)
}
6 years ago
// GetBLSPrivateKeyFromInt returns bls private key
func GetBLSPrivateKeyFromInt(value int) *bls.SecretKey {
priKey := [32]byte{}
binary.LittleEndian.PutUint32(priKey[:], uint32(value))
var privateKey bls.SecretKey
privateKey.SetLittleEndian(priKey[:])
return &privateKey
}