Skip to content

Commit 0e2c432

Browse files
envestccdustinxie
authored andcommitted
[api] fix json unmarshal error when calling TransactionByHash via ethclient (#3933)
1 parent 11b446b commit 0e2c432

File tree

2 files changed

+51
-5
lines changed

2 files changed

+51
-5
lines changed

api/web3server_marshal.go

Lines changed: 8 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -3,15 +3,17 @@ package api
33
import (
44
"encoding/hex"
55
"encoding/json"
6+
"math/big"
67

8+
"github.com/ethereum/go-ethereum/common/hexutil"
79
"github.com/ethereum/go-ethereum/core/types"
10+
"github.com/iotexproject/go-pkgs/crypto"
11+
"github.com/iotexproject/go-pkgs/hash"
12+
"github.com/iotexproject/iotex-address/address"
813
"github.com/pkg/errors"
914
"google.golang.org/grpc/status"
1015
"google.golang.org/protobuf/types/known/timestamppb"
1116

12-
"github.com/iotexproject/go-pkgs/crypto"
13-
"github.com/iotexproject/go-pkgs/hash"
14-
"github.com/iotexproject/iotex-address/address"
1517
"github.com/iotexproject/iotex-core/action"
1618
apitypes "github.com/iotexproject/iotex-core/api/types"
1719
"github.com/iotexproject/iotex-core/blockchain/block"
@@ -228,6 +230,7 @@ func (obj *getTransactionResult) MarshalJSON() ([]byte, error) {
228230
value, _ := intStrToHex(obj.ethTx.Value().String())
229231
gasPrice, _ := intStrToHex(obj.ethTx.GasPrice().String())
230232

233+
// TODO: if transaction is support EIP-155, we need to add chainID to v
231234
vVal := uint64(obj.signature[64])
232235
if vVal < 27 {
233236
vVal += 27
@@ -260,8 +263,8 @@ func (obj *getTransactionResult) MarshalJSON() ([]byte, error) {
260263
GasPrice: gasPrice,
261264
Gas: uint64ToHex(obj.ethTx.Gas()),
262265
Input: byteToHex(obj.ethTx.Data()),
263-
R: byteToHex(obj.signature[:32]),
264-
S: byteToHex(obj.signature[32:64]),
266+
R: hexutil.EncodeBig(new(big.Int).SetBytes(obj.signature[:32])),
267+
S: hexutil.EncodeBig(new(big.Int).SetBytes(obj.signature[32:64])),
265268
V: uint64ToHex(vVal),
266269
})
267270
}

api/web3server_marshal_test.go

Lines changed: 43 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -232,6 +232,7 @@ func TestTransactionObjectMarshal(t *testing.T) {
232232
ethTx: types.NewContractCreation(1, big.NewInt(10), 21000, big.NewInt(0), []byte{}),
233233
receipt: receipt,
234234
pubkey: _testPubKey,
235+
// TODO: should decode signature from hex string
235236
signature: []byte("69d89a0af27dcaa67f1b62a383594d97599aadd2b7b164cb4112aa8ddfd42f895649075cae1b7216c43a491c5e9be68d1d9a27b863d71155ecdd7c95dab5394f01"),
236237
})
237238
require.NoError(err)
@@ -254,6 +255,48 @@ func TestTransactionObjectMarshal(t *testing.T) {
254255
}
255256
`, string(res))
256257
})
258+
t.Run("ISSUE3932", func(t *testing.T) {
259+
sig, _ := hex.DecodeString("00d50c3169003e7c9c9392069a1c9e5251c8f558120cceb9aa312f0fa46247701f4cad47a5540cdda5459f7a46c62df752ca588e0b73722ec767ff08994134a41b")
260+
blkHash, _ := hash.HexStringToHash256("bdcf722cbc0dc9b9eb5b46ad4af9e15944a886310b1cb2227129070c0e4b7de5")
261+
contract := "0xc3527348de07d591c9d567ce1998efa2031b8675"
262+
data, _ := hex.DecodeString("1fad948c0000000000000000000000000000000000000000000000000000000000000040000000000000000000000000ec4daee51c4bf81bd00af165f8ca66823ee3b12a000000000000000000000000000000000000000000000000000000000000000100000000000000000000000000000000000000000000000000000000000000200000000000000000000000003e24491a4f2a946e30baf624fda6b4484d106c12000000000000000000000000000000000000000000000000000000000000005b000000000000000000000000000000000000000000000000000000000000016000000000000000000000000000000000000000000000000000000000000001800000000000000000000000000000000000000000000000000000000000009fd90000000000000000000000000000000000000000000000000000000000011da4000000000000000000000000000000000000000000000000000000000000db26000000000000000000000000000000000000000000000000000000e8d4a51000000000000000000000000000000000000000000000000000000000e8d4a510000000000000000000000000000000000000000000000000000000000000000240000000000000000000000000000000000000000000000000000000000000030000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000084b61d27f6000000000000000000000000065e1164818487818e6ba714e8d80b91718ad75800000000000000000000000000000000000000000000000000038d7ea4c6800000000000000000000000000000000000000000000000000000000000000000600000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000095ccc7012efb2e65aa31752f3ac01e23817c08a47500000000000000000000000000000000000000000000000000000000650af9f8000000000000000000000000000000000000000000000000000000006509a878b5acba7277159ae6fa661ed1988cc10ac2c96c58dc332bde2a6dc0d8531ea3924d9d04cda681c271411250ae7d9e9aea47661dba67a66f08d19804a255e45c561b0000000000000000000000000000000000000000000000000000000000000000000000000000000000004160daa88165299ca7e585d5d286cee98b54397b57ac704b74331a48d67651195322ef3884c7d60023333f2542a07936f34edc9efa3cbd19e8cd0f8972c54171a21b00000000000000000000000000000000000000000000000000000000000000")
263+
pubkey, _ := crypto.HexStringToPublicKey("04806b217cb0b6a675974689fd99549e525d967287eee9a62dc4e598eea981b8158acfe026da7bf58397108abd0607672832c28ef3bc7b5855077f6e67ab5fc096")
264+
actHash, _ := hash.HexStringToHash256("cbc2560d986d79a46bfd96a08d18c6045b29f97352c1360289e371d9cffd6b6a")
265+
res, err := json.Marshal(&getTransactionResult{
266+
blockHash: blkHash,
267+
to: &contract,
268+
ethTx: types.NewContractCreation(305, big.NewInt(0), 297131, big.NewInt(1000000000000), data),
269+
receipt: &action.Receipt{
270+
Status: 1,
271+
BlockHeight: 22354907,
272+
ActionHash: actHash,
273+
GasConsumed: 196223,
274+
ContractAddress: "",
275+
TxIndex: 0,
276+
},
277+
pubkey: pubkey,
278+
signature: sig,
279+
})
280+
require.NoError(err)
281+
require.JSONEq(`
282+
{
283+
"hash": "0xcbc2560d986d79a46bfd96a08d18c6045b29f97352c1360289e371d9cffd6b6a",
284+
"nonce": "0x131",
285+
"blockHash": "0xbdcf722cbc0dc9b9eb5b46ad4af9e15944a886310b1cb2227129070c0e4b7de5",
286+
"blockNumber": "0x1551bdb",
287+
"transactionIndex": "0x0",
288+
"from": "0xec4daee51c4bf81bd00af165f8ca66823ee3b12a",
289+
"to": "0xc3527348de07d591c9d567ce1998efa2031b8675",
290+
"value": "0x0",
291+
"gasPrice": "0xe8d4a51000",
292+
"gas": "0x488ab",
293+
"input": "0x1fad948c0000000000000000000000000000000000000000000000000000000000000040000000000000000000000000ec4daee51c4bf81bd00af165f8ca66823ee3b12a000000000000000000000000000000000000000000000000000000000000000100000000000000000000000000000000000000000000000000000000000000200000000000000000000000003e24491a4f2a946e30baf624fda6b4484d106c12000000000000000000000000000000000000000000000000000000000000005b000000000000000000000000000000000000000000000000000000000000016000000000000000000000000000000000000000000000000000000000000001800000000000000000000000000000000000000000000000000000000000009fd90000000000000000000000000000000000000000000000000000000000011da4000000000000000000000000000000000000000000000000000000000000db26000000000000000000000000000000000000000000000000000000e8d4a51000000000000000000000000000000000000000000000000000000000e8d4a510000000000000000000000000000000000000000000000000000000000000000240000000000000000000000000000000000000000000000000000000000000030000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000084b61d27f6000000000000000000000000065e1164818487818e6ba714e8d80b91718ad75800000000000000000000000000000000000000000000000000038d7ea4c6800000000000000000000000000000000000000000000000000000000000000000600000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000095ccc7012efb2e65aa31752f3ac01e23817c08a47500000000000000000000000000000000000000000000000000000000650af9f8000000000000000000000000000000000000000000000000000000006509a878b5acba7277159ae6fa661ed1988cc10ac2c96c58dc332bde2a6dc0d8531ea3924d9d04cda681c271411250ae7d9e9aea47661dba67a66f08d19804a255e45c561b0000000000000000000000000000000000000000000000000000000000000000000000000000000000004160daa88165299ca7e585d5d286cee98b54397b57ac704b74331a48d67651195322ef3884c7d60023333f2542a07936f34edc9efa3cbd19e8cd0f8972c54171a21b00000000000000000000000000000000000000000000000000000000000000",
294+
"r": "0xd50c3169003e7c9c9392069a1c9e5251c8f558120cceb9aa312f0fa4624770",
295+
"s": "0x1f4cad47a5540cdda5459f7a46c62df752ca588e0b73722ec767ff08994134a4",
296+
"v": "0x1b"
297+
}
298+
`, string(res))
299+
})
257300
}
258301

259302
func TestReceiptObjectMarshal(t *testing.T) {

0 commit comments

Comments
 (0)