Skip to content

Commit

Permalink
IOS-3271 Fix decoding
Browse files Browse the repository at this point in the history
  • Loading branch information
tureck1y committed Mar 23, 2023
1 parent d251dd1 commit 335d7e4
Show file tree
Hide file tree
Showing 2 changed files with 70 additions and 6 deletions.
39 changes: 33 additions & 6 deletions TangemSdk/TangemSdk/Crypto/HDWallet/BIP32/ExtendedPublicKey.swift
Original file line number Diff line number Diff line change
Expand Up @@ -10,13 +10,13 @@ import Foundation
import CryptoKit

@available(iOS 13.0, *)
public struct ExtendedPublicKey: Equatable, Hashable, JSONStringConvertible, Codable {
public struct ExtendedPublicKey: Equatable, Hashable, JSONStringConvertible {
public let publicKey: Data
public let chainCode: Data

public private(set) var depth: Int = 0
public private(set) var parentFingerprint: Data = Data(hexString: "0x00000000")
public private(set) var childNumber: UInt32 = 0
public let depth: Int
public let parentFingerprint: Data
public let childNumber: UInt32

public init(publicKey: Data, chainCode: Data, depth: Int, parentFingerprint: Data, childNumber: UInt32) throws {
self.depth = depth
Expand All @@ -35,8 +35,11 @@ public struct ExtendedPublicKey: Equatable, Hashable, JSONStringConvertible, Cod
/// - publicKey: publicKey
/// - chainCode: chainCode
public init(publicKey: Data, chainCode: Data) {
self.publicKey = publicKey
self.chainCode = chainCode
try! self.init(publicKey: publicKey,
chainCode: chainCode,
depth: 0,
parentFingerprint: Data(hexString: "0x00000000"),
childNumber: 0)
}

/// This function performs CKDpub((Kpar, cpar), i) → (Ki, ci) to compute a child extended public key from the parent extended public key.
Expand Down Expand Up @@ -151,3 +154,27 @@ extension ExtendedPublicKey: ExtendedKeySerializable {
return resultString
}
}

// MARK: - Decodable
@available(iOS 13.0, *)
extension ExtendedPublicKey: Decodable {
public init(from decoder: Decoder) throws {
let container = try decoder.container(keyedBy: CodingKeys.self)

let publicKey = try container.decode(Data.self, forKey: .publicKey)
let chainCode = try container.decode(Data.self, forKey: .chainCode)

guard let depth = try container.decodeIfPresent(Int.self, forKey: .depth),
let parentFingerprint = try container.decodeIfPresent(Data.self, forKey: .parentFingerprint),
let childNumber = try container.decodeIfPresent(UInt32.self, forKey: .childNumber) else {
self.init(publicKey: publicKey, chainCode: chainCode)
return
}

try self.init(publicKey: publicKey,
chainCode: chainCode,
depth: depth,
parentFingerprint: parentFingerprint,
childNumber: childNumber)
}
}
37 changes: 37 additions & 0 deletions TangemSdk/TangemSdkTests/JSONRPCTests.swift
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,43 @@ class JSONRPCTests: XCTestCase {
XCTAssertNotNil(data)
return try! JSONDecoder.tangemSdkDecoder.decode(Card.self, from: data!)
}

func testDecodeMasterExtendedPublicKey() throws {
let json =
"""
{
"publicKey": "0200300397571D99D41BB2A577E2CBE495C04AC5B9A97B7A4ECF999F23CE45E962",
"chainCode": "537F7361175B150732E17508066982B42D9FB1F8239C4D7BFC490088C83A8BBB",
}
"""

let decoded = try JSONDecoder.tangemSdkDecoder.decode(ExtendedPublicKey.self, from: json.data(using: .utf8)!)
XCTAssertEqual(decoded.publicKey.hexString, "0200300397571D99D41BB2A577E2CBE495C04AC5B9A97B7A4ECF999F23CE45E962")
XCTAssertEqual(decoded.chainCode.hexString, "537F7361175B150732E17508066982B42D9FB1F8239C4D7BFC490088C83A8BBB")
XCTAssertEqual(decoded.depth, 0)
XCTAssertEqual(decoded.childNumber, 0)
XCTAssertEqual(decoded.parentFingerprint.hexString, "00000000")
}

func testDecodeExtendedPublicKey() throws {
let json =
"""
{
"publicKey": "0200300397571D99D41BB2A577E2CBE495C04AC5B9A97B7A4ECF999F23CE45E962",
"chainCode": "537F7361175B150732E17508066982B42D9FB1F8239C4D7BFC490088C83A8BBB",
"depth" : 1,
"parentFingerprint" : "00000001",
"childNumber" : 2
}
"""

let decoded = try JSONDecoder.tangemSdkDecoder.decode(ExtendedPublicKey.self, from: json.data(using: .utf8)!)
XCTAssertEqual(decoded.publicKey.hexString, "0200300397571D99D41BB2A577E2CBE495C04AC5B9A97B7A4ECF999F23CE45E962")
XCTAssertEqual(decoded.chainCode.hexString, "537F7361175B150732E17508066982B42D9FB1F8239C4D7BFC490088C83A8BBB")
XCTAssertEqual(decoded.depth, 1)
XCTAssertEqual(decoded.childNumber, 2)
XCTAssertEqual(decoded.parentFingerprint.hexString, "00000001")
}

func testJsonRPCRequestParse() {
let json = "{\"jsonrpc\": \"2.0\", \"method\": \"subtract\", \"params\": {\"subtrahend\": 23, \"minuend\": 42}, \"id\": 3}"
Expand Down

0 comments on commit 335d7e4

Please sign in to comment.