@@ -81,7 +81,7 @@ func NewFromTapes(tapes []bpu.Tape) (a *Aip) {
81
81
if cell .S != nil && * cell .S == Prefix {
82
82
a = new (Aip )
83
83
a .FromTape (t )
84
- a .SetDataFromTapes (tapes )
84
+ a .SetDataFromTapes (tapes , 0 )
85
85
return
86
86
}
87
87
}
@@ -90,50 +90,80 @@ func NewFromTapes(tapes []bpu.Tape) (a *Aip) {
90
90
}
91
91
92
92
// SetDataFromTapes sets the data the AIP signature is signing
93
- func (a * Aip ) SetDataFromTapes (tapes []bpu.Tape ) {
94
-
93
+ func (a * Aip ) SetDataFromTapes (tapes []bpu.Tape , instance int ) {
95
94
// Set OP_RETURN to be consistent with BitcoinFiles SDK
95
+ // var data [][]byte
96
96
var data = []string {opReturn }
97
+ var foundAIP bool
98
+ var aipTapeIndex int
99
+ var aipCellIndex int
97
100
98
- if len (a .Indices ) == 0 {
99
-
100
- // Walk over all output values and concatenate them until we hit the AIP prefix, then add in the separator
101
- for _ , tape := range tapes {
102
- for _ , cell := range tape .Cell {
103
- if cell .S != nil && * cell .S == Prefix {
104
- data = append (data , pipe )
105
- a .Data = data
106
- return
107
- }
108
- // Skip the OPS
109
- // if cell.Ops != nil {
110
- if cell .Op != nil && (* cell .Op == 0 || * cell .Op > 0x4e ) {
111
- continue
112
- }
113
- if cell .S != nil {
114
- data = append (data , strings .TrimSpace (* cell .S ))
101
+ // First find the AIP tape and cell index
102
+ aipCount := 0
103
+ for i , tape := range tapes {
104
+ for j , cell := range tape .Cell {
105
+ if cell .S != nil && * cell .S == Prefix {
106
+ if aipCount == instance {
107
+ aipTapeIndex = i
108
+ aipCellIndex = j
109
+ foundAIP = true
110
+ break
115
111
}
116
-
112
+ aipCount ++
117
113
}
114
+
118
115
}
116
+ if foundAIP {
117
+ break
118
+ }
119
+ }
120
+
121
+ // If we found AIP, collect data from all tapes up to the AIP tape
122
+ if foundAIP {
123
+ if len (a .Indices ) == 0 {
124
+
125
+ // Walk over all output values and concatenate them until we hit the AIP prefix, then add in the separator
126
+ for i , tape := range tapes {
127
+ for j , cell := range tape .Cell {
119
128
120
- } else {
129
+ if cell .S != nil && * cell .S == Prefix {
130
+ data = append (data , pipe )
131
+ a .Data = data
132
+ if i == aipTapeIndex && j >= aipCellIndex {
133
+ return
134
+ }
135
+ }
121
136
122
- var indexCt = 0
137
+ // Skip the OPS
138
+ // if cell.Ops != nil {
139
+ if cell .Op != nil && (* cell .Op == 0 || * cell .Op > 0x4e ) {
140
+ continue
141
+ }
142
+ if cell .S != nil {
143
+ data = append (data , strings .TrimSpace (* cell .S ))
144
+ }
123
145
124
- for _ , tape := range tapes {
125
- for _ , cell := range tape .Cell {
126
- if cell .S != nil && * cell .S != Prefix && contains (a .Indices , indexCt ) {
127
- data = append (data , * cell .S )
128
- } else {
129
- data = append (data , pipe )
130
146
}
131
- indexCt ++
132
147
}
133
- }
134
148
149
+ } else {
150
+
151
+ var indexCt = 0
152
+
153
+ for _ , tape := range tapes {
154
+ for _ , cell := range tape .Cell {
155
+ if cell .S != nil && * cell .S != Prefix && contains (a .Indices , indexCt ) {
156
+ data = append (data , * cell .S )
157
+ } else {
158
+ data = append (data , pipe )
159
+ }
160
+ indexCt ++
161
+ }
162
+ }
163
+ }
135
164
a .Data = data
136
165
}
166
+
137
167
}
138
168
139
169
// SignBobOpReturnData appends a signature to a BOB Tx by adding a
@@ -200,7 +230,7 @@ func ValidateTapes(tapes []bpu.Tape) (bool, error) {
200
230
// Once we hit AIP Prefix, stop
201
231
if cell .S != nil && * cell .S == Prefix {
202
232
a := NewFromTape (tape )
203
- a .SetDataFromTapes (tapes )
233
+ a .SetDataFromTapes (tapes , 0 )
204
234
return a .Validate ()
205
235
}
206
236
}
@@ -218,3 +248,25 @@ func contains(s []int, e int) bool {
218
248
}
219
249
return false
220
250
}
251
+
252
+ // NewFromAllTapes will create all AIP objects from a []bob.Tape
253
+ func NewFromAllTapes (tapes []bpu.Tape ) []* Aip {
254
+ var aips []* Aip
255
+
256
+ // Find all tapes that contain the AIP prefix
257
+ instance := 0
258
+ for i , t := range tapes {
259
+ for _ , cell := range t .Cell {
260
+ if cell .S != nil && * cell .S == Prefix {
261
+ a := new (Aip )
262
+ a .FromTape (t )
263
+ // For all AIP entries, include all data from the start up to this entry
264
+ a .SetDataFromTapes (tapes [:i + 1 ], instance )
265
+ instance ++
266
+ aips = append (aips , a )
267
+ continue
268
+ }
269
+ }
270
+ }
271
+ return aips
272
+ }
0 commit comments