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

fix: OKX signature tweak signer #364

Open
wants to merge 4 commits into
base: v0.3.x
Choose a base branch
from
Open
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
4 changes: 2 additions & 2 deletions package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "simple-staking",
"version": "0.3.10",
"version": "0.3.11",
"private": true,
"scripts": {
"dev": "next dev",
Expand Down
27 changes: 24 additions & 3 deletions src/app/common/utils/psbt.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,14 +4,35 @@ import { WalletProvider } from "@/utils/wallet/wallet_provider";

const SIGN_PSBT_NOT_COMPATIBLE_WALLETS = ["OneKey"];

export type SignPsbtTransaction = (psbtHex: string) => Promise<Transaction>;
// wallets requiring to disable the tweakSigner option
const SIGN_PSBT_DISABLE_TWEAK_SIGNER = ["OKX"];

export type signPsbtMode = "staking" | "unbonding" | "withdrawal";

export type SignPsbtTransaction = (
psbtHex: string,
signPsbtMode: signPsbtMode,
) => Promise<Transaction>;

// This method is created to accommodate backward compatibility with the
// old implementation of signPsbt where the wallet.signPsbt method returns
// the signed transaction in hex
export const signPsbtTransaction = (wallet: WalletProvider) => {
return async (psbtHex: string) => {
const signedHex = await wallet.signPsbt(psbtHex);
return async (psbtHex: string, mode: signPsbtMode) => {
let shouldDisableTweakSigner = false;

if (
(mode === "unbonding" || mode === "withdrawal") &&
SIGN_PSBT_DISABLE_TWEAK_SIGNER.includes(
await wallet.getWalletProviderName(),
)
) {
shouldDisableTweakSigner = true;
}

const signedHex = await wallet.signPsbt(psbtHex, {
disableTweakSigner: shouldDisableTweakSigner,
});
const providerName = await wallet.getWalletProviderName();
if (SIGN_PSBT_NOT_COMPATIBLE_WALLETS.includes(providerName)) {
try {
Expand Down
1 change: 1 addition & 0 deletions src/utils/delegations/signStakingTx.ts
Original file line number Diff line number Diff line change
Expand Up @@ -124,6 +124,7 @@ export const signStakingTx = async (
try {
stakingTx = await signPsbtTransaction(btcWallet)(
unsignedStakingPsbt.toHex(),
"staking",
);
} catch (error: Error | any) {
throw new Error(error?.message || "Staking transaction signing PSBT error");
Expand Down
2 changes: 1 addition & 1 deletion src/utils/delegations/signUnbondingTx.ts
Original file line number Diff line number Diff line change
Expand Up @@ -82,7 +82,7 @@ export const signUnbondingTx = async (
// Sign the unbonding transaction
let unbondingTx: Transaction;
try {
unbondingTx = await signPsbtTx(unsignedUnbondingTx.toHex());
unbondingTx = await signPsbtTx(unsignedUnbondingTx.toHex(), "unbonding");
} catch (error) {
throw new Error("Failed to sign PSBT for the unbonding transaction");
}
Expand Down
2 changes: 1 addition & 1 deletion src/utils/delegations/signWithdrawalTx.ts
Original file line number Diff line number Diff line change
Expand Up @@ -112,7 +112,7 @@ export const signWithdrawalTx = async (
let withdrawalTx: Transaction;
try {
const { psbt } = withdrawPsbtTxResult;
withdrawalTx = await signPsbtTx(psbt.toHex());
withdrawalTx = await signPsbtTx(psbt.toHex(), "withdrawal");
} catch (error) {
throw new Error("Failed to sign PSBT for the withdrawal transaction");
}
Expand Down
30 changes: 27 additions & 3 deletions src/utils/wallet/providers/okx_wallet.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ import {
network,
validateAddress,
} from "@/config/network.config";
import { isTaproot } from "@/utils/wallet";

import {
getAddressBalance,
Expand All @@ -15,6 +16,7 @@ import {
Fees,
InscriptionIdentifier,
Network,
SignPsbtOptions,
UTXO,
WalletInfo,
WalletProvider,
Expand Down Expand Up @@ -107,15 +109,37 @@ export class OKXWallet extends WalletProvider {
return this.okxWalletInfo.publicKeyHex;
};

signPsbt = async (psbtHex: string): Promise<string> => {
signPsbt = async (
psbtHex: string,
options?: SignPsbtOptions,
): Promise<string> => {
if (!this.okxWalletInfo) {
throw new Error("OKX Wallet not connected");
}

const shouldDisableTweakSigner = options?.disableTweakSigner || false;

// Use signPsbt since it shows the fees
return await this.bitcoinNetworkProvider.signPsbt(psbtHex);
return await this.bitcoinNetworkProvider.signPsbt(
psbtHex,
// Required on 3.29.26 version
shouldDisableTweakSigner &&
isTaproot(this.okxWalletInfo.address) && {
toSignInputs: [
{
index: 0,
address: this.okxWalletInfo.address,
disableTweakSigner: true,
},
],
},
);
};

signPsbts = async (psbtsHexes: string[]): Promise<string[]> => {
signPsbts = async (
psbtsHexes: string[],
_options?: SignPsbtOptions,
): Promise<string[]> => {
if (!this.okxWalletInfo) {
throw new Error("OKX Wallet not connected");
}
Expand Down
14 changes: 12 additions & 2 deletions src/utils/wallet/wallet_provider.ts
Original file line number Diff line number Diff line change
Expand Up @@ -42,6 +42,10 @@ export type WalletInfo = {
address: string;
};

export type SignPsbtOptions = {
disableTweakSigner?: boolean;
};

/**
* Abstract class representing a wallet provider.
* Provides methods for connecting to a wallet, retrieving wallet information, signing transactions, and more.
Expand Down Expand Up @@ -79,14 +83,20 @@ export abstract class WalletProvider {
* @param psbtHex - The hex string of the unsigned PSBT to sign.
* @returns A promise that resolves to the hex string of the signed PSBT.
*/
abstract signPsbt(psbtHex: string): Promise<string>;
abstract signPsbt(
psbtHex: string,
options?: SignPsbtOptions,
): Promise<string>;

/**
* Signs multiple PSBTs in hex format.
* @param psbtsHexes - The hex strings of the unsigned PSBTs to sign.
* @returns A promise that resolves to an array of hex strings, each representing a signed PSBT.
*/
abstract signPsbts(psbtsHexes: string[]): Promise<string[]>;
abstract signPsbts(
psbtsHexes: string[],
options?: SignPsbtOptions,
): Promise<string[]>;

/**
* Gets the network of the current account.
Expand Down