This repository has been archived by the owner on May 11, 2024. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 334
/
tx_blob.go
174 lines (160 loc) · 4.23 KB
/
tx_blob.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
package rpc
import (
"errors"
"fmt"
"github.com/ethereum/go-ethereum/accounts/abi/bind"
"github.com/ethereum/go-ethereum/common"
"github.com/ethereum/go-ethereum/common/hexutil"
"github.com/ethereum/go-ethereum/core/types"
"github.com/ethereum/go-ethereum/crypto/kzg4844"
"github.com/ethereum/go-ethereum/params"
"github.com/holiman/uint256"
)
// TransactBlobTx create, sign and send blob tx.
func (c *EthClient) TransactBlobTx(
opts *bind.TransactOpts,
contract *common.Address,
input []byte,
sidecar *types.BlobTxSidecar,
) (*types.Transaction, error) {
// Sign the transaction and schedule it for execution
if opts.Signer == nil {
return nil, errors.New("no signer to authorize the transaction with")
}
// Create blob tx.
rawTx, err := c.createBlobTx(opts, contract, input, sidecar)
if err != nil {
return nil, err
}
signedTx, err := opts.Signer(opts.From, rawTx)
if err != nil {
return nil, err
}
if opts.NoSend {
return signedTx, nil
}
if err := c.SendTransaction(opts.Context, signedTx); err != nil {
return nil, err
}
return signedTx, nil
}
func (c *EthClient) createBlobTx(
opts *bind.TransactOpts,
contract *common.Address,
input []byte,
sidecar *types.BlobTxSidecar,
) (*types.Transaction, error) {
// Get nonce.
var nonce *hexutil.Uint64
if opts.Nonce != nil {
curNonce := hexutil.Uint64(opts.Nonce.Uint64())
nonce = &curNonce
}
if input == nil {
input = []byte{}
}
if contract == nil {
contract = &common.Address{}
}
var gas *hexutil.Uint64
if opts.GasLimit != 0 {
var gasVal = hexutil.Uint64(opts.GasLimit)
gas = &gasVal
}
rawTx, err := c.FillTransaction(opts.Context, &TransactionArgs{
From: &opts.From,
To: contract,
Gas: gas,
GasPrice: (*hexutil.Big)(opts.GasPrice),
MaxFeePerGas: (*hexutil.Big)(opts.GasFeeCap),
MaxPriorityFeePerGas: (*hexutil.Big)(opts.GasTipCap),
Value: (*hexutil.Big)(opts.Value),
Nonce: nonce,
Data: (*hexutil.Bytes)(&input),
AccessList: nil,
ChainID: nil,
BlobFeeCap: nil,
BlobHashes: sidecar.BlobHashes(),
})
if err != nil {
return nil, err
}
blobTx := &types.BlobTx{
ChainID: uint256.MustFromBig(rawTx.ChainId()),
Nonce: rawTx.Nonce(),
GasTipCap: uint256.MustFromBig(rawTx.GasTipCap()),
GasFeeCap: uint256.MustFromBig(rawTx.GasFeeCap()),
Gas: rawTx.Gas(),
To: *rawTx.To(),
Value: uint256.MustFromBig(rawTx.Value()),
Data: rawTx.Data(),
AccessList: rawTx.AccessList(),
BlobFeeCap: uint256.MustFromBig(rawTx.BlobGasFeeCap()),
BlobHashes: sidecar.BlobHashes(),
Sidecar: sidecar,
}
return types.NewTx(blobTx), nil
}
// MakeSidecarWithSingleBlob make a sidecar that just include one blob.
func MakeSidecarWithSingleBlob(data []byte) (*types.BlobTxSidecar, error) {
if len(data) > BlobBytes {
return nil, fmt.Errorf("data is bigger than 128k")
}
blob := EncodeBlobs(data)[0]
commitment, err := kzg4844.BlobToCommitment(blob)
if err != nil {
return nil, err
}
proof, err := kzg4844.ComputeBlobProof(blob, commitment)
if err != nil {
return nil, err
}
return &types.BlobTxSidecar{
Blobs: []kzg4844.Blob{blob},
Commitments: []kzg4844.Commitment{commitment},
Proofs: []kzg4844.Proof{proof},
}, nil
}
// EncodeBlobs encode bytes into Blob type.
func EncodeBlobs(data []byte) []kzg4844.Blob {
blobs := []kzg4844.Blob{{}}
blobIndex := 0
fieldIndex := -1
numOfElems := BlobBytes / 32
for i := 0; i < len(data); i += 31 {
fieldIndex++
if fieldIndex == numOfElems {
if blobIndex >= 1 {
break
}
blobs = append(blobs, kzg4844.Blob{})
blobIndex++
fieldIndex = 0
}
max := i + 31
if max > len(data) {
max = len(data)
}
copy(blobs[blobIndex][fieldIndex*32+1:], data[i:max])
}
return blobs
}
// DecodeBlob decode blob data.
func DecodeBlob(blob []byte) []byte {
if len(blob) != params.BlobTxFieldElementsPerBlob*32 {
panic("invalid blob encoding")
}
var data []byte
for i, j := 0, 0; i < params.BlobTxFieldElementsPerBlob; i++ {
data = append(data, blob[j:j+31]...)
j += 32
}
i := len(data) - 1
for ; i >= 0; i-- {
if data[i] != 0x00 {
break
}
}
data = data[:i+1]
return data
}