@bitgo-beta/unspents 0.13.2-beta.9 → 0.13.2-beta.900
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.
- package/.mocharc.js +1 -1
- package/CHANGELOG.md +541 -0
- package/bin/generate_tables.ts +94 -0
- package/dist/bin/generate_tables.d.ts +2 -0
- package/dist/bin/generate_tables.d.ts.map +1 -0
- package/dist/bin/generate_tables.js +107 -0
- package/dist/{codes.d.ts → src/codes.d.ts} +11 -10
- package/dist/src/codes.d.ts.map +1 -0
- package/dist/src/codes.js +182 -0
- package/dist/{dimensions.d.ts → src/dimensions.d.ts} +10 -11
- package/dist/src/dimensions.d.ts.map +1 -0
- package/dist/src/dimensions.js +500 -0
- package/dist/{index.d.ts → src/index.d.ts} +1 -0
- package/dist/src/index.d.ts.map +1 -0
- package/dist/src/index.js +44 -0
- package/dist/{inputWeights.d.ts → src/inputWeights.d.ts} +4 -3
- package/dist/src/inputWeights.d.ts.map +1 -0
- package/dist/src/inputWeights.js +97 -0
- package/dist/{scriptSizes.d.ts → src/scriptSizes.d.ts} +1 -0
- package/dist/src/scriptSizes.d.ts.map +1 -0
- package/dist/src/scriptSizes.js +50 -0
- package/dist/{types.d.ts → src/types.d.ts} +1 -0
- package/dist/src/types.d.ts.map +1 -0
- package/dist/src/types.js +39 -0
- package/dist/{virtualSizes.d.ts → src/virtualSizes.d.ts} +13 -12
- package/dist/src/virtualSizes.d.ts.map +1 -0
- package/dist/src/virtualSizes.js +114 -0
- package/dist/src/zcash.d.ts +9 -0
- package/dist/src/zcash.d.ts.map +1 -0
- package/dist/src/zcash.js +22 -0
- package/dist/test/chain.d.ts +2 -0
- package/dist/test/chain.d.ts.map +1 -0
- package/dist/test/chain.js +92 -0
- package/dist/test/dimensions.d.ts +2 -0
- package/dist/test/dimensions.d.ts.map +1 -0
- package/dist/test/dimensions.js +235 -0
- package/dist/test/signedTx/inputWeights.d.ts +2 -0
- package/dist/test/signedTx/inputWeights.d.ts.map +1 -0
- package/dist/test/signedTx/inputWeights.js +127 -0
- package/dist/test/signedTx/txCombinations.d.ts +3 -0
- package/dist/test/signedTx/txCombinations.d.ts.map +1 -0
- package/dist/test/signedTx/txCombinations.js +130 -0
- package/dist/test/signedTx/txGen.d.ts +74 -0
- package/dist/test/signedTx/txGen.d.ts.map +1 -0
- package/dist/test/signedTx/txGen.js +233 -0
- package/dist/test/testutils.d.ts +36 -0
- package/dist/test/testutils.d.ts.map +1 -0
- package/dist/test/testutils.js +190 -0
- package/dist/test/virtualSizes.d.ts +2 -0
- package/dist/test/virtualSizes.d.ts.map +1 -0
- package/dist/test/virtualSizes.js +18 -0
- package/dist/test/zcash.d.ts +2 -0
- package/dist/test/zcash.d.ts.map +1 -0
- package/dist/test/zcash.js +60 -0
- package/dist/tsconfig.build.tsbuildinfo +1 -0
- package/dist/tsconfig.tsbuildinfo +1 -0
- package/docs/input-costs.md +14 -0
- package/package.json +7 -7
- package/dist/codes.js +0 -168
- package/dist/dimensions.js +0 -490
- package/dist/index.js +0 -30
- package/dist/inputWeights.js +0 -97
- package/dist/scriptSizes.js +0 -51
- package/dist/types.js +0 -25
- 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
|
package/dist/dimensions.js
DELETED
|
@@ -1,490 +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 psbt - psbt
|
|
233
|
-
* @param inputIndex - psbt input index
|
|
234
|
-
*/
|
|
235
|
-
static fromPsbtInput(psbt, inputIndex) {
|
|
236
|
-
const parsed = utxolib.bitgo.parsePsbtInput(psbt, inputIndex);
|
|
237
|
-
if (parsed && parsed.scriptType) {
|
|
238
|
-
return Dimensions.fromScriptType(parsed.scriptType, parsed);
|
|
239
|
-
}
|
|
240
|
-
throw new Error(`illegal input ${inputIndex}: empty script`);
|
|
241
|
-
}
|
|
242
|
-
/**
|
|
243
|
-
* @param inputs - Array of inputs
|
|
244
|
-
* @param params - @see Dimensions.fromInput()
|
|
245
|
-
* @return {Dimensions} sum of the dimensions for each input (@see Dimensions.fromInput())
|
|
246
|
-
*/
|
|
247
|
-
static fromInputs(inputs, params) {
|
|
248
|
-
if (!Array.isArray(inputs)) {
|
|
249
|
-
throw new TypeError(`inputs must be array`);
|
|
250
|
-
}
|
|
251
|
-
return Dimensions.sum(...inputs.map((i) => Dimensions.fromInput(i, params)));
|
|
252
|
-
}
|
|
253
|
-
/**
|
|
254
|
-
* Create Dimensions from multiple psbt inputs
|
|
255
|
-
* @param psbt
|
|
256
|
-
* @param params - @see Dimensions.fromInput()
|
|
257
|
-
* @return {Dimensions} sum of the dimensions for each input (@see Dimensions.fromInput())
|
|
258
|
-
*/
|
|
259
|
-
static fromPsbtInputs(psbt) {
|
|
260
|
-
if (!Array.isArray(psbt.txInputs)) {
|
|
261
|
-
throw new TypeError(`psbt must have inputs`);
|
|
262
|
-
}
|
|
263
|
-
return Dimensions.sum(...psbt.txInputs.map((input, inputIndex) => Dimensions.fromPsbtInput(psbt, inputIndex)));
|
|
264
|
-
}
|
|
265
|
-
/**
|
|
266
|
-
* @param scriptLength {number} - size of the output script in bytes
|
|
267
|
-
* @return {Dimensions} - Dimensions of the output
|
|
268
|
-
*/
|
|
269
|
-
static fromOutputScriptLength(scriptLength) {
|
|
270
|
-
return Dimensions.sum({
|
|
271
|
-
outputs: {
|
|
272
|
-
count: 1,
|
|
273
|
-
size: Dimensions.getVSizeForOutputWithScriptLength(scriptLength),
|
|
274
|
-
},
|
|
275
|
-
});
|
|
276
|
-
}
|
|
277
|
-
/**
|
|
278
|
-
* @param output - a tx output
|
|
279
|
-
* @return Dimensions - the dimensions of the given output
|
|
280
|
-
*/
|
|
281
|
-
static fromOutput({ script }) {
|
|
282
|
-
if (!script) {
|
|
283
|
-
throw new Error('expected output script to be defined');
|
|
284
|
-
}
|
|
285
|
-
if (!Buffer.isBuffer(script)) {
|
|
286
|
-
throw new TypeError('expected script to be buffer, got ' + typeof script);
|
|
287
|
-
}
|
|
288
|
-
return Dimensions.fromOutputScriptLength(script.length);
|
|
289
|
-
}
|
|
290
|
-
/**
|
|
291
|
-
* @param outputs - Array of outputs
|
|
292
|
-
* @return {Dimensions} sum of the dimensions for each output (@see Dimensions.fromOutput())
|
|
293
|
-
*/
|
|
294
|
-
static fromOutputs(outputs) {
|
|
295
|
-
if (!Array.isArray(outputs)) {
|
|
296
|
-
throw new TypeError(`outputs must be array`);
|
|
297
|
-
}
|
|
298
|
-
return Dimensions.sum(...outputs.map(Dimensions.fromOutput));
|
|
299
|
-
}
|
|
300
|
-
/**
|
|
301
|
-
* Returns the dimensions of an output that will be created on a specific chain.
|
|
302
|
-
* Currently, this simply adds a default output.
|
|
303
|
-
*
|
|
304
|
-
* @param chain - Chain code as defined by utxolib.bitgo
|
|
305
|
-
* @return {Dimensions} - Dimensions for a single output on the given chain.
|
|
306
|
-
*/
|
|
307
|
-
static fromOutputOnChain(chain) {
|
|
308
|
-
return Dimensions.fromOutputScriptLength(Dimensions.getOutputScriptLengthForChain(chain));
|
|
309
|
-
}
|
|
310
|
-
/**
|
|
311
|
-
* Return dimensions of an unspent according to `chain` parameter
|
|
312
|
-
* @param chain - Chain code as defined by utxo.chain
|
|
313
|
-
* @param params - Hint for unspents with variable input sizes (p2tr).
|
|
314
|
-
* @return {Dimensions} of the unspent
|
|
315
|
-
* @throws if the chain code is invalid or unsupported
|
|
316
|
-
*/
|
|
317
|
-
static fromUnspent({ chain }, params = defaultUnspentParams) {
|
|
318
|
-
if (!isChainCode(chain)) {
|
|
319
|
-
throw new TypeError('invalid chain code');
|
|
320
|
-
}
|
|
321
|
-
const scriptType = scriptTypeForChain(chain);
|
|
322
|
-
return Dimensions.fromScriptType(scriptType, scriptType === 'p2tr' ? params.p2tr : scriptType === 'p2trMusig2' ? params.p2trMusig2 : {});
|
|
323
|
-
}
|
|
324
|
-
/**
|
|
325
|
-
* @param unspents
|
|
326
|
-
* @return {Dimensions} sum of the dimensions for each unspent (@see Dimensions.fromUnspent())
|
|
327
|
-
*/
|
|
328
|
-
static fromUnspents(unspents) {
|
|
329
|
-
if (!Array.isArray(unspents)) {
|
|
330
|
-
throw new TypeError(`unspents must be array`);
|
|
331
|
-
}
|
|
332
|
-
// Convert the individual unspents into dimensions and sum them up
|
|
333
|
-
return Dimensions.sum(...unspents.map((u) => Dimensions.fromUnspent(u)));
|
|
334
|
-
}
|
|
335
|
-
/**
|
|
336
|
-
* @param transaction - bitcoin-like transaction
|
|
337
|
-
* @param [param.assumeUnsigned] - default type for unsigned inputs
|
|
338
|
-
* @return {Dimensions}
|
|
339
|
-
*/
|
|
340
|
-
static fromTransaction({ ins, outs, }, params) {
|
|
341
|
-
return Dimensions.fromInputs(ins, params).plus(Dimensions.fromOutputs(outs));
|
|
342
|
-
}
|
|
343
|
-
/**
|
|
344
|
-
* Create Dimensions from psbt inputs and outputs
|
|
345
|
-
* @param psbt
|
|
346
|
-
* @return {Dimensions}
|
|
347
|
-
*/
|
|
348
|
-
static fromPsbt(psbt) {
|
|
349
|
-
return Dimensions.fromPsbtInputs(psbt).plus(Dimensions.fromOutputs(psbt.getUnsignedTx().outs));
|
|
350
|
-
}
|
|
351
|
-
/**
|
|
352
|
-
* @param dimensions (can be partially defined)
|
|
353
|
-
* @return new dimensions with argument added
|
|
354
|
-
*/
|
|
355
|
-
plus(dimensions) {
|
|
356
|
-
if (typeof dimensions !== 'object') {
|
|
357
|
-
throw new TypeError(`expected argument to be object`);
|
|
358
|
-
}
|
|
359
|
-
if (!(dimensions instanceof Dimensions)) {
|
|
360
|
-
dimensions = new Dimensions(dimensions);
|
|
361
|
-
}
|
|
362
|
-
// Catch instances where we try to initialize Dimensions from partial data using deprecated parameters
|
|
363
|
-
// using only "nOutputs".
|
|
364
|
-
if ('nOutputs' in dimensions) {
|
|
365
|
-
if (!('outputs' in dimensions)) {
|
|
366
|
-
throw new Error('deprecated partial addition: argument has key "nOutputs" but no "outputs"');
|
|
367
|
-
}
|
|
368
|
-
const { outputs, nOutputs } = dimensions;
|
|
369
|
-
if (outputs.count !== nOutputs) {
|
|
370
|
-
throw new Error('deprecated partial addition: inconsistent values for "nOutputs" and "outputs.count"');
|
|
371
|
-
}
|
|
372
|
-
}
|
|
373
|
-
return mapDimensions(this, (key, v) => {
|
|
374
|
-
var _a;
|
|
375
|
-
const w = (_a = dimensions[key]) !== null && _a !== void 0 ? _a : Dimensions.ZERO[key];
|
|
376
|
-
if (key === 'outputs') {
|
|
377
|
-
const vOutputs = v;
|
|
378
|
-
const wOutputs = w;
|
|
379
|
-
return new OutputDimensions({
|
|
380
|
-
count: vOutputs.count + wOutputs.count,
|
|
381
|
-
size: vOutputs.size + wOutputs.size,
|
|
382
|
-
});
|
|
383
|
-
}
|
|
384
|
-
return v + w;
|
|
385
|
-
});
|
|
386
|
-
}
|
|
387
|
-
/**
|
|
388
|
-
* Multiply dimensions by a given factor
|
|
389
|
-
* @param factor - Positive integer
|
|
390
|
-
* @return {Dimensions}
|
|
391
|
-
*/
|
|
392
|
-
times(factor) {
|
|
393
|
-
if (!types_1.PositiveInteger.is(factor)) {
|
|
394
|
-
throw new TypeError(`expected factor to be positive integer`);
|
|
395
|
-
}
|
|
396
|
-
return mapDimensions(this, (key, value) => {
|
|
397
|
-
if (key === 'outputs') {
|
|
398
|
-
const vOutputs = value;
|
|
399
|
-
return {
|
|
400
|
-
count: vOutputs.count * factor,
|
|
401
|
-
size: vOutputs.size * factor,
|
|
402
|
-
};
|
|
403
|
-
}
|
|
404
|
-
return value * factor;
|
|
405
|
-
});
|
|
406
|
-
}
|
|
407
|
-
/**
|
|
408
|
-
* @return Number of total inputs (p2sh, p2shP2wsh and p2wsh)
|
|
409
|
-
* @deprecated use `dimension.nInputs` instead
|
|
410
|
-
*/
|
|
411
|
-
getNInputs() {
|
|
412
|
-
return this.nInputs;
|
|
413
|
-
}
|
|
414
|
-
/**
|
|
415
|
-
* @returns {boolean} true iff dimensions have one or more (p2sh)p2wsh inputs
|
|
416
|
-
*/
|
|
417
|
-
isSegwit() {
|
|
418
|
-
return (this.nP2wshInputs +
|
|
419
|
-
this.nP2shP2wshInputs +
|
|
420
|
-
this.nP2trKeypathInputs +
|
|
421
|
-
this.nP2trScriptPathLevel1Inputs +
|
|
422
|
-
this.nP2trScriptPathLevel2Inputs >
|
|
423
|
-
0);
|
|
424
|
-
}
|
|
425
|
-
/**
|
|
426
|
-
* @return {Number} overhead vsize, based on result isSegwit().
|
|
427
|
-
*/
|
|
428
|
-
getOverheadVSize() {
|
|
429
|
-
return this.isSegwit() ? virtualSizes_1.VirtualSizes.txSegOverheadVSize : virtualSizes_1.VirtualSizes.txOverheadSize;
|
|
430
|
-
}
|
|
431
|
-
/**
|
|
432
|
-
* @returns {number} vsize of inputs, without transaction overhead
|
|
433
|
-
*/
|
|
434
|
-
getInputsVSize() {
|
|
435
|
-
const { txP2shInputSize, txP2shP2wshInputSize, txP2wshInputSize, txP2trKeypathInputSize, txP2trScriptPathLevel1InputSize, txP2trScriptPathLevel2InputSize, txP2shP2pkInputSize, } = virtualSizes_1.VirtualSizes;
|
|
436
|
-
const { nP2shInputs, nP2shP2wshInputs, nP2wshInputs, nP2trKeypathInputs, nP2trScriptPathLevel1Inputs, nP2trScriptPathLevel2Inputs, nP2shP2pkInputs, } = this;
|
|
437
|
-
const size = nP2shInputs * txP2shInputSize +
|
|
438
|
-
nP2shP2wshInputs * txP2shP2wshInputSize +
|
|
439
|
-
nP2wshInputs * txP2wshInputSize +
|
|
440
|
-
nP2trKeypathInputs * txP2trKeypathInputSize +
|
|
441
|
-
nP2shP2pkInputs * txP2shP2pkInputSize +
|
|
442
|
-
nP2trScriptPathLevel1Inputs * txP2trScriptPathLevel1InputSize +
|
|
443
|
-
nP2trScriptPathLevel2Inputs * txP2trScriptPathLevel2InputSize;
|
|
444
|
-
if (Number.isNaN(size)) {
|
|
445
|
-
throw new Error(`invalid size`);
|
|
446
|
-
}
|
|
447
|
-
return size;
|
|
448
|
-
}
|
|
449
|
-
/**
|
|
450
|
-
* @returns {number} return vsize of outputs, without overhead
|
|
451
|
-
*/
|
|
452
|
-
getOutputsVSize() {
|
|
453
|
-
return this.outputs.size;
|
|
454
|
-
}
|
|
455
|
-
/**
|
|
456
|
-
* Estimates the virtual size (1/4 weight) of a signed transaction as sum of
|
|
457
|
-
* overhead vsize, input vsize and output vsize.
|
|
458
|
-
* @returns {Number} The estimated vsize of the transaction dimensions.
|
|
459
|
-
*/
|
|
460
|
-
getVSize() {
|
|
461
|
-
return this.getOverheadVSize() + this.getInputsVSize() + this.getOutputsVSize();
|
|
462
|
-
}
|
|
463
|
-
}
|
|
464
|
-
exports.Dimensions = Dimensions;
|
|
465
|
-
Dimensions.ZERO = Object.freeze(new Dimensions());
|
|
466
|
-
Dimensions.SingleOutput = Object.freeze({
|
|
467
|
-
p2sh: Dimensions.singleOutput(virtualSizes_1.VirtualSizes.txP2shOutputSize),
|
|
468
|
-
p2shP2wsh: Dimensions.singleOutput(virtualSizes_1.VirtualSizes.txP2shP2wshOutputSize),
|
|
469
|
-
p2wsh: Dimensions.singleOutput(virtualSizes_1.VirtualSizes.txP2wshOutputSize),
|
|
470
|
-
p2tr: Dimensions.singleOutput(virtualSizes_1.VirtualSizes.txP2trOutputSize),
|
|
471
|
-
p2pkh: Dimensions.singleOutput(virtualSizes_1.VirtualSizes.txP2pkhOutputSize),
|
|
472
|
-
p2wpkh: Dimensions.singleOutput(virtualSizes_1.VirtualSizes.txP2wpkhOutputSize),
|
|
473
|
-
});
|
|
474
|
-
Dimensions.SingleInput = Object.freeze({
|
|
475
|
-
p2sh: Dimensions.sum({ nP2shInputs: 1 }),
|
|
476
|
-
p2shP2wsh: Dimensions.sum({ nP2shP2wshInputs: 1 }),
|
|
477
|
-
p2wsh: Dimensions.sum({ nP2wshInputs: 1 }),
|
|
478
|
-
p2trKeypath: Dimensions.sum({ nP2trKeypathInputs: 1 }),
|
|
479
|
-
p2trScriptPathLevel1: Dimensions.sum({ nP2trScriptPathLevel1Inputs: 1 }),
|
|
480
|
-
p2trScriptPathLevel2: Dimensions.sum({ nP2trScriptPathLevel2Inputs: 1 }),
|
|
481
|
-
p2shP2pk: Dimensions.sum({ nP2shP2pkInputs: 1 }),
|
|
482
|
-
});
|
|
483
|
-
Dimensions.ASSUME_P2SH = Dimensions.SingleInput.p2sh;
|
|
484
|
-
Dimensions.ASSUME_P2SH_P2WSH = Dimensions.SingleInput.p2shP2wsh;
|
|
485
|
-
Dimensions.ASSUME_P2WSH = Dimensions.SingleInput.p2wsh;
|
|
486
|
-
Dimensions.ASSUME_P2TR_KEYPATH = Dimensions.SingleInput.p2trKeypath;
|
|
487
|
-
Dimensions.ASSUME_P2TR_SCRIPTPATH_LEVEL1 = Dimensions.SingleInput.p2trScriptPathLevel1;
|
|
488
|
-
Dimensions.ASSUME_P2TR_SCRIPTPATH_LEVEL2 = Dimensions.SingleInput.p2trScriptPathLevel2;
|
|
489
|
-
Dimensions.ASSUME_P2SH_P2PK_INPUT = Dimensions.SingleInput.p2shP2pk;
|
|
490
|
-
//# 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
|