aboutsummaryrefslogtreecommitdiff
path: root/lib/_ak_ipfs
blob: 8780f5137d4549722b05bde081528ed4c48ee638 (plain)
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
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
#!/bin/bash
source $AK_LIBDIR/_ak_log
AK_IPFS_REPO="$AK_WORKDIR/ipfsrepo"

_ak_ipfs(){
    export IPFS_PATH=$AK_IPFS_REPO; ipfs $*
}

_ak_ipfs_get_peers(){
    _ak_ipfs swarm peers 1> /dev/null 2>&1
    if [ $? == 0 ]
    then
        _ak_ipfs swarm peers > $AK_WORKDIR/peers.ipfs
    fi
}

_ak_ipfs_scanner(){
    peersIPFSfile="$AK_WORKDIR/peers.ipfs"
    ak_peersIPFSfile="$AK_WORKDIR/ipfs.peers.akn"
    if [ ! -f $peersIPFSfile ]
    then
        _ak_ipfs_get_peers
        if [ $? -ne 0 ]
        then
            exit 1
        fi
    fi
    counter=0

    printf '[' > walk.aknet
    cat $peersIPFSfile \
        | cut -d '/' -f 2,3,7 \
        | sort \
        | uniq \
        | while read -r line || [ -n "$line" ]
    do
        protocol="`printf '%s' "$line" | cut -d '/' -f 1`"
        ip="`printf '%s' "$line" | cut -d '/' -f 2`"
        ipfsId="`printf '%s' "$line" | cut -d '/' -f 3`"
        if [ "$protocol" == "ip6" ]
        then
            url="http://[$ip]:8610/v0/node_info"
        else
            url="http://$ip:8610/v0/node_info"
        fi
        node_info="`curl \
            --connect-timeout 3 \
            -A 'akd/0.1.0; https://github.com/arching-kaos' \
            "$url" 2>/dev/null`"
        if [ $? -eq 0 ]
        then
            if [ "$counter" -ne "0" ]
            then
                printf ',' >> walk.aknet
            fi
            printf '{"ipfsPublicKey":"%s","ip":"%s","node_info":%s}' \
                "$ipfsId" "$ip" "$node_info" >> walk.aknet
            counter="`expr $counter + 1`"
        fi
    done
    printf ']' >> walk.aknet

    mv walk.aknet $ak_peersIPFSfile
}

_ak_ipfs_add(){
    if [ -z $1 ] || [ ! -n "$1" ]
    then
        logit "ERROR" "no argument given"
        exit 1
    fi
    # Receives a file
    # Returns a hash
    _ak_ipfs add -Qr "$1"
    if [ $? -ne 0 ]
    then
        logit "ERROR" "Failed to add $1"
        exit 1
    fi
}

_ak_ipfs_block_stat(){
    if [ -z $1 ] || [ ! -n "$1" ]
    then
        logit "ERROR" "no argument given"
        exit 1
    fi
    _ak_ipfs block stat "$1"
    if [ $? -ne 0 ]
    then
        logit "ERROR" "Failed to retrieve stat of block $1"
        exit 1
    fi
}

_ak_ipfs_cat(){
    if [ -z $1 ] || [ ! -n "$1" ]
    then
        logit "ERROR" "no argument given"
        exit 1
    fi
    _ak_ipfs --timeout=10s cat $1
    if [ "$?" -ne "0" ]
    then
        logit "ERROR" "Failed to cat $1"
        exit 1
    fi

}

_ak_ipfs_files_cp(){
    if [ -z $1 ] || [ ! -n "$1" ]
    then
        logit "ERROR" "No argument given"
        exit 1
    fi
    if [ -z $2 ] || [ ! -n "$2" ]
    then
        logit "ERROR" "No argument given"
        exit 1
    fi
    _ak_ipfs files cp "$1" "$2"
    if [ $? -ne 0 ]
    then
        logit "ERROR" "Failed to copy $1 to $2"
        exit 1
    fi
}

_ak_ipfs_files_ls(){
    _ak_ipfs files ls "$1"
    if [ $? -ne 0 ]
    then
        logit "ERROR" "Failed to list $1"
        exit 1
    fi
}

