-
-
Notifications
You must be signed in to change notification settings - Fork 24
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #469 from Adamant-im/develop
Release 3.6.0
- Loading branch information
Showing
154 changed files
with
2,752 additions
and
822 deletions.
There are no files selected for viewing
Large diffs are not rendered by default.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,108 @@ | ||
// | ||
// ADM+Decode.swift | ||
// Adamant | ||
// | ||
// Created by Stanislav Jelezoglo on 30.01.2024. | ||
// Copyright © 2024 Adamant. All rights reserved. | ||
// | ||
|
||
import Foundation | ||
|
||
struct JSONCodingKeys: CodingKey { | ||
var stringValue: String | ||
|
||
init?(stringValue: String) { | ||
self.stringValue = stringValue | ||
} | ||
|
||
var intValue: Int? | ||
|
||
init?(intValue: Int) { | ||
self.init(stringValue: "\(intValue)") | ||
self.intValue = intValue | ||
} | ||
} | ||
|
||
extension KeyedDecodingContainer { | ||
func decode(forKey key: K) throws -> Data { | ||
if let stringValue = try? decode(String.self, forKey: key) { | ||
return Data(stringValue.utf8) | ||
} else if (try? decode(Dictionary<String, Any>.self, forKey: key)) != nil { | ||
let container = try self.nestedContainer(keyedBy: JSONCodingKeys.self, forKey: key) | ||
let dictionary = try container.decode([String: Any].self) | ||
return try JSONSerialization.data(withJSONObject: dictionary, options: []) | ||
} | ||
|
||
return Data() | ||
} | ||
|
||
func decode(_ type: Dictionary<String, Any>.Type, forKey key: K) throws -> [String: Any] { | ||
let container = try self.nestedContainer(keyedBy: JSONCodingKeys.self, forKey: key) | ||
return try container.decode(type) | ||
} | ||
|
||
func decodeIfPresent(_ type: Dictionary<String, Any>.Type, forKey key: K) throws -> [String: Any]? { | ||
guard contains(key) else { | ||
return nil | ||
} | ||
return try decode(type, forKey: key) | ||
} | ||
|
||
func decode(_ type: Array<Any>.Type, forKey key: K) throws -> [Any] { | ||
var container = try self.nestedUnkeyedContainer(forKey: key) | ||
return try container.decode(type) | ||
} | ||
|
||
func decodeIfPresent(_ type: Array<Any>.Type, forKey key: K) throws -> [Any]? { | ||
guard contains(key) else { | ||
return nil | ||
} | ||
return try decode(type, forKey: key) | ||
} | ||
|
||
func decode(_ type: Dictionary<String, Any>.Type) throws -> [String: Any] { | ||
var dictionary: [String: Any] = [:] | ||
|
||
for key in allKeys { | ||
if let boolValue = try? decode(Bool.self, forKey: key) { | ||
dictionary[key.stringValue] = boolValue | ||
} else if let stringValue = try? decode(String.self, forKey: key) { | ||
dictionary[key.stringValue] = stringValue | ||
} else if let intValue = try? decode(Int.self, forKey: key) { | ||
dictionary[key.stringValue] = intValue | ||
} else if let doubleValue = try? decode(Double.self, forKey: key) { | ||
dictionary[key.stringValue] = doubleValue | ||
} else if let nestedDictionary = try? decode(Dictionary<String, Any>.self, forKey: key) { | ||
dictionary[key.stringValue] = nestedDictionary | ||
} else if let nestedArray = try? decode(Array<Any>.self, forKey: key) { | ||
dictionary[key.stringValue] = nestedArray | ||
} | ||
} | ||
return dictionary | ||
} | ||
} | ||
|
||
extension UnkeyedDecodingContainer { | ||
mutating func decode(_ type: Array<Any>.Type) throws -> [Any] { | ||
var array: [Any] = [] | ||
while isAtEnd == false { | ||
if let value = try? decode(Bool.self) { | ||
array.append(value) | ||
} else if let value = try? decode(Double.self) { | ||
array.append(value) | ||
} else if let value = try? decode(String.self) { | ||
array.append(value) | ||
} else if let nestedDictionary = try? decode(Dictionary<String, Any>.self) { | ||
array.append(nestedDictionary) | ||
} else if let nestedArray = try? decode(Array<Any>.self) { | ||
array.append(nestedArray) | ||
} | ||
} | ||
return array | ||
} | ||
|
||
mutating func decode(_ type: Dictionary<String, Any>.Type) throws -> [String: Any] { | ||
let nestedContainer = try self.nestedContainer(keyedBy: JSONCodingKeys.self) | ||
return try nestedContainer.decode(type) | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,29 @@ | ||
// | ||
// RPCResponseModel.swift | ||
// Adamant | ||
// | ||
// Created by Stanislav Jelezoglo on 29.01.2024. | ||
// Copyright © 2024 Adamant. All rights reserved. | ||
// | ||
|
||
import Foundation | ||
|
||
struct RPCResponseModel: Codable { | ||
let id: String | ||
let result: Data | ||
|
||
private enum CodingKeys: String, CodingKey { | ||
case id | ||
case result | ||
} | ||
|
||
init(from decoder: Decoder) throws { | ||
let container = try decoder.container(keyedBy: CodingKeys.self) | ||
id = try container.decode(String.self, forKey: .id) | ||
result = try container.decode(forKey: .result) | ||
} | ||
|
||
func serialize<Response: Decodable>() -> Response? { | ||
try? JSONDecoder().decode(Response.self, from: result) | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,54 @@ | ||
// | ||
// RpcRequestModel.swift | ||
// Adamant | ||
// | ||
// Created by Stanislav Jelezoglo on 31.01.2024. | ||
// Copyright © 2024 Adamant. All rights reserved. | ||
// | ||
|
||
import Foundation | ||
|
||
struct RpcRequest: Encodable { | ||
let method: String | ||
let id: String | ||
let params: [Parameter] | ||
let jsonrpc: String = "2.0" | ||
|
||
init(method: String, id: String, params: [Parameter]) { | ||
self.method = method | ||
self.id = id | ||
self.params = params | ||
} | ||
|
||
init(method: String, params: [Parameter]) { | ||
self.method = method | ||
self.id = method | ||
self.params = params | ||
} | ||
|
||
init(method: String) { | ||
self.method = method | ||
self.id = method | ||
self.params = [] | ||
} | ||
} | ||
|
||
extension RpcRequest { | ||
enum Parameter { | ||
case string(String) | ||
case bool(Bool) | ||
} | ||
} | ||
|
||
extension RpcRequest.Parameter: Encodable { | ||
func encode(to encoder: Encoder) throws { | ||
var container = encoder.singleValueContainer() | ||
|
||
switch self { | ||
case let .string(value): | ||
try container.encode(value) | ||
case let .bool(value): | ||
try container.encode(value) | ||
} | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.