refactor: remove unsupported finished prop

This commit is contained in:
fbwoolf
2021-05-26 09:49:32 -05:00
parent da5b27720d
commit bc75a29b0d
6 changed files with 44 additions and 56 deletions

View File

@@ -2,4 +2,4 @@
'@stacks/connect': major
---
This exposes the profile_url and other properties in the auth response. It also removes the deprecated 'finished' prop from AuthOptions as a breaking change. We will now only support the use of 'onFinish'.
This exposes the profile_url and other properties in the auth response. It also removes the deprecated 'finished' prop from AuthOptions and transaction options as a breaking change. We will now only support the use of 'onFinish'.

View File

@@ -1,5 +1,5 @@
import React, { useReducer, createContext } from 'react';
import { AuthOptions, FinishedData } from '@stacks/connect';
import { AuthOptions, FinishedAuthData } from '@stacks/connect';
import { UserSession } from '@stacks/auth';
enum States {
@@ -13,7 +13,7 @@ type Dispatch = (action: Action) => void;
type State = {
isOpen: boolean;
isAuthenticating: boolean;
authData?: FinishedData;
authData?: FinishedAuthData;
authOptions: AuthOptions;
userSession?: UserSession;
};
@@ -26,7 +26,7 @@ const initialState: State = {
authOptions: {
redirectTo: '',
manifestPath: '',
finished: () => null,
onFinish: () => null,
authOrigin: undefined,
sendToSignIn: false,
appDetails: {

View File

@@ -9,13 +9,13 @@ import {
openContractDeploy,
openSTXTransfer,
showBlockstackConnect,
FinishedData,
ContractCallRegularOptions,
ContractCallSponsoredOptions,
ContractDeployRegularOptions,
ContractDeploySponsoredOptions,
STXTransferRegularOptions,
STXTransferSponsoredOptions,
FinishedAuthData,
} from '@stacks/connect';
import { ConnectContext, ConnectDispatchContext, States } from '../components/connect/context';
@@ -31,7 +31,7 @@ export const useConnect = () => {
const { isOpen, isAuthenticating, authData, authOptions, userSession } = useContext(
ConnectContext
);
const finishedCallback = authOptions.onFinish || authOptions.finished;
const dispatch = useConnectDispatch();
const doUpdateAuthOptions = (payload: Partial<AuthOptions>) => {
@@ -48,9 +48,8 @@ export const useConnect = () => {
const _options: AuthOptions = {
...authOptions,
...options,
finished: undefined,
onFinish: (payload: FinishedData) => {
finishedCallback?.(payload);
onFinish: (payload: FinishedAuthData) => {
authOptions.onFinish?.(payload);
},
sendToSignIn: true,
};
@@ -64,13 +63,13 @@ export const useConnect = () => {
}
authOptions && doUpdateAuthOptions(authOptions);
};
const doAuth = (options: Partial<AuthOptions> = {}) => {
void authenticate({
...authOptions,
...options,
finished: undefined,
onFinish: (payload: FinishedData) => {
finishedCallback?.(payload);
onFinish: (payload: FinishedAuthData) => {
authOptions.onFinish?.(payload);
},
});
};

View File

@@ -72,23 +72,21 @@ export const authenticate = async (authOptions: AuthOptions) => {
}
);
await provider
.authenticationRequest(authRequest)
.then(async authResponse => {
await userSession.handlePendingSignIn(authResponse);
const token = decodeToken(authResponse);
const payload = token?.payload;
const authResponsePayload = (payload as unknown) as AuthResponsePayload;
onFinish?.({
authResponse,
authResponsePayload,
userSession,
});
})
.catch(error => {
console.error('[Connect] Error during auth request', error);
onCancel?.();
try {
const authResponse = await provider.authenticationRequest(authRequest);
await userSession.handlePendingSignIn(authResponse);
const token = decodeToken(authResponse);
const payload = token?.payload;
const authResponsePayload = (payload as unknown) as AuthResponsePayload;
onFinish?.({
authResponse,
authResponsePayload,
userSession,
});
} catch (error) {
console.error('[Connect] Error during auth request', error);
onCancel?.();
}
};
export const getUserData = async (userSession?: UserSession) => {

View File

@@ -91,38 +91,33 @@ const signPayload = async (payload: TransactionPayload, privateKey: string) => {
} as any);
};
const openTransactionPopup = ({ token, options }: TransactionPopup) => {
const openTransactionPopup = async ({ token, options }: TransactionPopup) => {
const provider = getStacksProvider();
if (!provider) {
throw new Error('Stacks Wallet not installed.');
}
void provider
.transactionRequest(token)
.then(data => {
const finishedCallback = options.finished || options.onFinish;
const { txRaw } = data;
const txBuffer = Buffer.from(txRaw.replace(/^0x/, ''), 'hex');
const stacksTransaction = deserializeTransaction(new BufferReader(txBuffer));
if ('sponsored' in options && options.sponsored) {
const finishedCallback = options.onFinish || options.finished;
finishedCallback?.({
...(data as SponsoredFinishedTxPayload),
stacksTransaction,
});
return;
}
finishedCallback?.({
...(data as FinishedTxPayload),
try {
const txResponse = await provider.transactionRequest(token);
const { txRaw } = txResponse;
const txBuffer = Buffer.from(txRaw.replace(/^0x/, ''), 'hex');
const stacksTransaction = deserializeTransaction(new BufferReader(txBuffer));
if ('sponsored' in options && options.sponsored) {
options.onFinish?.({
...(txResponse as SponsoredFinishedTxPayload),
stacksTransaction,
});
})
.catch(error => {
console.error('[Connect] Error during transaction request', error);
options.onCancel?.();
return;
}
options.onFinish?.({
...(txResponse as FinishedTxPayload),
stacksTransaction,
});
if (true) return;
} catch (error) {
console.error('[Connect] Error during transaction request', error);
options.onCancel?.();
}
};
export const makeContractCallToken = async (options: ContractCallOptions) => {

View File

@@ -84,16 +84,12 @@ export type Canceled = () => void;
export interface SponsoredOptionsBase extends TxBase, OptionsBase {
sponsored: true;
/** @deprecated use `onFinish` */
finished?: SponsoredFinished;
onFinish?: SponsoredFinished;
onCancel?: Canceled;
}
export interface RegularOptionsBase extends TxBase, OptionsBase {
sponsored?: false;
/** @deprecated use `onFinish` */
finished?: Finished;
onFinish?: Finished;
onCancel?: Canceled;
}