generated from bitcoin-sv/template
-
-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathb.go
More file actions
82 lines (65 loc) · 1.81 KB
/
b.go
File metadata and controls
82 lines (65 loc) · 1.81 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
package bitcom
import (
"github.com/bsv-blockchain/go-sdk/script"
)
// B PROTOCOL - PREFIX DATA MEDIA_TYPE ENCODING FILENAME
// BPrefix is the bitcom protocol prefix for B
const BPrefix = "19HxigV4QyBv3tHpQVcUEQyq1pzZVdoAut"
// Media types
type MediaType string
const (
MediaTypeTextPlain MediaType = "text/plain"
MediaTypeTextMarkdown MediaType = "text/markdown"
MediaTypeTextHTML MediaType = "text/html"
MediaTypeImagePNG MediaType = "image/png"
MediaTypeImageJPEG MediaType = "image/jpeg"
)
type Encoding string
var (
EncodingUTF8 Encoding = "utf-8"
EncodingBinay Encoding = "binary"
)
// B represents B protocol data
type B struct {
MediaType MediaType `json:"mediaType"`
Encoding Encoding `json:"encoding"`
Data []byte `json:"data"`
Filename string `json:"filename,omitempty"`
}
// DecodeB processes and extracts B protocol data from a transaction script.
// The function expects the script to contain protocol data in the format:
// DATA MEDIA_TYPE ENCODING [FILENAME]
// Where FILENAME is optional. Returns nil if the script is invalid or cannot be parsed.
func DecodeB(data any) *B {
scr := ToScript(data)
if scr == nil {
return nil
}
pos := ZERO
var op *script.ScriptChunk
var err error
b := &B{}
// Protocol order: PREFIX DATA MEDIA_TYPE ENCODING FILENAME
// Skip prefix as it's already checked
// Read DATA
if op, err = scr.ReadOp(&pos); err != nil {
return nil
}
b.Data = op.Data
// Read MEDIA_TYPE
if op, err = scr.ReadOp(&pos); err != nil {
return nil
}
b.MediaType = MediaType(op.Data)
// Read ENCODING
if op, err = scr.ReadOp(&pos); err != nil {
return nil
}
b.Encoding = Encoding(op.Data)
// Try to read optional FILENAME
if op, err = scr.ReadOp(&pos); err == nil {
// Successfully read filename
b.Filename = string(op.Data)
}
return b
}