@@ -128,7 +128,7 @@ export interface TransactionLike<A = string> {
128
128
* This is generally ``null``, unless you are creating BLOb
129
129
* transactions.
130
130
*/
131
- kzg ?: null | KzgLibrary ;
131
+ kzg ?: null | KzgLibraryLike ;
132
132
}
133
133
134
134
/**
@@ -165,6 +165,52 @@ export interface KzgLibrary {
165
165
computeBlobKzgProof : ( blob : Uint8Array , commitment : Uint8Array ) => Uint8Array ;
166
166
}
167
167
168
+ /**
169
+ * A KZG Library with any of the various API configurations.
170
+ * As the library is still experimental and the API is not
171
+ * stable, depending on the version used the method names and
172
+ * signatures are still in flux.
173
+ *
174
+ * This allows any of the versions to be passed into Transaction
175
+ * while providing a stable external API.
176
+ */
177
+ export type KzgLibraryLike = KzgLibrary | {
178
+ blobToKZGCommitment : ( blob : string ) => string ;
179
+ computeBlobKZGProof : ( blob : string , commitment : string ) => string ;
180
+ } ;
181
+
182
+ function getKzgLibrary ( kzg : KzgLibraryLike ) : KzgLibrary {
183
+ const blobToKzgCommitment = ( blob : Uint8Array ) => {
184
+ // API <0.5.0; blobToKzgCommitment(Uint8Array) => Uint8Array
185
+ if ( "blobToKzgCommitment" in kzg && typeof ( kzg . blobToKzgCommitment ) === "function" ) {
186
+ return kzg . blobToKzgCommitment ( blob ) ;
187
+ }
188
+
189
+ // API >= 0.5.0; blobToKZGCommitment(string) => string
190
+ if ( "blobToKZGCommitment" in kzg && typeof ( kzg . blobToKZGCommitment ) === "function" ) {
191
+ return getBytes ( kzg . blobToKZGCommitment ( hexlify ( blob ) ) ) ;
192
+ }
193
+
194
+ assertArgument ( false , "unsupported KZG library" , "kzg" , kzg ) ;
195
+ } ;
196
+
197
+ const computeBlobKzgProof = ( blob : Uint8Array , commitment : Uint8Array ) => {
198
+ // API <0.5.0; computeBlobKzgProof(Uint8Array, Uint8Array) => Uint8Array
199
+ if ( "computeBlobKzgProof" in kzg && typeof ( kzg . computeBlobKzgProof ) === "function" ) {
200
+ return kzg . computeBlobKzgProof ( blob , commitment ) ;
201
+ }
202
+
203
+ // API >= 0.5.0; computeBlobKZGProof(string, string) => string
204
+ if ( "computeBlobKZGProof" in kzg && typeof ( kzg . computeBlobKZGProof ) === "function" ) {
205
+ return getBytes ( kzg . computeBlobKZGProof ( hexlify ( blob ) , hexlify ( commitment ) ) ) ;
206
+ }
207
+
208
+ assertArgument ( false , "unsupported KZG library" , "kzg" , kzg ) ;
209
+ } ;
210
+
211
+ return { blobToKzgCommitment, computeBlobKzgProof } ;
212
+ }
213
+
168
214
function getVersionedHash ( version : number , hash : BytesLike ) : string {
169
215
let versioned = version . toString ( 16 ) ;
170
216
while ( versioned . length < 2 ) { versioned = "0" + versioned ; }
@@ -862,8 +908,12 @@ export class Transaction implements TransactionLike<string> {
862
908
}
863
909
864
910
get kzg ( ) : null | KzgLibrary { return this . #kzg; }
865
- set kzg ( kzg : null | KzgLibrary ) {
866
- this . #kzg = kzg ;
911
+ set kzg ( kzg : null | KzgLibraryLike ) {
912
+ if ( kzg == null ) {
913
+ this . #kzg = null ;
914
+ } else {
915
+ this . #kzg = getKzgLibrary ( kzg ) ;
916
+ }
867
917
}
868
918
869
919
/**
0 commit comments