From 86cea2ed7d4591f5e3b85a490e6af003d9a2838f Mon Sep 17 00:00:00 2001 From: rooooooooob Date: Tue, 2 Nov 2021 22:27:31 -0500 Subject: [PATCH 1/2] Added PrivateKey::from_bech32() This method existed already in js-chain-libs where we got this class from to begin wtih. It also exists in the other 3 keys types: PublicKey, Bip32PublicKey and Bip32PrivateKey so it makes sense to add here. --- rust/src/crypto.rs | 19 +++++++++++++++++++ 1 file changed, 19 insertions(+) diff --git a/rust/src/crypto.rs b/rust/src/crypto.rs index 5f8cdff4..95a3f1e7 100644 --- a/rust/src/crypto.rs +++ b/rust/src/crypto.rs @@ -229,6 +229,25 @@ impl PrivateKey { .map_err(|e| JsError::from_str(&format!("{}", e))) } + /// Get private key from its bech32 representation + /// ```javascript + /// PrivateKey.from_bech32('ed25519_sk1ahfetf02qwwg4dkq7mgp4a25lx5vh9920cr5wnxmpzz9906qvm8qwvlts0'); + /// ``` + /// For an extended 25519 key + /// ```javascript + /// PrivateKey.from_bech32('ed25519e_sk1gqwl4szuwwh6d0yk3nsqcc6xxc3fpvjlevgwvt60df59v8zd8f8prazt8ln3lmz096ux3xvhhvm3ca9wj2yctdh3pnw0szrma07rt5gl748fp'); + /// ``` + pub fn from_bech32(bech32_str: &str) -> Result { + crypto::SecretKey::try_from_bech32_str(&bech32_str) + .map(key::EitherEd25519SecretKey::Extended) + .or_else(|_| { + crypto::SecretKey::try_from_bech32_str(&bech32_str) + .map(key::EitherEd25519SecretKey::Normal) + }) + .map(PrivateKey) + .map_err(|_| JsError::from_str("Invalid secret key")) + } + pub fn to_bech32(&self) -> String { match self.0 { key::EitherEd25519SecretKey::Normal(ref secret) => secret.to_bech32_str(), From ab2506090d35980a5a41f159da22a298d9f2cf7f Mon Sep 17 00:00:00 2001 From: rooooooooob Date: Tue, 2 Nov 2021 22:32:51 -0500 Subject: [PATCH 2/2] update flow types --- rust/pkg/cardano_serialization_lib.js.flow | 6 ++++++ 1 file changed, 6 insertions(+) diff --git a/rust/pkg/cardano_serialization_lib.js.flow b/rust/pkg/cardano_serialization_lib.js.flow index 69c7aad8..4465885c 100644 --- a/rust/pkg/cardano_serialization_lib.js.flow +++ b/rust/pkg/cardano_serialization_lib.js.flow @@ -3381,6 +3381,12 @@ declare export class PrivateKey { */ static generate_ed25519extended(): PrivateKey; + /** + * @param {string} bech_str + * @returns {PrivateKey} + */ + static from_bech32(bech_str: string): PrivateKey; + /** * @returns {string} */