Skip to content

Commit d0d2e6e

Browse files
Merge branch 'main' into sae-poc
2 parents 2732ba6 + 1ec8741 commit d0d2e6e

File tree

4 files changed

+150
-4
lines changed

4 files changed

+150
-4
lines changed

libevm/ethtest/devkey.go

Lines changed: 66 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,66 @@
1+
// Copyright 2025 the libevm authors.
2+
//
3+
// The libevm additions to go-ethereum are free software: you can redistribute
4+
// them and/or modify them under the terms of the GNU Lesser General Public License
5+
// as published by the Free Software Foundation, either version 3 of the License,
6+
// or (at your option) any later version.
7+
//
8+
// The libevm additions are distributed in the hope that they will be useful,
9+
// but WITHOUT ANY WARRANTY; without even the implied warranty of
10+
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU Lesser
11+
// General Public License for more details.
12+
//
13+
// You should have received a copy of the GNU Lesser General Public License
14+
// along with the go-ethereum library. If not, see
15+
// <http://www.gnu.org/licenses/>.
16+
17+
package ethtest
18+
19+
import (
20+
"crypto/ecdsa"
21+
"crypto/elliptic"
22+
"math/big"
23+
"testing"
24+
25+
"github.com/stretchr/testify/require"
26+
27+
"github.com/ava-labs/libevm/crypto"
28+
)
29+
30+
// UNSAFEDeterministicPrivateKey returns a new [crypto.S256] private key,
31+
// deterministically generated from the `seed`.
32+
func UNSAFEDeterministicPrivateKey(tb testing.TB, seed []byte) *ecdsa.PrivateKey {
33+
tb.Helper()
34+
35+
curve := crypto.S256()
36+
d := privateKeyScalar(tb, seed, curve)
37+
38+
x, y := curve.ScalarBaseMult(d.Bytes())
39+
return &ecdsa.PrivateKey{
40+
D: d,
41+
PublicKey: ecdsa.PublicKey{
42+
X: x,
43+
Y: y,
44+
Curve: curve,
45+
},
46+
}
47+
}
48+
49+
func privateKeyScalar(tb testing.TB, seed []byte, curve elliptic.Curve) *big.Int {
50+
tb.Helper()
51+
52+
s := crypto.NewKeccakState()
53+
_, err := s.Write(seed)
54+
require.NoError(tb, err, "%T.Write()", s)
55+
56+
buf := make([]byte, 32)
57+
for {
58+
_, err := s.Read(buf)
59+
require.NoError(tb, err, "%T.Read()", s)
60+
d := new(big.Int).SetBytes(buf)
61+
62+
if isZero := d.Sign() == 0; !isZero && d.Cmp(curve.Params().N) == -1 {
63+
return d
64+
}
65+
}
66+
}

libevm/ethtest/devkey_test.go

