@btc-vision/bitcoin 7.0.0-alpha.0 → 7.0.0-alpha.1
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/browser/address.d.ts +1 -1
- package/browser/address.d.ts.map +1 -1
- package/browser/block.d.ts.map +1 -1
- package/browser/crypto.d.ts +1 -1
- package/browser/ecc/context.d.ts +4 -4
- package/browser/ecc/context.d.ts.map +1 -1
- package/browser/ecc/types.d.ts +1 -1
- package/browser/ecc/types.d.ts.map +1 -1
- package/browser/index.d.ts +1 -1
- package/browser/index.d.ts.map +1 -1
- package/browser/index.js +648 -653
- package/browser/io/BinaryReader.d.ts +15 -15
- package/browser/io/BinaryReader.d.ts.map +1 -1
- package/browser/io/BinaryWriter.d.ts +17 -17
- package/browser/io/BinaryWriter.d.ts.map +1 -1
- package/browser/io/MemoryPool.d.ts +20 -20
- package/browser/io/MemoryPool.d.ts.map +1 -1
- package/browser/payments/bip341.d.ts +1 -1
- package/browser/payments/bip341.d.ts.map +1 -1
- package/browser/payments/embed.d.ts +1 -1
- package/browser/payments/embed.d.ts.map +1 -1
- package/browser/payments/p2ms.d.ts.map +1 -1
- package/browser/payments/p2op.d.ts +1 -1
- package/browser/payments/p2op.d.ts.map +1 -1
- package/browser/payments/p2pk.d.ts +1 -1
- package/browser/payments/p2pk.d.ts.map +1 -1
- package/browser/payments/p2pkh.d.ts +1 -1
- package/browser/payments/p2pkh.d.ts.map +1 -1
- package/browser/payments/p2sh.d.ts.map +1 -1
- package/browser/payments/p2tr.d.ts +2 -2
- package/browser/payments/p2tr.d.ts.map +1 -1
- package/browser/payments/p2wpkh.d.ts +1 -1
- package/browser/payments/p2wpkh.d.ts.map +1 -1
- package/browser/payments/p2wsh.d.ts.map +1 -1
- package/browser/payments/types.d.ts +1 -1
- package/browser/payments/types.d.ts.map +1 -1
- package/browser/psbt/bip371.d.ts.map +1 -1
- package/browser/psbt/types.d.ts +2 -2
- package/browser/psbt/types.d.ts.map +1 -1
- package/browser/psbt/validation.d.ts.map +1 -1
- package/browser/psbt.d.ts +1 -1
- package/browser/psbt.d.ts.map +1 -1
- package/browser/script.d.ts.map +1 -1
- package/browser/transaction.d.ts.map +1 -1
- package/browser/types.d.ts +1 -1
- package/browser/types.d.ts.map +1 -1
- package/browser/workers/WorkerSigningPool.d.ts +17 -17
- package/browser/workers/WorkerSigningPool.d.ts.map +1 -1
- package/browser/workers/WorkerSigningPool.node.d.ts +12 -12
- package/browser/workers/WorkerSigningPool.node.d.ts.map +1 -1
- package/browser/workers/psbt-parallel.d.ts +1 -1
- package/browser/workers/psbt-parallel.d.ts.map +1 -1
- package/browser/workers/types.d.ts.map +1 -1
- package/build/address.d.ts +1 -1
- package/build/address.d.ts.map +1 -1
- package/build/address.js +3 -2
- package/build/address.js.map +1 -1
- package/build/block.d.ts.map +1 -1
- package/build/block.js +2 -4
- package/build/block.js.map +1 -1
- package/build/crypto.d.ts +1 -1
- package/build/ecc/context.d.ts +4 -4
- package/build/ecc/context.d.ts.map +1 -1
- package/build/ecc/context.js +7 -7
- package/build/ecc/context.js.map +1 -1
- package/build/ecc/types.d.ts +1 -1
- package/build/ecc/types.d.ts.map +1 -1
- package/build/index.d.ts +1 -1
- package/build/index.d.ts.map +1 -1
- package/build/index.js +2 -2
- package/build/index.js.map +1 -1
- package/build/io/BinaryReader.d.ts +15 -15
- package/build/io/BinaryReader.d.ts.map +1 -1
- package/build/io/BinaryReader.js +17 -17
- package/build/io/BinaryReader.js.map +1 -1
- package/build/io/BinaryWriter.d.ts +17 -17
- package/build/io/BinaryWriter.d.ts.map +1 -1
- package/build/io/BinaryWriter.js +39 -39
- package/build/io/BinaryWriter.js.map +1 -1
- package/build/io/MemoryPool.d.ts +20 -20
- package/build/io/MemoryPool.d.ts.map +1 -1
- package/build/io/MemoryPool.js +28 -28
- package/build/io/MemoryPool.js.map +1 -1
- package/build/payments/bip341.d.ts +1 -2
- package/build/payments/bip341.d.ts.map +1 -1
- package/build/payments/bip341.js +1 -2
- package/build/payments/bip341.js.map +1 -1
- package/build/payments/embed.d.ts +1 -1
- package/build/payments/embed.d.ts.map +1 -1
- package/build/payments/embed.js +14 -14
- package/build/payments/embed.js.map +1 -1
- package/build/payments/p2ms.d.ts.map +1 -1
- package/build/payments/p2ms.js +21 -21
- package/build/payments/p2ms.js.map +1 -1
- package/build/payments/p2op.d.ts +1 -1
- package/build/payments/p2op.d.ts.map +1 -1
- package/build/payments/p2op.js +18 -18
- package/build/payments/p2op.js.map +1 -1
- package/build/payments/p2pk.d.ts +1 -1
- package/build/payments/p2pk.d.ts.map +1 -1
- package/build/payments/p2pk.js +17 -17
- package/build/payments/p2pk.js.map +1 -1
- package/build/payments/p2pkh.d.ts +1 -1
- package/build/payments/p2pkh.d.ts.map +1 -1
- package/build/payments/p2pkh.js +20 -20
- package/build/payments/p2pkh.js.map +1 -1
- package/build/payments/p2sh.d.ts.map +1 -1
- package/build/payments/p2sh.js +22 -20
- package/build/payments/p2sh.js.map +1 -1
- package/build/payments/p2tr.d.ts +2 -2
- package/build/payments/p2tr.d.ts.map +1 -1
- package/build/payments/p2tr.js +23 -23
- package/build/payments/p2tr.js.map +1 -1
- package/build/payments/p2wpkh.d.ts +1 -1
- package/build/payments/p2wpkh.d.ts.map +1 -1
- package/build/payments/p2wpkh.js +20 -20
- package/build/payments/p2wpkh.js.map +1 -1
- package/build/payments/p2wsh.d.ts.map +1 -1
- package/build/payments/p2wsh.js +22 -22
- package/build/payments/p2wsh.js.map +1 -1
- package/build/payments/types.d.ts +1 -1
- package/build/payments/types.d.ts.map +1 -1
- package/build/psbt/bip371.d.ts.map +1 -1
- package/build/psbt/bip371.js +6 -2
- package/build/psbt/bip371.js.map +1 -1
- package/build/psbt/psbtutils.js +1 -1
- package/build/psbt/psbtutils.js.map +1 -1
- package/build/psbt/types.d.ts +2 -2
- package/build/psbt/types.d.ts.map +1 -1
- package/build/psbt/validation.d.ts.map +1 -1
- package/build/psbt/validation.js +1 -1
- package/build/psbt/validation.js.map +1 -1
- package/build/psbt.d.ts +1 -1
- package/build/psbt.d.ts.map +1 -1
- package/build/psbt.js +6 -12
- package/build/psbt.js.map +1 -1
- package/build/script.d.ts.map +1 -1
- package/build/script.js +2 -2
- package/build/script.js.map +1 -1
- package/build/transaction.d.ts.map +1 -1
- package/build/transaction.js +1 -1
- package/build/transaction.js.map +1 -1
- package/build/tsconfig.build.tsbuildinfo +1 -1
- package/build/types.d.ts +1 -1
- package/build/types.d.ts.map +1 -1
- package/build/types.js +3 -9
- package/build/types.js.map +1 -1
- package/build/workers/WorkerSigningPool.d.ts +17 -17
- package/build/workers/WorkerSigningPool.d.ts.map +1 -1
- package/build/workers/WorkerSigningPool.js +25 -25
- package/build/workers/WorkerSigningPool.js.map +1 -1
- package/build/workers/WorkerSigningPool.node.d.ts +12 -12
- package/build/workers/WorkerSigningPool.node.d.ts.map +1 -1
- package/build/workers/WorkerSigningPool.node.js +23 -23
- package/build/workers/WorkerSigningPool.node.js.map +1 -1
- package/build/workers/psbt-parallel.d.ts +1 -1
- package/build/workers/psbt-parallel.d.ts.map +1 -1
- package/build/workers/psbt-parallel.js.map +1 -1
- package/build/workers/types.d.ts.map +1 -1
- package/build/workers/types.js.map +1 -1
- package/package.json +3 -2
- package/src/address.ts +13 -4
- package/src/block.ts +15 -8
- package/src/crypto.ts +1 -1
- package/src/ecc/context.ts +12 -9
- package/src/ecc/types.ts +1 -8
- package/src/index.ts +12 -13
- package/src/io/BinaryReader.ts +18 -18
- package/src/io/BinaryWriter.ts +43 -43
- package/src/io/MemoryPool.ts +32 -32
- package/src/payments/bip341.ts +2 -4
- package/src/payments/embed.ts +18 -18
- package/src/payments/p2ms.ts +32 -25
- package/src/payments/p2op.ts +22 -22
- package/src/payments/p2pk.ts +20 -20
- package/src/payments/p2pkh.ts +25 -25
- package/src/payments/p2sh.ts +30 -27
- package/src/payments/p2tr.ts +31 -31
- package/src/payments/p2wpkh.ts +25 -25
- package/src/payments/p2wsh.ts +27 -27
- package/src/payments/types.ts +1 -1
- package/src/psbt/bip371.ts +7 -3
- package/src/psbt/psbtutils.ts +1 -1
- package/src/psbt/types.ts +2 -9
- package/src/psbt/validation.ts +4 -11
- package/src/psbt.ts +107 -83
- package/src/script.ts +4 -7
- package/src/transaction.ts +9 -6
- package/src/types.ts +15 -17
- package/src/workers/WorkerSigningPool.node.ts +31 -31
- package/src/workers/WorkerSigningPool.ts +35 -39
- package/src/workers/psbt-parallel.ts +2 -7
- package/src/workers/types.ts +5 -1
- package/typedoc.json +29 -0
package/src/psbt.ts
CHANGED
|
@@ -1,8 +1,3 @@
|
|
|
1
|
-
import {
|
|
2
|
-
Psbt as PsbtBase,
|
|
3
|
-
checkForInput,
|
|
4
|
-
checkForOutput,
|
|
5
|
-
} from 'bip174';
|
|
6
1
|
import type {
|
|
7
2
|
Bip32Derivation,
|
|
8
3
|
KeyValue,
|
|
@@ -17,16 +12,16 @@ import type {
|
|
|
17
12
|
Transaction as ITransaction,
|
|
18
13
|
TransactionFromBuffer,
|
|
19
14
|
} from 'bip174';
|
|
20
|
-
import {
|
|
15
|
+
import { checkForInput, checkForOutput, Psbt as PsbtBase } from 'bip174';
|
|
16
|
+
import { clone, equals, fromBase64, fromHex, reverse, toHex } from './io/index.js';
|
|
21
17
|
|
|
22
18
|
import type { BIP32Interface } from '@btc-vision/bip32';
|
|
23
19
|
import type { ECPairInterface } from 'ecpair';
|
|
24
20
|
import { fromOutputScript, isUnknownSegwitVersion, toOutputScript } from './address.js';
|
|
25
21
|
import { bitcoin as btcNetwork } from './networks.js';
|
|
22
|
+
import type { P2SHPayment, P2WSHPayment } from './payments/index.js';
|
|
26
23
|
import * as payments from './payments/index.js';
|
|
27
|
-
import type { P2WSHPayment } from './payments/index.js';
|
|
28
24
|
import { tapleafHash } from './payments/bip341.js';
|
|
29
|
-
import type { P2SHPayment } from './payments/index.js';
|
|
30
25
|
import {
|
|
31
26
|
checkTaprootInputFields,
|
|
32
27
|
checkTaprootOutputFields,
|
|
@@ -35,12 +30,7 @@ import {
|
|
|
35
30
|
tapScriptFinalizer,
|
|
36
31
|
} from './psbt/bip371.js';
|
|
37
32
|
import { toXOnly } from './pubkey.js';
|
|
38
|
-
import {
|
|
39
|
-
isP2TR,
|
|
40
|
-
isP2WPKH,
|
|
41
|
-
pubkeyInScript,
|
|
42
|
-
witnessStackToScriptWitness,
|
|
43
|
-
} from './psbt/psbtutils.js';
|
|
33
|
+
import { isP2TR, isP2WPKH, pubkeyInScript, witnessStackToScriptWitness } from './psbt/psbtutils.js';
|
|
44
34
|
import {
|
|
45
35
|
check32Bit,
|
|
46
36
|
checkCache,
|
|
@@ -64,51 +54,59 @@ import {
|
|
|
64
54
|
sighashTypeToString,
|
|
65
55
|
} from './psbt/utils.js';
|
|
66
56
|
import * as bscript from './script.js';
|
|
67
|
-
import { Transaction } from './transaction.js';
|
|
68
57
|
import type { Output } from './transaction.js';
|
|
69
|
-
import
|
|
70
|
-
|
|
71
|
-
|
|
72
|
-
|
|
73
|
-
|
|
74
|
-
|
|
75
|
-
|
|
76
|
-
|
|
77
|
-
|
|
58
|
+
import { Transaction } from './transaction.js';
|
|
59
|
+
import type {
|
|
60
|
+
Bytes20,
|
|
61
|
+
Bytes32,
|
|
62
|
+
PublicKey,
|
|
63
|
+
Satoshi,
|
|
64
|
+
SchnorrSignature,
|
|
65
|
+
Script,
|
|
66
|
+
Signature,
|
|
67
|
+
XOnlyPublicKey,
|
|
68
|
+
} from './types.js';
|
|
69
|
+
// Import types for internal use
|
|
70
|
+
import type {
|
|
71
|
+
AllScriptType,
|
|
72
|
+
FinalScriptsFunc,
|
|
73
|
+
FinalTaprootScriptsFunc,
|
|
74
|
+
GetScriptReturn,
|
|
75
|
+
HDSigner,
|
|
76
|
+
HDSignerAsync,
|
|
78
77
|
PsbtBaseExtended,
|
|
79
|
-
|
|
80
|
-
PsbtOpts,
|
|
78
|
+
PsbtCache,
|
|
81
79
|
PsbtInputExtended,
|
|
80
|
+
PsbtOpts,
|
|
81
|
+
PsbtOptsOptional,
|
|
82
82
|
PsbtOutputExtended,
|
|
83
83
|
PsbtOutputExtendedAddress,
|
|
84
|
-
|
|
85
|
-
|
|
86
|
-
HDSignerAsync,
|
|
87
|
-
SignerAlternative,
|
|
84
|
+
PsbtTxInput,
|
|
85
|
+
PsbtTxOutput,
|
|
88
86
|
Signer,
|
|
87
|
+
SignerAlternative,
|
|
89
88
|
SignerAsync,
|
|
90
89
|
TaprootHashCheckSigner,
|
|
91
|
-
|
|
90
|
+
TransactionInput,
|
|
91
|
+
TransactionOutput,
|
|
92
92
|
TxCacheNumberKey,
|
|
93
|
-
|
|
94
|
-
AllScriptType,
|
|
95
|
-
GetScriptReturn,
|
|
96
|
-
FinalScriptsFunc,
|
|
97
|
-
FinalTaprootScriptsFunc,
|
|
93
|
+
ValidateSigFunction,
|
|
98
94
|
} from './psbt/types.js';
|
|
99
95
|
|
|
100
|
-
//
|
|
101
|
-
|
|
96
|
+
// Re-export types from the types module
|
|
97
|
+
export type {
|
|
102
98
|
TransactionInput,
|
|
103
99
|
PsbtTxInput,
|
|
104
100
|
TransactionOutput,
|
|
105
101
|
PsbtTxOutput,
|
|
102
|
+
ValidateSigFunction,
|
|
106
103
|
PsbtBaseExtended,
|
|
107
104
|
PsbtOptsOptional,
|
|
108
105
|
PsbtOpts,
|
|
109
106
|
PsbtInputExtended,
|
|
110
107
|
PsbtOutputExtended,
|
|
111
108
|
PsbtOutputExtendedAddress,
|
|
109
|
+
PsbtOutputExtendedScript,
|
|
112
110
|
HDSigner,
|
|
113
111
|
HDSignerAsync,
|
|
114
112
|
SignerAlternative,
|
|
@@ -117,11 +115,11 @@ import type {
|
|
|
117
115
|
TaprootHashCheckSigner,
|
|
118
116
|
PsbtCache,
|
|
119
117
|
TxCacheNumberKey,
|
|
118
|
+
ScriptType,
|
|
120
119
|
AllScriptType,
|
|
121
120
|
GetScriptReturn,
|
|
122
121
|
FinalScriptsFunc,
|
|
123
122
|
FinalTaprootScriptsFunc,
|
|
124
|
-
ValidateSigFunction,
|
|
125
123
|
} from './psbt/types.js';
|
|
126
124
|
|
|
127
125
|
/**
|
|
@@ -405,11 +403,7 @@ export class Psbt {
|
|
|
405
403
|
addOutput(outputData: PsbtOutputExtended, checkPartialSigs: boolean = true): this {
|
|
406
404
|
const hasAddress = 'address' in outputData;
|
|
407
405
|
const hasScript = 'script' in outputData;
|
|
408
|
-
if (
|
|
409
|
-
!outputData ||
|
|
410
|
-
outputData.value === undefined ||
|
|
411
|
-
(!hasAddress && !hasScript)
|
|
412
|
-
) {
|
|
406
|
+
if (!outputData || outputData.value === undefined || (!hasAddress && !hasScript)) {
|
|
413
407
|
throw new Error(
|
|
414
408
|
`Invalid arguments for Psbt.addOutput. ` +
|
|
415
409
|
`Requires single object with at least [script or address] and [value]`,
|
|
@@ -437,7 +431,9 @@ export class Psbt {
|
|
|
437
431
|
|
|
438
432
|
extractTransaction(disableFeeCheck?: boolean, disableOutputChecks?: boolean): Transaction {
|
|
439
433
|
if (disableOutputChecks) {
|
|
440
|
-
(this.data as unknown as { inputs: PsbtInput[] }).inputs = this.data.inputs.filter(
|
|
434
|
+
(this.data as unknown as { inputs: PsbtInput[] }).inputs = this.data.inputs.filter(
|
|
435
|
+
(i) => !i.partialSig,
|
|
436
|
+
);
|
|
441
437
|
}
|
|
442
438
|
|
|
443
439
|
if (!this.data.inputs.every(isFinalized)) throw new Error('Not finalized');
|
|
@@ -516,10 +512,8 @@ export class Psbt {
|
|
|
516
512
|
script,
|
|
517
513
|
inputIndex,
|
|
518
514
|
'input',
|
|
519
|
-
input.redeemScript ||
|
|
520
|
-
|
|
521
|
-
input.witnessScript ||
|
|
522
|
-
redeemFromFinalWitnessScript(input.finalScriptWitness),
|
|
515
|
+
input.redeemScript || redeemFromFinalScriptSig(input.finalScriptSig),
|
|
516
|
+
input.witnessScript || redeemFromFinalWitnessScript(input.finalScriptWitness),
|
|
523
517
|
);
|
|
524
518
|
const type = result.type === 'raw' ? '' : result.type + '-';
|
|
525
519
|
const mainType = classifyScript(result.meaningfulScript);
|
|
@@ -755,7 +749,14 @@ export class Psbt {
|
|
|
755
749
|
|
|
756
750
|
signInputAsync(
|
|
757
751
|
inputIndex: number,
|
|
758
|
-
keyPair:
|
|
752
|
+
keyPair:
|
|
753
|
+
| Signer
|
|
754
|
+
| SignerAlternative
|
|
755
|
+
| SignerAsync
|
|
756
|
+
| HDSigner
|
|
757
|
+
| HDSignerAsync
|
|
758
|
+
| BIP32Interface
|
|
759
|
+
| ECPairInterface,
|
|
759
760
|
sighashTypes?: number[],
|
|
760
761
|
): Promise<void> {
|
|
761
762
|
return Promise.resolve().then(() => {
|
|
@@ -777,7 +778,14 @@ export class Psbt {
|
|
|
777
778
|
|
|
778
779
|
signTaprootInputAsync(
|
|
779
780
|
inputIndex: number,
|
|
780
|
-
keyPair:
|
|
781
|
+
keyPair:
|
|
782
|
+
| Signer
|
|
783
|
+
| SignerAlternative
|
|
784
|
+
| SignerAsync
|
|
785
|
+
| HDSigner
|
|
786
|
+
| HDSignerAsync
|
|
787
|
+
| BIP32Interface
|
|
788
|
+
| ECPairInterface,
|
|
781
789
|
tapLeafHash?: Uint8Array,
|
|
782
790
|
sighashTypes?: number[],
|
|
783
791
|
): Promise<void> {
|
|
@@ -872,16 +880,25 @@ export class Psbt {
|
|
|
872
880
|
checkTaprootHashesForSig(
|
|
873
881
|
inputIndex: number,
|
|
874
882
|
input: PsbtInput,
|
|
875
|
-
keyPair:
|
|
883
|
+
keyPair:
|
|
884
|
+
| Signer
|
|
885
|
+
| SignerAlternative
|
|
886
|
+
| SignerAsync
|
|
887
|
+
| HDSigner
|
|
888
|
+
| HDSignerAsync
|
|
889
|
+
| TaprootHashCheckSigner
|
|
890
|
+
| BIP32Interface
|
|
891
|
+
| ECPairInterface,
|
|
876
892
|
tapLeafHashToSign?: Uint8Array,
|
|
877
893
|
allowedSighashTypes?: number[],
|
|
878
894
|
): { hash: Bytes32; leafHash?: Bytes32 }[] {
|
|
879
895
|
if (!('signSchnorr' in keyPair) || typeof keyPair.signSchnorr !== 'function')
|
|
880
896
|
throw new Error(`Need Schnorr Signer to sign taproot input #${inputIndex}.`);
|
|
881
897
|
|
|
882
|
-
const pubkey =
|
|
883
|
-
|
|
884
|
-
|
|
898
|
+
const pubkey =
|
|
899
|
+
keyPair.publicKey instanceof Uint8Array
|
|
900
|
+
? keyPair.publicKey
|
|
901
|
+
: new Uint8Array(keyPair.publicKey);
|
|
885
902
|
|
|
886
903
|
const hashesForSig = getTaprootHashesForSig(
|
|
887
904
|
inputIndex,
|
|
@@ -894,9 +911,7 @@ export class Psbt {
|
|
|
894
911
|
);
|
|
895
912
|
|
|
896
913
|
if (!hashesForSig || !hashesForSig.length)
|
|
897
|
-
throw new Error(
|
|
898
|
-
`Can not sign for input #${inputIndex} with the key ${toHex(pubkey)}`,
|
|
899
|
-
);
|
|
914
|
+
throw new Error(`Can not sign for input #${inputIndex} with the key ${toHex(pubkey)}`);
|
|
900
915
|
|
|
901
916
|
return hashesForSig;
|
|
902
917
|
}
|
|
@@ -978,9 +993,7 @@ export class Psbt {
|
|
|
978
993
|
throw new Error('No signatures to validate');
|
|
979
994
|
if (typeof validator !== 'function')
|
|
980
995
|
throw new Error('Need validator function to validate signatures');
|
|
981
|
-
const mySigs = pubkey
|
|
982
|
-
? partialSig.filter((sig) => equals(sig.pubkey, pubkey))
|
|
983
|
-
: partialSig;
|
|
996
|
+
const mySigs = pubkey ? partialSig.filter((sig) => equals(sig.pubkey, pubkey)) : partialSig;
|
|
984
997
|
if (mySigs.length < 1) throw new Error('No signatures for this pubkey');
|
|
985
998
|
const results: boolean[] = [];
|
|
986
999
|
let hashCache: Bytes32 | undefined;
|
|
@@ -1066,9 +1079,10 @@ export class Psbt {
|
|
|
1066
1079
|
keyPair: Signer | SignerAlternative | HDSigner | BIP32Interface | ECPairInterface,
|
|
1067
1080
|
sighashTypes: number[] = [Transaction.SIGHASH_ALL],
|
|
1068
1081
|
): this {
|
|
1069
|
-
const pubkey =
|
|
1070
|
-
|
|
1071
|
-
|
|
1082
|
+
const pubkey =
|
|
1083
|
+
keyPair.publicKey instanceof Uint8Array
|
|
1084
|
+
? keyPair.publicKey
|
|
1085
|
+
: new Uint8Array(keyPair.publicKey);
|
|
1072
1086
|
|
|
1073
1087
|
const { hash, sighashType } = getHashAndSighashType(
|
|
1074
1088
|
this.data.inputs,
|
|
@@ -1155,12 +1169,20 @@ export class Psbt {
|
|
|
1155
1169
|
|
|
1156
1170
|
#signInputAsync(
|
|
1157
1171
|
inputIndex: number,
|
|
1158
|
-
keyPair:
|
|
1172
|
+
keyPair:
|
|
1173
|
+
| Signer
|
|
1174
|
+
| SignerAlternative
|
|
1175
|
+
| SignerAsync
|
|
1176
|
+
| HDSigner
|
|
1177
|
+
| HDSignerAsync
|
|
1178
|
+
| BIP32Interface
|
|
1179
|
+
| ECPairInterface,
|
|
1159
1180
|
sighashTypes: number[] = [Transaction.SIGHASH_ALL],
|
|
1160
1181
|
): Promise<void> {
|
|
1161
|
-
const pubkey =
|
|
1162
|
-
|
|
1163
|
-
|
|
1182
|
+
const pubkey =
|
|
1183
|
+
keyPair.publicKey instanceof Uint8Array
|
|
1184
|
+
? keyPair.publicKey
|
|
1185
|
+
: new Uint8Array(keyPair.publicKey);
|
|
1164
1186
|
|
|
1165
1187
|
const { hash, sighashType } = getHashAndSighashType(
|
|
1166
1188
|
this.data.inputs,
|
|
@@ -1187,7 +1209,14 @@ export class Psbt {
|
|
|
1187
1209
|
async #signTaprootInputAsync(
|
|
1188
1210
|
inputIndex: number,
|
|
1189
1211
|
input: PsbtInput,
|
|
1190
|
-
keyPair:
|
|
1212
|
+
keyPair:
|
|
1213
|
+
| Signer
|
|
1214
|
+
| SignerAlternative
|
|
1215
|
+
| SignerAsync
|
|
1216
|
+
| HDSigner
|
|
1217
|
+
| HDSignerAsync
|
|
1218
|
+
| BIP32Interface
|
|
1219
|
+
| ECPairInterface,
|
|
1191
1220
|
tapLeafHash?: Uint8Array,
|
|
1192
1221
|
sighashTypes: number[] = [Transaction.SIGHASH_DEFAULT],
|
|
1193
1222
|
): Promise<void> {
|
|
@@ -1359,12 +1388,14 @@ function hasSigs(neededSigs: number, partialSig?: PartialSig[], pubkeys?: Uint8A
|
|
|
1359
1388
|
|
|
1360
1389
|
function bip32DerivationIsMine(root: HDSigner): (d: Bip32Derivation) => boolean {
|
|
1361
1390
|
return (d: Bip32Derivation): boolean => {
|
|
1362
|
-
const fingerprint =
|
|
1363
|
-
|
|
1364
|
-
|
|
1391
|
+
const fingerprint =
|
|
1392
|
+
root.fingerprint instanceof Uint8Array
|
|
1393
|
+
? root.fingerprint
|
|
1394
|
+
: new Uint8Array(root.fingerprint);
|
|
1365
1395
|
if (!equals(d.masterFingerprint, fingerprint)) return false;
|
|
1366
1396
|
const derivedPubkey = root.derivePath(d.path).publicKey;
|
|
1367
|
-
const pubkey =
|
|
1397
|
+
const pubkey =
|
|
1398
|
+
derivedPubkey instanceof Uint8Array ? derivedPubkey : new Uint8Array(derivedPubkey);
|
|
1368
1399
|
if (!equals(pubkey, d.pubkey)) return false;
|
|
1369
1400
|
return true;
|
|
1370
1401
|
};
|
|
@@ -1570,12 +1601,7 @@ function getHashForSig(
|
|
|
1570
1601
|
const script = meaningfulScript as Script;
|
|
1571
1602
|
|
|
1572
1603
|
if (['p2sh-p2wsh', 'p2wsh'].indexOf(type) >= 0) {
|
|
1573
|
-
hash = unsignedTx.hashForWitnessV0(
|
|
1574
|
-
inputIndex,
|
|
1575
|
-
script,
|
|
1576
|
-
prevout.value,
|
|
1577
|
-
sighashType,
|
|
1578
|
-
);
|
|
1604
|
+
hash = unsignedTx.hashForWitnessV0(inputIndex, script, prevout.value, sighashType);
|
|
1579
1605
|
} else if (isP2WPKH(meaningfulScript)) {
|
|
1580
1606
|
// P2WPKH uses the P2PKH template for prevoutScript when signing
|
|
1581
1607
|
const p2pkhPayment = payments.p2pkh({
|
|
@@ -1647,7 +1673,7 @@ function getPrevoutTaprootKey(
|
|
|
1647
1673
|
cache: PsbtCache,
|
|
1648
1674
|
): XOnlyPublicKey | null {
|
|
1649
1675
|
const { script } = getScriptAndAmountFromUtxo(inputIndex, input, cache);
|
|
1650
|
-
return isP2TR(script) ? script.subarray(2, 34) as XOnlyPublicKey : null;
|
|
1676
|
+
return isP2TR(script) ? (script.subarray(2, 34) as XOnlyPublicKey) : null;
|
|
1651
1677
|
}
|
|
1652
1678
|
|
|
1653
1679
|
function trimTaprootSig(signature: Uint8Array): Uint8Array {
|
|
@@ -1669,9 +1695,7 @@ function getTaprootHashesForSig(
|
|
|
1669
1695
|
checkSighashTypeAllowed(sighashType, allowedSighashTypes);
|
|
1670
1696
|
|
|
1671
1697
|
if (!cache.prevOuts) {
|
|
1672
|
-
const prevOuts = inputs.map((i, index) =>
|
|
1673
|
-
getScriptAndAmountFromUtxo(index, i, cache),
|
|
1674
|
-
);
|
|
1698
|
+
const prevOuts = inputs.map((i, index) => getScriptAndAmountFromUtxo(index, i, cache));
|
|
1675
1699
|
cache.prevOuts = prevOuts;
|
|
1676
1700
|
cache.signingScripts = prevOuts.map((o) => o.script);
|
|
1677
1701
|
cache.values = prevOuts.map((o) => o.value);
|
package/src/script.ts
CHANGED
|
@@ -3,14 +3,15 @@
|
|
|
3
3
|
* @packageDocumentation
|
|
4
4
|
*/
|
|
5
5
|
import * as bip66 from './bip66.js';
|
|
6
|
-
import {
|
|
6
|
+
import { alloc, fromHex, toHex } from './io/index.js';
|
|
7
7
|
import type { Opcodes } from './opcodes.js';
|
|
8
8
|
import { opcodes, REVERSE_OPS } from './opcodes.js';
|
|
9
9
|
import * as pushdata from './push_data.js';
|
|
10
10
|
import * as scriptNumber from './script_number.js';
|
|
11
11
|
import * as scriptSignature from './script_signature.js';
|
|
12
|
-
import
|
|
12
|
+
import { isDefinedHashType } from './script_signature.js';
|
|
13
13
|
import type { Script, Stack } from './types.js';
|
|
14
|
+
import * as types from './types.js';
|
|
14
15
|
|
|
15
16
|
const OP_INT_BASE = opcodes.OP_RESERVED; // OP_1 - 1
|
|
16
17
|
export { opcodes };
|
|
@@ -116,9 +117,7 @@ export function compile(chunks: Uint8Array | Stack): Script {
|
|
|
116
117
|
return buffer as Script;
|
|
117
118
|
}
|
|
118
119
|
|
|
119
|
-
export function decompile(
|
|
120
|
-
buffer: Uint8Array | Stack,
|
|
121
|
-
): Array<number | Uint8Array> | null {
|
|
120
|
+
export function decompile(buffer: Uint8Array | Stack): Array<number | Uint8Array> | null {
|
|
122
121
|
// Already decompiled - return as-is
|
|
123
122
|
if (chunksIsArray(buffer)) return buffer as Array<number | Uint8Array>;
|
|
124
123
|
|
|
@@ -247,8 +246,6 @@ export function isCanonicalPubKey(buffer: Uint8Array): boolean {
|
|
|
247
246
|
return types.isPoint(buffer);
|
|
248
247
|
}
|
|
249
248
|
|
|
250
|
-
import { isDefinedHashType } from './script_signature.js';
|
|
251
|
-
|
|
252
249
|
export function isCanonicalScriptSignature(buffer: Uint8Array): boolean {
|
|
253
250
|
if (!(buffer instanceof Uint8Array)) return false;
|
|
254
251
|
if (!isDefinedHashType(buffer[buffer.length - 1]!)) return false;
|
package/src/transaction.ts
CHANGED
|
@@ -1,4 +1,4 @@
|
|
|
1
|
-
import { BinaryReader, BinaryWriter,
|
|
1
|
+
import { alloc, BinaryReader, BinaryWriter, fromHex, reverse, toHex, varuint } from './io/index.js';
|
|
2
2
|
import * as bcrypto from './crypto.js';
|
|
3
3
|
import * as bscript from './script.js';
|
|
4
4
|
import { opcodes } from './script.js';
|
|
@@ -23,9 +23,7 @@ function vectorSize(someVector: Uint8Array[]): number {
|
|
|
23
23
|
|
|
24
24
|
const EMPTY_BYTES = new Uint8Array(0) as Script;
|
|
25
25
|
const EMPTY_WITNESS: Uint8Array[] = [];
|
|
26
|
-
const ZERO = fromHex(
|
|
27
|
-
'0000000000000000000000000000000000000000000000000000000000000000',
|
|
28
|
-
) as Bytes32;
|
|
26
|
+
const ZERO = fromHex('0000000000000000000000000000000000000000000000000000000000000000') as Bytes32;
|
|
29
27
|
const ONE = fromHex('0000000000000000000000000000000000000000000000000000000000000001') as Bytes32;
|
|
30
28
|
|
|
31
29
|
/** Maximum value for SIGHASH_SINGLE blank outputs (0xFFFFFFFFFFFFFFFF) */
|
|
@@ -493,7 +491,9 @@ export class Transaction {
|
|
|
493
491
|
bufferWriter = new BinaryWriter(
|
|
494
492
|
prevOutScripts.map(varSliceSize).reduce((a, b) => a + b),
|
|
495
493
|
);
|
|
496
|
-
prevOutScripts.forEach((prevOutScript) =>
|
|
494
|
+
prevOutScripts.forEach((prevOutScript) =>
|
|
495
|
+
bufferWriter.writeVarBytes(prevOutScript),
|
|
496
|
+
);
|
|
497
497
|
hashScriptPubKeys = bcrypto.sha256(bufferWriter.finish());
|
|
498
498
|
|
|
499
499
|
bufferWriter = new BinaryWriter(4 * this.ins.length);
|
|
@@ -600,7 +600,10 @@ export class Transaction {
|
|
|
600
600
|
* @param values - Array of previous output values for all inputs
|
|
601
601
|
* @returns Cache object to pass to hashForWitnessV1
|
|
602
602
|
*/
|
|
603
|
-
getTaprootHashCache(
|
|
603
|
+
getTaprootHashCache(
|
|
604
|
+
prevOutScripts: readonly Script[],
|
|
605
|
+
values: readonly Satoshi[],
|
|
606
|
+
): TaprootHashCache {
|
|
604
607
|
// hashPrevouts
|
|
605
608
|
let bufferWriter = new BinaryWriter(36 * this.ins.length);
|
|
606
609
|
for (const txIn of this.ins) {
|
package/src/types.ts
CHANGED
|
@@ -3,7 +3,18 @@
|
|
|
3
3
|
*
|
|
4
4
|
* @packageDocumentation
|
|
5
5
|
*/
|
|
6
|
-
import {
|
|
6
|
+
import { compare, equals, fromHex, isZero } from './io/index.js';
|
|
7
|
+
import type {
|
|
8
|
+
Bytes20,
|
|
9
|
+
Bytes32,
|
|
10
|
+
PrivateKey,
|
|
11
|
+
PublicKey,
|
|
12
|
+
Satoshi,
|
|
13
|
+
SchnorrSignature,
|
|
14
|
+
Script,
|
|
15
|
+
Signature,
|
|
16
|
+
XOnlyPublicKey,
|
|
17
|
+
} from './branded.js';
|
|
7
18
|
|
|
8
19
|
// ============================================================================
|
|
9
20
|
// Branded Types (re-exported from branded.ts to avoid circular dependencies)
|
|
@@ -21,8 +32,6 @@ export type {
|
|
|
21
32
|
Script,
|
|
22
33
|
} from './branded.js';
|
|
23
34
|
|
|
24
|
-
import type { Bytes32, Bytes20, Satoshi, PrivateKey, XOnlyPublicKey, PublicKey, SchnorrSignature, Signature, Script } from './branded.js';
|
|
25
|
-
|
|
26
35
|
// ============================================================================
|
|
27
36
|
// Constants
|
|
28
37
|
// ============================================================================
|
|
@@ -40,20 +49,12 @@ export const TAPLEAF_VERSION_MASK = 0xfe;
|
|
|
40
49
|
// ============================================================================
|
|
41
50
|
|
|
42
51
|
export function isUInt8(value: unknown): value is number {
|
|
43
|
-
return (
|
|
44
|
-
typeof value === 'number' &&
|
|
45
|
-
Number.isInteger(value) &&
|
|
46
|
-
value >= 0 &&
|
|
47
|
-
value <= 0xff
|
|
48
|
-
);
|
|
52
|
+
return typeof value === 'number' && Number.isInteger(value) && value >= 0 && value <= 0xff;
|
|
49
53
|
}
|
|
50
54
|
|
|
51
55
|
export function isUInt32(value: unknown): value is number {
|
|
52
56
|
return (
|
|
53
|
-
typeof value === 'number' &&
|
|
54
|
-
Number.isInteger(value) &&
|
|
55
|
-
value >= 0 &&
|
|
56
|
-
value <= 0xffffffff
|
|
57
|
+
typeof value === 'number' && Number.isInteger(value) && value >= 0 && value <= 0xffffffff
|
|
57
58
|
);
|
|
58
59
|
}
|
|
59
60
|
|
|
@@ -244,10 +245,7 @@ export function assertXOnlyPublicKey(
|
|
|
244
245
|
}
|
|
245
246
|
}
|
|
246
247
|
|
|
247
|
-
export function assertPrivateKey(
|
|
248
|
-
value: unknown,
|
|
249
|
-
name: string,
|
|
250
|
-
): asserts value is PrivateKey {
|
|
248
|
+
export function assertPrivateKey(value: unknown, name: string): asserts value is PrivateKey {
|
|
251
249
|
if (!(value instanceof Uint8Array)) {
|
|
252
250
|
throw new TypeError(`${name} must be Uint8Array, got ${typeof value}`);
|
|
253
251
|
}
|
|
@@ -7,21 +7,21 @@
|
|
|
7
7
|
* @packageDocumentation
|
|
8
8
|
*/
|
|
9
9
|
|
|
10
|
-
import {
|
|
10
|
+
import { isMainThread, Worker } from 'worker_threads';
|
|
11
11
|
import { cpus } from 'os';
|
|
12
12
|
import type {
|
|
13
|
-
|
|
14
|
-
|
|
13
|
+
BatchSigningMessage,
|
|
14
|
+
BatchSigningResultMessage,
|
|
15
|
+
BatchSigningTask,
|
|
15
16
|
ParallelSignerKeyPair,
|
|
16
17
|
ParallelSigningResult,
|
|
18
|
+
PooledWorker,
|
|
17
19
|
SigningResultMessage,
|
|
20
|
+
SigningTask,
|
|
21
|
+
WorkerPoolConfig,
|
|
18
22
|
WorkerResponse,
|
|
19
|
-
BatchSigningMessage,
|
|
20
|
-
BatchSigningTask,
|
|
21
|
-
BatchSigningResultMessage,
|
|
22
|
-
PooledWorker,
|
|
23
23
|
} from './types.js';
|
|
24
|
-
import {
|
|
24
|
+
import { isBatchResult, isWorkerReady, WorkerState } from './types.js';
|
|
25
25
|
|
|
26
26
|
/**
|
|
27
27
|
* ECC library types for Node.js worker.
|
|
@@ -163,29 +163,6 @@ export class NodeWorkerSigningPool {
|
|
|
163
163
|
this.#preserveWorkers = this.#config.preserveWorkers;
|
|
164
164
|
}
|
|
165
165
|
|
|
166
|
-
/**
|
|
167
|
-
* Gets the singleton pool instance.
|
|
168
|
-
*
|
|
169
|
-
* @param config - Optional configuration (only used on first call)
|
|
170
|
-
* @returns The singleton pool instance
|
|
171
|
-
*/
|
|
172
|
-
public static getInstance(config?: NodeWorkerPoolConfig): NodeWorkerSigningPool {
|
|
173
|
-
if (!NodeWorkerSigningPool.#instance) {
|
|
174
|
-
NodeWorkerSigningPool.#instance = new NodeWorkerSigningPool(config);
|
|
175
|
-
}
|
|
176
|
-
return NodeWorkerSigningPool.#instance;
|
|
177
|
-
}
|
|
178
|
-
|
|
179
|
-
/**
|
|
180
|
-
* Resets the singleton instance (for testing).
|
|
181
|
-
*/
|
|
182
|
-
public static resetInstance(): void {
|
|
183
|
-
if (NodeWorkerSigningPool.#instance) {
|
|
184
|
-
NodeWorkerSigningPool.#instance.shutdown().catch(() => {});
|
|
185
|
-
NodeWorkerSigningPool.#instance = null;
|
|
186
|
-
}
|
|
187
|
-
}
|
|
188
|
-
|
|
189
166
|
/**
|
|
190
167
|
* Number of workers in the pool.
|
|
191
168
|
*/
|
|
@@ -214,6 +191,29 @@ export class NodeWorkerSigningPool {
|
|
|
214
191
|
return this.#preserveWorkers;
|
|
215
192
|
}
|
|
216
193
|
|
|
194
|
+
/**
|
|
195
|
+
* Gets the singleton pool instance.
|
|
196
|
+
*
|
|
197
|
+
* @param config - Optional configuration (only used on first call)
|
|
198
|
+
* @returns The singleton pool instance
|
|
199
|
+
*/
|
|
200
|
+
public static getInstance(config?: NodeWorkerPoolConfig): NodeWorkerSigningPool {
|
|
201
|
+
if (!NodeWorkerSigningPool.#instance) {
|
|
202
|
+
NodeWorkerSigningPool.#instance = new NodeWorkerSigningPool(config);
|
|
203
|
+
}
|
|
204
|
+
return NodeWorkerSigningPool.#instance;
|
|
205
|
+
}
|
|
206
|
+
|
|
207
|
+
/**
|
|
208
|
+
* Resets the singleton instance (for testing).
|
|
209
|
+
*/
|
|
210
|
+
public static resetInstance(): void {
|
|
211
|
+
if (NodeWorkerSigningPool.#instance) {
|
|
212
|
+
NodeWorkerSigningPool.#instance.shutdown().catch(() => {});
|
|
213
|
+
NodeWorkerSigningPool.#instance = null;
|
|
214
|
+
}
|
|
215
|
+
}
|
|
216
|
+
|
|
217
217
|
/**
|
|
218
218
|
* Enables worker preservation between signing batches.
|
|
219
219
|
*/
|
|
@@ -31,22 +31,18 @@
|
|
|
31
31
|
*/
|
|
32
32
|
|
|
33
33
|
import type {
|
|
34
|
-
|
|
35
|
-
|
|
34
|
+
BatchSigningMessage,
|
|
35
|
+
BatchSigningResultMessage,
|
|
36
|
+
BatchSigningTask,
|
|
36
37
|
ParallelSignerKeyPair,
|
|
37
38
|
ParallelSigningResult,
|
|
39
|
+
PooledWorker,
|
|
38
40
|
SigningResultMessage,
|
|
41
|
+
SigningTask,
|
|
42
|
+
WorkerPoolConfig,
|
|
39
43
|
WorkerResponse,
|
|
40
|
-
BatchSigningMessage,
|
|
41
|
-
BatchSigningTask,
|
|
42
|
-
BatchSigningResultMessage,
|
|
43
|
-
PooledWorker,
|
|
44
|
-
} from './types.js';
|
|
45
|
-
import {
|
|
46
|
-
WorkerState,
|
|
47
|
-
isBatchResult,
|
|
48
|
-
isWorkerReady,
|
|
49
44
|
} from './types.js';
|
|
45
|
+
import { isBatchResult, isWorkerReady, WorkerState, } from './types.js';
|
|
50
46
|
import { createWorkerBlobUrl, revokeWorkerBlobUrl } from './signing-worker.js';
|
|
51
47
|
|
|
52
48
|
/**
|
|
@@ -151,34 +147,6 @@ export class WorkerSigningPool {
|
|
|
151
147
|
this.#preserveWorkers = this.#config.preserveWorkers;
|
|
152
148
|
}
|
|
153
149
|
|
|
154
|
-
/**
|
|
155
|
-
* Gets the singleton pool instance.
|
|
156
|
-
*
|
|
157
|
-
* @param config - Optional configuration (only used on first call)
|
|
158
|
-
* @returns The singleton pool instance
|
|
159
|
-
*
|
|
160
|
-
* @example
|
|
161
|
-
* ```typescript
|
|
162
|
-
* const pool = WorkerSigningPool.getInstance({ workerCount: 8 });
|
|
163
|
-
* ```
|
|
164
|
-
*/
|
|
165
|
-
public static getInstance(config?: WorkerPoolConfig): WorkerSigningPool {
|
|
166
|
-
if (!WorkerSigningPool.#instance) {
|
|
167
|
-
WorkerSigningPool.#instance = new WorkerSigningPool(config);
|
|
168
|
-
}
|
|
169
|
-
return WorkerSigningPool.#instance;
|
|
170
|
-
}
|
|
171
|
-
|
|
172
|
-
/**
|
|
173
|
-
* Resets the singleton instance (for testing).
|
|
174
|
-
*/
|
|
175
|
-
public static resetInstance(): void {
|
|
176
|
-
if (WorkerSigningPool.#instance) {
|
|
177
|
-
WorkerSigningPool.#instance.shutdown().catch(() => {});
|
|
178
|
-
WorkerSigningPool.#instance = null;
|
|
179
|
-
}
|
|
180
|
-
}
|
|
181
|
-
|
|
182
150
|
/**
|
|
183
151
|
* Number of workers in the pool.
|
|
184
152
|
*/
|
|
@@ -207,6 +175,34 @@ export class WorkerSigningPool {
|
|
|
207
175
|
return this.#preserveWorkers;
|
|
208
176
|
}
|
|
209
177
|
|
|
178
|
+
/**
|
|
179
|
+
* Gets the singleton pool instance.
|
|
180
|
+
*
|
|
181
|
+
* @param config - Optional configuration (only used on first call)
|
|
182
|
+
* @returns The singleton pool instance
|
|
183
|
+
*
|
|
184
|
+
* @example
|
|
185
|
+
* ```typescript
|
|
186
|
+
* const pool = WorkerSigningPool.getInstance({ workerCount: 8 });
|
|
187
|
+
* ```
|
|
188
|
+
*/
|
|
189
|
+
public static getInstance(config?: WorkerPoolConfig): WorkerSigningPool {
|
|
190
|
+
if (!WorkerSigningPool.#instance) {
|
|
191
|
+
WorkerSigningPool.#instance = new WorkerSigningPool(config);
|
|
192
|
+
}
|
|
193
|
+
return WorkerSigningPool.#instance;
|
|
194
|
+
}
|
|
195
|
+
|
|
196
|
+
/**
|
|
197
|
+
* Resets the singleton instance (for testing).
|
|
198
|
+
*/
|
|
199
|
+
public static resetInstance(): void {
|
|
200
|
+
if (WorkerSigningPool.#instance) {
|
|
201
|
+
WorkerSigningPool.#instance.shutdown().catch(() => {});
|
|
202
|
+
WorkerSigningPool.#instance = null;
|
|
203
|
+
}
|
|
204
|
+
}
|
|
205
|
+
|
|
210
206
|
/**
|
|
211
207
|
* Enables worker preservation between signing batches.
|
|
212
208
|
*
|