-
Notifications
You must be signed in to change notification settings - Fork 1.3k
/
Copy pathinit.go
79 lines (62 loc) · 1.87 KB
/
init.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
package delegated
import (
"fmt"
"golang.org/x/crypto/sha3"
"github.com/filecoin-project/go-address"
gocrypto "github.com/filecoin-project/go-crypto"
"github.com/filecoin-project/go-state-types/builtin"
crypto1 "github.com/filecoin-project/go-state-types/crypto"
"github.com/filecoin-project/lotus/lib/sigs"
)
type delegatedSigner struct{}
func (delegatedSigner) GenPrivate() ([]byte, error) {
priv, err := gocrypto.GenerateKey()
if err != nil {
return nil, err
}
return priv, nil
}
func (delegatedSigner) ToPublic(pk []byte) ([]byte, error) {
return gocrypto.PublicKey(pk), nil
}
func (s delegatedSigner) Sign(pk []byte, msg []byte) ([]byte, error) {
hasher := sha3.NewLegacyKeccak256()
hasher.Write(msg)
hashSum := hasher.Sum(nil)
sig, err := gocrypto.Sign(pk, hashSum)
if err != nil {
return nil, err
}
return sig, nil
}
func (delegatedSigner) Verify(sig []byte, a address.Address, msg []byte) error {
hasher := sha3.NewLegacyKeccak256()
hasher.Write(msg)
hash := hasher.Sum(nil)
pubk, err := gocrypto.EcRecover(hash, sig)
if err != nil {
return err
}
// if we get an uncompressed public key (that's what we get from the library,
// but putting this check here for defensiveness), strip the prefix
if pubk[0] == 0x04 {
pubk = pubk[1:]
}
hasher.Reset()
hasher.Write(pubk)
addrHash := hasher.Sum(nil)
// The address hash will not start with [12]byte{0xff}, so we don't have to use
// EthAddr.ToFilecoinAddress() to handle the case with an id address
// Also, importing ethtypes here will cause circulating import
maybeaddr, err := address.NewDelegatedAddress(builtin.EthereumAddressManagerActorID, addrHash[12:])
if err != nil {
return err
}
if maybeaddr != a {
return fmt.Errorf("signature did not match maybeaddr: %s, signer: %s", maybeaddr, a)
}
return nil
}
func init() {
sigs.RegisterSignature(crypto1.SigTypeDelegated, delegatedSigner{})
}