Lines changed: 72 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,72 @@
1+
// Copyright 2025 the libevm authors.
2+
//
3+
// The libevm additions to go-ethereum are free software: you can redistribute
4+
// them and/or modify them under the terms of the GNU Lesser General Public License
5+
// as published by the Free Software Foundation, either version 3 of the License,
6+
// or (at your option) any later version.
7+
//
8+
// The libevm additions are distributed in the hope that they will be useful,
9+
// but WITHOUT ANY WARRANTY; without even the implied warranty of
10+
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU Lesser
11+
// General Public License for more details.
12+
//
13+
// You should have received a copy of the GNU Lesser General Public License
14+
// along with the go-ethereum library. If not, see
15+
// <http://www.gnu.org/licenses/>.
16+
17+
package ethtest
18+
19+
import (
20+
"testing"
21+
22+
"github.com/stretchr/testify/require"
23+
24+
"github.com/ava-labs/libevm/common"
25+
"github.com/ava-labs/libevm/core/types"
26+
"github.com/ava-labs/libevm/crypto"
27+
"github.com/ava-labs/libevm/params"
28+
)
29+
30+
func TestDeterministicPrivateKey(t *testing.T) {
31+
tests := []struct {
32+
seed []byte
33+
// Specific values are random, but we lock them in to ensure
34+
// deterministic generation.
35+
want common.Address
36+
}{
37+
{
38+
seed: nil,
39+
want: common.HexToAddress("0x9cce34F7aB185c7ABA1b7C8140d620B4BDA941d6"),
40+
},
41+
{
42+
seed: []byte{0},
43+
want: common.HexToAddress("0xa385D2E939787Af0B304512b2b6d56364F1722FA"),
44+
},
45+
{
46+
seed: []byte{1},
47+
want: common.HexToAddress("0x3Eea25034397B249a3eD8614BB4d0533e5b03594"),
48+
},
49+
}
50+
51+
signer := types.LatestSigner(params.MergedTestChainConfig)
52+
53+
for _, tt := range tests {
54+
t.Run("", func(t *testing.T) {
55+
key := UNSAFEDeterministicPrivateKey(t, tt.seed)
56+
57+
t.Run("address_from_pubkey", func(t *testing.T) {
58+
got := crypto.PubkeyToAddress(key.PublicKey)
59+
require.Equal(t, tt.want, got, "crypto.PubKeyToAddress(UNSAFEDeterministicPrivateKey())")
60+
})
61+
62+
t.Run("address_via_sender_recovery", func(t *testing.T) {
63+
got, err := types.Sender(
64+
signer,
65+
types.MustSignNewTx(key, signer, &types.LegacyTx{}),
66+
)
67+
require.NoError(t, err, "types.Sender(...)")
68+
require.Equal(t, tt.want, got, "types.Sender(..., types.MustSignNewTx(UNSAFEDeterministicPrivateKey(), ....))")
69+
})
70+
})
71+
}
72+
}

rpc/http.go

Lines changed: 11 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -168,13 +168,21 @@ func newClientTransportHTTP(endpoint string, cfg *clientConfig) reconnectFunc {
168168
}
169169
}
170170

171+
// cleanlyCloseBody avoids sending unnecessary RST_STREAM and PING frames by
172+
// ensuring the whole body is read before being closed.
173+
// See https://blog.cloudflare.com/go-and-enhance-your-calm/#reading-bodies-in-go-can-be-unintuitive
174+
func cleanlyCloseBody(body io.ReadCloser) error {
175+
io.Copy(io.Discard, body)
176+
return body.Close()
177+
}
178+
171179
func (c *Client) sendHTTP(ctx context.Context, op *requestOp, msg interface{}) error {
172180
hc := c.writeConn.(*httpConn)
173181
respBody, err := hc.doRequest(ctx, msg)
174182
if err != nil {
175183
return err
176184
}
177-
defer respBody.Close()
185+
defer cleanlyCloseBody(respBody)
178186

179187
var resp jsonrpcMessage
180188
batch := [1]*jsonrpcMessage{&resp}
@@ -191,7 +199,7 @@ func (c *Client) sendBatchHTTP(ctx context.Context, op *requestOp, msgs []*jsonr
191199
if err != nil {
192200
return err
193201
}
194-
defer respBody.Close()
202+
defer cleanlyCloseBody(respBody)
195203

196204
var respmsgs []*jsonrpcMessage
197205
if err := json.NewDecoder(respBody).Decode(&respmsgs); err != nil {
@@ -236,7 +244,7 @@ func (hc *httpConn) doRequest(ctx context.Context, msg interface{}) (io.ReadClos
236244
if _, err := buf.ReadFrom(resp.Body); err == nil {
237245
body = buf.Bytes()
238246
}
239-
resp.Body.Close()
247+
cleanlyCloseBody(resp.Body)
240248
return nil, HTTPError{
241249
Status: resp.Status,
242250
StatusCode: resp.StatusCode,

rpc/http_test.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -96,7 +96,7 @@ func confirmHTTPRequestYieldsStatusCode(t *testing.T, method, contentType, body
9696
if err != nil {
9797
t.Fatalf("request failed: %v", err)
9898
}
99-
resp.Body.Close()
99+
cleanlyCloseBody(resp.Body)
100100
confirmStatusCode(t, resp.StatusCode, expectedStatusCode)
101101
}
102102

0 commit comments

Comments
 (0)