UNPKG

ribbit-wallet-connect

Version:

Next-generation multi-chain wallet and payments app that makes crypto simple, secure, and usable in daily life.

106 lines (105 loc) 4.63 kB
"use strict"; Object.defineProperty(exports, "__esModule", { value: true }); exports.RibbitWalletSDK = void 0; const types_1 = require("../core/types"); const ts_sdk_1 = require("@aptos-labs/ts-sdk"); const buffer_1 = require("buffer"); const validation_1 = require("../utils/validation"); const crypto_1 = require("../utils/crypto"); const SESSION_KEY = 'RibbitWalletSession'; class RibbitWalletSDK { constructor(transport) { this.transport = transport; this.session = null; this.initialized = false; } initialize() { if (typeof window !== 'undefined' && !this.initialized) { this.transport.listen(); this.loadSession(); this.initialized = true; } } saveSession(s) { this.session = s; sessionStorage.setItem(SESSION_KEY, JSON.stringify(s)); } loadSession() { const raw = sessionStorage.getItem(SESSION_KEY); if (raw) this.session = JSON.parse(raw); } clearSession() { this.session = null; sessionStorage.removeItem(SESSION_KEY); } async connectToWallet() { if (this.session) return this.session; const sessionId = crypto_1.CryptoUtils.generateSessionId(); const expiresAt = Date.now() + 365 * 24 * 60 * 60 * 1000; // 365 days this.saveSession({ sessionId, expiresAt }); console.log('Session created'); return { sessionId, expiresAt }; } async createRawTransactionBuffer(rawTxnRequest) { const { sender, sequenceNumber, moduleAddress, moduleName, functionName, typeArgs = [], args, maxGasAmount, gasUnitPrice, expirationTimestampSecs, chainId, } = rawTxnRequest; try { validation_1.ValidationUtils.validateRawTxnRequest(rawTxnRequest); const entryFunc = new ts_sdk_1.EntryFunction(new ts_sdk_1.ModuleId(ts_sdk_1.AccountAddress.fromString(moduleAddress), new ts_sdk_1.Identifier(moduleName)), new ts_sdk_1.Identifier(functionName), typeArgs, args); const payload = new ts_sdk_1.TransactionPayloadEntryFunction(entryFunc); const rawTxn = new ts_sdk_1.RawTransaction(ts_sdk_1.AccountAddress.fromString(sender), BigInt(sequenceNumber), payload, BigInt(maxGasAmount), BigInt(gasUnitPrice), BigInt(expirationTimestampSecs), new ts_sdk_1.ChainId(chainId)); const serializer = new ts_sdk_1.Serializer(); rawTxn.serialize(serializer); const rawTxnBase64 = buffer_1.Buffer.from(serializer.toUint8Array()).toString('base64'); return rawTxnBase64; } catch (error) { console.error('Error creating raw transaction buffer:', error); const errorMessage = error instanceof Error ? error.message : String(error); throw new Error(`Failed to create raw transaction buffer: ${errorMessage}`); } } async signMessage(messageRequest) { if (!this.session) throw new Error('Not connected'); return this.transport.sendMessage(types_1.TransportMessageType.SIGN_MESSAGE, Object.assign(Object.assign({}, messageRequest), { sessionId: this.session.sessionId })); } async signAndSendRawTransaction(tx) { if (!this.session) throw new Error('Not connected'); if (!validation_1.ValidationUtils.validateTransactionPayload(tx)) throw new Error('Invalid tx payload'); return this.transport.sendMessage(types_1.TransportMessageType.SEND_TRANSACTION, Object.assign(Object.assign({}, tx), { sessionId: this.session.sessionId })); } async getWalletAddress(chainId) { if (!this.session) throw new Error('Not connected'); return this.transport.sendMessage(types_1.TransportMessageType.GET_WALLET_ADDRESS, { chainId, sessionId: this.session.sessionId, }); } async getWalletBalance(chainId, resourceType, decimals) { if (!this.session) throw new Error('Not connected'); return this.transport.sendMessage(types_1.TransportMessageType.GET_WALLET_BALANCE, { chainId, resourceType, decimals, }); } async getSession() { var _a; return { sessionId: ((_a = this.session) === null || _a === void 0 ? void 0 : _a.sessionId) || null }; } async disconnect() { if (this.session) { this.transport.sendMessage('DISCONNECT', { sessionId: this.session.sessionId, }); } this.clearSession(); } } exports.RibbitWalletSDK = RibbitWalletSDK;