/
public_key.go
116 lines (95 loc) · 2.27 KB
/
public_key.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
package key
import (
"crypto/ed25519"
"encoding/hex"
"encoding/json"
"fmt"
"github.com/mr-tron/base58"
"github.com/eteu-technologies/near-api-go/pkg/types/signature"
)
// TODO: SECP256K1
type PublicKey [33]byte
func (p PublicKey) Hash() string {
return hex.EncodeToString(p[1:])
}
func (p PublicKey) TypeByte() byte {
return p[0]
}
func (p PublicKey) Value() []byte {
return p[1:]
}
func (p PublicKey) MarshalJSON() ([]byte, error) {
return json.Marshal(base58.Encode(p[:]))
}
func (p *PublicKey) UnmarshalJSON(b []byte) error {
var s string
if err := json.Unmarshal(b, &s); err != nil {
return err
}
dec, err := base58.Decode(s)
if err != nil {
return err
}
*p = PublicKey{}
copy(p[:], dec)
return nil
}
func (p *PublicKey) Verify(data []byte, signature signature.Signature) (ok bool, err error) {
keyType := p.TypeByte()
if signature.Type() != keyType {
return false, fmt.Errorf("cannot verify signature type %d with key type %d", signature.Type(), p.TypeByte())
}
switch keyType {
case RawKeyTypeED25519:
ok = ed25519.Verify(ed25519.PublicKey(p.Value()), data, signature.Value())
case RawKeyTypeSECP256K1:
// TODO!
return false, fmt.Errorf("SECP256K1 is not supported yet")
}
return
}
func (p *PublicKey) ToBase58PublicKey() Base58PublicKey {
return Base58PublicKey{
Type: keyTypes[p[0]],
Value: base58.Encode(p[1:]),
pk: *p,
}
}
func PublicKeyFromBytes(b []byte) (pk PublicKey, err error) {
f := b[0]
l := len(b) - 1
switch f {
case RawKeyTypeED25519:
if l != ed25519.PublicKeySize {
return pk, ErrInvalidPublicKey
}
copy(pk[:], b)
return
case RawKeyTypeSECP256K1:
// TODO!
return pk, fmt.Errorf("SECP256K1 is not supported yet")
}
return pk, ErrInvalidKeyType
}
func WrapRawKey(keyType PublicKeyType, key []byte) (pk PublicKey, err error) {
switch keyType {
case KeyTypeED25519:
if len(key) != ed25519.PublicKeySize {
return pk, ErrInvalidPublicKey
}
pk[0] = RawKeyTypeED25519
copy(pk[1:], key[0:ed25519.PublicKeySize])
return
case KeyTypeSECP256K1:
// TODO!
return pk, fmt.Errorf("SECP256K1 is not supported yet")
}
return pk, ErrInvalidKeyType
}
func WrapED25519(key ed25519.PublicKey) PublicKey {
if pk, err := WrapRawKey(KeyTypeED25519, key); err != nil {
panic(err)
} else {
return pk
}
}