UNPKG

@toruslabs/solana-embed

Version:
105 lines (101 loc) 3.17 kB
'use strict'; var _objectSpread = require('@babel/runtime/helpers/objectSpread2'); var web3_js = require('@solana/web3.js'); var Web3Auth = require('@web3auth/ws-embed'); class Torus extends Web3Auth { constructor(params = {}) { super(_objectSpread(_objectSpread({}, params), {}, { web3AuthClientId: "BB512eQAHnoO6WlU8gOW7w2uzcuxvSEkJMXRcQYlqt-8mqbGBGPXajL4DQ3_b5A3WDeDAuEtyTNB8iaSHNZMb4k", web3AuthNetwork: "mainnet" })); } async init(params = {}) { await super.init(_objectSpread({ chainId: "0x65", chains: [], confirmationStrategy: "popup" }, params)); } async loginWithSessionId(_) { throw new Error("Not implemented"); } // Solana specific API async requestAccounts() { const accounts = await this.provider.request({ method: Web3Auth.SOLANA_METHOD_TYPES.SOLANA_REQUEST_ACCOUNTS }); return accounts; } async getAccounts() { const accounts = await this.provider.request({ method: Web3Auth.SOLANA_METHOD_TYPES.GET_ACCOUNTS }); return accounts; } async signAndSendTransaction(transaction) { const signature = await this.provider.request({ method: Web3Auth.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: Web3Auth.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: Web3Auth.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: Web3Auth.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"); } } module.exports = Torus;