Skip to content

Commit 6d95044

Browse files
committed
npm: allow the keypath to be a sequence of numbers too
The native representation of a bip32 keypath is a list of numbers, so we should allow it for keypaths in addition to the "m/..." strings.
1 parent 577a67c commit 6d95044

File tree

3 files changed

+31
-4
lines changed

3 files changed

+31
-4
lines changed

NPM_VERSION

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1 +1 @@
1-
0.3.2
1+
0.4.0

src/keypath.rs

Lines changed: 29 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -78,8 +78,35 @@ impl<'de> serde::Deserialize<'de> for Keypath {
7878
where
7979
D: serde::Deserializer<'de>,
8080
{
81-
let s = String::deserialize(deserializer)?;
82-
s.as_str().try_into().map_err(serde::de::Error::custom)
81+
struct KeypathVisitor;
82+
83+
impl<'de> serde::de::Visitor<'de> for KeypathVisitor {
84+
type Value = Keypath;
85+
86+
fn expecting(&self, formatter: &mut std::fmt::Formatter) -> std::fmt::Result {
87+
formatter.write_str("a string or a number sequence")
88+
}
89+
90+
fn visit_str<E>(self, value: &str) -> Result<Self::Value, E>
91+
where
92+
E: serde::de::Error,
93+
{
94+
value.try_into().map_err(serde::de::Error::custom)
95+
}
96+
97+
fn visit_seq<A>(self, mut seq: A) -> Result<Self::Value, A::Error>
98+
where
99+
A: serde::de::SeqAccess<'de>,
100+
{
101+
let mut vec = Vec::<u32>::new();
102+
while let Some(elem) = seq.next_element()? {
103+
vec.push(elem);
104+
}
105+
Ok(Keypath(vec))
106+
}
107+
}
108+
109+
deserializer.deserialize_any(KeypathVisitor)
83110
}
84111
}
85112

src/wasm/types.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -15,7 +15,7 @@ type Product = 'unknown' | 'bitbox02-multi' | 'bitbox02-btconly';
1515
type BtcCoin = 'btc' | 'tbtc' | 'ltc' | 'tltc';
1616
type BtcFormatUnit = 'default' | 'sat';
1717
type XPubType = 'tpub' | 'xpub' | 'ypub' | 'zpub' | 'vpub' | 'upub' | 'Vpub' | 'Zpub' | 'Upub' | 'Ypub';
18-
type Keypath = string;
18+
type Keypath = string | number[];
1919
type XPub = string;
2020
type DeviceInfo = {
2121
name: string;

0 commit comments

Comments
 (0)