/
key.go
201 lines (160 loc) · 5.32 KB
/
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
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
package did
import (
"crypto/ecdsa"
"crypto/ed25519"
"encoding/base64"
"encoding/hex"
"errors"
"strings"
"github.com/ethereum/go-ethereum/common"
"github.com/ethereum/go-ethereum/crypto"
)
// use a private key and a message to create a JWS format signature
func CreateJWSSignature(privKey *ecdsa.PrivateKey, message []byte) (string, error) {
/*
signer, err := gojose.NewSigner(gojose.SigningKey{Algorithm: gojose.ES256, Key: privKey}, nil)
if err != nil {
return "", err
}
c := privKey.PublicKey.Curve
N := c.Params().N
signature, err := signer.Sign(message)
if err != nil {
return "", err
}
sBytes := make([]byte, 32)
copy(sBytes, signature.Signatures[0].Signature[32:])
var s = new(big.Int).SetBytes(sBytes)
m := new(big.Int).Div(N, big.NewInt(2))
q := s.Cmp(m)
if q > 0 || s.Cmp(big.NewInt(1)) < 0 {
sub := new(big.Int).Sub(N, s)
s = new(big.Int).Mod(sub, N)
newBytes := s.Bytes()
sByte := make([]byte, 32)
copy(sByte[32-len(newBytes):], newBytes)
copy(signature.Signatures[0].Signature[32:], sByte)
}
compactserialized, err := signature.DetachedCompactSerialize()
if err != nil {
return "", err
}
*/
header := base64.RawURLEncoding.EncodeToString([]byte(`{"alg":"ES256K-R","b64":false,"crit":["b64"]}`))
// Replace the Signature with the SECP256k1r signature
sig, err := crypto.Sign(message[:], privKey)
if err != nil {
return "", err
}
// Manually calaulate the V byte by adding 27 to the recovery ID
sig[64] += 27
encodedSig := base64.RawURLEncoding.EncodeToString(sig)
compactserialized := header + "." + "." + encodedSig
return compactserialized, nil
}
// verify a JWS format signature using the matching public key and the original message
func VerifyJWSSignature(signature string, expectedFullBlkID string, message []byte) (bool, error) {
partedExpectedBlkID := strings.Split(expectedFullBlkID, ":")
if (len(partedExpectedBlkID) != 3 && len(partedExpectedBlkID) != 2) || partedExpectedBlkID[0] != "eip155" {
return false, ErrInvalidBlockID
}
expectedAddress := partedExpectedBlkID[len(partedExpectedBlkID)-1]
partedSig := strings.Split(signature, ".")
if len(partedSig) != 3 {
return false, ErrInValidSignature
}
sig, err := base64.RawURLEncoding.DecodeString(partedSig[2])
if err != nil {
return false, ErrInValidSignature
}
// Manually calaulate the Recovery ID by subtracting 27 from the V byte
if len(sig) != 65 {
return false, ErrInValidSignature
}
if sig[64] != 0x00 && sig[64] != 0x01 {
sig[64] -= 27
}
recoveredPubKey, err := crypto.SigToPub(message[:], sig)
if err != nil {
return false, ErrInValidSignature
}
recoveredAddress := crypto.PubkeyToAddress(*recoveredPubKey).Hex()
return recoveredAddress == expectedAddress, nil
}
// Function to verify an Ethereum EIP-712 signature
func VerifyEIP712Signature(signature string, expectedFullBlkID string, message []byte) (bool, error) {
partedExpectedBlkID := strings.Split(expectedFullBlkID, ":")
if (len(partedExpectedBlkID) != 3 && len(partedExpectedBlkID) != 2) || partedExpectedBlkID[0] != "eip155" {
return false, ErrInvalidBlockID
}
expectedAddress := partedExpectedBlkID[len(partedExpectedBlkID)-1]
// Parse the signature
sig, err := hex.DecodeString(signature)
if err != nil {
return false, err
}
if len(sig) != 65 {
return false, errors.New("invalid signature length")
}
/*
r := new(big.Int).SetBytes(sig[:32])
s := new(big.Int).SetBytes(sig[32:64])
v := sig[64]
*/
// Recover the public key
recoveredPubKey, err := crypto.SigToPub(message, sig)
if err != nil {
return false, err
}
// Compute the address
recoveredAddress := crypto.PubkeyToAddress(*recoveredPubKey).Hex()
// Compare the addresses
return recoveredAddress == expectedAddress, nil
}
func CreateEIP712Signature(privKey *ecdsa.PrivateKey, typedDataHash common.Hash) (string, error) {
signature, err := crypto.Sign(typedDataHash.Bytes(), privKey)
if err != nil {
return "", err
}
return hex.EncodeToString(signature), nil
}
func VerifyEd25519JWSSignature(signature string, pubKey ed25519.PublicKey, message []byte) (bool, error) {
partedSig := strings.Split(signature, ".")
if len(partedSig) != 3 {
return false, ErrInValidSignature
}
sig, err := base64.RawURLEncoding.DecodeString(partedSig[2])
if err != nil {
return false, ErrInValidSignature
}
return ed25519.Verify(pubKey, message, sig), nil
}
func CreateEd25519JWSSignature(privKey *ed25519.PrivateKey, message []byte) (string, error) {
header := base64.RawURLEncoding.EncodeToString([]byte(`{"alg":"EdDSA","b64":false,"crit":["b64"]}`))
sig := ed25519.Sign(*privKey, message)
encodedSig := base64.RawURLEncoding.EncodeToString(sig)
compactserialized := header + "." + "." + encodedSig
return compactserialized, nil
}
// Function to compare two Ethereum addresses by checking the identifiers and chainIDs
func CompareAddresses(address1 string, address2 string) bool {
// Parse the addresses
parsedAddress1 := strings.Split(address1, ":")
parsedAddress2 := strings.Split(address2, ":")
if len(parsedAddress1) == len(parsedAddress2) {
return address1 == address2
}
// Check the identifiers
if parsedAddress1[0] != parsedAddress2[0] {
return false
}
// Check the chainIDs
if parsedAddress1[1] != parsedAddress2[1] {
return false
}
// Check the addresses
if parsedAddress1[2] != parsedAddress2[2] {
return false
}
return true
}