Skip to content

Commit

Permalink
fix bug in send and work with stream
Browse files Browse the repository at this point in the history
  • Loading branch information
Arif committed Aug 10, 2018
1 parent 8e7539d commit 86a9614
Show file tree
Hide file tree
Showing 5 changed files with 69 additions and 10 deletions.
1 change: 1 addition & 0 deletions README.md
Expand Up @@ -87,5 +87,6 @@ Some of common task is a combination of multiple api call. Previously has been i
- [ ] Create new asset
- [ ] Reissue new asset
- [ ] List asset
- FIX STREAM DECODEE


19 changes: 14 additions & 5 deletions config/config.exs
@@ -1,10 +1,19 @@
use Mix.Config

# getchain
# config :multichain,
# protocol: "http",
# port: "9242",
# host: "uatblockchain.getmya.io",
# username: "multichainrpc",
# password: "9YZbdCN4XXUV64CQzJWztBcCESrVNiGZkovuYgnUs4S4",
# chain: "getchain"

# testchain
config :multichain,
protocol: "http",
port: "1234",
host: "188.199.177",
port: "9220",
host: "uatblockchain.getmya.io",
username: "multichainrpc",
password: "xxxxxxxxxxxxxxx",
chain: "chain1"

password: "hByvArunyPTpoPFkZiGS2L7iEENrGbhqs4WQ4ZHDQep",
chain: "testchain"
6 changes: 4 additions & 2 deletions lib/multichainhttp.ex
Expand Up @@ -12,12 +12,14 @@ defmodule Multichain.Http do
{:ok, config} ->
url = "#{config.protocol}://#{config.host}:#{config.port}"
headers = [{"Content-type", "application/json"}]
body = Poison.encode!(params |> Map.put("chain_name", config.chain)) |> IO.inspect()
body = Poison.encode!(params |> Map.put("chain_name", config.chain))
options = [hackney: [basic_auth: {config.username, config.password}]]

case HTTPoison.post(url, body, headers, options) do
# check the status code
{:ok, result} ->
result

case result.status_code do
200 -> Poison.decode(result.body)
401 -> {:error, "Unauthorized. The supplied credential is incorrect"}
Expand Down Expand Up @@ -54,7 +56,7 @@ defmodule Multichain.Http do
end
end

defp getconfig do
def getconfig do
port = Application.get_env(:multichain, :port)
host = Application.get_env(:multichain, :host)
username = Application.get_env(:multichain, :username)
Expand Down
48 changes: 47 additions & 1 deletion lib/multichainsuper.ex
Expand Up @@ -26,7 +26,7 @@ defmodule Multichain.Super do
case Http.jsonrpccall("importaddress", [keypair["address"], "", false]) do
# 3. grant permission sent
{:ok, _result2} ->
case Http.jsonrpccall("grant", [keypair["address"], "receive, receive"]) do
case Http.jsonrpccall("grant", [keypair["address"], "send,receive"]) do
{:ok, _result4} -> {:ok, keypair}
other -> other
end
Expand Down Expand Up @@ -221,6 +221,52 @@ defmodule Multichain.Super do
end
end

@doc """
This is a helper function to check whether an address have send permission or not.
You can unblock (give send permission) by using `unblock/1`
"""
def publish_stream(addr, streamname, key, value, privkey) do
compose =
case Multichain.api("createrawsendfrom", [
addr,
%{},
[%{"for" => streamname, "key" => key, "data" => Base.encode16(value)}]
]) do
{:ok, %{"error" => _error, "id" => _id, "result" => result}} ->
case Multichain.api("signrawtransaction", [result, [], [privkey], nil]) do
{:ok, %{"error" => nil, "id" => nil, "result" => %{"complete" => true, "hex" => hex}}} ->
Multichain.api("sendrawtransaction", [hex])

other ->
other
end

other ->
other
end
end

def get_stream_data!(stream, txid) do
case Multichain.api("getstreamitem", [stream, txid]) do
{:ok, %{"error" => nil, "id" => nil, "result" => result}} -> result
other -> other
end
end

def get_stream_data(stream, txid) do
case Multichain.api("getstreamitem", [stream, txid]) do
{:ok, %{"error" => nil, "id" => nil, "result" => result}} ->
case Base.decode16(result["data"], case: :lower) do
{:ok, string} -> string
_ -> :error
end

other ->
other
end
end

# ------------------------------------------------Private Area ----------------------------------

defp find_send_permission(list) do
Expand Down
5 changes: 3 additions & 2 deletions mix.exs
Expand Up @@ -4,7 +4,7 @@ defmodule Multichain.MixProject do
def project do
[
app: :multichain,
version: "0.0.3",
version: "0.0.4",
elixir: "~> 1.6",
start_permanent: Mix.env() == :prod,
deps: deps(),
Expand Down Expand Up @@ -32,7 +32,8 @@ defmodule Multichain.MixProject do
maintainers: ["Arif Yuliannur"],
licenses: ["MIT"],
links: %{"GitHub" => "https://github.com/virkillz/multichain-elixir.git"},
description: "A simple wrapper to call Multichain JSON RPC API in Elixir way. Also bunch of helper which combine common Multichain command."
description:
"A simple wrapper to call Multichain JSON RPC API in Elixir way. Also bunch of helper which combine common Multichain command."
]
end
end

0 comments on commit 86a9614

Please sign in to comment.