Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

add feat: ipfs dag put #338

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
32 changes: 32 additions & 0 deletions apps/arweave/src/apps/app_ipfs.erl
Expand Up @@ -119,6 +119,12 @@ add_local_ipfs_tx_data(TXid) ->
add_ipfs_data(TX, Hash);
false ->
{error, hash_not_found}
end,
case lists:keyfind(<<"IPFS-Dag-Put">>, 1, TX#tx.tags) of
{<<"IPFS-Dag-Put">>, Hash} ->
put_ipfs_dag(TX, Hash);
false ->
{error, hash_not_found}
end
end.

Expand Down Expand Up @@ -203,6 +209,13 @@ server(State=#state{
false ->
pass
end,
case lists:keyfind(<<"IPFS-Dag-Put">>, 1, Tags) of
{<<"IPFS-Dag-Put">>, Hash} ->
{ok, _Hash2} = put_ipfs_dag(TX, Hash),
spawn(ar_ipfs, dht_provide_hash, [Hash]);
false ->
pass
end,
server(State);
{recv_new_tx, X} ->
?LOG_DEBUG({app_ipfs, recv_new_tx, X}),
Expand All @@ -229,6 +242,25 @@ get_hash_and_queue(Hash, Queue) ->
{error, Reason}
end.

put_ipfs_dag(TX, Hash) ->
%% version 0.1, no validation
?LOG_DEBUG({recv_tx_ipfs_dag_put, TX#tx.id, Hash}),
{ok, _Hash2} = ar_ipfs:dag_put_object(TX#tx.data, Hash).

%% This may break for IPLD DAG Objects if the codec is not CBOR or DAG-JSON as they're not yet supported by go-ipfs HTTP API
resolve_hash_and_queue(Hash, Queue) ->
?LOG_DEBUG({fetching, Hash}),
case ar_ipfs:get_object_by_hash(Hash) of
{ok, Data} ->
{ok, Hash2} = ar_ipfs:dag_put_object(Data, Hash),
?LOG_DEBUG({added, Hash, Hash2}),
UnsignedTX = #tx{tags=[{<<"IPFS-Dag-Put">>, Hash}], data=Data},
app_queue:add(Queue, UnsignedTX),
ok;
{error, Reason} ->
{error, Reason}
end.

get_x(Pid, SendTag, RecvTag) ->
Pid ! {SendTag, self()},
receive
Expand Down
23 changes: 23 additions & 0 deletions apps/arweave/src/apps/ar_ipfs.erl
Expand Up @@ -96,6 +96,22 @@ add_data(IP, Port, DataB, FilenameB) ->
{<<"Hash">>, Hash} = lists:keyfind(<<"Hash">>, 1, Props),
{ok, Hash}.

dag_put_object(ObjectData, Multihash) ->
dag_put_object(?IPFS_HOST, ?IPFS_PORT, ObjectData, Multihash).

dag_put_object(IP, Port, ObjectDataB, MultihashB) ->
URL = "http://" ++ IP ++ ":" ++ Port ++ "/api/v0/dag/put?pin=true",
Data = binary_to_list(ObjectDataB),
Filename = thing_to_list(MultihashB),
Boundary = ?BOUNDARY,
Body = format_multipart_formdata(Boundary, [{Filename, Data}]),
ContentType = lists:concat(["multipart/form-data; boundary=", Boundary]),
Headers = [{"Content-Length", integer_to_list(length(Body))}],
{ok, Response} = request(post, {URL, Headers, ContentType, Body}),
{Props} = response_to_json(Response),
{<<"Hash">>, Hash} = lists:keyfind(<<"Hash">>, 1, Props),
{ok, Hash}.

add_file(Path) ->
add_file(?IPFS_HOST, ?IPFS_PORT, Path).

Expand All @@ -111,6 +127,13 @@ cat_data_by_hash(IP, Port, Hash) ->
URL = "http://" ++ IP ++ ":" ++ Port ++ "/api/v0/cat?arg=" ++ binary_to_list(Hash),
request(get, {URL, []}).

get_object_by_hash(Hash) ->
get_object_by_hash(?IPFS_HOST, ?IPFS_PORT, Hash).

get_object_by_hash(IP, Port, Hash) ->
URL = "http://" ++ IP ++ ":" ++ Port ++ "/api/v0/dag/get?arg=" ++ binary_to_list(Hash),
request(get, {URL, []}).

-spec config_get_identity() -> string().
config_get_identity() ->
lists:droplast(os:cmd("ipfs config Identity.PeerID")).
Expand Down