@@ -16,13 +16,13 @@ import {
16
16
} from '@ethereumjs/common'
17
17
import {
18
18
EthereumJSErrorWithoutCode ,
19
+ bytesToBigInt ,
19
20
bytesToHex ,
20
21
calculateSigRecovery ,
21
22
concatBytes ,
22
23
createAddressFromPrivateKey ,
23
24
createAddressFromString ,
24
25
ecrecover ,
25
- ecsign ,
26
26
hexToBytes ,
27
27
parseGethGenesisState ,
28
28
randomBytes ,
@@ -38,7 +38,7 @@ import {
38
38
sha256 as wasmSha256 ,
39
39
} from '@polkadot/wasm-crypto'
40
40
import { keccak256 } from 'ethereum-cryptography/keccak.js'
41
- import { ecdsaRecover , ecdsaSign } from 'ethereum-cryptography/secp256k1-compat .js'
41
+ import { secp256k1 } from 'ethereum-cryptography/secp256k1.js'
42
42
import { sha256 } from 'ethereum-cryptography/sha256.js'
43
43
import { KZG as microEthKZG } from 'micro-eth-signer/kzg'
44
44
import * as verkle from 'micro-eth-signer/verkle'
@@ -626,16 +626,12 @@ function generateAccount(): Account {
626
626
return [ address , privKey ]
627
627
}
628
628
629
- export async function generateClientConfig ( args : ClientOpts ) {
630
- // Give chainId priority over networkId
631
- // Give networkId precedence over network name
632
- const chainName = args . chainId ?? args . networkId ?? args . network ?? Chain . Mainnet
633
- const chain = getPresetChainConfig ( chainName )
629
+ export async function getCryptoFunctions ( useJsCrypto : boolean ) : Promise < CustomCrypto > {
634
630
const cryptoFunctions : CustomCrypto = { }
635
631
636
632
const kzg = new microEthKZG ( trustedSetup )
637
633
// Initialize WASM crypto if JS crypto is not specified
638
- if ( args . useJsCrypto === false ) {
634
+ if ( useJsCrypto === false ) {
639
635
await waitReadyPolkadotSha256 ( )
640
636
cryptoFunctions . keccak256 = keccak256WASM
641
637
cryptoFunctions . ecrecover = (
@@ -654,23 +650,12 @@ export async function generateClientConfig(args: ClientOpts) {
654
650
) . slice ( 1 )
655
651
cryptoFunctions . sha256 = wasmSha256
656
652
cryptoFunctions . ecsign = ( msg : Uint8Array , pk : Uint8Array ) => {
657
- if ( msg . length < 32 ) {
658
- // WASM errors with `unreachable` if we try to pass in less than 32 bytes in the message
659
- throw EthereumJSErrorWithoutCode ( 'message length must be 32 bytes or greater' )
660
- }
661
653
const buf = secp256k1Sign ( msg , pk )
662
- const r = buf . slice ( 0 , 32 )
663
- const s = buf . slice ( 32 , 64 )
664
- const v = BigInt ( buf [ 64 ] )
654
+ const r = bytesToBigInt ( buf . slice ( 0 , 32 ) )
655
+ const s = bytesToBigInt ( buf . slice ( 32 , 64 ) )
656
+ const recovery = buf [ 64 ]
665
657
666
- return { r, s, v }
667
- }
668
- cryptoFunctions . ecdsaSign = ( hash : Uint8Array , pk : Uint8Array ) => {
669
- const sig = secp256k1Sign ( hash , pk )
670
- return {
671
- signature : sig . slice ( 0 , 64 ) ,
672
- recid : sig [ 64 ] ,
673
- }
658
+ return { r, s, recovery }
674
659
}
675
660
cryptoFunctions . ecdsaRecover = ( sig : Uint8Array , recId : number , hash : Uint8Array ) => {
676
661
return secp256k1Recover ( hash , sig , recId )
@@ -679,12 +664,29 @@ export async function generateClientConfig(args: ClientOpts) {
679
664
cryptoFunctions . keccak256 = keccak256
680
665
cryptoFunctions . ecrecover = ecrecover
681
666
cryptoFunctions . sha256 = sha256
682
- cryptoFunctions . ecsign = ecsign
683
- cryptoFunctions . ecdsaSign = ecdsaSign
684
- cryptoFunctions . ecdsaRecover = ecdsaRecover
667
+ cryptoFunctions . ecsign = secp256k1 . sign
668
+ cryptoFunctions . ecdsaRecover = ( sig : Uint8Array , recId : number , hash : Uint8Array ) => {
669
+ // Adapted from @noble /curves docs
670
+ const sign = secp256k1 . Signature . fromCompact ( sig )
671
+ const point = sign . addRecoveryBit ( recId ) . recoverPublicKey ( hash )
672
+ const address = point . toRawBytes ( true )
673
+ return address
674
+ }
685
675
}
686
676
cryptoFunctions . kzg = kzg
687
677
cryptoFunctions . verkle = verkle
678
+ return cryptoFunctions
679
+ }
680
+
681
+ export async function generateClientConfig ( args : ClientOpts ) {
682
+ // Give chainId priority over networkId
683
+ // Give networkId precedence over network name
684
+ const chainName = args . chainId ?? args . networkId ?? args . network ?? Chain . Mainnet
685
+ const chain = getPresetChainConfig ( chainName )
686
+
687
+ // `useJsCrypto` defaults to `false` in the CLI defaults
688
+ const cryptoFunctions = await getCryptoFunctions ( args . useJsCrypto ?? false )
689
+
688
690
// Configure accounts for mining and prefunding in a local devnet
689
691
const accounts : Account [ ] = [ ]
690
692
if ( typeof args . unlock === 'string' ) {
0 commit comments