Skip to content

Commit 6d1ee0c

Browse files
committed
fix: base58 dynamic import
1 parent 7b6442e commit 6d1ee0c

File tree

1 file changed

+13
-1
lines changed
  • infrastructure/signature-validator/src

1 file changed

+13
-1
lines changed

infrastructure/signature-validator/src/index.ts

Lines changed: 13 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,16 @@
11
import axios from "axios";
22
import * as jose from "jose";
3-
import { base58btc } from "multiformats/bases/base58";
3+
4+
// Dynamic import for base58btc to handle ESM module in CommonJS context
5+
let base58btcCache: { decode: (input: string) => Uint8Array } | null = null;
6+
7+
async function getBase58btc() {
8+
if (!base58btcCache) {
9+
const base58Module = await import("multiformats/bases/base58");
10+
base58btcCache = base58Module.base58btc;
11+
}
12+
return base58btcCache;
13+
}
414

515
/**
616
* Options for signature verification
@@ -59,6 +69,7 @@ async function decodeMultibasePublicKey(multibaseKey: string): Promise<Uint8Arra
5969

6070
// Try base58btc (standard multibase 'z' prefix)
6171
try {
72+
const base58btc = await getBase58btc();
6273
return base58btc.decode(encoded);
6374
} catch (error) {
6475
throw new Error(
@@ -77,6 +88,7 @@ async function decodeSignature(signature: string): Promise<Uint8Array> {
7788
// If it starts with 'z', it's multibase base58btc
7889
if (signature.startsWith("z")) {
7990
try {
91+
const base58btc = await getBase58btc();
8092
return base58btc.decode(signature.slice(1));
8193
} catch (error) {
8294
throw new Error(`Failed to decode multibase signature: ${error instanceof Error ? error.message : String(error)}`);

0 commit comments

Comments
 (0)