File tree Expand file tree Collapse file tree 3 files changed +37
-1
lines changed
Expand file tree Collapse file tree 3 files changed +37
-1
lines changed Original file line number Diff line number Diff line change @@ -50,6 +50,14 @@ function hexToASCII(str: string): string {
5050 return ascii ;
5151}
5252
53+ function ASCIIToHex ( ascii : string ) : string {
54+ const hex : string [ ] = [ ] ;
55+ for ( let n = 0 ; n < ascii . length ; n ++ ) {
56+ hex . push ( Number ( ascii . charCodeAt ( n ) ) . toString ( 16 ) ) ;
57+ }
58+ return hex . join ( '' ) ;
59+ }
60+
5361/**
5462 * Converts an EVM ErrorMessage to a readable form. For example this :
5563 * 0x08c379a00000000000000000000000000000000000000000000000000000000000000020000000000000000000000000000000000000000000000000000000000000000d53657420746f2072657665727400000000000000000000000000000000000000
@@ -289,4 +297,5 @@ export {
289297 toHexString ,
290298 isValidEthereumAddress ,
291299 isHex ,
300+ ASCIIToHex ,
292301} ;
Original file line number Diff line number Diff line change @@ -39,6 +39,8 @@ import {
3939 toHash32 ,
4040 weibarHexToTinyBarInt ,
4141 trimPrecedingZeros ,
42+ ASCIIToHex ,
43+ isHex ,
4244} from '../formatters' ;
4345import crypto from 'crypto' ;
4446import HAPIService from './services/hapiService/hapiService' ;
@@ -2003,7 +2005,9 @@ export class EthImpl implements Eth {
20032005 } ;
20042006
20052007 if ( receiptResponse . error_message ) {
2006- receipt . revertReason = receiptResponse . error_message ;
2008+ receipt . revertReason = isHex ( prepend0x ( receiptResponse . error_message ) )
2009+ ? receiptResponse . error_message
2010+ : prepend0x ( ASCIIToHex ( receiptResponse . error_message ) ) ;
20072011 }
20082012
20092013 this . logger . trace ( `${ requestIdPrefix } receipt for ${ hash } found in block ${ receipt . blockNumber } ` ) ;
Original file line number Diff line number Diff line change @@ -37,6 +37,7 @@ import {
3737 isValidEthereumAddress ,
3838 trimPrecedingZeros ,
3939 isHex ,
40+ ASCIIToHex ,
4041} from '../../src/formatters' ;
4142import constants from '../../src/lib/constants' ;
4243import { BigNumber as BN } from 'bignumber.js' ;
@@ -465,4 +466,26 @@ describe('Formatters', () => {
465466 expect ( isHex ( '0x58' ) ) . to . be . true ;
466467 } ) ;
467468 } ) ;
469+ describe ( 'ASCIIToHex Function' , ( ) => {
470+ const inputs = [ 'Lorem Ipsum' , 'Foo' , 'Bar' ] ;
471+ const outputs = [ '4c6f72656d20497073756d' , '466f6f' , '426172' ] ;
472+
473+ it ( 'should return "" for empty string' , ( ) => {
474+ expect ( ASCIIToHex ( '' ) ) . to . equal ( '' ) ;
475+ } ) ;
476+
477+ it ( 'should return valid hex' , ( ) => {
478+ expect ( isHex ( prepend0x ( ASCIIToHex ( inputs [ 0 ] ) ) ) ) . to . be . true ;
479+ } ) ;
480+
481+ it ( 'should return expected hex formatted value' , ( ) => {
482+ expect ( inputs [ 0 ] ) . to . equal ( hexToASCII ( ASCIIToHex ( inputs [ 0 ] ) ) ) ;
483+ } ) ;
484+
485+ it ( 'should decode correctly regarding hardcoded mapping' , ( ) => {
486+ for ( let i = 0 ; i < inputs . length ; i ++ ) {
487+ expect ( ASCIIToHex ( inputs [ i ] ) ) . to . eq ( outputs [ i ] ) ;
488+ }
489+ } ) ;
490+ } ) ;
468491} ) ;
You can’t perform that action at this time.
0 commit comments