-
Notifications
You must be signed in to change notification settings - Fork 444
/
Utilities.swift
342 lines (316 loc) · 15.8 KB
/
Utilities.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
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
//
// Utilities.swift
//
//
// Created by Yaroslav Yashin on 11.07.2022.
//
import Foundation
import BigInt
public struct Utilities {
/// Convert a public key to the corresponding EthereumAddress. Accepts public keys in compressed (33 bytes), non-compressed (65 bytes)
/// or raw concat(X, Y) (64 bytes) format.
///
/// Returns 20 bytes of address data.
static func publicToAddressData(_ publicKey: Data) -> Data? {
if publicKey.count == 33 {
guard let decompressedKey = SECP256K1.combineSerializedPublicKeys(keys: [publicKey], outputCompressed: false) else {return nil}
return publicToAddressData(decompressedKey)
}
var stipped = publicKey
if stipped.count == 65 {
if stipped[0] != 4 {
return nil
}
stipped = stipped[1...64]
}
if stipped.count != 64 {
return nil
}
let sha3 = stipped.sha3(.keccak256)
let addressData = sha3[12...31]
return addressData
}
/// Convert a public key to the corresponding EthereumAddress. Accepts public keys in compressed (33 bytes), non-compressed (65 bytes)
/// or raw concat(X, Y) (64 bytes) format.
///
/// Returns the EthereumAddress object.
public static func publicToAddress(_ publicKey: Data) -> EthereumAddress? {
guard let addressData = publicToAddressData(publicKey) else {return nil}
let address = addressData.toHexString().addHexPrefix().lowercased()
return EthereumAddress(address)
}
/// Convert the private key (32 bytes of Data) to compressed (33 bytes) or non-compressed (65 bytes) public key.
public static func privateToPublic(_ privateKey: Data, compressed: Bool = false) -> Data? {
guard let publicKey = SECP256K1.privateToPublic(privateKey: privateKey, compressed: compressed) else {return nil}
return publicKey
}
/// Convert a public key to the corresponding EthereumAddress. Accepts public keys in compressed (33 bytes), non-compressed (65 bytes)
/// or raw concat(X, Y) (64 bytes) format.
///
/// Returns a 0x prefixed hex string.
public static func publicToAddressString(_ publicKey: Data) -> String? {
guard let addressData = Utilities.publicToAddressData(publicKey) else {return nil}
let address = addressData.toHexString().addHexPrefix().lowercased()
return address
}
/// Converts address data (20 bytes) to the 0x prefixed hex string. Does not perform checksumming.
static func addressDataToString(_ addressData: Data) -> String? {
guard addressData.count == 20 else {return nil}
return addressData.toHexString().addHexPrefix().lowercased()
}
/// Hashes a personal message by first padding it with the "\u{19}Ethereum Signed Message:\n" string and message length string.
/// Should be used if some arbitrary information should be hashed and signed to prevent signing an Ethereum transaction
/// by accident.
public static func hashPersonalMessage(_ personalMessage: Data) -> Data? {
var prefix = "\u{19}Ethereum Signed Message:\n"
prefix += String(personalMessage.count)
guard let prefixData = prefix.data(using: .ascii) else {return nil}
var data = Data()
if personalMessage.count >= prefixData.count && prefixData == personalMessage[0 ..< prefixData.count] {
data.append(personalMessage)
} else {
data.append(prefixData)
data.append(personalMessage)
}
let hash = data.sha3(.keccak256)
return hash
}
/// Parse a user-supplied string using the number of decimals for particular Ethereum unit.
/// If input is non-numeric or precision is not sufficient - returns nil.
/// Allowed decimal separators are ".", ",".
public static func parseToBigUInt(_ amount: String, units: Utilities.Units = .eth) -> BigUInt? {
let unitDecimals = units.decimals
return parseToBigUInt(amount, decimals: unitDecimals)
}
/// Parse a user-supplied string using the number of decimals.
/// If input is non-numeric or precision is not sufficient - returns nil.
/// Allowed decimal separators are ".", ",".
public static func parseToBigUInt(_ amount: String, decimals: Int = 18) -> BigUInt? {
let separators = CharacterSet(charactersIn: ".,")
let components = amount.trimmingCharacters(in: .whitespacesAndNewlines).components(separatedBy: separators)
guard components.count == 1 || components.count == 2 else {return nil}
let unitDecimals = decimals
guard let beforeDecPoint = BigUInt(components[0], radix: 10) else {return nil}
var mainPart = beforeDecPoint*BigUInt(10).power(unitDecimals)
if components.count == 2 {
let numDigits = components[1].count
guard numDigits <= unitDecimals else {return nil}
guard let afterDecPoint = BigUInt(components[1], radix: 10) else {return nil}
let extraPart = afterDecPoint*BigUInt(10).power(unitDecimals-numDigits)
mainPart = mainPart + extraPart
}
return mainPart
}
/// Formats a BigInt object to String. The supplied number is first divided into integer and decimal part based on "toUnits",
/// then limit the decimal part to "decimals" symbols and uses a "decimalSeparator" as a separator.
///
/// Returns nil of formatting is not possible to satisfy.
static func formatToEthereumUnits(_ bigNumber: BigInt, toUnits: Utilities.Units = .eth, decimals: Int = 4, decimalSeparator: String = ".") -> String? {
let magnitude = BigInt(bigNumber.magnitude)
guard let formatted = formatToEthereumUnits(magnitude, toUnits: toUnits, decimals: decimals, decimalSeparator: decimalSeparator) else {return nil}
switch bigNumber.sign {
case .plus:
return formatted
case .minus:
return "-" + formatted
}
}
/// Formats a BigInt object to String. The supplied number is first divided into integer and decimal part based on "toUnits",
/// then limit the decimal part to "decimals" symbols and uses a "decimalSeparator" as a separator.
/// Fallbacks to scientific format if higher precision is required.
///
/// Returns nil of formatting is not possible to satisfy.
public static func formatToPrecision(_ bigNumber: BigInt, numberDecimals: Int = 18, formattingDecimals: Int = 4, decimalSeparator: String = ".", fallbackToScientific: Bool = false) -> String? {
let magnitude = bigNumber.magnitude
guard let formatted = formatToPrecision(magnitude, numberDecimals: numberDecimals, formattingDecimals: formattingDecimals, decimalSeparator: decimalSeparator, fallbackToScientific: fallbackToScientific) else {return nil}
switch bigNumber.sign {
case .plus:
return formatted
case .minus:
return "-" + formatted
}
}
// /// Formats a BigUInt object to String. The supplied number is first divided into integer and decimal part based on "toUnits",
// /// then limit the decimal part to "decimals" symbols and uses a "decimalSeparator" as a separator.
// ///
// /// Returns nil of formatting is not possible to satisfy.
// static func formatToEthereumUnits(_ bigNumber: BigUInt, toUnits: Utilities.Units = .eth, decimals: Int = 4, decimalSeparator: String = ".", fallbackToScientific: Bool = false) -> String? {
// return formatToPrecision(bigNumber, numberDecimals: toUnits.decimals, formattingDecimals: decimals, decimalSeparator: decimalSeparator, fallbackToScientific: fallbackToScientific)
// }
/// Formats a BigUInt object to String. The supplied number is first divided into integer and decimal part based on "numberDecimals",
/// then limits the decimal part to "formattingDecimals" symbols and uses a "decimalSeparator" as a separator.
/// Fallbacks to scientific format if higher precision is required.
///
/// Returns nil of formatting is not possible to satisfy.
public static func formatToPrecision(_ bigNumber: BigUInt, numberDecimals: Int = 18, formattingDecimals: Int = 4, decimalSeparator: String = ".", fallbackToScientific: Bool = false) -> String? {
if bigNumber == 0 {
return "0"
}
let unitDecimals = numberDecimals
var toDecimals = formattingDecimals
if unitDecimals < toDecimals {
toDecimals = unitDecimals
}
let divisor = BigUInt(10).power(unitDecimals)
let (quotient, remainder) = bigNumber.quotientAndRemainder(dividingBy: divisor)
var fullRemainder = String(remainder)
let fullPaddedRemainder = fullRemainder.leftPadding(toLength: unitDecimals, withPad: "0")
let remainderPadded = fullPaddedRemainder[0..<toDecimals]
if remainderPadded == String(repeating: "0", count: toDecimals) {
if quotient != 0 {
return String(quotient)
} else if fallbackToScientific {
var firstDigit = 0
for char in fullPaddedRemainder {
if char == "0" {
firstDigit = firstDigit + 1
} else {
let firstDecimalUnit = String(fullPaddedRemainder[firstDigit ..< firstDigit+1])
var remainingDigits = ""
let numOfRemainingDecimals = fullPaddedRemainder.count - firstDigit - 1
if numOfRemainingDecimals <= 0 {
remainingDigits = ""
} else if numOfRemainingDecimals > formattingDecimals {
let end = firstDigit+1+formattingDecimals > fullPaddedRemainder.count ? fullPaddedRemainder.count: firstDigit+1+formattingDecimals
remainingDigits = String(fullPaddedRemainder[firstDigit+1 ..< end])
} else {
remainingDigits = String(fullPaddedRemainder[firstDigit+1 ..< fullPaddedRemainder.count])
}
if remainingDigits != "" {
fullRemainder = firstDecimalUnit + decimalSeparator + remainingDigits
} else {
fullRemainder = firstDecimalUnit
}
firstDigit = firstDigit + 1
break
}
}
return fullRemainder + "e-" + String(firstDigit)
}
}
if toDecimals == 0 {
return String(quotient)
}
return String(quotient) + decimalSeparator + remainderPadded
}
/// Recover the Ethereum address from recoverable secp256k1 signature. Message is first hashed using the "personal hash" protocol.
/// BE WARNED - changing a message will result in different Ethereum address, but not in error.
///
/// Input parameters should be hex Strings.
static func personalECRecover(_ personalMessage: String, signature: String) -> EthereumAddress? {
guard let data = Data.fromHex(personalMessage) else {return nil}
guard let sig = Data.fromHex(signature) else {return nil}
return Utilities.personalECRecover(data, signature: sig)
}
/// Recover the Ethereum address from recoverable secp256k1 signature. Message is first hashed using the "personal hash" protocol.
/// BE WARNED - changing a message will result in different Ethereum address, but not in error.
///
/// Input parameters should be Data objects.
public static func personalECRecover(_ personalMessage: Data, signature: Data) -> EthereumAddress? {
if signature.count != 65 { return nil}
let rData = signature[0..<32].bytes
let sData = signature[32..<64].bytes
var vData = signature[64]
if vData >= 27 && vData <= 30 {
vData -= 27
} else if vData >= 31 && vData <= 34 {
vData -= 31
} else if vData >= 35 && vData <= 38 {
vData -= 35
}
guard let signatureData = SECP256K1.marshalSignature(v: vData, r: rData, s: sData) else {return nil}
guard let hash = Utilities.hashPersonalMessage(personalMessage) else {return nil}
guard let publicKey = SECP256K1.recoverPublicKey(hash: hash, signature: signatureData) else {return nil}
return Utilities.publicToAddress(publicKey)
}
/// Recover the Ethereum address from recoverable secp256k1 signature.
/// Takes a hash of some message. What message is hashed should be checked by user separately.
///
/// Input parameters should be Data objects.
public static func hashECRecover(hash: Data, signature: Data) -> EthereumAddress? {
if signature.count != 65 { return nil}
let rData = signature[0..<32].bytes
let sData = signature[32..<64].bytes
var vData = signature[64]
if vData >= 27 && vData <= 30 {
vData -= 27
} else if vData >= 31 && vData <= 34 {
vData -= 31
} else if vData >= 35 && vData <= 38 {
vData -= 35
}
guard let signatureData = SECP256K1.marshalSignature(v: vData, r: rData, s: sData) else {return nil}
guard let publicKey = SECP256K1.recoverPublicKey(hash: hash, signature: signatureData) else {return nil}
return Utilities.publicToAddress(publicKey)
}
/// returns Ethereum variant of sha3 (keccak256) of data. Returns nil is data is empty
static func keccak256(_ data: Data) -> Data? {
if data.count == 0 {return nil}
return data.sha3(.keccak256)
}
/// returns Ethereum variant of sha3 (keccak256) of data. Returns nil is data is empty
static func sha3(_ data: Data) -> Data? {
if data.count == 0 {return nil}
return data.sha3(.keccak256)
}
/// returns sha256 of data. Returns nil is data is empty
static func sha256(_ data: Data) -> Data? {
if data.count == 0 {return nil}
return data.sha256()
}
/// Unmarshals a 65 byte recoverable EC signature into internal structure.
static func unmarshalSignature(signatureData: Data) -> SECP256K1.UnmarshaledSignature? {
if signatureData.count != 65 {return nil}
let bytes = signatureData.bytes
let r = Array(bytes[0..<32])
let s = Array(bytes[32..<64])
return SECP256K1.UnmarshaledSignature(v: bytes[64], r: Data(r), s: Data(s))
}
/// Marshals the V, R and S signature parameters into a 65 byte recoverable EC signature.
static func marshalSignature(v: UInt8, r: [UInt8], s: [UInt8]) -> Data? {
guard r.count == 32, s.count == 32 else {return nil}
var completeSignature = Data(r)
completeSignature.append(Data(s))
completeSignature.append(Data([v]))
return completeSignature
}
/// Marshals internal signature structure into a 65 byte recoverable EC signature.
static func marshalSignature(unmarshalledSignature: SECP256K1.UnmarshaledSignature) -> Data {
var completeSignature = Data(unmarshalledSignature.r)
completeSignature.append(Data(unmarshalledSignature.s))
completeSignature.append(Data([unmarshalledSignature.v]))
return completeSignature
}
}
extension Utilities {
/// Various units used in Ethereum ecosystem
public enum Units {
case eth
case wei
case Kwei
case Mwei
case Gwei
case Microether
case Finney
public var decimals: Int {
get {
switch self {
case .eth:
return 18
case .wei:
return 0
case .Kwei:
return 3
case .Mwei:
return 6
case .Gwei:
return 9
case .Microether:
return 12
case .Finney:
return 15
}
}
}
}
}