@vleap/warps-adapter-fastset 0.1.0-beta.47 → 0.1.0-beta.48

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
package/dist/index.mjs CHANGED
@@ -16,9 +16,6 @@ function uint8ArrayToHex(uint8Array) {
16
16
  function hexToUint8Array(hex) {
17
17
  return new Uint8Array(Buffer.from(hex, "hex"));
18
18
  }
19
- function stringToUint8Array(str) {
20
- return new Uint8Array(Buffer.from(str, "utf8"));
21
- }
22
19
 
23
20
  // src/helpers/general.ts
24
21
  import { getProviderConfig } from "@vleap/warps";
@@ -1932,16 +1929,19 @@ var WarpFastsetOutput = class {
1932
1929
  };
1933
1930
 
1934
1931
  // src/WarpFastsetWallet.ts
1935
- import * as bip39 from "@scure/bip39";
1936
1932
  import {
1937
- getWarpWalletAddressFromConfig as getWarpWalletAddressFromConfig3,
1938
- getWarpWalletPrivateKeyFromConfig
1933
+ initializeWalletCache
1939
1934
  } from "@vleap/warps";
1940
1935
 
1936
+ // src/providers/MnemonicWalletProvider.ts
1937
+ import * as bip39 from "@scure/bip39";
1938
+ import { wordlist } from "@scure/bip39/wordlists/english.js";
1939
+ import { getWarpWalletMnemonicFromConfig } from "@vleap/warps";
1940
+
1941
1941
  // src/sdk/ed25519-setup.ts
1942
1942
  import * as ed25519 from "@noble/ed25519";
1943
1943
 
1944
- // node_modules/@noble/hashes/utils.js
1944
+ // ../../node_modules/@noble/hashes/utils.js
1945
1945
  function isBytes2(a) {
1946
1946
  return a instanceof Uint8Array || ArrayBuffer.isView(a) && a.constructor.name === "Uint8Array";
1947
1947
  }
@@ -1991,7 +1991,7 @@ var oidNist = (suffix) => ({
1991
1991
  oid: Uint8Array.from([6, 9, 96, 134, 72, 1, 101, 3, 4, 2, suffix])
1992
1992
  });
1993
1993
 
1994
- // node_modules/@noble/hashes/_md.js
1994
+ // ../../node_modules/@noble/hashes/_md.js
1995
1995
  var HashMD = class {
1996
1996
  constructor(blockLen, outputLen, padOffset, isLE) {
1997
1997
  __publicField(this, "blockLen");
@@ -2106,7 +2106,7 @@ var SHA512_IV = /* @__PURE__ */ Uint32Array.from([
2106
2106
  327033209
2107
2107
  ]);
2108
2108
 
2109
- // node_modules/@noble/hashes/_u64.js
2109
+ // ../../node_modules/@noble/hashes/_u64.js
2110
2110
  var U32_MASK64 = /* @__PURE__ */ BigInt(2 ** 32 - 1);
2111
2111
  var _32n = /* @__PURE__ */ BigInt(32);
2112
2112
  function fromBig(n, le = false) {
@@ -2141,7 +2141,7 @@ var add4H = (low, Ah, Bh, Ch, Dh) => Ah + Bh + Ch + Dh + (low / 2 ** 32 | 0) | 0
2141
2141
  var add5L = (Al, Bl, Cl, Dl, El) => (Al >>> 0) + (Bl >>> 0) + (Cl >>> 0) + (Dl >>> 0) + (El >>> 0);
2142
2142
  var add5H = (low, Ah, Bh, Ch, Dh, Eh) => Ah + Bh + Ch + Dh + Eh + (low / 2 ** 32 | 0) | 0;
2143
2143
 
2144
- // node_modules/@noble/hashes/sha2.js
2144
+ // ../../node_modules/@noble/hashes/sha2.js
2145
2145
  var K512 = /* @__PURE__ */ (() => split([
2146
2146
  "0x428a2f98d728ae22",
2147
2147
  "0x7137449123ef65cd",
@@ -2353,77 +2353,260 @@ var sha512 = /* @__PURE__ */ createHasher(
2353
2353
  ed25519.hashes.sha512 = sha512;
2354
2354
  var ed = ed25519;
2355
2355
 
2356
- // src/WarpFastsetWallet.ts
2357
- var WarpFastsetWallet = class {
2356
+ // src/providers/MnemonicWalletProvider.ts
2357
+ var _MnemonicWalletProvider = class _MnemonicWalletProvider {
2358
2358
  constructor(config, chain2) {
2359
2359
  this.config = config;
2360
2360
  this.chain = chain2;
2361
- this.client = getConfiguredFastsetClient(this.config, this.chain);
2361
+ this.privateKey = null;
2362
+ }
2363
+ async getAddress() {
2364
+ try {
2365
+ const privateKey = this.getPrivateKey();
2366
+ const publicKey = ed.getPublicKey(privateKey);
2367
+ return FastsetClient.encodeBech32Address(publicKey);
2368
+ } catch {
2369
+ return null;
2370
+ }
2371
+ }
2372
+ async getPublicKey() {
2373
+ try {
2374
+ const privateKey = this.getPrivateKey();
2375
+ const publicKey = ed.getPublicKey(privateKey);
2376
+ return uint8ArrayToHex(publicKey);
2377
+ } catch {
2378
+ return null;
2379
+ }
2362
2380
  }
2363
2381
  async signTransaction(tx) {
2382
+ const privateKey = this.getPrivateKey();
2364
2383
  const msg = Transaction.serialize(tx);
2365
2384
  const msgBytes = msg.toBytes();
2366
2385
  const prefix = new TextEncoder().encode("Transaction::");
2367
2386
  const dataToSign = new Uint8Array(prefix.length + msgBytes.length);
2368
2387
  dataToSign.set(prefix, 0);
2369
2388
  dataToSign.set(msgBytes, prefix.length);
2370
- const privateKey = getWarpWalletPrivateKeyFromConfig(this.config, this.chain.name);
2371
- if (!privateKey) throw new Error("Wallet not initialized - no private key provided");
2372
- const privateKeyBytes = hexToUint8Array(privateKey);
2373
- const signature = ed.sign(dataToSign, privateKeyBytes);
2389
+ const signature = ed.sign(dataToSign, privateKey);
2374
2390
  return { ...tx, signature };
2375
2391
  }
2376
2392
  async signMessage(message) {
2377
- const messageBytes = stringToUint8Array(message);
2378
- const privateKey = getWarpWalletPrivateKeyFromConfig(this.config, this.chain.name);
2379
- if (!privateKey) throw new Error("Wallet not initialized - no private key provided");
2380
- const privateKeyBytes = hexToUint8Array(privateKey);
2381
- const signature = ed.sign(messageBytes, privateKeyBytes);
2393
+ const privateKey = this.getPrivateKey();
2394
+ const messageBytes = new TextEncoder().encode(message);
2395
+ const signature = ed.sign(messageBytes, privateKey);
2382
2396
  return uint8ArrayToHex(signature);
2383
2397
  }
2384
- async signTransactions(txs) {
2385
- return Promise.all(txs.map(async (tx) => this.signTransaction(tx)));
2386
- }
2387
- async sendTransaction(tx) {
2388
- const { signature, ...transactionWithoutSignature } = tx;
2389
- const _cert = await this.client.submitTransaction(transactionWithoutSignature, signature ?? null);
2390
- return "TODO";
2391
- }
2392
- async sendTransactions(txs) {
2393
- return Promise.all(txs.map(async (tx) => this.sendTransaction(tx)));
2394
- }
2395
2398
  create(mnemonic) {
2396
2399
  const seed = bip39.mnemonicToSeedSync(mnemonic);
2397
2400
  const privateKey = seed.slice(0, 32);
2398
2401
  const publicKey = ed.getPublicKey(privateKey);
2399
2402
  const address = FastsetClient.encodeBech32Address(publicKey);
2400
- return { provider: "privateKey", address, privateKey: uint8ArrayToHex(privateKey), mnemonic };
2403
+ return {
2404
+ provider: _MnemonicWalletProvider.PROVIDER_NAME,
2405
+ address,
2406
+ privateKey: null,
2407
+ mnemonic
2408
+ };
2401
2409
  }
2402
2410
  generate() {
2403
- const privateKey = ed.utils.randomSecretKey();
2411
+ const mnemonic = bip39.generateMnemonic(wordlist);
2412
+ const seed = bip39.mnemonicToSeedSync(mnemonic);
2413
+ const privateKey = seed.slice(0, 32);
2404
2414
  const publicKey = ed.getPublicKey(privateKey);
2405
2415
  const address = FastsetClient.encodeBech32Address(publicKey);
2406
- return { provider: "privateKey", address, privateKey: uint8ArrayToHex(privateKey), mnemonic: null };
2416
+ return {
2417
+ provider: _MnemonicWalletProvider.PROVIDER_NAME,
2418
+ address,
2419
+ privateKey: null,
2420
+ mnemonic
2421
+ };
2407
2422
  }
2408
- getAddress() {
2409
- return getWarpWalletAddressFromConfig3(this.config, this.chain.name);
2423
+ getPrivateKey() {
2424
+ if (this.privateKey) return this.privateKey;
2425
+ const mnemonic = getWarpWalletMnemonicFromConfig(this.config, this.chain.name);
2426
+ if (!mnemonic) throw new Error("No mnemonic provided");
2427
+ const seed = bip39.mnemonicToSeedSync(mnemonic);
2428
+ this.privateKey = seed.slice(0, 32);
2429
+ return this.privateKey;
2410
2430
  }
2411
- getPublicKey() {
2412
- const privateKey = getWarpWalletPrivateKeyFromConfig(this.config, this.chain.name);
2413
- if (privateKey) {
2414
- const privateKeyBytes = hexToUint8Array(privateKey);
2415
- const publicKey = ed.getPublicKey(privateKeyBytes);
2416
- return uint8ArrayToHex(publicKey);
2431
+ };
2432
+ _MnemonicWalletProvider.PROVIDER_NAME = "mnemonic";
2433
+ var MnemonicWalletProvider = _MnemonicWalletProvider;
2434
+
2435
+ // src/providers/PrivateKeyWalletProvider.ts
2436
+ import { getWarpWalletPrivateKeyFromConfig } from "@vleap/warps";
2437
+ var _PrivateKeyWalletProvider = class _PrivateKeyWalletProvider {
2438
+ constructor(config, chain2) {
2439
+ this.config = config;
2440
+ this.chain = chain2;
2441
+ this.privateKey = null;
2442
+ }
2443
+ async getAddress() {
2444
+ try {
2445
+ const privateKey = this.getPrivateKey();
2446
+ const publicKey = ed.getPublicKey(privateKey);
2447
+ return FastsetClient.encodeBech32Address(publicKey);
2448
+ } catch {
2449
+ return null;
2417
2450
  }
2418
- const address = getWarpWalletAddressFromConfig3(this.config, this.chain.name);
2419
- if (!address) return null;
2451
+ }
2452
+ async getPublicKey() {
2420
2453
  try {
2421
- const addressBytes = FastsetClient.decodeBech32Address(address);
2422
- return uint8ArrayToHex(addressBytes);
2454
+ const privateKey = this.getPrivateKey();
2455
+ const publicKey = ed.getPublicKey(privateKey);
2456
+ return uint8ArrayToHex(publicKey);
2423
2457
  } catch {
2424
2458
  return null;
2425
2459
  }
2426
2460
  }
2461
+ async signTransaction(tx) {
2462
+ const privateKey = this.getPrivateKey();
2463
+ const msg = Transaction.serialize(tx);
2464
+ const msgBytes = msg.toBytes();
2465
+ const prefix = new TextEncoder().encode("Transaction::");
2466
+ const dataToSign = new Uint8Array(prefix.length + msgBytes.length);
2467
+ dataToSign.set(prefix, 0);
2468
+ dataToSign.set(msgBytes, prefix.length);
2469
+ const signature = ed.sign(dataToSign, privateKey);
2470
+ return { ...tx, signature };
2471
+ }
2472
+ async signMessage(message) {
2473
+ const privateKey = this.getPrivateKey();
2474
+ const messageBytes = new TextEncoder().encode(message);
2475
+ const signature = ed.sign(messageBytes, privateKey);
2476
+ return uint8ArrayToHex(signature);
2477
+ }
2478
+ create(mnemonic) {
2479
+ throw new Error("PrivateKeyWalletProvider does not support creating wallets from mnemonics. Use MnemonicWalletProvider instead.");
2480
+ }
2481
+ generate() {
2482
+ const privateKey = ed.utils.randomSecretKey();
2483
+ const publicKey = ed.getPublicKey(privateKey);
2484
+ const address = FastsetClient.encodeBech32Address(publicKey);
2485
+ return {
2486
+ provider: _PrivateKeyWalletProvider.PROVIDER_NAME,
2487
+ address,
2488
+ privateKey: uint8ArrayToHex(privateKey),
2489
+ mnemonic: null
2490
+ };
2491
+ }
2492
+ getPrivateKey() {
2493
+ if (this.privateKey) return this.privateKey;
2494
+ const privateKeyHex = getWarpWalletPrivateKeyFromConfig(this.config, this.chain.name);
2495
+ if (!privateKeyHex) throw new Error("No private key provided");
2496
+ this.privateKey = hexToUint8Array(privateKeyHex);
2497
+ return this.privateKey;
2498
+ }
2499
+ };
2500
+ _PrivateKeyWalletProvider.PROVIDER_NAME = "privateKey";
2501
+ var PrivateKeyWalletProvider = _PrivateKeyWalletProvider;
2502
+
2503
+ // src/providers/ReadOnlyWalletProvider.ts
2504
+ import { getWarpWalletAddressFromConfig as getWarpWalletAddressFromConfig3 } from "@vleap/warps";
2505
+ var ReadOnlyWalletProvider = class {
2506
+ constructor(config, chain2) {
2507
+ this.config = config;
2508
+ this.chain = chain2;
2509
+ }
2510
+ async getAddress() {
2511
+ return getWarpWalletAddressFromConfig3(this.config, this.chain.name);
2512
+ }
2513
+ async getPublicKey() {
2514
+ return null;
2515
+ }
2516
+ async signTransaction(tx) {
2517
+ const address = await this.getAddress();
2518
+ throw new Error(`Wallet can not be used for signing: ${address}`);
2519
+ }
2520
+ async signMessage(message) {
2521
+ const address = await this.getAddress();
2522
+ throw new Error(`Wallet can not be used for signing: ${address}`);
2523
+ }
2524
+ create(mnemonic) {
2525
+ const address = getWarpWalletAddressFromConfig3(this.config, this.chain.name);
2526
+ throw new Error(`Wallet can not be used for signing: ${address}`);
2527
+ }
2528
+ generate() {
2529
+ const address = getWarpWalletAddressFromConfig3(this.config, this.chain.name);
2530
+ throw new Error(`Wallet can not be used for signing: ${address}`);
2531
+ }
2532
+ };
2533
+
2534
+ // src/WarpFastsetWallet.ts
2535
+ var WarpFastsetWallet = class {
2536
+ constructor(config, chain2) {
2537
+ this.config = config;
2538
+ this.chain = chain2;
2539
+ this.cachedAddress = null;
2540
+ this.cachedPublicKey = null;
2541
+ this.client = getConfiguredFastsetClient(this.config, this.chain);
2542
+ this.walletProvider = this.createProvider();
2543
+ this.initializeCache();
2544
+ }
2545
+ async signTransaction(tx) {
2546
+ if (!tx || typeof tx !== "object") throw new Error("Invalid transaction object");
2547
+ if (!this.walletProvider) throw new Error("No wallet provider available");
2548
+ if (this.walletProvider instanceof ReadOnlyWalletProvider) throw new Error(`Wallet (${this.chain.name}) is read-only`);
2549
+ return await this.walletProvider.signTransaction(tx);
2550
+ }
2551
+ async signTransactions(txs) {
2552
+ if (txs.length === 0) return [];
2553
+ const signedTxs = [];
2554
+ for (const tx of txs) {
2555
+ signedTxs.push(await this.signTransaction(tx));
2556
+ }
2557
+ return signedTxs;
2558
+ }
2559
+ async signMessage(message) {
2560
+ if (!this.walletProvider) throw new Error("No wallet provider available");
2561
+ if (this.walletProvider instanceof ReadOnlyWalletProvider) throw new Error(`Wallet (${this.chain.name}) is read-only`);
2562
+ return await this.walletProvider.signMessage(message);
2563
+ }
2564
+ async sendTransaction(tx) {
2565
+ const { signature, ...transactionWithoutSignature } = tx;
2566
+ const _cert = await this.client.submitTransaction(transactionWithoutSignature, signature ?? null);
2567
+ return "TODO";
2568
+ }
2569
+ async sendTransactions(txs) {
2570
+ return Promise.all(txs.map(async (tx) => this.sendTransaction(tx)));
2571
+ }
2572
+ create(mnemonic, provider) {
2573
+ const walletProvider = this.createProviderForOperation(provider);
2574
+ return walletProvider.create(mnemonic);
2575
+ }
2576
+ generate(provider) {
2577
+ const walletProvider = this.createProviderForOperation(provider);
2578
+ return walletProvider.generate();
2579
+ }
2580
+ getAddress() {
2581
+ return this.cachedAddress;
2582
+ }
2583
+ getPublicKey() {
2584
+ return this.cachedPublicKey;
2585
+ }
2586
+ createProvider() {
2587
+ const wallet = this.config.user?.wallets?.[this.chain.name];
2588
+ if (!wallet) return null;
2589
+ if (typeof wallet === "string") return new ReadOnlyWalletProvider(this.config, this.chain);
2590
+ return this.createProviderForOperation(wallet.provider);
2591
+ }
2592
+ initializeCache() {
2593
+ initializeWalletCache(this.walletProvider).then((cache) => {
2594
+ this.cachedAddress = cache.address;
2595
+ this.cachedPublicKey = cache.publicKey;
2596
+ });
2597
+ }
2598
+ createProviderForOperation(provider) {
2599
+ const customWalletProviders = this.config.walletProviders?.[this.chain.name];
2600
+ const providerFactory = customWalletProviders?.[provider];
2601
+ if (providerFactory) {
2602
+ const walletProvider = providerFactory(this.config, this.chain);
2603
+ if (!walletProvider) throw new Error(`Custom wallet provider factory returned null for ${provider}`);
2604
+ return walletProvider;
2605
+ }
2606
+ if (provider === "privateKey") return new PrivateKeyWalletProvider(this.config, this.chain);
2607
+ if (provider === "mnemonic") return new MnemonicWalletProvider(this.config, this.chain);
2608
+ throw new Error(`Unsupported wallet provider for ${this.chain.name}: ${provider}`);
2609
+ }
2427
2610
  };
2428
2611
 
2429
2612
  // src/main.ts