forked from ishbir/bmutil
/
message.go
127 lines (106 loc) · 3.25 KB
/
message.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
126
127
// Copyright (c) 2015 Monetas
// Copyright 2016 Daniel Krawisz.
// Use of this source code is governed by an ISC
// license that can be found in the LICENSE file.
package obj
import (
"bytes"
"encoding/hex"
"fmt"
"io"
"io/ioutil"
"time"
"github.com/DanielKrawisz/bmutil/pow"
"github.com/DanielKrawisz/bmutil/wire"
)
// MessageVersion is the standard version number for message objects.
const MessageVersion = 1
// Message implements the Object and Message interfaces and represents a
// message sent between two addresses. It can be decrypted only by those
// that have the private encryption key that corresponds to the
// destination address.
type Message struct {
header *wire.ObjectHeader
Encrypted []byte
}
func (msg *Message) decodePayload(r io.Reader) error {
var err error
msg.Encrypted, err = ioutil.ReadAll(r)
return err
}
// Decode decodes r using the bitmessage protocol encoding into the receiver.
// This is part of the Message interface implementation.
func (msg *Message) Decode(r io.Reader) error {
var err error
msg.header, err = wire.DecodeObjectHeader(r)
if err != nil {
return err
}
if msg.header.ObjectType != wire.ObjectTypeMsg {
str := fmt.Sprintf("Object Type should be %d, but is %d",
wire.ObjectTypeMsg, msg.header.ObjectType)
return wire.NewMessageError("Decode", str)
}
return msg.decodePayload(r)
}
// Encode encodes the receiver to w using the bitmessage protocol encoding.
// This is part of the Message interface implementation.
func (msg *Message) Encode(w io.Writer) error {
err := msg.header.Encode(w)
if err != nil {
return err
}
_, err = w.Write(msg.Encrypted)
return err
}
// MaxPayloadLength returns the maximum length the payload can be for the
// receiver. This is part of the Message interface implementation.
func (msg *Message) MaxPayloadLength() int {
return wire.MaxPayloadOfMsgObject
}
func (msg *Message) String() string {
return fmt.Sprintf("Message{%s, %s}",
msg.header.String(),
hex.EncodeToString(msg.Encrypted))
}
// Command returns the protocol command string for the message. This is part
// of the Message interface implementation.
func (msg *Message) Command() string {
return wire.CmdObject
}
// Header returns the object header.
func (msg *Message) Header() *wire.ObjectHeader {
return msg.header
}
// Payload return the object payload of the message.
func (msg *Message) Payload() []byte {
return msg.Encrypted
}
// MsgObject transforms the PubKeyObject to a *MsgObject.
func (msg *Message) MsgObject() *wire.MsgObject {
return wire.NewMsgObject(msg.header, msg.Encrypted)
}
// NewMessage returns a new object message that conforms to the Message interface
// using the passed parameters and defaults for the remaining fields.
func NewMessage(nonce pow.Nonce, expiration time.Time, streamNumber uint64, encrypted []byte) *Message {
return &Message{
header: wire.NewObjectHeader(
nonce,
expiration,
wire.ObjectTypeMsg,
MessageVersion,
streamNumber,
),
Encrypted: encrypted,
}
}
// DecodeMessage takes a byte array and turns it into a message object.
func DecodeMessage(obj []byte) (*Message, error) {
// Make sure that object type specific checks happen first.
var msg *Message
err := msg.Decode(bytes.NewReader(obj))
if err != nil {
return nil, err
}
return msg, nil
}