aqua-ipfs/aqua/ipfs-api.aqua

83 lines
2.7 KiB
Plaintext
Raw Normal View History

2021-07-01 23:14:59 +03:00
import "@fluencelabs/aqua-lib/builtin.aqua"
import "ipfs.aqua"
alias Multiaddr: string
alias PeerId: string
2021-07-26 12:14:39 +03:00
alias CID: string
alias Path: string
-- Download file from remote IPFS node to Fluence node and then
-- put that file to local IPFS node, effectively caching it on the local IPFS node.
--
-- Arguments:
-- node - PeerId of the node where execution should happen
-- cid IPFS Content ID to download
-- from - Multiaddress of IPFS node to download `cid` from
-- error - callback to notify function caller about errors
--
-- Returns:
-- Path on the node's local filesystem. It will be available only during single particle execution.
--
-- Errors:
-- If Ipfs.get_from or Ipfs.put fails, nil is returned.
-- Errors are reported to the `error` callback.
func get_and_cache(
node: PeerId,
cid: CID,
from: Multiaddr,
error: string, string -> ()
2021-08-02 17:50:21 +03:00
) -> ?CID:
-- localCid will be the same as cid
localCid: *CID
2021-07-26 12:14:39 +03:00
on node:
-- Download file from remote IPFS to local filesystem
get <- Ipfs.get_from(cid, from)
if get.success:
-- Put file to local IPFS node
put <- Ipfs.put(get.path)
if put.success:
2021-08-02 17:50:21 +03:00
localCid <<- put.hash
2021-07-26 12:14:39 +03:00
else:
-- report error in background co-routine
co error("Ipfs.put failed", put.error)
else:
-- report error in background co-routine
co error("Ipfs.get failed", get.error)
2021-08-02 17:50:21 +03:00
<- localCid
2021-07-01 23:14:59 +03:00
2021-07-21 20:19:13 +03:00
-- Upload file `path` to IPFS node running on `node`
-- path should exist & be available to `aqua-ipfs`
2021-07-09 15:52:41 +03:00
func put(node: PeerId, path: string) -> IpfsPutResult:
2021-07-01 23:14:59 +03:00
on node:
result <- Ipfs.put(path)
<- result
2021-07-26 12:14:39 +03:00
-- Download file `cid` from IPFS node `from` and save it to `node`
func get_from(node: PeerId, cid: CID, from: Multiaddr) -> IpfsGetResult:
2021-07-01 23:14:59 +03:00
on node:
2021-07-26 12:14:39 +03:00
result <- Ipfs.get_from(cid, from)
2021-07-01 23:14:59 +03:00
<- result
2021-07-14 22:09:19 +03:00
-- Set timeout for IPFS calls in `aqua-ipfs`
2021-07-14 22:09:19 +03:00
func set_timeout(node: PeerId, timeout_sec: u64):
on node:
Ipfs.set_timeout(timeout_sec)
2021-07-21 20:19:13 +03:00
2021-07-26 12:14:39 +03:00
-- Get externally available multiaddress of IPFS's HTTP RPC endpoint (usually on port 5001)
2021-07-21 20:19:13 +03:00
func get_external_api_multiaddr(node: PeerId) -> IpfsMultiaddrResult:
on node:
result <- Ipfs.get_external_api_multiaddr()
<- result
2021-07-26 12:14:39 +03:00
-- Get externally available multiaddress of IPFS's Swarm endpoint (usually on port 4001)
2021-07-21 20:19:13 +03:00
func get_external_swarm_multiaddr(node: PeerId) -> IpfsMultiaddrResult:
on node:
result <- Ipfs.get_external_swarm_multiaddr()
<- result
2021-07-26 12:14:39 +03:00
-- Get local multiaddress of IPFS's HTTP RPC endpoint (usually on port 5001)
func get_local_api_multiaddr(node: PeerId) -> IpfsMultiaddrResult:
on node:
result <- Ipfs.get_local_api_multiaddr()
<- result