-
Notifications
You must be signed in to change notification settings - Fork 12
/
signer.go
64 lines (51 loc) · 1.61 KB
/
signer.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
package helpers
import (
"fmt"
"github.com/cosmos/cosmos-sdk/crypto/keyring"
cryptotypes "github.com/cosmos/cosmos-sdk/crypto/types"
sdk "github.com/cosmos/cosmos-sdk/types"
"github.com/ethereum/go-ethereum/common"
"github.com/evmos/ethermint/crypto/ethsecp256k1"
)
var _ keyring.Signer = &Signer{}
// Signer defines a type that is used on testing for signing MsgEthereumTx
type Signer struct {
privKey cryptotypes.PrivKey
}
func NewSigner(sk cryptotypes.PrivKey) *Signer {
return &Signer{
privKey: sk,
}
}
func (s Signer) PrivKey() cryptotypes.PrivKey {
return s.privKey
}
func (s Signer) Address() common.Address {
return common.BytesToAddress(s.privKey.PubKey().Address())
}
func (s Signer) AccAddress() sdk.AccAddress {
return s.privKey.PubKey().Address().Bytes()
}
// Sign signs the message using the underlying private key
func (s Signer) Sign(_ string, msg []byte) ([]byte, cryptotypes.PubKey, error) {
if s.privKey.Type() != ethsecp256k1.KeyType {
return nil, nil, fmt.Errorf(
"invalid private key type for signing ethereum tx; expected %s, got %s",
ethsecp256k1.KeyType,
s.privKey.Type(),
)
}
sig, err := s.privKey.Sign(msg)
if err != nil {
return nil, nil, err
}
return sig, s.privKey.PubKey(), nil
}
// SignByAddress sign byte messages with a user key providing the address.
func (s Signer) SignByAddress(address sdk.Address, msg []byte) ([]byte, cryptotypes.PubKey, error) {
signer := sdk.AccAddress(s.privKey.PubKey().Address())
if !signer.Equals(address) {
return nil, nil, fmt.Errorf("address mismatch: signer %s ≠ given address %s", signer, address)
}
return s.Sign("", msg)
}