-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathKeyReconstructionDetails.swift
72 lines (67 loc) · 2.72 KB
/
KeyReconstructionDetails.swift
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
import Foundation
#if canImport(lib)
import lib
#endif
public final class KeyReconstructionDetails: Codable {
public var key: String
public var seed_phrase: [String]
public var all_keys: [String]
/// Instantiate a `KeyReconstructionDetails` object using the underlying pointer.
///
/// - Parameters:
/// - pointer: The pointer to the underlying foreign function interface object.
///
/// - Returns: `KeyReconstructionDetails`
///
/// - Throws: `RuntimeError`, indicates underlying pointer is invalid.
public init(pointer: OpaquePointer) throws {
var errorCode: Int32 = -1
let key = withUnsafeMutablePointer(to: &errorCode, { error in
key_reconstruction_get_private_key(pointer, error)
})
guard errorCode == 0 else {
throw RuntimeError("Error in KeyDetails, field Private Key")
}
self.key = String.init(cString: key!)
string_free(key)
self.seed_phrase = []
let seed_len = withUnsafeMutablePointer(to: &errorCode, { error in
key_reconstruction_get_seed_phrase_len(pointer, error)
})
guard errorCode == 0 else {
throw RuntimeError("Error in KeyDetails, field Seed Phrase")
}
if seed_len > 0 {
for index in 0...seed_len-1 {
let seed_item = withUnsafeMutablePointer(to: &errorCode, { error in
key_reconstruction_get_seed_phrase_at(pointer, index, error)
})
guard errorCode == 0 else {
throw RuntimeError("Error in KeyDetails, field Seed Phrase, index " + String(index))
}
self.seed_phrase.append(String.init(cString: seed_item!))
string_free(seed_item)
}
}
self.all_keys = []
let keys_len = withUnsafeMutablePointer(to: &errorCode, { error in
key_reconstruction_get_all_keys_len(pointer, error)
})
guard errorCode == 0 else {
throw RuntimeError("Error in KeyDetails, field Seed Phrase")
}
if keys_len > 0 {
for index in 0...keys_len-1 {
let seed_item = withUnsafeMutablePointer(to: &errorCode, { error in
key_reconstruction_get_all_keys_at(pointer, index, error)
})
guard errorCode == 0 else {
throw RuntimeError("Error in KeyDetails, field Seed Phrase, index " + String(index))
}
self.all_keys.append(String.init(cString: seed_item!))
string_free(seed_item)
}
}
key_reconstruction_details_free(pointer)
}
}