@@ -12,17 +12,15 @@ import {
1212 isTriple ,
1313 Triple ,
1414} from '@bitgo/sdk-core' ;
15- import { bitgo } from '@bitgo/utxo-lib' ;
16- import * as wasmUtxo from '@bitgo/wasm-utxo' ;
15+ import { fixedScriptWallet } from '@bitgo/wasm-utxo' ;
1716
18- import { getNetworkFromCoinName , UtxoCoinName } from '../names' ;
17+ import { UtxoCoinName } from '../names' ;
1918
20- type ScriptType2Of3 = bitgo . outputScripts . ScriptType2Of3 ;
19+ type ScriptType2Of3 = fixedScriptWallet . OutputScriptType ;
20+ type ChainCode = fixedScriptWallet . ChainCode ;
2121
2222export interface FixedScriptAddressCoinSpecific {
2323 outputScript ?: string ;
24- redeemScript ?: string ;
25- witnessScript ?: string ;
2624}
2725
2826export interface GenerateAddressOptions {
@@ -39,22 +37,27 @@ interface GenerateFixedScriptAddressOptions extends GenerateAddressOptions {
3937}
4038
4139function supportsAddressType ( coinName : UtxoCoinName , addressType : ScriptType2Of3 ) : boolean {
42- const network = getNetworkFromCoinName ( coinName ) ;
43- return bitgo . outputScripts . isSupportedScriptType ( network , addressType ) ;
40+ return fixedScriptWallet . supportsScriptType ( coinName , addressType ) ;
41+ }
42+
43+ /**
44+ * Normalize script type aliases. "p2tr" is an alias for "p2trLegacy".
45+ */
46+ function normalizeScriptType ( scriptType : ScriptType2Of3 | 'p2tr' ) : ScriptType2Of3 {
47+ return scriptType === 'p2tr' ? 'p2trLegacy' : scriptType ;
4448}
4549
4650export function generateAddressWithChainAndIndex (
4751 coinName : UtxoCoinName ,
48- keychains : bitgo . RootWalletKeys | Triple < string > ,
49- chain : bitgo . ChainCode ,
52+ keychains : fixedScriptWallet . WalletKeysArg | Triple < string > ,
53+ chain : ChainCode ,
5054 index : number ,
5155 format : CreateAddressFormat | undefined
5256) : string {
5357 // Convert CreateAddressFormat to AddressFormat for wasm-utxo
5458 // 'base58' -> 'default', 'cashaddr' -> 'cashaddr'
5559 const wasmFormat = format === 'base58' ? 'default' : format ;
56- const network = getNetworkFromCoinName ( coinName ) ;
57- return wasmUtxo . fixedScriptWallet . address ( keychains , chain , index , network , wasmFormat ) ;
60+ return fixedScriptWallet . address ( keychains , chain , index , coinName , wasmFormat ) ;
5861}
5962
6063/**
@@ -77,14 +80,14 @@ export function generateAddress(coinName: UtxoCoinName, params: GenerateFixedScr
7780
7881 const { keychains, chain, segwit = false , bech32 = false } = params as GenerateFixedScriptAddressOptions ;
7982
80- let derivationChain = bitgo . getExternalChainCode ( 'p2sh' ) ;
81- if ( _ . isNumber ( chain ) && _ . isInteger ( chain ) && bitgo . isChainCode ( chain ) ) {
83+ let derivationChain : ChainCode = fixedScriptWallet . ChainCode . value ( 'p2sh' , 'external ') ;
84+ if ( _ . isNumber ( chain ) && _ . isInteger ( chain ) && fixedScriptWallet . ChainCode . is ( chain ) ) {
8285 derivationChain = chain ;
8386 }
8487
8588 function convertFlagsToAddressType ( ) : ScriptType2Of3 {
86- if ( bitgo . isChainCode ( chain ) ) {
87- return bitgo . scriptTypeForChain ( chain ) ;
89+ if ( fixedScriptWallet . ChainCode . is ( chain ) ) {
90+ return fixedScriptWallet . ChainCode . scriptType ( chain ) ;
8891 }
8992 if ( _ . isBoolean ( segwit ) && segwit ) {
9093 return 'p2shP2wsh' ;
@@ -95,9 +98,9 @@ export function generateAddress(coinName: UtxoCoinName, params: GenerateFixedScr
9598 }
9699 }
97100
98- const addressType = params . addressType || convertFlagsToAddressType ( ) ;
101+ const addressType = normalizeScriptType ( params . addressType || convertFlagsToAddressType ( ) ) ;
99102
100- if ( addressType !== bitgo . scriptTypeForChain ( derivationChain ) ) {
103+ if ( addressType !== fixedScriptWallet . ChainCode . scriptType ( derivationChain ) ) {
101104 throw new AddressTypeChainMismatchError ( addressType , derivationChain ) ;
102105 }
103106
@@ -110,6 +113,7 @@ export function generateAddress(coinName: UtxoCoinName, params: GenerateFixedScr
110113 case 'p2wsh' :
111114 throw new P2wshUnsupportedError ( ) ;
112115 case 'p2tr' :
116+ case 'p2trLegacy' :
113117 throw new P2trUnsupportedError ( ) ;
114118 case 'p2trMusig2' :
115119 throw new P2trMusig2UnsupportedError ( ) ;
0 commit comments