@@ -15,7 +15,6 @@ import (
1515 "fmt"
1616 "strings"
1717
18- "github.com/bitcoinschema/go-bob/util"
1918 "github.com/bitcoinschema/go-bpu"
2019 "github.com/libsv/go-bt/v2"
2120 "github.com/libsv/go-bt/v2/bscript"
@@ -63,6 +62,9 @@ func NewFromString(line string) (bobTx *Tx, err error) {
6362func NewFromTx (tx * bt.Tx ) (bobTx * Tx , err error ) {
6463 bobTx = new (Tx )
6564 err = bobTx .FromTx (tx )
65+ if err != nil {
66+ return nil , err
67+ }
6668 return
6769}
6870
@@ -181,171 +183,41 @@ func (t *Tx) FromString(line string) (err error) {
181183// FromTx takes a bt.Tx
182184func (t * Tx ) FromTx (tx * bt.Tx ) error {
183185
184- // Set the transaction ID
185- t .Tx .H = tx .TxID ()
186-
187- // Set the inputs
188- for inIdx , i := range tx .Inputs {
189-
190- cellHex := hex .EncodeToString (i .Bytes (false ))
191- cellB64 := base64 .RawStdEncoding .EncodeToString (i .Bytes (false ))
192- cellStr := i .String ()
193- txid := hex .EncodeToString (i .PreviousTxID ())
194-
195- bobInput := bpu.Input {
196- XPut : bpu.XPut {
197- I : uint8 (inIdx ),
198- Tape : []bpu.Tape {{
199- Cell : []bpu.Cell {{
200- H : & cellHex ,
201- B : & cellB64 ,
202- S : & cellStr ,
203- }},
204- I : 0 ,
205- }},
206- E : bpu.E {
207- H : & txid ,
208- },
209- },
210- }
211-
212- t .In = append (t .In , bobInput )
186+ if tx == nil {
187+ return fmt .Errorf ("Tx must be set" )
213188 }
214-
215- // Process outputs
216- for idxOut , o := range tx .Outputs {
217- var adr string
218-
219- // Try to get a pub_key hash (ignore fail when this is not a locking script)
220- outPubKeyHash , _ := o .LockingScript .PublicKeyHash ()
221- if len (outPubKeyHash ) > 0 {
222- outAddress , err := bscript .NewAddressFromPublicKeyHash (outPubKeyHash , true )
223- if err != nil {
224- return fmt .Errorf ("failed to get address from pubkeyhash %x: %w" , outPubKeyHash , err )
225- }
226- adr = outAddress .AddressString
227- }
228-
229- // Initialize out tapes and locking script asm
230- asm , err := o .LockingScript .ToASM ()
231- if err != nil {
232- return err
233- }
234-
235- pushDatas := strings .Split (asm , " " )
236-
237- var outTapes []bpu.Tape
238- bobOutput := bpu.Output {
239- XPut : bpu.XPut {
240- I : uint8 (idxOut ),
241- Tape : outTapes ,
242- E : bpu.E {
243- A : & adr ,
244- },
189+ var separator = "|"
190+ var l = bpu .IncludeL
191+ var opReturn = uint8 (106 )
192+ var opFalse = uint8 (0 )
193+ var splitConfig = []bpu.SplitConfig {
194+ {
195+ Token : & bpu.Token {
196+ Op : & opReturn ,
245197 },
246- }
247-
248- var opTape bpu.Tape
249- var currentTape bpu.Tape
250- var opOffset = 0
251- if len (pushDatas ) > 0 {
252-
253- // Check for OP_RETURN or OP_FALSE + OP_RETURN
254- // Look for OP_FALSE OP_RETURN or just OP_RETURN and separate into a cell collection
255- if len (pushDatas [0 ]) > 0 {
256- if pushDatas [0 ] == "OP_FALSE" || pushDatas [0 ] == "0" {
257- // OP_FALSE in position 0
258- var op = bscript .OpFALSE
259- var ops = "OP_FALSE"
260- opTape .Cell = append (opTape .Cell , bpu.Cell {
261- Op : & op ,
262- Ops : & ops ,
263- I : uint8 (idxOut ),
264- II : uint8 (0 ),
265- })
266- opOffset ++
267- // Check for OP_RETURN
268- if len (pushDatas [1 ]) > 0 && pushDatas [1 ] == "OP_RETURN" {
269- // OP_FALSE OP_RETURN
270- var op = bscript .OpRETURN
271- var ops = "OP_RETURN"
272- opTape .Cell = append (opTape .Cell , bpu.Cell {
273- Op : & op ,
274- Ops : & ops ,
275- I : uint8 (idxOut ),
276- II : uint8 (1 ),
277- })
278- // pull them out into their own cell collection
279- outTapes = append (outTapes , opTape )
280- opOffset ++
281-
282- }
283- } else if len (pushDatas [0 ]) > 0 && pushDatas [0 ] == "OP_RETURN" {
284- var op = bscript .OpRETURN
285- var ops = "OP_RETURN"
286- opTape .Cell = append (opTape .Cell , bpu.Cell {
287- Op : & op ,
288- Ops : & ops ,
289- I : uint8 (idxOut ),
290- II : uint8 (0 ),
291- })
292- opOffset ++
293-
294- // OP_RETURN in position 0
295- }
296- if opOffset > 0 {
297- outTapes = append (outTapes , opTape )
298- }
299- }
300- for pdIdx , pushData := range pushDatas {
301- if pdIdx < opOffset {
302- continue
303- }
304- // Ignore error if it fails, use empty
305- pushDataBytes , _ := hex .DecodeString (pushData )
306- b64String := base64 .StdEncoding .EncodeToString (pushDataBytes )
307- var pushDataString = string (pushDataBytes )
308-
309- // assume the pushdata is a chunk of
310- pushDataHex := pushData
311- var op uint8
312- var ops string
313- // asm is being put into the hex field - need to convert back to hex from opcodes if they exist in here
314- if pushDataByte , ok := util .OpCodeStrings [pushData ]; ok {
315- // this pushdata is a valid opcode
316- pushDataHex = hex .EncodeToString ([]byte {pushDataByte })
317- op = pushDataByte
318- ops = pushData
319- }
320- if pushData != ProtocolDelimiterAsm {
321- currentTape .Cell = append (currentTape .Cell , bpu.Cell {
322- Op : & op ,
323- Ops : & ops ,
324- B : & b64String ,
325- H : & pushDataHex ,
326- S : & pushDataString ,
327- I : uint8 (idxOut ),
328- II : uint8 (pdIdx - opOffset ),
329- })
330- }
331- // Note: OP_SWAP is 0x7c which is also ascii "|" which is our protocol separator.
332- // This is not used as OP_SWAP at all since this is in the script after the OP_FALSE
333- // if "OP_RETURN" == pushData || ProtocolDelimiterAsm == pushData {
334- if ProtocolDelimiterAsm == pushData {
335- outTapes = append (outTapes , currentTape )
336- currentTape = bpu.Tape {}
337- opOffset = 0
338- }
339- }
340- }
341-
342- // Add the trailing tape
343- outTapes = append (outTapes , currentTape )
344- bobOutput .Tape = outTapes
345-
346- t .Out = append (t .Out , bobOutput )
198+ Include : & l ,
199+ },
200+ {
201+ Token : & bpu.Token {
202+ Op : & opFalse ,
203+ },
204+ Include : & l ,
205+ },
206+ {
207+ Token : & bpu.Token {
208+ S : & separator ,
209+ },
210+ Require : & opReturn ,
211+ },
347212 }
348213
214+ bpuTx , err := bpu .Parse (bpu.ParseConfig {Tx : tx , SplitConfig : splitConfig })
215+ if err != nil {
216+ return err
217+ }
218+ if bpuTx != nil {
219+ t .BpuTx = * bpuTx
220+ }
349221 return nil
350222}
351223
0 commit comments