-
Notifications
You must be signed in to change notification settings - Fork 1
/
msggetpubkey.go
125 lines (106 loc) · 3.06 KB
/
msggetpubkey.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
package bmwire
import (
"fmt"
"io"
"time"
)
const (
// Starting in version 4, Ripe is derived from the tag and not
// sent directly
TagBasedRipeVersion = 4
)
type MsgGetPubKey struct {
Nonce uint64
ExpiresTime time.Time
ObjectType ObjectType
Version uint64
StreamNumber uint64
Ripe *RipeHash
Tag *ShaHash
}
// Decode decodes r using the bitmessage protocol encoding into the receiver.
// This is part of the Message interface implementation.
func (msg *MsgGetPubKey) Decode(r io.Reader) error {
var sec int64
err := readElements(r, &msg.Nonce, &sec, &msg.ObjectType)
if err != nil {
return err
}
if msg.ObjectType != ObjectTypeGetPubKey {
str := fmt.Sprintf("Object Type should be %d, but is %d",
ObjectTypeGetPubKey, msg.ObjectType)
return messageError("Decode", str)
}
msg.ExpiresTime = time.Unix(sec, 0)
if msg.Version, err = readVarInt(r); err != nil {
return err
}
if msg.StreamNumber, err = readVarInt(r); err != nil {
return err
}
if msg.Version >= TagBasedRipeVersion {
msg.Tag, _ = NewShaHash(make([]byte, 32))
if err = readElement(r, msg.Tag); err != nil {
return err
}
} else {
msg.Ripe, _ = NewRipeHash(make([]byte, 20))
if err = readElement(r, msg.Ripe); err != nil {
return err
}
}
return err
}
// Encode encodes the receiver to w using the bitmessage protocol encoding.
// This is part of the Message interface implementation.
func (msg *MsgGetPubKey) Encode(w io.Writer) error {
err := writeElements(w, msg.Nonce, msg.ExpiresTime.Unix(), msg.ObjectType)
if err != nil {
return err
}
if err = writeVarInt(w, msg.Version); err != nil {
return err
}
if err = writeVarInt(w, msg.StreamNumber); err != nil {
return err
}
if msg.Version >= TagBasedRipeVersion {
if err = writeElement(w, msg.Tag); err != nil {
return err
}
} else {
if err = writeElement(w, msg.Ripe); err != nil {
return err
}
}
return err
}
// Command returns the protocol command string for the message. This is part
// of the Message interface implementation.
func (msg *MsgGetPubKey) Command() string {
return CmdObject
}
// MaxPayloadLength returns the maximum length the payload can be for the
// receiver. This is part of the Message interface implementation.
func (msg *MsgGetPubKey) MaxPayloadLength() uint32 {
return 1 << 18
}
func (msg *MsgGetPubKey) String() string {
return fmt.Sprintf("getpubkey: v%d %d %s %d %x %x", msg.Version, msg.Nonce, msg.ExpiresTime, msg.StreamNumber, msg.Ripe, msg.Tag)
}
// NewMsgGetPubKey returns a new object message that conforms to the
// Message interface using the passed parameters and defaults for the remaining
// fields.
func NewMsgGetPubKey(nonce uint64, expires time.Time, version, streamNumber uint64, ripe *RipeHash, tag *ShaHash) *MsgGetPubKey {
// Limit the timestamp to one second precision since the protocol
// doesn't support better.
return &MsgGetPubKey{
Nonce: nonce,
ExpiresTime: expires,
ObjectType: ObjectTypeGetPubKey,
Version: version,
StreamNumber: streamNumber,
Ripe: ripe,
Tag: tag,
}
}