|
| 1 | +import { type ModuleInterface, ModuleType } from "../../types/mod.ts"; |
| 2 | +import { parseError } from "../utils.ts"; |
| 3 | + |
| 4 | +declare const window: { |
| 5 | + bitkeep?: any; |
| 6 | +}; |
| 7 | + |
| 8 | +export const BITGET_WALLET_ID = "BitgetWallet"; |
| 9 | + |
| 10 | +export class BitgetModule implements ModuleInterface { |
| 11 | + moduleType: ModuleType = ModuleType.HOT_WALLET; |
| 12 | + productId: string = BITGET_WALLET_ID; |
| 13 | + productName: string = "Bitget Wallet"; |
| 14 | + productUrl: string = "https://web3.bitget.com"; |
| 15 | + productIcon: string = "https://stellar.creit.tech/wallet-icons/bitget.png"; |
| 16 | + provider: any; |
| 17 | + |
| 18 | + constructor() { |
| 19 | + this.provider = window.bitkeep?.stellar; |
| 20 | + } |
| 21 | + |
| 22 | + async runChecks(): Promise<void> { |
| 23 | + if (!(await this.isAvailable())) { |
| 24 | + throw new Error(`${this.productName} is not connected`); |
| 25 | + } |
| 26 | + } |
| 27 | + |
| 28 | + async isAvailable() { |
| 29 | + return !!this.provider; |
| 30 | + } |
| 31 | + async getAddress() { |
| 32 | + try { |
| 33 | + await this.runChecks(); |
| 34 | + const address = await this.provider.connect(); |
| 35 | + return { address }; |
| 36 | + } catch (e) { |
| 37 | + throw parseError(e); |
| 38 | + } |
| 39 | + } |
| 40 | + async signMessage( |
| 41 | + message: string, |
| 42 | + opts?: { |
| 43 | + networkPassphrase?: string; |
| 44 | + address?: string; |
| 45 | + path?: string; |
| 46 | + }, |
| 47 | + ) { |
| 48 | + try { |
| 49 | + await this.runChecks(); |
| 50 | + const signatureHex = await this.provider.signMessage(message, opts?.address); |
| 51 | + return { signedMessage: signatureHex }; |
| 52 | + } catch (e) { |
| 53 | + throw parseError(e); |
| 54 | + } |
| 55 | + } |
| 56 | + |
| 57 | + async signTransaction( |
| 58 | + xdr: string, |
| 59 | + opts?: { |
| 60 | + networkPassphrase?: string; |
| 61 | + address?: string; |
| 62 | + path?: string; |
| 63 | + submit?: boolean; |
| 64 | + submitUrl?: string; |
| 65 | + }, |
| 66 | + ) { |
| 67 | + try { |
| 68 | + await this.runChecks(); |
| 69 | + const signedTxXdr = await this.provider.signTransaction(xdr, opts); |
| 70 | + return { signedTxXdr }; |
| 71 | + } catch (e) { |
| 72 | + throw parseError(e); |
| 73 | + } |
| 74 | + } |
| 75 | + |
| 76 | + async signAuthEntry( |
| 77 | + authEntry: string, |
| 78 | + opts?: { |
| 79 | + networkPassphrase?: string; |
| 80 | + address?: string; |
| 81 | + path?: string; |
| 82 | + }, |
| 83 | + ): Promise<{ |
| 84 | + signedAuthEntry: string; |
| 85 | + signerAddress?: string; |
| 86 | + }> { |
| 87 | + throw { |
| 88 | + code: -3, |
| 89 | + message: `${this.productName} does not support the "signAuthEntry" function`, |
| 90 | + }; |
| 91 | + } |
| 92 | + |
| 93 | + async getNetwork() { |
| 94 | + try { |
| 95 | + await this.runChecks(); |
| 96 | + return this.provider.network(); |
| 97 | + } catch (e) { |
| 98 | + throw parseError(e); |
| 99 | + } |
| 100 | + } |
| 101 | +} |
0 commit comments