Skip to content

Commit

Permalink
fixup! feat(core): add nostr
Browse files Browse the repository at this point in the history
  • Loading branch information
ibz committed Jan 7, 2025
1 parent 14a8513 commit af4f669
Show file tree
Hide file tree
Showing 2 changed files with 18 additions and 8 deletions.
20 changes: 12 additions & 8 deletions common/protob/messages-nostr.proto
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@ import "options.proto";
/**
* Request: Ask the device for the Nostr public key
* @start
* @next NostrMessageSignature
* @next NostrPubkey
*/
message NostrGetPubkey {
repeated uint32 address_n = 1; // used to derive the key
Expand All @@ -31,24 +31,28 @@ message NostrTag {
// Nostr tags consist of at least one string (the key)
// followed by an arbitrary number of strings,
// the first of which (if present) is called "value".
// See: NIP-01 https://github.com/nostr-protocol/nips/blob/master/01.md#tags
// See NIP-01: https://github.com/nostr-protocol/nips/blob/master/01.md#tags
required string key = 1;
optional string value = 2;
repeated string extra = 3;
}

/**
* Request: Ask device to sign event
* Request: Ask device to sign an event
* @start
* @next NostrEventSignature
* @next Failure
*/
message NostrSignEvent {
repeated uint32 address_n = 1; // used to derive the key
required uint32 created_at = 2;
required uint32 kind = 3;
repeated NostrTag tags = 4;
required string content = 5;
repeated uint32 address_n = 1; // used to derive the key

// Nostr event fields, except the ones that are calculated by the signer
// See NIP-01: https://github.com/nostr-protocol/nips/blob/master/01.md

required uint32 created_at = 2; // Event created_at: unix timestamp in seconds
required uint32 kind = 3; // Event kind: integer between 0 and 65535
repeated NostrTag tags = 4; // Event tags
required string content = 5; // Event content: arbitrary string
}

/**
Expand Down
6 changes: 6 additions & 0 deletions core/src/apps/nostr/sign_event.py
Original file line number Diff line number Diff line change
Expand Up @@ -44,11 +44,17 @@ async def sign_event(msg: NostrSignEvent, keychain: Keychain) -> NostrEventSigna
await confirm_value(
title, content, "", "nostr_sign_event", info_items=[("", tags_str)]
)

# The event ID is obtained by serializing the event in a specific way:
# "[0,pubkey,created_at,kind,tags,content]"
# See NIP-01: https://github.com/nostr-protocol/nips/blob/master/01.md
serialized_tags = ",".join(
["[" + ",".join(f'"{t}"' for t in tag) + "]" for tag in tags]
)
serialized_event = f'[0,"{hexlify(pk).decode()}",{created_at},{kind},[{serialized_tags}],"{content}"]'
event_id = sha256(serialized_event).digest()

# The event signature is basically the signature of the event ID computed above
signature = secp256k1.sign(sk, event_id)[-64:]

return NostrEventSignature(pubkey=pk, id=event_id, signature=signature)

0 comments on commit af4f669

Please sign in to comment.