@vleap/warps-adapter-solana 0.1.0-beta.6 → 0.1.0-beta.7

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
@@ -65,6 +65,11 @@ var SolanaExplorerNames = {
65
65
  devnet: ["solscan_devnet" /* SolscanDevnet */, "solana_explorer_devnet" /* SolanaExplorerDevnet */]
66
66
  };
67
67
  var SolanaExplorerUrls = ExplorerUrls;
68
+ var X402SolanaNetworkIdentifiers = {
69
+ Mainnet: "solana:5eykt4UsFv8P8NJdTREpY1vzqKqZKvdp",
70
+ Devnet: "solana:EtWTRABZaYq6iMfeYKouRu166VU2xqa1"
71
+ };
72
+ var SupportedX402SolanaNetworks = [X402SolanaNetworkIdentifiers.Mainnet, X402SolanaNetworkIdentifiers.Devnet];
68
73
 
69
74
  // src/tokens/solana.ts
70
75
  import { WarpChainName } from "@vleap/warps";
@@ -1243,21 +1248,24 @@ var WarpSolanaExplorer = class {
1243
1248
  };
1244
1249
 
1245
1250
  // src/WarpSolanaWallet.ts
1246
- import * as bip392 from "@scure/bip39";
1247
- import { wordlist } from "@scure/bip39/wordlists/english.js";
1248
- import { Connection as Connection5, Keypair as Keypair2, Transaction as Transaction3, VersionedTransaction as VersionedTransaction2 } from "@solana/web3.js";
1251
+ import { createKeyPairSignerFromBytes } from "@solana/kit";
1252
+ import { Connection as Connection6, Transaction as Transaction4, VersionedTransaction as VersionedTransaction3 } from "@solana/web3.js";
1249
1253
  import {
1250
1254
  getProviderConfig as getProviderConfig4,
1251
1255
  initializeWalletCache
1252
1256
  } from "@vleap/warps";
1253
- import bs583 from "bs58";
1257
+ import { registerExactSvmScheme } from "@x402/svm/exact/client";
1254
1258
 
1255
- // src/providers/SolanaWalletProvider.ts
1256
- import { Keypair, Transaction as Transaction2, VersionedTransaction } from "@solana/web3.js";
1259
+ // src/providers/MnemonicWalletProvider.ts
1257
1260
  import * as bip39 from "@scure/bip39";
1261
+ import { wordlist } from "@scure/bip39/wordlists/english.js";
1262
+ import { Keypair, Transaction as Transaction2, VersionedTransaction } from "@solana/web3.js";
1263
+ import {
1264
+ getWarpWalletAddressFromConfig as getWarpWalletAddressFromConfig3,
1265
+ getWarpWalletMnemonicFromConfig
1266
+ } from "@vleap/warps";
1258
1267
  import bs582 from "bs58";
