-
Notifications
You must be signed in to change notification settings - Fork 462
/
signer.go
65 lines (54 loc) · 2.02 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
package state
import (
"context"
"github.com/filecoin-project/go-address"
"github.com/filecoin-project/venus/pkg/crypto"
"github.com/filecoin-project/venus/pkg/wallet"
"github.com/filecoin-project/venus/venus-shared/types"
)
// todo remove Account view a nd headsignerview
type AccountView interface {
ResolveToDeterministicAddress(ctx context.Context, address address.Address) (address.Address, error)
}
type tipSignerView interface {
GetHead() *types.TipSet
ResolveToDeterministicAddress(ctx context.Context, ts *types.TipSet, address address.Address) (address.Address, error)
}
// Signer looks up non-signing addresses before signing
type Signer struct {
wallet *wallet.Wallet
signerView AccountView
}
// NewSigner creates a new signer
func NewSigner(signerView AccountView, wallet *wallet.Wallet) *Signer {
return &Signer{
signerView: signerView,
wallet: wallet,
}
}
// SignBytes creates a signature for the given data using either the given addr or its associated signing address
func (s *Signer) SignBytes(ctx context.Context, data []byte, addr address.Address) (*crypto.Signature, error) {
signingAddr, err := s.signerView.ResolveToDeterministicAddress(ctx, addr)
if err != nil {
return nil, err
}
return s.wallet.SignBytes(ctx, data, signingAddr)
}
// HasAddress returns whether this signer can sign with the given address
func (s *Signer) HasAddress(ctx context.Context, addr address.Address) (bool, error) {
signingAddr, err := s.signerView.ResolveToDeterministicAddress(ctx, addr)
if err != nil {
return false, err
}
return s.wallet.HasAddress(ctx, signingAddr), nil
}
type HeadSignView struct {
tipSignerView
}
func NewHeadSignView(tipSignerView tipSignerView) *HeadSignView {
return &HeadSignView{tipSignerView: tipSignerView}
}
func (headSignView *HeadSignView) ResolveToDeterministicAddress(ctx context.Context, addr address.Address) (address.Address, error) {
head := headSignView.GetHead()
return headSignView.tipSignerView.ResolveToDeterministicAddress(ctx, head, addr) // nil will use latest
}