secp: requiresInit

This commit is contained in:
Jacek Sieka 2020-06-21 21:57:43 +02:00
parent 05f2a17ba4
commit 806547ddd1
No known key found for this signature in database
GPG Key ID: A1B09461ABB656B8
2 changed files with 6 additions and 11 deletions

View File

@ -19,7 +19,7 @@ type
provider*: RpcClient
subscriptions*: Table[string, Subscription]
defaultAccount*: Address
privateKey*: PrivateKey
privateKey*: Option[PrivateKey]
onDisconnect*: proc() {.gcsafe.}
Sender*[T] = ref object
@ -724,16 +724,13 @@ proc getJsonLogs*(s: Sender,
s.web3.provider.eth_getLogs(options)
proc signatureEnabled(w: Web3): bool {.inline.} =
w.privateKey.verify()
proc send*(web3: Web3, c: EthSend): Future[TxHash] {.async.} =
if web3.signatureEnabled():
if web3.privateKey.isSome():
var cc = c
if not cc.nonce.isSome:
let fromAddress = web3.privateKey.toPublicKey().tryGet().toCanonicalAddress.Address
let fromAddress = web3.privateKey.get().toPublicKey().toCanonicalAddress.Address
cc.nonce = some(int(await web3.provider.eth_getTransactionCount(fromAddress, "latest")))
let t = "0x" & encodeTransaction(cc, web3.privateKey)
let t = "0x" & encodeTransaction(cc, web3.privateKey.get())
return await web3.provider.eth_sendRawTransaction(t)
else:
return await web3.provider.eth_sendTransaction(c)
@ -747,7 +744,7 @@ proc send*(c: ContractCallBase, value = 0.u256, gas = 3000000'u64, gasPrice = 0)
cc.gas = some(Quantity(gas))
cc.value = some(value)
if web3.signatureEnabled() or gasPrice != 0:
if web3.privateKey.isSome() or gasPrice != 0:
cc.gasPrice = some(gasPrice)
web3.send(cc)

View File

@ -6,10 +6,8 @@ import
proc signTransaction(tr: var Transaction, pk: PrivateKey) =
let h = tr.txHashNoSignature
let s = sign(pk, h)
if s.isErr:
raise newException(CatchableError, "Could not sign transaction: " & $s.error)
var r = toRaw(s[])
var r = toRaw(s)
let v = r[64]
tr.R = fromBytesBE(UInt256, r.toOpenArray(0, 31))