Skip to content

Commit 58ac923

Browse files
Util: use hedged signatures by default (#3873)
* util: use hedged signatures by default * client: add comment about defaulting to wasm * tx: add extraEntropy params * vm: fix vm tests by forcing non-extra-entropy * util: fix and add tests for extraEntropy * tx: fix tests and add tests for hedged signatures * client: fix tests * tx: fix tests * vm: ensure 7702 authorization have no leading zeros * tx: add more robust hedged signature test * client: remove todo comment * util: update sign() docs with more clear text * util: pack chainId / extraEntropy into a single arg option * util: add docs for better visual docs * util: ecsign ban 2/3 recovery values
1 parent a3e7361 commit 58ac923

25 files changed

+209
-61
lines changed

packages/client/bin/utils.ts

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -647,11 +647,16 @@ export async function generateClientConfig(args: ClientOpts) {
647647
),
648648
).slice(1)
649649
cryptoFunctions.sha256 = wasmSha256
650-
cryptoFunctions.ecsign = (msg: Uint8Array, pk: Uint8Array, chainId?: bigint) => {
650+
cryptoFunctions.ecsign = (
651+
msg: Uint8Array,
652+
pk: Uint8Array,
653+
ecSignOpts: { chainId?: bigint } = {},
654+
) => {
651655
if (msg.length < 32) {
652656
// WASM errors with `unreachable` if we try to pass in less than 32 bytes in the message
653657
throw new Error('message length must be 32 bytes or greater')
654658
}
659+
const { chainId } = ecSignOpts
655660
const buf = secp256k1Sign(msg, pk)
656661
const r = buf.slice(0, 32)
657662
const s = buf.slice(32, 64)

packages/client/test/rpc/engine/getPayloadV3.spec.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -108,7 +108,7 @@ describe(method, () => {
108108
to: createZeroAddress(),
109109
},
110110
{ common },
111-
).sign(pkey)
111+
).sign(pkey, false)
112112

113113
await service.txPool.add(tx, true)
114114

packages/client/test/rpc/engine/newPayloadV1.spec.ts

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -186,7 +186,7 @@ describe(method, () => {
186186
gasLimit: 53_000,
187187
},
188188
{ common },
189-
).sign(accountPk)
189+
).sign(accountPk, false)
190190
const transactions = [bytesToHex(tx.serialize())]
191191
const blockDataWithValidTransaction = {
192192
...blockData,
@@ -230,7 +230,7 @@ describe(method, () => {
230230
gasLimit: 53_000,
231231
},
232232
{ common },
233-
).sign(accountPk)
233+
).sign(accountPk, false)
234234

235235
return bytesToHex(tx.serialize())
236236
})

packages/client/test/rpc/engine/newPayloadV2.spec.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -183,7 +183,7 @@ describe(`${method}: call with executionPayloadV1`, () => {
183183
gasLimit: 53_000,
184184
},
185185
{ common },
186-
).sign(accountPk)
186+
).sign(accountPk, false)
187187
const transactions = [bytesToHex(tx.serialize())]
188188
const blockDataWithValidTransaction = {
189189
...blockData,

packages/client/test/rpc/eth/getTransactionByBlockHashAndIndex.spec.ts

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -19,11 +19,11 @@ async function setUp() {
1919
to: '0x0000000000000000000000000000000000000000',
2020
},
2121
{ common },
22-
).sign(dummy.privKey),
22+
).sign(dummy.privKey, false),
2323
createLegacyTx(
2424
{ gasLimit: 21000, gasPrice: 50, nonce: 1, to: '0x0000000000000000000000000000000000000000' },
2525
{ common },
26-
).sign(dummy.privKey),
26+
).sign(dummy.privKey, false),
2727
]
2828

2929
await runBlockWithTxs(chain, execution, txs)

packages/client/test/rpc/eth/sendRawTransaction.spec.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -83,7 +83,7 @@ describe(method, () => {
8383
gasLimit: 21000,
8484
gasPrice: 0,
8585
nonce: 0,
86-
}).sign(hexToBytes(`0x${'42'.repeat(32)}`))
86+
}).sign(hexToBytes(`0x${'42'.repeat(32)}`), false)
8787

8888
const txData = bytesToHex(transaction.serialize())
8989

