@@ -25,6 +25,7 @@ public struct PublicKey {
25
25
case invalidTweak
26
26
case invalidIndex
27
27
case failedToCreateContext
28
+ case derivationFailed
28
29
29
30
public var errorDescription : String ? {
30
31
switch self {
@@ -38,6 +39,8 @@ public struct PublicKey {
38
39
return " Not a valid index "
39
40
case . failedToCreateContext:
40
41
return " Failed to create a new context "
42
+ case . derivationFailed:
43
+ return " Derivation Failed "
41
44
}
42
45
}
43
46
}
@@ -126,7 +129,7 @@ public struct PublicKey {
126
129
return Base58 . encode ( extendedPublicKeyData + checksum)
127
130
}
128
131
129
- public func derived( at index: UInt32 ) throws -> PublicKey {
132
+ private func derived( at index: UInt32 ) throws -> PublicKey {
130
133
let edge : UInt32 = 0x80000000
131
134
guard ( edge & index) == 0 else { throw Errors . invalidIndex }
132
135
@@ -163,7 +166,15 @@ public struct PublicKey {
163
166
return Void ( )
164
167
}
165
168
166
- return try PublicKey ( raw: publicKey, chainCode: derivedChainCode, network: self . network, depth: self . depth + 1 , fingerprint: self . fingerprint, index: index) . derived ( at: 0 )
169
+ return PublicKey ( raw: publicKey, chainCode: derivedChainCode, network: self . network, depth: self . depth + 1 , fingerprint: self . fingerprint, index: index)
170
+ }
171
+
172
+ public func derived( index: UInt32 ) throws -> PublicKey {
173
+ guard let derived = try ? self . derived ( at: 0 ) . derived ( at: index) else {
174
+ throw Errors . derivationFailed
175
+ }
176
+
177
+ return derived
167
178
}
168
179
}
169
180
0 commit comments