forked from irinazheltisheva/powergate
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathapi_wallet.go
More file actions
78 lines (69 loc) · 1.68 KB
/
api_wallet.go
File metadata and controls
78 lines (69 loc) · 1.68 KB
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
package api
import (
"context"
"fmt"
"math/big"
)
// Addrs returns the wallet addresses.
func (i *API) Addrs() []AddrInfo {
i.lock.Lock()
defer i.lock.Unlock()
var addrs []AddrInfo
for _, addr := range i.cfg.Addrs {
addrs = append(addrs, addr)
}
return addrs
}
// NewAddr creates a new address managed by the FFS instance.
func (i *API) NewAddr(ctx context.Context, name string, options ...NewAddressOption) (string, error) {
i.lock.Lock()
defer i.lock.Unlock()
conf := &NewAddressConfig{
makeDefault: false,
addressType: "bls",
}
for _, option := range options {
option(conf)
}
exists := false
for _, addr := range i.cfg.Addrs {
if addr.Name == name {
exists = true
break
}
}
if exists {
return "", fmt.Errorf("address with name %s already exists", name)
}
addr, err := i.wm.NewAddress(ctx, conf.addressType)
if err != nil {
return "", fmt.Errorf("creating new wallet addr: %s", err)
}
i.cfg.Addrs[addr] = AddrInfo{
Name: name,
Addr: addr,
Type: conf.addressType,
}
if conf.makeDefault {
i.cfg.DefaultStorageConfig.Cold.Filecoin.Addr = addr
}
if err := i.is.putInstanceConfig(i.cfg); err != nil {
return "", err
}
return addr, nil
}
// SendFil sends fil from a managed address to any another address, returns immediately but funds are sent asynchronously.
func (i *API) SendFil(ctx context.Context, from string, to string, amount *big.Int) error {
if !i.isManagedAddress(from) {
return fmt.Errorf("%v is not managed by ffs instance", from)
}
return i.wm.SendFil(ctx, from, to, amount)
}
func (i *API) isManagedAddress(addr string) bool {
for managedAddr := range i.cfg.Addrs {
if managedAddr == addr {
return true
}
}
return false
}