-
Notifications
You must be signed in to change notification settings - Fork 41
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 #279 from tangem/IOS-3574_derivedkeys_serialization
Serialize derivedKeys as object
- Loading branch information
Showing
11 changed files
with
83 additions
and
19 deletions.
There are no files selected for viewing
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,64 @@ | ||
// | ||
// DerivedKeys.swift | ||
// TangemSdk | ||
// | ||
// Created by Alexander Osokin on 04.05.2023. | ||
// Copyright © 2023 Tangem AG. All rights reserved. | ||
// | ||
|
||
import Foundation | ||
|
||
// We can't use CodingKeyRepresentable because of iOS 15 version | ||
@available(iOS 13.0, *) | ||
public struct DerivedKeys: JSONStringConvertible { | ||
public private(set) var keys: [DerivationPath:ExtendedPublicKey] | ||
|
||
public init(keys: [DerivationPath : ExtendedPublicKey]) { | ||
self.keys = keys | ||
} | ||
|
||
public subscript(_ path: DerivationPath) -> ExtendedPublicKey? { | ||
get { | ||
return keys[path] | ||
} | ||
set(newValue) { | ||
keys[path] = newValue | ||
} | ||
} | ||
} | ||
|
||
@available(iOS 13.0, *) | ||
extension DerivedKeys: Codable { | ||
public init(from decoder: Decoder) throws { | ||
let container = try decoder.singleValueContainer() | ||
let stringDictionary = try container.decode([String: ExtendedPublicKey].self) | ||
|
||
let keysDictionary: [DerivationPath: ExtendedPublicKey] = try stringDictionary.reduce(into: [:]) { partialResult, item in | ||
let path = try DerivationPath(rawPath: item.key) | ||
partialResult[path] = item.value | ||
} | ||
|
||
self.init(keys: keysDictionary) | ||
} | ||
|
||
public func encode(to encoder: Encoder) throws { | ||
let stringDictionary = keys.reduce(into: [:]) { partialResult, item in | ||
partialResult[item.key.rawPath] = item.value | ||
} | ||
|
||
var container = encoder.singleValueContainer() | ||
try container.encode(stringDictionary) | ||
} | ||
} | ||
|
||
|
||
@available(iOS 13.0, *) | ||
extension DerivedKeys: ExpressibleByDictionaryLiteral { | ||
public init(dictionaryLiteral elements: (DerivationPath, ExtendedPublicKey)...) { | ||
let dictionary = elements.reduce(into: [:]) { partialResult, item in | ||
partialResult[item.0] = item.1 | ||
} | ||
|
||
self.init(keys: dictionary) | ||
} | ||
} |
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
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