You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

490 lines
17 KiB

  1. // Package common contains all the common data structures used at the
  2. // hermez-node, zk.go contains the zkSnark inputs used to generate the proof
  3. package common
  4. import (
  5. "crypto/sha256"
  6. "encoding/binary"
  7. "encoding/json"
  8. "fmt"
  9. "math/big"
  10. "github.com/hermeznetwork/hermez-node/log"
  11. cryptoConstants "github.com/iden3/go-iden3-crypto/constants"
  12. "github.com/iden3/go-merkletree"
  13. "github.com/mitchellh/mapstructure"
  14. )
  15. // ZKMetadata contains ZKInputs metadata that is not used directly in the
  16. // ZKInputs result, but to calculate values for Hash check
  17. type ZKMetadata struct {
  18. // Circuit parameters
  19. // absolute maximum of L1 or L2 transactions allowed
  20. NTx uint32
  21. // merkle tree depth
  22. NLevels uint32
  23. MaxLevels uint32
  24. // absolute maximum of L1 transaction allowed
  25. MaxL1Tx uint32
  26. // total txs allowed
  27. MaxTx uint32
  28. // Maximum number of Idxs where Fees can be send in a batch (currently
  29. // is constant for all circuits: 64)
  30. MaxFeeIdxs uint32
  31. L1TxsData [][]byte
  32. L2TxsData [][]byte
  33. ChainID uint16
  34. NewLastIdxRaw Idx
  35. NewStateRootRaw *merkletree.Hash
  36. NewExitRootRaw *merkletree.Hash
  37. }
  38. // ZKInputs represents the inputs that will be used to generate the zkSNARK proof
  39. type ZKInputs struct {
  40. Metadata ZKMetadata `json:"-"`
  41. //
  42. // General
  43. //
  44. // inputs for final `hashGlobalInputs`
  45. // OldLastIdx is the last index assigned to an account
  46. OldLastIdx *big.Int `json:"oldLastIdx"` // uint64 (max nLevels bits)
  47. // OldStateRoot is the current state merkle tree root
  48. OldStateRoot *big.Int `json:"oldStateRoot"` // Hash
  49. // GlobalChainID is the blockchain ID (0 for Ethereum mainnet). This value can be get from the smart contract.
  50. GlobalChainID *big.Int `json:"globalChainID"` // uint16
  51. // FeeIdxs is an array of merkle tree indexes where the coordinator will receive the accumulated fees
  52. FeeIdxs []*big.Int `json:"feeIdxs"` // uint64 (max nLevels bits), len: [maxFeeIdxs]
  53. // accumulate fees
  54. // FeePlanTokens contains all the tokenIDs for which the fees are being accumulated
  55. FeePlanTokens []*big.Int `json:"feePlanTokens"` // uint32 (max 32 bits), len: [maxFeeIdxs]
  56. //
  57. // Txs (L1&L2)
  58. //
  59. // transaction L1-L2
  60. // TxCompressedData
  61. TxCompressedData []*big.Int `json:"txCompressedData"` // big.Int (max 251 bits), len: [nTx]
  62. // TxCompressedDataV2, only used in L2Txs, in L1Txs is set to 0
  63. TxCompressedDataV2 []*big.Int `json:"txCompressedDataV2"` // big.Int (max 193 bits), len: [nTx]
  64. // FromIdx
  65. FromIdx []*big.Int `json:"fromIdx"` // uint64 (max nLevels bits), len: [nTx]
  66. // AuxFromIdx is the Idx of the new created account which is consequence of a L1CreateAccountTx
  67. AuxFromIdx []*big.Int `json:"auxFromIdx"` // uint64 (max nLevels bits), len: [nTx]
  68. // ToIdx
  69. ToIdx []*big.Int `json:"toIdx"` // uint64 (max nLevels bits), len: [nTx]
  70. // AuxToIdx is the Idx of the Tx that has 'toIdx==0', is the coordinator who will find which Idx corresponds to the 'toBJJAy' or 'toEthAddr'
  71. AuxToIdx []*big.Int `json:"auxToIdx"` // uint64 (max nLevels bits), len: [nTx]
  72. // ToBJJAy
  73. ToBJJAy []*big.Int `json:"toBjjAy"` // big.Int, len: [nTx]
  74. // ToEthAddr
  75. ToEthAddr []*big.Int `json:"toEthAddr"` // ethCommon.Address, len: [nTx]
  76. // OnChain determines if is L1 (1/true) or L2 (0/false)
  77. OnChain []*big.Int `json:"onChain"` // bool, len: [nTx]
  78. //
  79. // Txs/L1Txs
  80. //
  81. // NewAccount boolean (0/1) flag set 'true' when L1 tx creates a new account (fromIdx==0)
  82. NewAccount []*big.Int `json:"newAccount"` // bool, len: [nTx]
  83. // LoadAmountF encoded as float16
  84. LoadAmountF []*big.Int `json:"loadAmountF"` // uint16, len: [nTx]
  85. // FromEthAddr
  86. FromEthAddr []*big.Int `json:"fromEthAddr"` // ethCommon.Address, len: [nTx]
  87. // FromBJJCompressed boolean encoded where each value is a *big.Int
  88. FromBJJCompressed [][256]*big.Int `json:"fromBjjCompressed"` // bool array, len: [nTx][256]
  89. //
  90. // Txs/L2Txs
  91. //
  92. // RqOffset relative transaction position to be linked. Used to perform atomic transactions.
  93. RqOffset []*big.Int `json:"rqOffset"` // uint8 (max 3 bits), len: [nTx]
  94. // transaction L2 request data
  95. // RqTxCompressedDataV2
  96. RqTxCompressedDataV2 []*big.Int `json:"rqTxCompressedDataV2"` // big.Int (max 251 bits), len: [nTx]
  97. // RqToEthAddr
  98. RqToEthAddr []*big.Int `json:"rqToEthAddr"` // ethCommon.Address, len: [nTx]
  99. // RqToBJJAy
  100. RqToBJJAy []*big.Int `json:"rqToBjjAy"` // big.Int, len: [nTx]
  101. // transaction L2 signature
  102. // S
  103. S []*big.Int `json:"s"` // big.Int, len: [nTx]
  104. // R8x
  105. R8x []*big.Int `json:"r8x"` // big.Int, len: [nTx]
  106. // R8y
  107. R8y []*big.Int `json:"r8y"` // big.Int, len: [nTx]
  108. //
  109. // State MerkleTree Leafs transitions
  110. //
  111. // state 1, value of the sender (from) account leaf
  112. TokenID1 []*big.Int `json:"tokenID1"` // uint32, len: [nTx]
  113. Nonce1 []*big.Int `json:"nonce1"` // uint64 (max 40 bits), len: [nTx]
  114. Sign1 []*big.Int `json:"sign1"` // bool, len: [nTx]
  115. Ay1 []*big.Int `json:"ay1"` // big.Int, len: [nTx]
  116. Balance1 []*big.Int `json:"balance1"` // big.Int (max 192 bits), len: [nTx]
  117. EthAddr1 []*big.Int `json:"ethAddr1"` // ethCommon.Address, len: [nTx]
  118. Siblings1 [][]*big.Int `json:"siblings1"` // big.Int, len: [nTx][nLevels + 1]
  119. // Required for inserts and deletes, values of the CircomProcessorProof (smt insert proof)
  120. IsOld0_1 []*big.Int `json:"isOld0_1"` // bool, len: [nTx]
  121. OldKey1 []*big.Int `json:"oldKey1"` // uint64 (max 40 bits), len: [nTx]
  122. OldValue1 []*big.Int `json:"oldValue1"` // Hash, len: [nTx]
  123. // state 2, value of the receiver (to) account leaf
  124. // if Tx is an Exit, state 2 is used for the Exit Merkle Proof
  125. TokenID2 []*big.Int `json:"tokenID2"` // uint32, len: [nTx]
  126. Nonce2 []*big.Int `json:"nonce2"` // uint64 (max 40 bits), len: [nTx]
  127. Sign2 []*big.Int `json:"sign2"` // bool, len: [nTx]
  128. Ay2 []*big.Int `json:"ay2"` // big.Int, len: [nTx]
  129. Balance2 []*big.Int `json:"balance2"` // big.Int (max 192 bits), len: [nTx]
  130. EthAddr2 []*big.Int `json:"ethAddr2"` // ethCommon.Address, len: [nTx]
  131. Siblings2 [][]*big.Int `json:"siblings2"` // big.Int, len: [nTx][nLevels + 1]
  132. // newExit determines if an exit transaction has to create a new leaf in the exit tree
  133. NewExit []*big.Int `json:"newExit"` // bool, len: [nTx]
  134. // Required for inserts and deletes, values of the CircomProcessorProof (smt insert proof)
  135. IsOld0_2 []*big.Int `json:"isOld0_2"` // bool, len: [nTx]
  136. OldKey2 []*big.Int `json:"oldKey2"` // uint64 (max 40 bits), len: [nTx]
  137. OldValue2 []*big.Int `json:"oldValue2"` // Hash, len: [nTx]
  138. // state 3, value of the account leaf receiver of the Fees
  139. // fee tx
  140. // State fees
  141. TokenID3 []*big.Int `json:"tokenID3"` // uint32, len: [maxFeeIdxs]
  142. Nonce3 []*big.Int `json:"nonce3"` // uint64 (max 40 bits), len: [maxFeeIdxs]
  143. Sign3 []*big.Int `json:"sign3"` // bool, len: [maxFeeIdxs]
  144. Ay3 []*big.Int `json:"ay3"` // big.Int, len: [maxFeeIdxs]
  145. Balance3 []*big.Int `json:"balance3"` // big.Int (max 192 bits), len: [maxFeeIdxs]
  146. EthAddr3 []*big.Int `json:"ethAddr3"` // ethCommon.Address, len: [maxFeeIdxs]
  147. Siblings3 [][]*big.Int `json:"siblings3"` // Hash, len: [maxFeeIdxs][nLevels + 1]
  148. //
  149. // Intermediate States
  150. //
  151. // Intermediate States to parallelize witness computation
  152. // Note: the Intermediate States (IS) of the last transaction does not
  153. // exist. Meaning that transaction 3 (4th) will fill the parameters
  154. // FromIdx[3] and ISOnChain[3], but last transaction (nTx-1) will fill
  155. // FromIdx[nTx-1] but will not fill ISOnChain. That's why IS have
  156. // length of nTx-1, while the other parameters have length of nTx.
  157. // Last transaction does not need intermediate state since its output
  158. // will not be used.
  159. // decode-tx
  160. // ISOnChain indicates if tx is L1 (true (1)) or L2 (false (0))
  161. ISOnChain []*big.Int `json:"imOnChain"` // bool, len: [nTx - 1]
  162. // ISOutIdx current index account for each Tx
  163. // Contains the index of the created account in case that the tx is of
  164. // account creation type.
  165. ISOutIdx []*big.Int `json:"imOutIdx"` // uint64 (max nLevels bits), len: [nTx - 1]
  166. // rollup-tx
  167. // ISStateRoot root at the moment of the Tx (once processed), the state root value once the Tx is processed into the state tree
  168. ISStateRoot []*big.Int `json:"imStateRoot"` // Hash, len: [nTx - 1]
  169. // ISExitTree root at the moment (once processed) of the Tx the value
  170. // once the Tx is processed into the exit tree
  171. ISExitRoot []*big.Int `json:"imExitRoot"` // Hash, len: [nTx - 1]
  172. // ISAccFeeOut accumulated fees once the Tx is processed. Contains the
  173. // array of FeeAccount Balances at each moment of each Tx processed.
  174. ISAccFeeOut [][]*big.Int `json:"imAccFeeOut"` // big.Int, len: [nTx - 1][maxFeeIdxs]
  175. // fee-tx
  176. // ISStateRootFee root at the moment of the Tx (once processed), the state root value once the Tx is processed into the state tree
  177. ISStateRootFee []*big.Int `json:"imStateRootFee"` // Hash, len: [maxFeeIdxs - 1]
  178. // ISInitStateRootFee state root once all L1-L2 tx are processed (before computing the fees-tx)
  179. ISInitStateRootFee *big.Int `json:"imInitStateRootFee"` // Hash
  180. // ISFinalAccFee final accumulated fees (before computing the fees-tx).
  181. // Contains the final values of the ISAccFeeOut parameter
  182. ISFinalAccFee []*big.Int `json:"imFinalAccFee"` // big.Int, len: [maxFeeIdxs - 1]
  183. }
  184. func bigIntsToStrings(v interface{}) interface{} {
  185. switch c := v.(type) {
  186. case *big.Int:
  187. return c.String()
  188. case []*big.Int:
  189. r := make([]interface{}, len(c))
  190. for i := range c {
  191. r[i] = bigIntsToStrings(c[i])
  192. }
  193. return r
  194. case [256]*big.Int:
  195. r := make([]interface{}, len(c))
  196. for i := range c {
  197. r[i] = bigIntsToStrings(c[i])
  198. }
  199. return r
  200. case [][]*big.Int:
  201. r := make([]interface{}, len(c))
  202. for i := range c {
  203. r[i] = bigIntsToStrings(c[i])
  204. }
  205. return r
  206. case [][256]*big.Int:
  207. r := make([]interface{}, len(c))
  208. for i := range c {
  209. r[i] = bigIntsToStrings(c[i])
  210. }
  211. return r
  212. case map[string]interface{}:
  213. // avoid printing a warning when there is a struct type
  214. default:
  215. log.Warnf("bigIntsToStrings unexpected type: %T\n", v)
  216. }
  217. return nil
  218. }
  219. // MarshalJSON implements the json marshaler for ZKInputs
  220. func (z ZKInputs) MarshalJSON() ([]byte, error) {
  221. var m map[string]interface{}
  222. dec, err := mapstructure.NewDecoder(&mapstructure.DecoderConfig{
  223. TagName: "json",
  224. Result: &m,
  225. })
  226. if err != nil {
  227. return nil, err
  228. }
  229. err = dec.Decode(z)
  230. if err != nil {
  231. return nil, err
  232. }
  233. for k, v := range m {
  234. m[k] = bigIntsToStrings(v)
  235. }
  236. return json.Marshal(m)
  237. }
  238. // NewZKInputs returns a pointer to an initialized struct of ZKInputs
  239. func NewZKInputs(nTx, maxL1Tx, maxTx, maxFeeIdxs, nLevels uint32) *ZKInputs {
  240. zki := &ZKInputs{}
  241. zki.Metadata.NTx = nTx
  242. zki.Metadata.MaxFeeIdxs = maxFeeIdxs
  243. zki.Metadata.NLevels = nLevels
  244. zki.Metadata.MaxLevels = uint32(48) //nolint:gomnd
  245. zki.Metadata.MaxL1Tx = maxL1Tx
  246. zki.Metadata.MaxTx = maxTx
  247. // General
  248. zki.OldLastIdx = big.NewInt(0)
  249. zki.OldStateRoot = big.NewInt(0)
  250. zki.GlobalChainID = big.NewInt(0)
  251. zki.FeeIdxs = newSlice(maxFeeIdxs)
  252. zki.FeePlanTokens = newSlice(maxFeeIdxs)
  253. // Txs
  254. zki.TxCompressedData = newSlice(nTx)
  255. zki.TxCompressedDataV2 = newSlice(nTx)
  256. zki.FromIdx = newSlice(nTx)
  257. zki.AuxFromIdx = newSlice(nTx)
  258. zki.ToIdx = newSlice(nTx)
  259. zki.AuxToIdx = newSlice(nTx)
  260. zki.ToBJJAy = newSlice(nTx)
  261. zki.ToEthAddr = newSlice(nTx)
  262. zki.OnChain = newSlice(nTx)
  263. zki.NewAccount = newSlice(nTx)
  264. // L1
  265. zki.LoadAmountF = newSlice(nTx)
  266. zki.FromEthAddr = newSlice(nTx)
  267. zki.FromBJJCompressed = make([][256]*big.Int, nTx)
  268. for i := 0; i < len(zki.FromBJJCompressed); i++ {
  269. // zki.FromBJJCompressed[i] = newSlice(256)
  270. for j := 0; j < 256; j++ {
  271. zki.FromBJJCompressed[i][j] = big.NewInt(0)
  272. }
  273. }
  274. // L2
  275. zki.RqOffset = newSlice(nTx)
  276. zki.RqTxCompressedDataV2 = newSlice(nTx)
  277. zki.RqToEthAddr = newSlice(nTx)
  278. zki.RqToBJJAy = newSlice(nTx)
  279. zki.S = newSlice(nTx)
  280. zki.R8x = newSlice(nTx)
  281. zki.R8y = newSlice(nTx)
  282. // State MerkleTree Leafs transitions
  283. zki.TokenID1 = newSlice(nTx)
  284. zki.Nonce1 = newSlice(nTx)
  285. zki.Sign1 = newSlice(nTx)
  286. zki.Ay1 = newSlice(nTx)
  287. zki.Balance1 = newSlice(nTx)
  288. zki.EthAddr1 = newSlice(nTx)
  289. zki.Siblings1 = make([][]*big.Int, nTx)
  290. for i := 0; i < len(zki.Siblings1); i++ {
  291. zki.Siblings1[i] = newSlice(nLevels + 1)
  292. }
  293. zki.IsOld0_1 = newSlice(nTx)
  294. zki.OldKey1 = newSlice(nTx)
  295. zki.OldValue1 = newSlice(nTx)
  296. zki.TokenID2 = newSlice(nTx)
  297. zki.Nonce2 = newSlice(nTx)
  298. zki.Sign2 = newSlice(nTx)
  299. zki.Ay2 = newSlice(nTx)
  300. zki.Balance2 = newSlice(nTx)
  301. zki.EthAddr2 = newSlice(nTx)
  302. zki.Siblings2 = make([][]*big.Int, nTx)
  303. for i := 0; i < len(zki.Siblings2); i++ {
  304. zki.Siblings2[i] = newSlice(nLevels + 1)
  305. }
  306. zki.NewExit = newSlice(nTx)
  307. zki.IsOld0_2 = newSlice(nTx)
  308. zki.OldKey2 = newSlice(nTx)
  309. zki.OldValue2 = newSlice(nTx)
  310. zki.TokenID3 = newSlice(maxFeeIdxs)
  311. zki.Nonce3 = newSlice(maxFeeIdxs)
  312. zki.Sign3 = newSlice(maxFeeIdxs)
  313. zki.Ay3 = newSlice(maxFeeIdxs)
  314. zki.Balance3 = newSlice(maxFeeIdxs)
  315. zki.EthAddr3 = newSlice(maxFeeIdxs)
  316. zki.Siblings3 = make([][]*big.Int, maxFeeIdxs)
  317. for i := 0; i < len(zki.Siblings3); i++ {
  318. zki.Siblings3[i] = newSlice(nLevels + 1)
  319. }
  320. // Intermediate States
  321. zki.ISOnChain = newSlice(nTx - 1)
  322. zki.ISOutIdx = newSlice(nTx - 1)
  323. zki.ISStateRoot = newSlice(nTx - 1)
  324. zki.ISExitRoot = newSlice(nTx - 1)
  325. zki.ISAccFeeOut = make([][]*big.Int, nTx-1)
  326. for i := 0; i < len(zki.ISAccFeeOut); i++ {
  327. zki.ISAccFeeOut[i] = newSlice(maxFeeIdxs)
  328. }
  329. zki.ISStateRootFee = newSlice(maxFeeIdxs - 1)
  330. zki.ISInitStateRootFee = big.NewInt(0)
  331. zki.ISFinalAccFee = newSlice(maxFeeIdxs - 1)
  332. return zki
  333. }
  334. // newSlice returns a []*big.Int slice of length n with values initialized at
  335. // 0.
  336. // Is used to initialize all *big.Ints of the ZKInputs data structure, so when
  337. // the transactions are processed and the ZKInputs filled, there is no need to
  338. // set all the elements, and if a transaction does not use a parameter, can be
  339. // leaved as it is in the ZKInputs, as will be 0, so later when using the
  340. // ZKInputs to generate the zkSnark proof there is no 'nil'/'null' values.
  341. func newSlice(n uint32) []*big.Int {
  342. s := make([]*big.Int, n)
  343. for i := 0; i < len(s); i++ {
  344. s[i] = big.NewInt(0)
  345. }
  346. return s
  347. }
  348. // HashGlobalData returns the HashGlobalData
  349. func (z ZKInputs) HashGlobalData() (*big.Int, error) {
  350. b, err := z.ToHashGlobalData()
  351. if err != nil {
  352. return nil, err
  353. }
  354. h := sha256.New()
  355. _, err = h.Write(b)
  356. if err != nil {
  357. return nil, err
  358. }
  359. r := new(big.Int).SetBytes(h.Sum(nil))
  360. v := r.Mod(r, cryptoConstants.Q)
  361. return v, nil
  362. }
  363. // ToHashGlobalData returns the data to be hashed in the method HashGlobalData
  364. func (z ZKInputs) ToHashGlobalData() ([]byte, error) {
  365. var b []byte
  366. bytesMaxLevels := int(z.Metadata.MaxLevels / 8) //nolint:gomnd
  367. // [MAX_NLEVELS bits] oldLastIdx
  368. oldLastIdx := make([]byte, bytesMaxLevels)
  369. copy(oldLastIdx, z.OldLastIdx.Bytes())
  370. b = append(b, SwapEndianness(oldLastIdx)...)
  371. // [MAX_NLEVELS bits] newLastIdx
  372. newLastIdx := make([]byte, bytesMaxLevels)
  373. newLastIdxBytes, err := z.Metadata.NewLastIdxRaw.Bytes()
  374. if err != nil {
  375. return nil, err
  376. }
  377. copy(newLastIdx, newLastIdxBytes[len(newLastIdxBytes)-bytesMaxLevels:])
  378. b = append(b, newLastIdx...)
  379. // [256 bits] oldStRoot
  380. oldStateRoot := make([]byte, 32)
  381. copy(oldStateRoot, z.OldStateRoot.Bytes())
  382. b = append(b, oldStateRoot...)
  383. // [256 bits] newStateRoot
  384. newStateRoot := make([]byte, 32)
  385. copy(newStateRoot, z.Metadata.NewStateRootRaw.Bytes())
  386. b = append(b, newStateRoot...)
  387. // [256 bits] newExitRoot
  388. newExitRoot := make([]byte, 32)
  389. copy(newExitRoot, z.Metadata.NewExitRootRaw.Bytes())
  390. b = append(b, newExitRoot...)
  391. // [MAX_L1_TX * (2 * MAX_NLEVELS + 480) bits] L1TxsData
  392. l1TxDataLen := (2*z.Metadata.MaxLevels + 480)
  393. l1TxsDataLen := (z.Metadata.MaxL1Tx * l1TxDataLen)
  394. l1TxsData := make([]byte, l1TxsDataLen/8) //nolint:gomnd
  395. for i := 0; i < len(z.Metadata.L1TxsData); i++ {
  396. dataLen := int(l1TxDataLen) / 8 //nolint:gomnd
  397. pos0 := i * dataLen
  398. pos1 := i*dataLen + dataLen
  399. copy(l1TxsData[pos0:pos1], z.Metadata.L1TxsData[i])
  400. }
  401. b = append(b, l1TxsData...)
  402. // [MAX_TX*(2*NLevels + 24) bits] L2TxsData
  403. var l2TxsData []byte
  404. l2TxDataLen := 2*z.Metadata.NLevels + 24 //nolint:gomnd
  405. l2TxsDataLen := (z.Metadata.MaxTx * l2TxDataLen)
  406. expectedL2TxsDataLen := l2TxsDataLen / 8 //nolint:gomnd
  407. for i := 0; i < len(z.Metadata.L2TxsData); i++ {
  408. l2TxsData = append(l2TxsData, z.Metadata.L2TxsData[i]...)
  409. }
  410. if len(l2TxsData) > int(expectedL2TxsDataLen) {
  411. return nil, fmt.Errorf("len(l2TxsData): %d, expected: %d", len(l2TxsData), expectedL2TxsDataLen)
  412. }
  413. l2TxsPadding := make([]byte, (int(z.Metadata.MaxTx)-len(z.Metadata.L2TxsData))*int(l2TxDataLen)/8) //nolint:gomnd
  414. b = append(b, l2TxsPadding...)
  415. b = append(b, l2TxsData...)
  416. // [NLevels * MAX_TOKENS_FEE bits] feeTxsData
  417. for i := 0; i < len(z.FeeIdxs); i++ {
  418. var r []byte
  419. padding := make([]byte, bytesMaxLevels/4) //nolint:gomnd
  420. r = append(r, padding...)
  421. feeIdx := make([]byte, bytesMaxLevels/2) //nolint:gomnd
  422. feeIdxBytes := z.FeeIdxs[i].Bytes()
  423. copy(feeIdx[len(feeIdx)-len(feeIdxBytes):], feeIdxBytes[:])
  424. r = append(r, feeIdx...)
  425. b = append(b, r...)
  426. }
  427. // [16 bits] chainID
  428. var chainID [2]byte
  429. binary.BigEndian.PutUint16(chainID[:], z.Metadata.ChainID)
  430. b = append(b, chainID[:]...)
  431. return b, nil
  432. }