-
Notifications
You must be signed in to change notification settings - Fork 3
Expand file tree
/
Copy pathreply_to.go
More file actions
153 lines (119 loc) · 3.57 KB
/
reply_to.go
File metadata and controls
153 lines (119 loc) · 3.57 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
package channels
import (
"bytes"
"fmt"
envelope "github.com/tokenized/envelope/pkg/golang/envelope/base"
"github.com/tokenized/pkg/bitcoin"
"github.com/tokenized/pkg/bsor"
"github.com/tokenized/pkg/peer_channels"
"github.com/pkg/errors"
)
const (
ReplyToVersion = uint8(0)
)
var (
ProtocolIDReplyTo = envelope.ProtocolID("RT") // Protocol ID for channel reply messages
)
type ReplyToProtocol struct{}
func NewReplyToProtocol() *ReplyToProtocol {
return &ReplyToProtocol{}
}
func (*ReplyToProtocol) ProtocolID() envelope.ProtocolID {
return ProtocolIDReplyTo
}
func (*ReplyToProtocol) Parse(payload envelope.Data) (Message, envelope.Data, error) {
return ParseReplyTo(payload)
}
func (*ReplyToProtocol) ResponseCodeToString(code uint32) string {
return ReplyToResponseCodeToString(code)
}
// ReplyTo is used to identify that a message is in reply to a previous message.
type ReplyTo struct {
PeerChannel *peer_channels.Channel `bsor:"1" json:"peer_channel,omitempty"`
Handle *string `bsor:"2" json:"handle,omitempty"`
}
func (r ReplyTo) String() string {
if r.Handle != nil {
return fmt.Sprintf("handle:%s", *r.Handle)
}
if r.PeerChannel != nil {
return fmt.Sprintf("peer_channel:%s", r.PeerChannel.MaskedString())
}
return ""
}
func (*ReplyTo) IsWrapperType() {}
func (*ReplyTo) ProtocolID() envelope.ProtocolID {
return ProtocolIDReplyTo
}
func (r *ReplyTo) Write() (envelope.Data, error) {
// Version
payload := bitcoin.ScriptItems{bitcoin.PushNumberScriptItem(int64(ReplyToVersion))}
// Message
msgScriptItems, err := bsor.Marshal(r)
if err != nil {
return envelope.Data{}, errors.Wrap(err, "marshal")
}
payload = append(payload, msgScriptItems...)
return envelope.Data{envelope.ProtocolIDs{ProtocolIDReplyTo}, payload}, nil
}
func (r *ReplyTo) Wrap(payload envelope.Data) (envelope.Data, error) {
// Version
scriptItems := bitcoin.ScriptItems{bitcoin.PushNumberScriptItem(int64(ReplyToVersion))}
// Message
msgScriptItems, err := bsor.Marshal(r)
if err != nil {
return envelope.Data{}, errors.Wrap(err, "marshal")
}
scriptItems = append(scriptItems, msgScriptItems...)
payload.ProtocolIDs = append(envelope.ProtocolIDs{ProtocolIDReplyTo}, payload.ProtocolIDs...)
payload.Payload = append(scriptItems, payload.Payload...)
return payload, nil
}
func (r ReplyTo) Copy() ReplyTo {
result := ReplyTo{}
if r.PeerChannel != nil {
c := r.PeerChannel.Copy()
result.PeerChannel = &c
}
if r.Handle != nil {
c := CopyString(*r.Handle)
result.Handle = &c
}
return result
}
func CopyString(s string) string {
result := make([]byte, len(s))
copy(result, s)
return string(result)
}
func ParseReplyTo(payload envelope.Data) (*ReplyTo, envelope.Data, error) {
if len(payload.ProtocolIDs) == 0 || !bytes.Equal(payload.ProtocolIDs[0], ProtocolIDReplyTo) {
return nil, payload, nil
}
payload.ProtocolIDs = payload.ProtocolIDs[1:]
if len(payload.Payload) < 2 {
return nil, payload, errors.Wrapf(ErrInvalidMessage, "not enough reply push ops: %d",
len(payload.Payload))
}
version, err := bitcoin.ScriptNumberValue(payload.Payload[0])
if err != nil {
return nil, payload, errors.Wrap(err, "version")
}
if version != 0 {
return nil, payload, errors.Wrap(ErrUnsupportedVersion,
fmt.Sprintf("reply: %d", version))
}
result := &ReplyTo{}
payloads, err := bsor.Unmarshal(payload.Payload[1:], result)
if err != nil {
return nil, payload, errors.Wrap(err, "unmarshal")
}
payload.Payload = payloads
return result, payload, nil
}
func ReplyToResponseCodeToString(code uint32) string {
switch code {
default:
return "parse_error"
}
}