UNPKG

@agentix/wallet-solana

Version:
203 lines (198 loc) 7.71 kB
"use strict"; var __create = Object.create; var __defProp = Object.defineProperty; var __getOwnPropDesc = Object.getOwnPropertyDescriptor; var __getOwnPropNames = Object.getOwnPropertyNames; var __getProtoOf = Object.getPrototypeOf; var __hasOwnProp = Object.prototype.hasOwnProperty; var __typeError = (msg) => { throw TypeError(msg); }; var __export = (target, all) => { for (var name in all) __defProp(target, name, { get: all[name], enumerable: true }); }; var __copyProps = (to, from, except, desc) => { if (from && typeof from === "object" || typeof from === "function") { for (let key of __getOwnPropNames(from)) if (!__hasOwnProp.call(to, key) && key !== except) __defProp(to, key, { get: () => from[key], enumerable: !(desc = __getOwnPropDesc(from, key)) || desc.enumerable }); } return to; }; var __toESM = (mod, isNodeMode, target) => (target = mod != null ? __create(__getProtoOf(mod)) : {}, __copyProps( // If the importer is in node compatibility mode or this is not an ESM // file that has been converted to a CommonJS file using a Babel- // compatible transform (i.e. "__esModule" has not been set), then set // "default" to the CommonJS "module.exports" for node compatibility. isNodeMode || !mod || !mod.__esModule ? __defProp(target, "default", { value: mod, enumerable: true }) : target, mod )); var __toCommonJS = (mod) => __copyProps(__defProp({}, "__esModule", { value: true }), mod); var __accessCheck = (obj, member, msg) => member.has(obj) || __typeError("Cannot " + msg); var __privateGet = (obj, member, getter) => (__accessCheck(obj, member, "read from private field"), getter ? getter.call(obj) : member.get(obj)); var __privateAdd = (obj, member, value) => member.has(obj) ? __typeError("Cannot add the same private member more than once") : member instanceof WeakSet ? member.add(obj) : member.set(obj, value); var __privateSet = (obj, member, value, setter) => (__accessCheck(obj, member, "write to private field"), setter ? setter.call(obj, value) : member.set(obj, value), value); // src/index.ts var index_exports = {}; __export(index_exports, { SolanaAdaptorWalletClient: () => SolanaAdaptorWalletClient, SolanaKeypairWalletClient: () => SolanaKeypairWalletClient, SolanaWalletClient: () => SolanaWalletClient, isVersionedTransaction: () => isVersionedTransaction, solanaAdaptor: () => solanaAdaptor, solanaKeypair: () => solanaKeypair }); module.exports = __toCommonJS(index_exports); // src/SolanaWalletClient.ts var import_agentix = require("agentix"); var import_web3 = require("@solana/web3.js"); var import_viem = require("viem"); var SolanaWalletClient = class extends import_agentix.SolanaWalletBase { constructor(params) { super(); this.connection = params.connection; } getChain() { return { type: "solana" }; } getConnection() { return this.connection; } async balanceOf(address) { const pubkey = new import_web3.PublicKey(address); const balance = await this.connection.getBalance(pubkey); return { decimals: 9, symbol: "SOL", name: "Solana", value: (0, import_viem.formatUnits)(BigInt(balance), 9), inBaseUnits: balance.toString() }; } }; // src/SolanaKeypairWalletClient.ts var import_web32 = require("@solana/web3.js"); var import_tweetnacl = __toESM(require("tweetnacl"), 1); var isVersionedTransaction = (tx) => { return "version" in tx; }; var _keypair; var SolanaKeypairWalletClient = class extends SolanaWalletClient { constructor(params) { const { keypair, connection } = params; super({ connection }); __privateAdd(this, _keypair); __privateSet(this, _keypair, keypair); } getAddress() { return __privateGet(this, _keypair).publicKey.toBase58(); } async signTransaction(transaction) { if (isVersionedTransaction(transaction)) { transaction.sign([__privateGet(this, _keypair)]); } else { transaction.partialSign(__privateGet(this, _keypair)); } return transaction; } async signAllTransactions(txs) { return txs.map((t) => { if (isVersionedTransaction(t)) { t.sign([__privateGet(this, _keypair)]); } else { t.partialSign(__privateGet(this, _keypair)); } return t; }); } async sendTransaction(transaction) { const connection = this.connection; if (transaction instanceof import_web32.VersionedTransaction) { transaction.sign([__privateGet(this, _keypair)]); } else { transaction.partialSign(__privateGet(this, _keypair)); } return await connection.sendRawTransaction(transaction.serialize()); } async signMessage(message) { const messageBytes = Buffer.from(message); const signature = import_tweetnacl.default.sign.detached(messageBytes, __privateGet(this, _keypair).secretKey); return { signature: Buffer.from(signature).toString("hex") }; } async signAndSendTransaction(transaction, options) { const connection = this.connection; if (transaction instanceof import_web32.VersionedTransaction) { transaction.sign([__privateGet(this, _keypair)]); } else { transaction.partialSign(__privateGet(this, _keypair)); } const signature = await connection.sendRawTransaction( transaction.serialize(), options ); return { signature }; } }; _keypair = new WeakMap(); var solanaKeypair = (params) => new SolanaKeypairWalletClient(params); // src/SolanaAdaptorWalletClient.ts var _publicKey, _sendTransaction, _signAllTransactions, _signMessage, _signTransaction; var SolanaAdaptorWalletClient = class extends SolanaWalletClient { constructor(params) { const { publicKey, sendTransaction, signAllTransactions, signMessage, signTransaction, connection } = params; super({ connection }); __privateAdd(this, _publicKey); __privateAdd(this, _sendTransaction); __privateAdd(this, _signAllTransactions); __privateAdd(this, _signMessage); __privateAdd(this, _signTransaction); __privateSet(this, _publicKey, publicKey); __privateSet(this, _sendTransaction, sendTransaction); __privateSet(this, _signAllTransactions, signAllTransactions); __privateSet(this, _signMessage, signMessage); __privateSet(this, _signTransaction, signTransaction); } getAddress() { return __privateGet(this, _publicKey).toBase58(); } async signTransaction(transaction) { return __privateGet(this, _signTransaction).call(this, transaction); } async signAllTransactions(txs) { return __privateGet(this, _signAllTransactions).call(this, txs); } async sendTransaction(transaction) { return __privateGet(this, _sendTransaction).call(this, transaction, this.connection); } async signMessage(message) { const messageBytes = Buffer.from(message); const signature = await __privateGet(this, _signMessage).call(this, messageBytes); return { signature: Buffer.from(signature).toString("hex") }; } async signAndSendTransaction(transaction, options) { const signature = await __privateGet(this, _sendTransaction).call(this, await __privateGet(this, _signTransaction).call(this, transaction), this.connection, options); return { signature }; } }; _publicKey = new WeakMap(); _sendTransaction = new WeakMap(); _signAllTransactions = new WeakMap(); _signMessage = new WeakMap(); _signTransaction = new WeakMap(); var solanaAdaptor = (params) => new SolanaAdaptorWalletClient(params); // Annotate the CommonJS export names for ESM import in node: 0 && (module.exports = { SolanaAdaptorWalletClient, SolanaKeypairWalletClient, SolanaWalletClient, isVersionedTransaction, solanaAdaptor, solanaKeypair });