forked from denkhaus/go-bitshares
-
Notifications
You must be signed in to change notification settings - Fork 2
/
memobuilder.go
59 lines (48 loc) · 1.28 KB
/
memobuilder.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
package cocos
import (
"math/rand"
"github.com/gkany/cocos-go/crypto"
"github.com/gkany/cocos-go/types"
"github.com/juju/errors"
)
// MemoBuilder is a memo factory
type MemoBuilder struct {
from types.GrapheneObject
to types.GrapheneObject
api WebsocketAPI
memo string
}
//NewMemoBuilder creates a new MemoBuilder
func (p *websocketAPI) NewMemoBuilder(from, to types.GrapheneObject, memo string) *MemoBuilder {
builder := MemoBuilder{
from: from,
to: to,
memo: memo,
api: p,
}
return &builder
}
// Encrypt encrypts the memo message with the corresponding private key found in keyBag
func (p *MemoBuilder) Encrypt(keyBag *crypto.KeyBag) (*types.Memo, error) {
accts, err := p.api.GetAccounts(p.from, p.to)
if err != nil {
return nil, errors.Annotate(err, "GetAccounts")
}
from := accts.Lookup(p.from)
if from == nil {
return nil, errors.Errorf("can't retrieve account infos for %s", p.from)
}
to := accts.Lookup(p.to)
if to == nil {
return nil, errors.Errorf("can't retrieve account infos for %s", p.to)
}
memo := types.Memo{
From: from.Options.MemoKey,
To: to.Options.MemoKey,
Nonce: types.UInt64(rand.Uint64()),
}
if err := keyBag.EncryptMemo(&memo, p.memo); err != nil {
return nil, errors.Annotate(err, "EncryptMemo")
}
return &memo, nil
}