This repository has been archived by the owner on Apr 11, 2022. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathcontract.go
141 lines (119 loc) · 3.4 KB
/
contract.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
135
136
137
138
139
140
141
package runlua
import (
"bytes"
"crypto/sha256"
"encoding/hex"
"encoding/json"
"errors"
"io/ioutil"
"net/http"
"strconv"
"github.com/btcsuite/btcd/chaincfg"
"github.com/btcsuite/btcutil"
decodepay "github.com/fiatjaf/ln-decodepay"
)
func make_lua_http(makeRequest func(*http.Request) (*http.Response, error)) (
lua_http_gettext func(string, ...map[string]interface{}) (string, error),
lua_http_getjson func(string, ...map[string]interface{}) (interface{}, error),
lua_http_postjson func(string, interface{}, ...map[string]interface{}) (interface{}, error),
calls_p *int,
) {
calls := 0
calls_p = &calls
http_call := func(method, url string, body interface{}, headers ...map[string]interface{}) (b []byte, err error) {
log.Debug().Str("method", method).Interface("body", body).Str("url", url).Msg("http call from contract")
bodyjson := new(bytes.Buffer)
if body != nil {
err = json.NewEncoder(bodyjson).Encode(body)
if err != nil {
log.Warn().Err(err).Msg("http: failed to encode body")
return
}
headers = append([]map[string]interface{}{{"Content-Type": "application/json"}}, headers...)
}
req, err := http.NewRequest(method, url, bodyjson)
if err != nil {
log.Warn().Err(err).Msg("http: failed to create request")
return
}
defer req.Body.Close()
for _, headermap := range headers {
for k, v := range headermap {
if sv, ok := v.(string); ok {
req.Header.Set(k, sv)
}
}
}
resp, err := makeRequest(req)
if err != nil {
log.Warn().Err(err).Msg("http: failed to make request")
return
}
if resp.StatusCode >= 300 {
log.Debug().Err(err).Int("code", resp.StatusCode).Msg("http: got bad status")
err = errors.New("response status code: " + strconv.Itoa(resp.StatusCode))
return
}
b, err = ioutil.ReadAll(resp.Body)
if err != nil {
log.Warn().Err(err).Msg("http: failed to read body")
return
}
return b, nil
}
lua_http_gettext = func(url string, headers ...map[string]interface{}) (t string, err error) {
respbytes, err := http_call("GET", url, nil, headers...)
if err != nil {
return "", err
}
return string(respbytes), nil
}
lua_http_getjson = func(url string, headers ...map[string]interface{}) (j interface{}, err error) {
respbytes, err := http_call("GET", url, nil, headers...)
if err != nil {
return nil, err
}
var value interface{}
err = json.Unmarshal(respbytes, &value)
if err != nil {
return nil, err
}
return value, nil
}
lua_http_postjson = func(url string, body interface{}, headers ...map[string]interface{}) (j interface{}, err error) {
respbytes, err := http_call("POST", url, body, headers...)
if err != nil {
return nil, err
}
var value interface{}
err = json.Unmarshal(respbytes, &value)
if err != nil {
return nil, err
}
return value, nil
}
return
}
func lua_sha256(preimage string) (hash string, err error) {
h := sha256.New()
_, err = h.Write([]byte(preimage))
if err != nil {
return "", err
}
hash = hex.EncodeToString(h.Sum(nil))
return hash, nil
}
func lua_parse_bolt11(bolt11 string) (map[string]interface{}, error) {
inv, err := decodepay.Decodepay(bolt11)
if err != nil {
return nil, err
}
jinv, _ := json.Marshal(inv)
minv := make(map[string]interface{})
json.Unmarshal(jinv, &minv)
return minv, nil
}
func lua_check_btc_address(address string) error {
_, err := btcutil.DecodeAddress(address, &chaincfg.MainNetParams)
return err
}