@btc-vision/bitcoin 7.0.0-alpha.2 → 7.0.0-alpha.3
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/README.md +334 -161
- package/browser/ecc/context.d.ts +22 -21
- package/browser/ecc/context.d.ts.map +1 -1
- package/browser/ecc/index.d.ts +1 -1
- package/browser/ecc/index.d.ts.map +1 -1
- package/browser/ecc/types.d.ts +10 -123
- package/browser/ecc/types.d.ts.map +1 -1
- package/browser/index.d.ts +2 -2
- package/browser/index.d.ts.map +1 -1
- package/browser/index.js +5790 -4062
- package/browser/payments/p2tr.d.ts.map +1 -1
- package/browser/psbt/types.d.ts +2 -68
- package/browser/psbt/types.d.ts.map +1 -1
- package/browser/psbt.d.ts +9 -11
- package/browser/psbt.d.ts.map +1 -1
- package/browser/types.d.ts +1 -1
- package/browser/types.d.ts.map +1 -1
- package/build/ecc/context.d.ts +22 -21
- package/build/ecc/context.d.ts.map +1 -1
- package/build/ecc/context.js +19 -114
- package/build/ecc/context.js.map +1 -1
- package/build/ecc/index.d.ts +1 -1
- package/build/ecc/index.d.ts.map +1 -1
- package/build/ecc/types.d.ts +7 -126
- package/build/ecc/types.d.ts.map +1 -1
- package/build/ecc/types.js +4 -1
- package/build/ecc/types.js.map +1 -1
- package/build/index.d.ts +2 -2
- package/build/index.d.ts.map +1 -1
- package/build/index.js.map +1 -1
- package/build/payments/p2tr.d.ts.map +1 -1
- package/build/payments/p2tr.js +2 -3
- package/build/payments/p2tr.js.map +1 -1
- package/build/psbt/types.d.ts +2 -68
- package/build/psbt/types.d.ts.map +1 -1
- package/build/psbt.d.ts +9 -11
- package/build/psbt.d.ts.map +1 -1
- package/build/psbt.js +38 -53
- package/build/psbt.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/package.json +4 -4
- package/src/ecc/context.ts +26 -147
- package/src/ecc/index.ts +2 -2
- package/src/ecc/types.ts +7 -138
- package/src/index.ts +1 -2
- package/src/payments/p2tr.ts +2 -2
- package/src/psbt/types.ts +2 -84
- package/src/psbt.ts +63 -121
- package/src/types.ts +1 -1
package/src/psbt.ts
CHANGED
|
@@ -10,7 +10,6 @@ import type {
|
|
|
10
10
|
import { checkForInput, checkForOutput, Psbt as PsbtBase } from 'bip174';
|
|
11
11
|
import { clone, equals, fromBase64, fromHex, toHex } from './io/index.js';
|
|
12
12
|
|
|
13
|
-
import type { BIP32Interface } from '@btc-vision/bip32';
|
|
14
13
|
import { fromOutputScript, toOutputScript } from './address.js';
|
|
15
14
|
import { bitcoin as btcNetwork } from './networks.js';
|
|
16
15
|
import * as payments from './payments/index.js';
|
|
@@ -41,7 +40,6 @@ import type {
|
|
|
41
40
|
PsbtTxInput,
|
|
42
41
|
PsbtTxOutput,
|
|
43
42
|
Signer,
|
|
44
|
-
SignerAlternative,
|
|
45
43
|
SignerAsync,
|
|
46
44
|
TaprootHashCheckSigner,
|
|
47
45
|
ValidateSigFunction,
|
|
@@ -67,7 +65,6 @@ import {
|
|
|
67
65
|
} from './psbt/validation.js';
|
|
68
66
|
import { checkInvalidP2WSH, classifyScript, getMeaningfulScript, range } from './psbt/utils.js';
|
|
69
67
|
import { witnessStackToScriptWitness } from './psbt/psbtutils.js';
|
|
70
|
-
import type { UniversalSigner } from '@btc-vision/ecpair';
|
|
71
68
|
|
|
72
69
|
// Re-export types from the types module
|
|
73
70
|
export type {
|
|
@@ -84,7 +81,6 @@ export type {
|
|
|
84
81
|
PsbtOutputExtendedScript,
|
|
85
82
|
HDSigner,
|
|
86
83
|
HDSignerAsync,
|
|
87
|
-
SignerAlternative,
|
|
88
84
|
Signer,
|
|
89
85
|
SignerAsync,
|
|
90
86
|
TaprootHashCheckSigner,
|
|
@@ -649,7 +645,7 @@ export class Psbt {
|
|
|
649
645
|
}
|
|
650
646
|
|
|
651
647
|
public signAllInputs(
|
|
652
|
-
keyPair: Signer |
|
|
648
|
+
keyPair: Signer | HDSigner,
|
|
653
649
|
sighashTypes?: number[],
|
|
654
650
|
): this {
|
|
655
651
|
if (!keyPair || !keyPair.publicKey) throw new Error('Need Signer to sign input');
|
|
@@ -670,7 +666,7 @@ export class Psbt {
|
|
|
670
666
|
}
|
|
671
667
|
|
|
672
668
|
public async signAllInputsAsync(
|
|
673
|
-
keyPair: Signer |
|
|
669
|
+
keyPair: Signer | SignerAsync | HDSigner | HDSignerAsync,
|
|
674
670
|
sighashTypes?: number[],
|
|
675
671
|
): Promise<void> {
|
|
676
672
|
if (!keyPair || !keyPair.publicKey) throw new Error('Need Signer to sign input');
|
|
@@ -697,7 +693,7 @@ export class Psbt {
|
|
|
697
693
|
|
|
698
694
|
public signInput(
|
|
699
695
|
inputIndex: number,
|
|
700
|
-
keyPair: Signer |
|
|
696
|
+
keyPair: Signer | HDSigner,
|
|
701
697
|
sighashTypes?: number[],
|
|
702
698
|
): this {
|
|
703
699
|
if (!keyPair || !keyPair.publicKey) {
|
|
@@ -714,7 +710,7 @@ export class Psbt {
|
|
|
714
710
|
|
|
715
711
|
public signTaprootInput(
|
|
716
712
|
inputIndex: number,
|
|
717
|
-
keyPair: Signer |
|
|
713
|
+
keyPair: Signer | HDSigner,
|
|
718
714
|
tapLeafHashToSign?: Uint8Array,
|
|
719
715
|
sighashTypes?: number[],
|
|
720
716
|
): this {
|
|
@@ -736,63 +732,45 @@ export class Psbt {
|
|
|
736
732
|
throw new Error(`Input #${inputIndex} is not of type Taproot.`);
|
|
737
733
|
}
|
|
738
734
|
|
|
739
|
-
public signInputAsync(
|
|
735
|
+
public async signInputAsync(
|
|
740
736
|
inputIndex: number,
|
|
741
|
-
keyPair:
|
|
742
|
-
| Signer
|
|
743
|
-
| SignerAlternative
|
|
744
|
-
| SignerAsync
|
|
745
|
-
| HDSigner
|
|
746
|
-
| HDSignerAsync
|
|
747
|
-
| BIP32Interface
|
|
748
|
-
| UniversalSigner,
|
|
737
|
+
keyPair: Signer | SignerAsync | HDSigner | HDSignerAsync,
|
|
749
738
|
sighashTypes?: number[],
|
|
750
739
|
): Promise<void> {
|
|
751
|
-
|
|
752
|
-
|
|
753
|
-
|
|
754
|
-
|
|
755
|
-
|
|
756
|
-
|
|
757
|
-
|
|
758
|
-
|
|
759
|
-
|
|
760
|
-
|
|
761
|
-
|
|
762
|
-
|
|
763
|
-
|
|
764
|
-
return this.#signInputAsync(inputIndex, keyPair, sighashTypes);
|
|
765
|
-
});
|
|
740
|
+
if (!keyPair || !keyPair.publicKey) throw new Error('Need Signer to sign input');
|
|
741
|
+
|
|
742
|
+
const input = checkForInput(this.data.inputs, inputIndex);
|
|
743
|
+
if (isTaprootInput(input))
|
|
744
|
+
return this.#signTaprootInputAsync(
|
|
745
|
+
inputIndex,
|
|
746
|
+
input,
|
|
747
|
+
keyPair,
|
|
748
|
+
undefined,
|
|
749
|
+
sighashTypes,
|
|
750
|
+
);
|
|
751
|
+
|
|
752
|
+
return this.#signInputAsync(inputIndex, keyPair, sighashTypes);
|
|
766
753
|
}
|
|
767
754
|
|
|
768
|
-
public signTaprootInputAsync(
|
|
755
|
+
public async signTaprootInputAsync(
|
|
769
756
|
inputIndex: number,
|
|
770
|
-
keyPair:
|
|
771
|
-
| Signer
|
|
772
|
-
| SignerAlternative
|
|
773
|
-
| SignerAsync
|
|
774
|
-
| HDSigner
|
|
775
|
-
| HDSignerAsync
|
|
776
|
-
| BIP32Interface
|
|
777
|
-
| UniversalSigner,
|
|
757
|
+
keyPair: Signer | SignerAsync | HDSigner | HDSignerAsync,
|
|
778
758
|
tapLeafHash?: Uint8Array,
|
|
779
759
|
sighashTypes?: number[],
|
|
780
760
|
): Promise<void> {
|
|
781
|
-
|
|
782
|
-
|
|
783
|
-
|
|
784
|
-
|
|
785
|
-
|
|
786
|
-
|
|
787
|
-
|
|
788
|
-
|
|
789
|
-
|
|
790
|
-
|
|
791
|
-
|
|
792
|
-
|
|
793
|
-
|
|
794
|
-
throw new Error(`Input #${inputIndex} is not of type Taproot.`);
|
|
795
|
-
});
|
|
761
|
+
if (!keyPair || !keyPair.publicKey) throw new Error('Need Signer to sign input');
|
|
762
|
+
|
|
763
|
+
const input = checkForInput(this.data.inputs, inputIndex);
|
|
764
|
+
if (isTaprootInput(input))
|
|
765
|
+
return this.#signTaprootInputAsync(
|
|
766
|
+
inputIndex,
|
|
767
|
+
input,
|
|
768
|
+
keyPair,
|
|
769
|
+
tapLeafHash,
|
|
770
|
+
sighashTypes,
|
|
771
|
+
);
|
|
772
|
+
|
|
773
|
+
throw new Error(`Input #${inputIndex} is not of type Taproot.`);
|
|
796
774
|
}
|
|
797
775
|
|
|
798
776
|
public toBuffer(): Uint8Array {
|
|
@@ -872,15 +850,7 @@ export class Psbt {
|
|
|
872
850
|
public checkTaprootHashesForSig(
|
|
873
851
|
inputIndex: number,
|
|
874
852
|
input: PsbtInput,
|
|
875
|
-
keyPair:
|
|
876
|
-
| Signer
|
|
877
|
-
| SignerAlternative
|
|
878
|
-
| SignerAsync
|
|
879
|
-
| HDSigner
|
|
880
|
-
| HDSignerAsync
|
|
881
|
-
| TaprootHashCheckSigner
|
|
882
|
-
| BIP32Interface
|
|
883
|
-
| UniversalSigner,
|
|
853
|
+
keyPair: Signer | SignerAsync | HDSigner | HDSignerAsync | TaprootHashCheckSigner,
|
|
884
854
|
tapLeafHashToSign?: Uint8Array,
|
|
885
855
|
allowedSighashTypes?: number[],
|
|
886
856
|
): { hash: MessageHash; leafHash?: Bytes32 }[] {
|
|
@@ -1064,7 +1034,7 @@ export class Psbt {
|
|
|
1064
1034
|
|
|
1065
1035
|
#signInput(
|
|
1066
1036
|
inputIndex: number,
|
|
1067
|
-
keyPair: Signer |
|
|
1037
|
+
keyPair: Signer | HDSigner,
|
|
1068
1038
|
sighashTypes: number[] = [Transaction.SIGHASH_ALL],
|
|
1069
1039
|
): this {
|
|
1070
1040
|
const pubkey =
|
|
@@ -1098,7 +1068,7 @@ export class Psbt {
|
|
|
1098
1068
|
#signTaprootInput(
|
|
1099
1069
|
inputIndex: number,
|
|
1100
1070
|
input: PsbtInput,
|
|
1101
|
-
keyPair: Signer |
|
|
1071
|
+
keyPair: Signer | HDSigner,
|
|
1102
1072
|
tapLeafHashToSign?: Uint8Array,
|
|
1103
1073
|
allowedSighashTypes: number[] = [Transaction.SIGHASH_DEFAULT],
|
|
1104
1074
|
): this {
|
|
@@ -1153,16 +1123,9 @@ export class Psbt {
|
|
|
1153
1123
|
return this;
|
|
1154
1124
|
}
|
|
1155
1125
|
|
|
1156
|
-
#signInputAsync(
|
|
1126
|
+
async #signInputAsync(
|
|
1157
1127
|
inputIndex: number,
|
|
1158
|
-
keyPair:
|
|
1159
|
-
| Signer
|
|
1160
|
-
| SignerAlternative
|
|
1161
|
-
| SignerAsync
|
|
1162
|
-
| HDSigner
|
|
1163
|
-
| HDSignerAsync
|
|
1164
|
-
| BIP32Interface
|
|
1165
|
-
| UniversalSigner,
|
|
1128
|
+
keyPair: Signer | SignerAsync | HDSigner | HDSignerAsync,
|
|
1166
1129
|
sighashTypes: number[] = [Transaction.SIGHASH_ALL],
|
|
1167
1130
|
): Promise<void> {
|
|
1168
1131
|
const pubkey =
|
|
@@ -1177,31 +1140,23 @@ export class Psbt {
|
|
|
1177
1140
|
sighashTypes,
|
|
1178
1141
|
);
|
|
1179
1142
|
|
|
1180
|
-
|
|
1181
|
-
|
|
1182
|
-
|
|
1183
|
-
|
|
1184
|
-
|
|
1185
|
-
|
|
1186
|
-
|
|
1187
|
-
|
|
1143
|
+
const signature = await keyPair.sign(hash);
|
|
1144
|
+
const sig = signature instanceof Uint8Array ? signature : new Uint8Array(signature);
|
|
1145
|
+
const partialSig = [
|
|
1146
|
+
{
|
|
1147
|
+
pubkey,
|
|
1148
|
+
signature: bscript.signature.encode(sig, sighashType),
|
|
1149
|
+
},
|
|
1150
|
+
];
|
|
1188
1151
|
|
|
1189
|
-
|
|
1190
|
-
|
|
1191
|
-
});
|
|
1152
|
+
this.data.updateInput(inputIndex, { partialSig });
|
|
1153
|
+
this.#cache.hasSignatures = true;
|
|
1192
1154
|
}
|
|
1193
1155
|
|
|
1194
1156
|
async #signTaprootInputAsync(
|
|
1195
1157
|
inputIndex: number,
|
|
1196
1158
|
input: PsbtInput,
|
|
1197
|
-
keyPair:
|
|
1198
|
-
| Signer
|
|
1199
|
-
| SignerAlternative
|
|
1200
|
-
| SignerAsync
|
|
1201
|
-
| HDSigner
|
|
1202
|
-
| HDSignerAsync
|
|
1203
|
-
| BIP32Interface
|
|
1204
|
-
| UniversalSigner,
|
|
1159
|
+
keyPair: Signer | SignerAsync | HDSigner | HDSignerAsync,
|
|
1205
1160
|
tapLeafHash?: Uint8Array,
|
|
1206
1161
|
sighashTypes: number[] = [Transaction.SIGHASH_DEFAULT],
|
|
1207
1162
|
): Promise<void> {
|
|
@@ -1225,42 +1180,29 @@ export class Psbt {
|
|
|
1225
1180
|
keyPair.signSchnorr as (hash: MessageHash) => SchnorrSignature | Promise<SchnorrSignature>
|
|
1226
1181
|
).bind(keyPair);
|
|
1227
1182
|
|
|
1228
|
-
|
|
1229
|
-
const signaturePromises: Promise<TapSignatureResult>[] = [];
|
|
1230
|
-
|
|
1231
|
-
const tapKeyHash = hashesForSig.filter((h) => !h.leafHash)[0];
|
|
1183
|
+
const tapKeyHash = hashesForSig.find((h) => !h.leafHash);
|
|
1232
1184
|
if (tapKeyHash) {
|
|
1233
|
-
const
|
|
1234
|
-
|
|
1235
|
-
|
|
1236
|
-
|
|
1237
|
-
});
|
|
1238
|
-
signaturePromises.push(tapKeySigPromise);
|
|
1185
|
+
const sig = await signSchnorr(tapKeyHash.hash);
|
|
1186
|
+
const tapKeySig = serializeTaprootSignature(sig, input.sighashType);
|
|
1187
|
+
this.data.updateInput(inputIndex, { tapKeySig });
|
|
1188
|
+
this.#cache.hasSignatures = true;
|
|
1239
1189
|
}
|
|
1240
1190
|
|
|
1241
1191
|
const tapScriptHashes = hashesForSig.filter(
|
|
1242
1192
|
(h): h is typeof h & { leafHash: Bytes32 } => !!h.leafHash,
|
|
1243
1193
|
);
|
|
1244
1194
|
if (tapScriptHashes.length) {
|
|
1245
|
-
const
|
|
1246
|
-
|
|
1247
|
-
|
|
1248
|
-
|
|
1249
|
-
{
|
|
1195
|
+
const tapScriptSigs = await Promise.all(
|
|
1196
|
+
tapScriptHashes.map(async (tsh) => {
|
|
1197
|
+
const signature = await signSchnorr(tsh.hash);
|
|
1198
|
+
return {
|
|
1250
1199
|
pubkey: toXOnly(pubkey),
|
|
1251
1200
|
signature: serializeTaprootSignature(signature, input.sighashType),
|
|
1252
1201
|
leafHash: tsh.leafHash,
|
|
1253
|
-
}
|
|
1254
|
-
|
|
1255
|
-
|
|
1256
|
-
|
|
1257
|
-
});
|
|
1258
|
-
signaturePromises.push(...tapScriptSigPromises);
|
|
1259
|
-
}
|
|
1260
|
-
|
|
1261
|
-
const results = await Promise.all(signaturePromises);
|
|
1262
|
-
for (const v of results) {
|
|
1263
|
-
this.data.updateInput(inputIndex, v as PsbtInputUpdate);
|
|
1202
|
+
} as TapScriptSig;
|
|
1203
|
+
}),
|
|
1204
|
+
);
|
|
1205
|
+
this.data.updateInput(inputIndex, { tapScriptSig: tapScriptSigs });
|
|
1264
1206
|
this.#cache.hasSignatures = true;
|
|
1265
1207
|
}
|
|
1266
1208
|
}
|
package/src/types.ts
CHANGED
|
@@ -182,7 +182,7 @@ export function isTaptree(value: unknown): value is Taptree {
|
|
|
182
182
|
// ECC Interface (re-exported from ecc/types.ts for backward compatibility)
|
|
183
183
|
// ============================================================================
|
|
184
184
|
|
|
185
|
-
export type { XOnlyPointAddTweakResult, EccLib, Parity } from './ecc/types.js';
|
|
185
|
+
export type { CryptoBackend, XOnlyPointAddTweakResult, EccLib, Parity } from './ecc/types.js';
|
|
186
186
|
|
|
187
187
|
// ============================================================================
|
|
188
188
|
// Stack Types
|