-
Notifications
You must be signed in to change notification settings - Fork 3
Expand file tree
/
Copy pathmessage_id.go
More file actions
104 lines (79 loc) · 2.56 KB
/
message_id.go
File metadata and controls
104 lines (79 loc) · 2.56 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
package channels
import (
"bytes"
"fmt"
envelope "github.com/tokenized/envelope/pkg/golang/envelope/base"
"github.com/tokenized/pkg/bitcoin"
"github.com/pkg/errors"
)
const (
MessageIDVersion = uint8(0)
)
var (
ProtocolIDMessageID = envelope.ProtocolID("ID") // Protocol ID for message id
ErrInvalidMessageID = errors.New("Invalid MessageID")
)
type MessageIDProtocol struct{}
func NewMessageIDProtocol() *MessageIDProtocol {
return &MessageIDProtocol{}
}
func (*MessageIDProtocol) ProtocolID() envelope.ProtocolID {
return ProtocolIDMessageID
}
func (*MessageIDProtocol) Parse(payload envelope.Data) (Message, envelope.Data, error) {
return ParseMessageID(payload)
}
func (*MessageIDProtocol) ResponseCodeToString(code uint32) string {
return "parse"
}
type MessageID struct {
MessageID uint64 `bsor:"-" json:"message_id"`
}
func (*MessageID) IsWrapperType() {}
func (*MessageID) ProtocolID() envelope.ProtocolID {
return ProtocolIDMessageID
}
// WrapMessageID wraps the payload with the message id and returns the new payload containing the
// message id.
func WrapMessageID(payload envelope.Data, messageID uint64) (envelope.Data, error) {
id := &MessageID{
MessageID: messageID,
}
return id.Wrap(payload)
}
func (m *MessageID) Wrap(payload envelope.Data) (envelope.Data, error) {
// Version
scriptItems := bitcoin.ScriptItems{bitcoin.PushNumberScriptItem(int64(MessageIDVersion))}
// Message
scriptItems = append(scriptItems, bitcoin.PushNumberScriptItemUnsigned(m.MessageID))
payload.ProtocolIDs = append(envelope.ProtocolIDs{ProtocolIDMessageID}, payload.ProtocolIDs...)
payload.Payload = append(scriptItems, payload.Payload...)
return payload, nil
}
func ParseMessageID(payload envelope.Data) (*MessageID, envelope.Data, error) {
if len(payload.ProtocolIDs) == 0 || !bytes.Equal(payload.ProtocolIDs[0], ProtocolIDMessageID) {
return nil, payload, nil
}
payload.ProtocolIDs = payload.ProtocolIDs[1:]
if len(payload.Payload) < 3 {
return nil, payload, errors.Wrapf(ErrInvalidMessage, "not enough message id 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("message id: %d", version))
}
value, err := bitcoin.ScriptNumberValueUnsigned(payload.Payload[1])
if err != nil {
return nil, payload, errors.Wrap(err, "value")
}
result := &MessageID{
MessageID: value,
}
payload.Payload = payload.Payload[2:]
return result, payload, nil
}