Skip to content
Open
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
13 changes: 12 additions & 1 deletion poetry.lock

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

1 change: 1 addition & 0 deletions pyproject.toml
Original file line number Diff line number Diff line change
Expand Up @@ -32,6 +32,7 @@ click = "~8.1.3"
base58 = "~2.1.1"
build = "~1.2.1"
ecdsa = "~0.19.0"
bech32 = "^1.2.0"

[tool.poetry.group.dev.dependencies]
black = "~24.4.2"
Expand Down
2 changes: 2 additions & 0 deletions src/bipsea/apps/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@
from bipsea.apps.dice.app import app as dice_app
from bipsea.apps.hex.app import app as hex_app
from bipsea.apps.mnemonic.app import app as mnemonic_app
from bipsea.apps.nostr.app import app as nostr_app
from bipsea.apps.wif.app import app as wif_app
from bipsea.apps.xprv.app import app as xprv_app

Expand All @@ -17,6 +18,7 @@
dice_app.name: dice_app,
hex_app.name: hex_app,
mnemonic_app.name: mnemonic_app,
nostr_app.name: nostr_app,
wif_app.name: wif_app,
xprv_app.name: xprv_app,
}
Empty file.
71 changes: 71 additions & 0 deletions src/bipsea/apps/nostr/app.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,71 @@
from typing import Any

from bech32 import bech32_encode, convertbits

from bipsea.app_protocol import Param, TestVector
from bipsea.apps.shared import hardened_int


def nsec_encode(key_bytes: bytes) -> str:
data = convertbits(key_bytes, 8, 5)
return bech32_encode("nsec", data)


class NostrApp:
name = "nostr"
code = "9000'"

@property
def params(self) -> list[Param]:
return [
Param(
"identity",
("--identity",),
int,
required=True,
range=(0, None),
help="Identity index (0=proof/revocation key, >=1 usable).",
),
]

def path_segments(self, index: int, identity: int, **_) -> list[str]:
return [f"{identity}'", f"{index}'"]

def parse_path(self, segments: list[str]) -> dict[str, Any]:
return {
"identity": hardened_int(segments[0]),
"index": hardened_int(segments[1]),
}

def apply(self, entropy: bytes, **_) -> dict[str, Any]:
key = entropy[:32]
return {
"entropy": key,
"application": nsec_encode(key),
}

@property
def vectors(self) -> list[TestVector]:
return [
TestVector(
master="xprv9s21ZrQH143K2LBWUUQRFXhucrQqBpKdRRxNVq2zBqsx8HVqFk2uYo8kmbaLLHRdqtQpUm98uKfu3vca1LqdGhUtyoFnCNkfmXRyPXLjbKb",
path="m/83696968'/9000'/1'/1'",
entropy="552ad1d578fe1bc927cec9612651652b07c52dde4017911bc23bc953568075ff",
output="nsec1254dr4tclcdujf7we9sjv5t99vru2tw7gqtezx7z80y4x45qwhlsmxapst",
),
TestVector(
master="xprv9s21ZrQH143K2LBWUUQRFXhucrQqBpKdRRxNVq2zBqsx8HVqFk2uYo8kmbaLLHRdqtQpUm98uKfu3vca1LqdGhUtyoFnCNkfmXRyPXLjbKb",
path="m/83696968'/9000'/1'/2'",
entropy="4fd36c0061a65db375b4350f44bb62a6d7f716ee93bd0f59887ac50b35fa8b96",
output="nsec1flfkcqrp5ewmxad5x585fwmz5mtlw9hwjw7s7kvg0tzskd063wtq34wlgr",
),
TestVector(
master="xprv9s21ZrQH143K2LBWUUQRFXhucrQqBpKdRRxNVq2zBqsx8HVqFk2uYo8kmbaLLHRdqtQpUm98uKfu3vca1LqdGhUtyoFnCNkfmXRyPXLjbKb",
path="m/83696968'/9000'/2'/1'",
entropy="b2d3b48992d46f98beac0196c4e258417087e467dbec1503342785368f4402c2",
output="nsec1ktfmfzvj63he304vqxtvfcjcg9cg0er8m0kp2qe5y7zndr6yqtpq7q5y44",
),
]


app = NostrApp()
26 changes: 25 additions & 1 deletion src/bipsea/bipsea.py
Original file line number Diff line number Diff line change
Expand Up @@ -205,7 +205,13 @@ def xprv(mnemonic, passphrase, mainnet):
type=click.Choice(ENTROPY_TO_VALUES),
help="Output language for `--application mnemonic`.",
)
def derive_cli(application, number, index, special, xprv, to):
@click.option(
"--identity",
type=click.IntRange(0, 2**31 - 1),
default=None,
help="Nostr identity index (0=proof/revocation key, >=1 usable). Required for --application nostr.",
)
def derive_cli(application, number, index, special, xprv, to, identity):
if xprv:
xprv = xprv.strip()
else:
Expand Down Expand Up @@ -253,6 +259,24 @@ def derive_cli(application, number, index, special, xprv, to):
elif application == "dice":
check_range(number, application)
path += f"/{special}'/{number}'/{index}'"
elif application == "nostr":
if identity is None:
raise click.UsageError(
"--identity is required for --application nostr."
)
if identity == 0:
click.secho(
"Warning: identity=0 is reserved as a proof key to link identities together.",
fg="yellow",
err=True,
)
if index == 0:
click.secho(
f"Warning: index=0 is reserved as the proof key to link accounts for identity {identity}.",
fg="yellow",
err=True,
)
path += f"/{identity}'/{index}'"

derived = derive(master, path)
if application == "drng":
Expand Down
55 changes: 55 additions & 0 deletions tests/test_cli.py
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@
from bipsea.apps.dice.app import app as dice_app
from bipsea.apps.hex.app import app as hex_app
from bipsea.apps.mnemonic.app import app as mnemonic_app
from bipsea.apps.nostr.app import app as nostr_app
from bipsea.apps.wif.app import app as wif_app
from bipsea.bip32types import validate_prv_str
from bipsea.bip39 import LANGUAGES, validate_mnemonic_words
Expand Down Expand Up @@ -449,6 +450,60 @@ def test_commands(self, group, commands):
Path(script.name).unlink()


class TestNostr:
def test_path_segments(self):
assert nostr_app.path_segments(index=2, identity=1) == ["1'", "2'"]

@pytest.mark.parametrize("vector", nostr_app.vectors)
def test_vectors(self, runner, vector):
segments = vector.path.split("/")
identity = int(segments[3].rstrip("'"))
index = int(segments[4].rstrip("'"))
result = runner.invoke(
cli,
[
"derive",
"-a", "nostr",
"-x", vector.master,
"--identity", identity,
"--index", index,
],
)
assert result.exit_code == 0
assert result.output.strip() == vector.output

def test_missing_identity(self, runner):
result = runner.invoke(
cli, ["derive", "-a", "nostr", "-x", nostr_app.vectors[0].master]
)
assert result.exit_code != 0
assert "identity" in result.output

def test_identity_zero_warning(self, runner):
result = runner.invoke(
cli,
[
"derive", "-a", "nostr",
"-x", nostr_app.vectors[0].master,
"--identity", 0,
"--index", 1,
],
)
assert "Warning" in result.output

def test_index_zero_warning(self, runner):
result = runner.invoke(
cli,
[
"derive", "-a", "nostr",
"-x", nostr_app.vectors[0].master,
"--identity", 1,
"--index", 0,
],
)
assert "Warning" in result.output


class TestCliAdapter:
def test_required_param(self):
param = Param("length", ("-n", "--length"), int, required=True, help="Length")
Expand Down