unofficial mirror of guix-patches@gnu.org 
 help / color / mirror / code / Atom feed
blob 9771414e7b36a6acb84365308f24776892c4fdf1 7748 bytes (raw)
name: guix/eris/ipfs.scm 	 # note: path name is non-authoritative(*)

  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
 
;;; GNU Guix --- Functional package management for GNU
;;; Copyright © 2018 Ludovic Courtès <ludo@gnu.org>
;;; Copyright © 2022 pukkamustard <pukkamustard@posteo.net>
;;;
;;; This file is part of GNU Guix.
;;;
;;; GNU Guix is free software; you can redistribute it and/or modify it
;;; under the terms of the GNU General Public License as published by
;;; the Free Software Foundation; either version 3 of the License, or (at
;;; your option) any later version.
;;;
;;; GNU Guix is distributed in the hope that it will be useful, but
;;; WITHOUT ANY WARRANTY; without even the implied warranty of
;;; MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
;;; GNU General Public License for more details.
;;;
;;; You should have received a copy of the GNU General Public License
;;; along with GNU Guix.  If not, see <http://www.gnu.org/licenses/>.


;;; Commentary:
;;;
;;; This module provides an interface to the IPFS daemons HTTP API for storing
;;; and retrieving blocks. This can be used to store blocks of ERIS encoded
;;; content.
;;; 
;;; See also the IPFS API documentation:
;;; https://docs.ipfs.io/reference/http/api/#api-v0-block-put

(define-module (guix eris ipfs)
  #:use-module (eris utils base32)
  #:use-module (sodium generichash)
  #:use-module (json)
  #:use-module (web uri)
  #:use-module (web client)
  #:use-module (web response)
  #:use-module (srfi srfi-71)
  #:use-module (rnrs io ports)
  #:use-module (rnrs bytevectors)

  #:use-module ((guix build download)
                #:select ((open-connection-for-uri
                           . guix:open-connection-for-uri)))
  #:export (%ipfs-base-url

	    eris-ipfs-reducer
            eris-ipfs-ref))


;; CID encoding

