-
Notifications
You must be signed in to change notification settings - Fork 2
Expand file tree
/
Copy pathDimensions.ts
More file actions
136 lines (121 loc) · 3.79 KB
/
Copy pathDimensions.ts
File metadata and controls
136 lines (121 loc) · 3.79 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
import { WasmDimensions } from "../wasm/wasm_utxo.js";
import type { BitGoPsbt, InputScriptType, SignPath } from "./BitGoPsbt.js";
import type { CoinName } from "../coinName.js";
import { toOutputScriptWithCoin } from "../address.js";
type FromInputParams = { chain: number; signPath?: SignPath } | { scriptType: InputScriptType };
/**
* Dimensions class for estimating transaction virtual size.
*
* Tracks weight internally with min/max bounds to handle ECDSA signature variance.
* Schnorr signatures have no variance (always 64 bytes).
*
* This is a thin wrapper over the WASM implementation.
*/
export class Dimensions {
private constructor(private _wasm: WasmDimensions) {}
/**
* Create empty dimensions (zero weight)
*/
static empty(): Dimensions {
return new Dimensions(WasmDimensions.empty());
}
/**
* Create dimensions from a BitGoPsbt
*
* Parses PSBT inputs and outputs to compute weight bounds without
* requiring wallet keys. Input types are detected from BIP32 derivation
* paths stored in the PSBT.
*/
static fromPsbt(psbt: BitGoPsbt): Dimensions {
return new Dimensions(WasmDimensions.from_psbt(psbt.wasm));
}
/**
* Create dimensions for a single input
*
* @param params - Either `{ chain, signPath? }` or `{ scriptType }`
*/
static fromInput(params: FromInputParams): Dimensions {
if ("scriptType" in params) {
return new Dimensions(WasmDimensions.from_input_script_type(params.scriptType));
}
return new Dimensions(
WasmDimensions.from_input(params.chain, params.signPath?.signer, params.signPath?.cosigner),
);
}
/**
* Create dimensions for a single output from script bytes
*/
static fromOutput(script: Uint8Array): Dimensions;
/**
* Create dimensions for a single output from an address
*/
static fromOutput(address: string, network: CoinName): Dimensions;
static fromOutput(scriptOrAddress: Uint8Array | string, network?: CoinName): Dimensions {
if (typeof scriptOrAddress === "string") {
if (network === undefined) {
throw new Error("network is required when passing an address string");
}
const script = toOutputScriptWithCoin(scriptOrAddress, network);
return new Dimensions(WasmDimensions.from_output_script(script));
}
return new Dimensions(WasmDimensions.from_output_script(scriptOrAddress));
}
/**
* Combine with another Dimensions instance
*/
plus(other: Dimensions): Dimensions {
return new Dimensions(this._wasm.plus(other._wasm));
}
/**
* Multiply dimensions by a scalar
*/
times(n: number): Dimensions {
return new Dimensions(this._wasm.times(n));
}
/**
* Whether any inputs are segwit (affects overhead calculation)
*/
get hasSegwit(): boolean {
return this._wasm.has_segwit();
}
/**
* Get total weight (min or max)
* @param size - "min" or "max", defaults to "max"
*/
getWeight(size: "min" | "max" = "max"): number {
return this._wasm.get_weight(size);
}
/**
* Get virtual size (min or max)
* @param size - "min" or "max", defaults to "max"
*/
getVSize(size: "min" | "max" = "max"): number {
return this._wasm.get_vsize(size);
}
/**
* Get input weight only (min or max)
* @param size - "min" or "max", defaults to "max"
*/
getInputWeight(size: "min" | "max" = "max"): number {
return this._wasm.get_input_weight(size);
}
/**
* Get input virtual size (min or max)
* @param size - "min" or "max", defaults to "max"
*/
getInputVSize(size: "min" | "max" = "max"): number {
return this._wasm.get_input_vsize(size);
}
/**
* Get output weight
*/
getOutputWeight(): number {
return this._wasm.get_output_weight();
}
/**
* Get output virtual size
*/
getOutputVSize(): number {
return this._wasm.get_output_vsize();
}
}