Add sender address derivation for several RPCs

This commit is contained in:
coffeepots 2018-08-23 21:21:17 +01:00 committed by zah
parent 55fee296d8
commit f8a3581af5
1 changed files with 28 additions and 31 deletions

View File

@ -33,6 +33,29 @@ func toHash(value: array[32, byte]): Hash256 {.inline.} =
func strToHash(value: string): Hash256 {.inline.} =
result = hexToPaddedByteArray[32](value).toHash
template toSignature(transaction: Transaction): Signature =
var bytes: array[65, byte]
bytes[0..31] = cast[array[32, byte]](transaction.R)
bytes[32..63] = cast[array[32, byte]](transaction.S)
#[
TODO: In the yellow paper:
It is assumed that v is the recovery id, a 1 byte value
specifying the sign and finiteness of the curve point; this
value is in the range of [27,30].
Does this need to be checked that it is [0, 1] and inc by 27?
]#
# TODO: Ugly casting below, is there a better way/helper func?
bytes[64] = (cast[uint64](transaction.V.data.lo) and 0xff'u64).uint8
initSignature(bytes)
proc getSender(transaction: Transaction, txHash: Hash256): EthAddress =
let
sig = transaction.toSignature()
pubKey = recoverKeyFromSignature(sig, txHash)
const pubKeySize = 64
result[0..19] = pubKey.data[pubKeySize - 20 .. pubKeySize - 1]
func headerFromTag(chain:BaseChainDB, blockTag: string): BlockHeader =
let tag = blockTag.toLowerAscii
case tag
@ -319,7 +342,7 @@ proc setupP2PRPC*(node: EthereumNode, rpcsrv: RpcServer) =
raise newException(ValueError, "Cannot find hash")
populateBlockObject(header, body)
func populateTransactionObject(transaction: Transaction, txHash: Hash256, txCount: UInt256, txIndex: int, blockHeader: BlockHeader, gas: int64): TransactionObject =
proc populateTransactionObject(transaction: Transaction, txHash: Hash256, txCount: UInt256, txIndex: int, blockHeader: BlockHeader, gas: int64): TransactionObject =
result.hash = txHash
result.nonce = txCount
result.blockHash = new Hash256
@ -328,8 +351,7 @@ proc setupP2PRPC*(node: EthereumNode, rpcsrv: RpcServer) =
result.blockNumber[] = blockHeader.blockNumber
result.transactionIndex = new int64
result.transactionIndex[] = txIndex
# TODO: Fetch or calculate `from` address with signature after signing with the private key
#result.source: EthAddress
result.source = transaction.getSender(transaction.rlpHash)
result.to = new EthAddress
result.to[] = transaction.to
result.value = transaction.value
@ -354,8 +376,7 @@ proc setupP2PRPC*(node: EthereumNode, rpcsrv: RpcServer) =
transaction = body.transactions[txDetails.index]
vmState = newBaseVMState(header, chain)
addressDb = vmState.chaindb.getStateDb(blockHash, true)
# TODO: Get/calculate address for this transaction
address = ZERO_ADDRESS
address = transaction.getSender(transaction.rlpHash)
txCount = addressDb.getNonce(address)
txHash = transaction.rlpHash
# TODO: Fetch account gas
@ -378,8 +399,7 @@ proc setupP2PRPC*(node: EthereumNode, rpcsrv: RpcServer) =
transaction = body.transactions[quantity]
vmState = newBaseVMState(header, chain)
addressDb = vmState.chaindb.getStateDb(blockHash, true)
# TODO: Get/calculate address for this transaction
address = ZERO_ADDRESS
address = transaction.getSender(transaction.rlpHash)
txCount = addressDb.getNonce(address)
txHash = transaction.rlpHash
# TODO: Fetch account gas
@ -401,36 +421,13 @@ proc setupP2PRPC*(node: EthereumNode, rpcsrv: RpcServer) =
transaction = body.transactions[quantity]
vmState = newBaseVMState(header, chain)
addressDb = vmState.chaindb.getStateDb(blockHash, true)
# TODO: Get/calculate address for this transaction
address = ZERO_ADDRESS
address = transaction.getSender(transaction.rlpHash)
txCount = addressDb.getNonce(address)
txHash = transaction.rlpHash
# TODO: Fetch account gas
accountGas = 0
populateTransactionObject(transaction, txHash, txCount, quantity, header, accountGas)
template toSignature(transaction: Transaction): Signature =
var bytes: array[65, byte]
bytes[0..31] = cast[array[32, byte]](transaction.R)
bytes[32..63] = cast[array[32, byte]](transaction.S)
#[
TODO: In the yellow paper:
It is assumed that v is the recovery id, a 1 byte value
specifying the sign and finiteness of the curve point; this
value is in the range of [27,30].
Does this need to be checked that it is [0, 1] and inc by 27?
]#
# TODO: Ugly casting below, is there a better way/helper func?
bytes[64] = (cast[uint64](transaction.V.data.lo) and 0xff'u64).uint8
initSignature(bytes)
proc getSender(transaction: Transaction, txHash: Hash256): EthAddress =
let
sig = transaction.toSignature()
pubKey = recoverKeyFromSignature(sig, txHash)
const pubKeySize = 64
result[0..19] = pubKey.data[pubKeySize - 20 .. pubKeySize - 1]
proc populateReceipt(receipt: Receipt, cumulativeGas: GasInt, transaction: Transaction, txIndex: int, blockHeader: BlockHeader): ReceiptObject =
result.transactionHash = transaction.rlpHash
result.transactionIndex = txIndex