forked from EscanBE/evermint
-
Notifications
You must be signed in to change notification settings - Fork 0
/
signer.go
50 lines (41 loc) · 1.28 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
package types
import (
"fmt"
"github.com/servprotocolorg/serv/v12/crypto/ethsecp256k1"
"github.com/cosmos/cosmos-sdk/crypto/keyring"
cryptotypes "github.com/cosmos/cosmos-sdk/crypto/types"
sdk "github.com/cosmos/cosmos-sdk/types"
)
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) keyring.Signer {
if sk.Type() != ethsecp256k1.KeyType {
panic(fmt.Sprintf(
"require key type %s, got %s",
ethsecp256k1.KeyType,
sk.Type(),
))
}
return &signer{
privKey: sk,
}
}
// Sign signs the message using the underlying private key
func (s signer) Sign(_ string, msg []byte) ([]byte, cryptotypes.PubKey, error) {
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) {
signerAddress := sdk.AccAddress(s.privKey.PubKey().Address())
if !signerAddress.Equals(address) {
return nil, nil, fmt.Errorf("address mismatch: signer %s ≠ given address %s", signerAddress, address)
}
return s.Sign("", msg)
}