-
Notifications
You must be signed in to change notification settings - Fork 302
Expand file tree
/
Copy pathfixedScript.ts
More file actions
176 lines (154 loc) · 5.2 KB
/
fixedScript.ts
File metadata and controls
176 lines (154 loc) · 5.2 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
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
import _ from 'lodash';
import {
AddressTypeChainMismatchError,
CreateAddressFormat,
InvalidAddressDerivationPropertyError,
UnexpectedAddressError,
P2shP2wshUnsupportedError,
P2trMusig2UnsupportedError,
P2trUnsupportedError,
P2wshUnsupportedError,
UnsupportedAddressTypeError,
isTriple,
Triple,
} from '@bitgo/sdk-core';
import { fixedScriptWallet } from '@bitgo/wasm-utxo';
import { UtxoCoinName } from '../names.js';
type ScriptType2Of3 = fixedScriptWallet.OutputScriptType;
type ChainCode = fixedScriptWallet.ChainCode;
export interface FixedScriptAddressCoinSpecific {
outputScript?: string;
}
export interface GenerateAddressOptions {
chain?: number;
index?: number;
segwit?: boolean;
bech32?: boolean;
}
interface GenerateFixedScriptAddressOptions extends GenerateAddressOptions {
format?: CreateAddressFormat;
keychains: { pub: string }[];
}
function supportsAddressType(coinName: UtxoCoinName, addressType: ScriptType2Of3): boolean {
return fixedScriptWallet.supportsScriptType(coinName, addressType);
}
/**
* Normalize script type aliases. "p2tr" is an alias for "p2trLegacy".
*/
function normalizeScriptType(scriptType: ScriptType2Of3 | 'p2tr'): ScriptType2Of3 {
return scriptType === 'p2tr' ? 'p2trLegacy' : scriptType;
}
export function generateAddressWithChainAndIndex(
coinName: UtxoCoinName,
keychains: fixedScriptWallet.WalletKeysArg | Triple<string>,
chain: ChainCode,
index: number,
format: CreateAddressFormat | undefined
): string {
// Convert CreateAddressFormat to AddressFormat for wasm-utxo
// 'base58' -> 'default', 'cashaddr' -> 'cashaddr'
const wasmFormat = format === 'base58' ? 'default' : format;
return fixedScriptWallet.address(keychains, chain, index, coinName, wasmFormat);
}
/**
* Generate an address for a wallet based on a set of configurations
* @param params.keychains {[object]} Array of objects with xpubs
* @param params.threshold {number} Minimum number of signatures
* @param params.chain {number} Derivation chain (see https://github.com/BitGo/unspents/blob/master/src/codes.ts for
* the corresponding address type of a given chain code)
* @param params.index {number} Derivation index
* @param params.segwit {boolean} Deprecated
* @param params.bech32 {boolean} Deprecated
* @returns {string} The generated address
*/
export function generateAddress(coinName: UtxoCoinName, params: GenerateFixedScriptAddressOptions): string {
let derivationIndex = 0;
if (_.isInteger(params.index) && (params.index as number) > 0) {
derivationIndex = params.index as number;
}
const { keychains, chain, segwit = false, bech32 = false } = params as GenerateFixedScriptAddressOptions;
let derivationChain: ChainCode = fixedScriptWallet.ChainCode.value('p2sh', 'external');
if (_.isNumber(chain) && _.isInteger(chain) && fixedScriptWallet.ChainCode.is(chain)) {
derivationChain = chain;
}
function convertFlagsToAddressType(): ScriptType2Of3 {
if (fixedScriptWallet.ChainCode.is(chain)) {
return fixedScriptWallet.ChainCode.scriptType(chain);
}
if (_.isBoolean(segwit) && segwit) {
return 'p2shP2wsh';
} else if (_.isBoolean(bech32) && bech32) {
return 'p2wsh';
} else {
return 'p2sh';
}
}
const addressType = normalizeScriptType(convertFlagsToAddressType());
if (addressType !== fixedScriptWallet.ChainCode.scriptType(derivationChain)) {
throw new AddressTypeChainMismatchError(addressType, derivationChain);
}
if (!supportsAddressType(coinName, addressType)) {
switch (addressType) {
case 'p2sh':
throw new Error(`internal error: p2sh should always be supported`);
case 'p2shP2wsh':
throw new P2shP2wshUnsupportedError();
case 'p2wsh':
throw new P2wshUnsupportedError();
case 'p2tr':
case 'p2trLegacy':
throw new P2trUnsupportedError();
case 'p2trMusig2':
throw new P2trMusig2UnsupportedError();
default:
throw new UnsupportedAddressTypeError();
}
}
if (!isTriple(keychains)) {
throw new Error('keychains must be a triple');
}
return generateAddressWithChainAndIndex(
coinName,
keychains.map((k) => k.pub) as Triple<string>,
derivationChain,
derivationIndex,
params.format
);
}
type Keychain = {
pub: string;
};
export function assertFixedScriptWalletAddress(
coinName: UtxoCoinName,
{
chain,
index,
keychains,
format,
address,
}: {
chain: number | undefined;
index: number;
keychains: Keychain[];
format: CreateAddressFormat;
address: string;
}
): void {
if ((_.isUndefined(chain) && _.isUndefined(index)) || !(_.isFinite(chain) && _.isFinite(index))) {
throw new InvalidAddressDerivationPropertyError(
`address validation failure: invalid chain (${chain}) or index (${index})`
);
}
if (!keychains) {
throw new Error('missing required param keychains');
}
const expectedAddress = generateAddress(coinName, {
format,
keychains,
chain,
index,
});
if (expectedAddress !== address) {
throw new UnexpectedAddressError(`address validation failure: expected ${expectedAddress} but got ${address}`);
}
}