Skip to content

Commit

Permalink
Fixed recovery id for ECDSA signatures in secret generation.
Browse files Browse the repository at this point in the history
  • Loading branch information
StefanIliev545 committed Feb 9, 2024
1 parent 2214997 commit 1c8f91d
Show file tree
Hide file tree
Showing 5 changed files with 66 additions and 9 deletions.

Large diffs are not rendered by default.

3 changes: 2 additions & 1 deletion contracts/src/management/ManagementContract.sol
Original file line number Diff line number Diff line change
Expand Up @@ -118,6 +118,7 @@ contract ManagementContract is Initializable, OwnableUpgradeable {
require(isAggAttested);

if (verifyAttester) {

// the data must be signed with by the correct private key
// signature = f(PubKey, PrivateKey, message)
// address = f(signature, message)
Expand All @@ -126,7 +127,7 @@ contract ManagementContract is Initializable, OwnableUpgradeable {
address recoveredAddrSignedCalculated = ECDSA.recover(calculatedHashSigned, attesterSig);

require(recoveredAddrSignedCalculated == attesterID, "calculated address and attesterID dont match");
}
}

// mark the requesterID aggregator as an attested aggregator and store its host address
attested[requesterID] = true;
Expand Down
17 changes: 10 additions & 7 deletions go/ethadapter/l1_transaction.go
Original file line number Diff line number Diff line change
Expand Up @@ -46,17 +46,20 @@ func (l *L1RespondSecretTx) Sign(privateKey *ecdsa.PrivateKey) *L1RespondSecretT
data = append(data, l.HostAddress...)
data = append(data, string(l.Secret)...)

// form the data
msg := fmt.Sprintf("\x19Ethereum Signed Message:\n%d%s", len(data), string(data))
// hash the data
hashedData := crypto.Keccak256Hash([]byte(msg))
ethereumMessageHash := func(data []byte) []byte {
prefix := fmt.Sprintf("\x19Ethereum Signed Message:\n%d", len(data))
return crypto.Keccak256([]byte(prefix), data)
}

hashedData := ethereumMessageHash(data)
// sign the hash
signedHash, err := crypto.Sign(hashedData.Bytes(), privateKey)
signedHash, err := crypto.Sign(hashedData, privateKey)
if err != nil {
return nil
}
// remove ECDSA recovery id
signedHash = signedHash[:len(signedHash)-1]

//set recovery id to 27; prevent malleable signatures

Check failure on line 61 in go/ethadapter/l1_transaction.go

View workflow job for this annotation

GitHub Actions / lint

File is not `gofumpt`-ed (gofumpt)
signedHash[64] += 27
l.AttesterSig = signedHash
return l
}
Expand Down
34 changes: 34 additions & 0 deletions integration/simulation/network/geth_utils.go
Original file line number Diff line number Diff line change
Expand Up @@ -177,6 +177,40 @@ func StopEth2Network(clients []ethadapter.EthClient, netw eth2network.Eth2Networ
}
}

func InitializeContract(workerClient ethadapter.EthClient, w wallet.Wallet, contractAddress common.Address) (*types.Receipt, error) {
ctr, err := ManagementContract.NewManagementContract(contractAddress, workerClient.EthClient())
if err != nil {
return nil, err
}

opts, err := bind.NewKeyedTransactorWithChainID(w.PrivateKey(), w.ChainID())
if err != nil {
return nil, err
}

tx, err := ctr.Initialize(opts)
if err != nil {
return nil, err
}
w.SetNonce(w.GetNonce())

var start time.Time
var receipt *types.Receipt
// todo (@matt) these timings should be driven by the L2 batch times and L1 block times
for start = time.Now(); time.Since(start) < 80*time.Second; time.Sleep(2 * time.Second) {
receipt, err = workerClient.TransactionReceipt(tx.Hash())
if err == nil && receipt != nil {
if receipt.Status != types.ReceiptStatusSuccessful {
return nil, errors.New("unable to initialize contract")
}
testlog.Logger().Info(fmt.Sprintf("Contract initialized"))

Check failure on line 206 in integration/simulation/network/geth_utils.go

View workflow job for this annotation

GitHub Actions / lint

S1039: unnecessary use of fmt.Sprintf (gosimple)
return receipt, nil
}
}

return receipt, nil
}

// DeployContract returns receipt of deployment
// todo (@matt) - this should live somewhere else
func DeployContract(workerClient ethadapter.EthClient, w wallet.Wallet, contractBytes []byte) (*types.Receipt, error) {
Expand Down
19 changes: 19 additions & 0 deletions integration/smartcontract/smartcontracts_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -113,12 +113,31 @@ func TestManagementContract(t *testing.T) {
if err != nil {
panic(err)
}

nonce, err := client.Nonce(w.Address())
if err != nil {
t.Error(err)
}

w.SetNonce(nonce)
// deploy the same contract to a new address
receipt, err := network.DeployContract(client, w, bytecode)
if err != nil {
t.Error(err)
}

_, err = network.InitializeContract(client, w, receipt.ContractAddress)
if err != nil {
t.Error(err)
}

nonce, err = client.Nonce(w.Address())
if err != nil {
t.Error(err)
}

w.SetNonce(nonce)

// run the test using the new contract, but same wallet
test(t,
newDebugMgmtContractLib(receipt.ContractAddress, client.EthClient(),
Expand Down

0 comments on commit 1c8f91d

Please sign in to comment.