[wallet] Use questionable to deal with optionals

This commit is contained in:
Mark Spanbroek 2022-07-14 11:24:21 +02:00 committed by markspanbroek
parent 0483e1676a
commit 1a8ca0bcad
1 changed files with 20 additions and 21 deletions

View File

@ -51,21 +51,13 @@ proc createRandom*(_: type Wallet, provider: Provider): Wallet =
result.provider = some provider result.provider = some provider
method provider*(wallet: Wallet): Provider = method provider*(wallet: Wallet): Provider =
if wallet.provider.isSome: without provider =? wallet.provider:
return wallet.provider.get
else:
raise newException(WalletError, "Wallet has no provider") raise newException(WalletError, "Wallet has no provider")
provider
method getAddress(wallet: Wallet): Future[Address] {.async.} = method getAddress(wallet: Wallet): Future[Address] {.async.} =
return wallet.address return wallet.address
func isPopulated(tx: transaction.Transaction) =
if tx.nonce.isNone or
tx.chainId.isNone or
tx.gasLimit.isNone or
(tx.gasPrice.isNone and (tx.maxFee.isNone or tx.maxPriorityFee.isNone)):
raise newException(WalletError, "Transaction is not properly populated")
proc signTransaction(tr: var SignableTransaction, pk: PrivateKey) = proc signTransaction(tr: var SignableTransaction, pk: PrivateKey) =
let h = tr.txHashNoSignature let h = tr.txHashNoSignature
let s = sign(pk, SkMessage(h.data)) let s = sign(pk, SkMessage(h.data))
@ -86,20 +78,27 @@ proc signTransaction(tr: var SignableTransaction, pk: PrivateKey) =
raise newException(WalletError, "Transaction type not supported") raise newException(WalletError, "Transaction type not supported")
proc signTransaction*(wallet: Wallet, tx: transaction.Transaction): Future[seq[byte]] {.async.} = proc signTransaction*(wallet: Wallet, tx: transaction.Transaction): Future[seq[byte]] {.async.} =
if tx.sender.isSome: if sender =? tx.sender and sender != wallet.address:
doAssert tx.sender.get == wallet.address, "from Address mismatch" raise newException(WalletError, "from address mismatch")
isPopulated(tx)
without nonce =? tx.nonce and chainId =? tx.chainId and gasLimit =? tx.gasLimit:
raise newException(WalletError, "Transaction is properly populated")
var s: SignableTransaction var s: SignableTransaction
if tx.maxFee.isSome and tx.maxPriorityFee.isSome:
if maxFee =? tx.maxFee and maxPriorityFee =? tx.maxPriorityFee:
s.txType = TxEip1559 s.txType = TxEip1559
s.maxFee = GasInt(tx.maxFee.get.truncate(uint64)) s.maxFee = GasInt(maxFee.truncate(uint64))
s.maxPriorityFee = GasInt(tx.maxPriorityFee.get.truncate(uint64)) s.maxPriorityFee = GasInt(maxPriorityFee.truncate(uint64))
else: elif gasPrice =? tx.gasPrice:
s.txType = TxLegacy s.txType = TxLegacy
s.gasPrice = GasInt(tx.gasPrice.get.truncate(uint64)) s.gasPrice = GasInt(gasPrice.truncate(uint64))
s.chainId = ChainId(tx.chainId.get.truncate(uint64)) else:
s.gasLimit = GasInt(tx.gasLimit.get.truncate(uint64)) raise newException(WalletError, "Transaction is properly populated")
s.nonce = tx.nonce.get.truncate(uint64)
s.chainId = ChainId(chainId.truncate(uint64))
s.gasLimit = GasInt(gasLimit.truncate(uint64))
s.nonce = nonce.truncate(uint64)
s.to = some EthAddress(tx.to) s.to = some EthAddress(tx.to)
s.payload = tx.data s.payload = tx.data
signTransaction(s, wallet.privateKey) signTransaction(s, wallet.privateKey)