Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Added HD Wallet Support (from_seed_and_derivation_path) #72

Closed
wants to merge 3 commits into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
36 changes: 32 additions & 4 deletions crates/keypair/src/lib.rs
Original file line number Diff line number Diff line change
@@ -1,11 +1,15 @@
use derive_more::{From, Into};
use pyo3::{prelude::*, types::PyBytes};
use serde::{Deserialize, Serialize};
use solana_sdk::signer::{
keypair::{
keypair_from_seed, keypair_from_seed_phrase_and_passphrase, Keypair as KeypairOriginal,
use solana_sdk::{
derivation_path::DerivationPath,
signature::keypair_from_seed_and_derivation_path,
signer::{
keypair::{
keypair_from_seed, keypair_from_seed_phrase_and_passphrase, Keypair as KeypairOriginal,
},
Signer as SignerTrait,
},
Signer as SignerTrait,
};
use solders_macros::{common_methods, pyhash, richcmp_signer};
use solders_pubkey::Pubkey;
Expand Down Expand Up @@ -193,6 +197,30 @@
handle_py_value_err(keypair_from_seed(&seed))
}

#[staticmethod]
/// Generate a keypair from a 32-byte seed and derivation path..
///
/// Args:
/// seed (bytes): 32-byte seed.
/// dpath (string): derivation path.
/// Returns:
/// Keypair: The generated keypair.
///
/// Example:
/// >>> from solders.keypair import Keypair
/// >>> from solders.pubkey import Pubkey
/// >>> seed_bytes = bytes([0] * 64)
/// >>> account_index = 0
/// >>> derivation_path = f"m/44'/501'/0'/{account_index}'"
/// >>> from_seed = Keypair.from_seed_and_derivation_path(seed_bytes, derivation_path)

pub fn from_seed_and_derivation_path(seed: [u8; 64], dpath: &str) -> PyResult<Self> {
handle_py_value_err(keypair_from_seed_and_derivation_path(
&seed,
Some(DerivationPath::from_key_str(&dpath).unwrap()),

Check warning on line 220 in crates/keypair/src/lib.rs

View workflow job for this annotation

GitHub Actions / lint

this expression creates a reference which is immediately dereferenced by the compiler

Check warning on line 220 in crates/keypair/src/lib.rs

View workflow job for this annotation

GitHub Actions / lint

this expression creates a reference which is immediately dereferenced by the compiler
))
}

#[staticmethod]
/// Generate a keypair from a seed phrase and passphrase.
///
Expand Down
2 changes: 2 additions & 0 deletions python/solders/keypair.pyi
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,8 @@ class Keypair:
@staticmethod
def from_seed(seed: Union[bytes, Sequence[int]]) -> "Keypair": ...
@staticmethod
def from_seed_and_derivation_path(seed: Union[bytes, Sequence[int]], dpath: str) -> "Keypair": ...
@staticmethod
def from_base58_string(s: str) -> "Keypair": ...
@staticmethod
def from_seed_phrase_and_passphrase(
Expand Down
10 changes: 10 additions & 0 deletions tests/test_keypair.py
Original file line number Diff line number Diff line change
Expand Up @@ -5,8 +5,17 @@
from pybip39 import Mnemonic, Seed
from pytest import mark, raises
from solders.keypair import Keypair
from mnemonic import Mnemonic


def test_from_seed_and_derivation_path():
mnemo = Mnemonic("english")
seed = mnemo.to_seed("pill tomorrow foster begin walnut borrow virtual kick shift mutual shoe scatter")

first_wallet = Keypair.from_seed_and_derivation_path(seed, "m/44'/501'/0'/0'").pubkey()
assert "5F86TNSTre3CYwZd1wELsGQGhqG2HkN3d8zxhbyBSnzm" == first_wallet


def test_from_bytes() -> None:
raw_bytes = (
b"\x99\xda\x95Y\xe1^\x91>\xe9\xab.S\xe3\xdf\xadW]\xa3;I\xbe\x11%\xbb\x92.3IOI"
Expand Down Expand Up @@ -115,3 +124,4 @@ def test_pickle() -> None:
def test_json() -> None:
obj = Keypair()
assert Keypair.from_json(obj.to_json()) == obj

Loading