-
Notifications
You must be signed in to change notification settings - Fork 17
/
Copy pathmain.go
134 lines (124 loc) · 3.07 KB
/
main.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
128
129
130
131
132
133
134
package main
import (
"context"
"crypto/sha256"
"fmt"
"log"
"os"
"os/exec"
"path"
"strconv"
"strings"
"syscall"
"github.com/filecoin-project/venus-wallet/api/remotecli"
"github.com/filecoin-project/venus-wallet/api/remotecli/httpparse"
"github.com/filecoin-project/venus-wallet/crypto"
wallet_api "github.com/filecoin-project/venus/venus-shared/api/wallet"
"github.com/filecoin-project/venus/venus-shared/types"
"github.com/filecoin-project/venus/venus-shared/types/gateway"
)
type RemoteWallet struct {
wallet_api.IWallet
Cancel func()
}
func SetupRemoteWallet(info string) (*RemoteWallet, error) {
ai, err := httpparse.ParseApiInfo(info)
if err != nil {
return nil, err
}
url, err := ai.DialArgs()
if err != nil {
return nil, err
}
wapi, closer, err := remotecli.NewWalletRPC(context.Background(), url, ai.AuthHeader())
if err != nil {
return nil, fmt.Errorf("creating jsonrpc client: %w", err)
}
return &RemoteWallet{
IWallet: wapi,
Cancel: closer,
}, nil
}
func (w *RemoteWallet) Get() wallet_api.IWallet {
if w == nil {
return nil
}
return w
}
// How to access remote wallet
func main() {
// env to prepare
// mock production environment
dir, err := os.Getwd()
if err != nil {
log.Fatal(err)
}
defer func() {
os.Remove(path.Join(dir, "example", "pid.tmp"))
os.Remove(path.Join(dir, "example", "remote-token.tmp"))
}()
cmd := exec.Cmd{
Path: path.Join(dir, "example", "wallet-setup.sh"),
Args: []string{"./wallet-setup.sh", "."},
Stdout: os.Stdout,
Stderr: os.Stdout,
}
err = cmd.Start()
if err != nil {
log.Fatal(err)
}
err = cmd.Wait()
if err != nil {
log.Fatal(err)
}
tb, err := os.ReadFile(path.Join(dir, "example", "remote-token.tmp"))
if err != nil {
log.Fatal(err)
}
token := strings.TrimSpace(string(tb))
pb, err := os.ReadFile(path.Join(dir, "example", "pid.tmp"))
if err != nil {
log.Fatal(err)
}
pid, err := strconv.Atoi(strings.TrimSpace(string(pb)))
if err != nil {
log.Fatal(err)
}
// remote wallet setup
remoteWallet, err := SetupRemoteWallet(token)
if err != nil {
log.Fatal(err)
}
addr, err := remoteWallet.WalletNew(context.Background(), types.KTSecp256k1)
if err != nil {
log.Fatalf("remote wallet new address error:%s", err)
}
log.Println("new address ", addr.String())
addrs, err := remoteWallet.WalletList(context.Background())
if err != nil {
log.Fatalf("remote wallet list addresses error:%s", err)
}
for _, v := range addrs {
log.Println(v.String())
}
exist, err := remoteWallet.WalletHas(context.Background(), addr)
if err != nil {
log.Fatalf("remote wallet check address exist error:%s", err)
}
log.Printf("addr:%s exist:%v", addr.String(), exist)
sh := sha256.New()
signData := sh.Sum(gateway.RandomBytes)
sig, err := remoteWallet.WalletSign(context.Background(), addr, signData, types.MsgMeta{Type: types.MTVerifyAddress})
if err != nil {
log.Fatalf("wallet sign: %v", err)
}
err = crypto.Verify(sig, addr, signData)
if err != nil {
log.Fatalf("verify signature: %v", err)
}
remoteWallet.Cancel()
err = syscall.Kill(pid, 9)
if err != nil {
log.Fatal(err)
}
}