@@ -40,6 +40,18 @@ extension HDNode: BIP44 {
4040}
4141
4242extension String {
43+ /// Verifies if current value matches BIP44 path standard
44+ var isBip44Path : Bool {
45+ do {
46+ let pattern = " ^m/44'/ \\ d+'/ \\ d+'/[0-1]/ \\ d+$ "
47+ let regex = try NSRegularExpression ( pattern: pattern, options: [ . caseInsensitive] )
48+ let matches = regex. numberOfMatches ( in: self , range: NSRange ( location: 0 , length: utf16. count) )
49+ return matches == 1
50+ } catch {
51+ return false
52+ }
53+ }
54+
4355 /// Returns the account from the path if the string contains a well formed BIP44 path
4456 var accountFromPath : Int ? {
4557 guard isBip44Path else {
@@ -54,37 +66,25 @@ extension String {
5466 return account
5567 }
5668
57- /// Returns a new BIP32 path that uses an external change, if the path is invalid returns nil
58- var externalChangePath : String ? {
59- do {
60- guard isBip44Path else {
61- return nil
62- }
63- let changePathPattern = " '/[0-1]/ "
64- let regex = try NSRegularExpression ( pattern: changePathPattern, options: [ . caseInsensitive] )
65- let range = NSRange ( location: 0 , length: utf16. count)
66- let matches = regex. numberOfMatches ( in: self , range: range)
67- if matches == 1 {
68- let result = regex. stringByReplacingMatches ( in: self , options: [ ] , range: range, withTemplate: " '/0/ " )
69- return result
70- } else {
71- return nil
72- }
73- } catch {
69+ /**
70+ Transforms a bip44 path into a new one changing `account` & `index`. The resulting one will have the change value equal to `0` to represent external chain. Format `m/44'/coin_type'/account'/change/address_index`
71+ - Parameter account: the new `account` to use
72+ - Parameter addressIndex: the new `addressIndex` to use
73+ - Returns: a valid bip44 path or nil otherwise
74+ */
75+ func newPath( account: Int , addressIndex: Int ) -> String ? {
76+ guard isBip44Path else {
7477 return nil
7578 }
76- }
77-
78- /// Verifies if matches BIP44 path standard
79- var isBip44Path : Bool {
80- do {
81- let pattern = " ^m/44'/ \\ d+'/ \\ d+'/[0-1]/ \\ d+$ "
82- let regex = try NSRegularExpression ( pattern: pattern, options: [ . caseInsensitive] )
83- let matches = regex. numberOfMatches ( in: self , range: NSRange ( location: 0 , length: utf16. count) )
84- return matches == 1
85- } catch {
86- return false
87- }
79+ var components = components ( separatedBy: " / " )
80+ let accountPosition = 3
81+ components [ accountPosition] = " \( account) ' "
82+ let changePosition = 4
83+ components [ changePosition] = " 0 "
84+ let addressIndexPosition = 5
85+ components [ addressIndexPosition] = " \( addressIndex) "
86+ let result = components. joined ( separator: " / " )
87+ return result
8888 }
8989}
9090
0 commit comments