diff --git a/blobsets.nimble b/blobsets.nimble index 9b1e8f4..befbd50 100644 --- a/blobsets.nimble +++ b/blobsets.nimble @@ -6,7 +6,7 @@ description = "Sets of named blobs" license = "AGPLv3" srcDir = "src" -requires "nim >= 0.18.0", "base58", "cbor >= 0.5.1", "siphash", "nimcrypto", "spryvm" +requires "nim >= 0.18.0", "cbor >= 0.5.1", "siphash", "nimcrypto", "spryvm" bin = @["blobset"] skipFiles = @["blobset.nim"] diff --git a/src/blobsets.nim b/src/blobsets.nim index 006ffc8..ea7d30e 100644 --- a/src/blobsets.nim +++ b/src/blobsets.nim @@ -1,5 +1,5 @@ import std/hashes, std/streams, std/strutils, std/bitops, std/unicode, std/endians -import base58/bitcoin, cbor, siphash +import cbor, siphash import ./blobsets/priv/hex import std/streams, std/strutils @@ -103,44 +103,6 @@ proc toBlobId*(cbor: CborNode): BlobId = proc toHex*(id: BlobId|SetId): string = hex.encode(id.data) ## Return BlobId encoded in hexidecimal. -proc writeUvarint*(s: Stream; n: SomeInteger) = - ## Write an IPFS varint - var n = n - while true: - let c = int8(n and 0x7f) - n = n shr 7 - if n == 0: - s.write((char)c.char) - break - else: - s.write((char)c or 0x80) - -proc readUvarint*(s: Stream): BiggestInt = - ## Read an IPFS varint - var shift: int - while shift < (9*8): - let c = (BiggestInt)s.readChar - result = result or ((c and 0x7f) shl shift) - if (c and 0x80) == 0: - break - shift.inc 7 - -proc toIpfs*(cid: BlobId): string = - ## Return BlobId encoded in IPFS multimulti. - const - multiRaw = 0x55 - multiBlake2b_256 = 0xb220 - let s = newStringStream() - s.writeUvarint 1 - s.writeUvarint multiRaw - s.writeUvarint multi_blake2b_256 - s.writeUvarint digestLen - for e in cid.data: - s.write e - s.setPosition 0 - result = 'z' & bitcoin.encode(s.readAll) - close s - const zeroChunk* = "8ddb61928ec76e4ee904cd79ed977ab6f5d9187f1102975060a6ba6ce10e5481".toDigest ## BlobId of zero chunk of maximum size. diff --git a/src/blobsets/ipfsdaemon.nim b/src/blobsets/ipfsdaemon.nim deleted file mode 100644 index a643c05..0000000 --- a/src/blobsets/ipfsdaemon.nim +++ /dev/null @@ -1,80 +0,0 @@ -import httpclient, json, base58/bitcoin, streams, cbor, tables - -import ../blobsets, ./stores, ./fsnodes - -type - IpfsStore* = ref IpfsStoreObj - IpfsStoreObj = object of DagfsStoreObj - ## IPFS daemon client. - http: HttpClient - baseUrl: string - -proc ipfsClose(s: DagfsStore) = - var ipfs = IpfsStore(s) - close ipfs.http - -proc putBlock(ipfs: IpfsStore; data: string; format = "raw"): tuple[key: string, size: int] = - # stuff in some MIME horseshit so it works - ipfs.http.headers = newHttpHeaders({ - "Content-Type": "multipart/form-data; boundary=------------------------KILL_A_WEBDEV"}) - let - trash = """ - ---------------------------KILL_A_WEBDEV -Content-Disposition: form-data; name="file"; filename="myfile" -Content-Type: application/octet-stream - -""" & data & """ - ---------------------------KILL_A_WEBDEV-- - """ - resp = ipfs.http.post(ipfs.baseUrl & "/api/v0/block/put?format=" & format, body=trash) - body = resp.body - js = parseJson body - # You can tell its written in Go when the JSON keys had to be capitalized - result = (js["Key"].getStr, js["Size"].getInt) - -proc ipfsPut(s: DagfsStore; blk: string): Cid = - var ipfs = IpfsStore(s) - let - isDag = blk.isUnixfs - tag = 0x55 - format = "raw" - result = CidBlake2b256 blk - discard ipfs.putBlock(blk, format) - # IPFS returns a different hash. Whatever. - -proc ipfsGetBuffer(s: DagfsStore; cid: Cid; buf: pointer; len: Natural): int = - var ipfs = IpfsStore(s) - let url = ipfs.baseUrl & "/api/v0/block/get?arg=" & $cid - try: - var body = ipfs.http.request(url).body - if not verify(cid, body): - raise newMissingObject cid - if body.len > len: - raise newException(BufferTooSmall, "") - result = body.len - copyMem(buf, body[0].addr, result) - except: - raise newMissingObject cid - -proc ipfsGet(s: DagfsStore; cid: Cid; result: var string) = - var ipfs = IpfsStore(s) - let url = ipfs.baseUrl & "/api/v0/block/get?arg=" & $cid - try: - result = ipfs.http.request(url).body - if not verify(cid, result): - raise newMissingObject cid - except: - raise newMissingObject cid - -proc newIpfsStore*(url = "http://127.0.0.1:5001"): IpfsStore = - ## Allocate a new synchronous store interface to the IPFS daemon at `url`. - ## Every block retrieved by `get` is hashed and verified. - new result - result.closeImpl = ipfsClose - result.putImpl = ipfsPut - result.getBufferImpl = ipfsGetBuffer - result.getImpl = ipfsGet - result.http = newHttpClient() - result.baseUrl = url