Skip to content

Commit

Permalink
Added gen_or_create. Extended search. Fixed search.
Browse files Browse the repository at this point in the history
  • Loading branch information
bahner committed Mar 18, 2023
1 parent d9bd024 commit b2d346a
Show file tree
Hide file tree
Showing 8 changed files with 88 additions and 30 deletions.
5 changes: 5 additions & 0 deletions Makefile
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,11 @@ cover:
format: deps
mix format

map:
mix xref graph --format dot
dot -Tpng xref_graph.dot -o xref_graph.png
eog xref_graph.png

mix: all
iex -S mix

Expand Down
10 changes: 5 additions & 5 deletions README.md
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
# ExIpns
# ExIpfsIpns

[![IPNS Unit and integration tests](https://github.com/bahner/ex-ipns/actions/workflows/testsuite.yaml/badge.svg)](https://github.com/bahner/ex-ipns/actions/workflows/testsuite.yaml)
[![Coverage Status](https://coveralls.io/repos/github/bahner/ex-ipns/badge.svg?branch=main)](https://coveralls.io/github/bahner/ex-ipns?branch=main)
Expand All @@ -12,15 +12,15 @@ This module features both publication and key handling.
## Usage

```elixir
iex(1)> ExIpns.Key.gen("foo")
iex(1)> ExIpfsIpns.Key.gen("foo")
{:ok,
%ExIpns.Key{
%ExIpfsIpns.Key{
name: "foo",
id: "k51qzi5uqu5dhxwb3x7bjg8k73tlkaqfugy217mgpf3vpdmoqn9du2qp865ddv"
}}
iex(2)> ExIpns.Name.publish("/ipfs/QmWGeRAEgtsHW3ec7U4qW2CyVy7eA2mFRVbk1nb24jFyks", key: "foo")
iex(2)> ExIpfsIpns.Name.publish("/ipfs/QmWGeRAEgtsHW3ec7U4qW2CyVy7eA2mFRVbk1nb24jFyks", key: "foo")
{:ok,
%ExIpns.Name{
%ExIpfsIpns.Name{
name: "k51qzi5uqu5dhxwb3x7bjg8k73tlkaqfugy217mgpf3vpdmoqn9du2qp865ddv",
value: "/ipfs/QmWGeRAEgtsHW3ec7U4qW2CyVy7eA2mFRVbk1nb24jFyks"
}}
Expand Down
10 changes: 5 additions & 5 deletions lib/ex_ipns.ex → lib/ex_ipfs_ipns.ex
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
defmodule ExIpns do
defmodule ExIpfsIpns do
@moduledoc """
Interplanetary Name System commands
"""
Expand Down Expand Up @@ -35,15 +35,15 @@ defmodule ExIpns do
## Example
```
iex(1)> ExIpns.Key.gen("foo")
iex(1)> ExIpfsIpns.Key.gen("foo")
{:ok,
%ExIpns.Key{
%ExIpfsIpns.Key{
name: "foo",
id: "k51qzi5uqu5dhxwb3x7bjg8k73tlkaqfugy217mgpf3vpdmoqn9du2qp865ddv"
}}
iex(2)> ExIpns.Name.publish("/ipfs/QmWGeRAEgtsHW3ec7U4qW2CyVy7eA2mFRVbk1nb24jFyks", key: "foo")
iex(2)> ExIpfsIpns.Name.publish("/ipfs/QmWGeRAEgtsHW3ec7U4qW2CyVy7eA2mFRVbk1nb24jFyks", key: "foo")
{:ok,
%ExIpns.Name{
%ExIpfsIpns.Name{
name: "k51qzi5uqu5dhxwb3x7bjg8k73tlkaqfugy217mgpf3vpdmoqn9du2qp865ddv",
value: "/ipfs/QmWGeRAEgtsHW3ec7U4qW2CyVy7eA2mFRVbk1nb24jFyks"
}}
Expand Down
39 changes: 34 additions & 5 deletions lib/ex_ipns/key.ex → lib/ex_ipfs_ipns/key.ex
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
defmodule ExIpns.Key do
defmodule ExIpfsIpns.Key do
@moduledoc """
ExIpns.Key module handles creating, listing, renaming, and removing IPNS keys.
ExIpfsIpns.Key module handles creating, listing, renaming, and removing IPNS keys.
Keys are what generates an IPNS name. The key is a cryptographic key pair, and the name is a hash of the public key. The name is what is used to resolve the IPNS name.
"""
Expand Down Expand Up @@ -64,6 +64,23 @@ defmodule ExIpns.Key do
|> okify()
end

@doc """
Returns the IPNS key for the given name.
## Parameters
`name` - Name of the key to search for.
`search_type` - Type of search criteria. Defaults to `:name
If you want to search for the name of a key, use `:id`
and provide the key id instead.
"""
@spec get_or_create(binary(), atom) :: {:ok, t()} | ExIpfs.Api.error_response()
def get_or_create(name, search_type \\ :name) do
case ExIpfsIpns.Key.exists?(name) do
true -> ExIpfsIpns.Key.search(name, search_type)
false -> ExIpfsIpns.Key.gen(name)
end
end

@doc """
Import a key and prints imported key id.
Expand Down Expand Up @@ -128,10 +145,22 @@ defmodule ExIpns.Key do

@doc """
Check if a keypair exists. This search both the name and id of the keypair.
## Parameters
`key` - Name of the key to search for.
`search_type` - Type of search criteria. Defaults to `:both`
If you want to search for the `id` of a key, use `:name`
and provide the key name.
If you want to search for the `name` of a key, use `:id`
and provide the key id.
"""
@spec exists?(binary) :: boolean
def exists?(key) when is_binary(key) do
search(key) != nil and search(key, :id) != nil
@spec exists?(binary, atom) :: boolean
def exists?(key, search_type \\ :both) when is_binary(key) do
case search_type do
:both -> search(key) != nil or search(key, :id) != nil
:name -> search(key) != nil
:id -> search(key, :id) != nil
end
end

@doc """
Expand Down
10 changes: 5 additions & 5 deletions mix.exs
Original file line number Diff line number Diff line change
@@ -1,19 +1,19 @@
defmodule ExIpns.MixProject do
defmodule ExIpfsIpns.MixProject do
@moduledoc """
Elixir IPNS Mix Project
"""
use Mix.Project

def project do
[
app: :ex_ipns,
version: "0.0.2",
app: :ex_ipfs_ipns,
version: "0.0.3",
elixir: "~> 1.14",
start_permanent: Mix.env() == :prod,
package: package(),
description: description(),
deps: deps(),
source_url: "https://github.com/bahner/ex-ipns.git",
source_url: "https://github.com/bahner/ex_ipfs_ipns.git",
test_coverage: [tool: ExCoveralls],
preferred_cli_env: [
coveralls: :test,
Expand Down Expand Up @@ -51,7 +51,7 @@ defmodule ExIpns.MixProject do
files: ["lib", "mix.exs", "README*", "LICENSE*", "AUTHORS*"],
maintainers: ["Lars Bahner"],
licenses: ["GPLv3"],
links: %{"GitHub" => "https://github.com/bahner/ex-ipns"}
links: %{"GitHub" => "https://github.com/bahner/ex_ipfs_ipns.git"}
]
end
end
8 changes: 4 additions & 4 deletions mix.lock
Original file line number Diff line number Diff line change
Expand Up @@ -3,11 +3,11 @@
"certifi": {:hex, :certifi, "2.9.0", "6f2a475689dd47f19fb74334859d460a2dc4e3252a3324bd2111b8f0429e7e21", [:rebar3], [], "hexpm", "266da46bdb06d6c6d35fde799bcb28d36d985d424ad7c08b5bb48f5b5cdd4641"},
"credo": {:hex, :credo, "1.6.7", "323f5734350fd23a456f2688b9430e7d517afb313fbd38671b8a4449798a7854", [:mix], [{:bunt, "~> 0.2.1", [hex: :bunt, repo: "hexpm", optional: false]}, {:file_system, "~> 0.2.8", [hex: :file_system, repo: "hexpm", optional: false]}, {:jason, "~> 1.0", [hex: :jason, repo: "hexpm", optional: false]}], "hexpm", "41e110bfb007f7eda7f897c10bf019ceab9a0b269ce79f015d54b0dcf4fc7dd3"},
"dialyxir": {:hex, :dialyxir, "1.2.0", "58344b3e87c2e7095304c81a9ae65cb68b613e28340690dfe1a5597fd08dec37", [:mix], [{:erlex, ">= 0.2.6", [hex: :erlex, repo: "hexpm", optional: false]}], "hexpm", "61072136427a851674cab81762be4dbeae7679f85b1272b6d25c3a839aff8463"},
"earmark_parser": {:hex, :earmark_parser, "1.4.30", "0b938aa5b9bafd455056440cdaa2a79197ca5e693830b4a982beada840513c5f", [:mix], [], "hexpm", "3b5385c2d36b0473d0b206927b841343d25adb14f95f0110062506b300cd5a1b"},
"earmark_parser": {:hex, :earmark_parser, "1.4.31", "a93921cdc6b9b869f519213d5bc79d9e218ba768d7270d46fdcf1c01bacff9e2", [:mix], [], "hexpm", "317d367ee0335ef037a87e46c91a2269fef6306413f731e8ec11fc45a7efd059"},
"erlex": {:hex, :erlex, "0.2.6", "c7987d15e899c7a2f34f5420d2a2ea0d659682c06ac607572df55a43753aa12e", [:mix], [], "hexpm", "2ed2e25711feb44d52b17d2780eabf998452f6efda104877a3881c2f8c0c0c75"},
"ex_doc": {:hex, :ex_doc, "0.29.1", "b1c652fa5f92ee9cf15c75271168027f92039b3877094290a75abcaac82a9f77", [:mix], [{:earmark_parser, "~> 1.4.19", [hex: :earmark_parser, repo: "hexpm", optional: false]}, {:makeup_elixir, "~> 0.14", [hex: :makeup_elixir, repo: "hexpm", optional: false]}, {:makeup_erlang, "~> 0.1", [hex: :makeup_erlang, repo: "hexpm", optional: false]}], "hexpm", "b7745fa6374a36daf484e2a2012274950e084815b936b1319aeebcf7809574f6"},
"ex_ipfs": {:hex, :ex_ipfs, "0.1.2", "75aa01c6e05bca5f2c60de2646974811708e9d14ba9e9f85e313c43db0cd8c8b", [:mix], [{:hackney, "~> 1.18", [hex: :hackney, repo: "hexpm", optional: false]}, {:jason, "~> 1.4", [hex: :jason, repo: "hexpm", optional: false]}, {:nanoid, "~> 2.0", [hex: :nanoid, repo: "hexpm", optional: false]}, {:recase, "~> 0.7.0", [hex: :recase, repo: "hexpm", optional: false]}, {:temp, "~> 0.4.7", [hex: :temp, repo: "hexpm", optional: false]}, {:tesla, "~> 1.5", [hex: :tesla, repo: "hexpm", optional: false]}], "hexpm", "1ed0e172a4f2a50227842475723147b3c5272c0eb153b94a8f8896d9866d50a4"},
"excoveralls": {:hex, :excoveralls, "0.15.3", "54bb54043e1cf5fe431eb3db36b25e8fd62cf3976666bafe491e3fa5e29eba47", [:mix], [{:hackney, "~> 1.16", [hex: :hackney, repo: "hexpm", optional: false]}, {:jason, "~> 1.0", [hex: :jason, repo: "hexpm", optional: false]}], "hexpm", "f8eb5d8134d84c327685f7bb8f1db4147f1363c3c9533928234e496e3070114e"},
"ex_doc": {:hex, :ex_doc, "0.29.2", "dfa97532ba66910b2a3016a4bbd796f41a86fc71dd5227e96f4c8581fdf0fdf0", [:mix], [{:earmark_parser, "~> 1.4.19", [hex: :earmark_parser, repo: "hexpm", optional: false]}, {:makeup_elixir, "~> 0.14", [hex: :makeup_elixir, repo: "hexpm", optional: false]}, {:makeup_erlang, "~> 0.1", [hex: :makeup_erlang, repo: "hexpm", optional: false]}], "hexpm", "6b5d7139eda18a753e3250e27e4a929f8d2c880dd0d460cb9986305dea3e03af"},
"ex_ipfs": {:hex, :ex_ipfs, "0.1.4", "e21ab51d1c2099f80d5488fc161c255b8e47560786d71164094b850ab4bb297c", [:mix], [{:hackney, "~> 1.18", [hex: :hackney, repo: "hexpm", optional: false]}, {:jason, "~> 1.4", [hex: :jason, repo: "hexpm", optional: false]}, {:nanoid, "~> 2.0", [hex: :nanoid, repo: "hexpm", optional: false]}, {:recase, "~> 0.7.0", [hex: :recase, repo: "hexpm", optional: false]}, {:temp, "~> 0.4.7", [hex: :temp, repo: "hexpm", optional: false]}, {:tesla, "~> 1.5", [hex: :tesla, repo: "hexpm", optional: false]}], "hexpm", "98baae4e7c9a1324b71d51c56882c3ea82950cb58c3714183e1dcbff81902900"},
"excoveralls": {:hex, :excoveralls, "0.16.0", "41f4cfbf7caaa3bc2cf411db6f89c1f53afedf0f1fe8debac918be1afa19c668", [:mix], [{:hackney, "~> 1.16", [hex: :hackney, repo: "hexpm", optional: false]}, {:jason, "~> 1.0", [hex: :jason, repo: "hexpm", optional: false]}], "hexpm", "401205356482ab99fb44d9812cd14dd83b65de8e7ae454697f8b34ba02ecd916"},
"file_system": {:hex, :file_system, "0.2.10", "fb082005a9cd1711c05b5248710f8826b02d7d1784e7c3451f9c1231d4fc162d", [:mix], [], "hexpm", "41195edbfb562a593726eda3b3e8b103a309b733ad25f3d642ba49696bf715dc"},
"hackney": {:hex, :hackney, "1.18.1", "f48bf88f521f2a229fc7bae88cf4f85adc9cd9bcf23b5dc8eb6a1788c662c4f6", [:rebar3], [{:certifi, "~> 2.9.0", [hex: :certifi, repo: "hexpm", optional: false]}, {:idna, "~> 6.1.0", [hex: :idna, repo: "hexpm", optional: false]}, {:metrics, "~> 1.0.0", [hex: :metrics, repo: "hexpm", optional: false]}, {:mimerl, "~> 1.1", [hex: :mimerl, repo: "hexpm", optional: false]}, {:parse_trans, "3.3.1", [hex: :parse_trans, repo: "hexpm", optional: false]}, {:ssl_verify_fun, "~> 1.1.0", [hex: :ssl_verify_fun, repo: "hexpm", optional: false]}, {:unicode_util_compat, "~> 0.7.0", [hex: :unicode_util_compat, repo: "hexpm", optional: false]}], "hexpm", "a4ecdaff44297e9b5894ae499e9a070ea1888c84afdd1fd9b7b2bc384950128e"},
"idna": {:hex, :idna, "6.1.1", "8a63070e9f7d0c62eb9d9fcb360a7de382448200fbbd1b106cc96d3d8099df8d", [:rebar3], [{:unicode_util_compat, "~> 0.7.0", [hex: :unicode_util_compat, repo: "hexpm", optional: false]}], "hexpm", "92376eb7894412ed19ac475e4a86f7b413c1b9fbb5bd16dccd57934157944cea"},
Expand Down
28 changes: 26 additions & 2 deletions test/ex_ipns/key_test.exs
Original file line number Diff line number Diff line change
@@ -1,9 +1,9 @@
defmodule ExIpns.KeyTest do
defmodule ExIpfsIpns.KeyTest do
@moduledoc false

use ExUnit.Case, async: true

alias ExIpns.Key
alias ExIpfsIpns.Key

test "generate and remove key" do
key = "exipns-test-gen-remove"
Expand Down Expand Up @@ -35,4 +35,28 @@ defmodule ExIpns.KeyTest do
assert rename.now == overwrite_key
assert rename.overwrite == true
end

test "search for keys" do
search_key = "search-1"
non_existent_key = "search-2-fjhdlslfdghslfdh"

Key.gen(search_key)
Key.rm(non_existent_key)

result = Key.search(non_existent_key, :name)
assert is_nil(result)
result = Key.search(non_existent_key, :id)
assert is_nil(result)

result = Key.search(search_key, :id)
assert is_nil(result)
assert %ExIpfsIpns.Key{} = Key.search(search_key, :name)
result = Key.search(search_key, :name)
assert %ExIpfsIpns.Key{} = result
assert result.name == search_key
assert not is_nil(result.id)

Key.rm(search_key)

end
end
8 changes: 4 additions & 4 deletions test/ex_ipns_test.exs
Original file line number Diff line number Diff line change
@@ -1,18 +1,18 @@
defmodule ExIpnsTest do
defmodule ExIpfsIpnsTest do
@moduledoc false

use ExUnit.Case, async: true

alias ExIpns.Key
alias ExIpfsIpns.Key

@tag timeout: :infinity
@key "exipns-ExIpns-test-key"
@key "exipns-ExIpfsIpns-test-key"

test "publish" do
Key.gen(@key)

assert {:ok, _} =
ExIpns.publish(
ExIpfsIpns.publish(
"/ipfs/Qmc5gCcjYypU7y28oCALwfSvxCBskLuPKWpK4qpterKC7z",
key: @key
)
Expand Down

0 comments on commit b2d346a

Please sign in to comment.