@vleap/warps-adapter-solana 0.1.0-beta.5 → 0.1.0-beta.6
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.d.cts +6 -3
- package/dist/index.d.ts +6 -3
- package/dist/index.js +112 -85
- package/dist/index.js.map +1 -1
- package/dist/index.mjs +107 -81
- package/dist/index.mjs.map +1 -1
- package/package.json +2 -2
package/dist/index.mjs
CHANGED
|
@@ -1243,24 +1243,44 @@ var WarpSolanaExplorer = class {
|
|
|
1243
1243
|
};
|
|
1244
1244
|
|
|
1245
1245
|
// 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";
|
|
1246
1249
|
import {
|
|
1247
1250
|
getProviderConfig as getProviderConfig4,
|
|
1248
|
-
|
|
1249
|
-
getWarpWalletPrivateKeyFromConfig
|
|
1251
|
+
initializeWalletCache
|
|
1250
1252
|
} from "@vleap/warps";
|
|
1251
|
-
import
|
|
1253
|
+
import bs583 from "bs58";
|
|
1254
|
+
|
|
1255
|
+
// src/providers/SolanaWalletProvider.ts
|
|
1256
|
+
import { Keypair, Transaction as Transaction2, VersionedTransaction } from "@solana/web3.js";
|
|
1252
1257
|
import * as bip39 from "@scure/bip39";
|
|
1253
|
-
import { wordlist } from "@scure/bip39/wordlists/english.js";
|
|
1254
1258
|
import bs582 from "bs58";
|
|
1255
|
-
|
|
1256
|
-
|
|
1259
|
+
import { getWarpWalletMnemonicFromConfig, getWarpWalletPrivateKeyFromConfig } from "@vleap/warps";
|
|
1260
|
+
var SolanaWalletProvider = class {
|
|
1261
|
+
constructor(config, chain, connection) {
|
|
1257
1262
|
this.config = config;
|
|
1258
1263
|
this.chain = chain;
|
|
1259
|
-
|
|
1260
|
-
this.
|
|
1264
|
+
this.connection = connection;
|
|
1265
|
+
this.keypair = null;
|
|
1266
|
+
}
|
|
1267
|
+
async getAddress() {
|
|
1268
|
+
try {
|
|
1269
|
+
const keypair = this.getKeypair();
|
|
1270
|
+
return keypair.publicKey.toBase58();
|
|
1271
|
+
} catch {
|
|
1272
|
+
return null;
|
|
1273
|
+
}
|
|
1274
|
+
}
|
|
1275
|
+
async getPublicKey() {
|
|
1276
|
+
try {
|
|
1277
|
+
const keypair = this.getKeypair();
|
|
1278
|
+
return keypair.publicKey.toBase58();
|
|
1279
|
+
} catch {
|
|
1280
|
+
return null;
|
|
1281
|
+
}
|
|
1261
1282
|
}
|
|
1262
1283
|
async signTransaction(tx) {
|
|
1263
|
-
if (!tx || typeof tx !== "object") throw new Error("Invalid transaction object");
|
|
1264
1284
|
const keypair = this.getKeypair();
|
|
1265
1285
|
if (tx instanceof VersionedTransaction) {
|
|
1266
1286
|
tx.sign([keypair]);
|
|
@@ -1287,28 +1307,6 @@ var WarpSolanaWallet = class {
|
|
|
1287
1307
|
}
|
|
1288
1308
|
throw new Error("Invalid transaction format");
|
|
1289
1309
|
}
|
|
1290
|
-
async signTransactions(txs) {
|
|
1291
|
-
if (txs.length === 0) return [];
|
|
1292
|
-
const keypair = this.getKeypair();
|
|
1293
|
-
return Promise.all(
|
|
1294
|
-
txs.map(async (tx) => {
|
|
1295
|
-
if (tx instanceof VersionedTransaction) {
|
|
1296
|
-
tx.sign([keypair]);
|
|
1297
|
-
return tx;
|
|
1298
|
-
}
|
|
1299
|
-
if (tx instanceof Transaction2) {
|
|
1300
|
-
tx.sign(keypair);
|
|
1301
|
-
return tx;
|
|
1302
|
-
}
|
|
1303
|
-
if (tx.transaction && typeof tx.transaction === "object") {
|
|
1304
|
-
const transaction = Transaction2.from(tx.transaction);
|
|
1305
|
-
transaction.sign(keypair);
|
|
1306
|
-
return { ...tx, transaction: transaction.serialize() };
|
|
1307
|
-
}
|
|
1308
|
-
throw new Error("Invalid transaction format");
|
|
1309
|
-
})
|
|
1310
|
-
);
|
|
1311
|
-
}
|
|
1312
1310
|
async signMessage(message) {
|
|
1313
1311
|
const keypair = this.getKeypair();
|
|
1314
1312
|
const messageBytes = new TextEncoder().encode(message);
|
|
@@ -1325,26 +1323,90 @@ var WarpSolanaWallet = class {
|
|
|
1325
1323
|
const signature = nacl.sign.detached(messageBytes, privateKeyBytes);
|
|
1326
1324
|
return bs582.encode(signature);
|
|
1327
1325
|
}
|
|
1326
|
+
getKeypairInstance() {
|
|
1327
|
+
return this.getKeypair();
|
|
1328
|
+
}
|
|
1329
|
+
getKeypair() {
|
|
1330
|
+
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}`);
|
|
1347
|
+
}
|
|
1348
|
+
throw new Error("Invalid private key format");
|
|
1349
|
+
}
|
|
1350
|
+
}
|
|
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;
|
|
1356
|
+
}
|
|
1357
|
+
throw new Error("No private key or mnemonic provided");
|
|
1358
|
+
}
|
|
1359
|
+
};
|
|
1360
|
+
|
|
1361
|
+
// src/WarpSolanaWallet.ts
|
|
1362
|
+
var WarpSolanaWallet = class {
|
|
1363
|
+
constructor(config, chain, walletProvider) {
|
|
1364
|
+
this.config = config;
|
|
1365
|
+
this.chain = chain;
|
|
1366
|
+
this.cachedAddress = null;
|
|
1367
|
+
this.cachedPublicKey = null;
|
|
1368
|
+
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);
|
|
1371
|
+
this.initializeCache();
|
|
1372
|
+
}
|
|
1373
|
+
initializeCache() {
|
|
1374
|
+
initializeWalletCache(this.walletProvider).then((cache) => {
|
|
1375
|
+
this.cachedAddress = cache.address;
|
|
1376
|
+
this.cachedPublicKey = cache.publicKey;
|
|
1377
|
+
});
|
|
1378
|
+
}
|
|
1379
|
+
async signTransaction(tx) {
|
|
1380
|
+
if (!tx || typeof tx !== "object") throw new Error("Invalid transaction object");
|
|
1381
|
+
return await this.walletProvider.signTransaction(tx);
|
|
1382
|
+
}
|
|
1383
|
+
async signTransactions(txs) {
|
|
1384
|
+
if (txs.length === 0) return [];
|
|
1385
|
+
return Promise.all(txs.map(async (tx) => this.signTransaction(tx)));
|
|
1386
|
+
}
|
|
1387
|
+
async signMessage(message) {
|
|
1388
|
+
return await this.walletProvider.signMessage(message);
|
|
1389
|
+
}
|
|
1328
1390
|
async sendTransaction(tx) {
|
|
1329
1391
|
if (!tx || typeof tx !== "object") throw new Error("Invalid transaction object");
|
|
1330
1392
|
let transaction;
|
|
1331
|
-
if (tx instanceof
|
|
1393
|
+
if (tx instanceof VersionedTransaction2) {
|
|
1332
1394
|
transaction = tx;
|
|
1333
|
-
} else if (tx instanceof
|
|
1395
|
+
} else if (tx instanceof Transaction3) {
|
|
1334
1396
|
transaction = tx;
|
|
1335
1397
|
} else if (tx.transaction) {
|
|
1336
|
-
if (tx.transaction instanceof
|
|
1398
|
+
if (tx.transaction instanceof Transaction3) {
|
|
1337
1399
|
transaction = tx.transaction;
|
|
1338
|
-
} else if (tx.transaction instanceof
|
|
1400
|
+
} else if (tx.transaction instanceof VersionedTransaction2) {
|
|
1339
1401
|
transaction = tx.transaction;
|
|
1340
1402
|
} else if (typeof tx.transaction === "object") {
|
|
1341
1403
|
try {
|
|
1342
|
-
transaction =
|
|
1404
|
+
transaction = Transaction3.from(tx.transaction);
|
|
1343
1405
|
} catch {
|
|
1344
1406
|
throw new Error("Invalid transaction format");
|
|
1345
1407
|
}
|
|
1346
1408
|
} else if (Buffer.isBuffer(tx.transaction) || typeof tx.transaction === "string") {
|
|
1347
|
-
transaction =
|
|
1409
|
+
transaction = Transaction3.from(tx.transaction);
|
|
1348
1410
|
} else {
|
|
1349
1411
|
throw new Error("Transaction must be signed before sending");
|
|
1350
1412
|
}
|
|
@@ -1361,65 +1423,29 @@ var WarpSolanaWallet = class {
|
|
|
1361
1423
|
return Promise.all(txs.map(async (tx) => this.sendTransaction(tx)));
|
|
1362
1424
|
}
|
|
1363
1425
|
create(mnemonic) {
|
|
1364
|
-
const seed =
|
|
1365
|
-
const keypair =
|
|
1426
|
+
const seed = bip392.mnemonicToSeedSync(mnemonic);
|
|
1427
|
+
const keypair = Keypair2.fromSeed(seed.slice(0, 32));
|
|
1366
1428
|
return {
|
|
1367
1429
|
address: keypair.publicKey.toBase58(),
|
|
1368
|
-
privateKey:
|
|
1430
|
+
privateKey: bs583.encode(keypair.secretKey),
|
|
1369
1431
|
mnemonic
|
|
1370
1432
|
};
|
|
1371
1433
|
}
|
|
1372
1434
|
generate() {
|
|
1373
|
-
const keypair =
|
|
1435
|
+
const keypair = Keypair2.generate();
|
|
1374
1436
|
const entropy = keypair.secretKey.slice(0, 16);
|
|
1375
|
-
const mnemonic =
|
|
1437
|
+
const mnemonic = bip392.entropyToMnemonic(entropy, wordlist);
|
|
1376
1438
|
return {
|
|
1377
1439
|
address: keypair.publicKey.toBase58(),
|
|
1378
|
-
privateKey:
|
|
1440
|
+
privateKey: bs583.encode(keypair.secretKey),
|
|
1379
1441
|
mnemonic
|
|
1380
1442
|
};
|
|
1381
1443
|
}
|
|
1382
1444
|
getAddress() {
|
|
1383
|
-
|
|
1384
|
-
const keypair = this.getKeypair();
|
|
1385
|
-
return keypair.publicKey.toBase58();
|
|
1386
|
-
} catch {
|
|
1387
|
-
return null;
|
|
1388
|
-
}
|
|
1445
|
+
return this.cachedAddress;
|
|
1389
1446
|
}
|
|
1390
1447
|
getPublicKey() {
|
|
1391
|
-
|
|
1392
|
-
const keypair = this.getKeypair();
|
|
1393
|
-
return keypair.publicKey.toBase58();
|
|
1394
|
-
} catch {
|
|
1395
|
-
return null;
|
|
1396
|
-
}
|
|
1397
|
-
}
|
|
1398
|
-
getKeypair() {
|
|
1399
|
-
const privateKey = getWarpWalletPrivateKeyFromConfig(this.config, this.chain.name);
|
|
1400
|
-
if (privateKey) {
|
|
1401
|
-
try {
|
|
1402
|
-
const secretKey = bs582.decode(privateKey);
|
|
1403
|
-
if (secretKey.length === 64) {
|
|
1404
|
-
return Keypair.fromSecretKey(secretKey);
|
|
1405
|
-
} else if (secretKey.length === 32) {
|
|
1406
|
-
return Keypair.fromSeed(secretKey);
|
|
1407
|
-
} else {
|
|
1408
|
-
throw new Error(`Invalid private key length: expected 32 or 64 bytes, got ${secretKey.length}`);
|
|
1409
|
-
}
|
|
1410
|
-
} catch (error) {
|
|
1411
|
-
if (error instanceof Error) {
|
|
1412
|
-
throw new Error(`Invalid private key format: ${error.message}`);
|
|
1413
|
-
}
|
|
1414
|
-
throw new Error("Invalid private key format");
|
|
1415
|
-
}
|
|
1416
|
-
}
|
|
1417
|
-
const mnemonic = getWarpWalletMnemonicFromConfig(this.config, this.chain.name);
|
|
1418
|
-
if (mnemonic) {
|
|
1419
|
-
const seed = bip39.mnemonicToSeedSync(mnemonic);
|
|
1420
|
-
return Keypair.fromSeed(seed.slice(0, 32));
|
|
1421
|
-
}
|
|
1422
|
-
throw new Error("No private key or mnemonic provided");
|
|
1448
|
+
return this.cachedPublicKey;
|
|
1423
1449
|
}
|
|
1424
1450
|
};
|
|
1425
1451
|
|