1259
- import { getWarpWalletMnemonicFromConfig, getWarpWalletPrivateKeyFromConfig } from "@vleap/warps";
1260
- var SolanaWalletProvider = class {
1268
+ var MnemonicWalletProvider = class {
1261
1269
  constructor(config, chain, connection) {
1262
1270
  this.config = config;
1263
1271
  this.chain = chain;
@@ -1265,6 +1273,8 @@ var SolanaWalletProvider = class {
1265
1273
  this.keypair = null;
1266
1274
  }
1267
1275
  async getAddress() {
1276
+ const address = getWarpWalletAddressFromConfig3(this.config, this.chain.name);
1277
+ if (address) return address;
1268
1278
  try {
1269
1279
  const keypair = this.getKeypair();
1270
1280
  return keypair.publicKey.toBase58();
@@ -1326,50 +1336,185 @@ var SolanaWalletProvider = class {
1326
1336
  getKeypairInstance() {
1327
1337
  return this.getKeypair();
1328
1338
  }
1339
+ create(mnemonic) {
1340
+ const seed = bip39.mnemonicToSeedSync(mnemonic);
1341
+ const keypair = Keypair.fromSeed(seed.slice(0, 32));
1342
+ return {
1343
+ provider: "mnemonic",
1344
+ address: keypair.publicKey.toBase58(),
1345
+ privateKey: bs582.encode(keypair.secretKey),
1346
+ mnemonic
1347
+ };
1348
+ }
1349
+ generate() {
1350
+ const keypair = Keypair.generate();
1351
+ const entropy = keypair.secretKey.slice(0, 16);
1352
+ const mnemonic = bip39.entropyToMnemonic(entropy, wordlist);
1353
+ return {
1354
+ provider: "mnemonic",
1355
+ address: keypair.publicKey.toBase58(),
1356
+ privateKey: bs582.encode(keypair.secretKey),
1357
+ mnemonic
1358
+ };
1359
+ }
1329
1360
  getKeypair() {
1330
1361
  if (this.keypair) return this.keypair;
1331
- const privateKey = getWarpWalletPrivateKeyFromConfig(this.config, this.chain.name);
1332
- if (privateKey) {
1333
- try {
1334
- const secretKey = bs582.decode(privateKey);
1335
- if (secretKey.length === 64) {
1336
- this.keypair = Keypair.fromSecretKey(secretKey);
1337
- return this.keypair;
1338
- } else if (secretKey.length === 32) {
1339
- this.keypair = Keypair.fromSeed(secretKey);
1340
- return this.keypair;
1341
- } else {
1342
- throw new Error(`Invalid private key length: expected 32 or 64 bytes, got ${secretKey.length}`);
1343
- }
1344
- } catch (error) {
1345
- if (error instanceof Error) {
1346
- throw new Error(`Invalid private key format: ${error.message}`);
1362
+ const mnemonic = getWarpWalletMnemonicFromConfig(this.config, this.chain.name);
1363
+ if (!mnemonic) throw new Error("No mnemonic provided");
1364
+ const seed = bip39.mnemonicToSeedSync(mnemonic);
1365
+ this.keypair = Keypair.fromSeed(seed.slice(0, 32));
1366
+ return this.keypair;
1367
+ }
1368
+ };
1369
+
1370
+ // src/providers/PrivateKeyWalletProvider.ts
1371
+ import * as bip392 from "@scure/bip39";
1372
+ import { wordlist as wordlist2 } from "@scure/bip39/wordlists/english.js";
1373
+ import { Keypair as Keypair2, Transaction as Transaction3, VersionedTransaction as VersionedTransaction2 } from "@solana/web3.js";
1374
+ import {
1375
+ getWarpWalletAddressFromConfig as getWarpWalletAddressFromConfig4,
1376
+ getWarpWalletPrivateKeyFromConfig
1377
+ } from "@vleap/warps";
1378
+ import bs583 from "bs58";
1379
+ var PrivateKeyWalletProvider = class {
1380
+ constructor(config, chain, connection) {
1381
+ this.config = config;
1382
+ this.chain = chain;
1383
+ this.connection = connection;
1384
+ this.keypair = null;
1385
+ }
1386
+ async getAddress() {
1387
+ const address = getWarpWalletAddressFromConfig4(this.config, this.chain.name);
1388
+ if (address) return address;
1389
+ try {
1390
+ const keypair = this.getKeypair();
1391
+ return keypair.publicKey.toBase58();
1392
+ } catch {
1393
+ return null;
1394
+ }
1395
+ }
1396
+ async getPublicKey() {
1397
+ try {
1398
+ const keypair = this.getKeypair();
1399
+ return keypair.publicKey.toBase58();
1400
+ } catch {
1401
+ return null;
1402
+ }
1403
+ }
1404
+ async signTransaction(tx) {
1405
+ const keypair = this.getKeypair();
1406
+ if (tx instanceof VersionedTransaction2) {
1407
+ tx.sign([keypair]);
1408
+ return tx;
1409
+ }
1410
+ if (tx instanceof Transaction3) {
1411
+ tx.sign(keypair);
1412
+ return tx;
1413
+ }
1414
+ if (tx.transaction) {
1415
+ if (tx.transaction instanceof Transaction3) {
1416
+ tx.transaction.sign(keypair);
1417
+ return { ...tx, transaction: tx.transaction.serialize() };
1418
+ }
1419
+ if (typeof tx.transaction === "object") {
1420
+ try {
1421
+ const transaction = Transaction3.from(tx.transaction);
1422
+ transaction.sign(keypair);
1423
+ return { ...tx, transaction: transaction.serialize(), signature: transaction.signature };
1424
+ } catch {
1425
+ throw new Error("Invalid transaction format");
1347
1426
  }
1348
- throw new Error("Invalid private key format");
1349
1427
  }
1350
1428
  }
1351
- const mnemonic = getWarpWalletMnemonicFromConfig(this.config, this.chain.name);
1352
- if (mnemonic) {
1353
- const seed = bip39.mnemonicToSeedSync(mnemonic);
1354
- this.keypair = Keypair.fromSeed(seed.slice(0, 32));
1355
- return this.keypair;
1429
+ throw new Error("Invalid transaction format");
1430
+ }
1431
+ async signMessage(message) {
1432
+ const keypair = this.getKeypair();
1433
+ const messageBytes = new TextEncoder().encode(message);
1434
+ const nacl = await import("tweetnacl");
1435
+ const secretKey = keypair.secretKey;
1436
+ if (secretKey.length !== 64) {
1437
+ throw new Error(`Invalid secret key length: expected 64, got ${secretKey.length}`);
1438
+ }
1439
+ const privateKeySlice = secretKey.slice(0, 32);
1440
+ const privateKeyBytes = new Uint8Array(privateKeySlice);
1441
+ if (privateKeyBytes.length !== 32) {
1442
+ throw new Error(`Invalid private key length: expected 32, got ${privateKeyBytes.length}`);
1443
+ }
1444
+ const signature = nacl.sign.detached(messageBytes, privateKeyBytes);
1445
+ return bs583.encode(signature);
1446
+ }
1447
+ getKeypairInstance() {
1448
+ return this.getKeypair();
1449
+ }
1450
+ create(mnemonic) {
1451
+ const seed = bip392.mnemonicToSeedSync(mnemonic);
1452
+ const keypair = Keypair2.fromSeed(seed.slice(0, 32));
1453
+ return {
1454
+ provider: "privateKey",
1455
+ address: keypair.publicKey.toBase58(),
1456
+ privateKey: bs583.encode(keypair.secretKey),
1457
+ mnemonic
1458
+ };
1459
+ }
1460
+ generate() {
1461
+ const keypair = Keypair2.generate();
1462
+ const entropy = keypair.secretKey.slice(0, 16);
1463
+ const mnemonic = bip392.entropyToMnemonic(entropy, wordlist2);
1464
+ return {
1465
+ provider: "privateKey",
1466
+ address: keypair.publicKey.toBase58(),
1467
+ privateKey: bs583.encode(keypair.secretKey),
1468
+ mnemonic
1469
+ };
1470
+ }
1471
+ getKeypair() {
1472
+ if (this.keypair) return this.keypair;
1473
+ const privateKey = getWarpWalletPrivateKeyFromConfig(this.config, this.chain.name);
1474
+ if (!privateKey) throw new Error("No private key provided");
1475
+ try {
1476
+ const secretKey = bs583.decode(privateKey);
1477
+ if (secretKey.length === 64) {
1478
+ this.keypair = Keypair2.fromSecretKey(secretKey);
1479
+ return this.keypair;
1480
+ } else if (secretKey.length === 32) {
1481
+ this.keypair = Keypair2.fromSeed(secretKey);
1482
+ return this.keypair;
1483
+ } else {
1484
+ throw new Error(`Invalid private key length: expected 32 or 64 bytes, got ${secretKey.length}`);
1485
+ }
1486
+ } catch (error) {
1487
+ if (error instanceof Error) {
1488
+ throw new Error(`Invalid private key format: ${error.message}`);
1489
+ }
1490
+ throw new Error("Invalid private key format");
1356
1491
  }
1357
- throw new Error("No private key or mnemonic provided");
1358
1492
  }
1359
1493
  };
1360
1494
 
1361
1495
  // src/WarpSolanaWallet.ts
1362
1496
  var WarpSolanaWallet = class {
1363
- constructor(config, chain, walletProvider) {
1497
+ constructor(config, chain) {
1364
1498
  this.config = config;
1365
1499
  this.chain = chain;
1366
1500
  this.cachedAddress = null;
1367
1501
  this.cachedPublicKey = null;
1368
1502
  const providerConfig = getProviderConfig4(config, chain.name, config.env, chain.defaultApiUrl);
1369
- this.connection = new Connection5(providerConfig.url, "confirmed");
1370
- this.walletProvider = walletProvider || new SolanaWalletProvider(config, chain, this.connection);
1503
+ this.connection = new Connection6(providerConfig.url, "confirmed");
1504
+ this.walletProvider = this.createProvider();
1371
1505
  this.initializeCache();
1372
1506
  }
1507
+ createProvider() {
1508
+ const wallet = this.config.user?.wallets?.[this.chain.name];
1509
+ if (!wallet) return null;
1510
+ if (typeof wallet === "string") throw new Error(`Wallet can not be used for signing: ${wallet}`);
1511
+ const customWalletProviders = this.config.walletProviders?.[this.chain.name];
1512
+ const providerFactory = customWalletProviders?.[wallet.provider];
1513
+ if (providerFactory) return providerFactory(this.config, this.chain);
1514
+ if (wallet.provider === "privateKey") return new PrivateKeyWalletProvider(this.config, this.chain, this.connection);
1515
+ if (wallet.provider === "mnemonic") return new MnemonicWalletProvider(this.config, this.chain, this.connection);
1516
+ throw new Error(`Unsupported wallet provider for ${this.chain.name}: ${wallet.provider}`);
1517
+ }
1373
1518
  initializeCache() {
1374
1519
  initializeWalletCache(this.walletProvider).then((cache) => {
1375
1520
  this.cachedAddress = cache.address;
@@ -1378,6 +1523,7 @@ var WarpSolanaWallet = class {
1378
1523
  }
1379
1524
  async signTransaction(tx) {
1380
1525
  if (!tx || typeof tx !== "object") throw new Error("Invalid transaction object");
1526
+ if (!this.walletProvider) throw new Error("No wallet provider available");
1381
1527
  return await this.walletProvider.signTransaction(tx);
1382
1528
  }
1383
1529
  async signTransactions(txs) {
@@ -1385,28 +1531,29 @@ var WarpSolanaWallet = class {
1385
1531
  return Promise.all(txs.map(async (tx) => this.signTransaction(tx)));
1386
1532
  }
1387
1533
  async signMessage(message) {
1534
+ if (!this.walletProvider) throw new Error("No wallet provider available");
1388
1535
  return await this.walletProvider.signMessage(message);
1389
1536
  }
1390
1537
  async sendTransaction(tx) {
1391
1538
  if (!tx || typeof tx !== "object") throw new Error("Invalid transaction object");
1392
1539
  let transaction;
1393
- if (tx instanceof VersionedTransaction2) {
1540
+ if (tx instanceof VersionedTransaction3) {
1394
1541
  transaction = tx;
1395
- } else if (tx instanceof Transaction3) {
1542
+ } else if (tx instanceof Transaction4) {
1396
1543
  transaction = tx;
1397
1544
  } else if (tx.transaction) {
1398
- if (tx.transaction instanceof Transaction3) {
1545
+ if (tx.transaction instanceof Transaction4) {
1399
1546
  transaction = tx.transaction;
1400
- } else if (tx.transaction instanceof VersionedTransaction2) {
1547
+ } else if (tx.transaction instanceof VersionedTransaction3) {
1401
1548
  transaction = tx.transaction;
1402
1549
  } else if (typeof tx.transaction === "object") {
1403
1550
  try {
1404
- transaction = Transaction3.from(tx.transaction);
1551
+ transaction = Transaction4.from(tx.transaction);
1405
1552
  } catch {
1406
1553
  throw new Error("Invalid transaction format");
1407
1554
  }
1408
1555
  } else if (Buffer.isBuffer(tx.transaction) || typeof tx.transaction === "string") {
1409
- transaction = Transaction3.from(tx.transaction);
1556
+ transaction = Transaction4.from(tx.transaction);
1410
1557
  } else {
1411
1558
  throw new Error("Transaction must be signed before sending");
1412
1559
  }
@@ -1423,23 +1570,12 @@ var WarpSolanaWallet = class {
1423
1570
  return Promise.all(txs.map(async (tx) => this.sendTransaction(tx)));
1424
1571
  }
1425
1572
  create(mnemonic) {
1426
- const seed = bip392.mnemonicToSeedSync(mnemonic);
1427
- const keypair = Keypair2.fromSeed(seed.slice(0, 32));
1428
- return {
1429
- address: keypair.publicKey.toBase58(),
1430
- privateKey: bs583.encode(keypair.secretKey),
1431
- mnemonic
1432
- };
1573
+ if (!this.walletProvider) throw new Error("No wallet provider available");
1574
+ return this.walletProvider.create(mnemonic);
1433
1575
  }
1434
1576
  generate() {
1435
- const keypair = Keypair2.generate();
1436
- const entropy = keypair.secretKey.slice(0, 16);
1437
- const mnemonic = bip392.entropyToMnemonic(entropy, wordlist);
1438
- return {
1439
- address: keypair.publicKey.toBase58(),
1440
- privateKey: bs583.encode(keypair.secretKey),
1441
- mnemonic
1442
- };
1577
+ if (!this.walletProvider) throw new Error("No wallet provider available");
1578
+ return this.walletProvider.generate();
1443
1579
  }
1444
1580
  getAddress() {
1445
1581
  return this.cachedAddress;
@@ -1447,6 +1583,22 @@ var WarpSolanaWallet = class {
1447
1583
  getPublicKey() {
1448
1584
  return this.cachedPublicKey;
1449
1585
  }
1586
+ async registerX402Handlers(client) {
1587
+ if (!this.walletProvider) return {};
1588
+ const provider = this.walletProvider;
1589
+ const getKeypair = provider.getKeypairInstance;
1590
+ if (typeof getKeypair !== "function") return {};
1591
+ const keypair = getKeypair();
1592
+ if (!keypair || !keypair.secretKey) return {};
1593
+ const signer = await createKeyPairSignerFromBytes(keypair.secretKey);
1594
+ const handlers = {};
1595
+ for (const network of SupportedX402SolanaNetworks) {
1596
+ handlers[network] = () => {
1597
+ registerExactSvmScheme(client, { signer });
1598
+ };
1599
+ }
1600
+ return handlers;
1601
+ }
1450
1602
  };
1451
1603
 
1452
1604
  // src/chains/common.ts
@@ -1478,7 +1630,7 @@ var NativeTokenSol = {
1478
1630
  decimals: 9,
1479
1631
  logoUrl: "https://joai.ai/images/tokens/sol.svg"
1480
1632
  };
1481
- var getSolanaAdapter = createSolanaAdapter(WarpChainName2.Solana, {
1633
+ var SolanaAdapter = createSolanaAdapter(WarpChainName2.Solana, {
1482
1634
  mainnet: {
1483
1635
  name: WarpChainName2.Solana,
1484
1636
  displayName: "Solana Mainnet",
@@ -1514,10 +1666,12 @@ export {
1514
1666
  ExplorerUrls,
1515
1667
  KnownTokens,
1516
1668
  NativeTokenSol,
1669
+ SolanaAdapter,
1517
1670
  SolanaExplorerMap,
1518
1671
  SolanaExplorerNames,
1519
1672
  SolanaExplorerUrls,
1520
1673
  SolanaExplorers,
1674
+ SupportedX402SolanaNetworks,
1521
1675
  WarpSolanaConstants,
1522
1676
  WarpSolanaDataLoader,
1523
1677
  WarpSolanaExecutor,
@@ -1525,8 +1679,8 @@ export {
1525
1679
  WarpSolanaOutput,
1526
1680
  WarpSolanaSerializer,
1527
1681
  WarpSolanaWallet,
1682
+ X402SolanaNetworkIdentifiers,
1528
1683
  findKnownTokenById,
1529
- getKnownTokensForChain,
1530
- getSolanaAdapter
1684
+ getKnownTokensForChain
1531
1685
  };
1532
1686
  //# sourceMappingURL=index.mjs.map