Skip to content

Commit

Permalink
fix parsing keystore definition version
Browse files Browse the repository at this point in the history
  • Loading branch information
ERussel committed Nov 9, 2021
1 parent cab677f commit d2312b9
Show file tree
Hide file tree
Showing 6 changed files with 45 additions and 5 deletions.
4 changes: 4 additions & 0 deletions Example/SubstrateSdk.xcodeproj/project.pbxproj
Original file line number Diff line number Diff line change
Expand Up @@ -54,6 +54,7 @@
84708302272683C6003408E8 /* Images.xcassets in Resources */ = {isa = PBXBuildFile; fileRef = 847082FD272683C6003408E8 /* Images.xcassets */; };
84708303272683C6003408E8 /* AppDelegate.swift in Sources */ = {isa = PBXBuildFile; fileRef = 847082FE272683C6003408E8 /* AppDelegate.swift */; };
84784105273569A000401EFA /* keystore-ethereum.json in Resources */ = {isa = PBXBuildFile; fileRef = 84784104273569A000401EFA /* keystore-ethereum.json */; };
849CFADE27394E5D0096D201 /* keystore-ethereum-int-version.json in Resources */ = {isa = PBXBuildFile; fileRef = 849CFADD27394E5D0096D201 /* keystore-ethereum-int-version.json */; };
849E0A2125C81DB500B33506 /* FixedArrayParserTests.swift in Sources */ = {isa = PBXBuildFile; fileRef = 849E0A2025C81DB500B33506 /* FixedArrayParserTests.swift */; };
849E0A7425C9458F00B33506 /* StructNodeFactoryTests.swift in Sources */ = {isa = PBXBuildFile; fileRef = 849E0A7325C9458F00B33506 /* StructNodeFactoryTests.swift */; };
849E0A7A25C94E3F00B33506 /* EnumMappingFactoryTests.swift in Sources */ = {isa = PBXBuildFile; fileRef = 849E0A7925C94E3F00B33506 /* EnumMappingFactoryTests.swift */; };
Expand Down Expand Up @@ -182,6 +183,7 @@
847082FD272683C6003408E8 /* Images.xcassets */ = {isa = PBXFileReference; lastKnownFileType = folder.assetcatalog; path = Images.xcassets; sourceTree = "<group>"; };
847082FE272683C6003408E8 /* AppDelegate.swift */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.swift; path = AppDelegate.swift; sourceTree = "<group>"; };
84784104273569A000401EFA /* keystore-ethereum.json */ = {isa = PBXFileReference; lastKnownFileType = text.json; path = "keystore-ethereum.json"; sourceTree = "<group>"; };
849CFADD27394E5D0096D201 /* keystore-ethereum-int-version.json */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = text.json; path = "keystore-ethereum-int-version.json"; sourceTree = "<group>"; };
849E0A2025C81DB500B33506 /* FixedArrayParserTests.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = FixedArrayParserTests.swift; sourceTree = "<group>"; };
849E0A7325C9458F00B33506 /* StructNodeFactoryTests.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = StructNodeFactoryTests.swift; sourceTree = "<group>"; };
849E0A7925C94E3F00B33506 /* EnumMappingFactoryTests.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = EnumMappingFactoryTests.swift; sourceTree = "<group>"; };
Expand Down Expand Up @@ -490,6 +492,7 @@
84CB471F24DBE7BD00837E11 /* Keystore */ = {
isa = PBXGroup;
children = (
849CFADD27394E5D0096D201 /* keystore-ethereum-int-version.json */,
84CB472024DBE7E100837E11 /* keystore-sr25519.json */,
84A0DE2D24DBF3F300F436A8 /* keystore-ed25519.json */,
84A0DE2F24DBF40900F436A8 /* keystore-ecdsa.json */,
Expand Down Expand Up @@ -722,6 +725,7 @@
84452C0F25D2D5C300F47EC5 /* polkadot-metadata in Resources */,
8429C82725C434ED0086D7F6 /* westend.json in Resources */,
84A0DE3124DBF49400F436A8 /* keystore-ed25519.json in Resources */,
849CFADE27394E5D0096D201 /* keystore-ethereum-int-version.json in Resources */,
AE4B521826CE127200A66D2C /* BIP32HDKD.json in Resources */,
84F4394225DBAAA200AEDA56 /* test-metadata in Resources */,
8429C81D25C434E70086D7F6 /* kusama.json in Resources */,
Expand Down
25 changes: 25 additions & 0 deletions SubstrateSdk/Classes/Keystore/KeystoreDefinition.swift
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,12 @@ public struct KeystoreDefinition: Codable {
}

public struct KeystoreEncoding: Codable {
enum CodingKeys: String, CodingKey {
case content
case type
case version
}

public let content: [String]
public let type: [String]
public let version: String
Expand All @@ -27,6 +33,25 @@ public struct KeystoreEncoding: Codable {
self.type = type
self.version = version
}

public init(from decoder: Decoder) throws {
let container = try decoder.container(keyedBy: CodingKeys.self)

content = try container.decode([String].self, forKey: .content)
type = try container.decode([String].self, forKey: .type)

if let stringVersion = try? container.decode(String.self, forKey: .version) {
version = stringVersion
} else if let intVersion = try? container.decode(Int.self, forKey: .version) {
version = String(intVersion)
} else {
throw DecodingError.dataCorruptedError(
forKey: .version,
in: container,
debugDescription: "Unexpected value type"
)
}
}
}

public struct KeystoreMeta: Codable {
Expand Down
15 changes: 12 additions & 3 deletions Tests/Keystore/KeystoreExtractorTests.swift
Original file line number Diff line number Diff line change
Expand Up @@ -107,8 +107,18 @@ class KeystoreExtractorTests: XCTestCase {
}

func testEthereumJson() {
performEthereumTest(for: "keystore-ethereum", password: "Moonriver")
}

func testEthereumJsonWithIntVersion() {
performEthereumTest(for: "keystore-ethereum-int-version", password: "Moonriver")
}

// MARK: Private

private func performEthereumTest(for filename: String, password: String) {
guard let url = Bundle(for: KeystoreExtractorTests.self)
.url(forResource: "keystore-ethereum", withExtension: "json") else {
.url(forResource: filename, withExtension: "json") else {
XCTFail("Can't find resource")
return
}
Expand All @@ -119,8 +129,7 @@ class KeystoreExtractorTests: XCTestCase {

let extractor = KeystoreExtractor()

let keystoreData = try extractor.extractFromDefinition(keystore,
password: "Moonriver")
let keystoreData = try extractor.extractFromDefinition(keystore, password: password)

let rawPrivateKey = keystoreData.secretKeyData
let keypair = try SECKeyFactory().derive(fromPrivateKey: SECPrivateKey(rawData: rawPrivateKey))
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
{"encoded":"X12+UAecNf4z2bdJSLQXWpOd5d3b9M9tNwYF12eiCqwAgAAAAQAAAAgAAAB9301H/76yPhz9X7PLAIsTRo0iFP8oNf/Ly4ha5tkFazLfGzE7WxQm7QgDU8lHiIs5QxDrEsdnR/+omRlhJD2S9PwvDROMAGCixgL/7eVIZUkqxWYSsgTHMi79AH0h8WbKoubmZCx7+FMsw2QQw7mOlvVpiy94tcfr5XPzXDg=","encoding":{"content":["pkcs8","ethereum"],"type":["scrypt","xsalsa20-poly1305"],"version":3},"address":"0x037696315becb7727aa241231266c96f4bf9387804614cb02b2c4bb5d1619a7d9d","meta":{"genesisHash":"0x401a1f9dca3da46f5c4091016c8a2f26dcea05865116b286f60f668207d1474b","isHardware":false,"name":"mooriver-account","tags":[],"whenCreated":1636118632081}}
3 changes: 2 additions & 1 deletion Tests/Runtime/Parser/ComponentsParserTests.swift
Original file line number Diff line number Diff line change
Expand Up @@ -36,7 +36,8 @@ class ComponentsParserTests: XCTestCase {
let result = parser.parse(json: .stringValue(type))

if let expectedResult = expectedResult {
XCTAssertEqual(result?.map({ $0.stringValue }), expectedResult)
let actualResult = result?.compactMap { $0.stringValue }
XCTAssertEqual(actualResult, expectedResult)
} else {
XCTAssertNil(result)
}
Expand Down
2 changes: 1 addition & 1 deletion Tests/Runtime/Parser/RegexParserTests.swift
Original file line number Diff line number Diff line change
Expand Up @@ -95,7 +95,7 @@ class RegexParserTests: XCTestCase {
let result = parser.parse(json: .stringValue(type))

if let expectedResult = expectedResult {
XCTAssertEqual(result?.map({ $0.stringValue }), expectedResult)
XCTAssertEqual(result?.compactMap({ $0.stringValue }), expectedResult)
} else {
XCTAssertNil(result)
}
Expand Down

0 comments on commit d2312b9

Please sign in to comment.