Skip to content

Commit

Permalink
IOS-3291 Get entropy command
Browse files Browse the repository at this point in the history
  • Loading branch information
tureck1y committed Mar 27, 2023
1 parent 1ebe14f commit 358ee5b
Show file tree
Hide file tree
Showing 5 changed files with 69 additions and 1 deletion.
6 changes: 6 additions & 0 deletions Example/TangemSdkExample/AppModel.swift
Original file line number Diff line number Diff line change
Expand Up @@ -579,6 +579,10 @@ extension AppModel {
func resetToFactory() {
tangemSdk.startSession(with: ResetToFactorySettingsTask(), completion: handleCompletion)
}

func getEntropy() {
tangemSdk.startSession(with: GetEntropyCommand(), completion: handleCompletion)
}
}

//MARK:- Json RPC
Expand Down Expand Up @@ -674,6 +678,7 @@ extension AppModel {
case personalize
case resetBackup
case resetToFactory
case getEntropy
}

private func chooseMethod(walletPublicKey: Data? = nil) {
Expand Down Expand Up @@ -706,6 +711,7 @@ extension AppModel {
case .personalize: personalize()
case .resetBackup: resetBackup()
case .resetToFactory: resetToFactory()
case .getEntropy: getEntropy()
}
}
}
Expand Down
4 changes: 4 additions & 0 deletions TangemSdk/TangemSdk.xcodeproj/project.pbxproj
Original file line number Diff line number Diff line change
Expand Up @@ -318,6 +318,7 @@
DC59CB0E29AF70C700EC14E1 /* Mnemonic.swift in Sources */ = {isa = PBXBuildFile; fileRef = DC59CB0D29AF70C700EC14E1 /* Mnemonic.swift */; };
DC8B0E3F286F221D009D64F7 /* BiometricsUtil.swift in Sources */ = {isa = PBXBuildFile; fileRef = DC8B0E3E286F221D009D64F7 /* BiometricsUtil.swift */; };
DCA9706628E35EAD0046E62E /* GenerateOTPCommand.swift in Sources */ = {isa = PBXBuildFile; fileRef = DCA9706528E35EAD0046E62E /* GenerateOTPCommand.swift */; };
DCC6A64829D212E8007BA5B7 /* GetEntropyCommand.swift in Sources */ = {isa = PBXBuildFile; fileRef = DCC6A64729D212E8007BA5B7 /* GetEntropyCommand.swift */; };
DCEA3ABC2875AEBA00B0B0DA /* BiometricsStorage.swift in Sources */ = {isa = PBXBuildFile; fileRef = DCEA3ABB2875AEBA00B0B0DA /* BiometricsStorage.swift */; };
DCEA3ABE2875AF0F00B0B0DA /* SecureStorageKey.swift in Sources */ = {isa = PBXBuildFile; fileRef = DCEA3ABD2875AF0F00B0B0DA /* SecureStorageKey.swift */; };
DCFCA17728F5629F0037586C /* FocusableTextField.swift in Sources */ = {isa = PBXBuildFile; fileRef = DCFCA17628F5629F0037586C /* FocusableTextField.swift */; };
Expand Down Expand Up @@ -653,6 +654,7 @@
DC59CB0D29AF70C700EC14E1 /* Mnemonic.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = Mnemonic.swift; sourceTree = "<group>"; };
DC8B0E3E286F221D009D64F7 /* BiometricsUtil.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = BiometricsUtil.swift; sourceTree = "<group>"; };
DCA9706528E35EAD0046E62E /* GenerateOTPCommand.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = GenerateOTPCommand.swift; sourceTree = "<group>"; };
DCC6A64729D212E8007BA5B7 /* GetEntropyCommand.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = GetEntropyCommand.swift; sourceTree = "<group>"; };
DCEA3ABB2875AEBA00B0B0DA /* BiometricsStorage.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = BiometricsStorage.swift; sourceTree = "<group>"; };
DCEA3ABD2875AF0F00B0B0DA /* SecureStorageKey.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = SecureStorageKey.swift; sourceTree = "<group>"; };
DCFCA17628F5629F0037586C /* FocusableTextField.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = FocusableTextField.swift; sourceTree = "<group>"; };
Expand Down Expand Up @@ -998,6 +1000,7 @@
B006971325FFA4420040D203 /* PreflightReadTask.swift */,
5D0631DA2477DAAA0056BF21 /* OpenSessionCommand.swift */,
DCA9706528E35EAD0046E62E /* GenerateOTPCommand.swift */,
DCC6A64729D212E8007BA5B7 /* GetEntropyCommand.swift */,
);
path = Operations;
sourceTree = "<group>";
Expand Down Expand Up @@ -1803,6 +1806,7 @@
DC1244E229BB7B390037BC05 /* WIF.swift in Sources */,
5D539ECB276CDD8600AB8B53 /* DeriveMultipleWalletPublicKeysTask.swift in Sources */,
DA6C752A292682650070EEFD /* LAContext+.swift in Sources */,
DCC6A64829D212E8007BA5B7 /* GetEntropyCommand.swift in Sources */,
5DE43A6026D511CE00ECA36A /* LinkBackupCardsCommand.swift in Sources */,
5D0A6C5D2428CF3C0094FA83 /* Error+.swift in Sources */,
5D270F2726A020DA00D2EDC1 /* WalletDataDeserializer.swift in Sources */,
Expand Down
1 change: 1 addition & 0 deletions TangemSdk/TangemSdk/Common/APDU/Instruction.swift
Original file line number Diff line number Diff line change
Expand Up @@ -41,4 +41,5 @@ public enum Instruction: Byte {
case authorize = 0xD3
case backupReset = 0xEE
case generateOTP = 0xE2
case getEntropy = 0xE7
}
2 changes: 1 addition & 1 deletion TangemSdk/TangemSdk/Operations/GenerateOTPCommand.swift
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@

