/
wallet.go
51 lines (41 loc) · 1.12 KB
/
wallet.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
package tx
import (
"crypto/ecdsa"
"github.com/ethereum/go-ethereum/common"
"github.com/ethereum/go-ethereum/core/types"
"github.com/ethereum/go-ethereum/crypto"
stderr "github.com/pkg/errors"
"github.com/oasislabs/oasis-gateway/errors"
)
// Wallet is an interface for any type that signs transactions
// and receives responses
type Wallet interface {
Address() common.Address
SignTransaction(tx *types.Transaction) (*types.Transaction, errors.Err)
}
type InternalWallet struct {
privateKey *ecdsa.PrivateKey
signer types.Signer
}
func NewWallet(
privateKey *ecdsa.PrivateKey,
signer types.Signer,
) *InternalWallet {
w := &InternalWallet{
privateKey: privateKey,
signer: signer,
}
return w
}
func (w *InternalWallet) Address() common.Address {
return crypto.PubkeyToAddress(w.privateKey.PublicKey)
}
func (w *InternalWallet) SignTransaction(tx *types.Transaction) (*types.Transaction, errors.Err) {
var err error
tx, err = types.SignTx(tx, w.signer, w.privateKey)
if err != nil {
err := errors.New(errors.ErrSignedTx, stderr.Wrap(err, "Failed to sign transaction"))
return nil, err
}
return tx, nil
}