Skip to content

Commit d8bce06

Browse files
committed
check nodekey before start
1 parent 94ef826 commit d8bce06

3 files changed

Lines changed: 51 additions & 16 deletions

File tree

apps/tpnode/src/nodekey.erl

Lines changed: 19 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -23,31 +23,36 @@ get_privs() ->
2323
end.
2424

2525
get_priv() ->
26-
{ok, K1}=application:get_env(tpnode, privkey),
27-
case K1 of
28-
<<_:32/binary>> -> K1;
29-
<<_:48/binary>> -> K1;
30-
<<_:64/binary>> -> K1;
31-
_ ->
32-
Res=hex:parse(K1),
33-
%32=size(Res),
34-
application:set_env(tpnode, privkey, Res),
35-
Res
26+
case application:get_env(tpnode,privkey_dec) of
27+
{ok, Bin} ->
28+
Bin;
29+
undefined ->
30+
{ok, K1}=application:get_env(tpnode, privkey),
31+
case K1 of
32+
<<_:32/binary>> -> K1;
33+
<<_:48/binary>> -> K1;
34+
<<_:64/binary>> -> K1;
35+
_ ->
36+
Res=hex:parse(K1),
37+
%32=size(Res),
38+
application:set_env(tpnode, privkey_dec, Res),
39+
Res
40+
end
3641
end.
3742

3843
get_pub() ->
3944
case application:get_env(tpnode, pubkey) of
4045
{ok, Bin} when is_binary(Bin) ->
4146
Bin;
42-
_ ->
47+
_ ->
4348
Pub=tpecdsa:calc_pub(get_priv()),
4449
application:set_env(tpnode, pubkey, Pub),
4550
Pub
4651
end.
4752

4853
node_id() ->
4954
case application:get_env(tpnode, nodeid) of
50-
undefined ->
55+
undefined ->
5156
ID=node_id(get_pub()),
5257
application:set_env(tpnode, nodeid, ID),
5358
ID;
@@ -67,12 +72,12 @@ node_name(Default) ->
6772
node_name() ->
6873
try
6974
case application:get_env(tpnode, nodename) of
70-
undefined ->
75+
undefined ->
7176
case chainsettings:is_our_node(get_pub()) of
7277
Name when is_binary(Name) ->
7378
application:set_env(tpnode, nodename, Name),
7479
Name;
75-
_ ->
80+
_ ->
7681
node_id()
7782
end;
7883
{ok, Name} -> Name

apps/tpnode/src/tpnode.erl

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -33,7 +33,8 @@ reload() ->
3333
application:set_env(tpnode, K, V)
3434
end, Config),
3535
logger_reconfig(),
36-
application:unset_env(tpnode, pubkey);
36+
application:unset_env(tpnode, pubkey),
37+
application:unset_env(tpnode,privkey_dec);
3738
{error, Any} ->
3839
{error, Any}
3940
end.

apps/tpnode/src/tpnode_sup.erl

Lines changed: 30 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,7 @@
66
-export([start_link/0]).
77

88
%% Supervisor callbacks
9-
-export([init/1]).
9+
-export([init/1, check_key/0]).
1010

1111
%% Helper macro for declaring children of supervisor
1212
-define(CHILD(I, Type), {I, {I, start_link, []}, permanent, 5000, Type, [I]}).
@@ -22,6 +22,28 @@ start_link() ->
2222
%% Supervisor callbacks
2323
%% ===================================================================
2424

25+
check_key() ->
26+
try
27+
Priv=nodekey:get_priv(),
28+
case tpecdsa:keytype(Priv) of
29+
{priv, ed25519} -> ok;
30+
{priv, Type} ->
31+
throw({keytype_not_supported,Type})
32+
end,
33+
Public=nodekey:get_pub(),
34+
logger:notice("Starting up, pubkey is ~s",[hex:encode(Public)]),
35+
ok
36+
catch
37+
error:{badmatch,undefined} ->
38+
{error,"privkey does not specified"};
39+
throw:Reason ->
40+
{error,Reason};
41+
Ec:Ee ->
42+
logger:notice("Node key error ~p:~p",[Ec,Ee]),
43+
{error,"privkey broken"}
44+
end.
45+
46+
2547
init([repl_sup]) ->
2648
Sup={_SupFlags = {simple_one_for_one, 5, 10},
2749
[
@@ -32,6 +54,13 @@ init([repl_sup]) ->
3254

3355
init([]) ->
3456
tpnode:reload(),
57+
58+
case check_key() of
59+
ok -> ok;
60+
{error, Reason} ->
61+
throw(Reason)
62+
end,
63+
3564
MandatoryServices = [ api ],
3665
VMHost=case application:get_env(tpnode,vmaddr,undefined) of
3766
XHost when is_list(XHost) ->

0 commit comments

Comments
 (0)