-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathexternal_sign_test.go
126 lines (104 loc) · 3.61 KB
/
external_sign_test.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
package externalsign
import (
"math/big"
"testing"
"github.com/morph-l2/go-ethereum/common"
"github.com/morph-l2/go-ethereum/common/hexutil"
"github.com/morph-l2/go-ethereum/core/types"
"github.com/morph-l2/go-ethereum/crypto/kzg4844"
"github.com/holiman/uint256"
"github.com/stretchr/testify/require"
)
func TestRequestSign(t *testing.T) {
appid := "morph-tx-submitter-399A1722-3F2C-4E39-ABD2-1B65D02C66BA"
rsaPrivStr := ""
url := "http://localhost:8080/v1/sign/tx_sign"
addr := "0x33d5b507868b7e8ac930cd3bde9eadd60c638479"
to := common.HexToAddress("0xc5eb133df513af12580bf898428d24825d90821a")
chain := "QANET-L1"
chainid := big.NewInt(900)
signer := types.LatestSignerForChainID(chainid)
rsa, err := ParseRsaPrivateKey(rsaPrivStr)
require.NoError(t, err)
es := NewExternalSign(appid, rsa, addr, chain, signer)
// testdata
gas := uint64(50000)
txdata := &types.DynamicFeeTx{
To: &to,
Gas: gas,
GasFeeCap: big.NewInt(1),
GasTipCap: big.NewInt(2),
Value: big.NewInt(3),
ChainID: chainid,
}
txdatas := make([]types.TxData, 0)
txdatas = append(txdatas, txdata, createEmptyBlobTxInner(true))
for _, txdata := range txdatas {
tx := types.NewTx(txdata)
signedTx, err := es.RequestSign(url, tx)
require.NoError(t, err)
from, err := signer.Sender(signedTx)
require.NoError(t, err)
require.Equal(t, hexutil.Encode(from.Bytes()), addr)
switch tp := txdata.(type) {
case *types.DynamicFeeTx:
require.Equal(t, tp.Gas, signedTx.Gas())
require.Equal(t, tp.GasFeeCap.Uint64(), signedTx.GasFeeCap().Uint64())
require.Equal(t, tp.GasTipCap.Uint64(), signedTx.GasTipCap().Uint64())
require.Equal(t, tp.Value.Uint64(), signedTx.Value().Uint64())
require.Equal(t, tp.Data, signedTx.Data())
case *types.BlobTx:
require.Equal(t, tp.Gas, signedTx.Gas())
require.Equal(t, tp.GasFeeCap.Uint64(), signedTx.GasFeeCap().Uint64())
require.Equal(t, tp.GasTipCap.Uint64(), signedTx.GasTipCap().Uint64())
require.Equal(t, tp.Data, signedTx.Data())
require.Equal(t, tp.BlobFeeCap.Uint64(), signedTx.BlobGasFeeCap().Uint64())
require.Equal(t, tp.BlobHashes, signedTx.BlobHashes())
require.Equal(t, tp.Sidecar, signedTx.BlobTxSidecar())
}
}
}
func TestNewWallet(t *testing.T) {
//test data
appid := "morph-tx-submitter-399A1722-3F2C-4E39-ABD2-1B65D02C66BA"
rsaPrivStr := ""
url := "http://localhost:8080/v1/sign/gen_address"
chain := "QANET-L1"
chainid := big.NewInt(900)
signer := types.LatestSignerForChainID(chainid)
rsaPriv, err := ParseRsaPrivateKey(rsaPrivStr)
require.NoError(t, err)
es := NewExternalSign(appid, rsaPriv, "", chain, signer)
addr, err := es.RequestWalletAddr(url)
require.NoError(t, err)
require.NotEmpty(t, addr)
t.Log("addr", addr)
}
func createEmptyBlobTxInner(withSidecar bool) *types.BlobTx {
var (
emptyBlob = new(kzg4844.Blob)
emptyBlobCommit, _ = kzg4844.BlobToCommitment(emptyBlob)
emptyBlobProof, _ = kzg4844.ComputeBlobProof(emptyBlob, emptyBlobCommit)
chainid = uint256.NewInt(900)
)
sidecar := &types.BlobTxSidecar{
Blobs: []kzg4844.Blob{*emptyBlob},
Commitments: []kzg4844.Commitment{emptyBlobCommit},
Proofs: []kzg4844.Proof{emptyBlobProof},
}
blobtx := &types.BlobTx{
ChainID: chainid,
Nonce: uint64(23),
GasTipCap: uint256.MustFromBig(big.NewInt(1)),
GasFeeCap: uint256.MustFromBig(big.NewInt(1)),
Gas: 23,
To: common.HexToAddress("0xc5eb133df513af12580bf898428d24825d90821f"),
Data: make([]byte, 50),
BlobFeeCap: uint256.NewInt(15),
BlobHashes: sidecar.BlobHashes(),
}
if withSidecar {
blobtx.Sidecar = sidecar
}
return blobtx
}