-
Notifications
You must be signed in to change notification settings - Fork 3
/
Copy pathmain.go
72 lines (58 loc) · 1.82 KB
/
main.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
package main
import (
"crypto/ecdsa"
"fmt"
"os"
"github.com/ethereum/go-ethereum/common/hexutil"
"github.com/ethereum/go-ethereum/crypto"
)
const (
SIGN_PAYLOAD = "Test123123!"
)
func main() {
// Generate secret key instance.
sk, err := crypto.GenerateKey()
if err != nil {
panic(err)
}
fmt.Printf("Generated secret key: %s\n", hexutil.Encode(crypto.FromECDSA(sk)))
fmt.Printf("Sign payload: %s\n", SIGN_PAYLOAD)
sign, err := signPersonal([]byte(SIGN_PAYLOAD), sk)
if err != nil {
panic(err)
}
fmt.Printf("Signature: %s\n\n", hexutil.Encode(sign))
// Try to recover this.
pk_recovered, err := recover(sign, []byte(SIGN_PAYLOAD))
if err != nil {
panic(err)
}
fmt.Printf("Original public key: %s\n", hexutil.Encode(crypto.FromECDSAPub(&sk.PublicKey)))
fmt.Printf("Recovered public key: %s\n", hexutil.Encode(crypto.FromECDSAPub(pk_recovered)))
os.Exit(0)
}
// signPersonal signs a payload using given secret key.
func signPersonal(payload []byte, sk *ecdsa.PrivateKey) (signature []byte, err error) {
digest := signPersonalDigest(payload)
signature, err = crypto.Sign(digest, sk)
if err != nil {
return nil, err
}
return signature, nil
}
// signPersonalDigest hashes the given payload with eth.personal.sign struct.
// NOTE: `len(data)` is byte count, not Unicode codepoint count.
// i.e. `len("🐴") == 4`
func signPersonalDigest(data []byte) []byte {
messsage := fmt.Sprintf("\x19Ethereum Signed Message:\n%d%s", len(data), data)
return crypto.Keccak256([]byte(messsage))
}
// recover recovers the public key from the given signature and payload.
func recover(signature, message []byte) (pubkey *ecdsa.PublicKey, err error) {
digest := signPersonalDigest(message)
pubkey_bytes, err := crypto.Ecrecover(digest, signature)
if err != nil {
return nil, err
}
return crypto.UnmarshalPubkey(pubkey_bytes)
}