Skip to content

Commit e5036e7

Browse files
committed
Accept modern KZG library API while exposing legacy API (#4841).
1 parent bca8d1b commit e5036e7

File tree

4 files changed

+57
-7
lines changed

4 files changed

+57
-7
lines changed

src.ts/ethers.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -180,7 +180,7 @@ export type {
180180

181181
export type {
182182
AccessList, AccessListish, AccessListEntry,
183-
Blob, BlobLike, KzgLibrary,
183+
Blob, BlobLike, KzgLibrary, KzgLibraryLike,
184184
TransactionLike
185185
} from "./transaction/index.js";
186186

src.ts/providers/provider.ts

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -10,7 +10,7 @@ import type { AddressLike, NameResolver } from "../address/index.js";
1010
import type { BigNumberish, EventEmitterable } from "../utils/index.js";
1111
import type { Signature } from "../crypto/index.js";
1212
import type {
13-
AccessList, AccessListish, BlobLike, KzgLibrary, TransactionLike
13+
AccessList, AccessListish, BlobLike, KzgLibraryLike, TransactionLike
1414
} from "../transaction/index.js";
1515

1616
import type { ContractRunner } from "./contracts.js";
@@ -239,7 +239,7 @@ export interface TransactionRequest {
239239
* This is generally ``null``, unless you are creating BLOb
240240
* transactions.
241241
*/
242-
kzg?: null | KzgLibrary;
242+
kzg?: null | KzgLibraryLike;
243243

244244
// Todo?
245245
//gasMultiplier?: number;

src.ts/transaction/index.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -29,5 +29,5 @@ export { computeAddress, recoverAddress } from "./address.js";
2929
export { Transaction } from "./transaction.js";
3030

3131
export type {
32-
Blob, BlobLike, KzgLibrary, TransactionLike
32+
Blob, BlobLike, KzgLibrary, KzgLibraryLike, TransactionLike
3333
} from "./transaction.js";

src.ts/transaction/transaction.ts

Lines changed: 53 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -128,7 +128,7 @@ export interface TransactionLike<A = string> {
128128
* This is generally ``null``, unless you are creating BLOb
129129
* transactions.
130130
*/
131-
kzg?: null | KzgLibrary;
131+
kzg?: null | KzgLibraryLike;
132132
}
133133

134134
/**
@@ -165,6 +165,52 @@ export interface KzgLibrary {
165165
computeBlobKzgProof: (blob: Uint8Array, commitment: Uint8Array) => Uint8Array;
166166
}
167167

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+
168214
function getVersionedHash(version: number, hash: BytesLike): string {
169215
let versioned = version.toString(16);
170216
while (versioned.length < 2) { versioned = "0" + versioned; }
@@ -862,8 +908,12 @@ export class Transaction implements TransactionLike<string> {
862908
}
863909

864910
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+
}
867917
}
868918

869919
/**

0 commit comments

Comments
 (0)