-
Notifications
You must be signed in to change notification settings - Fork 10
/
ancestors_binary.go
228 lines (209 loc) · 5.68 KB
/
ancestors_binary.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
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
package spv
import (
"context"
"github.com/libsv/go-bk/crypto"
"github.com/libsv/go-bt/v2"
"github.com/libsv/go-bt/v2/bscript"
"github.com/libsv/go-bc"
)
const (
flagTx = byte(1)
flagProof = byte(2)
flagMapi = byte(3)
)
// Ancestry is a payment and its ancestors.
type Ancestry struct {
PaymentTx *bt.Tx
Ancestors map[[32]byte]*Ancestor
}
// Ancestor is an internal struct for validating transactions with their ancestors.
type Ancestor struct {
Tx *bt.Tx
Proof []byte
MapiResponses []*bc.MapiCallback
}
// binaryChunk is a clear way to pass around chunks while keeping their type explicit.
type binaryChunk struct {
ContentType byte
Data []byte
}
type extendedInput struct {
input *bt.Input
vin int
}
// NewAncestryFromBytes creates a new struct from the bytes of a txContext.
func NewAncestryFromBytes(b []byte) (*Ancestry, error) {
if b[0] != 1 { // the first byte is the version number.
return nil, ErrUnsupporredVersion
}
offset := uint64(1)
total := uint64(len(b))
ancestry := &Ancestry{
Ancestors: make(map[[32]byte]*Ancestor),
}
var TxID [32]byte
if total == offset {
return nil, ErrCannotCalculateFeePaid
}
// first Data must be a Tx
if b[offset] != 1 {
return nil, ErrTipTxConfirmed
}
for total > offset {
chunk, size := parseChunk(b, offset)
offset += size
switch chunk.ContentType {
case flagTx:
hash := crypto.Sha256d(chunk.Data)
copy(TxID[:], bt.ReverseBytes(hash)) // fixed size array from slice.
tx, err := bt.NewTxFromBytes(chunk.Data)
if err != nil {
return nil, err
}
if len(tx.Inputs) == 0 {
return nil, ErrNoTxInputsToVerify
}
ancestry.Ancestors[TxID] = &Ancestor{
Tx: tx,
}
case flagProof:
ancestry.Ancestors[TxID].Proof = chunk.Data
case flagMapi:
callBacks, err := parseMapiCallbacks(chunk.Data)
if err != nil {
return nil, err
}
ancestry.Ancestors[TxID].MapiResponses = callBacks
default:
continue
}
}
return ancestry, nil
}
func parseChunk(b []byte, start uint64) (binaryChunk, uint64) {
offset := start
typeOfNextData := b[offset]
offset++
l, size := bt.NewVarIntFromBytes(b[offset:])
offset += uint64(size)
chunk := binaryChunk{
ContentType: typeOfNextData,
Data: b[offset : offset+uint64(l)],
}
offset += uint64(l)
return chunk, offset - start
}
func parseMapiCallbacks(b []byte) ([]*bc.MapiCallback, error) {
if len(b) == 0 {
return nil, ErrTriedToParseZeroBytes
}
var internalOffset uint64
allBinary := uint64(len(b))
numOfMapiResponses := b[internalOffset]
if numOfMapiResponses == 0 && len(b) == 1 {
return nil, ErrTriedToParseZeroBytes
}
internalOffset++
var responses = [][]byte{}
for allBinary > internalOffset {
l, size := bt.NewVarIntFromBytes(b[internalOffset:])
internalOffset += uint64(size)
response := b[internalOffset : internalOffset+uint64(l)]
internalOffset += uint64(l)
responses = append(responses, response)
}
mapiResponses := make([]*bc.MapiCallback, 0)
for _, response := range responses {
mapiResponse, err := bc.NewMapiCallbackFromBytes(response)
if err != nil {
return nil, err
}
mapiResponses = append(mapiResponses, mapiResponse)
}
return mapiResponses, nil
}
// VerifyAncestors will run through the map of Ancestors and check each input of each transaction to verify it.
// Only if there is no Proof attached.
func VerifyAncestors(ctx context.Context, ancestry *Ancestry, mpv MerkleProofVerifier, opts *verifyOptions) error {
ancestors := ancestry.Ancestors
var paymentTxID [32]byte
copy(paymentTxID[:], ancestry.PaymentTx.TxIDBytes())
ancestors[paymentTxID] = &Ancestor{
Tx: ancestry.PaymentTx,
}
if opts.fees {
if opts.feeQuote == nil {
return ErrNoFeeQuoteSupplied
}
ok, err := ancestry.PaymentTx.IsFeePaidEnough(opts.feeQuote)
if err != nil {
return err
}
if !ok {
return ErrFeePaidNotEnough
}
}
for _, ancestor := range ancestors {
inputsToCheck := make(map[[32]byte]*extendedInput)
if len(ancestor.Tx.Inputs) == 0 {
return ErrNoTxInputsToVerify
}
for idx, input := range ancestor.Tx.Inputs {
var inputID [32]byte
copy(inputID[:], input.PreviousTxID())
inputsToCheck[inputID] = &extendedInput{
input: input,
vin: idx,
}
}
// if we have a proof, check it.
if opts.proofs {
if ancestor.Proof == nil {
for inputID := range inputsToCheck {
// check if we have that ancestor, if not validation fail.
if ancestry.Ancestors[inputID] == nil {
return ErrProofOrInputMissing
}
}
} else {
// check proof.
response, err := mpv.VerifyMerkleProof(ctx, ancestor.Proof)
if response == nil {
return ErrInvalidProof
}
if response.TxID != "" && response.TxID != ancestor.Tx.TxID() {
return ErrTxIDMismatch
}
if err != nil || !response.Valid {
return ErrInvalidProof
}
}
}
if opts.script {
// otherwise check the inputs.
for inputID, extendedInput := range inputsToCheck {
input := extendedInput.input
// check if we have that ancestor, if not validation fail.
if ancestry.Ancestors[inputID] == nil {
if ancestor.Proof == nil && opts.proofs {
return ErrProofOrInputMissing
}
continue
}
if len(ancestry.Ancestors[inputID].Tx.Outputs) <= int(input.PreviousTxOutIndex) {
return ErrInputRefsOutOfBoundsOutput
}
lockingScript := ancestry.Ancestors[inputID].Tx.Outputs[input.PreviousTxOutIndex].LockingScript
unlockingScript := input.UnlockingScript
if !verifyInputOutputPair(ancestor.Tx, lockingScript, unlockingScript) {
return ErrPaymentNotVerified
}
}
}
}
return nil
}
func verifyInputOutputPair(tx *bt.Tx, lock *bscript.Script, unlock *bscript.Script) bool {
// TODO script interpreter.
return true
}