Skip to content

Commit

Permalink
IOS-3271 Validate the key
Browse files Browse the repository at this point in the history
  • Loading branch information
tureck1y committed Mar 23, 2023
1 parent 335d7e4 commit 29908fe
Show file tree
Hide file tree
Showing 2 changed files with 27 additions and 8 deletions.
22 changes: 14 additions & 8 deletions TangemSdk/TangemSdk/Crypto/HDWallet/BIP32/ExtendedPublicKey.swift
Original file line number Diff line number Diff line change
Expand Up @@ -163,18 +163,24 @@ extension ExtendedPublicKey: Decodable {

let publicKey = try container.decode(Data.self, forKey: .publicKey)
let chainCode = try container.decode(Data.self, forKey: .chainCode)
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)

if let depth, let parentFingerprint, let childNumber {
try self.init(publicKey: publicKey,
chainCode: chainCode,
depth: depth,
parentFingerprint: parentFingerprint,
childNumber: childNumber)
return
}

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 {
if depth == nil, parentFingerprint == nil, childNumber == nil {
self.init(publicKey: publicKey, chainCode: chainCode)
return
}

try self.init(publicKey: publicKey,
chainCode: chainCode,
depth: depth,
parentFingerprint: parentFingerprint,
childNumber: childNumber)
throw TangemSdkError.decodingFailed("Missing data in the ExtendedPublicKey")
}
}
13 changes: 13 additions & 0 deletions TangemSdk/TangemSdkTests/JSONRPCTests.swift
Original file line number Diff line number Diff line change
Expand Up @@ -36,6 +36,19 @@ class JSONRPCTests: XCTestCase {
XCTAssertEqual(decoded.parentFingerprint.hexString, "00000000")
}

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

XCTAssertThrowsError(try JSONDecoder.tangemSdkDecoder.decode(ExtendedPublicKey.self, from: json.data(using: .utf8)!))
}

func testDecodeExtendedPublicKey() throws {
let json =
"""
Expand Down

0 comments on commit 29908fe

Please sign in to comment.