Skip to content

Commit 17af937

Browse files
chore: updated docs for randomBytes + minor refactoring
1 parent 468beaa commit 17af937

File tree

1 file changed

+8
-10
lines changed

1 file changed

+8
-10
lines changed

Sources/Web3Core/Utility/Data+Extension.swift

Lines changed: 8 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -42,25 +42,23 @@ public extension Data {
4242
}
4343

4444
/**
45-
Generates an array of random bytes with the specified length.
46-
45+
Generates an array of random bytes of the specified length.
46+
This function uses `SecRandomCopyBytes` to generate random bytes returning it as a `Data` object.
47+
If an error occurs during random bytes generation, the function returns `nil`.
48+
Error occurs only if `SecRandomCopyBytes` returns status that is not `errSecSuccess`.
49+
See [all status codes](https://developer.apple.com/documentation/security/1542001-security_framework_result_codes) for possible error reasons.
50+
Note: in v4 of web3swift this function will be deprecated and a new implementation will be provided that will throw occured error.
4751
- Parameter length: The number of random bytes to generate.
4852

49-
- Returns: An optional `Data` object containing the generated random bytes, or `nil` if an error occurs during generation.
50-
- Note: This function uses `SecRandomCopyBytes` to generate random bytes and shuffles the resulting array before returning it as a `Data` object. If an error occurs during random bytes generation, the function returns `nil`.
53+
- Returns: optional `Data` object containing the generated random bytes, or `nil` if an error occured during generation.
5154
*/
5255
static func randomBytes(length: Int) -> Data? {
5356
var entropyBytes = [UInt8](repeating: 0, count: length)
54-
5557
let status = SecRandomCopyBytes(kSecRandomDefault, entropyBytes.count, &entropyBytes)
56-
5758
guard status == errSecSuccess else {
5859
return nil
5960
}
60-
61-
let entropyData = entropyBytes.shuffled()
62-
63-
return Data(entropyData)
61+
return Data(entropyBytes)
6462
}
6563

6664
func bitsInRange(_ startingBit: Int, _ length: Int) -> UInt64? { // return max of 8 bytes for simplicity, non-public

0 commit comments

Comments
 (0)