Skip to content

Commit

Permalink
Merge pull request #5441 from BitGo/BTC-1801.fix-presign-for-descript…
Browse files Browse the repository at this point in the history
…or-wallets

fix(abstract-utxo): presign for descriptor wallets
  • Loading branch information
OttoAllmendinger authored Jan 29, 2025
2 parents 2195803 + 2702580 commit c259e2d
Show file tree
Hide file tree
Showing 3 changed files with 43 additions and 2 deletions.
6 changes: 5 additions & 1 deletion modules/abstract-utxo/src/abstractUtxoCoin.ts
Original file line number Diff line number Diff line change
Expand Up @@ -74,7 +74,8 @@ import { toBip32Triple, UtxoKeychain, UtxoNamedKeychains } from './keychains';
import { verifyKeySignature, verifyUserPublicKey } from './verifyKey';
import { getPolicyForEnv } from './descriptor/validatePolicy';
import { signTransaction } from './transaction/signTransaction';
import { UtxoWallet } from './wallet';
import { isUtxoWalletData, UtxoWallet } from './wallet';
import { isDescriptorWalletData } from './descriptor/descriptorWallet';

import ScriptType2Of3 = utxolib.bitgo.outputScripts.ScriptType2Of3;

Expand Down Expand Up @@ -1094,6 +1095,9 @@ export abstract class AbstractUtxoCoin extends BaseCoin {
}

async presignTransaction(params: PresignTransactionOptions): Promise<any> {
if (params.walletData && isUtxoWalletData(params.walletData) && isDescriptorWalletData(params.walletData)) {
return params;
}
// In the case that we have a 'psbt-lite' transaction format, we want to indicate in signing to not fail
const txHex = (params.txHex ?? params.txPrebuild?.txHex) as string;
if (
Expand Down
32 changes: 31 additions & 1 deletion modules/abstract-utxo/src/names.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,35 @@
import * as utxolib from '@bitgo/utxo-lib';

export const utxoCoinsMainnet = ['btc', 'bch', 'bcha', 'bsv', 'btg', 'dash', 'doge', 'ltc', 'zec'] as const;
export const utxoCoinsTestnet = [
'tbtc',
'tbch',
'tbsv',
'tdash',
'tdoge',
'tltc',
'tzec',
'tbtcsig',
'tbtc4',
'tbtcbgsig',
] as const;

export type UtxoCoinNameMainnet = (typeof utxoCoinsMainnet)[number];
export type UtxoCoinNameTestnet = (typeof utxoCoinsTestnet)[number];
export type UtxoCoinName = UtxoCoinNameMainnet | UtxoCoinNameTestnet;

export function isUtxoCoinNameMainnet(coinName: string): coinName is UtxoCoinNameMainnet {
return utxoCoinsMainnet.includes(coinName as UtxoCoinNameMainnet);
}

export function isUtxoCoinNameTestnet(coinName: string): coinName is UtxoCoinNameTestnet {
return utxoCoinsTestnet.includes(coinName as UtxoCoinNameTestnet);
}

export function isUtxoCoinName(coinName: string): coinName is UtxoCoinName {
return isUtxoCoinNameMainnet(coinName) || isUtxoCoinNameTestnet(coinName);
}

function getNetworkName(n: utxolib.Network): utxolib.NetworkName {
const name = utxolib.getNetworkName(n);
if (!name) {
Expand All @@ -12,7 +42,7 @@ function getNetworkName(n: utxolib.Network): utxolib.NetworkName {
* @param n
* @returns the family name for a network. Testnets and mainnets of the same coin share the same family name.
*/
export function getFamilyFromNetwork(n: utxolib.Network): string {
export function getFamilyFromNetwork(n: utxolib.Network): UtxoCoinNameMainnet {
switch (getNetworkName(n)) {
case 'bitcoin':
case 'testnet':
Expand Down
7 changes: 7 additions & 0 deletions modules/abstract-utxo/src/wallet.ts
Original file line number Diff line number Diff line change
@@ -1,14 +1,21 @@
import { Wallet, WalletData } from '@bitgo/sdk-core';

import { isUtxoCoinName, UtxoCoinName } from './names';

// parseTransactions' return type makes use of WalletData's type but with customChangeKeySignatures as required.
export interface UtxoWalletData extends WalletData {
coin: UtxoCoinName;
customChangeKeySignatures: {
user: string;
backup: string;
bitgo: string;
};
}

export function isUtxoWalletData(obj: WalletData): obj is UtxoWalletData {
return isUtxoCoinName(obj.coin);
}

export interface UtxoWallet extends Wallet {
_wallet: UtxoWalletData;
}

0 comments on commit c259e2d

Please sign in to comment.