@@ -81,7 +81,7 @@ func NewFromTapes(tapes []bpu.Tape) (a *Aip) {
8181 if cell .S != nil && * cell .S == Prefix {
8282 a = new (Aip )
8383 a .FromTape (t )
84- a .SetDataFromTapes (tapes )
84+ a .SetDataFromTapes (tapes , 0 )
8585 return
8686 }
8787 }
@@ -90,50 +90,80 @@ func NewFromTapes(tapes []bpu.Tape) (a *Aip) {
9090}
9191
9292// 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 ) {
9594 // Set OP_RETURN to be consistent with BitcoinFiles SDK
95+ // var data [][]byte
9696 var data = []string {opReturn }
97+ var foundAIP bool
98+ var aipTapeIndex int
99+ var aipCellIndex int
97100
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
115111 }
116-
112+ aipCount ++
117113 }
114+
118115 }
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 {
119128
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+ }
121136
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+ }
123145
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 )
130146 }
131- indexCt ++
132147 }
133- }
134148
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+ }
135164 a .Data = data
136165 }
166+
137167}
138168
139169// SignBobOpReturnData appends a signature to a BOB Tx by adding a
@@ -200,7 +230,7 @@ func ValidateTapes(tapes []bpu.Tape) (bool, error) {
200230 // Once we hit AIP Prefix, stop
201231 if cell .S != nil && * cell .S == Prefix {
202232 a := NewFromTape (tape )
203- a .SetDataFromTapes (tapes )
233+ a .SetDataFromTapes (tapes , 0 )
204234 return a .Validate ()
205235 }
206236 }
@@ -218,3 +248,25 @@ func contains(s []int, e int) bool {
218248 }
219249 return false
220250}
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