-
Notifications
You must be signed in to change notification settings - Fork 302
Expand file tree
/
Copy pathabstractUtxoCoinUtil.ts
More file actions
58 lines (52 loc) · 2.16 KB
/
abstractUtxoCoinUtil.ts
File metadata and controls
58 lines (52 loc) · 2.16 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
import assert from 'assert';
import { coins, UtxoCoin } from '@bitgo/statics';
import * as utxolib from '@bitgo/utxo-lib';
import ScriptType2Of3 = utxolib.bitgo.outputScripts.ScriptType2Of3;
import { WalletType } from '../wallet';
/** @deprecated - will be removed when we drop support for utxolib */
export function inferAddressType(addressDetails: { chain: number }): ScriptType2Of3 | null {
return utxolib.bitgo.isChainCode(addressDetails.chain)
? utxolib.bitgo.scriptTypeForChain(addressDetails.chain)
: null;
}
/**
* Get the supported 2 of 3 script types for a given utxo coin
*/
export function getUtxoCoinScriptTypes2Of3(coinName: string): utxolib.bitgo.outputScripts.ScriptType2Of3[] {
const coin = coins.get(coinName);
assert(coin instanceof UtxoCoin, `coin ${coinName} is not a utxo coin`);
const network = utxolib.networks[coin.network.utxolibName as utxolib.NetworkName];
return utxolib.bitgo.outputScripts.scriptTypes2Of3.filter((v) =>
utxolib.bitgo.outputScripts.isSupportedScriptType(network, v)
);
}
/**
* Check if script type is enabled for a given walletType and network
* @param network
* @param walletType
* @param scriptType
*/
function isEnabledAddressType(network: utxolib.Network, walletType: WalletType, scriptType: ScriptType2Of3): boolean {
if (!utxolib.bitgo.outputScripts.isSupportedScriptType(network, scriptType)) {
return false;
}
if (scriptType === 'p2trMusig2') {
return walletType === 'hot' || (walletType === 'cold' && utxolib.isTestnet(network));
}
return true;
}
/**
* Get the supported 2 of 3 script types for a given utxo coin and wallet type
* @param coinName
* @param walletType
*/
export function getUtxoCoinScriptTypesForWalletType(
coinName: string,
walletType: WalletType
): utxolib.bitgo.outputScripts.ScriptType2Of3[] {
const scriptTypes = getUtxoCoinScriptTypes2Of3(coinName);
const coin = coins.get(coinName);
assert(coin instanceof UtxoCoin, `coin ${coinName} is not a utxo coin`);
const network = utxolib.networks[coin.network.utxolibName as utxolib.NetworkName];
return scriptTypes.filter((scriptType) => isEnabledAddressType(network, walletType, scriptType as ScriptType2Of3));
}