1
1
package v2
2
2
3
3
import (
4
+ "crypto/sha256"
5
+ "math/big"
4
6
"testing"
5
7
8
+ corev1 "github.com/Layr-Labs/eigenda/core"
9
+ core "github.com/Layr-Labs/eigenda/core/v2"
10
+ "github.com/Layr-Labs/eigenda/encoding"
11
+ "github.com/consensys/gnark-crypto/ecc/bn254/fp"
12
+ "github.com/ethereum/go-ethereum/crypto"
6
13
"github.com/stretchr/testify/assert"
14
+ "github.com/stretchr/testify/require"
7
15
)
8
16
9
17
func TestGetAccountID (t * testing.T ) {
@@ -19,3 +27,119 @@ func TestGetAccountID(t *testing.T) {
19
27
assert .NoError (t , err )
20
28
assert .Equal (t , expectedAccountID , accountID )
21
29
}
30
+
31
+ func TestSignBlobRequest (t * testing.T ) {
32
+ privateKey := "0123456789abcdef0123456789abcdef0123456789abcdef0123456789abcded"
33
+ signer := NewLocalBlobRequestSigner (privateKey )
34
+ accountID , err := signer .GetAccountID ()
35
+ require .NoError (t , err )
36
+ require .Equal (t , "0x1aa8226f6d354380dDE75eE6B634875c4203e522" , accountID )
37
+
38
+ var commitX , commitY fp.Element
39
+ _ , err = commitX .SetString ("21661178944771197726808973281966770251114553549453983978976194544185382599016" )
40
+ assert .NoError (t , err )
41
+ _ , err = commitY .SetString ("9207254729396071334325696286939045899948985698134704137261649190717970615186" )
42
+ assert .NoError (t , err )
43
+
44
+ commitment := & encoding.G1Commitment {
45
+ X : commitX ,
46
+ Y : commitY ,
47
+ }
48
+ var lengthXA0 , lengthXA1 , lengthYA0 , lengthYA1 fp.Element
49
+ _ , err = lengthXA0 .SetString ("10857046999023057135944570762232829481370756359578518086990519993285655852781" )
50
+ assert .NoError (t , err )
51
+ _ , err = lengthXA1 .SetString ("11559732032986387107991004021392285783925812861821192530917403151452391805634" )
52
+ assert .NoError (t , err )
53
+ _ , err = lengthYA0 .SetString ("8495653923123431417604973247489272438418190587263600148770280649306958101930" )
54
+ assert .NoError (t , err )
55
+ _ , err = lengthYA1 .SetString ("4082367875863433681332203403145435568316851327593401208105741076214120093531" )
56
+ assert .NoError (t , err )
57
+
58
+ var lengthProof , lengthCommitment encoding.G2Commitment
59
+ lengthProof .X .A0 = lengthXA0
60
+ lengthProof .X .A1 = lengthXA1
61
+ lengthProof .Y .A0 = lengthYA0
62
+ lengthProof .Y .A1 = lengthYA1
63
+
64
+ lengthCommitment = lengthProof
65
+
66
+ header := & core.BlobHeader {
67
+ BlobCommitments : encoding.BlobCommitments {
68
+ Commitment : commitment ,
69
+ LengthCommitment : & lengthCommitment ,
70
+ LengthProof : & lengthProof ,
71
+ Length : 48 ,
72
+ },
73
+ BlobVersion : 1 ,
74
+ QuorumNumbers : []corev1.QuorumID {1 , 2 },
75
+ PaymentMetadata : corev1.PaymentMetadata {
76
+ AccountID : accountID ,
77
+ CumulativePayment : big .NewInt (100 ),
78
+ ReservationPeriod : 100 ,
79
+ },
80
+ }
81
+
82
+ // Sign the blob request
83
+ signature , err := signer .SignBlobRequest (header )
84
+ require .NoError (t , err )
85
+ require .NotNil (t , signature )
86
+
87
+ // Verify the signature
88
+ blobKey , err := header .BlobKey ()
89
+ require .NoError (t , err )
90
+
91
+ // Recover the public key from the signature
92
+ pubKey , err := crypto .SigToPub (blobKey [:], signature )
93
+ require .NoError (t , err )
94
+
95
+ // Verify that the recovered address matches the signer's address
96
+ recoveredAddr := crypto .PubkeyToAddress (* pubKey ).Hex ()
97
+ assert .Equal (t , accountID , recoveredAddr )
98
+ }
99
+
100
+ func TestSignPaymentStateRequest (t * testing.T ) {
101
+ privateKey := "0123456789abcdef0123456789abcdef0123456789abcdef0123456789abcded"
102
+ signer := NewLocalBlobRequestSigner (privateKey )
103
+ expectedAddr := "0x1aa8226f6d354380dDE75eE6B634875c4203e522"
104
+ accountID , err := signer .GetAccountID ()
105
+ require .NoError (t , err )
106
+ hash := sha256 .Sum256 ([]byte (accountID ))
107
+
108
+ // Sign payment state request
109
+ signature , err := signer .SignPaymentStateRequest ()
110
+ require .NoError (t , err )
111
+ require .NotNil (t , signature )
112
+
113
+ // Recover the public key from the signature
114
+ pubKey , err := crypto .SigToPub (hash [:], signature )
115
+ require .NoError (t , err )
116
+
117
+ // Verify that the recovered address matches the signer's address
118
+ recoveredAddr := crypto .PubkeyToAddress (* pubKey ).Hex ()
119
+ assert .Equal (t , expectedAddr , recoveredAddr )
120
+ }
121
+
122
+ func TestNoopSigner (t * testing.T ) {
123
+ signer := NewLocalNoopSigner ()
124
+
125
+ t .Run ("SignBlobRequest" , func (t * testing.T ) {
126
+ sig , err := signer .SignBlobRequest (nil )
127
+ assert .Error (t , err )
128
+ assert .Nil (t , sig )
129
+ assert .Equal (t , "noop signer cannot sign blob request" , err .Error ())
130
+ })
131
+
132
+ t .Run ("SignPaymentStateRequest" , func (t * testing.T ) {
133
+ sig , err := signer .SignPaymentStateRequest ()
134
+ assert .Error (t , err )
135
+ assert .Nil (t , sig )
136
+ assert .Equal (t , "noop signer cannot sign payment state request" , err .Error ())
137
+ })
138
+
139
+ t .Run ("GetAccountID" , func (t * testing.T ) {
140
+ accountID , err := signer .GetAccountID ()
141
+ assert .Error (t , err )
142
+ assert .Empty (t , accountID )
143
+ assert .Equal (t , "noop signer cannot get accountID" , err .Error ())
144
+ })
145
+ }
0 commit comments