1
+
1
2
import CryptoKit
2
3
import Foundation
3
4
@@ -11,22 +12,28 @@ enum StamperError: Error {
11
12
@objc public class NativeTEKStamperImpl : NSObject {
12
13
// TODO: we probably want to keep this longer term somewhere, because the RN session manager will
13
14
// hold on to the bundle and try to recreate a session if a user is still logged in
14
- var ephemeralPrivateKey : P256 . KeyAgreement . PrivateKey ? = nil ;
15
+ var ephemeralPrivateKey : P256 . KeyAgreement . PrivateKey ? = nil
15
16
16
17
// These can be ephemeral and held in memory because the session manager will handle re-authenticating
17
18
var apiPublicKey : P256 . Signing . PublicKey ? = nil ;
18
19
var apiPrivateKey : P256 . Signing . PrivateKey ? = nil ;
19
20
20
21
@objc public func create( ) async throws -> NSString {
21
- if ( ephemeralPrivateKey == nil ) {
22
- ephemeralPrivateKey = P256 . KeyAgreement. PrivateKey ( )
23
- }
24
- let targetPublicKey = try ephemeralPrivateKey!. publicKey. toString ( representation: . x963)
22
+ var _ephemeralPrivateKey : P256 . KeyAgreement . PrivateKey ? = PrivateKeyChainUtilities . getPrivateKeyFromKeychain ( )
25
23
24
+ if ( _ephemeralPrivateKey == nil ) {
25
+ _ephemeralPrivateKey = P256 . KeyAgreement. PrivateKey ( )
26
+ PrivateKeyChainUtilities . savePrivateKeyToKeychain ( _ephemeralPrivateKey!)
27
+ }
28
+
29
+ let targetPublicKey = try _ephemeralPrivateKey!. publicKey. toString ( representation: . x963)
30
+ ephemeralPrivateKey = _ephemeralPrivateKey
31
+
26
32
return NSString ( string: targetPublicKey)
27
33
}
28
34
29
35
@objc public func clear( ) {
36
+ PrivateKeyChainUtilities . deletePrivateKeyFromKeychain ( )
30
37
ephemeralPrivateKey = nil
31
38
apiPublicKey = nil
32
39
apiPrivateKey = nil
@@ -41,18 +48,17 @@ enum StamperError: Error {
41
48
}
42
49
}
43
50
44
- @objc public func injectCredentialBundle( bundle: NSString ) async throws -> ObjCBool {
45
- if let ephemeralPrivateKey = ephemeralPrivateKey {
46
- let ( bundlePrivateKey, bundlePublicKey) = try AuthManager . decryptBundle ( encryptedBundle: bundle as String , ephemeralPrivateKey: ephemeralPrivateKey)
51
+ @objc public func injectCredentialBundle( bundle: NSString ) async throws -> ObjCBool {
52
+ if let ephemeralPrivateKey = ephemeralPrivateKey {
53
+ let ( bundlePrivateKey, bundlePublicKey) = try AuthManager . decryptBundle ( encryptedBundle: bundle as String , ephemeralPrivateKey: ephemeralPrivateKey)
54
+ apiPublicKey = bundlePublicKey
55
+ apiPrivateKey = bundlePrivateKey
47
56
48
- apiPublicKey = bundlePublicKey
49
- apiPrivateKey = bundlePrivateKey
50
-
51
- return true ;
52
- } else {
53
- throw StamperError . notInitialized
54
- }
55
- }
57
+ return true
58
+ } else {
59
+ throw StamperError . notInitialized
60
+ }
61
+ }
56
62
57
63
// TODO: we should use the turnkey stamper for all of this, but we need it published as a pod
58
64
// and it shouldn't require use_frameworks!
@@ -70,7 +76,6 @@ enum StamperError: Error {
70
76
71
77
let signatureHex = signature. derRepresentation. toHexString ( )
72
78
73
- print ( apiPublicKey. compressedRepresentation. toHexString ( ) )
74
79
let stamp : [ String : Any ] = [
75
80
" publicKey " : apiPublicKey. compressedRepresentation. toHexString ( ) ,
76
81
" scheme " : " SIGNATURE_SCHEME_TK_API_P256 " ,
0 commit comments