Format all ethereum addresses as EIP55

pull/383/head
Martin Boehm 2020-03-04 10:17:47 +01:00
parent a6c01534f2
commit 53cc6237a7
2 changed files with 21 additions and 5 deletions

View File

@ -96,18 +96,31 @@ func ethNumber(n string) (int64, error) {
return 0, errors.Errorf("Not a number: '%v'", n) 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 txid := tx.Hash
var ( var (
fa, ta []string fa, ta []string
err error err error
) )
if len(tx.From) > 2 { if len(tx.From) > 2 {
if fixEIP55 {
tx.From = EIP55AddressFromAddress(tx.From)
}
fa = []string{tx.From} fa = []string{tx.From}
} }
if len(tx.To) > 2 { if len(tx.To) > 2 {
if fixEIP55 {
tx.To = EIP55AddressFromAddress(tx.To)
}
ta = []string{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{ ct := completeTransaction{
Tx: tx, Tx: tx,
Receipt: receipt, Receipt: receipt,
@ -198,6 +211,9 @@ func EIP55Address(addrDesc bchain.AddressDescriptor) string {
// EIP55AddressFromAddress returns an EIP55-compliant hex string representation of the address // EIP55AddressFromAddress returns an EIP55-compliant hex string representation of the address
func EIP55AddressFromAddress(address string) string { func EIP55AddressFromAddress(address string) string {
if has0xPrefix(address) {
address = address[2:]
}
b, err := hex.DecodeString(address) b, err := hex.DecodeString(address)
if err != nil { if err != nil {
return address return address
@ -369,7 +385,7 @@ func (p *EthereumParser) UnpackTx(buf []byte) (*bchain.Tx, uint32, error) {
Logs: logs, 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 { if err != nil {
return nil, 0, err return nil, 0, err
} }

View File

@ -533,7 +533,7 @@ func (b *EthereumRPC) GetBlock(hash string, height uint32) (*bchain.Block, error
btxs := make([]bchain.Tx, len(body.Transactions)) btxs := make([]bchain.Tx, len(body.Transactions))
for i := range body.Transactions { for i := range body.Transactions {
tx := &body.Transactions[i] 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 { if err != nil {
return nil, errors.Annotatef(err, "hash %v, height %v, txid %v", hash, height, tx.Hash) 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 var btx *bchain.Tx
if tx.BlockNumber == "" { if tx.BlockNumber == "" {
// mempool tx // mempool tx
btx, err = b.Parser.ethTxToTx(tx, nil, 0, 0) btx, err = b.Parser.ethTxToTx(tx, nil, 0, 0, true)
if err != nil { if err != nil {
return nil, errors.Annotatef(err, "txid %v", txid) return nil, errors.Annotatef(err, "txid %v", txid)
} }
@ -632,7 +632,7 @@ func (b *EthereumRPC) GetTransaction(txid string) (*bchain.Tx, error) {
if err != nil { if err != nil {
return nil, errors.Annotatef(err, "txid %v", txid) 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 { if err != nil {
return nil, errors.Annotatef(err, "txid %v", txid) return nil, errors.Annotatef(err, "txid %v", txid)
} }