|
| 1 | +/** |
| 2 | + * Hana wallet adapter. |
| 3 | + * |
| 4 | + * Hana is a Stellar-compatible extension wallet. |
| 5 | + * Wraps the SWK instance provided by the consumer. |
| 6 | + */ |
| 7 | + |
| 8 | +import { WalletType } from "../types"; |
| 9 | +import type { |
| 10 | + WalletAdapter, |
| 11 | + SignTransactionInput, |
| 12 | + SWKInstance, |
| 13 | +} from "../types"; |
| 14 | +import { ok, err, SorokitErrorCode } from "../../shared/response"; |
| 15 | +import type { SorokitResult } from "../../shared/response"; |
| 16 | +import { isBrowser } from "../../shared"; |
| 17 | +import { swkSignTransaction, describeSignFailure } from "./swkSign"; |
| 18 | + |
| 19 | +function describeHanaFailure(action: "connection" | "signing", cause: unknown): string { |
| 20 | + return describeSignFailure("Hana", action, cause); |
| 21 | +} |
| 22 | + |
| 23 | +export class HanaAdapter implements WalletAdapter { |
| 24 | + readonly walletType = WalletType.HANA; |
| 25 | + |
| 26 | + constructor(private readonly kit: SWKInstance) {} |
| 27 | + |
| 28 | + isAvailable(): boolean { |
| 29 | + return isBrowser(); |
| 30 | + } |
| 31 | + |
| 32 | + async connect(): Promise<SorokitResult<string>> { |
| 33 | + if (!this.isAvailable()) { |
| 34 | + return err( |
| 35 | + SorokitErrorCode.WALLET_BROWSER_ONLY, |
| 36 | + "Hana requires a browser environment.", |
| 37 | + ); |
| 38 | + } |
| 39 | + try { |
| 40 | + const { address } = await this.kit.getAddress(); |
| 41 | + return ok(address); |
| 42 | + } catch (cause) { |
| 43 | + return err( |
| 44 | + SorokitErrorCode.WALLET_CONNECT_FAILED, |
| 45 | + describeHanaFailure("connection", cause), |
| 46 | + cause, |
| 47 | + ); |
| 48 | + } |
| 49 | + } |
| 50 | + |
| 51 | + async disconnect(): Promise<SorokitResult<undefined>> { |
| 52 | + return ok(undefined); |
| 53 | + } |
| 54 | + |
| 55 | + async signTransaction( |
| 56 | + input: SignTransactionInput, |
| 57 | + ): Promise<SorokitResult<string>> { |
| 58 | + if (!this.isAvailable()) { |
| 59 | + return err( |
| 60 | + SorokitErrorCode.WALLET_BROWSER_ONLY, |
| 61 | + "Hana requires a browser environment.", |
| 62 | + ); |
| 63 | + } |
| 64 | + return swkSignTransaction(this.kit, "Hana", input); |
| 65 | + } |
| 66 | +} |
0 commit comments