Skip to content

Commit

Permalink
Support loading keys from Polkadot-JS accounts. (#1661)
Browse files Browse the repository at this point in the history
* Import key pair from JSON.

* Get secret.

* Fix JSON decryption.

* Fix error handling.

* Fix warnings.

* Add polkadot-js links.

* Fix packages.

* Fix json feature.

* Add copyright message.

* Update Cargo.toml

Co-authored-by: Niklas Adolfsson <niklasadolfsson1@gmail.com>

* Improve error handling.

* Expect that provided parameters are valid.

* Add Scrypt parameters comment from JS implementation.

* Fix expect() message

* Make from_ed25519_bytes() pub(crate)

* Rename json feature to polkadot-js-compat

* Add comment about polkadot-js-compat dependencies.

Co-authored-by: James Wilson <james@jsdw.me>

* Add decrypt_json() public method.

* json.rs -> polkadot_js_compat.rs

* Simplify JSON structs.

* Only declare from_ed25519_bytes() with polkadot-js-compat

* Move decrypt_json() to top of file.

* Don't enable new crates on std feature

* Avoid enabling a couple of existing optional crates on std feature

---------

Co-authored-by: Jonathan Brown <jbrown@acuity.network>
Co-authored-by: Niklas Adolfsson <niklasadolfsson1@gmail.com>
Co-authored-by: James Wilson <james@jsdw.me>
  • Loading branch information
4 people committed Aug 23, 2024
1 parent a8a700c commit c7ccc58
Show file tree
Hide file tree
Showing 6 changed files with 304 additions and 9 deletions.
55 changes: 49 additions & 6 deletions Cargo.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

3 changes: 3 additions & 0 deletions Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -162,6 +162,9 @@ keccak-hash = { version = "0.10.0", default-features = false }
secrecy = "0.8.0"
sha2 = { version = "0.10.8", default-features = false }
zeroize = { version = "1", default-features = false }
base64 = { version = "0.22.1", default-features = false }
scrypt = { version = "0.11.0", default-features = false }
crypto_secretbox = { version = "0.1.1", default-features = false }

[profile.dev.package.smoldot-light]
opt-level = 2
Expand Down
21 changes: 18 additions & 3 deletions signer/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -23,8 +23,13 @@ std = [
"sha2/std",
"hmac/std",
"bip39/std",
"schnorrkel/std",
"secp256k1/std",
"schnorrkel?/std",
"secp256k1?/std",
"serde?/std",
"serde_json?/std",
"base64?/std",
"scrypt?/std",
"crypto_secretbox?/std",
]

# Pick the signer implementation(s) you need by enabling the
Expand All @@ -35,6 +40,9 @@ sr25519 = ["schnorrkel"]
ecdsa = ["secp256k1"]
unstable-eth = ["keccak-hash", "ecdsa", "secp256k1", "bip32"]

# Enable support for loading key pairs from polkadot-js json.
polkadot-js-compat = ["std", "subxt", "sr25519", "base64", "scrypt", "crypto_secretbox", "serde", "serde_json"]

# Make the keypair algorithms here compatible with Subxt's Signer trait,
# so that they can be used to sign transactions for compatible chains.
subxt = ["dep:subxt-core"]
Expand Down Expand Up @@ -66,6 +74,13 @@ secp256k1 = { workspace = true, optional = true, features = [
] }
keccak-hash = { workspace = true, optional = true }

# These are used if the polkadot-js-compat feature is enabled
serde = { workspace = true, optional = true }
serde_json = { workspace = true, optional = true }
base64 = { workspace = true, optional = true, features = ["alloc"] }
scrypt = { workspace = true, default-features = false, optional = true }
crypto_secretbox = { workspace = true, optional = true, features = ["alloc", "salsa20"] }

# We only pull this in to enable the JS flag for schnorrkel to use.
getrandom = { workspace = true, optional = true }

Expand All @@ -86,4 +101,4 @@ rustdoc-args = ["--cfg", "docsrs"]
defalt-features = true

[lints]
workspace = true
workspace = true
5 changes: 5 additions & 0 deletions signer/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -37,6 +37,11 @@ pub mod ecdsa;
#[cfg_attr(docsrs, doc(cfg(feature = "unstable-eth")))]
pub mod eth;

/// A polkadot-js account json loader.
#[cfg(feature = "polkadot-js-compat")]
#[cfg_attr(docsrs, doc(cfg(feature = "polkadot-js-compat")))]
pub mod polkadot_js_compat;

// Re-export useful bits and pieces for generating a Pair from a phrase,
// namely the Mnemonic struct.
pub use bip39;
Expand Down
Loading

0 comments on commit c7ccc58

Please sign in to comment.