Skip to content

Commit 8461d3f

Browse files
lightclientzzyalbert
authored andcommitted
cmd/evm: add support for signing transactions in the unprotected format (#23937)
* cmd/evm: add support for signing transactions in the unprotected format * cmd/evm: simplify signing of unprotected txs
1 parent ce12371 commit 8461d3f

File tree

7 files changed

+97
-10
lines changed

7 files changed

+97
-10
lines changed

cmd/evm/internal/t8ntool/transition.go

Lines changed: 25 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -268,27 +268,34 @@ func Transition(ctx *cli.Context) error {
268268
// txWithKey is a helper-struct, to allow us to use the types.Transaction along with
269269
// a `secretKey`-field, for input
270270
type txWithKey struct {
271-
key *ecdsa.PrivateKey
272-
tx *types.Transaction
271+
key *ecdsa.PrivateKey
272+
tx *types.Transaction
273+
protected bool
273274
}
274275

275276
func (t *txWithKey) UnmarshalJSON(input []byte) error {
276-
// Read the secretKey, if present
277-
type sKey struct {
278-
Key *common.Hash `json:"secretKey"`
277+
// Read the metadata, if present
278+
type txMetadata struct {
279+
Key *common.Hash `json:"secretKey"`
280+
Protected *bool `json:"protected"`
279281
}
280-
var key sKey
281-
if err := json.Unmarshal(input, &key); err != nil {
282+
var data txMetadata
283+
if err := json.Unmarshal(input, &data); err != nil {
282284
return err
283285
}
284-
if key.Key != nil {
285-
k := key.Key.Hex()[2:]
286+
if data.Key != nil {
287+
k := data.Key.Hex()[2:]
286288
if ecdsaKey, err := crypto.HexToECDSA(k); err != nil {
287289
return err
288290
} else {
289291
t.key = ecdsaKey
290292
}
291293
}
294+
if data.Protected != nil {
295+
t.protected = *data.Protected
296+
} else {
297+
t.protected = true
298+
}
292299
// Now, read the transaction itself
293300
var tx types.Transaction
294301
if err := json.Unmarshal(input, &tx); err != nil {
@@ -317,7 +324,15 @@ func signUnsignedTransactions(txs []*txWithKey, signer types.Signer) (types.Tran
317324
v, r, s := tx.RawSignatureValues()
318325
if key != nil && v.BitLen()+r.BitLen()+s.BitLen() == 0 {
319326
// This transaction needs to be signed
320-
signed, err := types.SignTx(tx, signer, key)
327+
var (
328+
signed *types.Transaction
329+
err error
330+
)
331+
if txWithKey.protected {
332+
signed, err = types.SignTx(tx, signer, key)
333+
} else {
334+
signed, err = types.SignTx(tx, types.FrontierSigner{}, key)
335+
}
321336
if err != nil {
322337
return nil, NewError(ErrorJson, fmt.Errorf("tx %d: failed to sign tx: %v", i, err))
323338
}

cmd/evm/t8n_test.go

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -195,6 +195,14 @@ func TestT8n(t *testing.T) {
195195
output: t8nOutput{result: true},
196196
expOut: "exp_arrowglacier.json",
197197
},
198+
{ // Sign unprotected (pre-EIP155) transaction
199+
base: "./testdata/23",
200+
input: t8nInput{
201+
"alloc.json", "txs.json", "env.json", "Berlin", "",
202+
},
203+
output: t8nOutput{result: true},
204+
expOut: "exp.json",
205+
},
198206
} {
199207

200208
args := []string{"t8n"}

cmd/evm/testdata/23/alloc.json

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,16 @@
1+
{
2+
"0x095e7baea6a6c7c4c2dfeb977efac326af552d87" : {
3+
"balance" : "0x0de0b6b3a7640000",
4+
"code" : "0x6001",
5+
"nonce" : "0x00",
6+
"storage" : {
7+
}
8+
},
9+
"0xa94f5374fce5edbc8e2a8697c15331677e6ebf0b" : {
10+
"balance" : "0x0de0b6b3a7640000",
11+
"code" : "0x",
12+
"nonce" : "0x00",
13+
"storage" : {
14+
}
15+
}
16+
}

cmd/evm/testdata/23/env.json

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,7 @@
1+
{
2+
"currentCoinbase" : "0x2adc25665018aa1fe0e6bc666dac8fc2697ff9ba",
3+
"currentDifficulty" : "0x020000",
4+
"currentGasLimit" : "0x3b9aca00",
5+
"currentNumber" : "0x05",
6+
"currentTimestamp" : "0x03e8"
7+
}

cmd/evm/testdata/23/exp.json

Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,25 @@
1+
{
2+
"result": {
3+
"stateRoot": "0x65334305e4accfa18352deb24f007b837b5036425b0712cf0e65a43bfa95154d",
4+
"txRoot": "0x75e61774a2ff58cbe32653420256c7f44bc715715a423b0b746d5c622979af6b",
5+
"receiptsRoot": "0xf951f9396af203499cc7d379715a9110323de73967c5700e2f424725446a3c76",
6+
"logsHash": "0x1dcc4de8dec75d7aab85b567b6ccd41ad312451b948a7413f0a142fd40d49347",
7+
"logsBloom": "0x00000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000",
8+
"receipts": [
9+
{
10+
"root": "0x",
11+
"status": "0x1",
12+
"cumulativeGasUsed": "0x520b",
13+
"logsBloom": "0x00000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000",
14+
"logs": null,
15+
"transactionHash": "0x72fadbef39cd251a437eea619cfeda752271a5faaaa2147df012e112159ffb81",
16+
"contractAddress": "0x0000000000000000000000000000000000000000",
17+
"gasUsed": "0x520b",
18+
"blockHash": "0x0000000000000000000000000000000000000000000000000000000000000000",
19+
"transactionIndex": "0x0"
20+
}
21+
],
22+
"currentDifficulty": "0x20000",
23+
"gasUsed": "0x520b"
24+
}
25+
}

cmd/evm/testdata/23/readme.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
These files examplify how to sign a transaction using the pre-EIP155 scheme.

cmd/evm/testdata/23/txs.json

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,15 @@
1+
[
2+
{
3+
"input" : "0x",
4+
"gas" : "0x5f5e100",
5+
"gasPrice" : "0x1",
6+
"nonce" : "0x0",
7+
"to" : "0x095e7baea6a6c7c4c2dfeb977efac326af552d87",
8+
"value" : "0x186a0",
9+
"v" : "0x0",
10+
"r" : "0x0",
11+
"s" : "0x0",
12+
"secretKey" : "0x45a915e4d060149eb4365960e6a7a45f334393093061116b197e3240065ff2d8",
13+
"protected": false
14+
}
15+
]

0 commit comments

Comments
 (0)