Skip to content

Commit bfedd45

Browse files
committedJun 27, 2018
Thread access
1 parent fe689e5 commit bfedd45

File tree

1 file changed

+13
-2
lines changed

1 file changed

+13
-2
lines changed
 

‎HDWalletKit/PublicKey.swift

+13-2
Original file line numberDiff line numberDiff line change
@@ -25,6 +25,7 @@ public struct PublicKey {
2525
case invalidTweak
2626
case invalidIndex
2727
case failedToCreateContext
28+
case derivationFailed
2829

2930
public var errorDescription: String? {
3031
switch self {
@@ -38,6 +39,8 @@ public struct PublicKey {
3839
return "Not a valid index"
3940
case .failedToCreateContext:
4041
return "Failed to create a new context"
42+
case .derivationFailed:
43+
return "Derivation Failed"
4144
}
4245
}
4346
}
@@ -126,7 +129,7 @@ public struct PublicKey {
126129
return Base58.encode(extendedPublicKeyData + checksum)
127130
}
128131

129-
public func derived(at index: UInt32) throws -> PublicKey {
132+
private func derived(at index: UInt32) throws -> PublicKey {
130133
let edge: UInt32 = 0x80000000
131134
guard (edge & index) == 0 else { throw Errors.invalidIndex }
132135

@@ -163,7 +166,15 @@ public struct PublicKey {
163166
return Void()
164167
}
165168

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
167178
}
168179
}
169180

0 commit comments

Comments
 (0)
Please sign in to comment.