/
signer.go
72 lines (59 loc) · 1.46 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
package relayer
import (
"encoding/json"
"os"
"path/filepath"
"io/ioutil"
"github.com/ethereum/go-ethereum/accounts/abi/bind"
ethereum "github.com/ethereum/go-ethereum/common"
"github.com/ethereum/go-ethereum/core/types"
)
type Signer struct {
Keystore string `json:"keystore_path"`
Passphrase string `json:"passphrase"`
opts *bind.TransactOpts
}
func (self *Signer) GetTransactOpts() *bind.TransactOpts {
return self.opts
}
func (self *Signer) GetAddress() ethereum.Address {
return self.opts.From
}
func (self *Signer) Sign(tx *types.Transaction) (*types.Transaction, error) {
return self.opts.Signer(types.HomesteadSigner{}, self.GetAddress(), tx)
}
func NewSignerFile(file string, fileLocation string) *Signer {
raw, err := ioutil.ReadFile(file)
if err != nil {
panic(err)
}
signer := &Signer{}
err = json.Unmarshal(raw, signer)
if err != nil {
panic(err)
}
keyio, err := os.Open(filepath.Join(fileLocation, signer.Keystore))
if err != nil {
panic(err)
}
logger.Debug("keyio: ", keyio)
auth, err := bind.NewTransactor(keyio, signer.Passphrase)
if err != nil {
panic(err)
}
logger.Debug("auth: ", auth.From.Hex())
signer.opts = auth
return signer
}
func NewSigner() *Signer {
signer := &Signer{}
passParser, keyio := GetKeyStore()
signer.Passphrase = passParser
auth, err := bind.NewTransactor(keyio, signer.Passphrase)
if err != nil {
panic(err)
}
logger.Debug("auth: ", auth.From.Hex())
signer.opts = auth
return signer
}