@toruslabs/solana-embed
Version:
Embed script for solana blockchain
109 lines (101 loc) • 3.19 kB
JavaScript
import _objectSpread from '@babel/runtime/helpers/objectSpread2';
import Web3Auth, { SOLANA_METHOD_TYPES } from '@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(_objectSpread({
chainId: "0x65",
chains: [],
confirmationStrategy: "popup"
}, params), {}, {
whiteLabel: _objectSpread({
logoDark: "https://images.web3auth.io/web3auth-logo-w-light.svg",
logoLight: "https://images.web3auth.io/web3auth-logo-w.svg"
}, params.whiteLabel || {})
}));
}
async loginWithSessionId(_) {
throw new Error("Not implemented");
}
// Solana specific API
async requestAccounts() {
const accounts = await this.provider.request({
method: SOLANA_METHOD_TYPES.SOLANA_REQUEST_ACCOUNTS
});
return accounts;
}
async getAccounts() {
const accounts = await this.provider.request({
method: SOLANA_METHOD_TYPES.GET_ACCOUNTS
});
return accounts;
}
async signAndSendTransaction(transaction) {
const signature = await this.provider.request({
method: 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: 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: 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: 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) {
return Buffer.from(transaction.serialize({
requireAllSignatures: false
})).toString("base64");
}
}
export { Torus as default };