Skip to content

Commit 374d301

Browse files
committed
Added KZG library support for micro-ecc-signer.
1 parent 047fc04 commit 374d301

File tree

1 file changed

+29
-7
lines changed

1 file changed

+29
-7
lines changed

src.ts/transaction/transaction.ts

Lines changed: 29 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -183,18 +183,34 @@ export interface KzgLibrary {
183183
* while providing a stable external API.
184184
*/
185185
export type KzgLibraryLike = KzgLibrary | {
186+
// kzg-wasm >= 0.5.0
186187
blobToKZGCommitment: (blob: string) => string;
187188
computeBlobKZGProof: (blob: string, commitment: string) => string;
189+
} | {
190+
// micro-ecc-signer
191+
blobToKzgCommitment: (blob: string) => string | Uint8Array;
192+
computeBlobProof: (blob: string, commitment: string) => string | Uint8Array;
188193
};
189194

190195
function getKzgLibrary(kzg: KzgLibraryLike): KzgLibrary {
196+
191197
const blobToKzgCommitment = (blob: Uint8Array) => {
192-
// API <0.5.0; blobToKzgCommitment(Uint8Array) => Uint8Array
193-
if ("blobToKzgCommitment" in kzg && typeof(kzg.blobToKzgCommitment) === "function") {
194-
return kzg.blobToKzgCommitment(blob);
198+
199+
if ("computeBlobProof" in kzg) {
200+
// micro-ecc-signer; check for computeBlobProof since this API
201+
// expects a string while the kzg-wasm below expects a Unit8Array
202+
203+
if ("blobToKzgCommitment" in kzg && typeof(kzg.blobToKzgCommitment) === "function") {
204+
return getBytes(kzg.blobToKzgCommitment(hexlify(blob)))
205+
}
206+
207+
} else if ("blobToKzgCommitment" in kzg && typeof(kzg.blobToKzgCommitment) === "function") {
208+
// kzg-wasm <0.5.0; blobToKzgCommitment(Uint8Array) => Uint8Array
209+
210+
return getBytes(kzg.blobToKzgCommitment(blob));
195211
}
196212

197-
// API >= 0.5.0; blobToKZGCommitment(string) => string
213+
// kzg-wasm >= 0.5.0; blobToKZGCommitment(string) => string
198214
if ("blobToKZGCommitment" in kzg && typeof(kzg.blobToKZGCommitment) === "function") {
199215
return getBytes(kzg.blobToKZGCommitment(hexlify(blob)));
200216
}
@@ -203,12 +219,18 @@ function getKzgLibrary(kzg: KzgLibraryLike): KzgLibrary {
203219
};
204220

205221
const computeBlobKzgProof = (blob: Uint8Array, commitment: Uint8Array) => {
206-
// API <0.5.0; computeBlobKzgProof(Uint8Array, Uint8Array) => Uint8Array
222+
223+
// micro-ecc-signer
224+
if ("computeBlobProof" in kzg && typeof(kzg.computeBlobProof) === "function") {
225+
return getBytes(kzg.computeBlobProof(hexlify(blob), hexlify(commitment)))
226+
}
227+
228+
// kzg-wasm <0.5.0; computeBlobKzgProof(Uint8Array, Uint8Array) => Uint8Array
207229
if ("computeBlobKzgProof" in kzg && typeof(kzg.computeBlobKzgProof) === "function") {
208230
return kzg.computeBlobKzgProof(blob, commitment);
209231
}
210232

211-
// API >= 0.5.0; computeBlobKZGProof(string, string) => string
233+
// kzg-wasm >= 0.5.0; computeBlobKZGProof(string, string) => string
212234
if ("computeBlobKZGProof" in kzg && typeof(kzg.computeBlobKZGProof) === "function") {
213235
return getBytes(kzg.computeBlobKZGProof(hexlify(blob), hexlify(commitment)));
214236
}
@@ -749,7 +771,7 @@ export class Transaction implements TransactionLike<string> {
749771
case 3: case "cancun": case "eip-4844":
750772
this.#type = 3;
751773
break;
752-
case 4: case "eip-7702":
774+
case 4: case "pectra": case "eip-7702":
753775
this.#type = 4;
754776
break;
755777
default:

0 commit comments

Comments
 (0)