@bitgo-beta/unspents 0.13.2-beta.67 → 0.13.2-beta.671

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
Files changed (65) hide show
  1. package/.mocharc.js +1 -1
  2. package/CHANGELOG.md +483 -0
  3. package/bin/generate_tables.ts +94 -0
  4. package/dist/bin/generate_tables.d.ts +2 -0
  5. package/dist/bin/generate_tables.d.ts.map +1 -0
  6. package/dist/bin/generate_tables.js +107 -0
  7. package/dist/{codes.d.ts → src/codes.d.ts} +11 -10
  8. package/dist/src/codes.d.ts.map +1 -0
  9. package/dist/src/codes.js +182 -0
  10. package/dist/{dimensions.d.ts → src/dimensions.d.ts} +5 -4
  11. package/dist/src/dimensions.d.ts.map +1 -0
  12. package/dist/src/dimensions.js +500 -0
  13. package/dist/{index.d.ts → src/index.d.ts} +1 -0
  14. package/dist/src/index.d.ts.map +1 -0
  15. package/dist/src/index.js +44 -0
  16. package/dist/{inputWeights.d.ts → src/inputWeights.d.ts} +4 -3
  17. package/dist/src/inputWeights.d.ts.map +1 -0
  18. package/dist/src/inputWeights.js +97 -0
  19. package/dist/{scriptSizes.d.ts → src/scriptSizes.d.ts} +1 -0
  20. package/dist/src/scriptSizes.d.ts.map +1 -0
  21. package/dist/src/scriptSizes.js +50 -0
  22. package/dist/{types.d.ts → src/types.d.ts} +1 -0
  23. package/dist/src/types.d.ts.map +1 -0
  24. package/dist/src/types.js +39 -0
  25. package/dist/{virtualSizes.d.ts → src/virtualSizes.d.ts} +13 -12
  26. package/dist/src/virtualSizes.d.ts.map +1 -0
  27. package/dist/src/virtualSizes.js +114 -0
  28. package/dist/src/zcash.d.ts +9 -0
  29. package/dist/src/zcash.d.ts.map +1 -0
  30. package/dist/src/zcash.js +22 -0
  31. package/dist/test/chain.d.ts +2 -0
  32. package/dist/test/chain.d.ts.map +1 -0
  33. package/dist/test/chain.js +92 -0
  34. package/dist/test/dimensions.d.ts +2 -0
  35. package/dist/test/dimensions.d.ts.map +1 -0
  36. package/dist/test/dimensions.js +235 -0
  37. package/dist/test/signedTx/inputWeights.d.ts +2 -0
  38. package/dist/test/signedTx/inputWeights.d.ts.map +1 -0
  39. package/dist/test/signedTx/inputWeights.js +127 -0
  40. package/dist/test/signedTx/txCombinations.d.ts +3 -0
  41. package/dist/test/signedTx/txCombinations.d.ts.map +1 -0
  42. package/dist/test/signedTx/txCombinations.js +130 -0
  43. package/dist/test/signedTx/txGen.d.ts +74 -0
  44. package/dist/test/signedTx/txGen.d.ts.map +1 -0
  45. package/dist/test/signedTx/txGen.js +233 -0
  46. package/dist/test/testutils.d.ts +36 -0
  47. package/dist/test/testutils.d.ts.map +1 -0
  48. package/dist/test/testutils.js +190 -0
  49. package/dist/test/virtualSizes.d.ts +2 -0
  50. package/dist/test/virtualSizes.d.ts.map +1 -0
  51. package/dist/test/virtualSizes.js +18 -0
  52. package/dist/test/zcash.d.ts +2 -0
  53. package/dist/test/zcash.d.ts.map +1 -0
  54. package/dist/test/zcash.js +60 -0
  55. package/dist/tsconfig.build.tsbuildinfo +1 -0
  56. package/dist/tsconfig.tsbuildinfo +1 -0
  57. package/docs/input-costs.md +14 -0
  58. package/package.json +7 -7
  59. package/dist/codes.js +0 -168
  60. package/dist/dimensions.js +0 -485
  61. package/dist/index.js +0 -30
  62. package/dist/inputWeights.js +0 -97
  63. package/dist/scriptSizes.js +0 -51
  64. package/dist/types.js +0 -25
  65. package/dist/virtualSizes.js +0 -114
