-
-
Notifications
You must be signed in to change notification settings - Fork 5
/
bmap.go
141 lines (131 loc) · 3.75 KB
/
bmap.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
// Package bmap detects known data protocols within Bitcoin transactions
package bmap
import (
"github.com/bitcoin-sv/go-sdk/transaction"
"github.com/bitcoinschema/go-aip"
"github.com/bitcoinschema/go-b"
"github.com/bitcoinschema/go-bap"
"github.com/bitcoinschema/go-bmap/ord"
"github.com/bitcoinschema/go-bmap/run"
"github.com/bitcoinschema/go-bob"
"github.com/bitcoinschema/go-boost"
"github.com/bitcoinschema/go-bpu"
magic "github.com/bitcoinschema/go-map"
"github.com/bitcoinschema/go-sigma"
)
// Tx is a Bmap formatted tx
type Tx struct {
bpu.Tx
AIP []*aip.Aip `json:"AIP,omitempty" bson:"AIP,omitempty"`
B []*b.B `json:"B,omitempty" bson:"B,omitempty"`
BAP []*bap.Bap `json:"BAP,omitempty" bson:"BAP,omitempty"`
BOOST []*boost.Boost `json:"BOOST,omitempty" bson:"BOOST,omitempty"`
MAP []magic.MAP `json:"MAP,omitempty" bson:"MAP,omitempty"`
Run []*run.Jig `json:"Run,omitempty" bson:"Run,omitempty"`
Ord []*ord.Ordinal `json:"Ord,omitempty" bson:"Ord,omitempty"`
Sigma []*sigma.Sig `json:"Sigma,omitempty" bson:"Sigma,omitempty"`
}
// NewFromBob returns a new BmapTx from a BobTx
func NewFromBob(bobTx *bob.Tx) (bmapTx *Tx, err error) {
bmapTx = new(Tx)
err = bmapTx.FromBob(bobTx)
if err != nil {
bmapTx = nil
}
return
}
// NewFromTx returns a new BmapTx from a *bt.Tx
func NewFromTx(tx *transaction.Transaction) (bmapTx *Tx, err error) {
var bobTx *bob.Tx
if bobTx, err = bob.NewFromTx(tx); err != nil {
return
}
bmapTx = new(Tx)
err = bmapTx.FromBob(bobTx)
return
}
// NewFromRawTxString returns a new BmapTx from a hex string
func NewFromRawTxString(tx string) (bmapTx *Tx, err error) {
var bobTx *bob.Tx
if bobTx, err = bob.NewFromRawTxString(tx); err != nil {
return
}
bmapTx = new(Tx)
err = bmapTx.FromBob(bobTx)
return
}
// FromBob returns a BmapTx from a BobTx
func (t *Tx) FromBob(bobTx *bob.Tx) (err error) {
for vout, out := range bobTx.Out {
for index, tape := range out.Tape {
// Handle string prefixes
if len(tape.Cell) > 0 && tape.Cell[0].S != nil {
prefixData := *tape.Cell[0].S
switch prefixData {
case run.Prefix:
var runOut *run.Jig
if runOut, err = run.NewFromTape(tape); err != nil {
return err
}
t.Run = append(t.Run, runOut)
continue
case aip.Prefix:
aipOut := aip.NewFromTape(tape)
aipOut.SetDataFromTapes(out.Tape)
t.AIP = append(t.AIP, aipOut)
continue
case bap.Prefix:
var bapOut *bap.Bap
if bapOut, err = bap.NewFromTape(&out.Tape[index]); err != nil {
return err
}
t.BAP = append(t.BAP, bapOut)
continue
case magic.Prefix:
var mapOut magic.MAP
if mapOut, err = magic.NewFromTape(&out.Tape[index]); err != nil {
return err
}
t.MAP = append(t.MAP, mapOut)
continue
case boost.Prefix:
var boostOut *boost.Boost
if boostOut, err = boost.NewFromTape(&out.Tape[index]); err != nil {
return err
}
t.BOOST = append(t.BOOST, boostOut)
continue
case b.Prefix:
var bOut *b.B
if bOut, err = b.NewFromTape(out.Tape[index]); err != nil {
return err
}
t.B = append(t.B, bOut)
continue
case sigma.Prefix:
sigOut := sigma.NewSigFromTape(out.Tape[index], vout)
t.Sigma = append(t.Sigma, sigOut)
continue
}
}
// Handle OPCODE prefixes
if len(tape.Cell) > 5 && tape.Cell[0].Ops != nil {
ordScript := ord.ScriptFromTape(tape)
if len(ordScript) > 0 {
var ordOut *ord.Ordinal
if ordOut, err = ord.NewFromTape(out.Tape[index]); err != nil {
return err
}
ordOut.Vout = uint8(len(t.Ord))
t.Ord = append(t.Ord, ordOut)
}
}
}
// Set inherited fields
t.Blk = bobTx.Blk
t.In = bobTx.In
t.Out = bobTx.Out
t.Tx = bobTx.Tx
}
return
}