Support many binary encoding formats

parent d7b19d8d
Pipeline #29164911 passed with stages
in 2 minutes and 19 seconds
(ns ercoin-wallet.binary
"Conversions between binary vectors and strings"
(:require
[alphabase.base58 :as base58]
[alphabase.hex :as hex]
[ercoin-wallet.uint :as uint]
[goog.crypt.base64 :as base64]))
(defprotocol Converter
(encode [this from])
(decode [this from]))
(defrecord BinaryConverter [name key encoder decoder]
Converter
(encode [this vect] (encoder (or vect [])))
(decode [this string] (decoder string)))
(def base58
(BinaryConverter.
"Base58"
:base58
#(base58/encode (clj->js %))
#(-> %
base58/decode
js/Array.from
js->clj)))
(def base64
(BinaryConverter.
"Base64"
:base64
#(-> %
uint/vec->uint8arr
base64/encodeByteArray)
#(-> %
base64/decodeStringToUint8Array
uint/uint8arr->vec)))
(def hex
(BinaryConverter.
"Hex"
:hex
#(hex/encode (clj->js %))
#(-> %
hex/decode
js/Array.from
js->clj)))
(def raw
(BinaryConverter.
"Raw bytes"
:raw
#(clojure.string/join "," %)
#(vec (map js/parseInt (clojure.string/split % ",")))))
(def all-converters
[base58 base64 hex raw])
(def all-converters-map
(into {} (map (fn [conv] [(:key conv) conv]) all-converters)))
(defn get-converter [key]
(get all-converters-map key))
......@@ -7,6 +7,7 @@
[reagent.core :as r]
[cljsjs.nacl-fast]
[ercoin-wallet.account :as a]
[ercoin-wallet.binary :as binary]
[ercoin-wallet.tendermint :as tendermint]
[ercoin-wallet.tx :as tx]
[ercoin-wallet.uint :as uint]))
......@@ -17,6 +18,8 @@
(local-storage (r/atom {}) :accounts))
(defonce rpc-endpoint
(local-storage (r/atom "http://localhost:26657") :rpc-endpoint))
(defonce binary-encoding
(local-storage (r/atom :base58) :binary-encoding))
;; Tendermint communication
......@@ -44,12 +47,20 @@
(doseq [account (vals @accounts)]
(refresh-account! account)))
;; Binary encoding
(defn binary-decode [string]
(binary/decode (binary/get-converter @binary-encoding) string))
(defn binary-encode [vect]
(binary/encode (binary/get-converter @binary-encoding) vect))
;; Views
(defn hidden-key [key]
[:input {:type :button
:value "Show"
:on-click #(js/alert (uint/vec->base58 key))}])
:on-click #(js/alert (binary-encode key))}])
(defn account-row [account]
(let [editing (r/atom false)]
......@@ -76,7 +87,7 @@
(-> e .-target .-value))))}]
label)]
[:td
[:code (uint/vec->base58 address)]]
[:code (binary-encode address)]]
[:td
(when private-key
[hidden-key private-key])]
......@@ -84,7 +95,7 @@
[:td valid-until]
[:td locked-until]
[:td
[:code (uint/vec->base58 validator-address)]]
[:code (binary-encode validator-address)]]
[:td
[:input {:type :button
:value "Remove"
......@@ -119,6 +130,18 @@
:value @rpc-endpoint
:on-change #(reset! rpc-endpoint (-> % .-target .-value))}]])
(defn binary-encoding-field []
[:p
"Binary encoding: "
[:select
{:defaultValue @binary-encoding}
(for [{key :key name :name} binary/all-converters]
^{:key key}
[:option
{:on-click #(reset! binary-encoding key)
:value key}
name])]])
(defn accounts-refresh-button []
[:p
[:input {:type :button
......@@ -165,8 +188,7 @@
[:div
[:p
[:label "Generated transaction: "]
[:code (str @tx-atom)]]
;; [:code (hex/encode (clj->js @tx-atom))]]
[:code (binary-encode @tx-atom)]]
[:p
[:input {:type :button
:value "Broadcast"
......@@ -308,21 +330,21 @@
[:p
[:label "Public key"]
[:input {:type :text
:value (uint/vec->base58 (::a/address @account))
:value (binary-encode (::a/address @account))
:on-change (fn [e] (swap! account #(assoc % ::a/address (->
e
.-target
.-value
uint/base58->vec))))}]]
binary-decode))))}]]
[:p
[:label "Private key"]
[:input {:type :text
:value (uint/vec->base58 (::a/private-key @account))
:value (binary-encode (::a/private-key @account))
:on-change (fn [e] (swap! account #(assoc % ::a/private-key (->
e
.-target
.-value
uint/base58->vec))))}]]
binary-decode))))}]]
[:input {:type :button
:value "Abort"
:on-click #(do
......@@ -340,7 +362,8 @@
(defn home-page []
[:div
[:section [:h2 "Configuration"]
[rpc-endpoint-field]]
[rpc-endpoint-field]
[binary-encoding-field]]
[:section [:h2 "Accounts"]
[accounts-refresh-button]
[:p [:kbd "Double click"] " label to edit, " [:kbd "Enter"] " or " [:kbd "Esc"] " to finish editing."]
......
(ns ercoin-wallet.uint
"Conversions between base58 strings, byte vectors, strings, integers"
"Conversions between byte vectors, strings, integers"
(:require
[alphabase.base58 :as base58]
[clojure.spec.alpha :as s]
[goog.crypt :as crypt]
))
......@@ -9,12 +8,6 @@
(defonce utf8-decoder (new js/TextDecoder "utf-8"))
(defonce utf8-encoder (new js/TextEncoder "utf-8"))
(defn decode [string]
(base58/decode string))
(defn encode [array]
(base58/encode array))
(defn uint8arr->vec [arr]
(js->clj (js/Array.from arr)))
......@@ -32,16 +25,6 @@
:args (s/cat :string string?)
:ret (s/coll-of int? :kind vector?))
(defn vec->base58 [vect]
(base58/encode (clj->js vect)))
(defn base58->vec [b58]
(-> b58
(base58/decode)
(or [])
(js/Array.from)
(js->clj)))
(defn vec->uint [uint-bin]
(loop [acc 0
rem-uint-bin uint-bin]
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment