Skip to content
This repository has been archived by the owner on Dec 2, 2024. It is now read-only.

Commit

Permalink
Merge pull request #21 from tangem/BSS-120_Sign-tx-interface
Browse files Browse the repository at this point in the history
BSS-120 sign tx interface
  • Loading branch information
Andrew Son authored May 18, 2021
2 parents 90b0204 + 9971c8d commit dfe788a
Showing 1 changed file with 32 additions and 21 deletions.
53 changes: 32 additions & 21 deletions BlockchainSdk/WalletManagers/Ethereum/EthereumWalletManager.swift
Original file line number Diff line number Diff line change
Expand Up @@ -38,6 +38,10 @@ public protocol EthereumGasLoader: AnyObject {
func getGasLimit(amount: Amount, destination: String) -> AnyPublisher<BigUInt, Never>
}

public protocol EthereumTransactionSigner: AnyObject {
func sign(_ transaction: Transaction, signer: TransactionSigner) -> AnyPublisher<String, Error>
}

class EthereumWalletManager: WalletManager {
var txBuilder: EthereumTransactionBuilder!
var networkService: EthereumNetworkService!
Expand Down Expand Up @@ -128,27 +132,15 @@ extension EthereumWalletManager: TransactionSender {
var allowsFeeSelection: Bool { true }

func send(_ transaction: Transaction, signer: TransactionSigner) -> AnyPublisher<Void, Error> {
guard let txForSign = txBuilder.buildForSign(transaction: transaction,
nonce: txCount,
gasLimit: gasLimit ?? getFixedGasLimit(for: transaction.amount)) else {
return Fail(error: WalletError.failedToBuildTx).eraseToAnyPublisher()
}

return signer.sign(hash: txForSign.hash, cardId: wallet.cardId, walletPublicKey: wallet.publicKey)
.tryMap {[unowned self] signature throws -> String in
guard let tx = self.txBuilder.buildForSend(transaction: txForSign.transaction, hash: txForSign.hash, signature: signature) else {
throw WalletError.failedToBuildTx
}
return "0x\(tx.toHexString())"
}
.flatMap {[unowned self] tx -> AnyPublisher<Void, Error> in
self.networkService.send(transaction: tx).map {[unowned self] sendResponse in
var tx = transaction
tx.hash = sendResponse
self.wallet.add(transaction: tx)
}.eraseToAnyPublisher()
}
.eraseToAnyPublisher()
sign(transaction, signer: signer)
.flatMap {[unowned self] tx -> AnyPublisher<Void, Error> in
self.networkService.send(transaction: tx).map {[unowned self] sendResponse in
var tx = transaction
tx.hash = sendResponse
self.wallet.add(transaction: tx)
}.eraseToAnyPublisher()
}
.eraseToAnyPublisher()
}

func getFee(amount: Amount, destination: String, includeFee: Bool) -> AnyPublisher<[Amount],Error> {
Expand All @@ -171,6 +163,25 @@ extension EthereumWalletManager: TransactionSender {

}

extension EthereumWalletManager: EthereumTransactionSigner {
func sign(_ transaction: Transaction, signer: TransactionSigner) -> AnyPublisher<String, Error> {
guard let txForSign = txBuilder.buildForSign(transaction: transaction,
nonce: txCount,
gasLimit: gasLimit ?? getFixedGasLimit(for: transaction.amount)) else {
return Fail(error: WalletError.failedToBuildTx).eraseToAnyPublisher()
}

return signer.sign(hash: txForSign.hash, cardId: wallet.cardId, walletPublicKey: wallet.publicKey)
.tryMap {[unowned self] signature throws -> String in
guard let tx = self.txBuilder.buildForSend(transaction: txForSign.transaction, hash: txForSign.hash, signature: signature) else {
throw WalletError.failedToBuildTx
}
return "0x\(tx.toHexString())"
}
.eraseToAnyPublisher()
}
}

extension EthereumWalletManager: EthereumGasLoader {
func getGasPrice() -> AnyPublisher<BigUInt, Error> {
networkService.getGasPrice()
Expand Down

0 comments on commit dfe788a

Please sign in to comment.