_ak_ipfs_files_mkdir(){
    if [ -z $1 ] || [ ! -n "$1" ]
    then
        logit "ERROR" "No argument given"
        exit 1
    fi
    _ak_ipfs files mkdir "$1"
    if [ $? -ne 0 ]
    then
        logit "ERROR" "Failed to mkdir $1"
        exit 1
    fi
}

_ak_ipfs_files_mv(){
    if [ -z $1 ] || [ ! -n "$1" ]
    then
        logit "ERROR" "No argument given"
        exit 1
    fi
    if [ -z $2 ] || [ ! -n "$2" ]
    then
        logit "ERROR" "No argument given"
        exit 1
    fi
    _ak_ipfs files mv "$1" "$2"
    if [ $? -ne 0 ]
    then
        logit "ERROR" "Failed to move $1 to $2"
        exit 1
    fi
}

_ak_ipfs_files_rm(){
    if [ -z $1 ] || [ ! -n "$1" ]
    then
        logit "ERROR" "No argument given"
        exit 1
    fi
    _ak_ipfs files rm "$1"
    if [ $? -ne 0 ]
    then
        logit "ERROR" "Failed to remove $1"
        exit 1
    fi
}

_ak_ipfs_files_stat(){
    if [ -z $1 ] || [ ! -n "$1" ]
    then
        logit "ERROR" "No argument given"
        exit 1
    fi
    _ak_ipfs files stat "$1"
    if [ $? -ne 0 ]
    then
        logit "ERROR" "Failed to get file's stats $1"
        exit 1
    fi
}

_ak_ipfs_get(){
    if [ -z $1 ] || [ ! -n "$1" ]
    then
        logit "ERROR" "No argument given"
        exit 1
    fi
    _ak_ipfs --timeout=10s get "$1" > /dev/null 2>&1
    if [ $? -ne 0 ]
    then
        logit "ERROR" "Failed to get $1"
        exit 1
    fi
}

_ak_ipfs_key_gen(){
    if [ -z $1 ] || [ ! -n "$1" ]
    then
        logit "ERROR" "No argument given"
        exit 1
    fi
    _ak_ipfs key gen "$1"
    if [ $? -ne 0 ]
    then
        logit "ERROR" "Failed to generate key $1"
        exit 1
    fi
}

_ak_ipfs_key_list(){
    _ak_ipfs key list
    if [ $? -ne 0 ]
    then
        logit "ERROR" "Failed to get key list"
        exit 1
    fi
}

_ak_ipfs_key_list_full(){
    _ak_ipfs key list -l
    if [ $? -ne 0 ]
    then
        logit "ERROR" "Failed to get key list"
        exit 1
    fi
}

_ak_ipfs_name_publish(){
    if [ -z $1 ] || [ ! -n "$1" ]
    then
        logit "ERROR" "No argument given"
        exit 1
    fi
    if [ -z $2 ] || [ ! -n "$2" ]
    then
        logit "ERROR" "No argument given"
        exit 1
    fi
    _ak_ipfs name publish "$1" "$2"
    if [ $? -ne 0 ]
    then
        logit "ERROR" "Failed to get $1"
        exit 1
    fi

}

_ak_ipfs_name_resolve(){
    if [ -z $1 ] || [ ! -n "$1" ]
    then
        logit "ERROR" "No argument given"
        exit 1
    fi
    _ak_ipfs name resolve "$1"
    if [ $? -ne 0 ]
    then
        logit "ERROR" "Failed to resolve $1"
        exit 1
    fi

}

_ak_ipfs_swarm_peers(){
    _ak_ipfs swarm peers
    if [ $? -ne 0 ]
    then
        logit "ERROR" "Failed to get list of peers"
        exit 1
    fi
}

_ak_ipfs_starter(){
    _ak_ipfs daemon --routing=dht --migrate
    if [ $? -ne 0 ]
    then
        logit "ERROR" "Failed to start IPFS daemon"
        exit 1
    fi
}

_ak_ipns_resolve(){
    if [ ! -z $1 ]
    then
        rsld=$(_ak_ipfs_name_resolve $1)
        if [ $? -ne 0 ]
        then
            logit "ERROR" "Failed to resolve $1"
            exit 1
        fi
        echo -n $rsld | sed -e 's/\/ipfs\///'
        logit "INFO" "Resolved $1 to $rsld"
    else
        exit 1
    fi
}

