-
Notifications
You must be signed in to change notification settings - Fork 236
WIP: descriptor request using "getkeys" #73
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Closed
Closed
Changes from all commits
Commits
Show all changes
3 commits
Select commit
Hold shift + click to select a range
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or 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 hidden or 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 hidden or 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,89 @@ | ||
| import re | ||
|
|
||
| class Descriptor: | ||
| def __init__(self, origin_fingerprint, origin_path, base_key, path_suffix, testnet, sh_wpkh, wpkh, is_request): | ||
| self.origin_fingerprint = origin_fingerprint | ||
| self.origin_path = origin_path | ||
| self.path_suffix = path_suffix | ||
| self.base_key = base_key | ||
| self.testnet = testnet | ||
| self.sh_wpkh = sh_wpkh | ||
| self.wpkh = wpkh | ||
| self.m_path = None | ||
| self.is_request = is_request | ||
|
|
||
| if origin_path: | ||
| self.m_path = "m" + origin_path + (path_suffix or "") | ||
|
|
||
| if is_request: | ||
| self.m_path_base = "m" + origin_path | ||
|
|
||
| @classmethod | ||
| def parse(cls, desc, testnet = False): | ||
| sh_wpkh = None | ||
| wpkh = None | ||
| origin_fingerprint = None | ||
| origin_path = None | ||
| base_key_and_path_match = None | ||
| base_key = None | ||
| is_request = False | ||
|
|
||
| if desc.startswith("sh(wpkh("): | ||
| sh_wpkh = True | ||
| elif desc.startswith("wpkh("): | ||
| wpkh = True | ||
|
|
||
| origin_match = re.search(r"\[(.*)\]", desc) | ||
| if origin_match: | ||
| origin = origin_match.group(1) | ||
| match = re.search(r"^([0-9a-fA-F]{8})(\/.*)", origin) | ||
| if match: | ||
| origin_fingerprint = match.group(1) | ||
| origin_path = match.group(2) | ||
| # Replace h with ' | ||
| origin_path = origin_path.replace('h', '\'') | ||
|
|
||
| base_key_and_path_match = re.search(r"\[.*\](\w+)([\/\)][\d'\/\*]*)", desc) | ||
| else: | ||
| base_key_and_path_match = re.search(r"\((\w+)([\/\)][\d'\/\*]*)", desc) | ||
|
|
||
| if base_key_and_path_match: | ||
| base_key = base_key_and_path_match.group(1) | ||
| path_suffix = base_key_and_path_match.group(2) | ||
| if path_suffix == ")": | ||
| path_suffix = None | ||
| else: | ||
| if origin_match == None: | ||
| return None | ||
| else: | ||
| # Check if this is a descriptor request, which does not contain | ||
| # a key, must contain an origin and the last path element must be * or *' | ||
| request_match = re.search(r"\[.*\]([\/\)][\d'\/\*]*\*[']?)", desc) | ||
| if request_match is None: | ||
| return None | ||
|
|
||
| path_suffix = request_match.group(1) | ||
| is_request = True | ||
|
|
||
| return cls(origin_fingerprint, origin_path, base_key, path_suffix, testnet, sh_wpkh, wpkh, is_request) | ||
|
|
||
|
|
||
| def serialize(self): | ||
| descriptor_open = 'pkh(' | ||
| descriptor_close = ')' | ||
| origin = '' | ||
| path_suffix = '' | ||
|
|
||
| if self.wpkh == True: | ||
| descriptor_open = 'wpkh(' | ||
| elif self.sh_wpkh == True: | ||
| descriptor_open = 'sh(wpkh(' | ||
| descriptor_close = '))' | ||
|
|
||
| if self.origin_fingerprint and self.origin_path: | ||
| origin = '[' + self.origin_fingerprint + self.origin_path + ']' | ||
|
|
||
| if self.path_suffix: | ||
| path_suffix = self.path_suffix | ||
|
|
||
| return descriptor_open + origin + self.base_key + path_suffix + descriptor_close | ||
This file contains hidden or 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 hidden or 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,90 @@ | ||
| #! /usr/bin/env python3 | ||
|
|
||
| from hwilib.descriptor import Descriptor | ||
| import unittest | ||
|
|
||
| class TestDescriptor(unittest.TestCase): | ||
| def test_parse_descriptor_with_origin(self): | ||
| desc = Descriptor.parse("wpkh([00000001/84'/1'/0']tpubD6NzVbkrYhZ4WaWSyoBvQwbpLkojyoTZPRsgXELWz3Popb3qkjcJyJUGLnL4qHHoQvao8ESaAstxYSnhyswJ76uZPStJRJCTKvosUCJZL5B/0/0)", True) | ||
| self.assertIsNotNone(desc) | ||
| self.assertEqual(desc.wpkh, True) | ||
| self.assertEqual(desc.sh_wpkh, None) | ||
| self.assertEqual(desc.origin_fingerprint, "00000001") | ||
| self.assertEqual(desc.origin_path, "/84'/1'/0'") | ||
| self.assertEqual(desc.base_key, "tpubD6NzVbkrYhZ4WaWSyoBvQwbpLkojyoTZPRsgXELWz3Popb3qkjcJyJUGLnL4qHHoQvao8ESaAstxYSnhyswJ76uZPStJRJCTKvosUCJZL5B") | ||
| self.assertEqual(desc.path_suffix, "/0/0") | ||
| self.assertEqual(desc.testnet, True) | ||
| self.assertEqual(desc.m_path, "m/84'/1'/0'/0/0") | ||
|
|
||
| def test_parse_descriptor_without_origin(self): | ||
| desc = Descriptor.parse("wpkh(tpubD6NzVbkrYhZ4WaWSyoBvQwbpLkojyoTZPRsgXELWz3Popb3qkjcJyJUGLnL4qHHoQvao8ESaAstxYSnhyswJ76uZPStJRJCTKvosUCJZL5B/0/0)", True) | ||
| self.assertIsNotNone(desc) | ||
| self.assertEqual(desc.wpkh, True) | ||
| self.assertEqual(desc.sh_wpkh, None) | ||
| self.assertEqual(desc.origin_fingerprint, None) | ||
| self.assertEqual(desc.origin_path, None) | ||
| self.assertEqual(desc.base_key, "tpubD6NzVbkrYhZ4WaWSyoBvQwbpLkojyoTZPRsgXELWz3Popb3qkjcJyJUGLnL4qHHoQvao8ESaAstxYSnhyswJ76uZPStJRJCTKvosUCJZL5B") | ||
| self.assertEqual(desc.path_suffix, "/0/0") | ||
| self.assertEqual(desc.testnet, True) | ||
| self.assertEqual(desc.m_path, None) | ||
|
|
||
| def test_parse_descriptor_with_key_at_end_with_origin(self): | ||
| desc = Descriptor.parse("wpkh([00000001/84'/1'/0'/0/0]0297dc3f4420402e01a113984311bf4a1b8de376cac0bdcfaf1b3ac81f13433c7)", True) | ||
| self.assertIsNotNone(desc) | ||
| self.assertEqual(desc.wpkh, True) | ||
| self.assertEqual(desc.sh_wpkh, None) | ||
| self.assertEqual(desc.origin_fingerprint, "00000001") | ||
| self.assertEqual(desc.origin_path, "/84'/1'/0'/0/0") | ||
| self.assertEqual(desc.base_key, "0297dc3f4420402e01a113984311bf4a1b8de376cac0bdcfaf1b3ac81f13433c7") | ||
| self.assertEqual(desc.path_suffix, None) | ||
| self.assertEqual(desc.testnet, True) | ||
| self.assertEqual(desc.m_path, "m/84'/1'/0'/0/0") | ||
|
|
||
| def test_parse_descriptor_with_key_at_end_without_origin(self): | ||
| desc = Descriptor.parse("wpkh(0297dc3f4420402e01a113984311bf4a1b8de376cac0bdcfaf1b3ac81f13433c7)", True) | ||
| self.assertIsNotNone(desc) | ||
| self.assertEqual(desc.wpkh, True) | ||
| self.assertEqual(desc.sh_wpkh, None) | ||
| self.assertEqual(desc.origin_fingerprint, None) | ||
| self.assertEqual(desc.origin_path, None) | ||
| self.assertEqual(desc.base_key, "0297dc3f4420402e01a113984311bf4a1b8de376cac0bdcfaf1b3ac81f13433c7") | ||
| self.assertEqual(desc.path_suffix, None) | ||
| self.assertEqual(desc.testnet, True) | ||
| self.assertEqual(desc.m_path, None) | ||
|
|
||
| def test_parse_empty_descriptor(self): | ||
| desc = Descriptor.parse("", True) | ||
| self.assertIsNone(desc) | ||
|
|
||
| def test_parse_descriptor_replace_h(self): | ||
| desc = Descriptor.parse("wpkh([00000001/84h/1h/0']tpubD6NzVbkrYhZ4WaWSyoBvQwbpLkojyoTZPRsgXELWz3Popb3qkjcJyJUGLnL4qHHoQvao8ESaAstxYSnhyswJ76uZPStJRJCTKvosUCJZL5B/0/0)", True) | ||
| self.assertIsNotNone(desc) | ||
| self.assertEqual(desc.origin_path, "/84'/1'/0'") | ||
|
|
||
| def test_parse_descriptor_request(self): | ||
| desc = Descriptor.parse("wpkh([00000001/84'/1'/0']/0/*)", True) | ||
| self.assertIsNotNone(desc) | ||
| self.assertEqual(desc.origin_fingerprint, "00000001") | ||
| self.assertEqual(desc.origin_path, "/84'/1'/0'") | ||
| self.assertEqual(desc.base_key, None) | ||
| self.assertEqual(desc.path_suffix, "/0/*") | ||
| self.assertEqual(desc.m_path, "m/84'/1'/0'/0/*") | ||
| self.assertEqual(desc.m_path_base, "m/84'/1'/0'") | ||
|
|
||
| def test_serialize_descriptor_with_origin(self): | ||
| descriptor = "wpkh([00000001/84'/1'/0']tpubD6NzVbkrYhZ4WaWSyoBvQwbpLkojyoTZPRsgXELWz3Popb3qkjcJyJUGLnL4qHHoQvao8ESaAstxYSnhyswJ76uZPStJRJCTKvosUCJZL5B/0/0)" | ||
| desc = Descriptor.parse(descriptor, True) | ||
| self.assertEqual(desc.serialize(), descriptor) | ||
|
|
||
| def test_serialize_descriptor_without_origin(self): | ||
| descriptor = "wpkh(tpubD6NzVbkrYhZ4WaWSyoBvQwbpLkojyoTZPRsgXELWz3Popb3qkjcJyJUGLnL4qHHoQvao8ESaAstxYSnhyswJ76uZPStJRJCTKvosUCJZL5B/0/0)" | ||
| desc = Descriptor.parse(descriptor, True) | ||
| self.assertEqual(desc.serialize(), descriptor) | ||
|
|
||
| def test_serialize_descriptor_with_key_at_end_with_origin(self): | ||
| descriptor = "wpkh([00000001/84'/1'/0'/0/0]0297dc3f4420402e01a113984311bf4a1b8de376cac0bdcfaf1b3ac81f13433c7)" | ||
| desc = Descriptor.parse(descriptor, True) | ||
| self.assertEqual(desc.serialize(), descriptor) | ||
|
|
||
| if __name__ == "__main__": | ||
| unittest.main() |
This file contains hidden or 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
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.