Skip to content

Commit 49f176c

Browse files
author
Fernando Fernandes
committed
Fix signature for POST requests
1 parent 33dd095 commit 49f176c

File tree

2 files changed

+31
-3
lines changed

2 files changed

+31
-3
lines changed

Sources/SwiftTrader/Network/Kucoin/KucoinAPI+Header.swift

Lines changed: 23 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -44,12 +44,18 @@ private extension KucoinAPI {
4444

4545
/// "KC-API-SIGN"
4646
static func setAPISignature(request: inout URLRequest, kucoinAuth: KucoinAuth) throws {
47+
48+
// MARK: HTTP Method
49+
4750
guard let httpMethodString = request.httpMethod else {
4851
throw KucoinAPIError.missingHTTPMethod
4952
}
5053
guard let httpMethod = HTTPMethod(rawValue: httpMethodString) else {
5154
throw KucoinAPIError.unsupportedHTTPMethod
5255
}
56+
57+
// MARK: Path
58+
5359
guard let url = request.url else {
5460
throw KucoinAPIError.missingURL(url: request.url)
5561
}
@@ -62,7 +68,23 @@ private extension KucoinAPI {
6268
} else {
6369
finalPath = url.path
6470
}
65-
let signature = try createSignature(for: httpMethod, path: finalPath, secret: kucoinAuth.apiSecret)
71+
72+
// MARK: Body
73+
74+
let body: String
75+
if let bodyData = request.httpBody,
76+
let bodyString = String(data: bodyData, encoding: .utf8) {
77+
body = bodyString
78+
} else {
79+
body = ""
80+
}
81+
82+
let signature = try createSignature(
83+
for: httpMethod,
84+
path: finalPath,
85+
body: body,
86+
secret: kucoinAuth.apiSecret
87+
)
6688
request.setValue(signature, forHTTPHeaderField: KucoinAPI.HeaderField.apiSign)
6789
}
6890

Sources/SwiftTrader/Network/Kucoin/KucoinAPI+Signature.swift

Lines changed: 8 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -13,19 +13,25 @@ public extension KucoinAPI {
1313

1414
/// Creates and returns the signature for the `KC-API-SIGN` header field.
1515
///
16+
/// From Kucoin documentation *"Use API-Secret to encrypt the prehash string
17+
/// {timestamp+method+endpoint+body} with sha256 HMAC. The request body
18+
/// is a JSON string and need to be the same with the parameters passed by the API.
19+
/// After that, use base64-encode to encrypt the result..."*
20+
///
1621
/// https://docs.kucoin.com/futures/#authentication
1722
/// - Parameters:
1823
/// - method: `HTTPMethod`. E.g.: `GET`, `POST.`
1924
/// - path: Endpoint path including query parameters, if any. E.g.: *"/api/v1/account-overview?currency=USDT"*
25+
/// - body: The request body as a `JSON` `String`. For `GET`, `DELETE` requests, the body is "".
2026
/// - secret: The API secret.
2127
/// - Returns: `Base64` encoded signature.
22-
static func createSignature(for method: HTTPMethod, path: String, secret: String) throws -> String {
28+
static func createSignature(for method: HTTPMethod, path: String, body: String, secret: String) throws -> String {
2329
guard let secretData = secret.data(using: .utf8) else {
2430
throw KucoinAPIError.stringToDataFailed(string: secret)
2531
}
2632
let key = SymmetricKey(data: secretData)
2733
let timestamp = Date().timestampMilliseconds
28-
let stringToSign = "\(timestamp)" + HTTPMethod.GET.rawValue + path
34+
let stringToSign = "\(timestamp)" + method.rawValue + path + body
2935
guard let stringToSignData = stringToSign.data(using: .utf8) else {
3036
throw KucoinAPIError.stringToDataFailed(string: stringToSign)
3137
}

0 commit comments

Comments
 (0)