From 53cc6237a75fabe6d9bf363931f15143b4a0ea61 Mon Sep 17 00:00:00 2001 From: Martin Boehm Date: Wed, 4 Mar 2020 10:17:47 +0100 Subject: [PATCH] Format all ethereum addresses as EIP55 --- bchain/coins/eth/ethparser.go | 20 ++++++++++++++++++-- bchain/coins/eth/ethrpc.go | 6 +++--- 2 files changed, 21 insertions(+), 5 deletions(-) diff --git a/bchain/coins/eth/ethparser.go b/bchain/coins/eth/ethparser.go index 48da0e11..d19b2543 100644 --- a/bchain/coins/eth/ethparser.go +++ b/bchain/coins/eth/ethparser.go @@ -96,18 +96,31 @@ func ethNumber(n string) (int64, error) { return 0, errors.Errorf("Not a number: '%v'", n) } -func (p *EthereumParser) ethTxToTx(tx *rpcTransaction, receipt *rpcReceipt, blocktime int64, confirmations uint32) (*bchain.Tx, error) { +func (p *EthereumParser) ethTxToTx(tx *rpcTransaction, receipt *rpcReceipt, blocktime int64, confirmations uint32, fixEIP55 bool) (*bchain.Tx, error) { txid := tx.Hash var ( fa, ta []string err error ) if len(tx.From) > 2 { + if fixEIP55 { + tx.From = EIP55AddressFromAddress(tx.From) + } fa = []string{tx.From} } if len(tx.To) > 2 { + if fixEIP55 { + tx.To = EIP55AddressFromAddress(tx.To) + } ta = []string{tx.To} } + if fixEIP55 && receipt != nil && receipt.Logs != nil { + for _, l := range receipt.Logs { + if len(l.Address) > 2 { + l.Address = EIP55AddressFromAddress(l.Address) + } + } + } ct := completeTransaction{ Tx: tx, Receipt: receipt, @@ -198,6 +211,9 @@ func EIP55Address(addrDesc bchain.AddressDescriptor) string { // EIP55AddressFromAddress returns an EIP55-compliant hex string representation of the address func EIP55AddressFromAddress(address string) string { + if has0xPrefix(address) { + address = address[2:] + } b, err := hex.DecodeString(address) if err != nil { return address @@ -369,7 +385,7 @@ func (p *EthereumParser) UnpackTx(buf []byte) (*bchain.Tx, uint32, error) { Logs: logs, } } - tx, err := p.ethTxToTx(&rt, rr, int64(pt.BlockTime), 0) + tx, err := p.ethTxToTx(&rt, rr, int64(pt.BlockTime), 0, false) if err != nil { return nil, 0, err } diff --git a/bchain/coins/eth/ethrpc.go b/bchain/coins/eth/ethrpc.go index df6a3a8e..d464b8a5 100644 --- a/bchain/coins/eth/ethrpc.go +++ b/bchain/coins/eth/ethrpc.go @@ -533,7 +533,7 @@ func (b *EthereumRPC) GetBlock(hash string, height uint32) (*bchain.Block, error btxs := make([]bchain.Tx, len(body.Transactions)) for i := range body.Transactions { tx := &body.Transactions[i] - btx, err := b.Parser.ethTxToTx(tx, &rpcReceipt{Logs: logs[tx.Hash]}, bbh.Time, uint32(bbh.Confirmations)) + btx, err := b.Parser.ethTxToTx(tx, &rpcReceipt{Logs: logs[tx.Hash]}, bbh.Time, uint32(bbh.Confirmations), true) if err != nil { return nil, errors.Annotatef(err, "hash %v, height %v, txid %v", hash, height, tx.Hash) } @@ -599,7 +599,7 @@ func (b *EthereumRPC) GetTransaction(txid string) (*bchain.Tx, error) { var btx *bchain.Tx if tx.BlockNumber == "" { // mempool tx - btx, err = b.Parser.ethTxToTx(tx, nil, 0, 0) + btx, err = b.Parser.ethTxToTx(tx, nil, 0, 0, true) if err != nil { return nil, errors.Annotatef(err, "txid %v", txid) } @@ -632,7 +632,7 @@ func (b *EthereumRPC) GetTransaction(txid string) (*bchain.Tx, error) { if err != nil { return nil, errors.Annotatef(err, "txid %v", txid) } - btx, err = b.Parser.ethTxToTx(tx, &receipt, time, confirmations) + btx, err = b.Parser.ethTxToTx(tx, &receipt, time, confirmations, true) if err != nil { return nil, errors.Annotatef(err, "txid %v", txid) }