forked from Onyx-Protocol/Onyx
/
receivers.go
38 lines (32 loc) · 891 Bytes
/
receivers.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
package account
import (
"context"
"time"
"chain/core/txbuilder"
"chain/errors"
)
const defaultReceiverExpiry = 30 * 24 * time.Hour // 30 days
// CreateReceiver creates a new account receiver for an account
// with the provided expiry. If a zero time is provided for the
// expiry, a default expiry of 30 days from the current time is
// used.
func (m *Manager) CreateReceiver(ctx context.Context, accID, accAlias string, expiresAt time.Time) (*txbuilder.Receiver, error) {
if expiresAt.IsZero() {
expiresAt = time.Now().Add(defaultReceiverExpiry)
}
if accAlias != "" {
s, err := m.FindByAlias(ctx, accAlias)
if err != nil {
return nil, err
}
accID = s.ID
}
cp, err := m.CreateControlProgram(ctx, accID, false, expiresAt)
if err != nil {
return nil, errors.Wrap(err)
}
return &txbuilder.Receiver{
ControlProgram: cp,
ExpiresAt: expiresAt,
}, nil
}