Skip to content

Commit 9b683fc

Browse files
Merge pull request #113 from 0xPolygon/fix/hash-to-sign
Fix/hash to sign
2 parents b48599a + 872bb66 commit 9b683fc

File tree

4 files changed

+38
-29
lines changed

4 files changed

+38
-29
lines changed

go.mod

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,7 @@ require (
99
github.com/ethereum/go-ethereum v1.13.14
1010
github.com/gorilla/websocket v1.5.0
1111
github.com/hermeznetwork/tracerr v0.3.2
12+
github.com/iden3/go-iden3-crypto v0.0.16
1213
github.com/invopop/jsonschema v0.7.0
1314
github.com/jmoiron/sqlx v1.2.0
1415
github.com/lib/pq v1.10.7

go.sum

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -162,6 +162,8 @@ github.com/huin/goupnp v1.3.0/go.mod h1:gnGPsThkYa7bFi/KWmEysQRf48l2dvR5bxr2OFck
162162
github.com/iancoleman/orderedmap v0.0.0-20190318233801-ac98e3ecb4b0 h1:i462o439ZjprVSFSZLZxcsoAe592sZB1rci2Z8j4wdk=
163163
github.com/iancoleman/orderedmap v0.0.0-20190318233801-ac98e3ecb4b0/go.mod h1:N0Wam8K1arqPXNWjMo21EXnBPOPp36vB07FNRdD2geA=
164164
github.com/ianlancetaylor/demangle v0.0.0-20200824232613-28f6c0f3b639/go.mod h1:aSSvb/t6k1mPoxDqO4vJh6VOCGPwU4O0C2/Eqndh1Sc=
165+
github.com/iden3/go-iden3-crypto v0.0.16 h1:zN867xiz6HgErXVIV/6WyteGcOukE9gybYTorBMEdsk=
166+
github.com/iden3/go-iden3-crypto v0.0.16/go.mod h1:dLpM4vEPJ3nDHzhWFXDjzkn1qHoBeOT/3UEhXsEsP3E=
165167
github.com/invopop/jsonschema v0.7.0 h1:2vgQcBz1n256N+FpX3Jq7Y17AjYt46Ig3zIWyy770So=
166168
github.com/invopop/jsonschema v0.7.0/go.mod h1:O9uiLokuu0+MGFlyiaqtWxwqJm41/+8Nj0lD7A36YH0=
167169
github.com/jackpal/go-nat-pmp v1.0.2 h1:KzKSgb7qkJvOUTqYl9/Hg/me3pWgBmERKrTGD7BdWus=

services/datacom/datacom.go

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -6,9 +6,11 @@ import (
66
"fmt"
77

88
"github.com/0xPolygon/cdk-data-availability/db"
9+
"github.com/0xPolygon/cdk-data-availability/log"
910
"github.com/0xPolygon/cdk-data-availability/rpc"
1011
"github.com/0xPolygon/cdk-data-availability/sequencer"
1112
"github.com/0xPolygon/cdk-data-availability/types"
13+
"github.com/ethereum/go-ethereum/common"
1214
)
1315

1416
// APIDATACOM is the namespace of the datacom service
@@ -41,6 +43,7 @@ func (d *Endpoints) SignSequence(signedSequence types.SignedSequence) (interface
4143
// After storing the data that will be sent hashed to the contract, it returns the signature.
4244
// This endpoint is only accessible to the sequencer
4345
func (d *Endpoints) SignSequenceBanana(signedSequence types.SignedSequenceBanana) (interface{}, rpc.Error) {
46+
log.Debugf("signing sequence, hash to sign: %s", common.BytesToHash(signedSequence.Sequence.HashToSign()))
4447
return d.signSequence(&signedSequence)
4548
}
4649

types/sequencebanana.go

Lines changed: 32 additions & 29 deletions
Original file line numberDiff line numberDiff line change
@@ -3,10 +3,11 @@ package types
33
import (
44
"crypto/ecdsa"
55
"errors"
6+
"math/big"
67

78
"github.com/ethereum/go-ethereum/common"
89
"github.com/ethereum/go-ethereum/crypto"
9-
solsha3 "github.com/miguelmota/go-solidity-sha3"
10+
"github.com/iden3/go-iden3-crypto/keccak256"
1011
)
1112

1213
// Batch represents the batch data that the sequencer will send to L1
@@ -30,39 +31,41 @@ type SequenceBanana struct {
3031
// HashToSign returns the accumulated input hash of the sequence.
3132
// Note that this is equivalent to what happens on the smart contract
3233
func (s *SequenceBanana) HashToSign() []byte {
33-
currentHash := s.OldAccInputHash.Bytes()
34+
v1 := s.OldAccInputHash.Bytes()
3435
for _, b := range s.Batches {
35-
types := []string{
36-
"bytes32", // oldAccInputHash
37-
"bytes32", // currentTransactionsHash
38-
"bytes32", // forcedGlobalExitRoot or l1InfoRoot
39-
"uint64", // forcedTimestamp
40-
"address", // coinbase
41-
"bytes32", // forcedBlockHashL1
42-
}
43-
var values []interface{}
36+
v2 := b.L2Data
37+
var v3, v4 []byte
4438
if b.ForcedTimestamp > 0 {
45-
values = []interface{}{
46-
currentHash,
47-
crypto.Keccak256(b.L2Data),
48-
b.ForcedGER,
49-
b.ForcedTimestamp,
50-
b.Coinbase,
51-
b.ForcedBlockHashL1,
52-
}
39+
v3 = b.ForcedGER.Bytes()
40+
v4 = big.NewInt(0).SetUint64(uint64(b.ForcedTimestamp)).Bytes()
5341
} else {
54-
values = []interface{}{
55-
currentHash,
56-
crypto.Keccak256(b.L2Data),
57-
s.L1InfoRoot,
58-
s.MaxSequenceTimestamp,
59-
b.Coinbase,
60-
common.Hash{},
61-
}
42+
v3 = s.L1InfoRoot.Bytes()
43+
v4 = big.NewInt(0).SetUint64(uint64(s.MaxSequenceTimestamp)).Bytes()
44+
}
45+
v5 := b.Coinbase.Bytes()
46+
v6 := b.ForcedBlockHashL1.Bytes()
47+
48+
// Add 0s to make values 32 bytes long
49+
for len(v1) < 32 {
50+
v1 = append([]byte{0}, v1...)
51+
}
52+
v2 = keccak256.Hash(v2)
53+
for len(v3) < 32 {
54+
v3 = append([]byte{0}, v3...)
6255
}
63-
currentHash = solsha3.SoliditySHA3(types, values)
56+
for len(v4) < 8 {
57+
v4 = append([]byte{0}, v4...)
58+
}
59+
for len(v5) < 20 {
60+
v5 = append([]byte{0}, v5...)
61+
}
62+
for len(v6) < 32 {
63+
v6 = append([]byte{0}, v6...)
64+
}
65+
v1 = keccak256.Hash(v1, v2, v3, v4, v5, v6)
6466
}
65-
return currentHash
67+
68+
return v1
6669
}
6770

6871
// Sign returns a signed sequence by the private key.

0 commit comments

Comments
 (0)