@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.
Files changed (51) hide show
  1. package/README.md +334 -161
  2. package/browser/ecc/context.d.ts +22 -21
  3. package/browser/ecc/context.d.ts.map +1 -1
  4. package/browser/ecc/index.d.ts +1 -1
  5. package/browser/ecc/index.d.ts.map +1 -1
  6. package/browser/ecc/types.d.ts +10 -123
  7. package/browser/ecc/types.d.ts.map +1 -1
  8. package/browser/index.d.ts +2 -2
  9. package/browser/index.d.ts.map +1 -1
  10. package/browser/index.js +5790 -4062
  11. package/browser/payments/p2tr.d.ts.map +1 -1
  12. package/browser/psbt/types.d.ts +2 -68
  13. package/browser/psbt/types.d.ts.map +1 -1
  14. package/browser/psbt.d.ts +9 -11
  15. package/browser/psbt.d.ts.map +1 -1
  16. package/browser/types.d.ts +1 -1
  17. package/browser/types.d.ts.map +1 -1
  18. package/build/ecc/context.d.ts +22 -21
  19. package/build/ecc/context.d.ts.map +1 -1
  20. package/build/ecc/context.js +19 -114
  21. package/build/ecc/context.js.map +1 -1
  22. package/build/ecc/index.d.ts +1 -1
  23. package/build/ecc/index.d.ts.map +1 -1
  24. package/build/ecc/types.d.ts +7 -126
  25. package/build/ecc/types.d.ts.map +1 -1
  26. package/build/ecc/types.js +4 -1
  27. package/build/ecc/types.js.map +1 -1
  28. package/build/index.d.ts +2 -2
  29. package/build/index.d.ts.map +1 -1
  30. package/build/index.js.map +1 -1
  31. package/build/payments/p2tr.d.ts.map +1 -1
  32. package/build/payments/p2tr.js +2 -3
  33. package/build/payments/p2tr.js.map +1 -1
  34. package/build/psbt/types.d.ts +2 -68
  35. package/build/psbt/types.d.ts.map +1 -1
  36. package/build/psbt.d.ts +9 -11
  37. package/build/psbt.d.ts.map +1 -1
  38. package/build/psbt.js +38 -53
  39. package/build/psbt.js.map +1 -1
  40. package/build/tsconfig.build.tsbuildinfo +1 -1
  41. package/build/types.d.ts +1 -1
  42. package/build/types.d.ts.map +1 -1
  43. package/package.json +4 -4
  44. package/src/ecc/context.ts +26 -147
  45. package/src/ecc/index.ts +2 -2
  46. package/src/ecc/types.ts +7 -138
  47. package/src/index.ts +1 -2
  48. package/src/payments/p2tr.ts +2 -2
  49. package/src/psbt/types.ts +2 -84
  50. package/src/psbt.ts +63 -121
  51. 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 | SignerAlternative | BIP32Interface | UniversalSigner,
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 | SignerAlternative | SignerAsync | BIP32Interface | UniversalSigner,
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 | SignerAlternative | HDSigner | BIP32Interface | UniversalSigner,
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 | SignerAlternative | HDSigner | BIP32Interface | UniversalSigner,
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
- return Promise.resolve().then(() => {
752
- if (!keyPair || !keyPair.publicKey) throw new Error('Need Signer to sign input');
753
-
754
- const input = checkForInput(this.data.inputs, inputIndex);
755
- if (isTaprootInput(input))
756
- return this.#signTaprootInputAsync(
757
- inputIndex,
758
- input,
759
- keyPair,
760
- undefined,
761
- sighashTypes,
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
- return Promise.resolve().then(() => {
782
- if (!keyPair || !keyPair.publicKey) throw new Error('Need Signer to sign input');
783
-
784
- const input = checkForInput(this.data.inputs, inputIndex);
785
- if (isTaprootInput(input))
786
- return this.#signTaprootInputAsync(
787
- inputIndex,
788
- input,
789
- keyPair,
790
- tapLeafHash,
791
- sighashTypes,
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 | SignerAlternative | HDSigner | BIP32Interface | UniversalSigner,
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 | SignerAlternative | HDSigner | BIP32Interface | UniversalSigner,
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
- return Promise.resolve(keyPair.sign(hash)).then((signature) => {
1181
- const sig = signature instanceof Uint8Array ? signature : new Uint8Array(signature);
1182
- const partialSig = [
1183
- {
1184
- pubkey,
1185
- signature: bscript.signature.encode(sig, sighashType),
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
- this.data.updateInput(inputIndex, { partialSig });
1190
- this.#cache.hasSignatures = true;
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
- type TapSignatureResult = { tapKeySig: Uint8Array } | { tapScriptSig: TapScriptSig[] };
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 tapKeySigPromise = Promise.resolve(signSchnorr(tapKeyHash.hash)).then((sig) => {
1234
- return {
1235
- tapKeySig: serializeTaprootSignature(sig, input.sighashType),
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 tapScriptSigPromises = tapScriptHashes.map(async (tsh) => {
1246
- const signature = await signSchnorr(tsh.hash);
1247
-
1248
- const tapScriptSig: TapScriptSig[] = [
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
- return { tapScriptSig };
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