Skip to content

Commit 8ff7617

Browse files
committed
improve local transfer APIs
1 parent 028fb17 commit 8ff7617

6 files changed

Lines changed: 71 additions & 23 deletions

File tree

ex/config/runtime.exs

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -36,10 +36,10 @@ end
3636

3737
path = Path.join([work_folder, "sk"])
3838
if !File.exists?(path) do
39-
IO.puts "No trainer sk (BLS12-381) in #{path} as base58"
39+
#IO.puts "No trainer sk (BLS12-381) in #{path} as base58"
4040
sk = :crypto.strong_rand_bytes(64)
41-
pk = BlsEx.get_public_key!(sk)
42-
IO.puts "generated random sk, your pk is #{Base58.encode(pk)}"
41+
#pk = BlsEx.get_public_key!(sk)
42+
#IO.puts "generated random sk, your pk is #{Base58.encode(pk)}"
4343
:ok = File.write!(path, Base58.encode(sk))
4444
end
4545
sk = File.read!(path) |> String.trim() |> Base58.decode()
@@ -48,7 +48,7 @@ pop = BlsEx.sign!(sk, pk, BLS12AggSig.dst_pop())
4848

4949
config :ama, :trainer_pk_b58, pk |> Base58.encode()
5050
config :ama, :trainer_pk, pk
51-
config :ama, :trainer_sk, sk
51+
config :ama, :trainer_sk, System.get_env("SEED64") || sk
5252
config :ama, :trainer_pop, pop
5353

5454
config :ama, :archival_node, System.get_env("ARCHIVALNODE") in ["true", "y", "yes"]

ex/lib/api/api_chain.ex

Lines changed: 12 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -5,13 +5,23 @@ defmodule API.Chain do
55
%{error: :ok, entry: entry}
66
end
77

8-
def entry(entry_hash) do
8+
def entry(entry_hash, filter_on_function \\ nil) do
99
entry_hash = if byte_size(entry_hash) != 32, do: Base58.decode(entry_hash), else: entry_hash
1010
entry = Fabric.entry_by_hash(entry_hash)
1111
if !entry do
1212
%{error: :not_found}
1313
else
14+
next_entry = API.Chain.by_height(entry.header_unpacked.height+1).entries
15+
|> Enum.find(& &1.header_unpacked.prev_hash == Base58.encode(entry.hash) && &1[:consensus][:finality_reached])
1416
entry = format_entry_for_client(entry)
17+
entry = if !next_entry do entry else
18+
put_in(entry, [:next_entry_hash_finality_reached], next_entry.hash)
19+
end
20+
entry = if !filter_on_function do entry else
21+
txs_filtered = API.TX.get_by_entry(entry.hash)
22+
|> Enum.filter(& List.first(&1.tx.actions)[:function] == filter_on_function)
23+
put_in(entry, [:txs_filtered], txs_filtered)
24+
end
1525
%{error: :ok, entry: entry}
1626
end
1727
end
@@ -116,6 +126,7 @@ defmodule API.Chain do
116126
if !score do entry else
117127
entry = put_in(entry, [:consensus], %{})
118128
entry = put_in(entry, [:consensus, :score], Float.round(score, 3))
129+
entry = put_in(entry, [:consensus, :finality_reached], Float.round(score, 3) >= 0.67)
119130
entry = put_in(entry, [:consensus, :mut_hash], Base58.encode(mut_hash))
120131
end
121132
end

ex/lib/api/api_wallet.ex

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -30,6 +30,7 @@ defmodule API.Wallet do
3030
def transfer(from_sk, to, amount, symbol, broadcast \\ true) do
3131
from_sk = if byte_size(from_sk) != 64, do: Base58.decode(from_sk), else: from_sk
3232
to = if byte_size(to) != 48, do: Base58.decode(to), else: to
33+
if !BlsEx.validate_public_key(to) and to != @burn_address, do: throw(%{error: :invalid_receiver_pk})
3334
amount = if is_float(amount) do trunc(amount * 1_000_000_000) else amount end
3435
amount = if is_integer(amount) do :erlang.integer_to_binary(amount) else amount end
3536
tx_packed = TX.build(from_sk, "Coin", "transfer", [to, amount, symbol])

ex/lib/api/rpc_api.ex

Lines changed: 23 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -8,20 +8,35 @@ defmodule RPC.API do
88

99
defmodule Wallet do
1010
def transfer(seed64, receiver, amount_float, symbol \\ "AMA") do
11-
tx_packed = API.Wallet.transfer(seed64, receiver, amount_float, symbol, false)
12-
RPC.API.get("/api/tx/submit/#{Base58.encode(tx_packed)}")
11+
if !BlsEx.validate_public_key(receiver) and receiver != @burn_address do
12+
IO.inspect {"sending #{amount_float} AMA to invalid public key", receiver}
13+
%{error: :invalid_public_key, pk: receiver}
14+
else
15+
tx_packed = API.Wallet.transfer(seed64, receiver, amount_float, symbol, false)
16+
RPC.API.get("/api/tx/submit/#{Base58.encode(tx_packed)}")
17+
end
1318
end
1419

