-
Notifications
You must be signed in to change notification settings - Fork 3
/
signer.go
79 lines (63 loc) · 1.88 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
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
package mock
import (
"crypto/ecdsa"
"math/big"
"github.com/ethereum/go-ethereum/common"
"github.com/ethereum/go-ethereum/core/types"
"github.com/gauss-project/aurorafs/pkg/crypto"
"github.com/gauss-project/aurorafs/pkg/crypto/eip712"
)
type signerMock struct {
signTx func(transaction *types.Transaction, chainID *big.Int) (*types.Transaction, error)
signTypedData func(*eip712.TypedData) ([]byte, error)
ethereumAddress func() (common.Address, error)
}
func (m *signerMock) PrivateKey() *ecdsa.PrivateKey {
panic("implement me")
}
func (m *signerMock) EthereumAddress() (common.Address, error) {
if m.ethereumAddress != nil {
return m.ethereumAddress()
}
return common.Address{}, nil
}
func (*signerMock) Sign(data []byte) ([]byte, error) {
return nil, nil
}
func (m *signerMock) SignTx(transaction *types.Transaction, chainID *big.Int) (*types.Transaction, error) {
return m.signTx(transaction, chainID)
}
func (*signerMock) PublicKey() (*ecdsa.PublicKey, error) {
return nil, nil
}
func (m *signerMock) SignTypedData(d *eip712.TypedData) ([]byte, error) {
return m.signTypedData(d)
}
func New(opts ...Option) crypto.Signer {
mock := new(signerMock)
for _, o := range opts {
o.apply(mock)
}
return mock
}
// Option is the option passed to the mock Chequebook service
type Option interface {
apply(*signerMock)
}
type optionFunc func(*signerMock)
func (f optionFunc) apply(r *signerMock) { f(r) }
func WithSignTxFunc(f func(transaction *types.Transaction, chainID *big.Int) (*types.Transaction, error)) Option {
return optionFunc(func(s *signerMock) {
s.signTx = f
})
}
func WithSignTypedDataFunc(f func(*eip712.TypedData) ([]byte, error)) Option {
return optionFunc(func(s *signerMock) {
s.signTypedData = f
})
}
func WithEthereumAddressFunc(f func() (common.Address, error)) Option {
return optionFunc(func(s *signerMock) {
s.ethereumAddress = f
})
}