-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathKeyPoint.swift
163 lines (149 loc) · 5.28 KB
/
KeyPoint.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
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
import Foundation
#if canImport(lib)
import lib
#endif
public enum PublicKeyEncoding : Equatable, Hashable {
case EllipticCompress
case FullAddress
public var value : String {
switch self {
case .EllipticCompress:
return "elliptic-compressed"
case .FullAddress :
return ""
}
}
}
public final class KeyPoint: Equatable {
/// Compares two KeyPoint objects
///
/// - Parameters:
/// - lhs: First `KeyPoint` to compare.
/// - rhs: Second `Keypoint` to compare.
///
/// - Returns: `true` if they are equal, `false` otherwise
public static func == (lhs: KeyPoint, rhs: KeyPoint) -> Bool {
do {
let lhsx = try lhs.getX()
let lhsy = try lhs.getY()
let rhsx = try rhs.getX()
let rhsy = try rhs.getY()
if lhsx == rhsx && lhsy == rhsy
{
return true
} else {
return false
}
} catch {
return false
}
}
public var pointer: OpaquePointer?
/// Instantiate a `KeyPoint` object using the underlying pointer.
///
/// - Returns: `KeyPoint`
///
/// - Parameters:
/// - pointer: The pointer to the underlying foreign function interface object.
public init(pointer: OpaquePointer) {
self.pointer = pointer
}
/// Instantiates a `KeyPoint` object using X and Y co-ordinates in hexadecimal format.
///
/// - Parameters:
/// - x: X value of co-ordinate pair.
/// - y: Y value of co-ordinate pair.
///
/// - Returns: `KeyPoint` object.
///
/// - Throws: `RuntimeError`, indicates invalid parameters was used.
public init(x: String, y: String) throws {
var errorCode: Int32 = -1
let xPtr = UnsafeMutablePointer<Int8>(mutating: (x as NSString).utf8String)
let yPtr = UnsafeMutablePointer<Int8>(mutating: (y as NSString).utf8String)
let result = withUnsafeMutablePointer(to: &errorCode, { error in
key_point_new(xPtr, yPtr, error)
})
guard errorCode == 0 else {
throw RuntimeError("Error in KeyPoint, new")
}
pointer = result;
}
/// Instantiates a `KeyPoint` object using X and Y co-ordinates in hexadecimal format.
///
/// - Parameters:
/// - address : compress or full address
///
/// - Returns: `KeyPoint` object.
///
/// - Throws: `RuntimeError`, indicates invalid parameters was used.
public init(address: String ) throws {
var errorCode: Int32 = -1
let addressPtr = UnsafeMutablePointer<Int8>(mutating: (address as NSString).utf8String)
let result = withUnsafeMutablePointer(to: &errorCode, { error in
key_point_new_addr(addressPtr, error)
})
guard errorCode == 0 else {
throw RuntimeError("Error in KeyPoint, new")
}
pointer = result;
}
/// Retrieves the X value of the co-ordinate pair.
///
/// - Returns: X value in hexadecimal format as `String`
///
/// - Throws: `RuntimeError`, indicates underlying pointer is invalid.
public func getX() throws -> String {
var errorCode: Int32 = -1
let result = withUnsafeMutablePointer(to: &errorCode, { error in
key_point_get_x(pointer, error)
})
guard errorCode == 0 else {
throw RuntimeError("Error in KeyPoint, field X")
}
let x = String.init(cString: result!)
string_free(result)
return x
}
/// Retrieves the Y value of the co-ordinate pair.
///
/// - Returns: Y value in hexadecimal format as `String`
///
/// - Throws: `RuntimeError`, indicates underlying pointer is invalid.
public func getY() throws -> String {
var errorCode: Int32 = -1
let result = withUnsafeMutablePointer(to: &errorCode, { error in
key_point_get_y(pointer, error)
})
guard errorCode == 0 else {
throw RuntimeError("Error in KeyPoint, field Y")
}
let y = String.init(cString: result!)
string_free(result)
return y
}
/// Gets the serialized form of a `KeyPoint`, should it be a valid PublicKey.
///
/// - Parameters:
/// - format: `"elliptic-compressed"` for the compressed form, otherwise the uncompressed form will be returned.
///
/// - Returns: Serialized form of `KeyPoint` as `String`
///
/// - Throws: `RuntimeError`, indicates either the underlying pointer is invalid or the co-ordinate pair is not a valid PublicKey.
public func getPublicKey(format: PublicKeyEncoding ) throws -> String {
var errorCode: Int32 = -1
let encoder_format = UnsafeMutablePointer<Int8>(mutating: (format.value as NSString).utf8String)
let result = withUnsafeMutablePointer(to: &errorCode, { error in
key_point_encode(pointer, encoder_format, error)
})
guard errorCode == 0 else {
throw RuntimeError("Error in KeyPoint, getPublicKey")
}
let compressed = String.init(cString: result!)
string_free(result)
return compressed
}
deinit {
key_point_free(pointer)
}
}