-
Notifications
You must be signed in to change notification settings - Fork 342
/
Copy pathsender_matcher.go
52 lines (41 loc) · 1.25 KB
/
sender_matcher.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
package transaction
import (
"context"
"errors"
"fmt"
"github.com/ethereum/go-ethereum/common"
"github.com/ethereum/go-ethereum/core/types"
"github.com/ethersphere/bee/pkg/crypto"
"github.com/ethersphere/bee/pkg/swarm"
)
type Matcher struct {
backend Backend
signer types.Signer
}
var (
ErrTransactionNotFound = errors.New("transaction not found")
ErrTransactionPending = errors.New("transaction in pending status")
ErrTransactionSender = errors.New("transaction sender")
)
func NewMatcher(backend Backend, signer types.Signer) *Matcher {
return &Matcher{
backend: backend,
signer: signer,
}
}
func (m Matcher) Matches(ctx context.Context, tx string, networkID uint64, senderOverlay swarm.Address) (bool, error) {
incomingTx := common.HexToHash(tx)
nTx, isPending, err := m.backend.TransactionByHash(ctx, incomingTx)
if err != nil {
return false, fmt.Errorf("%v: %w", err, ErrTransactionNotFound)
}
if isPending {
return false, ErrTransactionPending
}
sender, err := types.Sender(m.signer, nTx)
if err != nil {
return false, fmt.Errorf("%v: %w", err, ErrTransactionSender)
}
expectedRemoteBzzAddress := crypto.NewOverlayFromEthereumAddress(sender.Bytes(), networkID)
return expectedRemoteBzzAddress.Equal(senderOverlay), nil
}