-
Notifications
You must be signed in to change notification settings - Fork 2
/
signature.go
108 lines (86 loc) · 2.66 KB
/
signature.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
package crypto
import (
"errors"
"fmt"
"idp/models/resolver"
"log"
"strings"
"github.com/ethereum/go-ethereum/common"
"github.com/ethereum/go-ethereum/common/hexutil"
"github.com/ethereum/go-ethereum/crypto"
)
func signHash(data []byte) []byte {
msg := fmt.Sprintf("\x19Ethereum Signed Message:\n%d%s", len(data), data)
log.Println(msg)
return crypto.Keccak256([]byte(msg))
}
// EcRecover returns the address for the Account that was used to create the signature.
// func EcRecover(msg, sig string) (addr string, err error) {
func EcRecover(msg, sigHex string) (addr string, err error) {
defer func() {
if r := recover(); r != nil {
addr = common.Address{}.String()
err = r.(error)
}
}()
data := []byte(msg)
hash := signHash(data)
sig := hexutil.MustDecode(sigHex)
// https://github.com/ethereum/go-ethereum/blob/55599ee95d4151a2502465e0afc7c47bd1acba77/internal/ethapi/api.go#L442
if sig[64] != 27 && sig[64] != 28 {
return common.Address{}.String(), errors.New("nvalid Ethereum signature (V is not 27 or 28)")
}
sig[64] -= 27
sigPiblicKey, err := crypto.SigToPub(hash, sig)
if err != nil {
fmt.Println("ECRECOVER" + err.Error())
panic(err)
}
commAddr := crypto.PubkeyToAddress(*sigPiblicKey)
return commAddr.String(), nil
}
func SigPublicKey(msg, sigHex string) (publickey string, err error) {
data := []byte(msg)
hash := signHash(data)
sig := hexutil.MustDecode(sigHex)
// https://github.com/ethereum/go-ethereum/blob/55599ee95d4151a2502465e0afc7c47bd1acba77/internal/ethapi/api.go#L442
if sig[64] != 27 && sig[64] != 28 {
return common.Address{}.String(), errors.New("nvalid Ethereum signature (V is not 27 or 28)")
}
sig[64] -= 27
sigPiblicKey, err := crypto.SigToPub(hash, sig)
if err != nil {
fmt.Println("ECRECOVER" + err.Error())
return "", err
}
publickeyBytes := crypto.FromECDSAPub(sigPiblicKey)
publickey = hexutil.Encode(publickeyBytes)
return publickey, nil
}
func VerifyAuth(msg, id, sig string) (info string, err error) {
r, err := resolver.NewResolver("infuraRopsten", "0x1DbF8e4B47EA53a2b932850F7FEC8585C6A9c1d2")
owner, err := r.IdentityOwner(id)
if err != nil {
log.Println(err)
return "", err
}
publickey, err := SigPublicKey(msg, sig)
if err != nil {
log.Println(err)
return "", err
}
ok, err := r.ValidAuthentication(id, "sigAuth", publickey)
addr, err := EcRecover(msg, sig)
if err != nil {
log.Println(err)
return "", err
}
if strings.ToLower(addr) == strings.ToLower(id) {
return "sign by self", nil
} else if strings.ToLower(owner) == strings.ToLower(addr) {
return "sign by did owner", nil
} else if ok {
return "sign with authKey", nil
}
return "", errors.New("verify failed")
}