File tree 3 files changed +31
-0
lines changed
3 files changed +31
-0
lines changed Original file line number Diff line number Diff line change @@ -61,6 +61,16 @@ func (ec *Client) Close() {
61
61
62
62
// Blockchain Access
63
63
64
+ // ChainId retrieves the current chain ID for transaction replay protection.
65
+ func (ec * Client ) ChainID (ctx context.Context ) (* big.Int , error ) {
66
+ var result hexutil.Big
67
+ err := ec .c .CallContext (ctx , & result , "eth_chainId" )
68
+ if err != nil {
69
+ return nil , err
70
+ }
71
+ return (* big .Int )(& result ), err
72
+ }
73
+
64
74
// BlockByHash returns the given full block.
65
75
//
66
76
// Note that loading full blocks requires two requests. Use HeaderByHash
Original file line number Diff line number Diff line change @@ -319,3 +319,19 @@ func TestTransactionInBlockInterrupted(t *testing.T) {
319
319
t .Fatal ("error should not be nil" )
320
320
}
321
321
}
322
+
323
+ func TestChainID (t * testing.T ) {
324
+ backend , _ := newTestBackend (t )
325
+ client , _ := backend .Attach ()
326
+ defer backend .Stop ()
327
+ defer client .Close ()
328
+ ec := NewClient (client )
329
+
330
+ id , err := ec .ChainID (context .Background ())
331
+ if err != nil {
332
+ t .Fatalf ("unexpected error: %v" , err )
333
+ }
334
+ if id == nil || id .Cmp (params .AllEthashProtocolChanges .ChainID ) != 0 {
335
+ t .Fatalf ("ChainID returned wrong number: %+v" , id )
336
+ }
337
+ }
Original file line number Diff line number Diff line change @@ -529,6 +529,11 @@ func NewPublicBlockChainAPI(b Backend) *PublicBlockChainAPI {
529
529
return & PublicBlockChainAPI {b }
530
530
}
531
531
532
+ // ChainId returns the chainID value for transaction replay protection.
533
+ func (s * PublicBlockChainAPI ) ChainId () * hexutil.Big {
534
+ return (* hexutil .Big )(s .b .ChainConfig ().ChainID )
535
+ }
536
+
532
537
// BlockNumber returns the block number of the chain head.
533
538
func (s * PublicBlockChainAPI ) BlockNumber () hexutil.Uint64 {
534
539
header , _ := s .b .HeaderByNumber (context .Background (), rpc .LatestBlockNumber ) // latest header should always be available
You can’t perform that action at this time.
0 commit comments