@bitgo/wasm-utxo 1.30.0 → 1.32.0
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/dist/cjs/js/descriptorWallet/DescriptorMap.d.ts +12 -0
- package/dist/cjs/js/descriptorWallet/DescriptorMap.js +15 -0
- package/dist/cjs/js/descriptorWallet/DescriptorOutput.d.ts +26 -0
- package/dist/cjs/js/descriptorWallet/DescriptorOutput.js +42 -0
- package/dist/cjs/js/descriptorWallet/Output.d.ts +46 -0
- package/dist/cjs/js/descriptorWallet/Output.js +63 -0
- package/dist/cjs/js/descriptorWallet/VirtualSize.d.ts +25 -0
- package/dist/cjs/js/descriptorWallet/VirtualSize.js +112 -0
- package/dist/cjs/js/descriptorWallet/address.d.ts +8 -0
- package/dist/cjs/js/descriptorWallet/address.js +14 -0
- package/dist/cjs/js/descriptorWallet/derive.d.ts +16 -0
- package/dist/cjs/js/descriptorWallet/derive.js +48 -0
- package/dist/cjs/js/descriptorWallet/index.d.ts +43 -0
- package/dist/cjs/js/descriptorWallet/index.js +62 -0
- package/dist/cjs/js/descriptorWallet/parse/PatternMatcher.d.ts +17 -0
- package/dist/cjs/js/descriptorWallet/parse/PatternMatcher.js +67 -0
- package/dist/cjs/js/descriptorWallet/psbt/assertSatisfiable.d.ts +20 -0
- package/dist/cjs/js/descriptorWallet/psbt/assertSatisfiable.js +79 -0
- package/dist/cjs/js/descriptorWallet/psbt/createPsbt.d.ts +41 -0
- package/dist/cjs/js/descriptorWallet/psbt/createPsbt.js +82 -0
- package/dist/cjs/js/descriptorWallet/psbt/findDescriptors.d.ts +57 -0
- package/dist/cjs/js/descriptorWallet/psbt/findDescriptors.js +110 -0
- package/dist/cjs/js/descriptorWallet/psbt/index.d.ts +9 -0
- package/dist/cjs/js/descriptorWallet/psbt/index.js +25 -0
- package/dist/cjs/js/descriptorWallet/psbt/parse.d.ts +62 -0
- package/dist/cjs/js/descriptorWallet/psbt/parse.js +113 -0
- package/dist/cjs/js/descriptorWallet/psbt/sign.d.ts +33 -0
- package/dist/cjs/js/descriptorWallet/psbt/sign.js +55 -0
- package/dist/cjs/js/fixedScriptWallet/BitGoPsbt.d.ts +29 -2
- package/dist/cjs/js/fixedScriptWallet/BitGoPsbt.js +38 -2
- package/dist/cjs/js/fixedScriptWallet/ZcashBitGoPsbt.d.ts +8 -0
- package/dist/cjs/js/fixedScriptWallet/ZcashBitGoPsbt.js +10 -0
- package/dist/cjs/js/index.d.ts +40 -0
- package/dist/cjs/js/index.js +2 -1
- package/dist/cjs/js/transaction.d.ts +49 -3
- package/dist/cjs/js/transaction.js +51 -0
- package/dist/cjs/js/wasm/wasm_utxo.d.ts +180 -0
- package/dist/cjs/js/wasm/wasm_utxo.js +488 -0
- package/dist/cjs/js/wasm/wasm_utxo_bg.wasm +0 -0
- package/dist/cjs/js/wasm/wasm_utxo_bg.wasm.d.ts +74 -53
- package/dist/esm/js/descriptorWallet/DescriptorMap.d.ts +12 -0
- package/dist/esm/js/descriptorWallet/DescriptorMap.js +12 -0
- package/dist/esm/js/descriptorWallet/DescriptorOutput.d.ts +26 -0
- package/dist/esm/js/descriptorWallet/DescriptorOutput.js +36 -0
- package/dist/esm/js/descriptorWallet/Output.d.ts +46 -0
- package/dist/esm/js/descriptorWallet/Output.js +56 -0
- package/dist/esm/js/descriptorWallet/VirtualSize.d.ts +25 -0
- package/dist/esm/js/descriptorWallet/VirtualSize.js +106 -0
- package/dist/esm/js/descriptorWallet/address.d.ts +8 -0
- package/dist/esm/js/descriptorWallet/address.js +10 -0
- package/dist/esm/js/descriptorWallet/derive.d.ts +16 -0
- package/dist/esm/js/descriptorWallet/derive.js +44 -0
- package/dist/esm/js/descriptorWallet/index.d.ts +43 -0
- package/dist/esm/js/descriptorWallet/index.js +46 -0
- package/dist/esm/js/descriptorWallet/parse/PatternMatcher.d.ts +17 -0
- package/dist/esm/js/descriptorWallet/parse/PatternMatcher.js +63 -0
- package/dist/esm/js/descriptorWallet/psbt/assertSatisfiable.d.ts +20 -0
- package/dist/esm/js/descriptorWallet/psbt/assertSatisfiable.js +74 -0
- package/dist/esm/js/descriptorWallet/psbt/createPsbt.d.ts +41 -0
- package/dist/esm/js/descriptorWallet/psbt/createPsbt.js +77 -0
- package/dist/esm/js/descriptorWallet/psbt/findDescriptors.d.ts +57 -0
- package/dist/esm/js/descriptorWallet/psbt/findDescriptors.js +106 -0
- package/dist/esm/js/descriptorWallet/psbt/index.d.ts +9 -0
- package/dist/esm/js/descriptorWallet/psbt/index.js +9 -0
- package/dist/esm/js/descriptorWallet/psbt/parse.d.ts +62 -0
- package/dist/esm/js/descriptorWallet/psbt/parse.js +109 -0
- package/dist/esm/js/descriptorWallet/psbt/sign.d.ts +33 -0
- package/dist/esm/js/descriptorWallet/psbt/sign.js +50 -0
- package/dist/esm/js/fixedScriptWallet/BitGoPsbt.d.ts +29 -2
- package/dist/esm/js/fixedScriptWallet/BitGoPsbt.js +38 -2
- package/dist/esm/js/fixedScriptWallet/ZcashBitGoPsbt.d.ts +8 -0
- package/dist/esm/js/fixedScriptWallet/ZcashBitGoPsbt.js +10 -0
- package/dist/esm/js/index.d.ts +40 -0
- package/dist/esm/js/index.js +1 -0
- package/dist/esm/js/transaction.d.ts +49 -3
- package/dist/esm/js/transaction.js +51 -0
- package/dist/esm/js/wasm/wasm_utxo.d.ts +180 -0
- package/dist/esm/js/wasm/wasm_utxo_bg.js +488 -0
- package/dist/esm/js/wasm/wasm_utxo_bg.wasm +0 -0
- package/dist/esm/js/wasm/wasm_utxo_bg.wasm.d.ts +74 -53
- package/package.json +1 -1
|
@@ -0,0 +1,12 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* DescriptorMap type and utilities.
|
|
3
|
+
* Moved from @bitgo/utxo-core.
|
|
4
|
+
*/
|
|
5
|
+
import { Descriptor } from "../index.js";
|
|
6
|
+
/** Map from descriptor name to descriptor (TypeScript Map) */
|
|
7
|
+
export type DescriptorMap = Map<string, Descriptor>;
|
|
8
|
+
/** Convert an array of descriptor name-value pairs to a descriptor map */
|
|
9
|
+
export declare function toDescriptorMap(descriptors: {
|
|
10
|
+
name: string;
|
|
11
|
+
value: Descriptor | string;
|
|
12
|
+
}[]): DescriptorMap;
|
|
@@ -0,0 +1,15 @@
|
|
|
1
|
+
"use strict";
|
|
2
|
+
Object.defineProperty(exports, "__esModule", { value: true });
|
|
3
|
+
exports.toDescriptorMap = toDescriptorMap;
|
|
4
|
+
/**
|
|
5
|
+
* DescriptorMap type and utilities.
|
|
6
|
+
* Moved from @bitgo/utxo-core.
|
|
7
|
+
*/
|
|
8
|
+
const index_js_1 = require("../index.js");
|
|
9
|
+
/** Convert an array of descriptor name-value pairs to a descriptor map */
|
|
10
|
+
function toDescriptorMap(descriptors) {
|
|
11
|
+
return new Map(descriptors.map((d) => [
|
|
12
|
+
d.name,
|
|
13
|
+
d.value instanceof index_js_1.Descriptor ? d.value : index_js_1.Descriptor.fromStringDetectType(d.value),
|
|
14
|
+
]));
|
|
15
|
+
}
|
|
@@ -0,0 +1,26 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* Descriptor output types and utilities.
|
|
3
|
+
* Moved from @bitgo/utxo-core.
|
|
4
|
+
*/
|
|
5
|
+
import { Descriptor } from "../index.js";
|
|
6
|
+
import { MaxOutput, Output, PrevOutput } from "./Output.js";
|
|
7
|
+
import { DescriptorMap } from "./DescriptorMap.js";
|
|
8
|
+
export type WithDescriptor<T> = T & {
|
|
9
|
+
descriptor: Descriptor;
|
|
10
|
+
};
|
|
11
|
+
export type WithOptDescriptor<T> = T & {
|
|
12
|
+
descriptor?: Descriptor;
|
|
13
|
+
};
|
|
14
|
+
export declare function isInternalOutput<T extends object>(output: T | WithDescriptor<T>): output is WithDescriptor<T>;
|
|
15
|
+
export declare function isExternalOutput<T extends object>(output: T | WithDescriptor<T>): output is T;
|
|
16
|
+
/**
|
|
17
|
+
* @return the sum of the external outputs that are not 'max'
|
|
18
|
+
* @param outputs
|
|
19
|
+
*/
|
|
20
|
+
export declare function getExternalFixedAmount(outputs: WithOptDescriptor<Output | MaxOutput>[]): bigint;
|
|
21
|
+
export type DescriptorWalletOutput = PrevOutput & {
|
|
22
|
+
descriptorName: string;
|
|
23
|
+
descriptorIndex: number | undefined;
|
|
24
|
+
};
|
|
25
|
+
export type DerivedDescriptorWalletOutput = WithDescriptor<PrevOutput>;
|
|
26
|
+
export declare function toDerivedDescriptorWalletOutput(output: DescriptorWalletOutput, descriptorMap: DescriptorMap): DerivedDescriptorWalletOutput;
|
|
@@ -0,0 +1,42 @@
|
|
|
1
|
+
"use strict";
|
|
2
|
+
Object.defineProperty(exports, "__esModule", { value: true });
|
|
3
|
+
exports.isInternalOutput = isInternalOutput;
|
|
4
|
+
exports.isExternalOutput = isExternalOutput;
|
|
5
|
+
exports.getExternalFixedAmount = getExternalFixedAmount;
|
|
6
|
+
exports.toDerivedDescriptorWalletOutput = toDerivedDescriptorWalletOutput;
|
|
7
|
+
/**
|
|
8
|
+
* Descriptor output types and utilities.
|
|
9
|
+
* Moved from @bitgo/utxo-core.
|
|
10
|
+
*/
|
|
11
|
+
const index_js_1 = require("../index.js");
|
|
12
|
+
const Output_js_1 = require("./Output.js");
|
|
13
|
+
const derive_js_1 = require("./derive.js");
|
|
14
|
+
function isInternalOutput(output) {
|
|
15
|
+
return "descriptor" in output && output.descriptor !== undefined;
|
|
16
|
+
}
|
|
17
|
+
function isExternalOutput(output) {
|
|
18
|
+
return !isInternalOutput(output);
|
|
19
|
+
}
|
|
20
|
+
/**
|
|
21
|
+
* @return the sum of the external outputs that are not 'max'
|
|
22
|
+
* @param outputs
|
|
23
|
+
*/
|
|
24
|
+
function getExternalFixedAmount(outputs) {
|
|
25
|
+
return (0, Output_js_1.getFixedOutputSum)(outputs.filter(isExternalOutput));
|
|
26
|
+
}
|
|
27
|
+
function toDerivedDescriptorWalletOutput(output, descriptorMap) {
|
|
28
|
+
const descriptor = descriptorMap.get(output.descriptorName);
|
|
29
|
+
if (!descriptor) {
|
|
30
|
+
throw new Error(`Descriptor not found: ${output.descriptorName}`);
|
|
31
|
+
}
|
|
32
|
+
if (!(descriptor instanceof index_js_1.Descriptor)) {
|
|
33
|
+
throw new Error(`Expected Descriptor instance for ${output.descriptorName}`);
|
|
34
|
+
}
|
|
35
|
+
const descriptorAtIndex = (0, derive_js_1.getDescriptorAtIndexCheckScript)(descriptor, output.descriptorIndex, output.witnessUtxo.script, output.descriptorName);
|
|
36
|
+
return {
|
|
37
|
+
hash: output.hash,
|
|
38
|
+
index: output.index,
|
|
39
|
+
witnessUtxo: output.witnessUtxo,
|
|
40
|
+
descriptor: descriptorAtIndex,
|
|
41
|
+
};
|
|
42
|
+
}
|
|
@@ -0,0 +1,46 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* Output types and utilities for descriptor wallets.
|
|
3
|
+
* Moved from @bitgo/utxo-core.
|
|
4
|
+
*/
|
|
5
|
+
export type Output<TValue = bigint> = {
|
|
6
|
+
script: Uint8Array;
|
|
7
|
+
value: TValue;
|
|
8
|
+
};
|
|
9
|
+
export type MaxOutput = Output<"max">;
|
|
10
|
+
type ValueBigInt = {
|
|
11
|
+
value: bigint;
|
|
12
|
+
};
|
|
13
|
+
type ValueMax = {
|
|
14
|
+
value: "max";
|
|
15
|
+
};
|
|
16
|
+
/**
|
|
17
|
+
* @return true if the output is a max output
|
|
18
|
+
*/
|
|
19
|
+
export declare function isMaxOutput<A extends ValueBigInt, B extends ValueMax>(output: A | B): output is B;
|
|
20
|
+
/**
|
|
21
|
+
* @return the max output if there is one
|
|
22
|
+
* @throws if there are multiple max outputs
|
|
23
|
+
*/
|
|
24
|
+
export declare function getMaxOutput<A extends ValueBigInt, B extends ValueMax>(outputs: (A | B)[]): B | undefined;
|
|
25
|
+
/**
|
|
26
|
+
* @return the sum of the outputs
|
|
27
|
+
*/
|
|
28
|
+
export declare function getOutputSum(outputs: ValueBigInt[]): bigint;
|
|
29
|
+
/**
|
|
30
|
+
* @return the sum of the outputs that are not 'max'
|
|
31
|
+
*/
|
|
32
|
+
export declare function getFixedOutputSum(outputs: (ValueBigInt | ValueMax)[]): bigint;
|
|
33
|
+
/**
|
|
34
|
+
* @param outputs
|
|
35
|
+
* @param params
|
|
36
|
+
* @return the outputs with the 'max' output replaced with the max amount
|
|
37
|
+
*/
|
|
38
|
+
export declare function toFixedOutputs<A extends ValueBigInt, B extends ValueMax>(outputs: (A | B)[], params: {
|
|
39
|
+
maxAmount: bigint;
|
|
40
|
+
}): A[];
|
|
41
|
+
export type PrevOutput = {
|
|
42
|
+
hash: string;
|
|
43
|
+
index: number;
|
|
44
|
+
witnessUtxo: Output;
|
|
45
|
+
};
|
|
46
|
+
export {};
|
|
@@ -0,0 +1,63 @@
|
|
|
1
|
+
"use strict";
|
|
2
|
+
/**
|
|
3
|
+
* Output types and utilities for descriptor wallets.
|
|
4
|
+
* Moved from @bitgo/utxo-core.
|
|
5
|
+
*/
|
|
6
|
+
Object.defineProperty(exports, "__esModule", { value: true });
|
|
7
|
+
exports.isMaxOutput = isMaxOutput;
|
|
8
|
+
exports.getMaxOutput = getMaxOutput;
|
|
9
|
+
exports.getOutputSum = getOutputSum;
|
|
10
|
+
exports.getFixedOutputSum = getFixedOutputSum;
|
|
11
|
+
exports.toFixedOutputs = toFixedOutputs;
|
|
12
|
+
/**
|
|
13
|
+
* @return true if the output is a max output
|
|
14
|
+
*/
|
|
15
|
+
function isMaxOutput(output) {
|
|
16
|
+
return output.value === "max";
|
|
17
|
+
}
|
|
18
|
+
/**
|
|
19
|
+
* @return the max output if there is one
|
|
20
|
+
* @throws if there are multiple max outputs
|
|
21
|
+
*/
|
|
22
|
+
function getMaxOutput(outputs) {
|
|
23
|
+
const max = outputs.filter((isMaxOutput));
|
|
24
|
+
if (max.length === 0) {
|
|
25
|
+
return undefined;
|
|
26
|
+
}
|
|
27
|
+
if (max.length > 1) {
|
|
28
|
+
throw new Error("Multiple max outputs");
|
|
29
|
+
}
|
|
30
|
+
return max[0];
|
|
31
|
+
}
|
|
32
|
+
/**
|
|
33
|
+
* @return the sum of the outputs
|
|
34
|
+
*/
|
|
35
|
+
function getOutputSum(outputs) {
|
|
36
|
+
return outputs.reduce((sum, output) => sum + output.value, 0n);
|
|
37
|
+
}
|
|
38
|
+
/**
|
|
39
|
+
* @return the sum of the outputs that are not 'max'
|
|
40
|
+
*/
|
|
41
|
+
function getFixedOutputSum(outputs) {
|
|
42
|
+
return getOutputSum(outputs.filter((o) => !isMaxOutput(o)));
|
|
43
|
+
}
|
|
44
|
+
/**
|
|
45
|
+
* @param outputs
|
|
46
|
+
* @param params
|
|
47
|
+
* @return the outputs with the 'max' output replaced with the max amount
|
|
48
|
+
*/
|
|
49
|
+
function toFixedOutputs(outputs, params) {
|
|
50
|
+
// assert that there is at most one max output
|
|
51
|
+
const maxOutput = getMaxOutput(outputs);
|
|
52
|
+
return outputs.map((output) => {
|
|
53
|
+
if (isMaxOutput(output)) {
|
|
54
|
+
if (output !== maxOutput) {
|
|
55
|
+
throw new Error("illegal state");
|
|
56
|
+
}
|
|
57
|
+
return { ...output, value: params.maxAmount };
|
|
58
|
+
}
|
|
59
|
+
else {
|
|
60
|
+
return output;
|
|
61
|
+
}
|
|
62
|
+
});
|
|
63
|
+
}
|
|
@@ -0,0 +1,25 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* Virtual size estimation for descriptor wallets.
|
|
3
|
+
* Moved from @bitgo/utxo-core.
|
|
4
|
+
*/
|
|
5
|
+
import { Descriptor, Psbt } from "../index.js";
|
|
6
|
+
import { DescriptorMap } from "./DescriptorMap.js";
|
|
7
|
+
export declare function getInputVSizesForDescriptors(descriptors: DescriptorMap): Record<string, number>;
|
|
8
|
+
export declare function getChangeOutputVSizesForDescriptor(d: Descriptor): {
|
|
9
|
+
inputVSize: number;
|
|
10
|
+
outputVSize: number;
|
|
11
|
+
};
|
|
12
|
+
type InputWithDescriptorName = {
|
|
13
|
+
descriptorName: string;
|
|
14
|
+
};
|
|
15
|
+
type OutputWithScript = {
|
|
16
|
+
script: Uint8Array;
|
|
17
|
+
};
|
|
18
|
+
type Tx<TInput> = {
|
|
19
|
+
inputs: TInput[];
|
|
20
|
+
outputs: OutputWithScript[];
|
|
21
|
+
};
|
|
22
|
+
export declare function getVirtualSize(tx: Tx<Descriptor>): number;
|
|
23
|
+
export declare function getVirtualSize(tx: Tx<InputWithDescriptorName>, descriptors: DescriptorMap): number;
|
|
24
|
+
export declare function getVirtualSizeEstimateForPsbt(psbt: Psbt, descriptorMap: DescriptorMap): number;
|
|
25
|
+
export {};
|
|
@@ -0,0 +1,112 @@
|
|
|
1
|
+
"use strict";
|
|
2
|
+
Object.defineProperty(exports, "__esModule", { value: true });
|
|
3
|
+
exports.getInputVSizesForDescriptors = getInputVSizesForDescriptors;
|
|
4
|
+
exports.getChangeOutputVSizesForDescriptor = getChangeOutputVSizesForDescriptor;
|
|
5
|
+
exports.getVirtualSize = getVirtualSize;
|
|
6
|
+
exports.getVirtualSizeEstimateForPsbt = getVirtualSizeEstimateForPsbt;
|
|
7
|
+
/**
|
|
8
|
+
* Virtual size estimation for descriptor wallets.
|
|
9
|
+
* Moved from @bitgo/utxo-core.
|
|
10
|
+
*/
|
|
11
|
+
const index_js_1 = require("../index.js");
|
|
12
|
+
const Dimensions_js_1 = require("../fixedScriptWallet/Dimensions.js");
|
|
13
|
+
// Transaction overhead for segwit transactions
|
|
14
|
+
// 4 (version) + 1 (marker) + 1 (flag) + 1 (input count) + 1 (output count) + 4 (locktime) = 12
|
|
15
|
+
// Weight units: 4*10 + 2 = 42, vsize = ceil(42/4) = 10.5
|
|
16
|
+
const TX_SEGWIT_OVERHEAD_VSIZE = 10.5;
|
|
17
|
+
function getScriptPubKeyLength(descType) {
|
|
18
|
+
// See https://bitcoinops.org/en/tools/calc-size/
|
|
19
|
+
switch (descType) {
|
|
20
|
+
case "Wpkh":
|
|
21
|
+
// https://github.com/bitcoin/bips/blob/master/bip-0141.mediawiki#p2wpkh
|
|
22
|
+
return 22;
|
|
23
|
+
case "Sh":
|
|
24
|
+
case "ShWsh":
|
|
25
|
+
case "ShWpkh":
|
|
26
|
+
// https://github.com/bitcoin/bips/blob/master/bip-0016.mediawiki#specification
|
|
27
|
+
return 23;
|
|
28
|
+
case "Pkh":
|
|
29
|
+
return 25;
|
|
30
|
+
case "Wsh":
|
|
31
|
+
case "Tr":
|
|
32
|
+
// P2WSH: https://github.com/bitcoin/bips/blob/master/bip-0141.mediawiki#p2wsh
|
|
33
|
+
// P2TR: https://github.com/bitcoin/bips/blob/58ffd93812ff25e87d53d1f202fbb389fdfb85bb/bip-0341.mediawiki#script-validation-rules
|
|
34
|
+
// > A Taproot output is a native SegWit output (see BIP141) with version number 1, and a 32-byte witness program.
|
|
35
|
+
// 32 bytes for the hash, 1 byte for the version, 1 byte for the push opcode
|
|
36
|
+
return 34;
|
|
37
|
+
case "Bare":
|
|
38
|
+
throw new Error("cannot determine scriptPubKey length for Bare descriptor");
|
|
39
|
+
default:
|
|
40
|
+
throw new Error("unexpected descriptor type " + descType);
|
|
41
|
+
}
|
|
42
|
+
}
|
|
43
|
+
function getInputVSizeForDescriptor(descriptor) {
|
|
44
|
+
// FIXME(BTC-1489): this can overestimate the size of the input significantly
|
|
45
|
+
const maxWeight = descriptor.maxWeightToSatisfy();
|
|
46
|
+
const maxVSize = Math.ceil(maxWeight / 4);
|
|
47
|
+
const sizeOpPushdata1 = 1;
|
|
48
|
+
const sizeOpPushdata2 = 2;
|
|
49
|
+
return (
|
|
50
|
+
// inputId
|
|
51
|
+
32 +
|
|
52
|
+
// vOut
|
|
53
|
+
4 +
|
|
54
|
+
// nSequence
|
|
55
|
+
4 +
|
|
56
|
+
// script overhead
|
|
57
|
+
(maxVSize < 255 ? sizeOpPushdata1 : sizeOpPushdata2) +
|
|
58
|
+
// script
|
|
59
|
+
maxVSize);
|
|
60
|
+
}
|
|
61
|
+
function getInputVSizesForDescriptors(descriptors) {
|
|
62
|
+
return Object.fromEntries(Array.from(descriptors.entries()).map(([name, d]) => {
|
|
63
|
+
return [name, getInputVSizeForDescriptor(d)];
|
|
64
|
+
}));
|
|
65
|
+
}
|
|
66
|
+
function getChangeOutputVSizesForDescriptor(d) {
|
|
67
|
+
return {
|
|
68
|
+
inputVSize: getInputVSizeForDescriptor(d),
|
|
69
|
+
outputVSize: getScriptPubKeyLength(d.descType()),
|
|
70
|
+
};
|
|
71
|
+
}
|
|
72
|
+
function getVirtualSize(tx, descriptorMap) {
|
|
73
|
+
const lookup = descriptorMap ? getInputVSizesForDescriptors(descriptorMap) : undefined;
|
|
74
|
+
const inputVSize = tx.inputs.reduce((sum, input) => {
|
|
75
|
+
if (input instanceof index_js_1.Descriptor) {
|
|
76
|
+
return sum + getInputVSizeForDescriptor(input);
|
|
77
|
+
}
|
|
78
|
+
if ("descriptorName" in input) {
|
|
79
|
+
if (!lookup) {
|
|
80
|
+
throw new Error("missing descriptorMap");
|
|
81
|
+
}
|
|
82
|
+
const vsize = lookup[input.descriptorName];
|
|
83
|
+
if (!vsize) {
|
|
84
|
+
throw new Error(`Could not find descriptor ${input.descriptorName}`);
|
|
85
|
+
}
|
|
86
|
+
return sum + vsize;
|
|
87
|
+
}
|
|
88
|
+
throw new Error("unexpected input");
|
|
89
|
+
}, 0);
|
|
90
|
+
const outputVSize = tx.outputs.reduce((sum, o) => {
|
|
91
|
+
// Use the Dimensions class to calculate output vsize
|
|
92
|
+
return sum + Dimensions_js_1.Dimensions.fromOutput({ length: o.script.length }).getOutputVSize();
|
|
93
|
+
}, 0);
|
|
94
|
+
// we will just assume that we have at least one segwit input
|
|
95
|
+
return inputVSize + outputVSize + TX_SEGWIT_OVERHEAD_VSIZE;
|
|
96
|
+
}
|
|
97
|
+
function getVirtualSizeEstimateForPsbt(psbt, descriptorMap) {
|
|
98
|
+
const inputCount = psbt.inputCount();
|
|
99
|
+
const outputCount = psbt.outputCount();
|
|
100
|
+
// Calculate a rough estimate based on descriptor map
|
|
101
|
+
// For a more accurate estimation, we would need to deserialize the PSBT data
|
|
102
|
+
let totalInputVSize = 0;
|
|
103
|
+
for (const descriptor of descriptorMap.values()) {
|
|
104
|
+
totalInputVSize += getInputVSizeForDescriptor(descriptor);
|
|
105
|
+
}
|
|
106
|
+
// Average input size * input count
|
|
107
|
+
const avgInputVSize = descriptorMap.size > 0 ? totalInputVSize / descriptorMap.size : 100; // fallback
|
|
108
|
+
const inputVSize = avgInputVSize * inputCount;
|
|
109
|
+
// Assume P2WPKH outputs (34 bytes each) as a reasonable default
|
|
110
|
+
const outputVSize = outputCount * Dimensions_js_1.Dimensions.fromOutput({ length: 34 }).getOutputVSize();
|
|
111
|
+
return inputVSize + outputVSize + TX_SEGWIT_OVERHEAD_VSIZE;
|
|
112
|
+
}
|
|
@@ -0,0 +1,8 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* Descriptor address generation utilities.
|
|
3
|
+
* Moved from @bitgo/utxo-core.
|
|
4
|
+
*/
|
|
5
|
+
import { Descriptor } from "../index.js";
|
|
6
|
+
import type { CoinName } from "../coinName.js";
|
|
7
|
+
export declare function createScriptPubKeyFromDescriptor(descriptor: Descriptor, index: number | undefined): Uint8Array;
|
|
8
|
+
export declare function createAddressFromDescriptor(descriptor: Descriptor, index: number | undefined, coin: CoinName): string;
|
|
@@ -0,0 +1,14 @@
|
|
|
1
|
+
"use strict";
|
|
2
|
+
Object.defineProperty(exports, "__esModule", { value: true });
|
|
3
|
+
exports.createScriptPubKeyFromDescriptor = createScriptPubKeyFromDescriptor;
|
|
4
|
+
exports.createAddressFromDescriptor = createAddressFromDescriptor;
|
|
5
|
+
const address_js_1 = require("../address.js");
|
|
6
|
+
function createScriptPubKeyFromDescriptor(descriptor, index) {
|
|
7
|
+
if (index === undefined) {
|
|
8
|
+
return descriptor.scriptPubkey();
|
|
9
|
+
}
|
|
10
|
+
return createScriptPubKeyFromDescriptor(descriptor.atDerivationIndex(index), undefined);
|
|
11
|
+
}
|
|
12
|
+
function createAddressFromDescriptor(descriptor, index, coin) {
|
|
13
|
+
return (0, address_js_1.fromOutputScriptWithCoin)(createScriptPubKeyFromDescriptor(descriptor, index), coin);
|
|
14
|
+
}
|
|
@@ -0,0 +1,16 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* Descriptor derivation utilities.
|
|
3
|
+
* Moved from @bitgo/utxo-core.
|
|
4
|
+
*/
|
|
5
|
+
import { Descriptor } from "../index.js";
|
|
6
|
+
/**
|
|
7
|
+
* Get a descriptor at a specific derivation index.
|
|
8
|
+
* For wildcard descriptors (containing '*'), the index is required and used for derivation.
|
|
9
|
+
* For definite descriptors (not containing '*'), no index should be provided.
|
|
10
|
+
* @param descriptor - The descriptor to derive from
|
|
11
|
+
* @param index - The derivation index for wildcard descriptors
|
|
12
|
+
* @returns A new descriptor at the specified index for wildcard descriptors, or the original descriptor for definite ones
|
|
13
|
+
* @throws {Error} If index is undefined for a wildcard descriptor or if index is provided for a definite descriptor
|
|
14
|
+
*/
|
|
15
|
+
export declare function getDescriptorAtIndex(descriptor: Descriptor, index: number | undefined): Descriptor;
|
|
16
|
+
export declare function getDescriptorAtIndexCheckScript(descriptor: Descriptor, index: number | undefined, script: Uint8Array, descriptorString?: string): Descriptor;
|
|
@@ -0,0 +1,48 @@
|
|
|
1
|
+
"use strict";
|
|
2
|
+
Object.defineProperty(exports, "__esModule", { value: true });
|
|
3
|
+
exports.getDescriptorAtIndex = getDescriptorAtIndex;
|
|
4
|
+
exports.getDescriptorAtIndexCheckScript = getDescriptorAtIndexCheckScript;
|
|
5
|
+
/**
|
|
6
|
+
* Descriptor derivation utilities.
|
|
7
|
+
* Moved from @bitgo/utxo-core.
|
|
8
|
+
*/
|
|
9
|
+
const index_js_1 = require("../index.js");
|
|
10
|
+
/**
|
|
11
|
+
* Get a descriptor at a specific derivation index.
|
|
12
|
+
* For wildcard descriptors (containing '*'), the index is required and used for derivation.
|
|
13
|
+
* For definite descriptors (not containing '*'), no index should be provided.
|
|
14
|
+
* @param descriptor - The descriptor to derive from
|
|
15
|
+
* @param index - The derivation index for wildcard descriptors
|
|
16
|
+
* @returns A new descriptor at the specified index for wildcard descriptors, or the original descriptor for definite ones
|
|
17
|
+
* @throws {Error} If index is undefined for a wildcard descriptor or if index is provided for a definite descriptor
|
|
18
|
+
*/
|
|
19
|
+
function getDescriptorAtIndex(descriptor, index) {
|
|
20
|
+
if (!(descriptor instanceof index_js_1.Descriptor)) {
|
|
21
|
+
throw new Error("Expected Descriptor instance");
|
|
22
|
+
}
|
|
23
|
+
index_js_1.Descriptor.fromString(descriptor.toString(), "derivable");
|
|
24
|
+
descriptor = index_js_1.Descriptor.fromStringDetectType(descriptor.toString());
|
|
25
|
+
if (descriptor.hasWildcard()) {
|
|
26
|
+
if (index === undefined) {
|
|
27
|
+
throw new Error("Derivable descriptor requires an index");
|
|
28
|
+
}
|
|
29
|
+
return descriptor.atDerivationIndex(index);
|
|
30
|
+
}
|
|
31
|
+
else {
|
|
32
|
+
if (index !== undefined) {
|
|
33
|
+
throw new Error("Definite descriptor cannot be derived with index");
|
|
34
|
+
}
|
|
35
|
+
return descriptor;
|
|
36
|
+
}
|
|
37
|
+
}
|
|
38
|
+
function getDescriptorAtIndexCheckScript(descriptor, index, script, descriptorString = descriptor.toString()) {
|
|
39
|
+
if (!(descriptor instanceof index_js_1.Descriptor)) {
|
|
40
|
+
throw new Error("Expected Descriptor instance");
|
|
41
|
+
}
|
|
42
|
+
const descriptorAtIndex = getDescriptorAtIndex(descriptor, index);
|
|
43
|
+
const expectedScript = descriptorAtIndex.scriptPubkey();
|
|
44
|
+
if (script.length !== expectedScript.length || !script.every((b, i) => b === expectedScript[i])) {
|
|
45
|
+
throw new Error(`Script mismatch: descriptor ${descriptorString}`);
|
|
46
|
+
}
|
|
47
|
+
return descriptorAtIndex;
|
|
48
|
+
}
|
|
@@ -0,0 +1,43 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* Descriptor wallet utilities.
|
|
3
|
+
* Moved from @bitgo/utxo-core.
|
|
4
|
+
*
|
|
5
|
+
* This module provides descriptor wallet functionality using wasm-utxo directly,
|
|
6
|
+
* eliminating the need for utxo-lib in descriptor wallet code paths.
|
|
7
|
+
*
|
|
8
|
+
* ## Migration from utxo-core
|
|
9
|
+
*
|
|
10
|
+
* If you were previously importing from `@bitgo/utxo-core/descriptor`, you should
|
|
11
|
+
* now import from `@bitgo/wasm-utxo`:
|
|
12
|
+
*
|
|
13
|
+
* ```typescript
|
|
14
|
+
* // Before (deprecated)
|
|
15
|
+
* import { DescriptorMap, createPsbt } from '@bitgo/utxo-core/descriptor';
|
|
16
|
+
*
|
|
17
|
+
* // After
|
|
18
|
+
* import { descriptorWallet } from '@bitgo/wasm-utxo';
|
|
19
|
+
* const { DescriptorMap, createPsbt } = descriptorWallet;
|
|
20
|
+
* ```
|
|
21
|
+
*
|
|
22
|
+
* @see {@link ./MIGRATION.md} for detailed migration instructions.
|
|
23
|
+
*
|
|
24
|
+
* @remarks
|
|
25
|
+
* The following imports from `@bitgo/utxo-core/descriptor` are now deprecated
|
|
26
|
+
* and should use this module instead:
|
|
27
|
+
* - DescriptorMap, toDescriptorMap
|
|
28
|
+
* - createPsbt, parse, findDescriptorForInput, findDescriptorForOutput
|
|
29
|
+
* - getDescriptorAtIndex, getDescriptorAtIndexCheckScript
|
|
30
|
+
* - createScriptPubKeyFromDescriptor, createAddressFromDescriptor
|
|
31
|
+
* - getVirtualSize, getVirtualSizeEstimateForPsbt
|
|
32
|
+
* - signWithKey, getNewSignatureCount
|
|
33
|
+
* - assertSatisfiable, getRequiredLocktime
|
|
34
|
+
* - PatternMatcher
|
|
35
|
+
*/
|
|
36
|
+
export * from "./Output.js";
|
|
37
|
+
export * from "./DescriptorMap.js";
|
|
38
|
+
export * from "./derive.js";
|
|
39
|
+
export * from "./DescriptorOutput.js";
|
|
40
|
+
export * from "./address.js";
|
|
41
|
+
export * from "./VirtualSize.js";
|
|
42
|
+
export * from "./psbt/index.js";
|
|
43
|
+
export * from "./parse/PatternMatcher.js";
|
|
@@ -0,0 +1,62 @@
|
|
|
1
|
+
"use strict";
|
|
2
|
+
/**
|
|
3
|
+
* Descriptor wallet utilities.
|
|
4
|
+
* Moved from @bitgo/utxo-core.
|
|
5
|
+
*
|
|
6
|
+
* This module provides descriptor wallet functionality using wasm-utxo directly,
|
|
7
|
+
* eliminating the need for utxo-lib in descriptor wallet code paths.
|
|
8
|
+
*
|
|
9
|
+
* ## Migration from utxo-core
|
|
10
|
+
*
|
|
11
|
+
* If you were previously importing from `@bitgo/utxo-core/descriptor`, you should
|
|
12
|
+
* now import from `@bitgo/wasm-utxo`:
|
|
13
|
+
*
|
|
14
|
+
* ```typescript
|
|
15
|
+
* // Before (deprecated)
|
|
16
|
+
* import { DescriptorMap, createPsbt } from '@bitgo/utxo-core/descriptor';
|
|
17
|
+
*
|
|
18
|
+
* // After
|
|
19
|
+
* import { descriptorWallet } from '@bitgo/wasm-utxo';
|
|
20
|
+
* const { DescriptorMap, createPsbt } = descriptorWallet;
|
|
21
|
+
* ```
|
|
22
|
+
*
|
|
23
|
+
* @see {@link ./MIGRATION.md} for detailed migration instructions.
|
|
24
|
+
*
|
|
25
|
+
* @remarks
|
|
26
|
+
* The following imports from `@bitgo/utxo-core/descriptor` are now deprecated
|
|
27
|
+
* and should use this module instead:
|
|
28
|
+
* - DescriptorMap, toDescriptorMap
|
|
29
|
+
* - createPsbt, parse, findDescriptorForInput, findDescriptorForOutput
|
|
30
|
+
* - getDescriptorAtIndex, getDescriptorAtIndexCheckScript
|
|
31
|
+
* - createScriptPubKeyFromDescriptor, createAddressFromDescriptor
|
|
32
|
+
* - getVirtualSize, getVirtualSizeEstimateForPsbt
|
|
33
|
+
* - signWithKey, getNewSignatureCount
|
|
34
|
+
* - assertSatisfiable, getRequiredLocktime
|
|
35
|
+
* - PatternMatcher
|
|
36
|
+
*/
|
|
37
|
+
var __createBinding = (this && this.__createBinding) || (Object.create ? (function(o, m, k, k2) {
|
|
38
|
+
if (k2 === undefined) k2 = k;
|
|
39
|
+
var desc = Object.getOwnPropertyDescriptor(m, k);
|
|
40
|
+
if (!desc || ("get" in desc ? !m.__esModule : desc.writable || desc.configurable)) {
|
|
41
|
+
desc = { enumerable: true, get: function() { return m[k]; } };
|
|
42
|
+
}
|
|
43
|
+
Object.defineProperty(o, k2, desc);
|
|
44
|
+
}) : (function(o, m, k, k2) {
|
|
45
|
+
if (k2 === undefined) k2 = k;
|
|
46
|
+
o[k2] = m[k];
|
|
47
|
+
}));
|
|
48
|
+
var __exportStar = (this && this.__exportStar) || function(m, exports) {
|
|
49
|
+
for (var p in m) if (p !== "default" && !Object.prototype.hasOwnProperty.call(exports, p)) __createBinding(exports, m, p);
|
|
50
|
+
};
|
|
51
|
+
Object.defineProperty(exports, "__esModule", { value: true });
|
|
52
|
+
// Core types and utilities
|
|
53
|
+
__exportStar(require("./Output.js"), exports);
|
|
54
|
+
__exportStar(require("./DescriptorMap.js"), exports);
|
|
55
|
+
__exportStar(require("./derive.js"), exports);
|
|
56
|
+
__exportStar(require("./DescriptorOutput.js"), exports);
|
|
57
|
+
__exportStar(require("./address.js"), exports);
|
|
58
|
+
__exportStar(require("./VirtualSize.js"), exports);
|
|
59
|
+
// PSBT utilities
|
|
60
|
+
__exportStar(require("./psbt/index.js"), exports);
|
|
61
|
+
// Pattern matching
|
|
62
|
+
__exportStar(require("./parse/PatternMatcher.js"), exports);
|
|
@@ -0,0 +1,17 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* Pattern matching utilities for descriptor ASTs.
|
|
3
|
+
* Moved from @bitgo/utxo-core.
|
|
4
|
+
*/
|
|
5
|
+
export type PatternVar = {
|
|
6
|
+
$var: string;
|
|
7
|
+
};
|
|
8
|
+
export type Pattern = PatternVar | string | number | {
|
|
9
|
+
[key: string]: Pattern | Pattern[];
|
|
10
|
+
} | Pattern[];
|
|
11
|
+
export type ExtractedVars = Record<string, unknown>;
|
|
12
|
+
export declare class PatternMatcher {
|
|
13
|
+
match(node: unknown, pattern: Pattern): ExtractedVars | null;
|
|
14
|
+
private matchNode;
|
|
15
|
+
private isPatternVar;
|
|
16
|
+
private deepEqual;
|
|
17
|
+
}
|
|
@@ -0,0 +1,67 @@
|
|
|
1
|
+
"use strict";
|
|
2
|
+
/**
|
|
3
|
+
* Pattern matching utilities for descriptor ASTs.
|
|
4
|
+
* Moved from @bitgo/utxo-core.
|
|
5
|
+
*/
|
|
6
|
+
Object.defineProperty(exports, "__esModule", { value: true });
|
|
7
|
+
exports.PatternMatcher = void 0;
|
|
8
|
+
class PatternMatcher {
|
|
9
|
+
match(node, pattern) {
|
|
10
|
+
const vars = {};
|
|
11
|
+
return this.matchNode(node, pattern, vars) ? vars : null;
|
|
12
|
+
}
|
|
13
|
+
matchNode(node, pattern, vars) {
|
|
14
|
+
// Variable placeholder
|
|
15
|
+
if (this.isPatternVar(pattern)) {
|
|
16
|
+
const varName = pattern.$var;
|
|
17
|
+
if (varName in vars) {
|
|
18
|
+
return this.deepEqual(vars[varName], node);
|
|
19
|
+
}
|
|
20
|
+
vars[varName] = node;
|
|
21
|
+
return true;
|
|
22
|
+
}
|
|
23
|
+
// Primitive values
|
|
24
|
+
if (typeof node !== typeof pattern)
|
|
25
|
+
return false;
|
|
26
|
+
if (typeof node === "string" || typeof node === "number") {
|
|
27
|
+
return node === pattern;
|
|
28
|
+
}
|
|
29
|
+
// Arrays
|
|
30
|
+
if (Array.isArray(node) && Array.isArray(pattern)) {
|
|
31
|
+
return (node.length === pattern.length &&
|
|
32
|
+
node.every((item, i) => this.matchNode(item, pattern[i], vars)));
|
|
33
|
+
}
|
|
34
|
+
// Objects
|
|
35
|
+
if (typeof node === "object" &&
|
|
36
|
+
typeof pattern === "object" &&
|
|
37
|
+
node !== null &&
|
|
38
|
+
pattern !== null) {
|
|
39
|
+
const nodeKeys = Object.keys(node);
|
|
40
|
+
const patternKeys = Object.keys(pattern);
|
|
41
|
+
return (nodeKeys.length === patternKeys.length &&
|
|
42
|
+
nodeKeys.every((key) => patternKeys.includes(key) &&
|
|
43
|
+
this.matchNode(node[key], pattern[key], vars)));
|
|
44
|
+
}
|
|
45
|
+
return false;
|
|
46
|
+
}
|
|
47
|
+
isPatternVar(value) {
|
|
48
|
+
return value !== null && typeof value === "object" && "$var" in value;
|
|
49
|
+
}
|
|
50
|
+
deepEqual(a, b) {
|
|
51
|
+
if (a === b)
|
|
52
|
+
return true;
|
|
53
|
+
if (typeof a !== typeof b)
|
|
54
|
+
return false;
|
|
55
|
+
if (Array.isArray(a) && Array.isArray(b)) {
|
|
56
|
+
return a.length === b.length && a.every((item, i) => this.deepEqual(item, b[i]));
|
|
57
|
+
}
|
|
58
|
+
if (typeof a === "object" && a !== null && typeof b === "object" && b !== null) {
|
|
59
|
+
const keysA = Object.keys(a);
|
|
60
|
+
const keysB = Object.keys(b);
|
|
61
|
+
return (keysA.length === keysB.length &&
|
|
62
|
+
keysA.every((key) => this.deepEqual(a[key], b[key])));
|
|
63
|
+
}
|
|
64
|
+
return false;
|
|
65
|
+
}
|
|
66
|
+
}
|
|
67
|
+
exports.PatternMatcher = PatternMatcher;
|
|
@@ -0,0 +1,20 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* Helpers for testing satisfiability of descriptors in PSBTs.
|
|
3
|
+
*
|
|
4
|
+
* They are mostly a debugging aid - if an input cannot be satisified, the `finalizePsbt()` method will fail, but
|
|
5
|
+
* the error message is pretty vague.
|
|
6
|
+
*
|
|
7
|
+
* The methods here have the goal of catching certain cases earlier and with a better error message.
|
|
8
|
+
*
|
|
9
|
+
* The goal is not an exhaustive check, but to catch common mistakes.
|
|
10
|
+
*
|
|
11
|
+
* Moved from @bitgo/utxo-core.
|
|
12
|
+
*/
|
|
13
|
+
import { Descriptor, Psbt } from "../../index.js";
|
|
14
|
+
export declare const FINAL_SEQUENCE = 4294967295;
|
|
15
|
+
/**
|
|
16
|
+
* Get the required locktime for a descriptor.
|
|
17
|
+
* @param descriptor
|
|
18
|
+
*/
|
|
19
|
+
export declare function getRequiredLocktime(descriptor: unknown): number | undefined;
|
|
20
|
+
export declare function assertSatisfiable(psbt: Psbt, _inputIndex: number, descriptor: Descriptor): void;
|