-
Notifications
You must be signed in to change notification settings - Fork 2
/
examples_test.go
129 lines (105 loc) · 2.79 KB
/
examples_test.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
package wos_test
import (
"bytes"
"context"
"encoding/json"
"fmt"
"io"
"net/http"
"github.com/conduition/wos"
)
func ExampleCreateWallet() {
wallet, creds, err := wos.CreateWallet(context.Background(), nil)
if err != nil {
panic(err)
}
fmt.Println(wallet.LightningAddress())
fmt.Printf("API token: %s\n", creds.APIToken)
fmt.Printf("API secret: %s\n", creds.APISecret)
}
func ExampleReader() {
reader := wos.NewReader("edcc867c-96ff-4b0d-ba68-165c16071de0", nil)
addresses, err := reader.Addresses(context.Background())
if err != nil {
panic(err)
}
fmt.Println(addresses.Lightning)
// output:
}
func ExampleCredentials_OpenWallet() {
creds := wos.Credentials{
APIToken: "edcc867c-96ff-4b0d-ba68-165c16071de0",
APISecret: "91ul0rDKV1gANhQWWyEXhdWaSa6aQwAF",
}
wallet, err := creds.OpenWallet(context.Background(), nil)
if err != nil {
panic(err)
}
fmt.Println(wallet.LightningAddress().String())
// output:
}
func ExampleWallet_PayLightningAddress() {
creds := wos.Credentials{
APIToken: "edcc867c-96ff-4b0d-ba68-165c16071de0",
APISecret: "91ul0rDKV1gANhQWWyEXhdWaSa6aQwAF",
}
ctx := context.Background()
wallet, err := creds.OpenWallet(ctx, nil)
if err != nil {
panic(err)
}
// API token: 6edf02b8-d4e9-4640-b7e4-90bc97f476ab
// API secret: sgN5hn2RibvSba1vv260NvwnwVy0oiuh
lnAddress, err := wos.ParseLightningAddress("[email protected]")
if err != nil {
panic(err)
}
payment, err := wallet.PayLightningAddress(ctx, lnAddress, "", 0.00000001)
if err != nil {
panic(err)
}
fmt.Println(payment.Status)
}
type RemoteSigner struct {
URL string
}
func (rs RemoteSigner) SignRequest(
ctx context.Context,
endpoint, nonce, requestBody, apiToken string,
) ([]byte, error) {
bodyBytes, err := json.Marshal(map[string]string{
"endpoint": endpoint,
"nonce": nonce,
"body": requestBody,
// apiToken omitted for security; assume the remote knows it already.
})
if err != nil {
return nil, err
}
req, err := http.NewRequestWithContext(ctx, "POST", rs.URL, bytes.NewReader(bodyBytes))
if err != nil {
return nil, err
}
req.Header.Set("Content-Type", "application/json")
resp, err := http.DefaultClient.Do(req)
if err != nil {
return nil, err
}
defer resp.Body.Close()
if resp.StatusCode != 200 {
return nil, fmt.Errorf("received status code %d from remote signer", resp.StatusCode)
}
return io.ReadAll(resp.Body)
}
func ExampleSigner() {
reader := wos.NewReader("93b9c574-30a2-4bf5-81ba-f9feadb313a7", nil)
signer := RemoteSigner{"https://somewheresecure.place/api/sign"}
wallet, err := wos.OpenWallet(context.Background(), reader, signer)
if err != nil {
panic(err)
}
fmt.Println(wallet.LightningAddress())
}