tendermint/account/priv_account.go

62 lines
1.7 KiB
Go
Raw Normal View History

2014-12-17 01:37:13 -08:00
package account
import (
2015-06-09 23:17:19 -04:00
"github.com/tendermint/tendermint/Godeps/_workspace/src/github.com/tendermint/ed25519"
"github.com/tendermint/tendermint/binary"
2015-04-01 17:30:16 -07:00
. "github.com/tendermint/tendermint/common"
)
type PrivAccount struct {
2015-05-01 17:26:49 -07:00
Address []byte `json:"address"`
PubKey PubKey `json:"pub_key"`
PrivKey PrivKey `json:"priv_key"`
}
// Generates a new account with private key.
func GenPrivAccount() *PrivAccount {
privKeyBytes := new([64]byte)
copy(privKeyBytes[:32], CRandBytes(32))
pubKeyBytes := ed25519.MakePublicKey(privKeyBytes)
pubKey := PubKeyEd25519(pubKeyBytes[:])
privKey := PrivKeyEd25519(privKeyBytes[:])
return &PrivAccount{
Address: pubKey.Address(),
PubKey: pubKey,
2015-01-13 21:03:01 -08:00
PrivKey: privKey,
}
}
2014-12-17 01:37:13 -08:00
// Generates a new account with private key from SHA256 hash of a secret
func GenPrivAccountFromSecret(secret []byte) *PrivAccount {
2015-07-10 12:15:46 -07:00
privKey32 := binary.BinarySha256(secret) // Not Ripemd160 because we want 32 bytes.
privKeyBytes := new([64]byte)
copy(privKeyBytes[:32], privKey32)
pubKeyBytes := ed25519.MakePublicKey(privKeyBytes)
pubKey := PubKeyEd25519(pubKeyBytes[:])
privKey := PrivKeyEd25519(privKeyBytes[:])
return &PrivAccount{
Address: pubKey.Address(),
PubKey: pubKey,
PrivKey: privKey,
}
}
func GenPrivAccountFromKey(privKeyBytes [64]byte) *PrivAccount {
pubKeyBytes := ed25519.MakePublicKey(&privKeyBytes)
pubKey := PubKeyEd25519(pubKeyBytes[:])
privKey := PrivKeyEd25519(privKeyBytes[:])
return &PrivAccount{
Address: pubKey.Address(),
PubKey: pubKey,
PrivKey: privKey,
}
}
2015-05-29 17:53:57 -04:00
func (privAccount *PrivAccount) Sign(chainID string, o Signable) Signature {
return privAccount.PrivKey.Sign(SignBytes(chainID, o))
2014-12-17 01:37:13 -08:00
}
2015-01-16 00:31:34 -08:00
func (privAccount *PrivAccount) String() string {
return Fmt("PrivAccount{%X}", privAccount.Address)
}