1520
def transfer_bulk(seed64, receiver_amount_list) do
1621
Enum.map(receiver_amount_list, fn
1722
{receiver, amount_float} ->
18-
IO.inspect {"sending #{amount_float} AMA to ", receiver}
19-
tx_packed = API.Wallet.transfer(seed64, receiver, amount_float, "AMA", false)
20-
RPC.API.get("/api/tx/submit/#{Base58.encode(tx_packed)}")
23+
if !BlsEx.validate_public_key(receiver) and receiver != @burn_address do
24+
IO.inspect {"sending #{amount_float} AMA to invalid public key", receiver}
25+
%{error: :invalid_public_key, pk: receiver}
26+
else
27+
IO.inspect {"sending #{amount_float} AMA to ", receiver}
28+
tx_packed = API.Wallet.transfer(seed64, receiver, amount_float, "AMA", false)
29+
RPC.API.get("/api/tx/submit/#{Base58.encode(tx_packed)}")
30+
end
2131
{receiver, amount_float, symbol} ->
22-
IO.inspect {"sending #{amount_float} #{symbol} to ", receiver}
23-
tx_packed = API.Wallet.transfer(seed64, receiver, amount_float, symbol, false)
24-
RPC.API.get("/api/tx/submit/#{Base58.encode(tx_packed)}")
32+
if !BlsEx.validate_public_key(receiver) and receiver != @burn_address do
33+
IO.inspect {"sending #{amount_float} AMA to invalid public key", receiver}
34+
%{error: :invalid_public_key, pk: receiver}
35+
else
36+
IO.inspect {"sending #{amount_float} #{symbol} to ", receiver}
37+
tx_packed = API.Wallet.transfer(seed64, receiver, amount_float, symbol, false)
38+
RPC.API.get("/api/tx/submit/#{Base58.encode(tx_packed)}")
39+
end
2540
end)
2641
end
2742

ex/lib/ex_bakeware.ex

Lines changed: 23 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -6,12 +6,15 @@ defmodule Ama.Bakeware do
66
def main(args) do
77
arg0 = List.first(args)
88
cond do
9-
arg0 == "buildtx" ->
10-
Process.sleep(500)
11-
IO.puts ""
9+
arg0 == "generate_wallet" ->
10+
seed64 = :crypto.strong_rand_bytes(64)
11+
pk = BlsEx.get_public_key!(seed64)
12+
IO.puts "#{Base58.encode(pk)} #{Base58.encode(seed64)}"
13+
:erlang.halt()
1214

15+
arg0 == "buildtx" ->
1316
["buildtx", contract, func, args | rest] = args
14-
contract = if Base58.likely(contract) do Base58.decode(contract) else contract end
17+
contract = if byte_size(contract) > 48 do Base58.decode(contract) else contract end
1518
sk = Application.fetch_env!(:ama, :trainer_sk)
1619
{args, []} = Code.eval_string(args)
1720
[attach_symbol, attach_amount] = if length(rest) != 2 do [nil,nil] else
@@ -21,10 +24,23 @@ defmodule Ama.Bakeware do
2124
IO.puts Base58.encode(packed_tx)
2225
:erlang.halt()
2326

24-
arg0 == "deploytx" ->
25-
Process.sleep(500)
26-
IO.puts ""
27+
arg0 == "build_and_broadcasttx" ->
28+
["build_and_broadcasttx", contract, func, args | rest] = args
29+
contract = if byte_size(contract) > 48 do Base58.decode(contract) else contract end
30+
sk = Application.fetch_env!(:ama, :trainer_sk)
31+
{args, []} = Code.eval_string(args)
32+
[attach_symbol, attach_amount] = if length(rest) != 2 do [nil,nil] else
33+
[attach_symbol, attach_amount] = rest
34+
end
35+
packed_tx = TX.build(sk, contract, func, args, nil, attach_symbol, attach_amount)
36+
result = RPC.API.get("/api/tx/submit/#{Base58.encode(packed_tx)}")
37+
#IO.puts Base58.encode(packed_tx)
38+
if result[:error] == "ok" do
39+
IO.puts(result.hash)
40+
end
41+
:erlang.halt()
2742

43+
arg0 == "deploytx" ->
2844
["deploytx", wasmpath] = args
2945
sk = Application.fetch_env!(:ama, :trainer_sk)
3046
wasmbytes = File.read!(wasmpath)
@@ -38,9 +54,6 @@ defmodule Ama.Bakeware do
3854
:erlang.halt()
3955

4056
arg0 == "getpk" ->
41-
Process.sleep(500)
42-
IO.puts ""
43-
4457
["getpk", path] = args
4558
sk = File.read!(path) |> String.trim()
4659
pk = BlsEx.get_public_key!(Base58.decode(sk))

ex/lib/http/multiserver.ex

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -101,6 +101,14 @@ defmodule Ama.MultiServer do
101101
r.method == "GET" and String.starts_with?(r.path, "/api/chain/tip") ->
102102
result = API.Chain.entry_tip()
103103
quick_reply(state, result)
104+
105+
r.method == "GET" and String.starts_with?(r.path, "/api/chain/hash/") ->
106+
hash = String.replace(r.path, "/api/chain/hash/", "")
107+
query = r.query && Photon.HTTP.parse_query(r.query)
108+
filter_on_function = query[:filter_on_function]
109+
result = API.Chain.entry(hash, filter_on_function)
110+
quick_reply(state, result)
111+
104112
r.method == "GET" and String.starts_with?(r.path, "/api/chain/height/") ->
105113
height = String.replace(r.path, "/api/chain/height/", "")
106114
|> :erlang.binary_to_integer()

0 commit comments

Comments
 (0)