Skip to content
GitLab
Explore
Sign in
Primary navigation
Search or go to…
Project
Ğ
Ğcli-v2s
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
clients
Rust
Ğcli-v2s
Commits
f1ff1004
Commit
f1ff1004
authored
1 year ago
by
Hugo Trentesaux
Browse files
Options
Downloads
Patches
Plain Diff
move into function
parent
c70b26e8
No related branches found
No related tags found
1 merge request
!22
add vault
Changes
1
Show whitespace changes
Inline
Side-by-side
Showing
1 changed file
src/commands/vault.rs
+23
-18
23 additions, 18 deletions
src/commands/vault.rs
with
23 additions
and
18 deletions
src/commands/vault.rs
+
23
−
18
View file @
f1ff1004
...
...
@@ -10,7 +10,7 @@ pub enum Subcommand {
List
,
/// Show where vault stores secret
Where
,
/// Generate
new key (unimplemented)
/// Generate
a mnemonic
Generate
,
/// Import mnemonic with interactive prompt
Import
,
...
...
@@ -58,33 +58,38 @@ pub fn handle_command(data: Data, command: Subcommand) -> Result<(), GcliError>
println!
(
"{}"
,
data
.project_dir
.data_dir
()
.to_str
()
.unwrap
());
}
Subcommand
::
Generate
=>
{
let
mnemonic
=
bip39
::
Mnemonic
::
generate
(
12
)
.unwrap
();
// TODO allow word count
// TODO allow custom word count
let
mnemonic
=
bip39
::
Mnemonic
::
generate
(
12
)
.unwrap
();
println!
(
"{mnemonic}"
);
}
Subcommand
::
Import
=>
{
// --- currently only support mnemonic secret
// get secret
let
secret
=
rpassword
::
prompt_password
(
"Mnemonic: "
)
?
;
// check validity by deriving keypair
let
keypair
=
pair_from_str
(
&
secret
)
?
;
// ask password to protect key
let
mnemonic
=
rpassword
::
prompt_password
(
"Mnemonic: "
)
?
;
println!
(
"Enter password to protect the key"
);
let
password
=
rpassword
::
prompt_password
(
"Password: "
)
?
;
// write encrypted secret in file identitfied by pubkey
let
path
=
data
.project_dir
.data_dir
()
.join
(
keypair
.public
()
.to_string
());
let
mut
file
=
std
::
fs
::
OpenOptions
::
new
()
.create
(
true
)
.write
(
true
)
.open
(
path
)
?
;
file
.write_all
(
&
encrypt
(
secret
.as_bytes
(),
password
)
.map_err
(|
e
|
anyhow!
(
e
))
?
[
..
])
?
;
let
address
=
store_mnemonic
(
&
data
,
&
mnemonic
,
password
)
?
;
println!
(
"Stored secret for {address}"
);
}
};
Ok
(())
}
/// store mnemonic protected with password
pub
fn
store_mnemonic
(
data
:
&
Data
,
mnemonic
:
&
str
,
password
:
String
,
)
->
Result
<
AccountId
,
GcliError
>
{
// check validity by deriving keypair
let
keypair
=
pair_from_str
(
&
mnemonic
)
?
;
let
address
=
keypair
.public
();
// write encrypted mnemonic in file identified by pubkey
let
path
=
data
.project_dir
.data_dir
()
.join
(
address
.to_string
());
let
mut
file
=
std
::
fs
::
File
::
create
(
path
)
?
;
file
.write_all
(
&
encrypt
(
mnemonic
.as_bytes
(),
password
)
.map_err
(|
e
|
anyhow!
(
e
))
?
[
..
])
?
;
Ok
(
keypair
.public
()
.into
())
}
/// try get secret in keystore
pub
fn
try_fetch_secret
(
data
:
&
Data
,
address
:
AccountId
)
->
Result
<
Option
<
String
>
,
GcliError
>
{
let
path
=
data
.project_dir
.data_dir
()
.join
(
address
.to_string
());
...
...
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