nim-dagger/dagger.nim

50 lines
1.3 KiB
Nim
Raw Normal View History

import pkg/chronos
2021-02-01 10:11:30 +00:00
import pkg/libp2p/peerinfo
import pkg/libp2p/multiaddress
import ./ipfs/p2p/switch
import ./ipfs/repo
import ./ipfs/chunking
import ./ipfs/bitswap
export peerinfo except IPFS
export multiaddress except IPFS
type
Ipfs* = ref object
2021-02-01 10:11:30 +00:00
repo: Repo
switch: Switch
bitswap: Bitswap
proc info*(ipfs: Ipfs): PeerInfo =
ipfs.switch.peerInfo
proc start*(_: type Ipfs, addresses: seq[MultiAddress]): Future[Ipfs] {.async.} =
let repo = Repo()
let switch = Switch.create()
let bitswap = Bitswap.start(switch, repo)
switch.peerInfo.addrs.add(addresses)
discard await switch.start()
result = Ipfs(repo: repo, switch: switch, bitswap: bitswap)
2021-02-01 10:11:30 +00:00
proc start*(_: type Ipfs, address: MultiAddress): Future[Ipfs] {.async.} =
result = await Ipfs.start(@[address])
2021-02-01 10:11:30 +00:00
proc start*(_: type Ipfs): Future[Ipfs] {.async.} =
result = await Ipfs.start(@[])
2021-02-01 10:11:30 +00:00
proc connect*(peer: Ipfs, info: PeerInfo) {.async.} =
await peer.bitswap.connect(info)
2021-02-01 10:11:30 +00:00
proc add*(peer: Ipfs, input: File): Future[Cid] {.async.} =
let obj = createObject(input)
peer.repo.store(obj)
result = obj.cid
2021-02-01 10:11:30 +00:00
proc get*(peer: Ipfs, identifier: Cid, output: File) {.async.} =
let obj = await peer.bitswap.retrieve(identifier)
if obj.isSome:
obj.get().writeToFile(output)
2021-02-01 10:11:30 +00:00
proc stop*(peer: Ipfs) {.async.} =
await peer.switch.stop()