Skip to content

Commit e597dfa

Browse files
authored
Merge pull request #25 from CicadaBank/release/v0.1.2
Release/v0.1.2
2 parents 7e0ebbe + 9919954 commit e597dfa

File tree

7 files changed

+80
-39
lines changed

7 files changed

+80
-39
lines changed

.github/workflows/test.yml

Lines changed: 15 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
# from https://github.com/Flexiana/framework/blob/main/.github/workflows/test.yml
2-
name: Clojure CI
2+
name: Test Library CI
33

44
on:
55
push:
@@ -45,3 +45,17 @@ jobs:
4545

4646
- name: Run tests
4747
run: clojure -M:run/test
48+
49+
- name: Generate Package
50+
run: clojure -T:generator jar
51+
52+
- name: Set TAG env variable
53+
run: echo "TAG=$(grep :version release.edn | awk -F" " '{print $2}' | tr -d "\"")" >> $GITHUB_ENV
54+
55+
- name: Test Package
56+
run: cp -r test target/ && cd target && clojure -Sforce -Sverbose -Sdeps '
57+
{:deps {com.cicadabank/bips {:local/root "bips-${{env.TAG}}.jar"},
58+
clj-async-test/clj-async-test {:mvn/version "0.0.5"},
59+
io.github.cognitect-labs/test-runner {:git/tag "v0.5.0"
60+
:git/sha "b3fd0d2"}},
61+
:paths ["test/"]}' -m cognitect.test-runner

README.md

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -242,6 +242,14 @@ index 0) of the Bitcoin coin type (coin type "BTC").
242242
;; => "m/44'/0'/0'/0/0"
243243
```
244244

245+
A second version of `derivation-path` takes only 2 arguments a
246+
`coin-type` and an `account` index to derive an account address.
247+
248+
```clojure
249+
(derivation-path "BTC" 0)
250+
;; => "m/44'/0'/0'"
251+
```
252+
245253
Reference
246254
---------
247255

release.edn

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
11
{:group-id "com.cicadabank"
22
:artifact-id "bips"
3-
:version "0.1.1"
3+
:version "0.1.2"
44
:scm-url "https://github.com/CicadaBank/bips"}

src/bips/bip32.clj

Lines changed: 23 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -119,7 +119,7 @@
119119
{:public-key (compress-public-key
120120
(.toString
121121
(private->public-key
122-
(BigInteger. (apply str k-par) 16)) 16))
122+
(BigInteger. k-par 16)) 16))
123123
:chain-code c-par
124124
:index index
125125
:depth depth})
@@ -133,20 +133,25 @@
133133
:index 1000000000,
134134
:depth 5}`"
135135
[seed chain-path key-type]
136-
(let [path-parts (str/split chain-path #"/")]
137-
(loop [current-node (if (= "m" (first path-parts))
138-
`(derive-master-node ~seed)
139-
(throw (Exception.
140-
(str "Invalid path: " (first path-parts)))))
141-
parts (rest path-parts)]
142-
(if (seq parts)
143-
(let [part (first parts)
144-
index (if (hardened? part)
145-
(hardened (Integer/parseInt (subs part 0 (- (count part) 1))))
146-
(Integer/parseInt part))]
147-
(recur
148-
`(CKDpriv ~current-node ~index)
149-
(rest parts)))
150-
(if (= :public key-type)
151-
`(N ~current-node)
152-
current-node)))))
136+
(let [path-parts (gensym 'path-parts)
137+
part (gensym 'part)
138+
parts (gensym 'parts)
139+
current-node (gensym 'current-node)
140+
index (gensym 'index)]
141+
`(let [~path-parts (str/split ~chain-path #"/")]
142+
(loop [~current-node (if (= "m" (first ~path-parts))
143+
(derive-master-node ~seed)
144+
(throw (Exception.
145+
(str "Invalid path: " (first ~path-parts)))))
146+
~parts (rest ~path-parts)]
147+
(if (seq ~parts)
148+
(let [~part (first ~parts)
149+
~index (if (hardened? ~part)
150+
(hardened (Integer/parseInt (subs ~part 0 (- (count ~part) 1))))
151+
(Integer/parseInt ~part))]
152+
(recur
153+
(CKDpriv ~current-node ~index)
154+
(rest ~parts)))
155+
(if (= :public ~key-type)
156+
(N ~current-node)
157+
~current-node))))))

src/bips/bip32_utils.clj

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -85,7 +85,7 @@
8585
(str (if (= 0 (mod (nth (codecs/hex->bytes (pad-leading-zeros 128 K)) 63) 2))
8686
"02"
8787
"03")
88-
(apply str (take 64 K))))
88+
(apply str (take 64 (pad-leading-zeros 128 K)))))
8989

9090
(defn decompressKey
9191
"Decompress a compressed public key (x co-ord and low-bit of y-coord).

src/bips/bip44.clj

Lines changed: 18 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -22,18 +22,24 @@
2222
[bips.constants :as const]))
2323

2424
(defn derivation-path
25-
"The derivation-path function takes four arguments: a `coin-type`, an
26-
`account`, a `chain`, and an `address`. It uses these values to
27-
construct a string representing a path to a specific wallet address.
28-
Throws an exception with an error message if the `coin-type` in not
29-
found in `coin_types.edn`."
30-
[coin-type account chain address]
31-
(if-let [matching-coin-type (first (filter #(= (:symbol %) coin-type)
32-
const/coin-types))]
33-
(str "m/44'/" (:coin-type matching-coin-type)
34-
"'/" account "'/" (get const/chain-map chain) "/" address)
35-
(throw (Exception. (str "Coin type " coin-type " not found in coin_types.edn file.")))))
25+
"The derivation-path multi-arity function. The first version takes
26+
four arguments: a `coin-type`, an `account` index, a `chain` type,
27+
and an `address` index. It uses these values to construct a string
28+
representing a path to a specific wallet address. Throws an
29+
exception with an error message if the `coin-type` in not found in
30+
`coin_types.edn`. A second version taking two arguments is used to
31+
derive an account address from a `coin-type` and an `account`
32+
index."
33+
([coin-type account chain address]
34+
(str (derivation-path coin-type account) "/" (get const/chain-map chain) "/" address))
35+
([coin-type account]
36+
(if-let [matching-coin-type (first (filter #(= (:symbol %) coin-type)
37+
const/coin-types))]
38+
(str "m/44'/" (:coin-type matching-coin-type)
39+
"'/" account "'")
40+
(throw (Exception. (str "Coin type " coin-type " not found in coin_types.edn file."))))))
3641

