-
Notifications
You must be signed in to change notification settings - Fork 7
/
libp2p_impls.go
122 lines (111 loc) · 3.19 KB
/
libp2p_impls.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
package crypto
import (
"github.com/decred/dcrd/dcrec/secp256k1/v4"
"github.com/decred/dcrd/dcrec/secp256k1/v4/schnorr"
"github.com/indra-labs/indra/pkg/crypto/sha256"
"github.com/libp2p/go-libp2p/core/crypto"
crypto_pb "github.com/libp2p/go-libp2p/core/crypto/pb"
)
// Equals is an implementation of the libp2p crypto.Key interface, allowing the
// Indra keys to be used by libp2p as peer identity keys.
func (p *Prv) Equals(key crypto.Key) (eq bool) {
var e error
var rawA, rawB []byte
if rawA, e = key.Raw(); fails(e) {
return
}
if rawB, e = p.Raw(); fails(e) {
return
}
if len(rawA) != len(rawB) {
return
}
for i := range rawA {
if rawA[i] != rawB[i] {
for j := range rawA {
rawA[j], rawB[j] = 0, 0
}
return
}
}
return true
}
// GetPublic derives the public key matching a private key, an implementation of
// the libp2p crypto.PrivKey interface, allowing the Indra keys to be used by libp2p
// as peer identity keys.
func (p *Prv) GetPublic() crypto.PubKey {
if p == nil {
return nil
}
return DerivePub(p)
}
// Raw is an implementation of the libp2p crypto.Key interface, allowing the
// Indra keys to be used by libp2p as peer identity keys.
func (p *Prv) Raw() ([]byte, error) {
b := p.ToBytes()
return b[:], nil
}
// Sign is an implementation of the libp2p crypto.PrivKey interface, allowing the
// Indra keys to be used by libp2p as peer identity keys.
func (p *Prv) Sign(bytes []byte) (b []byte, e error) {
hash := sha256.Single(bytes)
//s := ecdsa.Sign((*secp256k1.PrivateKey)(p), hash[:])
var s *schnorr.Signature
s, e = schnorr.Sign((*secp256k1.PrivateKey)(p), hash[:])
var sig SigBytes
copy(sig[:], s.Serialize())
return sig[:], e
}
// Verify is an implementation of the libp2p crypto.PkbKey interface, allowing
// the Indra keys to be used by libp2p as peer identity keys.
//
// The output of Sign above and the bytes of the message are the required inputs.
func (k *Pub) Verify(data []byte, sigBytes []byte) (is bool,
e error) {
var sig *schnorr.Signature
if sig, e = schnorr.ParseSignature(sigBytes); fails(e) {
return
}
sigB := sha256.Single(data)
is = sig.Verify(sigB[:], (*secp256k1.PublicKey)(k))
return
}
// Type is an implementation of the libp2p crypto.Key interface, allowing the
// Indra keys to be used by libp2p as peer identity keys.
func (p *Prv) Type() crypto_pb.KeyType {
return crypto_pb.KeyType_Secp256k1
}
// Equals compares two public keys and returns true if they match.
func (k *Pub) Equals(key crypto.Key) (eq bool) {
var e error
var rawA, rawB []byte
if rawA, e = key.Raw(); fails(e) {
return
}
if rawB, e = k.Raw(); fails(e) {
return
}
if len(rawA) != len(rawB) {
return
}
for i := range rawA {
if rawA[i] != rawB[i] {
for j := range rawA {
rawA[j], rawB[j] = 0, 0
}
return
}
}
return true
}
// Raw is an implementation of the libp2p crypto.Key interface, allowing the
// Indra keys to be used by libp2p as peer identity keys.
func (k *Pub) Raw() ([]byte, error) {
b := k.ToBytes()
return b[:], nil
}
// Type is an implementation of the libp2p crypto.Key interface, allowing the
// Indra keys to be used by libp2p as peer identity keys.
func (k *Pub) Type() crypto_pb.KeyType {
return crypto_pb.KeyType_Secp256k1
}