_ak_ipfs_check(){
    _ak_ipfs_files_ls /zarchive > /dev/null
    if [ $? != 0 ]
    then
        logit "ERROR" "/zarchive is missing"
    else
        logit "INFO" "/zarchive OK"
    fi

    _ak_ipfs_files_ls /zlatest > /dev/null
    if [ $? != 0 ]
    then
        logit "ERROR" "/zlatest is missing"
    else
        logit "INFO" "/zlatest is OK"
    fi

    _ak_ipfs_key_list | grep zchain > /dev/null
    if [ $? != 0 ]; then
        logit "WARNING" "zchain key is missing"
        _ak_ipfs_key_gen zchain > $ZCHAIN
        if [ $? != 0 ]; then
            logit "ERROR" "zchain fails to create"
        else
            logit "INFO" "zchain created"
        fi
    else
        logit "INFO" "zchain is there"
    fi

    _ak_ipfs_key_list | grep ak-config > /dev/null
    if [ $? != 0 ]; then
        logit "WARNING" "ak-config key is missing"
        _ak_ipfs_key_gen ak-config
        if [ $? != 0 ]; then
            logit "ERROR" "ak-config fails to create"
        else
            logit "INFO" "ak-config created"
        fi
    else
        logit "INFO" "ak-config is there"
    fi
}

_ak_ipfs_init(){
    if [ ! -d $AK_IPFS_REPO ]
    then
        mkdir $AK_IPFS_REPO
        _ak_ipfs init

    fi
}

_ak_ipfs_download(){
    logit "INFO" "Attempting to install IPFS..."
    IPFS_VERSION="$(curl \
        --connect-timeout 3 \
        -s https://dist.ipfs.tech/kubo/versions | tail -1)"
    if [ "$(uname --machine)" == "x86_64" ]
    then
        ARCH="amd64"
    elif [ "$(uname --machine)" == "armv7l" ]
    then
        ARCH="arm"
    else
        echo "ERROR UNKNOWN ARCHITECTURE $(uname --machine)"
        exit 1
    fi
    IPFS_TARGET_FILE="kubo_"$IPFS_VERSION"_linux-$ARCH.tar.gz"

    logit "INFO" "Downloading ipfs $IPFS_VERSION"
    if [ ! -f $AK_ARCHIVESDIR/$IPFS_TARGET_FILE ]
    then
        wget -O $AK_ARCHIVESDIR/$IPFS_TARGET_FILE https://dist.ipfs.tech/kubo/$IPFS_VERSION/$IPFS_TARGET_FILE ;
    else
        logit "INFO" "Already have the latest version"
        exit 0
    fi
}

_ak_ipfs_cid_v0_check () {
    if [ -z $1 ] || [ ! -n "$1" ]
    then
        logit "ERROR" "No argument given"
        exit 1
    fi
    echo $1 | grep -e 'Qm.\{44\}' > /dev/null
    if [ "$?" -ne 0 ]
    then
        logit "ERROR" "$1 is not an IPFS CIDv0 string"
        exit 1
    fi
    logit "INFO" "$1 provided is an IPFS CIDv0 string"
}

_ak_ipfs_swarm_install() {
    SWARMSHA512SUM="7001e37412758c43d372a969e977ca11511e034c8c1e233a58dc3ce1c6f3c1aa7d2da8cba9944a5eabaa8885742bfe6cc6794224c146b7129da8f633b53b9cfc"

    if [ ! -f $AK_IPFS_REPO/swarm.key ]
    then
        logit "INFO" "Downloading swarm key"
        wget -O $AK_IPFS_REPO/swarm.key https://arching-kaos.net/files/swarm.key
    elif [ -f $AK_IPFS_REPO/swarm.key ] && [ "$(sha512sum $AK_IPFS_REPO/swarm.key | awk '{ print $1 }')" == "$SWARMSHA512SUM" ]
    then
        logit "INFO" "Congrats! You are already in our swarm"
    else
        logit "ERROR" "Found swarm.key but not ours"
        logit "ERROR" "Visit https://arching-kaos.net/files/swarm.key and copy it to your ipfs folder"
    fi
}