-
Notifications
You must be signed in to change notification settings - Fork 1
/
utxo_from.go
67 lines (55 loc) · 1.75 KB
/
utxo_from.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
package gobtcsign
import (
"github.com/btcsuite/btcd/btcutil"
"github.com/btcsuite/btcd/rpcclient"
"github.com/btcsuite/btcd/wire"
"github.com/pkg/errors"
)
type GetUtxoFromInterface interface {
GetUtxoFrom(utxo wire.OutPoint) (*SenderAmountUtxo, error)
}
type SenderAmountUtxoClient struct {
client *rpcclient.Client
}
func NewSenderAmountUtxoClient(client *rpcclient.Client) *SenderAmountUtxoClient {
return &SenderAmountUtxoClient{client: client}
}
func (uc *SenderAmountUtxoClient) GetUtxoFrom(utxo wire.OutPoint) (*SenderAmountUtxo, error) {
previousUtxoTx, err := GetRawTransaction(uc.client, utxo.Hash.String())
if err != nil {
return nil, errors.WithMessage(err, "get-raw-transaction")
}
previousOutput := previousUtxoTx.Vout[utxo.Index]
previousAmount, err := btcutil.NewAmount(previousOutput.Value)
if err != nil {
return nil, errors.WithMessage(err, "get-previous-amount")
}
utxoFrom := NewSenderAmountUtxo(
NewAddressTuple(previousOutput.ScriptPubKey.Address),
int64(previousAmount),
)
return utxoFrom, nil
}
type SenderAmountUtxo struct {
sender *AddressTuple
amount int64
}
func NewSenderAmountUtxo(sender *AddressTuple, amount int64) *SenderAmountUtxo {
return &SenderAmountUtxo{
sender: sender,
amount: amount,
}
}
type SenderAmountUtxoCache struct {
outputUtxoMap map[wire.OutPoint]*SenderAmountUtxo
}
func NewSenderAmountUtxoCache(utxoMap map[wire.OutPoint]*SenderAmountUtxo) *SenderAmountUtxoCache {
return &SenderAmountUtxoCache{outputUtxoMap: utxoMap}
}
func (uc SenderAmountUtxoCache) GetUtxoFrom(utxo wire.OutPoint) (*SenderAmountUtxo, error) {
utxoFrom, ok := uc.outputUtxoMap[utxo]
if !ok {
return nil, errors.Errorf("wrong utxo[%s:%d] not-exist-in-cache", utxo.Hash.String(), utxo.Index)
}
return utxoFrom, nil
}