UNPKG

@web3auth/no-modal

Version:
92 lines (88 loc) 2.76 kB
'use strict'; var _defineProperty = require('@babel/runtime/helpers/defineProperty'); var web3_js = require('@solana/web3.js'); var wsEmbed = require('@web3auth/ws-embed'); class SolanaWallet { constructor(provider) { _defineProperty(this, "provider", void 0); this.provider = provider; } async requestAccounts() { const accounts = await this.provider.request({ method: wsEmbed.SOLANA_METHOD_TYPES.SOLANA_REQUEST_ACCOUNTS }); return accounts; } async getAccounts() { const accounts = await this.provider.request({ method: wsEmbed.SOLANA_METHOD_TYPES.GET_ACCOUNTS }); return accounts; } async signAndSendTransaction(transaction) { const signature = await this.provider.request({ method: wsEmbed.SOLANA_METHOD_TYPES.SEND_TRANSACTION, params: { message: this.serializeTransaction(transaction) } }); return signature; } /** * Signs a transaction and returns the signature * @param transaction - The transaction to sign * @returns The signature of the transaction encoded in base58 */ async signTransaction(transaction) { const signature = await this.provider.request({ method: wsEmbed.SOLANA_METHOD_TYPES.SIGN_TRANSACTION, params: { message: this.serializeTransaction(transaction) } }); return signature; } /** * Signs multiple transactions and returns the serialized transactions * @param transactions - The transactions to sign * @returns The serialized transactions encoded in base64 */ async signAllTransactions(transactions) { const serializedTransactions = transactions.map(tx => this.serializeTransaction(tx)); const signedTransactions = await this.provider.request({ method: wsEmbed.SOLANA_METHOD_TYPES.SIGN_ALL_TRANSACTIONS, params: { message: serializedTransactions } }); return signedTransactions; } /** * Signs a message and returns the signature * @param message - The message to sign * @returns The signature of the message encoded in base58 */ async signMessage(message, pubKey) { const response = await this.provider.request({ method: wsEmbed.SOLANA_METHOD_TYPES.SIGN_MESSAGE, params: { data: message, from: pubKey } }); return response; } async request(args) { const result = await this.provider.request(args); return result; } serializeTransaction(transaction) { if (transaction instanceof web3_js.VersionedTransaction) { return Buffer.from(transaction.serialize()).toString("base64"); } return Buffer.from(transaction.serialize({ requireAllSignatures: false })).toString("base64"); } } exports.SolanaWallet = SolanaWallet;