3742
(comment
3843
(derivation-path "BTC" 0 :external 0)
39-
(derivation-path "XMR" 0 :change 0))
44+
(derivation-path "XMR" 0 :change 0)
45+
(derivation-path "BTC" 0))

test/bips/bip32_test.clj

Lines changed: 14 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -78,8 +78,8 @@
7878
(doseq [i# (range (count (:derived-keys ~tv)))]
7979
(let [derived-key# (nth (:derived-keys ~tv) i#)
8080
path# (:path derived-key#)
81-
extended-private-key# (eval `(derive-path ~seed# ~path# :private))
82-
extended-public-key# (eval `(derive-path ~seed# ~path# :public))
81+
extended-private-key# (derive-path seed# path# :private)
82+
extended-public-key# (derive-path seed# path# :public)
8383
bip32-xprvkey# (:bip32-xprvkey derived-key#)
8484
bip32-xpubkey# (:bip32-xpubkey derived-key#)
8585
identifier# (:identifier derived-key#)
@@ -643,14 +643,22 @@
643643
;; Chain: m/44H/0H
644644
(is (= "xprv9vsUaQydQJbS21eDwfKv3WYoU9cr1JcFxt252mQneUB4ABBQScW5sx7ZjJ5HMGDGy1coR5Cvit6YWio3Nj4fSiBAfJFJMMWHNdBB3FEowpt"
645645
bip32-xprv-grandchild-private-key))
646-
(is (= "xpub69rpyvWXEg9jEVih3grvQeVY2BTLQmL7L6wfq9pQCoi32yWYz9pLRkS3aZhG1fvxPKjwWPUqoLFvTdGAjuUeNdYWRWFu9BdivDFrWHjwiVZ"
646+
(is (= "xpub69rpyvWXEg9jEVih3grvQeVY2BTLQmL7L6wfq9pQCoi32yWYz9pLRkS3aYXrkHLgUia7Fx9XkNJmYtdJM2HYPvXftMeL6KKaRCaemYiBBfZ"
647647
bip32-xpub-grandchild-public-key))
648-
(is (= "8809de7d" (format "%x" grandchild-node-fingerprint)))
648+
(is (= "6ebca719" (format "%x" grandchild-node-fingerprint)))
649649
;; Chain: m/44H/0H/0H
650-
(is (= "xprv9yfQqFhGvzYGeZvyLrdwHSreqxRQEXPjJzstFgGNwV6wT1ueWfU9Dyt4yBCZo45Zt5fEMTh3wEp8nyUnjsDMYHVNN9bmtcQZt6ouQxGCcGJ"
650+
(is (= "xprv9yUdDyYgknA92Cb4xfsqSXxQzGtELBm1kvXVvmp5MpW3UwjevPGEX29pjR9MAL13UTE1ZDfCwZ7Y3Uwpqv5BGP4cvdkS6DSTbvdYK7RicHk"
651651
bip32-xprv-grand-grandchild-private-key))
652-
(is (= "xpub6CemEmEAmN6Zs41SStAweaoPPzFtdz7agDoV44fzVpdvKpEo4CnPmnCYpTLNuT55NfJG6DDGWhcK7XA6xP1XrqfRtscmhcFSkbHNLRW6Wxt"
652+
(is (= "xpub6CTydV5ab9iSEgfY4hQqofu9YJiijeUs89T6jADgvA32Mk4oTvaV4pUJahHAGizYy2s3HyBRX1uiN2d94RsMawEgTMmRuDHLUR71EayS34i"
653653
bip32-xpub-grand-grandchild-public-key))))
654654

655+
(deftest threading-macro-works-with-derive-path
656+
(is (= (let [key "000102030405060708090a0b0c0d0e0f"
657+
path "m/0H/1/2H/2/1000000000"
658+
key-type :private]
659+
(-> key
660+
(derive-path path key-type)))
661+
(derive-path "000102030405060708090a0b0c0d0e0f" "m/0H/1/2H/2/1000000000" :private))))
662+
655663
(comment
656664
(clojure.test/run-all-tests))

0 commit comments

Comments
 (0)