forked from llSourcell/kerala
-
Notifications
You must be signed in to change notification settings - Fork 0
/
ipfs.go
314 lines (214 loc) · 7.64 KB
/
ipfs.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
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
package kerala
import (
"github.com/ipfs/go-ipfs/core"
"github.com/ipfs/go-ipfs/repo/fsrepo"
"golang.org/x/net/context"
"fmt"
u "github.com/ipfs/go-ipfs-util"
merkledag "github.com/ipfs/go-ipfs/merkledag"
"net/http"
"bytes"
"strings"
"io/ioutil"
"encoding/hex"
"encoding/json"
commands "github.com/ipfs/go-ipfs/core/commands"
)
func StartNode() (*core.IpfsNode, error) {
//[1] init IPFS node
builder := core.NewNodeBuilder().Online()
r := fsrepo.At("~/.go-ipfs")
if err := r.Open(); err != nil {
return nil, err
}
builder.SetRepo(r)
// Make our 'master' context and defer cancelling it
ctx, _ := context.WithCancel(context.Background())
//defer cancel()
node, err := builder.Build(ctx)
if err != nil {
return nil, err
}
return node, nil
}
func GetStrings(node *core.IpfsNode, userID string) ([]string, error) {
var Key = u.B58KeyDecode(userID)
var tweetArray = resolveAllInOrder(node,Key)
return tweetArray, nil
}
func GetDAG(node *core.IpfsNode, id string) (u.Key, error) {
pointsTo, err := node.Namesys.Resolve(node.Context(), id)
return pointsTo, err
}
func resolveAllInOrder(nd * core.IpfsNode, k u.Key) []string {
var stringArr []string
var node * merkledag.Node
node, err := nd.DAG.Get(k)
fmt.Printf("the node is", node)
if err != nil {
fmt.Println(err)
//return
}
fmt.Printf("bout to crash")
fmt.Printf("%s ", string(node.Data[:]))
fmt.Println("not crashed ")
for ;; {
var err error
if len(node.Links) == 0 {
break;
}
node, err = node.Links[0].GetNode(nd.DAG)
if err != nil {
fmt.Println(err)
}
fmt.Printf("%s ", string(node.Data[:]))
stringArr = append(stringArr, string(node.Data[:]))
}
fmt.Printf("\n");
return stringArr
}
func AddString(node *core.IpfsNode, inputString string) (u.Key, error) {
pointsTo, err := node.Namesys.Resolve(node.Context(), node.Identity.Pretty())
//If there is an error, user is new and hasn't yet created a DAG.
if err != nil {
//[3] Initialize a MerkleDAG node and key
var NewNode * merkledag.Node
var Key u.Key
//[4] Fill the node with user input
NewNode = makeStringNode(inputString)
//[5] Add the node to IPFS
Key, _ = node.DAG.Add(NewNode)
// //publish to IPNS
output, err := commands.Publish(node, node.PrivateKey,Key.B58String())
if err != nil {
fmt.Println(err)
} else {
fmt.Println("You published to IPNS. Your peer ID is ", output.Name)
}
return Key, nil
} else {
//[7] Initialize a new MerkleDAG node and key
var NewNode * merkledag.Node
var Key u.Key
//[8] Fill the node with user input
NewNode = makeStringNode(inputString)
//[10] Convert it into a key
Key = u.B58KeyDecode(pointsTo.B58String())
//[11] Get the Old MerkleDAG node and key
var OldNode * merkledag.Node
var Key2 u.Key
OldNode, _ = node.DAG.Get(Key)
//[12]Add a link to the old node
NewNode.AddNodeLink("next", OldNode)
//[13] Add thew new node to IPFS
Key2, _ = node.DAG.Add(NewNode)
// //publish to IPNS
output, err := commands.Publish(node, node.PrivateKey,Key2.B58String())
if err != nil {
fmt.Println(err)
} else {
fmt.Println("You published to IPNS. Your peer ID is ", output.Name)
}
return Key2, nil
}
}
func makeStringNode(s string) * merkledag.Node {
n := new(merkledag.Node)
n.Data = make([]byte, len(s))
copy(n.Data, s)
return n;
}
func Pay(fee string, from_address string, to_address string, amount string, asset_id string, private_key string) (string) {
unsignedResponse := sendasset(fee,from_address,to_address,amount,asset_id)
signedResponse := signtransaction(unsignedResponse, private_key)
transactionHash := pushtransaction(signedResponse)
return transactionHash
}
func sendasset(fee_placeholder string, from_address string, to_address string, amount_placeholder string, asset_id_placeholder string) (string) {
client := &http.Client{}
str := "{\n \"fees\": fee_placeholder,\n \"from\": \"from_address\",\n \"to\": [\n {\n \"address\": \"to_address\",\n \"amount\": \"amount_placeholder\",\n \"asset_id\": \"asset_id_placeholder\"\n }\n ]\n}"
strings.Replace(str, "fee_placeholder", fee_placeholder, -1)
strings.Replace(str, "from_address", from_address, -1)
strings.Replace(str, "to_address", to_address, -1)
strings.Replace(str, "amount_placeholder", amount_placeholder, -1)
strings.Replace(str, "asset_id_placeholder", asset_id_placeholder, -1)
body := []byte(str)
req, _ := http.NewRequest("POST", "https://private-anon-e4123b065-coinprism.apiary-mock.com/v1/sendasset?format=json", bytes.NewBuffer(body))
req.Header.Add("Content-Type", "application/json")
resp, err := client.Do(req)
if err != nil {
fmt.Println("Error when sending request to the server")
}
defer resp.Body.Close()
resp_body, _ := ioutil.ReadAll(resp.Body)
fmt.Println(resp.Status)
fmt.Println(string(resp_body))
strhex := hex.EncodeToString(resp_body)
return string(strhex)
}
func signtransaction(hex_placeholder string, priv_key_placeholder string) (string) {
client := &http.Client{}
str := "{\n \"transaction\": \"hex_placeholder\",\n \"keys\": [\n \"priv_key_placeholder\"\n ]\n}"
strings.Replace(str, "hex_placeholder", hex_placeholder, -1)
strings.Replace(str, "priv_key_placeholder", priv_key_placeholder, -1)
body := []byte(str)
req, _ := http.NewRequest("POST", "https://private-anon-e4123b065-coinprism.apiary-mock.com/v1/signtransaction", bytes.NewBuffer(body))
req.Header.Add("Content-Type", "application/json")
resp, err := client.Do(req)
if err != nil {
fmt.Println("Errored when sending request to the server")
}
defer resp.Body.Close()
resp_body, _ := ioutil.ReadAll(resp.Body)
return string(resp_body)
}
func pushtransaction(input_placeholder string) (string) {
client := &http.Client{}
str := "\"input_placeholder\""
strings.Replace(str, "input_placeholder", input_placeholder, -1)
body := []byte(str)
req, _ := http.NewRequest("POST", "https://private-anon-e4123b065-coinprism.apiary-mock.com/v1/sendrawtransaction", bytes.NewBuffer(body))
req.Header.Add("Content-Type", "application/json")
resp, err := client.Do(req)
if err != nil {
fmt.Println("Error when sending request to the server")
}
defer resp.Body.Close()
resp_body, _ := ioutil.ReadAll(resp.Body)
return string(resp_body)
}
func GetBalance(my_address string) (float64) {
client := &http.Client{}
req, _ := http.NewRequest("GET", "https://private-anon-e4123b065-coinprism.apiary-mock.com/v1/addresses/address", nil)
resp, err := client.Do(req)
if err != nil {
fmt.Println("Error when sending request to the server")
}
defer resp.Body.Close()
resp_body, _ := ioutil.ReadAll(resp.Body)
var dat map[string]interface{}
if err := json.Unmarshal([]byte(string(resp_body)), &dat); err != nil {
panic(err)
}
num := dat["balance"]
fmt.Println("NUM" , num.(float64))
return num.(float64)
}
func GenerateAddress() (string) {
client := &http.Client{}
body := []byte("{\n \"alias\": \"address label\"\n}")
req, _ := http.NewRequest("POST", "https://private-anon-e4123b065-coinprism.apiary-mock.com/v1/account/createaddress", bytes.NewBuffer(body))
req.Header.Add("Content-Type", "application/json")
req.Header.Add("X-Coinprism-Username", "username")
req.Header.Add("X-Coinprism-Password", "account-password")
resp, err := client.Do(req)
if err != nil {
fmt.Println("Errored when sending request to the server")
return
}
defer resp.Body.Close()
resp_body, _ := ioutil.ReadAll(resp.Body)
fmt.Println(resp.Status)
return string(resp_body)
}
}