@@ -1629,38 +1629,65 @@ def test_hsmtool_generatehsm_file_exists_error(node_factory):
16291629 hsmtool .is_in_log (r"hsm_secret file.*already exists" )
16301630
16311631
1632- def test_hsmtool_all_commands_work_with_mnemonic_formats (node_factory ):
1632+ @pytest .mark .parametrize ("test_case" , [
1633+ pytest .param ({
1634+ "mnemonic" : "abandon abandon abandon abandon abandon abandon abandon abandon abandon abandon abandon about" ,
1635+ "passphrase" : "" ,
1636+ "test_commands" : [
1637+ ("getnodeid" , "03653e90c1ce4660fd8505dd6d643356e93cfe202af109d382787639dd5890e87d" ),
1638+ ("getsecret" , "abandon abandon abandon abandon abandon abandon abandon abandon abandon abandon abandon about" ),
1639+ ("makerune" , "6VkrWMI2hm2a2UTkg-EyUrrBJN0RcuPB80I1pCVkTD89MA==" ),
1640+ ("dumponchaindescriptors" ,
1641+ "wpkh(xpub6BgBgsespWvERF3LHQu6CnqdvfEvtMcQjYrcRzx53QJjSxarj2afYWcLteoGVky7D3UKDP9QyrLprQ3VCECoY49yfdDEHGCtMMj92pReUsQ/0/*)#hjszq0wk\n "
1642+ "sh(wpkh(xpub6BgBgsespWvERF3LHQu6CnqdvfEvtMcQjYrcRzx53QJjSxarj2afYWcLteoGVky7D3UKDP9QyrLprQ3VCECoY49yfdDEHGCtMMj92pReUsQ/0/*))#u0t3u3xz\n "
1643+ "tr(xpub6BgBgsespWvERF3LHQu6CnqdvfEvtMcQjYrcRzx53QJjSxarj2afYWcLteoGVky7D3UKDP9QyrLprQ3VCECoY49yfdDEHGCtMMj92pReUsQ/0/*)#8e7pq23w" ),
1644+ ]
1645+ }, id = "no_passphrase" ),
1646+ pytest .param ({
1647+ "mnemonic" : "ritual idle hat sunny universe pluck key alpha wing cake have wedding" ,
1648+ "passphrase" : "test_passphrase" ,
1649+ "test_commands" : [
1650+ ("getnodeid" , "039020371fb803cd4ce1e9a909b502d7b0a9e0f10cccc35c3e9be959c52d3ba6bd" ),
1651+ ("getsecret" , "ritual idle hat sunny universe pluck key alpha wing cake have wedding" ),
1652+ ("makerune" , "2JtNfGMM_U0b3_R2DU9L5K6dxxRjHPgDGf7daoVwKEc9MA==" ),
1653+ ("dumponchaindescriptors" ,
1654+ "wpkh(xpub6DQ23AMoZvy6gVSMi5EvBawiyXMVWW9txHh1msjH1b8W1HsC3VbcxSz71qjMeDqc5EFFADz7DuiSHsZtLs9hxYaH1WrRoWECiD2YbpyiFbd/0/*)#9n6j954n\n "
1655+ "sh(wpkh(xpub6DQ23AMoZvy6gVSMi5EvBawiyXMVWW9txHh1msjH1b8W1HsC3VbcxSz71qjMeDqc5EFFADz7DuiSHsZtLs9hxYaH1WrRoWECiD2YbpyiFbd/0/*))#ynr7hayd\n "
1656+ "tr(xpub6DQ23AMoZvy6gVSMi5EvBawiyXMVWW9txHh1msjH1b8W1HsC3VbcxSz71qjMeDqc5EFFADz7DuiSHsZtLs9hxYaH1WrRoWECiD2YbpyiFbd/0/*)#3njcgkrj" ),
1657+ ]
1658+ }, id = "with_passphrase" )
1659+ ])
1660+ def test_hsmtool_all_commands_work_with_mnemonic_formats (node_factory , test_case ):
16331661 """Test that all hsmtool commands work with mnemonic formats"""
16341662 l1 = node_factory .get_node (start = False )
16351663 hsm_path = os .path .join (l1 .daemon .lightning_dir , TEST_NETWORK , "hsm_secret" )
16361664 os .remove (hsm_path )
16371665
1638- # Create a mnemonic-based hsm_secret (no passphrase for simplicity)
1666+ mnemonic = test_case ['mnemonic' ]
1667+ passphrase = test_case ['passphrase' ]
1668+ test_commands = test_case ['test_commands' ]
1669+
1670+ # Create a mnemonic-based hsm_secret
16391671 hsmtool = HsmTool (node_factory .directory , "generatehsm" , hsm_path )
16401672 master_fd , slave_fd = os .openpty ()
16411673 hsmtool .start (stdin = slave_fd )
16421674 hsmtool .wait_for_log (r"Introduce your BIP39 word list" )
1643- write_all (master_fd , "abandon abandon abandon abandon abandon abandon abandon abandon abandon abandon abandon about \n " .encode ("utf-8" ))
1675+ write_all (master_fd , f" { mnemonic } \n " .encode ("utf-8" ))
16441676 hsmtool .wait_for_log (r"Enter your passphrase:" )
1645- write_all (master_fd , " \n " .encode ("utf-8" ))
1677+ write_all (master_fd , f" { passphrase } \n " .encode ("utf-8" ))
16461678 assert hsmtool .proc .wait (WAIT_TIMEOUT ) == 0
16471679
1648- # Test various commands work with mnemonic format
1649- test_commands = [
1650- (["getnodeid" , hsm_path ], "03653e90c1ce4660fd8505dd6d643356e93cfe202af109d382787639dd5890e87d" ),
1651- (["getsecret" , hsm_path ], "abandon abandon abandon abandon abandon abandon abandon abandon abandon abandon abandon about" ),
1652- (["makerune" , hsm_path ], "6VkrWMI2hm2a2UTkg-EyUrrBJN0RcuPB80I1pCVkTD89MA==" ),
1653- (["dumponchaindescriptors" , hsm_path ],
1654- "wpkh(xpub6BgBgsespWvERF3LHQu6CnqdvfEvtMcQjYrcRzx53QJjSxarj2afYWcLteoGVky7D3UKDP9QyrLprQ3VCECoY49yfdDEHGCtMMj92pReUsQ/0/*)#hjszq0wk\n "
1655- "sh(wpkh(xpub6BgBgsespWvERF3LHQu6CnqdvfEvtMcQjYrcRzx53QJjSxarj2afYWcLteoGVky7D3UKDP9QyrLprQ3VCECoY49yfdDEHGCtMMj92pReUsQ/0/*))#u0t3u3xz\n "
1656- "tr(xpub6BgBgsespWvERF3LHQu6CnqdvfEvtMcQjYrcRzx53QJjSxarj2afYWcLteoGVky7D3UKDP9QyrLprQ3VCECoY49yfdDEHGCtMMj92pReUsQ/0/*)#8e7pq23w" ),
1657- ]
1658-
1659- for cmd_args , expected_output in test_commands :
1660- cmd_line = ["tools/lightning-hsmtool" ] + cmd_args
1661- out = subprocess .check_output (cmd_line ).decode ("utf8" )
1662- actual_output = out .strip ()
1663- assert actual_output == expected_output , f"Command { cmd_args [0 ]} output mismatch"
1680+ for cmd , expected_output in test_commands :
1681+ result = subprocess .run (
1682+ ["tools/lightning-hsmtool" , cmd , hsm_path ],
1683+ input = f"{ passphrase } \n " if passphrase else None ,
1684+ capture_output = True , text = True
1685+ )
1686+ actual_output = result .stdout .strip ()
1687+ if passphrase :
1688+ # remove 'Enter hsm_secret password:'
1689+ actual_output = "\n " .join (actual_output .split ("\n " )[1 :])
1690+ assert actual_output == expected_output , f"Command { cmd } output mismatch"
16641691
16651692
16661693def test_hsmtool_deterministic_node_ids (node_factory ):
0 commit comments