-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathscript_parser.go
More file actions
252 lines (222 loc) · 6.84 KB
/
Copy pathscript_parser.go
File metadata and controls
252 lines (222 loc) · 6.84 KB
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
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
package scriptparser
import (
"encoding/hex"
"github.com/btcsuite/btcd/btcec/v2"
"github.com/btcsuite/btcd/btcec/v2/schnorr"
"github.com/btcsuite/btcd/btcutil"
"github.com/btcsuite/btcd/chaincfg"
"github.com/btcsuite/btcd/txscript"
"github.com/btcsuite/btcd/wire"
"github.com/ethereum/go-ethereum/log"
)
const (
ProtocolID string = "6f7264"
BodyTag string = "00"
ContentTypeTag string = "01"
)
type TransactionInscription struct {
Validator *ValidatorContent
Inscription *InscriptionContent
TxInIndex uint32
TxInOffset uint64
}
type InscriptionContent struct {
ContentType []byte
ContentBody []byte
ContentLength uint64
IsUnrecognizedEvenField bool
}
type ValidatorContent struct {
ValidatorAddresses []string
}
func ParseInscriptionsFromTransaction(msgTx *wire.MsgTx, net *chaincfg.Params) []*TransactionInscription {
var inscriptionsFromTx []*TransactionInscription
txHash := msgTx.TxHash().String()
if !msgTx.HasWitness() {
log.Debug("Tx: %s inputs does not contain witness data", txHash)
return nil
}
for i, v := range msgTx.TxIn {
index, input := i, v
if len(input.Witness) <= 1 {
log.Debug("Tx: %s, the length of tx input witness data is %d", txHash, len(input.Witness))
continue
}
if len(input.Witness) == 2 && input.Witness[len(input.Witness)-1][0] == txscript.TaprootAnnexTag {
log.Debug("Tx: %s, tx witness contains Taproot Annex data but the length of tx input witness data is 2",
txHash)
continue
}
// If Taproot Annex data exists, take the last element of the witness as the script data, otherwise,
// take the penultimate element of the witness as the script data
var witnessScript []byte
if input.Witness[len(input.Witness)-1][0] == txscript.TaprootAnnexTag {
witnessScript = input.Witness[len(input.Witness)-1]
} else {
witnessScript = input.Witness[len(input.Witness)-2]
}
if !needParse(witnessScript) {
continue
}
validator := ParseValidator(witnessScript, net)
if len(validator.ValidatorAddresses) == 0 {
continue
}
// Parse script and get ordinals content
inscriptions := ParseInscriptions(witnessScript)
if len(inscriptions) == 0 {
continue
}
for i, v := range inscriptions {
txInOffset, inscription := i, v
inscriptionsFromTx = append(inscriptionsFromTx, &TransactionInscription{
Validator: validator,
Inscription: inscription,
TxInIndex: uint32(index),
TxInOffset: uint64(txInOffset),
})
}
}
return inscriptionsFromTx
}
func needParse(witnessScript []byte) bool {
flag := false
tokenizer := txscript.MakeScriptTokenizer(0, witnessScript)
for tokenizer.Next() {
if tokenizer.OpcodePosition() == 8 {
if hex.EncodeToString(tokenizer.Data()) == ProtocolID {
flag = true
break
}
}
}
return flag
}
func ParseValidator(witnessScript []byte, net *chaincfg.Params) *ValidatorContent {
var validators ValidatorContent
tokenizer := txscript.MakeScriptTokenizer(0, witnessScript)
for tokenizer.Next() {
if tokenizer.OpcodePosition() == 0 || tokenizer.OpcodePosition() == 2 {
pubkey, _ := schnorr.ParsePubKey(tokenizer.Data())
address, _ := convertPubKey2Address(pubkey, net)
log.Info("Data: ", address)
validators.ValidatorAddresses = append(validators.ValidatorAddresses, address)
}
}
return &validators
}
func ParseInscriptions(witnessScript []byte) []*InscriptionContent {
var inscriptions []*InscriptionContent
// Parse inscription content from witness script
tokenizer := txscript.MakeScriptTokenizer(0, witnessScript)
for tokenizer.Next() {
// Check inscription envelop header: OP_FALSE(0x00), OP_IF(0x63), PROTOCOL_ID([0x6f, 0x72, 0x64])
if tokenizer.Opcode() == txscript.OP_FALSE {
if !tokenizer.Next() || tokenizer.Opcode() != txscript.OP_IF {
return inscriptions
}
if !tokenizer.Next() || hex.EncodeToString(tokenizer.Data()) != ProtocolID {
return inscriptions
}
inscription := parseOneInscription(&tokenizer)
if inscription != nil {
inscriptions = append(inscriptions, inscription)
}
}
}
return inscriptions
}
func parseOneInscription(tokenizer *txscript.ScriptTokenizer) *InscriptionContent {
var (
tags = make(map[string][]byte)
contentType []byte
contentBody []byte
contentLength uint64
isUnrecognizedEvenField bool
)
// Find any pushed data in the script. This includes OP_0, but not OP_1 - OP_16.
for tokenizer.Next() {
if tokenizer.Opcode() == txscript.OP_ENDIF {
break
} else if hex.EncodeToString([]byte{tokenizer.Opcode()}) == BodyTag {
var body []byte
for tokenizer.Next() {
if tokenizer.Opcode() == txscript.OP_ENDIF {
break
} else if tokenizer.Opcode() == txscript.OP_0 {
// OP_0 push no data
continue
} else if tokenizer.Opcode() >= txscript.OP_DATA_1 && tokenizer.Opcode() <= txscript.OP_PUSHDATA4 {
// Taproot's restriction, individual data pushes may not be larger than 520 bytes.
if len(tokenizer.Data()) > 520 {
log.Error("data is longer than 520")
return nil
}
body = append(body, tokenizer.Data()...)
} else {
// Invalid opcode found in content body, e.g., 615a7c90df1d4fdd07c6ea98766bc6846dd5264a9fa81ca41611bbf9bde38cf8.
return nil
}
}
contentBody = body
contentLength = uint64(len(body))
break
} else {
if tokenizer.Data() == nil {
return nil
}
tag := hex.EncodeToString(tokenizer.Data())
if _, ok := tags[tag]; ok {
return nil
}
if tokenizer.Next() {
if tokenizer.Opcode() != txscript.OP_0 && tokenizer.Data() == nil {
// Invalid data length, e.g., 0b71bd09c848be66334c0cdaa32686e98dffa8a212af694f59165cdbb588e587
return nil
}
tags[tag] = tokenizer.Data()
}
}
}
// No OP_ENDIF
if tokenizer.Opcode() != txscript.OP_ENDIF {
return nil
}
// Error occurred
if err := tokenizer.Err(); err != nil {
return nil
}
// Get inscription content
for k := range tags {
key := k
if key == ContentTypeTag {
contentType = tags[ContentTypeTag]
continue
}
// Unrecognized even tag
tag, _ := hex.DecodeString(key)
if len(tag) > 0 && int(tag[0])%2 == 0 {
isUnrecognizedEvenField = true
}
}
inscription := &InscriptionContent{
ContentType: contentType,
ContentBody: contentBody,
ContentLength: contentLength,
IsUnrecognizedEvenField: isUnrecognizedEvenField,
}
return inscription
}
func convertPubKey2Address(pubKey *btcec.PublicKey, net *chaincfg.Params) (string, error) {
// 将公钥转换为压缩格式的字节数组
pubKeyBytes := pubKey.SerializeCompressed()
// 将公钥字节数组转换为比特币地址
address, err := btcutil.NewAddressPubKey(pubKeyBytes, net)
if err != nil {
log.Error("%v", err.Error())
return "", err
}
// 输出比特币地址
log.Info("Bitcoin Address:", address.EncodeAddress())
return address.EncodeAddress(), nil
}