aboutsummaryrefslogtreecommitdiffstats
path: root/whisper/message.go
blob: db0110b4a785d5acf6a750a8e15ec6a31e73076a (plain) (blame)
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
package whisper

import (
    "crypto/ecdsa"
    "time"

    "github.com/ethereum/go-ethereum/crypto"
)

type Message struct {
    Flags     byte
    Signature []byte
    Payload   []byte
}

func NewMessage(payload []byte) *Message {
    return &Message{Flags: 0, Payload: payload}
}

func (self *Message) hash() []byte {
    return crypto.Sha3(append([]byte{self.Flags}, self.Payload...))
}

func (self *Message) sign(key *ecdsa.PrivateKey) (err error) {
    self.Flags = 1
    self.Signature, err = crypto.Sign(self.hash(), key)
    return
}

func (self *Message) Recover() *ecdsa.PublicKey {
    defer func() { recover() }() // in case of invalid sig
    return crypto.SigToPub(self.hash(), self.Signature)
}

func (self *Message) Encrypt(to *ecdsa.PublicKey) (err error) {
    self.Payload, err = crypto.Encrypt(to, self.Payload)
    if err != nil {
        return err
    }

    return nil
}

func (self *Message) Bytes() []byte {
    return append([]byte{self.Flags}, append(self.Signature, self.Payload...)...)
}

type Opts struct {
    From   *ecdsa.PrivateKey
    To     *ecdsa.PublicKey
    Ttl    time.Duration
    Topics [][]byte
}

func (self *Message) Seal(pow time.Duration, opts Opts) (*Envelope, error) {
    if opts.From != nil {
        err := self.sign(opts.From)
        if err != nil {
            return nil, err
        }
    }

    if opts.To != nil {
        err := self.Encrypt(opts.To)
        if err != nil {
            return nil, err
        }
    }

    envelope := NewEnvelope(DefaultTtl, opts.Topics, self)
    envelope.Seal(pow)

    return envelope, nil
}
* Stagifybapt2013-11-041-1/+0 * - Update to 4.0.6.jkim2013-10-262-5/+4 * Add NO_STAGE all over the place in preparation for the staging support (cat: ...bapt2013-09-2112-15/+13 * Update to 4.0.5.jkim2013-08-231-4/+4 * KDE3 and QT3 expired on 2013-07-01, remove these ports.rene2013-07-275-1134/+0 * Fix INDEX by really fixing the kde4-l10n portsbapt2013-07-101-0/+1 * KDE4 l10n fixes.rakuco2013-07-102-3/+1 * Update the KDE Software Compilation to 4.10.5.rakuco2013-07-051-2/+2 * Update to KDE SC 4.10.4, proudly presented by the KDE on FreeBSD team.rakuco2013-07-031-2/+2 * Update to 4.0.4.jkim2013-06-201-4/+4 * KDE/FreeBSD team presents KDE SC 4.10.3 ports!makc2013-05-192-4/+2 * Update to 4.0.3.jkim2013-05-101-4/+4 * - Convert USE_GETTEXT to USES (part 2)ak2013-04-241-1/+1 * Update to 4.0.2.jkim2013-04-171-4/+4 * - Update Calligra and l10n ports to 2.6.2:avilla2013-03-302-7/+5 * KDE/FreeBSD team presents KDE SC 4.10.1 ports!makc2013-03-273-16/+33 * Update to 4.0.1.jkim2013-03-091-4/+4 * Merge from area51 repository:makc2013-02-052-3/+3 * Update to 3.6.5.jkim2013-02-051-4/+4 * KDE/FreeBSD team presents KDE SC 4.9.5 ports!makc2013-02-043-30/+40 * - Change MAINTAINER addresstabthorpe2013-01-106-34/+10 * - Deprecate QT3, KDE3 and unmaintained ports depending on them. QT 3.3.8beat2012-12-301-0/+3 * - Update to 3.5.7.jkim2012-10-272-9/+4 * - Update Calligra Suite to 2.5.2.avilla2012-09-171-2/+2 * - Update Calligra to 2.5.1.avilla2012-09-012-7/+2 * The KDE/FreeBSD team is pleased to announce version 2.5 of Calligra,avilla2012-08-262-11/+3 * - Update to 3.5.6.jkim2012-08-241-4/+4 * Fix typos in COMMENTcs2012-07-291-1/+1 * - Update LibreOffice and the language packs to 3.5.5.jkim2012-07-181-4/+4 * - Update Calligra to 2.4.3.avilla2012-07-052-2/+4 * - The FreeBSD Office team is proud to announce LibreOffice.org 3.5.4 releasefluffy2012-07-011-4/+4 * KDE/FreeBSD team presents KDE SC 4.8.4, probably the last release in 4.8.x se...makc2012-06-152-3/+2 * - update png to 1.5.10dinoex2012-06-012-1/+2 * - Remove koffice-i18n ports, as they are not very useful withoutavilla2012-05-315-68/+0 * The KDE/FreeBSD team is pleased to announce Calligra Suite 2.4.2, KDEavilla2012-05-319-108/+47 * KDE/FreeBSD team presents long awaited KDE SC 4.8.3!makc2012-05-253-10/+72 * - upgrade to 3.5.2bapt2012-04-234-0/+22 * - Bump PORTREVISION to chase the update of multimedia/libvpxashish2012-02-163-0/+3 * The KDE/FreeBSD team is pleased to announce KDE SC 4.7.4, whichavilla2012-01-252-2/+7 * - Pass maintainership to office@FreeBSD.orgsunpoet2011-11-291-1/+1 * The KDE on FreeBSD team is pleased to update the KDE4 ports to 4.7.3.rakuco2011-11-141-2/+2 * The KDE/FreeBSD team is pleased to announce KDE Software Compilationavilla2011-10-172-8/+27 * - Set DIST_SUBDIR: move dist files to DISTDIR/mythessunpoet2011-08-18