-
Notifications
You must be signed in to change notification settings - Fork 82
/
truekey.go
137 lines (119 loc) · 3.46 KB
/
truekey.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
package crypto
import (
"bytes"
"crypto/ecdsa"
"fmt"
tcrypyo "github.com/ethereum/go-ethereum/crypto"
"github.com/tendermint/go-amino"
"github.com/truechain/truechain-engineering-code/consensus/tbft/help"
)
//-------------------------------------
const (
EcdsaPrivKeyAminoRoute = "true/PrivKeyTrue"
EcdsaPubKeyAminoRoute = "true/PubKeyTrue"
// Size of an Edwards25519 signature. Namely the size of a compressed
// Edwards25519 point, and a field element. Both of which are 32 bytes.
SignatureEd25519Size = 64
)
var cdc = amino.NewCodec()
func init() {
cdc.RegisterInterface((*PubKey)(nil), nil)
cdc.RegisterConcrete(PubKeyTrue{},
EcdsaPubKeyAminoRoute, nil)
cdc.RegisterInterface((*PrivKey)(nil), nil)
cdc.RegisterConcrete(PrivKeyTrue{},
EcdsaPrivKeyAminoRoute, nil)
}
// PrivKeyTrue implements PrivKey.
type PrivKeyTrue ecdsa.PrivateKey
// Bytes marshals the privkey using amino encoding.
func (priv PrivKeyTrue) Bytes() []byte {
return cdc.MustMarshalBinaryBare(priv)
}
// Sign produces a signature on the provided message.
func (priv PrivKeyTrue) Sign(msg []byte) ([]byte, error) {
priv1 := ecdsa.PrivateKey(priv)
return tcrypyo.Sign(msg, &priv1)
}
// PubKey gets the corresponding public key from the private key.
func (priv PrivKeyTrue) PubKey() PubKey {
priv1 := ecdsa.PrivateKey(priv)
pub0, ok := priv1.Public().(*ecdsa.PublicKey)
if !ok {
panic(0)
}
pub := PubKeyTrue(*pub0)
return &pub
}
// Equals - you probably don't need to use this.
// Runs in constant time based on length of the keys.
func (priv PrivKeyTrue) Equals(other PrivKey) bool {
if otherEd, ok := other.(PrivKeyTrue); ok {
priv0 := ecdsa.PrivateKey(otherEd)
data0 := tcrypyo.FromECDSA(&priv0)
priv1 := ecdsa.PrivateKey(priv)
data1 := tcrypyo.FromECDSA(&priv1)
return bytes.Equal(data0[:], data1[:])
}
return false
}
// GenPrivKey generates a new ed25519 private key.
func GenPrivKey() PrivKeyTrue {
priv, err := tcrypyo.GenerateKey()
if err != nil {
panic(err)
}
privKey := PrivKeyTrue(*priv)
return privKey
}
//-------------------------------------
// PubKeyTrue implements PubKey for the ecdsa.PublicKey signature scheme.
type PubKeyTrue ecdsa.PublicKey
// Address is the Keccak256 of the raw pubkey bytes.
func (pub PubKeyTrue) Address() help.Address {
pub1 := ecdsa.PublicKey(pub)
data := tcrypyo.PubkeyToAddress(pub1)
return help.Address(data[:])
}
// Bytes marshals the PubKey using amino encoding.
func (pub PubKeyTrue) Bytes() []byte {
//bz, err := cdc.MarshalBinaryBare(pub)
pub1 := ecdsa.PublicKey(pub)
bz := tcrypyo.FromECDSAPub(&pub1)
//bz := elliptic.Marshal(tcrypyo.S256(), pub.X, pub.Y)
//if err != nil {
// panic(err)
//}
return bz
}
//VerifyBytes is check msg
func (pub PubKeyTrue) VerifyBytes(msg []byte, sig []byte) bool {
// make sure we use the same algorithm to sign
if pub0, err := tcrypyo.SigToPub(msg, sig); err == nil {
pub1 := PubKeyTrue(*pub0)
return pub.Equals(pub1)
}
return false
}
func (pub PubKeyTrue) String() string {
pub1 := ecdsa.PublicKey(pub)
data := tcrypyo.FromECDSAPub(&pub1)
if data == nil {
return ""
}
return fmt.Sprintf("PubKeyTrue{%X}", data[:])
}
// Equals is comp public key
func (pub PubKeyTrue) Equals(other PubKey) bool {
if otherEd, ok := other.(PubKeyTrue); ok {
pub0 := ecdsa.PublicKey(otherEd)
pub1 := ecdsa.PublicKey(pub)
data0 := tcrypyo.FromECDSAPub(&pub0)
data1 := tcrypyo.FromECDSAPub(&pub1)
if data0 == nil || data1 == nil {
return false
}
return bytes.Equal(data0[:], data1[:])
}
return false
}