packages/client/test/util/wasmCrypto.spec.ts

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -50,8 +50,8 @@ describe('WASM crypto tests', () => {
5050
const common = new Common({ chain: Mainnet })
5151

5252
const pk = randomBytes(32)
53-
const tx = createLegacyTx({}, { common }).sign(pk)
54-
const tx2 = createLegacyTx({}, { common: commonWithCustomCrypto }).sign(pk)
53+
const tx = createLegacyTx({}, { common }).sign(pk, false)
54+
const tx2 = createLegacyTx({}, { common: commonWithCustomCrypto }).sign(pk, false)
5555

5656
assert.deepEqual(tx.getSenderPublicKey(), tx2.getSenderPublicKey())
5757
assert.deepEqual(tx.hash(), tx2.hash())
@@ -85,7 +85,7 @@ describe('WASM crypto tests', () => {
8585
await waitReady()
8686
const msg = hexToBytes('0x82ff40c0a986c6a5cfad4ddf4c3aa6996f1a7837f9c398e17e5de5cbd5a12b28')
8787
const pk = hexToBytes('0x3c9229289a6125f7fdf1885a77bb12c37a8d3b4962d936f7e3084dece32a3ca1')
88-
const jsSig = ecsign(msg, pk)
88+
const jsSig = ecsign(msg, pk, { extraEntropy: false })
8989
const wasmSig = wasmSign(msg, pk)
9090
assert.deepEqual(wasmSig, jsSig, 'wasm signatures produce same result as js signatures')
9191
assert.throws(

packages/common/src/types.ts

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -91,7 +91,11 @@ export interface CustomCrypto {
9191
chainId?: bigint,
9292
) => Uint8Array
9393
sha256?: (msg: Uint8Array) => Uint8Array
94-
ecsign?: (msg: Uint8Array, pk: Uint8Array, chainId?: bigint) => ECDSASignature
94+
ecsign?: (
95+
msg: Uint8Array,
96+
pk: Uint8Array,
97+
ecSignOpts?: { chainId?: bigint; extraEntropy?: Uint8Array | boolean },
98+
) => ECDSASignature
9599
ecdsaSign?: (msg: Uint8Array, pk: Uint8Array) => { signature: Uint8Array; recid: number }
96100
ecdsaRecover?: (sig: Uint8Array, recId: number, hash: Uint8Array) => Uint8Array
97101
kzg?: KZG

packages/common/test/customCrypto.spec.ts

Lines changed: 11 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -24,8 +24,16 @@ describe('[Common]: Custom Crypto', () => {
2424
return msg
2525
}
2626

27-
const customEcSign = (_msg: Uint8Array, _pk: Uint8Array, chainId?: bigint): ECDSASignature => {
28-
return { v: chainId ?? 27n, r: Uint8Array.from([0, 1, 2, 3]), s: Uint8Array.from([0, 1, 2, 3]) }
27+
const customEcSign = (
28+
_msg: Uint8Array,
29+
_pk: Uint8Array,
30+
ecSignOpts?: { chainId?: bigint; extraEntropy?: Uint8Array | boolean },
31+
): ECDSASignature => {
32+
return {
33+
v: ecSignOpts?.chainId ?? 27n,
34+
r: Uint8Array.from([0, 1, 2, 3]),
35+
s: Uint8Array.from([0, 1, 2, 3]),
36+
}
2937
}
3038

3139
it('keccak256', () => {
@@ -77,7 +85,7 @@ describe('[Common]: Custom Crypto', () => {
7785
ecsign: customEcSign,
7886
}
7987
const c = new Common({ chain: Mainnet, customCrypto })
80-
assert.equal(c.customCrypto.ecsign!(randomBytes(32), randomBytes(32), 0n).v, 0n)
88+
assert.equal(c.customCrypto.ecsign!(randomBytes(32), randomBytes(32), { chainId: 0n }).v, 0n)
8189
assert.equal(c.customCrypto.ecsign!(randomBytes(32), randomBytes(32)).v, 27n)
8290
})
8391
})

packages/tx/src/1559/tx.ts

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -357,8 +357,8 @@ export class FeeMarket1559Tx implements TransactionInterface<TransactionType.Fee
357357
return Legacy.getSenderAddress(this)
358358
}
359359

360-
sign(privateKey: Uint8Array): FeeMarket1559Tx {
361-
return <FeeMarket1559Tx>Legacy.sign(this, privateKey)
360+
sign(privateKey: Uint8Array, extraEntropy: Uint8Array | boolean = true): FeeMarket1559Tx {
361+
return <FeeMarket1559Tx>Legacy.sign(this, privateKey, extraEntropy)
362362
}
363363

364364
public isSigned(): boolean {

0 commit comments

Comments
 (0)
pFad - Phonifier reborn

Pfad - The Proxy pFad of © 2024 Garber Painting. All rights reserved.

Note: This service is not intended for secure transactions such as banking, social media, email, or purchasing. Use at your own risk. We assume no liability whatsoever for broken pages.


Alternative Proxies:

Alternative Proxy

pFad Proxy

pFad v3 Proxy

pFad v4 Proxy