package/dist/codes.js DELETED
@@ -1,168 +0,0 @@
1
- "use strict";
2
- var __createBinding = (this && this.__createBinding) || (Object.create ? (function(o, m, k, k2) {
3
- if (k2 === undefined) k2 = k;
4
- Object.defineProperty(o, k2, { enumerable: true, get: function() { return m[k]; } });
5
- }) : (function(o, m, k, k2) {
6
- if (k2 === undefined) k2 = k;
7
- o[k2] = m[k];
8
- }));
9
- var __setModuleDefault = (this && this.__setModuleDefault) || (Object.create ? (function(o, v) {
10
- Object.defineProperty(o, "default", { enumerable: true, value: v });
11
- }) : function(o, v) {
12
- o["default"] = v;
13
- });
14
- var __importStar = (this && this.__importStar) || function (mod) {
15
- if (mod && mod.__esModule) return mod;
16
- var result = {};
17
- if (mod != null) for (var k in mod) if (k !== "default" && Object.prototype.hasOwnProperty.call(mod, k)) __createBinding(result, mod, k);
18
- __setModuleDefault(result, mod);
19
- return result;
20
- };
21
- Object.defineProperty(exports, "__esModule", { value: true });
22
- exports.CodesByType = exports.CodesByPurpose = exports.CodeGroup = exports.isValid = exports.ChainType = exports.Purpose = exports.UnspentType = exports.ErrorInvalidCode = void 0;
23
- const tcomb = __importStar(require("tcomb"));
24
- class ErrorInvalidCode extends Error {
25
- constructor(code) {
26
- super(`invalid code ${code}`);
27
- }
28
- }
29
- exports.ErrorInvalidCode = ErrorInvalidCode;
30
- var UnspentType;
31
- (function (UnspentType) {
32
- UnspentType["p2pkh"] = "p2pkh";
33
- UnspentType["p2sh"] = "p2sh";
34
- UnspentType["p2shP2wsh"] = "p2shP2wsh";
35
- UnspentType["p2wpkh"] = "p2wpkh";
36
- UnspentType["p2wsh"] = "p2wsh";
37
- UnspentType["p2tr"] = "p2tr";
38
- })(UnspentType = exports.UnspentType || (exports.UnspentType = {}));
39
- const UnspentTypeTcomb = tcomb.enums.of(Object.keys(UnspentType));
40
- var Purpose;
41
- (function (Purpose) {
42
- Purpose["internal"] = "internal";
43
- Purpose["external"] = "external";
44
- })(Purpose = exports.Purpose || (exports.Purpose = {}));
45
- const PurposeTcomb = tcomb.enums.of(Object.keys(Purpose));
46
- const codeList = Object.freeze([
47
- [0, UnspentType.p2sh, Purpose.external],
48
- [10, UnspentType.p2shP2wsh, Purpose.external],
49
- [20, UnspentType.p2wsh, Purpose.external],
50
- [30, UnspentType.p2tr, Purpose.external],
51
- [1, UnspentType.p2sh, Purpose.internal],
52
- [11, UnspentType.p2shP2wsh, Purpose.internal],
53
- [21, UnspentType.p2wsh, Purpose.internal],
54
- [31, UnspentType.p2tr, Purpose.internal],
55
- ].map(([id, type, purpose]) => Object.freeze({ id, type, purpose })));
56
- exports.ChainType = tcomb.irreducible('ChainType', (n) => exports.isValid(n));
57
- const forType = (u) => {
58
- // Do tcomb type checking in js projects that use this lib
59
- if (!UnspentTypeTcomb.is(u)) {
60
- throw new Error(`invalid unspent type: ${u}`);
61
- }
62
- return new CodesByPurpose(u);
63
- };
64
- const typeForCode = (c) => {
65
- const code = codeList.find(({ id }) => id === c);
66
- if (!code) {
67
- throw new ErrorInvalidCode(c);
68
- }
69
- return code.type;
70
- };
71
- const isValid = (c) => codeList.some(({ id }) => id === c);
72
- exports.isValid = isValid;
73
- const throwIfUndefined = (v) => {
74
- if (v === undefined) {
75
- throw new Error(`expected value to be defined`);
76
- }
77
- return v;
78
- };
79
- class CodeGroup {
80
- constructor(values) {
81
- this.values = Object.freeze([...values]);
82
- }
83
- has(code) {
84
- if (!exports.isValid(code)) {
85
- throw new ErrorInvalidCode(code);
86
- }
87
- return this.values.includes(code);
88
- }
89
- }
90
- exports.CodeGroup = CodeGroup;
91
- class CodesByPurpose extends CodeGroup {
92
- constructor(t) {
93
- const codeMap = new Map(codeList.filter(({ type }) => type === t).map(({ purpose, id }) => [purpose, id]));
94
- if (codeMap.size !== 2) {
95
- throw new Error(`unexpected number of codes for type ${t}`);
96
- }
97
- super(codeMap.values());
98
- this.internal = throwIfUndefined(codeMap.get(Purpose.internal));
99
- this.external = throwIfUndefined(codeMap.get(Purpose.external));
100
- }
101
- }
102
- exports.CodesByPurpose = CodesByPurpose;
103
- class CodesByType extends CodeGroup {
104
- constructor(p) {
105
- const codeMap = new Map(codeList.filter(({ purpose }) => purpose === p).map(({ type, id }) => [type, id]));
106
- if (codeMap.size !== 4) {
107
- throw new Error(`unexpected number of codes`);
108
- }
109
- super(codeMap.values());
110
- this.p2sh = throwIfUndefined(codeMap.get(UnspentType.p2sh));
111
- this.p2shP2wsh = throwIfUndefined(codeMap.get(UnspentType.p2shP2wsh));
112
- this.p2wsh = throwIfUndefined(codeMap.get(UnspentType.p2wsh));
113
- this.p2tr = throwIfUndefined(codeMap.get(UnspentType.p2tr));
114
- }
115
- }
116
- exports.CodesByType = CodesByType;
117
- const boundHas = (instance) => instance.has.bind(instance);
118
- const p2sh = Object.freeze(new CodesByPurpose(UnspentType.p2sh));
119
- const p2shP2wsh = Object.freeze(new CodesByPurpose(UnspentType.p2shP2wsh));
120
- const p2wsh = Object.freeze(new CodesByPurpose(UnspentType.p2wsh));
121
- const p2tr = Object.freeze(new CodesByPurpose(UnspentType.p2tr));
122
- const external = Object.freeze(new CodesByType(Purpose.external));
123
- const internal = Object.freeze(new CodesByType(Purpose.internal));
124
- const all = Object.freeze(codeList.map(({ id }) => id));
125
- /** @deprecated - use utxolib.bitgo */
126
- exports.default = Object.freeze({
127
- /* @deprecated: use ChainCodeTcomb */
128
- ChainType: exports.ChainType,
129
- /** @deprecated - use utxolib.bitgo */
130
- ChainCodeTcomb: exports.ChainType,
131
- /** @deprecated - use utxolib.bitgo */
132
- PurposeTcomb,
133
- /** @deprecated - use utxolib.bitgo */
134
- UnspentTypeTcomb,
135
- /** @deprecated - use utxolib.bitgo */
136
- p2sh,
137
- /** @deprecated - use utxolib.bitgo */
138
- p2shP2wsh,
139
- /** @deprecated - use utxolib.bitgo */
140
- p2wsh,
141
- /** @deprecated - use utxolib.bitgo */
142
- p2tr,
143
- /** @deprecated - use utxolib.bitgo */
144
- external,
145
- /** @deprecated - use utxolib.bitgo */
146
- internal,
147
- /** @deprecated - use utxolib.bitgo */
148
- all,
149
- /** @deprecated - use utxolib.bitgo */
150
- isP2sh: boundHas(p2sh),
151
- /** @deprecated - use utxolib.bitgo */
152
- isP2shP2wsh: boundHas(p2shP2wsh),
153
- /** @deprecated - use utxolib.bitgo */
154
- isP2wsh: boundHas(p2wsh),
155
- /** @deprecated - use utxolib.bitgo */
156
- isP2tr: boundHas(p2tr),
157
- /** @deprecated - use utxolib.bitgo */
158
- isExternal: boundHas(external),
159
- /** @deprecated - use utxolib.bitgo */
160
- isInternal: boundHas(internal),
161
- /** @deprecated - use utxolib.bitgo */
162
- isValid: exports.isValid,
163
- /** @deprecated - use utxolib.bitgo */
164
- forType,
165
- /** @deprecated - use utxolib.bitgo */
166
- typeForCode,
167
- });
168
- //# sourceMappingURL=codes.js.map
@@ -1,485 +0,0 @@
1
- "use strict";
2
- var __createBinding = (this && this.__createBinding) || (Object.create ? (function(o, m, k, k2) {
3
- if (k2 === undefined) k2 = k;
4
- Object.defineProperty(o, k2, { enumerable: true, get: function() { return m[k]; } });
5
- }) : (function(o, m, k, k2) {
6
- if (k2 === undefined) k2 = k;
7
- o[k2] = m[k];
8
- }));
9
- var __setModuleDefault = (this && this.__setModuleDefault) || (Object.create ? (function(o, v) {
10
- Object.defineProperty(o, "default", { enumerable: true, value: v });
11
- }) : function(o, v) {
12
- o["default"] = v;
13
- });
14
- var __importStar = (this && this.__importStar) || function (mod) {
15
- if (mod && mod.__esModule) return mod;
16
- var result = {};
17
- if (mod != null) for (var k in mod) if (k !== "default" && Object.prototype.hasOwnProperty.call(mod, k)) __createBinding(result, mod, k);
18
- __setModuleDefault(result, mod);
19
- return result;
20
- };
21
- Object.defineProperty(exports, "__esModule", { value: true });
22
- exports.Dimensions = exports.OutputDimensions = exports.VirtualSizes = void 0;
23
- const utxolib = __importStar(require("@bitgo-beta/utxo-lib"));
24
- const utxo_lib_1 = require("@bitgo-beta/utxo-lib");
25
- const { isChainCode, scriptTypeForChain } = utxo_lib_1.bitgo;
26
- const scriptSizes_1 = require("./scriptSizes");
27
- const types_1 = require("./types");
28
- const virtualSizes_1 = require("./virtualSizes");
29
- Object.defineProperty(exports, "VirtualSizes", { enumerable: true, get: function () { return virtualSizes_1.VirtualSizes; } });
30
- /**
31
- * Apply `f` to all properties of `d`
32
- */
33
- function mapDimensions(d, f) {
34
- return new Dimensions(Object.fromEntries(Object.entries(d).map(([key, value]) => [key, f(key, value)])));
35
- }
36
- /**
37
- * Aggregate count and size of transaction outputs
38
- */
39
- class OutputDimensions {
40
- constructor({ count = 0, size = 0 } = { count: 0, size: 0 }) {
41
- if (count === 0 || size === 0) {
42
- if (count !== 0 || size !== 0) {
43
- throw new Error(`count and size must both be zero if one is zero`);
44
- }
45
- }
46
- this.count = count;
47
- this.size = size;
48
- Object.freeze(this);
49
- }
50
- }
51
- exports.OutputDimensions = OutputDimensions;
52
- const defaultUnspentParams = {
53
- p2tr: {
54
- scriptPathLevel: 1,
55
- },
56
- p2trMusig2: {
57
- // Default to script path spend, to make it easier for recovery case callers (WRW etc).
58
- // WP can explicitly pass undefined to use key path.
59
- scriptPathLevel: 1,
60
- },
61
- };
62
- /**
63
- * Dimensions of a BitGo wallet transactions.
64
- */
65
- class Dimensions {
66
- constructor(d = {}) {
67
- /** Input counts for BitGo wallet multi-signature inputs */
68
- this.nP2shInputs = 0;
69
- this.nP2shP2wshInputs = 0;
70
- this.nP2wshInputs = 0;
71
- this.nP2trKeypathInputs = 0;
72
- this.nP2trScriptPathLevel1Inputs = 0;
73
- this.nP2trScriptPathLevel2Inputs = 0;
74
- /* Input count for single-signature inputs (Replay Protection inputs) */
75
- this.nP2shP2pkInputs = 0;
76
- this.outputs = new OutputDimensions();
77
- Object.entries(d).forEach(([key, value]) => this.setProperty(key, value));
78
- Object.freeze(this);
79
- }
80
- setProperty(k, v) {
81
- switch (k) {
82
- case 'nP2shInputs':
83
- case 'nP2shP2wshInputs':
84
- case 'nP2wshInputs':
85
- case 'nP2trKeypathInputs':
86
- case 'nP2trScriptPathLevel1Inputs':
87
- case 'nP2trScriptPathLevel2Inputs':
88
- case 'nP2shP2pkInputs':
89
- if (typeof v !== 'number') {
90
- throw new Error(`property ${k} must be number`);
91
- }
92
- if (!Number.isSafeInteger(v) || v < 0) {
93
- throw new Error(`property ${k} must be zero or positive integer`);
94
- }
95
- break;
96
- case 'outputs':
97
- if (!(v instanceof OutputDimensions)) {
98
- v = new OutputDimensions(v);
99
- }
100
- break;
101
- default:
102
- throw new Error(`unknown property ${k}`);
103
- }
104
- this[k] = v;
105
- }
106
- /**
107
- * @deprecated use ZERO
108
- * @return Dimensions for an empty transaction
109
- */
110
- static zero() {
111
- return this.ZERO;
112
- }
113
- /**
114
- * @param size
115
- * @return Dimensions for a single output with given size
116
- */
117
- static singleOutput(size) {
118
- return Dimensions.sum({ outputs: { count: 1, size } });
119
- }
120
- /**
121
- * @return Number of total inputs (p2sh + p2shP2wsh + p2wsh + p2tr)
122
- */
123
- get nInputs() {
124
- return (this.nP2shInputs +
125
- this.nP2shP2wshInputs +
126
- this.nP2wshInputs +
127
- this.nP2trKeypathInputs +
128
- this.nP2trScriptPathLevel1Inputs +
129
- this.nP2trScriptPathLevel2Inputs +
130
- this.nP2shP2pkInputs);
131
- }
132
- set nInputs(_) {
133
- throw new Error('read-only property nInputs');
134
- }
135
- /**
136
- * @return Number of total outputs
137
- */
138
- get nOutputs() {
139
- return this.outputs.count;
140
- }
141
- set nOutputs(_) {
142
- throw new Error(`read-only property nOutputs`);
143
- }
144
- /**
145
- * @param args - Dimensions (can be partially defined)
146
- * @return {Dimensions} sum of arguments
147
- */
148
- static sum(...args) {
149
- return args.reduce((a, b) => a.plus(b), new Dimensions());
150
- }
151
- /**
152
- * @param chain
153
- * @return {Number}
154
- */
155
- static getOutputScriptLengthForChain(chain) {
156
- switch (scriptTypeForChain(chain)) {
157
- case 'p2wsh':
158
- case 'p2tr':
159
- case 'p2trMusig2':
160
- return 34;
161
- default:
162
- return 23;
163
- }
164
- }
165
- /**
166
- * @param scriptLength
167
- * @return {Number} vSize of an output with script length
168
- */
169
- static getVSizeForOutputWithScriptLength(scriptLength) {
170
- if (!types_1.PositiveInteger.is(scriptLength)) {
171
- throw new TypeError(`expected positive integer for scriptLength, got ${scriptLength}`);
172
- }
173
- return scriptLength + scriptSizes_1.compactSize(scriptLength) + virtualSizes_1.VirtualSizes.txOutputAmountSize;
174
- }
175
- /**
176
- * @return
177
- */
178
- static fromScriptType(scriptType, params = {}) {
179
- switch (scriptType) {
180
- case 'p2sh':
181
- case 'p2shP2wsh':
182
- case 'p2wsh':
183
- case 'p2shP2pk':
184
- return Dimensions.SingleInput[scriptType];
185
- case 'p2tr':
186
- case 'taprootScriptPathSpend':
187
- switch (params.scriptPathLevel) {
188
- case 1:
189
- return Dimensions.SingleInput.p2trScriptPathLevel1;
190
- case 2:
191
- return Dimensions.SingleInput.p2trScriptPathLevel2;
192
- default:
193
- throw new Error(`unexpected script path level`);
194
- }
195
- case 'p2trMusig2':
196
- switch (params.scriptPathLevel) {
197
- case undefined:
198
- return Dimensions.SingleInput.p2trKeypath;
199
- case 1:
200
- return Dimensions.SingleInput.p2trScriptPathLevel1;
201
- default:
202
- throw new Error(`unexpected script path level`);
203
- }
204
- case 'taprootKeyPathSpend':
205
- return Dimensions.SingleInput.p2trKeypath;
206
- default:
207
- throw new Error(`unexpected scriptType ${scriptType}`);
208
- }
209
- }
210
- static getAssumedDimension(params = {}, index) {
211
- const { assumeUnsigned } = params;
212
- if (!assumeUnsigned) {
213
- throw new Error(`illegal input ${index}: empty script and assumeUnsigned not set`);
214
- }
215
- return assumeUnsigned;
216
- }
217
- /**
218
- * @param input - the transaction input to count
219
- * @param params
220
- * [param.assumeUnsigned] - default type for unsigned input
221
- */
222
- static fromInput(input, params = {}) {
223
- var _a, _b;
224
- if (((_a = input.script) === null || _a === void 0 ? void 0 : _a.length) || ((_b = input.witness) === null || _b === void 0 ? void 0 : _b.length)) {
225
- const parsed = utxolib.bitgo.parseSignatureScript(input);
226
- return Dimensions.fromScriptType(parsed.scriptType, parsed);
227
- }
228
- return Dimensions.getAssumedDimension(params, input.index);
229
- }
230
- /**
231
- * Create Dimensions from psbt input
232
- * @param input - psbt input
233
- */
234
- static fromPsbtInput(input) {
235
- const parsed = utxo_lib_1.bitgo.parsePsbtInput(input);
236
- return Dimensions.fromScriptType(parsed.scriptType, parsed);
237
- }
238
- /**
239
- * @param inputs - Array of inputs
240
- * @param params - @see Dimensions.fromInput()
241
- * @return {Dimensions} sum of the dimensions for each input (@see Dimensions.fromInput())
242
- */
243
- static fromInputs(inputs, params) {
244
- if (!Array.isArray(inputs)) {
245
- throw new TypeError(`inputs must be array`);
246
- }
247
- return Dimensions.sum(...inputs.map((i) => Dimensions.fromInput(i, params)));
248
- }
249
- /**
250
- * Create Dimensions from multiple psbt inputs
251
- * @param inputs psbt input array
252
- * @return {Dimensions} sum of the dimensions for each input (@see Dimensions.fromPsbtInput())
253
- */
254
- static fromPsbtInputs(inputs) {
255
- if (!Array.isArray(inputs)) {
256
- throw new TypeError(`inputs must be array`);
257
- }
258
- return Dimensions.sum(...inputs.map((input, _) => Dimensions.fromPsbtInput(input)));
259
- }
260
- /**
261
- * @param scriptLength {number} - size of the output script in bytes
262
- * @return {Dimensions} - Dimensions of the output
263
- */
264
- static fromOutputScriptLength(scriptLength) {
265
- return Dimensions.sum({
266
- outputs: {
267
- count: 1,
268
- size: Dimensions.getVSizeForOutputWithScriptLength(scriptLength),
269
- },
270
- });
271
- }
272
- /**
273
- * @param output - a tx output
274
- * @return Dimensions - the dimensions of the given output
275
- */
276
- static fromOutput({ script }) {
277
- if (!script) {
278
- throw new Error('expected output script to be defined');
279
- }
280
- if (!Buffer.isBuffer(script)) {
281
- throw new TypeError('expected script to be buffer, got ' + typeof script);
282
- }
283
- return Dimensions.fromOutputScriptLength(script.length);
284
- }
285
- /**
286
- * @param outputs - Array of outputs
287
- * @return {Dimensions} sum of the dimensions for each output (@see Dimensions.fromOutput())
288
- */
289
- static fromOutputs(outputs) {
290
- if (!Array.isArray(outputs)) {
291
- throw new TypeError(`outputs must be array`);
292
- }
293
- return Dimensions.sum(...outputs.map(Dimensions.fromOutput));
294
- }
295
- /**
296
- * Returns the dimensions of an output that will be created on a specific chain.
297
- * Currently, this simply adds a default output.
298
- *
299
- * @param chain - Chain code as defined by utxolib.bitgo
300
- * @return {Dimensions} - Dimensions for a single output on the given chain.
301
- */
302
- static fromOutputOnChain(chain) {
303
- return Dimensions.fromOutputScriptLength(Dimensions.getOutputScriptLengthForChain(chain));
304
- }
305
- /**
306
- * Return dimensions of an unspent according to `chain` parameter
307
- * @param chain - Chain code as defined by utxo.chain
308
- * @param params - Hint for unspents with variable input sizes (p2tr).
309
- * @return {Dimensions} of the unspent
310
- * @throws if the chain code is invalid or unsupported
311
- */
312
- static fromUnspent({ chain }, params = defaultUnspentParams) {
313
- if (!isChainCode(chain)) {
314
- throw new TypeError('invalid chain code');
315
- }
316
- const scriptType = scriptTypeForChain(chain);
317
- return Dimensions.fromScriptType(scriptType, scriptType === 'p2tr' ? params.p2tr : scriptType === 'p2trMusig2' ? params.p2trMusig2 : {});
318
- }
319
- /**
320
- * @param unspents
321
- * @return {Dimensions} sum of the dimensions for each unspent (@see Dimensions.fromUnspent())
322
- */
323
- static fromUnspents(unspents) {
324
- if (!Array.isArray(unspents)) {
325
- throw new TypeError(`unspents must be array`);
326
- }
327
- // Convert the individual unspents into dimensions and sum them up
328
- return Dimensions.sum(...unspents.map((u) => Dimensions.fromUnspent(u)));
329
- }
330
- /**
331
- * @param transaction - bitcoin-like transaction
332
- * @param [param.assumeUnsigned] - default type for unsigned inputs
333
- * @return {Dimensions}
334
- */
335
- static fromTransaction({ ins, outs, }, params) {
336
- return Dimensions.fromInputs(ins, params).plus(Dimensions.fromOutputs(outs));
337
- }
338
- /**
339
- * Create Dimensions from psbt inputs and outputs
340
- * @param psbt
341
- * @return {Dimensions}
342
- */
343
- static fromPsbt(psbt) {
344
- return Dimensions.fromPsbtInputs(psbt.data.inputs).plus(Dimensions.fromOutputs(psbt.getUnsignedTx().outs));
345
- }
346
- /**
347
- * @param dimensions (can be partially defined)
348
- * @return new dimensions with argument added
349
- */
350
- plus(dimensions) {
351
- if (typeof dimensions !== 'object') {
352
- throw new TypeError(`expected argument to be object`);
353
- }
354
- if (!(dimensions instanceof Dimensions)) {
355
- dimensions = new Dimensions(dimensions);
356
- }
357
- // Catch instances where we try to initialize Dimensions from partial data using deprecated parameters
358
- // using only "nOutputs".
359
- if ('nOutputs' in dimensions) {
360
- if (!('outputs' in dimensions)) {
361
- throw new Error('deprecated partial addition: argument has key "nOutputs" but no "outputs"');
362
- }
363
- const { outputs, nOutputs } = dimensions;
364
- if (outputs.count !== nOutputs) {
365
- throw new Error('deprecated partial addition: inconsistent values for "nOutputs" and "outputs.count"');
366
- }
367
- }
368
- return mapDimensions(this, (key, v) => {
369
- var _a;
370
- const w = (_a = dimensions[key]) !== null && _a !== void 0 ? _a : Dimensions.ZERO[key];
371
- if (key === 'outputs') {
372
- const vOutputs = v;
373
- const wOutputs = w;
374
- return new OutputDimensions({
375
- count: vOutputs.count + wOutputs.count,
376
- size: vOutputs.size + wOutputs.size,
377
- });
378
- }
379
- return v + w;
380
- });
381
- }
382
- /**
383
- * Multiply dimensions by a given factor
384
- * @param factor - Positive integer
385
- * @return {Dimensions}
386
- */
387
- times(factor) {
388
- if (!types_1.PositiveInteger.is(factor)) {
389
- throw new TypeError(`expected factor to be positive integer`);
390
- }
391
- return mapDimensions(this, (key, value) => {
392
- if (key === 'outputs') {
393
- const vOutputs = value;
394
- return {
395
- count: vOutputs.count * factor,
396
- size: vOutputs.size * factor,
397
- };
398
- }
399
- return value * factor;
400
- });
401
- }
402
- /**
403
- * @return Number of total inputs (p2sh, p2shP2wsh and p2wsh)
404
- * @deprecated use `dimension.nInputs` instead
405
- */
406
- getNInputs() {
407
- return this.nInputs;
408
- }
409
- /**
410
- * @returns {boolean} true iff dimensions have one or more (p2sh)p2wsh inputs
411
- */
412
- isSegwit() {
413
- return (this.nP2wshInputs +
414
- this.nP2shP2wshInputs +
415
- this.nP2trKeypathInputs +
416
- this.nP2trScriptPathLevel1Inputs +
417
- this.nP2trScriptPathLevel2Inputs >
418
- 0);
419
- }
420
- /**
421
- * @return {Number} overhead vsize, based on result isSegwit().
422
- */
423
- getOverheadVSize() {
424
- return this.isSegwit() ? virtualSizes_1.VirtualSizes.txSegOverheadVSize : virtualSizes_1.VirtualSizes.txOverheadSize;
425
- }
426
- /**
427
- * @returns {number} vsize of inputs, without transaction overhead
428
- */
429
- getInputsVSize() {
430
- const { txP2shInputSize, txP2shP2wshInputSize, txP2wshInputSize, txP2trKeypathInputSize, txP2trScriptPathLevel1InputSize, txP2trScriptPathLevel2InputSize, txP2shP2pkInputSize, } = virtualSizes_1.VirtualSizes;
431
- const { nP2shInputs, nP2shP2wshInputs, nP2wshInputs, nP2trKeypathInputs, nP2trScriptPathLevel1Inputs, nP2trScriptPathLevel2Inputs, nP2shP2pkInputs, } = this;
432
- const size = nP2shInputs * txP2shInputSize +
433
- nP2shP2wshInputs * txP2shP2wshInputSize +
434
- nP2wshInputs * txP2wshInputSize +
435
- nP2trKeypathInputs * txP2trKeypathInputSize +
436
- nP2shP2pkInputs * txP2shP2pkInputSize +
437
- nP2trScriptPathLevel1Inputs * txP2trScriptPathLevel1InputSize +
438
- nP2trScriptPathLevel2Inputs * txP2trScriptPathLevel2InputSize;
439
- if (Number.isNaN(size)) {
440
- throw new Error(`invalid size`);
441
- }
442
- return size;
443
- }
444
- /**
445
- * @returns {number} return vsize of outputs, without overhead
446
- */
447
- getOutputsVSize() {
448
- return this.outputs.size;
449
- }
450
- /**
451
- * Estimates the virtual size (1/4 weight) of a signed transaction as sum of
452
- * overhead vsize, input vsize and output vsize.
453
- * @returns {Number} The estimated vsize of the transaction dimensions.
454
- */
455
- getVSize() {
456
- return this.getOverheadVSize() + this.getInputsVSize() + this.getOutputsVSize();
457
- }
458
- }
459
- exports.Dimensions = Dimensions;
460
- Dimensions.ZERO = Object.freeze(new Dimensions());
461
- Dimensions.SingleOutput = Object.freeze({
462
- p2sh: Dimensions.singleOutput(virtualSizes_1.VirtualSizes.txP2shOutputSize),
463
- p2shP2wsh: Dimensions.singleOutput(virtualSizes_1.VirtualSizes.txP2shP2wshOutputSize),
464
- p2wsh: Dimensions.singleOutput(virtualSizes_1.VirtualSizes.txP2wshOutputSize),
465
- p2tr: Dimensions.singleOutput(virtualSizes_1.VirtualSizes.txP2trOutputSize),
466
- p2pkh: Dimensions.singleOutput(virtualSizes_1.VirtualSizes.txP2pkhOutputSize),
467
- p2wpkh: Dimensions.singleOutput(virtualSizes_1.VirtualSizes.txP2wpkhOutputSize),
468
- });
469
- Dimensions.SingleInput = Object.freeze({
470
- p2sh: Dimensions.sum({ nP2shInputs: 1 }),
471
- p2shP2wsh: Dimensions.sum({ nP2shP2wshInputs: 1 }),
472
- p2wsh: Dimensions.sum({ nP2wshInputs: 1 }),
473
- p2trKeypath: Dimensions.sum({ nP2trKeypathInputs: 1 }),
474
- p2trScriptPathLevel1: Dimensions.sum({ nP2trScriptPathLevel1Inputs: 1 }),
475
- p2trScriptPathLevel2: Dimensions.sum({ nP2trScriptPathLevel2Inputs: 1 }),
476
- p2shP2pk: Dimensions.sum({ nP2shP2pkInputs: 1 }),
477
- });
478
- Dimensions.ASSUME_P2SH = Dimensions.SingleInput.p2sh;
479
- Dimensions.ASSUME_P2SH_P2WSH = Dimensions.SingleInput.p2shP2wsh;
480
- Dimensions.ASSUME_P2WSH = Dimensions.SingleInput.p2wsh;
481
- Dimensions.ASSUME_P2TR_KEYPATH = Dimensions.SingleInput.p2trKeypath;
482
- Dimensions.ASSUME_P2TR_SCRIPTPATH_LEVEL1 = Dimensions.SingleInput.p2trScriptPathLevel1;
483
- Dimensions.ASSUME_P2TR_SCRIPTPATH_LEVEL2 = Dimensions.SingleInput.p2trScriptPathLevel2;
484
- Dimensions.ASSUME_P2SH_P2PK_INPUT = Dimensions.SingleInput.p2shP2pk;
485
- //# sourceMappingURL=dimensions.js.map
package/dist/index.js DELETED
@@ -1,30 +0,0 @@
1
- "use strict";
2
- var __createBinding = (this && this.__createBinding) || (Object.create ? (function(o, m, k, k2) {
3
- if (k2 === undefined) k2 = k;
4
- Object.defineProperty(o, k2, { enumerable: true, get: function() { return m[k]; } });
5
- }) : (function(o, m, k, k2) {
6
- if (k2 === undefined) k2 = k;
7
- o[k2] = m[k];
8
- }));
9
- var __setModuleDefault = (this && this.__setModuleDefault) || (Object.create ? (function(o, v) {
10
- Object.defineProperty(o, "default", { enumerable: true, value: v });
11
- }) : function(o, v) {
12
- o["default"] = v;
13
- });
14
- var __importStar = (this && this.__importStar) || function (mod) {
15
- if (mod && mod.__esModule) return mod;
16
- var result = {};
17
- if (mod != null) for (var k in mod) if (k !== "default" && Object.prototype.hasOwnProperty.call(mod, k)) __createBinding(result, mod, k);
18
- __setModuleDefault(result, mod);
19
- return result;
20
- };
21
- var __exportStar = (this && this.__exportStar) || function(m, exports) {
22
- for (var p in m) if (p !== "default" && !Object.prototype.hasOwnProperty.call(exports, p)) __createBinding(exports, m, p);
23
- };
24
- Object.defineProperty(exports, "__esModule", { value: true });
25
- exports.CodesTypes = exports.Codes = void 0;
26
- const codes_1 = __importStar(require("./codes")), CodesTypes = codes_1;
27
- exports.Codes = codes_1.default;
28
- exports.CodesTypes = CodesTypes;
29
- __exportStar(require("./dimensions"), exports);
30
- //# sourceMappingURL=index.js.map