From d31a1490537db439dd853269781955f6a0cdf78e Mon Sep 17 00:00:00 2001 From: Vinny Fiano Date: Mon, 26 Aug 2024 00:16:48 -0400 Subject: [PATCH] NIP-102: hierarchical deterministic subkeys --- 102.md | 96 +++++++++++++++++++++++++++++++++++++++++++++++++++++++ README.md | 1 + 2 files changed, 97 insertions(+) create mode 100644 102.md diff --git a/102.md b/102.md new file mode 100644 index 00000000..633da2a0 --- /dev/null +++ b/102.md @@ -0,0 +1,96 @@ +NIP-102 +======= + +Hierarchical Deterministic Key Management +----------------------------------------- + +`draft` `optional` + +This NIP defines a way to separate identity from authentication using hierarchical deterministic (HD) keys. This allows people to use one key pair to issue independent key pairs for different apps. If a key is compromised, the root key pair can publish an event revoking that key as of a specific time. + +HD keys (BIP-32, BIP-39, BIP-44) provide a means of creating new key pairs from a parent in such a way that the new pubkey can be verified to be a child of the parent's pubkey. A message signed by this new key can be rapidly and locally verified as an authentic subkey of the claimed parent. + +NIP-06 declares the default nostr derivation path to be `m/44'/1237'/'/0/0`. + +``` +recovery phrase -> seed -> xpriv + -> m/44'/1237'/0' -> Account0 + -> m/44'/1237'/0'/0/0 -> Account0 Subkey0 + -> m/44'/1237'/0'/1/0 -> Account0 Subkey1 + -> m/44'/1237'/0'/2/0 -> Account0 Subkey2 +``` + +## Management Event + +The parent key can publish a `Kind 10102` event for subkey management. This event lists subkeys that have been revoked, as well as those that are currently active, and a preference for how valid subkeys that are not listed should be treated. This is only a preference, as it may not always be immediately available to clients and relays. + +Content: +```json +{ + "keys": { + "": { "active_at": "" }, + "": { "active_at": "", "revoked_at": "" }, + "": { "active_at": "" } + }, + "default_policy": "allow" +} +``` + +## Signing + +When signing events using a subkey, the account pubkey and subkey derivation path will be included as a well known attribute. Relays and clients should validate the subkey->parent relationship, and immediately discard messages with invalid claims. + +```json +{ + "pubkey": "", + "kind": 1, + "tags": [ + ["account", "", ""], + ], +} +``` + +For clients that don't implement NIP-102, we can have the account key publish a `Kind 10102` key management event using the subkey at the time of creation: + +Content: +```json +{ + "account": "", + "derivation_path": "" +} +``` + +## Behavior + +All searches for the account pubkey should return those signed by any account subkey. If an event is replaceable, it is up to the client or relay as to whether both messages are maintained, or if only the latest across all subkeys is maintained. In any situation where data will be lost, a reasonable effort should be made to locate the most recent revocation list before proceeding. + +## Migration + +Most current keys do not use BIP-39 derivation and will need to perform a manual key rotation to a new pubkey using a method not defined here. + +## Reference Code + +```python +from bip_utils import Bip39SeedGenerator, Bip32Slip10Secp256k1 + +account0_derivation_path = "m/44'/1237'/0'" +subkey0_derivation_path = "0/0" + +mnemonic = "abandon abandon abandon abandon abandon abandon abandon abandon abandon abandon abandon about" +master_xpriv = "xprv9s21ZrQH143K3GJpoapnV8SFfukcVBSfeCficPSGfubmSFDxo1kuHnLisriDvSnRRuL2Qrg5ggqHKNVpxR86QEC8w35uxmGoggxtQTPvfUu" +account0_xpub = "xpub6D6V5EX8HTe95getx2tTH2QApmrA1nPJFEnneAK813RjcDdSc3WaAF7BRNpTF7o7zXjVm3DD3VMX66jhQ7wLaZ9sS6NzyfiwfzqDZbxvpDN" +subkey0_xpub = "xpub6Gf5o5yEF14TykSmvZBzS9wFSgnqvPsxit1v4CaaNf6S6S5mm169FRN3QkCsVsDm8NNaN8eGbQg9vR43BD9UqQTrfWFmRKoWep2gxQpFh3Q" + +seed = Bip32Slip10Secp256k1.FromSeed(Bip39SeedGenerator(mnemonic).Generate()) +account0 = seed.DerivePath(account0_derivation_path) +subkey0 = account0.DerivePath(subkey0_derivation_path) + +def is_subkey(pubkey, subkey, derivation_path): + parent = Bip32Slip10Secp256k1.FromExtendedKey(pubkey) + if parent.DerivePath(derivation_path).PublicKey().ToExtended() == subkey: + return True + else: + return False + +print(f'subkey0_xpub is child of account0_xpub: {is_subkey(account0_xpub, subkey0_xpub, subkey0_derivation_path)}') +``` diff --git a/README.md b/README.md index 80b8dcf2..9f456fa8 100644 --- a/README.md +++ b/README.md @@ -156,6 +156,7 @@ They exist to document what may be implemented by [Nostr](https://github.com/nos | `10030` | User emoji list | [51](51.md) | | `10050` | Relay list to receive DMs | [51](51.md), [17](17.md) | | `10096` | File storage server list | [96](96.md) | +| `10102` | Key management | [102](102.md) | | `13194` | Wallet Info | [47](47.md) | | `21000` | Lightning Pub RPC | [Lightning.Pub][lnpub] | | `22242` | Client Authentication | [42](42.md) |