A peer-to-peer stack for code collaboration
Name chain (#335)
Julian Arnic0d5b0736e3c
diff --git a/bin/rad-issues b/bin/rad-issuesindex f7180cc..4867727 100755--- a/bin/rad-issues+++ b/bin/rad-issues@@ -6,17 +6,23 @@(def base-name "http://machines.radicle.xyz/chains/")+(def name-base "http://machines.radicle.xyz/chains/names")+(def help(string-append"rad-issues - Radicle issue CLIUsage:rad-issues [list|new|create-chain] <chain-name>+ rad-issues make-key <nick>+ rad-issues namesrad-issues helplist - Select from all issues to view or comment onnew - Create a new issue in $EDITORcreate-chain - Create a new issues chain+ make-key - Create a key-pair and register the name+ names - See all registered nameshelp - Print this help and exitIf <chain-name> begins with 'http://', that URL will be used. Otherwise,@@ -77,8 +83,6 @@(def new-issue(fn [chain](def chain-ref (make-chain-ref chain))- (load! "rad/prelude/io-utils.rad")- (load! "rad/monadic/issues.rad")(def template";; Issue template. An empty or invalid value will abort.{:title \"Pick a title\"@@ -95,9 +99,23 @@(fn [chain](create-issues-chain! (make-name chain))))+(def make-key+ (fn [name]+ (def kp (gen-key-pair! (default-ecc-curve)))+ (write-file! "my-keys.rad" (show kp))+ (load! "rad/monadic/names.rad")+ (add-name! name-base name (lookup :public-key kp))))++(def names+ (fn []+ (load! "rad/monadic/names.rad")+ (print! (list-names name-base))))+(def args (get-args!))(match args["list" 'chain] (list-issues chain)+ ["names"] (names)+ ["make-key" 'name] (make-key name)["new" 'chain] (new-issue chain)["create-chain" 'chain] (create-chain chain)["help"] (put-str! help)diff --git a/rad/monadic/issues.rad b/rad/monadic/issues.radindex c660bf9..64267eb 100644--- a/rad/monadic/issues.rad+++ b/rad/monadic/issues.rad@@ -164,7 +164,7 @@(:test"The monadic issues chain works."[:setup- (do (def chain-name "http://localhost:8024/chains/fxoo")+ (do (def chain-name "http://localhost:8000/chains/fxoo")(def chain (ref (chain/new-chain chain-name)))(create-issues-chain! chain-name)(simple-create-issue! chain "title0" "body0")diff --git a/rad/monadic/names-remote.rad b/rad/monadic/names-remote.radnew file mode 100644index 0000000..ab65c31--- /dev/null+++ b/rad/monadic/names-remote.rad@@ -0,0 +1,75 @@+(import prelude/basic :unqualified)+(import prelude/bool :unqualified)+(import prelude/dict :unqualified)+(import prelude/ref :unqualified)+(import prelude/validation :as 'validator)+(import prelude/patterns :unqualified)+(import prelude/util :unqualified)+(import prelude/lens :unqualified)++(def names (ref []))++(def name-unused?+ (fn [name]+ (not (elem? name (map first (read-ref names))))))++(def key-unused?+ (fn [key]+ (not (elem? key (map (fn [x] (nth 1 x)) (read-ref names))))))++(def validator/name+ (validator/and+ [(validator/type :string)+ (validator/pred "Name has been taken" name-unused?)+ ]))++(def add-name+ (fn [name pubkey]+ (validator/name name)+ ((validator/pred "Key has been taken" key-unused?) pubkey)+ (modify-ref names (fn [n] (cons [name pubkey] n)))))++(def list-names+ (fn []+ (read-ref names)))+++;; Commands++(def commands+ "The set of allowed chain commands."+ (ref+ {'add-name add-name+ 'list-names list-names}))++(def process-command+ "Function that maps post-eval commands to existing functions"+ (fn [expr]+ (def command (first expr))+ (def args (rest expr))+ (def do-this (lookup command (read-ref commands)))+ (apply do-this args)))++(def eval-fn-app+ "Given a state, a function, an argument and a callback, returns+ the result of evaluating the function call on the arg in the given+ state, while also calling the callback on the result."+ (fn [state f arg cb]+ (def exec (base-eval (list f (list 'quote arg)) state))+ (cb (first exec))+ exec))++(def updatable-eval+ "Given an evaluation function `f`, returns a new one which augments `f` with a new+ command `(update expr)` which evaluates arbitrary expression using `base-eval`."+ (fn [sub-eval]+ (fn [expr state]+ (match expr+ (/cons 'cmd _) (if (eq? cmd 'update)+ (base-eval (nth 1 expr) state)+ (sub-eval expr state))+ _ (sub-eval expr state)))))++(def eval (updatable-eval+ (fn [expr state]+ (eval-fn-app state 'process-command expr (fn [x] x)))))diff --git a/rad/monadic/names.rad b/rad/monadic/names.radnew file mode 100644index 0000000..4cf59b2--- /dev/null+++ b/rad/monadic/names.rad@@ -0,0 +1,44 @@+(load! "rad/prelude.rad")++(def create-name-chain!+ (fn [url]+ (chain/send-prelude! url)+ (send-code! url "rad/monadic/names-remote.rad")))++(def lookup-key+ (fn [chain-ref key]+ (def chain (read-ref chain-ref))+ (def names (lookup :result (chain/eval-in-chain '(list-names) chain)))+ (match (filter (fn [v] (eq? (nth 1 v) key)) names)+ [['name _]] name+ _ :nothing)))++(def list-names+ (fn [chain-name]+ (def chain (chain/load-chain! chain-name))+ (lookup :result (chain/eval-in-chain '(list-names) chain))))++(def add-name!+ (fn [name-chain-url name key]+ (send!+ name-chain-url+ [(list 'add-name name key)])))++(def fetch!+ "Update the locally cached state of the chain."+ (fn [chain]+ (chain/update-chain-ref! chain)))++(:test+ "The monadic name chain works."+ [:setup+ (do (def chain-name "http://localhost:8000/chains/shkjksa")+ (create-name-chain! chain-name)+ (add-name! chain-name "alice" "a")+ (add-name! chain-name "bob" "b")+ (def chain (ref (chain/load-chain! chain-name))))+ ]+ [ (lookup-key chain "a") ==> "alice" ]+ [ (lookup-key chain "b") ==> "bob" ]+ [ (lookup-key chain "c") ==> :nothing ]+)diff --git a/test/spec/Radicle/Tests.hs b/test/spec/Radicle/Tests.hsindex 9cd6b02..1a6ce3b 100644--- a/test/spec/Radicle/Tests.hs+++ b/test/spec/Radicle/Tests.hs@@ -830,7 +830,10 @@ test_cbor =-- Radicle source with the @:test@ macro.test_source_files :: IO TestTreetest_source_files = do- tests <- join <$> traverse testOne ["rad/prelude.rad", "rad/monadic/issues.rad"]+ tests <- join <$> traverse testOne [ "rad/prelude.rad"+ , "rad/monadic/issues.rad"+ , "rad/monadic/names.rad"+ ]pure $ testGroup "Radicle source file tests" testswheretestOne :: FilePath -> IO [TestTree]