-
Notifications
You must be signed in to change notification settings - Fork 0
/
EosioSoftkeySignatureProvider.swift
112 lines (103 loc) · 5.83 KB
/
EosioSoftkeySignatureProvider.swift
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
//
// EosioSwiftSoftkeySignatureProvider.swift
// EosioSwiftSoftkeySignatureProvider
//
// Created by Farid Rahmani on 3/14/19.
// Copyright (c) 2017-2019 block.one and its contributors. All rights reserved.
//
import Foundation
import EosioSwift
#if SWIFT_PACKAGE
import EosioSwiftEcc
#endif
/// Example signature provider for EOSIO SDK for Swift for signing transactions using in-memory K1 private keys. This
/// signature provider implementation stores keys in memory and is therefore not secure. Use only for development purposes.
public final class EosioSoftkeySignatureProvider: EosioSignatureProviderProtocol {
private struct Key {
let eosioPublicKey: String
let uncompressedPublicKey: Data
let compressedPublicKey: Data
let privateKey: Data
}
private var keyPairs = [Data: Key]()
private let lock = String()
/// Initializes the signature provider using the private keys in the given array.
///
/// - Parameter privateKeys: Array of private keys in `String` format.
/// - Throws: Throws an error if any of the keys in the given `privateKeys` array is not valid.
public init(privateKeys: [String]) throws {
for privateKey in privateKeys {
let (_, version, _) = try privateKey.eosioComponents()
if version != "K1" {
throw EosioError(EosioErrorCode.keyManagementError, reason: "Unsupported key type. Only K1 key types are supported in this version of the library. Key: \(privateKey) Type: \(version)")
}
let privateKeyData = try Data(eosioPrivateKey: privateKey)
let publicKeyData = try EccRecoverKey.recoverPublicKey(privateKey: privateKeyData, curve: .k1)
guard let compressedPublicKey = publicKeyData.toCompressedPublicKey else {
throw EosioError(EosioErrorCode.keyManagementError, reason: "Cannot compress key \(publicKeyData.hex)")
}
let eosioPublicKey = compressedPublicKey.toEosioK1PublicKey
let key = Key(eosioPublicKey: eosioPublicKey, uncompressedPublicKey: publicKeyData, compressedPublicKey: compressedPublicKey, privateKey: privateKeyData)
keyPairs[compressedPublicKey] = key
}
}
/// Asynchronous method signing a transaction request. Invoked by an `EosioTransaction` during the signing process.
///
/// - Parameters:
/// - request: An `EosioTransactionSignatureRequest` struct (as defined in the `EosioSwift` library).
/// - prompt: Prompt for biometric challenge if required. Ignored as softkey provider does no biometric auth.
/// - completion: Calls the completion with an `EosioTransactionSignatureResponse` struct (as defined in the `EosioSwift` library).
public func signTransaction(request: EosioTransactionSignatureRequest,
prompt: String,
completion: @escaping (EosioTransactionSignatureResponse) -> Void) {
self.signTransaction(request: request, completion: completion)
}
/// Asynchronous method signing a transaction request. Invoked by an `EosioTransaction` during the signing process.
///
/// - Parameters:
/// - request: An `EosioTransactionSignatureRequest` struct (as defined in the `EosioSwift` library).
/// - completion: Calls the completion with an `EosioTransactionSignatureResponse` struct (as defined in the `EosioSwift` library).
public func signTransaction(request: EosioTransactionSignatureRequest, completion: @escaping (EosioTransactionSignatureResponse) -> Void) {
var response = EosioTransactionSignatureResponse()
do {
var signatures = [String]()
for eosioPublicKey in request.publicKeys {
let compressedPublicKey = try Data(eosioPublicKey: eosioPublicKey)
objc_sync_enter(lock)
guard let key = keyPairs[compressedPublicKey] else {
response.error = EosioError(.keyManagementError, reason: "No private key available for \(eosioPublicKey)")
return completion(response)
}
objc_sync_exit(lock)
let chainIdData = try Data(hex: request.chainId)
var contextFreeDataHash = Data(repeating: 0, count: 32)
if request.serializedContextFreeData.count > 0 {
contextFreeDataHash = request.serializedContextFreeData.sha256
}
let data = try EosioEccSign.signWithK1(publicKey: key.uncompressedPublicKey, privateKey: key.privateKey, data: chainIdData + request.serializedTransaction + contextFreeDataHash)
signatures.append(data.toEosioK1Signature)
}
var signedTransaction = EosioTransactionSignatureResponse.SignedTransaction()
signedTransaction.signatures = signatures
signedTransaction.serializedTransaction = request.serializedTransaction
signedTransaction.serializedContextFreeData = request.serializedContextFreeData
response.signedTransaction = signedTransaction
completion(response)
} catch {
response.error = error as? EosioError
completion(response)
}
}
/// Asynchronous method that provides available public keys to the `EosioTransaction` during the signing preparation process.
///
/// - Parameter completion: Calls the completion with an `EosioAvailableKeysResponse` stuct containing an optional array of available public keys in `String` format.
public func getAvailableKeys(completion: @escaping (EosioAvailableKeysResponse) -> Void) {
var response = EosioAvailableKeysResponse()
objc_sync_enter(lock)
response.keys = Array(keyPairs.values).compactMap({ (key) -> String? in
return key.eosioPublicKey
})
objc_sync_exit(lock)
completion(response)
}
}