Skip to content

A peer-to-peer stack for code collaboration

Handle my-keys via key-mgmt module & add a key creation command (#411)

Merle Breitkreuze901120e2473
diff --git a/bin/rad-create-keys b/bin/rad-create-keys
new file mode 100755
index 0000000..6aa19fc
--- /dev/null
+++ b/bin/rad-create-keys
@@ -0,0 +1,33 @@
+#!/usr/bin/env radicle
+
+(load! (find-module-file! "prelude.rad"))
+
+(import prelude/key-management :as 'key-mgmt)
+
+(def help
+ "rad-create-keys - Radicle Key Management CLI
+
+ Usage:
+ rad-create-keys [--force]
+ rad-create-keys help
+
+ Create a new key pair with `rad-create-keys`. If `--force` is appended, an
+ existing key pair file will be overwritten.
+ Per default, key pairs are stored in `$HOME/.config/radicle/my-keys.rad`
+ this can be adjusted by setting `$XDG_CONFIG_HOME`.")
+
+(def make-keys
+ (fn [] (key-mgmt/create-keys!)))
+
+(def safe-make-keys
+ (fn []
+ (match (key-mgmt/read-keys!)
+ :nothing (make-keys)
+ _ (put-str! "The `my-keys.rad` file already exists.
+If you are sure that you want to replace your key pair, run `rad-create-keys --force`"))))
+
+(def args (get-args!))
+(match args
+ ["--force"] (make-keys)
+ [] (safe-make-keys)
+ _ (put-str! help))
diff --git a/docs/source/reference.rst b/docs/source/reference.rst
index 5eca6b0..06fb1f4 100644
--- a/docs/source/reference.rst
+++ b/docs/source/reference.rst
@@ -1127,6 +1127,31 @@ Like ``set``, but for refs.
Like ``over``, but for refs.
+``prelude/key-management``
+--------------------------
+
+Providing functions for creating and reading key pairs for signing send
+commands. Per default, key pairs are stored in
+``$HOME/.config/radicle/my-keys.rad`` this can be adjusted by setting
+``$XDG_CONFIG_HOME``.
+
+``(read-keys!)``
+~~~~~~~~~~~~~~~~
+
+Reads the keys stored in ``my-keys.rad`` or returns ``:nothing`` if the
+file doesn't exist.
+
+``(create-keys!)``
+~~~~~~~~~~~~~~~~~~
+
+Creates a new key pair and stores it in ``my-keys.rad``
+
+``(use-fake-keys!)``
+~~~~~~~~~~~~~~~~~~~~
+
+Bypass reading the keys from ``my-keys.rad``. This is intended for
+testing.
+
``prelude/chain``
-----------------
diff --git a/rad/monadic/diff.rad b/rad/monadic/diff.rad
index 234960c..eb3f3eb 100644
--- a/rad/monadic/diff.rad
+++ b/rad/monadic/diff.rad
@@ -4,6 +4,7 @@
(import prelude/chain '[send-code! ]:unqualified)
(import prelude/time '[install-fake-clock] :unqualified)
(import prelude/io :as 'io)
+(import prelude/key-management '[use-fake-keys!] :unqualified)
(def radicle-diff-chain-id
"The name of this chain."
@@ -111,9 +112,7 @@
[:setup
(do (chain/install-remote-chain-fake)
(install-fake-clock)
- (io/install-fake-filesystem!
- { "../my-keys.rad" (show (gen-key-pair! (default-ecc-curve)))
- })
+ (use-fake-keys!)
(def chain-name "http://foo")
(def chain (ref (chain/new-chain chain-name)))
(create-diffs-chain! chain-name)
diff --git a/rad/monadic/issues.rad b/rad/monadic/issues.rad
index 21457c6..37a97ec 100644
--- a/rad/monadic/issues.rad
+++ b/rad/monadic/issues.rad
@@ -11,6 +11,7 @@
(import prelude/chain '[send-code! send-signed-command!] :unqualified)
(import prelude/time '[install-fake-clock] :unqualified)
(import prelude/io :as 'io)
+(import prelude/key-management '[use-fake-keys!] :unqualified)
(def radicle-issues-chain-id
"The name of this chain."
@@ -174,9 +175,7 @@
[:setup
(do (chain/install-remote-chain-fake)
(install-fake-clock)
- (io/install-fake-filesystem!
- { "../my-keys.rad" (show (gen-key-pair! (default-ecc-curve)))
- })
+ (use-fake-keys!)
(def chain-name (string-append "http://foo"))
(def chain (ref (chain/new-chain chain-name)))
(create-issues-chain! chain-name)
diff --git a/rad/monadic/project.rad b/rad/monadic/project.rad
index 790557f..d2b3a05 100644
--- a/rad/monadic/project.rad
+++ b/rad/monadic/project.rad
@@ -4,6 +4,7 @@
(import prelude/chain '[send-code! send-signed-command! make-chain-ref!] :unqualified)
(import prelude/time '[install-fake-clock] :unqualified)
(import prelude/io :as 'io)
+(import prelude/key-management '[use-fake-keys!] :unqualified)
(def get-project-url!
(fn []
@@ -72,9 +73,7 @@
[:setup
(do (chain/install-remote-chain-fake)
(install-fake-clock)
- (io/install-fake-filesystem!
- { "../my-keys.rad" (show (gen-key-pair! (default-ecc-curve)))
- })
+ (use-fake-keys!)
(def chain-name "http://bar")
(def chain (ref (chain/new-chain chain-name)))
(create-project! chain-name { :name "test" :description "test project"})
diff --git a/rad/prelude.rad b/rad/prelude.rad
index ca56137..6d4a298 100644
--- a/rad/prelude.rad
+++ b/rad/prelude.rad
@@ -14,6 +14,7 @@
(file-module! "prelude/set.rad")
(file-module! "prelude/ref.rad")
(file-module! "prelude/lens.rad")
+(file-module! "prelude/key-management.rad")
(file-module! "prelude/chain.rad")
(file-module! "prelude/chain-remote.rad")
(file-module! "prelude/state-machine.rad")
diff --git a/rad/prelude/chain.rad b/rad/prelude/chain.rad
index c7345d7..35d2bd0 100644
--- a/rad/prelude/chain.rad
+++ b/rad/prelude/chain.rad
@@ -12,6 +12,7 @@
(import prelude/patterns :unqualified)
(import prelude/strings :unqualified)
(import prelude/seq :unqualified)
+(import prelude/key-management :as 'key-mgmt)
(def base-send!
"See documentation of `send!`"
@@ -264,6 +265,14 @@
(fn [chain-id filename]
(send! chain-id (io/read-file-values! filename))))
+(def get-keys!
+ (fn []
+ (match (key-mgmt/read-keys!)
+ :nothing (do (put-str! "Missing file: my-keys.rad
+Do `rad-create-keys` to create your own key pair and rerun your command.")
+ (exit! 1))
+ 'keys keys)))
+
(def sign-entity!
"Assumes a key pair is stored at `my-keys.rad`. Using that key pair, will sign a
dict by adding `:author` and `:signature` fields, so that it is valid
@@ -272,7 +281,7 @@
(def nonce (uuid!))
(def e_ (<> e {:chain-id chain-id
:nonce nonce}))
- (def ks (io/read-file-value! (find-module-file! "../my-keys.rad")))
+ (def ks (get-keys!))
(def sig
(gen-signature! (lookup :private-key ks)
(show e_)))
diff --git a/rad/prelude/key-management.rad b/rad/prelude/key-management.rad
new file mode 100644
index 0000000..5912f58
--- /dev/null
+++ b/rad/prelude/key-management.rad
@@ -0,0 +1,48 @@
+{:module 'prelude/key-management
+ :doc "Providing functions for creating and reading key pairs for signing send
+ commands. Per default, key pairs are stored in `$HOME/.config/radicle/my-keys.rad`
+ this can be adjusted by setting `$XDG_CONFIG_HOME`."
+ :exports '[read-keys! create-keys! use-fake-keys!]}
+
+(import prelude/io :as 'io)
+(import prelude/strings :as 'string)
+(import prelude/lens :unqualified)
+
+(def key-pair-mode (ref {:use-fake-key #f}))
+
+(def use-fake-keys!
+ "Bypass reading the keys from `my-keys.rad`. This is intended for testing."
+ (fn []
+ (set-ref key-pair-mode (@ :use-fake-key) #t)
+ (set-ref key-pair-mode (@ :fake-key) (gen-key-pair! (default-ecc-curve)))))
+
+(def base-path
+ (fn []
+ (def xgd-config-home (string/unlines (io/shell-with-stdout! "echo $XDG_CONFIG_HOME" "")))
+ (def home (string/unlines (io/shell-with-stdout! "echo $HOME" "")))
+ (def config-path
+ (if (eq? xgd-config-home "")
+ (string-append home "/.config")
+ xgd-config-home))
+ (string-append config-path "/radicle")))
+
+(def key-path
+ (fn []
+ (string-append (base-path) "/my-keys.rad")))
+
+(def read-keys!
+ "Reads the keys stored in `my-keys.rad` or returns `:nothing` if the file
+ doesn't exist."
+ (fn []
+ (if (lookup :use-fake-key (read-ref key-pair-mode))
+ (lookup :fake-key (read-ref key-pair-mode))
+ (do (catch 'any
+ (io/read-file-value! (find-module-file! (key-path)))
+ (fn [x] :nothing))))))
+
+(def create-keys!
+ "Creates a new key pair and stores it in `my-keys.rad`"
+ (fn []
+ (io/shell! (string-append "mkdir -p " (base-path)) "")
+ (def kp (gen-key-pair! (default-ecc-curve)))
+ (io/write-file! (key-path) (show kp))))
diff --git a/reference-doc.yaml b/reference-doc.yaml
index 80801a6..99c30f0 100644
--- a/reference-doc.yaml
+++ b/reference-doc.yaml
@@ -144,6 +144,7 @@ modules:
- prelude/set
- prelude/ref
- prelude/lens
+- prelude/key-management
- prelude/chain
- prelude/state-machine
- prelude/validation