import Foundation

/// /// Deserialized response from the Tangem card after `GenerateOTPCommand`.
/// Deserialized response from the Tangem card after `GenerateOTPCommand`.
@available(iOS 13.0, *)
public struct GenerateOTPResponse: JSONStringConvertible {
/// Unique Tangem card ID number.
Expand Down
57 changes: 57 additions & 0 deletions TangemSdk/TangemSdk/Operations/GetEntropyCommand.swift
Original file line number Diff line number Diff line change
@@ -0,0 +1,57 @@
//
// GetEntropyCommand.swift
// TangemSdk
//
// Created by Alexander Osokin on 27.03.2023.
// Copyright © 2023 Tangem AG. All rights reserved.
//

import Foundation

/// Deserialized response from the Tangem card after `GetEntropyCommand`.
@available(iOS 13.0, *)
public struct GetEntropyResponse: JSONStringConvertible {
/// Unique Tangem card ID number.
public let cardId: String
/// Random bytes
public let data: Data
}

/// Generate OTP on the card.
@available(iOS 13.0, *)
public class GetEntropyCommand: Command {
public var preflightReadMode: PreflightReadMode { .readCardOnly }

public init() {}

deinit {
Log.debug("GetEntropyCommand deinit")
}

func performPreCheck(_ card: Card) -> TangemSdkError? {
// guard card.firmwareVersion >= .isExternalWalletsAllowed else {
// return TangemSdkError.walletNotFound
// }

return nil
}

func serialize(with environment: SessionEnvironment) throws -> CommandApdu {
let tlvBuilder = try createTlvBuilder(legacyMode: environment.legacyMode)
.append(.pin, value: environment.accessCode.value)
.append(.cardId, value: environment.card?.cardId)

return CommandApdu(.getEntropy, tlv: tlvBuilder.serialize())
}

func deserialize(with environment: SessionEnvironment, from apdu: ResponseApdu) throws -> GetEntropyResponse {
guard let tlv = apdu.getTlvData(encryptionKey: environment.encryptionKey) else {
throw TangemSdkError.deserializeApduFailed
}

let decoder = TlvDecoder(tlv: tlv)

return GetEntropyResponse(cardId: try decoder.decode(.cardId),
data: try decoder.decode(.issuerData))
}
}

0 comments on commit 358ee5b

Please sign in to comment.