-
-
Notifications
You must be signed in to change notification settings - Fork 671
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
21 changed files
with
1,289 additions
and
367 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,35 @@ | ||
syntax = "proto2"; | ||
package hw.trezor.messages.nostr; | ||
|
||
// Sugar for easier handling in Java | ||
option java_package = "com.satoshilabs.trezor.lib.protobuf"; | ||
option java_outer_classname = "TrezorMessageNostr"; | ||
|
||
import "options.proto"; | ||
|
||
option (include_in_bitcoin_only) = true; | ||
|
||
|
||
/** | ||
* Request: Ask device to sign event | ||
* @start | ||
* @next NostrMessageSignature | ||
* @next Failure | ||
*/ | ||
message NostrSignEvent { | ||
repeated uint32 address_n = 1; // used to derive the key | ||
optional uint32 created_at = 2; | ||
optional uint32 kind = 3; | ||
repeated string tags = 4; | ||
optional string content = 5; | ||
} | ||
|
||
/** | ||
* Response: Computed event ID and signature | ||
* @end | ||
*/ | ||
message NostrEventSignature { | ||
required bytes pubkey = 1; // pubkey used to sign the event | ||
required bytes id = 2; // ID of the event | ||
required bytes signature = 3; // signature of the event | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,5 @@ | ||
from apps.common.paths import PATTERN_BIP44 | ||
|
||
CURVE = "secp256k1" | ||
SLIP44_ID = 1237 | ||
PATTERN = PATTERN_BIP44 |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,34 @@ | ||
from typing import TYPE_CHECKING | ||
|
||
from apps.common.keychain import auto_keychain | ||
|
||
from ..bitcoin.common import encode_bech32_address | ||
|
||
if TYPE_CHECKING: | ||
from trezor.messages import NostrEventSignature, NostrSignEvent | ||
from apps.common.keychain import Keychain | ||
|
||
|
||
@auto_keychain(__name__) | ||
async def sign_event( | ||
msg: NostrSignEvent, keychain: Keychain | ||
) -> NostrEventSignature: | ||
from trezor.crypto.curve import bip340 | ||
from trezor.crypto.hashlib import sha256 | ||
from trezor.messages import NostrEventSignature | ||
|
||
address_n = msg.address_n | ||
created_at = msg.created_at | ||
kind = msg.kind | ||
tags = msg.tags | ||
content = msg.content | ||
|
||
node = keychain.derive(address_n) | ||
pk = node.public_key() | ||
sk = node.private_key() | ||
|
||
serialized_event = f"[0,\"{pk}\",\"{created_at}\",{kind},{tags},\"{content}\"]" | ||
event_id = sha256(serialized_event).digest() | ||
signature = bip340.sign(sk, event_id) | ||
|
||
return NostrEventSignature(pubkey=pk, id=event_id, signature=signature) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,59 @@ | ||
# This file is part of the Trezor project. | ||
# | ||
# Copyright (C) 2012-2024 SatoshiLabs and contributors | ||
# | ||
# This library is free software: you can redistribute it and/or modify | ||
# it under the terms of the GNU Lesser General Public License version 3 | ||
# as published by the Free Software Foundation. | ||
# | ||
# This library is distributed in the hope that it will be useful, | ||
# but WITHOUT ANY WARRANTY; without even the implied warranty of | ||
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the | ||
# GNU Lesser General Public License for more details. | ||
# | ||
# You should have received a copy of the License along with this library. | ||
# If not, see <https://www.gnu.org/licenses/lgpl-3.0.html>. | ||
|
||
from typing import TYPE_CHECKING, Dict | ||
|
||
import click | ||
import json | ||
|
||
from .. import nostr, tools | ||
from . import with_client | ||
|
||
if TYPE_CHECKING: | ||
from ..client import TrezorClient | ||
|
||
@click.group(name="nostr") | ||
def cli() -> None: | ||
pass | ||
|
||
@cli.command() | ||
@click.option("-n", "--address", required=True, help="BIP-32 path") | ||
@click.argument("event") | ||
@with_client | ||
def sign_event( | ||
client: "TrezorClient", | ||
address: str, | ||
event: str, | ||
) -> Dict[str, str]: | ||
"""Sign an event using address of given path.""" | ||
|
||
event_json = json.loads(event) | ||
|
||
address_n = tools.parse_path(address) | ||
|
||
res = nostr.sign_event( | ||
client, | ||
address_n, | ||
event, | ||
) | ||
|
||
event_json['id'] = res.id.hex() | ||
event_json['pubkey'] = res.pubkey.hex() | ||
event_json['sig'] = res.signature.hex() | ||
|
||
return { | ||
"signed_event": json.dumps(event_json), | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.
Oops, something went wrong.