Skip to content
GitLab
Explore
Sign in
Primary navigation
Search or go to…
Project
D
duniter-mini-client
Manage
Activity
Members
Labels
Plan
Issues
Issue boards
Milestones
Wiki
Code
Merge requests
Repository
Branches
Commits
Tags
Repository graph
Compare revisions
Snippets
Build
Pipelines
Jobs
Pipeline schedules
Artifacts
Deploy
Releases
Package Registry
Container Registry
Model registry
Operate
Environments
Terraform modules
Monitor
Incidents
Service Desk
Analyze
Value stream analytics
Contributor analytics
CI/CD analytics
Repository analytics
Model experiments
Help
Help
Support
GitLab documentation
Compare GitLab plans
Community forum
Contribute to GitLab
Provide feedback
Keyboard shortcuts
?
Snippets
Groups
Projects
Show more breadcrumbs
Cédric Moreau
duniter-mini-client
Commits
1d38e8ae
Commit
1d38e8ae
authored
3 years ago
by
Cédric Moreau
Browse files
Options
Downloads
Patches
Plain Diff
refact: lighter main
parent
e1e1834c
No related branches found
Branches containing commit
No related tags found
No related merge requests found
Changes
5
Hide whitespace changes
Inline
Side-by-side
Showing
5 changed files
.gitignore
+1
-0
1 addition, 0 deletions
.gitignore
src/bma/lookup_identity.rs
+1
-1
1 addition, 1 deletion
src/bma/lookup_identity.rs
src/cli/certify.rs
+1
-1
1 addition, 1 deletion
src/cli/certify.rs
src/cli/mod.rs
+84
-15
84 additions, 15 deletions
src/cli/mod.rs
src/main.rs
+13
-10
13 additions, 10 deletions
src/main.rs
with
100 additions
and
27 deletions
.gitignore
+
1
−
0
View file @
1d38e8ae
/target
.idea/
\ No newline at end of file
This diff is collapsed.
Click to expand it.
src/bma/lookup_identity.rs
+
1
−
1
View file @
1d38e8ae
...
...
@@ -23,7 +23,7 @@ impl LookupIdentity {
}
}
pub
fn
lookup
(
node
:
BmaNode
,
uid_or_pub
:
String
)
->
()
{
pub
fn
lookup
(
node
:
&
BmaNode
,
uid_or_pub
:
&
String
)
->
()
{
let
address
=
node
.get_address
();
let
resp
=
reqwest
::
blocking
::
get
(
format!
(
"{}/wot/lookup/{}"
,
address
,
uid_or_pub
))
.expect
(
"Could not fetch lookup data from distant node"
);
lookup_print
(
&
LookupResult
(
&
resp
.json
()
.expect
(
"Could not get JSON result from distant node"
)));
...
...
This diff is collapsed.
Click to expand it.
src/cli/certify.rs
+
1
−
1
View file @
1d38e8ae
...
...
@@ -5,7 +5,7 @@ use crate::crypto::duniter_key::ToDuniterKey;
use
crate
::
dubp
::
documents
::
certification
::
Certification
;
use
crate
::
dubp
::
signable
::
Signable
;
pub
fn
certify
(
node
:
BmaNode
,
uid_or_pub
:
String
)
->
Result
<
(),
String
>
{
pub
fn
certify
(
node
:
&
BmaNode
,
uid_or_pub
:
&
String
)
->
Result
<
(),
String
>
{
let
address
=
node
.get_address
();
println!
(
"Fetching identity using
\"
{}
\"
pattern..."
,
uid_or_pub
);
let
resp
=
reqwest
::
blocking
::
get
(
format!
(
"{}/wot/lookup/{}"
,
address
,
uid_or_pub
))
.expect
(
"Could not fetch lookup data from distant node"
);
...
...
This diff is collapsed.
Click to expand it.
src/cli/mod.rs
+
84
−
15
View file @
1d38e8ae
...
...
@@ -3,62 +3,131 @@ use std::env::Args;
use
crate
::
cli
::
Command
::
*
;
use
crate
::
bma
::
BmaNode
;
use
std
::
env
;
use
crate
::{
bma
,
cli
,
compute_key
,
compute_pub
,
compute_sec
};
use
crate
::
crypto
::
duniter_key
::
ScryptDuniterKey
;
use
core
::
fmt
;
pub
mod
certify
;
const
DEFAULT_NODE
:
&
str
=
"https://g1-test.duniter.org"
;
// TODO: constantize pub, sec, etc
pub
enum
Command
{
/// Compute the public key from salt/passwd and displays it
PUB
(
String
,
String
),
PUB
(
String
,
String
,
String
),
/// Compute the secret key from salt/passwd and displays it
SEC
(
String
,
String
),
SEC
(
String
,
String
,
String
),
/// Compute the keyring from salt/passwd and displays it
KEYRING
(
String
,
String
),
KEYRING
(
String
,
String
,
String
),
/// Search an identity on a Duniter node (BMA API)
LOOKUP
(
BmaNode
,
String
),
LOOKUP
(
String
,
BmaNode
,
String
),
/// Search an identity on a Duniter node (BMA API) and certify it
CERTIFY
(
BmaNode
,
String
),
///
U
nknown command
UNKNOWN
(
String
)
CERTIFY
(
String
,
BmaNode
,
String
),
///
Some u
nknown command
UNKNOWN
(
String
)
,
}
impl
Command
{
pub
fn
from
(
mut
args
:
Args
)
->
Command
{
pub
fn
name
(
&
self
)
->
String
{
let
str_name
=
match
self
{
PUB
(
_
,
_
,
_
)
=>
"pub"
,
SEC
(
_
,
_
,
_
)
=>
"sec"
,
KEYRING
(
_
,
_
,
_
)
=>
"keyring"
,
LOOKUP
(
_
,
_
,
_
)
=>
"lookup"
,
CERTIFY
(
_
,
_
,
_
)
=>
"certify"
,
UNKNOWN
(
name
)
=>
name
.as_str
(),
};
str_name
.to_string
()
}
pub
fn
execute
(
&
self
)
->
Result
<
(),
String
>
{
match
self
{
PUB
(
_
,
salt
,
passwd
)
=>
{
println!
(
"{}"
,
compute_pub
(
ScryptDuniterKey
::
new
(
salt
.to_string
(),
passwd
.to_string
())));
}
SEC
(
_
,
salt
,
passwd
)
=>
println!
(
"{}"
,
compute_sec
(
ScryptDuniterKey
::
new
(
salt
.to_string
(),
passwd
.to_string
()))),
KEYRING
(
_
,
salt
,
passwd
)
=>
println!
(
"{}"
,
compute_key
(
ScryptDuniterKey
::
new
(
salt
.to_string
(),
passwd
.to_string
()))),
LOOKUP
(
_
,
bma_node
,
uid_or_pub
)
=>
bma
::
lookup_identity
::
lookup
(
bma_node
,
uid_or_pub
),
CERTIFY
(
_
,
bma_node
,
uid_or_pub
)
=>
cli
::
certify
::
certify
(
bma_node
,
uid_or_pub
)
.unwrap_or_else
(|
e
|
eprintln!
(
"{}"
,
e
)),
UNKNOWN
(
cmd
)
=>
eprintln!
(
"Unknown command {}"
,
cmd
),
_
=>
return
Err
(
"Not handled command yet"
.to_string
())
}
Ok
(())
}
pub
fn
from
<
I
>
(
mut
args
:
I
)
->
Option
<
Command
>
where
I
:
Iterator
<
Item
=
String
>
{
args
.next
();
let
command
=
args
.next
()
.expect
(
"Command must be provided"
);
let
command
=
match
args
.next
()
{
None
=>
return
None
,
Some
(
cmd_name
)
=>
cmd_name
};
let
command
=
command
.as_str
();
match
command
{
"pub"
=>
{
let
salt
=
args
.next
()
.expect
(
"Salt must be provided"
);
let
passwd
=
args
.next
()
.expect
(
"Password must be provided"
);
PUB
(
salt
,
passwd
)
Some
(
PUB
(
command
.to_string
(),
salt
,
passwd
)
)
},
"sec"
=>
{
let
salt
=
args
.next
()
.expect
(
"Salt must be provided"
);
let
passwd
=
args
.next
()
.expect
(
"Password must be provided"
);
S
EC
(
salt
,
passwd
)
S
ome
(
SEC
(
command
.to_string
(),
salt
,
passwd
)
)
},
"keyring"
=>
{
let
salt
=
args
.next
()
.expect
(
"Salt must be provided"
);
let
passwd
=
args
.next
()
.expect
(
"Password must be provided"
);
KEYRING
(
salt
,
passwd
)
Some
(
KEYRING
(
command
.to_string
(),
salt
,
passwd
)
)
},
"lookup"
=>
{
let
uid_or_pub
=
args
.next
()
.expect
(
"UID or pubkey must be provided"
);
let
default
=
String
::
from
(
DEFAULT_NODE
);
let
bma_node
=
BmaNode
::
new
(
env
::
var
(
"DUNITER_NODE"
)
.unwrap_or
(
default
)
.as_str
());
LOOKUP
(
bma_node
,
uid_or_pub
)
Some
(
LOOKUP
(
command
.to_string
(),
bma_node
,
uid_or_pub
)
)
},
"cert"
=>
{
let
uid_or_pub
=
args
.next
()
.expect
(
"UID or pubkey must be provided"
);
let
default
=
String
::
from
(
DEFAULT_NODE
);
let
bma_node
=
BmaNode
::
new
(
env
::
var
(
"DUNITER_NODE"
)
.unwrap_or
(
default
)
.as_str
());
CERTIFY
(
bma_node
,
uid_or_pub
)
Some
(
CERTIFY
(
command
.to_string
(),
bma_node
,
uid_or_pub
)
)
},
_
=>
UNKNOWN
(
String
::
from
(
command
)),
_
=>
Some
(
UNKNOWN
(
command
.to_string
()
)),
}
}
}
#[cfg(test)]
mod
tests
{
use
crate
::
cli
::
Command
;
use
crate
::
cli
::
Command
::
PUB
;
#[test]
fn
known_command
()
{
let
args
=
vec!
[
""
,
"pub"
,
"abc"
,
"def"
];
let
cmd
=
Command
::
from
(
args
.into_iter
()
.map
(|
x
|
x
.to_string
()));
assert!
(
cmd
.is_some
());
let
cmd
=
cmd
.unwrap
();
let
is_expected_cmd
=
if
let
Command
::
PUB
(
..
)
=
cmd
{
true
}
else
{
false
};
assert!
(
is_expected_cmd
);
}
#[test]
fn
unknown_command
()
{
let
args
=
vec!
[
""
,
"abracadabra"
];
let
cmd
=
Command
::
from
(
args
.into_iter
()
.map
(|
x
|
x
.to_string
()));
assert!
(
cmd
.is_some
());
let
cmd
=
cmd
.unwrap
();
let
is_expected_cmd
=
if
let
Command
::
UNKNOWN
(
..
)
=
cmd
{
true
}
else
{
false
};
assert!
(
is_expected_cmd
);
}
#[test]
fn
no_command
()
{
let
args
=
vec!
[
""
];
let
cmd
=
Command
::
from
(
args
.into_iter
()
.map
(|
x
|
x
.to_string
()));
assert!
(
cmd
.is_none
());
}
}
\ No newline at end of file
This diff is collapsed.
Click to expand it.
src/main.rs
+
13
−
10
View file @
1d38e8ae
use
std
::
env
;
use
std
::
process
::
exit
;
use
duniter_mini_client
::{
compute_pub
,
compute_sec
,
compute_key
,
cli
,
bma
};
use
duniter_mini_client
::
cli
::
Command
;
use
duniter_mini_client
::
cli
::
Command
::{
UNKNOWN
,
PUB
,
SEC
,
KEYRING
,
LOOKUP
,
CERTIFY
};
use
duniter_mini_client
::
cli
::
Command
::{
PUB
,
SEC
,
KEYRING
,
LOOKUP
,
CERTIFY
};
use
duniter_mini_client
::
crypto
::
duniter_key
::
ScryptDuniterKey
;
fn
main
()
{
let
command
=
Command
::
from
(
env
::
args
());
// TODO: Result<(), String>
match
command
{
PUB
(
salt
,
passwd
)
=>
println!
(
"{}"
,
compute_pub
(
ScryptDuniterKey
::
new
(
salt
,
passwd
))),
SEC
(
salt
,
passwd
)
=>
println!
(
"{}"
,
compute_sec
(
ScryptDuniterKey
::
new
(
salt
,
passwd
))),
KEYRING
(
salt
,
passwd
)
=>
println!
(
"{}"
,
compute_key
(
ScryptDuniterKey
::
new
(
salt
,
passwd
))),
LOOKUP
(
bma_node
,
uid_or_pub
)
=>
bma
::
lookup_identity
::
lookup
(
bma_node
,
uid_or_pub
),
CERTIFY
(
bma_node
,
uid_or_pub
)
=>
cli
::
certify
::
certify
(
bma_node
,
uid_or_pub
)
.unwrap_or_else
(|
e
|
eprintln!
(
"{}"
,
e
)),
UNKNOWN
(
cmd
)
=>
eprintln!
(
"Unknown command {}"
,
cmd
),
let
command
=
match
command
{
Some
(
c
)
=>
c
,
None
=>
{
eprintln!
(
"No command found"
);
exit
(
1
);
}
};
if
let
Err
(
e
)
=
command
.execute
()
{
eprintln!
(
"Command executed with error: {}"
,
e
);
exit
(
2
);
}
}
\ No newline at end of file
This diff is collapsed.
Click to expand it.
Preview
0%
Loading
Try again
or
attach a new file
.
Cancel
You are about to add
0
people
to the discussion. Proceed with caution.
Finish editing this message first!
Save comment
Cancel
Please
register
or
sign in
to comment