MCPcopy Create free account
hub / github.com/ElementsProject/lightning / test_hsmtool_deterministic_node_ids

Function test_hsmtool_deterministic_node_ids

tests/test_wallet.py:1692–1748  ·  view source on GitHub ↗

Test that HSM daemon creates deterministic node IDs in new mnemonic format

(node_factory)

Source from the content-addressed store, hash-verified

1690
1691
1692def test_hsmtool_deterministic_node_ids(node_factory):
1693 """Test that HSM daemon creates deterministic node IDs in new mnemonic format"""
1694 # Create a node and start it to trigger HSM daemon to create new format
1695 l1 = node_factory.get_node(start=False)
1696 hsm_path = os.path.join(l1.daemon.lightning_dir, TEST_NETWORK, "hsm_secret")
1697
1698 # Delete any existing hsm_secret so HSM daemon creates it in new format
1699 if os.path.exists(hsm_path):
1700 os.remove(hsm_path)
1701
1702 # Start the node to get its node ID (this will create a new hsm_secret in new format)
1703 l1.start()
1704 normal_node_id = l1.rpc.getinfo()['id']
1705 l1.stop()
1706
1707 # Verify the hsm_secret was created in the new mnemonic format
1708 with open(hsm_path, 'rb') as f:
1709 content = f.read()
1710 # Should be longer than 32 bytes (new format has 32-byte hash + mnemonic)
1711 assert len(content) > 32, f"Expected new mnemonic format, got {len(content)} bytes"
1712
1713 # First 32 bytes should be the passphrase hash (likely zeros for no passphrase)
1714 passphrase_hash = content[:32]
1715 assert passphrase_hash == b'\x00' * 32
1716 mnemonic_bytes = content[32:]
1717
1718 # Decode the mnemonic bytes
1719 mnemonic = mnemonic_bytes.decode('utf-8').strip()
1720
1721 # Verify it's a valid mnemonic (should be 12 words)
1722 words = mnemonic.split()
1723 assert len(words) == 12, f"Expected 12 words, got {len(words)}: {mnemonic}"
1724
1725 # Create a second node and use generatehsm with the mnemonic from the first node
1726 l2 = node_factory.get_node(start=False)
1727 hsm_path2 = os.path.join(l2.daemon.lightning_dir, TEST_NETWORK, "hsm_secret")
1728
1729 # Delete any existing hsm_secret for the second node
1730 if os.path.exists(hsm_path2):
1731 os.remove(hsm_path2)
1732
1733 # Generate hsm_secret with the mnemonic from the first node
1734 hsmtool = HsmTool(node_factory.directory, "generatehsm", hsm_path2)
1735 master_fd, slave_fd = os.openpty()
1736 hsmtool.start(stdin=slave_fd)
1737 hsmtool.wait_for_log(r"Introduce your BIP39 word list")
1738 write_all(master_fd, f"{mnemonic}\n".encode("utf-8"))
1739 hsmtool.wait_for_log(r"Enter your passphrase:")
1740 write_all(master_fd, "\n".encode("utf-8"))
1741 assert hsmtool.proc.wait(WAIT_TIMEOUT) == 0
1742
1743 # Get the node ID from the generated hsm_secret
1744 cmd_line = ["tools/lightning-hsmtool", "getnodeid", hsm_path2]
1745 generated_node_id = subprocess.check_output(cmd_line).decode("utf8").strip()
1746
1747 # Verify both node IDs are identical
1748 assert normal_node_id == generated_node_id, f"Node IDs don't match: {normal_node_id} != {generated_node_id}"
1749

Callers

nothing calls this directly

Calls 13

HsmToolClass · 0.85
joinMethod · 0.80
wait_for_logMethod · 0.80
write_allFunction · 0.70
get_nodeMethod · 0.45
removeMethod · 0.45
startMethod · 0.45
getinfoMethod · 0.45
stopMethod · 0.45
readMethod · 0.45
decodeMethod · 0.45
encodeMethod · 0.45

Tested by

no test coverage detected