diff --git a/package.json b/package.json index 1a1776c..3fa55ea 100644 --- a/package.json +++ b/package.json @@ -1,6 +1,6 @@ { "name": "@utxos/sdk", - "version": "0.0.77", + "version": "0.0.78", "description": "UTXOS SDK - Web3 infrastructure platform for UTXO blockchains", "main": "./dist/index.cjs", "browser": "./dist/index.js", @@ -41,7 +41,7 @@ "typescript": "^5.3.3" }, "dependencies": { - "@buildonspark/spark-sdk": "^0.4.2", + "@buildonspark/spark-sdk": "0.5.0", "@meshsdk/bitcoin": "1.9.0-beta.89", "@meshsdk/common": "1.9.0-beta.89", "@meshsdk/core-cst": "1.9.0-beta.89", diff --git a/src/chains/spark/wallet.ts b/src/chains/spark/wallet.ts index 5a3e801..854e378 100644 --- a/src/chains/spark/wallet.ts +++ b/src/chains/spark/wallet.ts @@ -1,5 +1,11 @@ import axios, { AxiosInstance } from "axios"; -import { openWindow, AddressSummary, OpenWindowResult, ApiError } from "../../"; +import { + openWindow, + AddressSummary, + OpenWindowResult, + ApiError, + ExitSpeed, +} from "../../"; export type ValidSparkNetwork = "MAINNET" | "REGTEST"; @@ -214,6 +220,50 @@ export class Web3SparkWallet { }; } + public async withdrawToBitcoin({ + exitSpeed, + amountSats, + deductFeeFromWithdrawalAmount, + withdrawalAddress, + }: { + exitSpeed: ExitSpeed; + amountSats: number; + deductFeeFromWithdrawalAmount: boolean; + withdrawalAddress: string; + }) { + const res: OpenWindowResult = await openWindow( + { + method: "spark-withdraw-to-bitcoin", + exitSpeed, + withdrawalAddress, + amountSats: String(amountSats), + deductFeeFromWithdrawalAmount: + deductFeeFromWithdrawalAmount === true ? "true" : "false", + projectId: this.projectId, + networkId: String(this.networkId), + }, + this.appUrl, + ); + + if (res.success === false) { + throw new ApiError({ + code: 3, + info: "UserDeclined - User declined the transfer.", + }); + } + + if (res.data.method !== "spark-withdraw-to-bitcoin") { + throw new ApiError({ + code: 2, + info: "Recieved the wrong response from wallet popover.", + }); + } + + return { + withdrawalId: res.data.withdrawalId, + }; + } + public async signMessage({ message }: { message: string }) { const res: OpenWindowResult = await openWindow( { diff --git a/src/types/spark/index.ts b/src/types/spark/index.ts index c8859a3..4746aa7 100644 --- a/src/types/spark/index.ts +++ b/src/types/spark/index.ts @@ -220,3 +220,5 @@ declare enum TransferType { COUNTER_SWAP = 40, UNRECOGNIZED = -1, } + +export type ExitSpeed = "FAST" | "MEDIUM" | "SLOW"; diff --git a/src/types/window/open-window-params.ts b/src/types/window/open-window-params.ts index ea89d57..2d5dce2 100644 --- a/src/types/window/open-window-params.ts +++ b/src/types/window/open-window-params.ts @@ -1,4 +1,5 @@ import { Web3AuthProvider } from "../core"; +import { ExitSpeed } from "../spark"; /** in this schema you will see string versions of undefined & boolean, this type is exclusively used for converting into URLSearchParams (where undefined & booleans don't exist) */ export type OpenWindowParams = @@ -100,6 +101,15 @@ export type OpenWindowParams = networkId: string; txId: string; } + | { + method: "spark-withdraw-to-bitcoin"; + projectId: string; + networkId: string; + exitSpeed: ExitSpeed; + amountSats: string; + deductFeeFromWithdrawalAmount: "true" | "false"; + withdrawalAddress: string; + } /** to be deprecated */ | { method: "sign-tx"; diff --git a/src/types/window/open-window-result.ts b/src/types/window/open-window-result.ts index d6b267b..e694bf3 100644 --- a/src/types/window/open-window-result.ts +++ b/src/types/window/open-window-result.ts @@ -72,6 +72,10 @@ export type OpenWindowResult = method: "spark-claim-static-deposit"; transferId: string; } + | { + method: "spark-withdraw-to-bitcoin"; + withdrawalId: string; + } /** to be deprecated */ | { method: "sign-data";