;; Multicodec codes
;; (https://github.com/multiformats/multicodec/blob/master/table.csv)
(define multicodec-raw-code #x55)
(define multicodec-blake2b-256-code #xb220)

(define (blake2b-256->binary-cid hash)
  "Encode a Blake2b-256 hash as binary CID"
  (call-with-values
      (lambda () (open-bytevector-output-port))
    (lambda (port get-bytevector)
      ;; CID version
      (put-u8 port 1)
      ;; multicoded content-type
      (put-u8 port multicodec-raw-code)
      ;; set multihash to blake2b-256. This is the manually encoded varint of
      ;; 0xb220
      (put-u8 port 160) (put-u8 port 228) (put-u8 port 2)
      ;; set hash lenght
      (put-u8 port 32)
      ;; and finally the hash itself
      (put-bytevector port hash)

      ;; finalize and get the bytevector
      (get-bytevector))))

(define (binary-cid->cid bcid)
  "Encode a binary CID as Base32 encoded CID"
  ;; 'b' is the multibsae code for base32
  (string-append "b"
                 ;; the IPFS daemon uses lower-case, so to be consistent we
                 ;; also.
                 (string-downcase
                  ;; base32 encode the binary cid
                  (base32-encode bcid))))

(define blake2b-256->cid
  (compose binary-cid->cid blake2b-256->binary-cid))


;; IPFS API

(define %ipfs-base-url
  ;; URL of the IPFS gateway.
  (make-parameter "http://localhost:5001"))

(define* (call url decode
               #:optional
               (method http-post)
               #:key port body (false-if-404? #t) (headers '())
               (keep-alive #t)
               (open-connection guix:open-connection-for-uri)
               (timeout 10))
  "Invoke the endpoint at URL using METHOD.  Decode the resulting JSON body
using DECODE, a one-argument procedure that takes an input port; when DECODE
is false, return the input port.  When FALSE-IF-404? is true, return #f upon
404 responses."
  (let* ((url (if (string? url) (string->uri url)  url))
         (port (or port (open-connection url #:timeout timeout)))
         (response response-port
                   (if keep-alive
                       (method url #:streaming? #t
                               #:body body
                               #:port port
                               #:keep-alive? #t)
                       (method url #:streaming? #t
                               #:body body
                               #:port port
                               ;; IPFS daemon seems to responds with bad
                               ;; request if PUT requests are kept alive and
                               ;; do not have "Connection: close" header.
                               #:keep-alive? #f
                               #:headers `((connection close)
                                           ,@headers)))))
    (cond ((= 200 (response-code response))
           (if decode
               (let ((result (decode response-port)))
                 (close-port response-port)
                 result)
               response-port))
          ((and false-if-404?
                (= 404 (response-code response)))
           (close-port response-port)
           #f)
          (else
           (close-port response-port)
           (format #t "~a\n" response)
           (throw 'ipfs-error url response)))))

(define-syntax-rule (false-if-ipfs-error exp)
  "Return $f if EXP triggers a network related or IPFS related exception."
  (with-exception-handler
      (lambda (exn)
        (let ((kind (exception-kind exn))
              (errno (system-error-errno
                      (cons 'system-error (exception-args exn)))))
          (cond
           ((= errno ECONNREFUSED) #f)
           (else (raise-exception exp)))))
    (lambda () exp)
    #:unwind? #t))

(define %multipart-boundary
  ;; XXX: We might want to find a more reliable boundary.
  (string-append (make-string 24 #\-) "2698127afd7425a6"))

(define (bytevector->form-data bv port)
  "Write to PORT a 'multipart/form-data' representation of BV."
  (display (string-append "--" %multipart-boundary "\r\n"
                          "Content-Disposition: form-data\r\n"
                          "Content-Type: application/octet-stream\r\n\r\n")
           port)
  (put-bytevector port bv)
  (display (string-append "\r\n--" %multipart-boundary "--\r\n")
           port))

(define (ipfs-block-put bv)
  "Store a block on IPFS and return the CID of the block"
  (call (string-append (%ipfs-base-url)
                       "/api/v0/block/put"
                       "?format=raw&mhtype=blake2b-256")
        (lambda (port) (assoc-ref (json->scm port) "Key"))
        #:headers `((content-type
                     . (multipart/form-data
                        (boundary . ,%multipart-boundary))))
        #:body (call-with-bytevector-output-port
                (lambda (port) (bytevector->form-data bv port)))
        ;; IPFS daemon does not seem to accept connection re-use when putting
        ;; blocks.
        #:keep-alive #f))

(define* (ipfs-block-get cid #:key
                         (open-connection guix:open-connection-for-uri))
  "Get a block from IPFS via the HTTP API"
  (false-if-ipfs-error
   (call (string-append (%ipfs-base-url)
		        "/api/v0/block/get"
		        "?arg=" cid)
	 get-bytevector-all
         #:timeout 5
         #:open-connection open-connection)))

;; ERIS block reducer

(define eris-ipfs-reducer
  (case-lambda
    ;; initialization. Nothing to do here. In an improved implementation we
    ;; might create a single HTTP connection and reuse it for all blocks.
    (() '())

    ;; Completion. Again, nothing to do.
    ((_) 'done)

    ;; store a block
    ((_ ref-block)
     ;; ref-block is a pair consisting of the reference to the block and the
     ;; block itself.
     (ipfs-block-put (cdr ref-block)))))

(define* (eris-ipfs-ref ref #:key
                        (open-connection guix:open-connection-for-uri))
  "Dereference a block from IPFS"
  (ipfs-block-get (blake2b-256->cid ref)
                  #:open-connection open-connection))

debug log:

solving 9771414e7b ...
found 9771414e7b in https://yhetil.org/guix-patches/20221229181327.758-9-pukkamustard@posteo.net/

applying [1/1] https://yhetil.org/guix-patches/20221229181327.758-9-pukkamustard@posteo.net/
diff --git a/guix/eris/ipfs.scm b/guix/eris/ipfs.scm
new file mode 100644
index 0000000000..9771414e7b

1:32: trailing whitespace.
;;; 
Checking patch guix/eris/ipfs.scm...
Applied patch guix/eris/ipfs.scm cleanly.
warning: 1 line adds whitespace errors.

index at:
100644 9771414e7b36a6acb84365308f24776892c4fdf1	guix/eris/ipfs.scm

(*) Git path names are given by the tree(s) the blob belongs to.
    Blobs themselves have no identifier aside from the hash of its contents.^

Code repositories for project(s) associated with this public inbox

	https://git.savannah.gnu.org/cgit/guix.git

This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox;
as well as URLs for read-only IMAP folder(s) and NNTP newsgroup(s).