7 secp256k1 "github.com/btcsuite/btcd/btcec"
8 "github.com/tendermint/ed25519"
9 "github.com/tendermint/ed25519/extra25519"
10 "github.com/tendermint/go-wire"
11 data "github.com/tendermint/go-wire/data"
12 . "github.com/tendermint/tmlibs/common"
13 "golang.org/x/crypto/ripemd160"
16 func PubKeyFromBytes(pubKeyBytes []byte) (pubKey PubKey, err error) {
17 err = wire.ReadBinaryBytes(pubKeyBytes, &pubKey)
21 //----------------------------------------
23 // DO NOT USE THIS INTERFACE.
24 // You probably want to use PubKey
25 // +gen wrapper:"PubKey,Impl[PubKeyEd25519,PubKeySecp256k1],ed25519,secp256k1"
26 type PubKeyInner interface {
31 VerifyBytes(msg []byte, sig Signature) bool
36 //-------------------------------------
38 var _ PubKeyInner = PubKeyEd25519{}
40 // Implements PubKeyInner
41 type PubKeyEd25519 [32]byte
43 func (pubKey PubKeyEd25519) AssertIsPubKeyInner() {}
45 func (pubKey PubKeyEd25519) Address() []byte {
46 w, n, err := new(bytes.Buffer), new(int), new(error)
47 wire.WriteBinary(pubKey[:], w, n, err)
52 encodedPubkey := append([]byte{TypeEd25519}, w.Bytes()...)
53 hasher := ripemd160.New()
54 hasher.Write(encodedPubkey) // does not error
55 return hasher.Sum(nil)
58 func (pubKey PubKeyEd25519) Bytes() []byte {
59 return wire.BinaryBytes(PubKey{pubKey})
62 func (pubKey PubKeyEd25519) VerifyBytes(msg []byte, sig_ Signature) bool {
63 // make sure we use the same algorithm to sign
64 sig, ok := sig_.Unwrap().(SignatureEd25519)
68 pubKeyBytes := [32]byte(pubKey)
69 sigBytes := [64]byte(sig)
70 return ed25519.Verify(&pubKeyBytes, msg, &sigBytes)
73 func (p PubKeyEd25519) MarshalJSON() ([]byte, error) {
74 return data.Encoder.Marshal(p[:])
77 func (p *PubKeyEd25519) UnmarshalJSON(enc []byte) error {
79 err := data.Encoder.Unmarshal(&ref, enc)
84 // For use with golang/crypto/nacl/box
85 // If error, returns nil.
86 func (pubKey PubKeyEd25519) ToCurve25519() *[32]byte {
87 keyCurve25519, pubKeyBytes := new([32]byte), [32]byte(pubKey)
88 ok := extra25519.PublicKeyToCurve25519(keyCurve25519, &pubKeyBytes)
95 func (pubKey PubKeyEd25519) String() string {
96 return Fmt("PubKeyEd25519{%X}", pubKey[:])
99 // Must return the full bytes in hex.
100 // Used for map keying, etc.
101 func (pubKey PubKeyEd25519) KeyString() string {
102 return Fmt("%X", pubKey[:])
105 func (pubKey PubKeyEd25519) Equals(other PubKey) bool {
106 if otherEd, ok := other.Unwrap().(PubKeyEd25519); ok {
107 return bytes.Equal(pubKey[:], otherEd[:])
113 //-------------------------------------
115 var _ PubKeyInner = PubKeySecp256k1{}
117 // Implements PubKey.
118 // Compressed pubkey (just the x-cord),
119 // prefixed with 0x02 or 0x03, depending on the y-cord.
120 type PubKeySecp256k1 [33]byte
122 func (pubKey PubKeySecp256k1) AssertIsPubKeyInner() {}
124 // Implements Bitcoin style addresses: RIPEMD160(SHA256(pubkey))
125 func (pubKey PubKeySecp256k1) Address() []byte {
126 hasherSHA256 := sha256.New()
127 hasherSHA256.Write(pubKey[:]) // does not error
128 sha := hasherSHA256.Sum(nil)
130 hasherRIPEMD160 := ripemd160.New()
131 hasherRIPEMD160.Write(sha) // does not error
132 return hasherRIPEMD160.Sum(nil)
135 func (pubKey PubKeySecp256k1) Bytes() []byte {
136 return wire.BinaryBytes(PubKey{pubKey})
139 func (pubKey PubKeySecp256k1) VerifyBytes(msg []byte, sig_ Signature) bool {
140 // and assert same algorithm to sign and verify
141 sig, ok := sig_.Unwrap().(SignatureSecp256k1)
146 pub__, err := secp256k1.ParsePubKey(pubKey[:], secp256k1.S256())
150 sig__, err := secp256k1.ParseDERSignature(sig[:], secp256k1.S256())
154 return sig__.Verify(Sha256(msg), pub__)
157 func (p PubKeySecp256k1) MarshalJSON() ([]byte, error) {
158 return data.Encoder.Marshal(p[:])
161 func (p *PubKeySecp256k1) UnmarshalJSON(enc []byte) error {
163 err := data.Encoder.Unmarshal(&ref, enc)
168 func (pubKey PubKeySecp256k1) String() string {
169 return Fmt("PubKeySecp256k1{%X}", pubKey[:])
172 // Must return the full bytes in hex.
173 // Used for map keying, etc.
174 func (pubKey PubKeySecp256k1) KeyString() string {
175 return Fmt("%X", pubKey[:])
178 func (pubKey PubKeySecp256k1) Equals(other PubKey) bool {
179 if otherSecp, ok := other.Unwrap().(PubKeySecp256k1); ok {
180 return bytes.Equal(pubKey[:], otherSecp[:])