Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat: add open btc transfer #285

Closed
wants to merge 4 commits into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
5 changes: 5 additions & 0 deletions .changeset/violet-masks-hunt.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
---
'@stacks/connect': minor
---

Add experimental support for BTC transfers
2 changes: 1 addition & 1 deletion lerna.json
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@
"packages": [
"packages/*"
],
"version": "5.0.5",
"version": "independent",
"npmClient": "yarn",
"command": {
"version": {
Expand Down
1 change: 1 addition & 0 deletions packages/connect/src/index.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
export * from './auth';
export * from './transactions';
export * from './transactions/btc'; // todo: tmp
export * from './signature';
export * from './signature/structuredData';
export * from './profile';
Expand Down
55 changes: 55 additions & 0 deletions packages/connect/src/transactions/btc.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,55 @@
import { createUnsecuredToken, Json } from 'jsontokens';
import { BtcRecipient, BTCTransferOptions, BTCTransferPayload, TransactionTypes } from '../types';
import { getStacksProvider } from '../utils';

const openGenericTransactionPopup = async ({
token,
options,
}: {
token: string;
options: BTCTransferOptions; // todo: add generic alternative
}) => {
const provider = getStacksProvider();
if (!provider) {
throw new Error('Hiro Wallet not installed');
}

try {
const txResponse = await provider.transactionRequest(token);

options.onFinish?.(txResponse as any); // todo: add new `onFinish` for parsed BTC tx data
} catch (error) {
console.error('[Connect] Error during transaction request', error);
options.onCancel?.();
}
};

/**
* ⚠ Experimental method for opening a BTC transfer request. This API interface
* subject to change and does not adhere to semantic versioning.
* @experimental
*/
export function openBTCTransfer(options: BTCTransferOptions): Promise<void> {
const { recipients, ..._options } = {
// todo: do we maybe want userSession, stxAddress, ...?
// ...getDefaults(options),
...options,
};

const payLoadRecipients: BtcRecipient[] = [];
recipients.forEach(recipient => {
payLoadRecipients.push({
recipient: recipient.recipient,
amount: recipient.amount.toString(10),
});
});

const payload: Partial<BTCTransferPayload> = {
..._options,
recipients: payLoadRecipients,
txType: TransactionTypes.BTCTransfer,
};

const token = createUnsecuredToken(payload as Json);
return openGenericTransactionPopup({ token, options });
}
3 changes: 2 additions & 1 deletion packages/connect/src/transactions/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -74,7 +74,8 @@ export function getStxAddress(options: TransactionOptions) {
return address;
}

function getDefaults(options: TransactionOptions) {
/** @internal */
export function getDefaults(options: TransactionOptions) {
const network = options.network || new StacksTestnet();

const userSession = getUserSession(options.userSession);
Expand Down
31 changes: 29 additions & 2 deletions packages/connect/src/types/transactions.ts
Original file line number Diff line number Diff line change
Expand Up @@ -48,6 +48,7 @@ export enum TransactionTypes {
ContractCall = 'contract_call',
ContractDeploy = 'smart_contract',
STXTransfer = 'token_transfer',
BTCTransfer = 'token_transfer_btc', // todo: `btc_transfer` ? (generice interface for selecting token/chain)
}

/**
Expand Down Expand Up @@ -153,10 +154,36 @@ export interface STXTransferPayload extends STXTransferBase {
* Transaction Popup
*/

export type TransactionOptions = ContractCallOptions | ContractDeployOptions | STXTransferOptions;
export type TransactionPayload = ContractCallPayload | ContractDeployPayload | STXTransferPayload;
export type TransactionOptions =
| ContractCallOptions
| ContractDeployOptions
| STXTransferOptions
| BTCTransferOptions;
export type TransactionPayload =
| ContractCallPayload
| ContractDeployPayload
| STXTransferPayload
| BTCTransferPayload;

export interface TransactionPopup {
token: string;
options: TransactionOptions;
}

/**
* BTC Transfer
*/

export interface BtcRecipient {
recipient: string;
amount: bigint | number | string;
}

export interface BTCTransferOptions extends RegularOptionsBase {
recipients: BtcRecipient[];
}

export interface BTCTransferPayload extends BTCTransferOptions {
recipients: BtcRecipient[];
txType: TransactionTypes.BTCTransfer;
}