Skip to content

Commit fdc178e

Browse files
committed
Add support for seed phrase
1 parent 7ef67a3 commit fdc178e

3 files changed

Lines changed: 14 additions & 6 deletions

File tree

pyproject.toml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -92,5 +92,5 @@ testpaths = [
9292
"tests",
9393
]
9494

95-
[tool.ruff.per-file-ignores]
95+
[tool.ruff.lint.per-file-ignores]
9696
"**/__init__.py" = ["F403", "F405"]

src/soroban/models.py

Lines changed: 10 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -27,6 +27,7 @@ def _load_configuration(id: str | pathlib.Path, kind: Literal["identity", "netwo
2727

2828

2929
class Identity(BaseSettings):
30+
seed_phrase: str | None = None
3031
secret_key: str | None = None
3132
public_key: str | None = None
3233
keypair: Keypair | None = None
@@ -35,15 +36,21 @@ class Identity(BaseSettings):
3536

3637
@model_validator(mode="after")
3738
def load_keys(self) -> "Identity":
38-
if self.keypair is None and self.secret_key is None:
39+
if (
40+
self.keypair is None
41+
and self.secret_key is None
42+
and self.seed_phrase is None
43+
):
3944
raise ValueError(
40-
"Either provide a secret key or a Keypair object. Also look"
45+
"Either provide a seed phrase, secret key or a Keypair object. Also look"
4146
"in 'identity.toml'"
4247
)
4348
if self.keypair is not None:
4449
self.secret_key = self.keypair.secret
45-
else:
50+
elif self.secret_key is not None:
4651
self.keypair = Keypair.from_secret(self.secret_key)
52+
else:
53+
self.keypair = Keypair.from_mnemonic_phrase(self.seed_phrase)
4754
self.public_key = self.keypair.public_key
4855
return self
4956

tests/test_models.py

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -31,11 +31,12 @@ def test_from_source_account(self):
3131

3232
def test_raises(self):
3333
os.chdir(pathlib.Path(__file__).parent.parent)
34-
with pytest.raises(ValueError, match="provide a secret key or a Keypair"):
34+
msg = "Either provide a seed phrase, secret key"
35+
with pytest.raises(ValueError, match=msg):
3536
soroban.Identity()
3637

3738
keypair = Keypair.random()
38-
with pytest.raises(ValueError, match="provide a secret key or a Keypair"):
39+
with pytest.raises(ValueError, match=msg):
3940
soroban.Identity(public_key=keypair.public_key)
4041

4142

0 commit comments

Comments
 (0)