@btc-vision/transaction 1.0.112 → 1.0.114

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 (150) hide show
  1. package/browser/_version.d.ts +1 -1
  2. package/browser/abi/ABICoder.d.ts +31 -0
  3. package/browser/buffer/BinaryReader.d.ts +37 -0
  4. package/browser/buffer/BinaryWriter.d.ts +46 -0
  5. package/browser/deterministic/AddressMap.d.ts +10 -0
  6. package/browser/deterministic/AddressSet.d.ts +13 -0
  7. package/browser/deterministic/DeterministicMap.d.ts +17 -0
  8. package/browser/deterministic/DeterministicSet.d.ts +13 -0
  9. package/browser/deterministic/Map.d.ts +14 -0
  10. package/browser/event/NetEvent.d.ts +5 -0
  11. package/browser/generators/AddressGenerator.d.ts +3 -0
  12. package/browser/index.js +1 -1
  13. package/browser/keypair/Address.d.ts +23 -0
  14. package/browser/keypair/AddressVerificator.d.ts +15 -4
  15. package/browser/keypair/EcKeyPair.d.ts +15 -12
  16. package/browser/keypair/Wallet.d.ts +12 -5
  17. package/browser/metadata/ContractBaseMetadata.d.ts +3 -4
  18. package/browser/metadata/contracts/wBTC.d.ts +2 -3
  19. package/browser/metadata/tokens.d.ts +1 -1
  20. package/browser/opnet.d.ts +11 -0
  21. package/browser/signer/TweakedSigner.d.ts +2 -2
  22. package/browser/transaction/TransactionFactory.d.ts +6 -6
  23. package/browser/transaction/browser/BrowserSignerBase.d.ts +3 -4
  24. package/browser/transaction/browser/extensions/UnisatSigner.d.ts +3 -4
  25. package/browser/transaction/browser/types/Unisat.d.ts +2 -3
  26. package/browser/transaction/builders/CustomScriptTransaction.d.ts +4 -5
  27. package/browser/transaction/builders/DeploymentTransaction.d.ts +4 -2
  28. package/browser/transaction/builders/FundingTransaction.d.ts +2 -1
  29. package/browser/transaction/builders/MultiSignTransaction.d.ts +6 -6
  30. package/browser/transaction/builders/SharedInteractionTransaction.d.ts +1 -1
  31. package/browser/transaction/builders/TransactionBuilder.d.ts +6 -7
  32. package/browser/transaction/builders/WrapTransaction.d.ts +2 -2
  33. package/browser/transaction/interfaces/ITransactionParameters.d.ts +6 -6
  34. package/browser/transaction/processor/PsbtTransaction.d.ts +3 -4
  35. package/browser/transaction/shared/P2TR_MS.d.ts +1 -2
  36. package/browser/transaction/shared/TweakedTransaction.d.ts +8 -7
  37. package/browser/utils/BufferHelper.d.ts +11 -0
  38. package/browser/utils/types.d.ts +12 -0
  39. package/browser/utxo/OPNetLimitedProvider.d.ts +1 -2
  40. package/build/_version.d.ts +1 -1
  41. package/build/_version.js +1 -1
  42. package/build/abi/ABICoder.d.ts +31 -0
  43. package/build/abi/ABICoder.js +137 -0
  44. package/build/buffer/BinaryReader.d.ts +37 -0
  45. package/build/buffer/BinaryReader.js +192 -0
  46. package/build/buffer/BinaryWriter.d.ts +46 -0
  47. package/build/buffer/BinaryWriter.js +258 -0
  48. package/build/deterministic/AddressMap.d.ts +10 -0
  49. package/build/deterministic/AddressMap.js +46 -0
  50. package/build/deterministic/AddressSet.d.ts +13 -0
  51. package/build/deterministic/AddressSet.js +44 -0
  52. package/build/deterministic/DeterministicMap.d.ts +17 -0
  53. package/build/deterministic/DeterministicMap.js +83 -0
  54. package/build/deterministic/DeterministicSet.d.ts +13 -0
  55. package/build/deterministic/DeterministicSet.js +46 -0
  56. package/build/deterministic/Map.d.ts +14 -0
  57. package/build/deterministic/Map.js +56 -0
  58. package/build/event/NetEvent.d.ts +5 -0
  59. package/build/event/NetEvent.js +6 -0
  60. package/build/generators/AddressGenerator.d.ts +3 -0
  61. package/build/generators/AddressGenerator.js +8 -1
  62. package/build/keypair/Address.d.ts +23 -0
  63. package/build/keypair/Address.js +129 -0
  64. package/build/keypair/AddressVerificator.d.ts +15 -4
  65. package/build/keypair/AddressVerificator.js +79 -3
  66. package/build/keypair/EcKeyPair.d.ts +15 -12
  67. package/build/keypair/EcKeyPair.js +61 -9
  68. package/build/keypair/Wallet.d.ts +12 -5
  69. package/build/keypair/Wallet.js +19 -5
  70. package/build/metadata/ContractBaseMetadata.d.ts +3 -4
  71. package/build/metadata/contracts/wBTC.d.ts +2 -3
  72. package/build/metadata/contracts/wBTC.js +4 -6
  73. package/build/metadata/tokens.d.ts +1 -1
  74. package/build/metadata/tokens.js +19 -15
  75. package/build/opnet.d.ts +11 -0
  76. package/build/opnet.js +11 -0
  77. package/build/signer/TweakedSigner.d.ts +2 -2
  78. package/build/signer/TweakedSigner.js +1 -1
  79. package/build/transaction/TransactionFactory.d.ts +6 -6
  80. package/build/transaction/TransactionFactory.js +3 -2
  81. package/build/transaction/browser/BrowserSignerBase.d.ts +3 -4
  82. package/build/transaction/browser/extensions/UnisatSigner.d.ts +3 -4
  83. package/build/transaction/browser/types/Unisat.d.ts +2 -3
  84. package/build/transaction/builders/CustomScriptTransaction.d.ts +4 -5
  85. package/build/transaction/builders/CustomScriptTransaction.js +3 -3
  86. package/build/transaction/builders/DeploymentTransaction.d.ts +4 -2
  87. package/build/transaction/builders/DeploymentTransaction.js +10 -6
  88. package/build/transaction/builders/FundingTransaction.d.ts +2 -1
  89. package/build/transaction/builders/MultiSignTransaction.d.ts +6 -6
  90. package/build/transaction/builders/SharedInteractionTransaction.d.ts +1 -1
  91. package/build/transaction/builders/SharedInteractionTransaction.js +4 -4
  92. package/build/transaction/builders/TransactionBuilder.d.ts +6 -7
  93. package/build/transaction/builders/UnwrapSegwitTransaction.js +2 -1
  94. package/build/transaction/builders/UnwrapTransaction.js +4 -3
  95. package/build/transaction/builders/WrapTransaction.d.ts +2 -2
  96. package/build/transaction/builders/WrapTransaction.js +5 -6
  97. package/build/transaction/interfaces/ITransactionParameters.d.ts +6 -6
  98. package/build/transaction/processor/PsbtTransaction.d.ts +3 -4
  99. package/build/transaction/shared/P2TR_MS.d.ts +1 -2
  100. package/build/transaction/shared/TweakedTransaction.d.ts +8 -7
  101. package/build/transaction/shared/TweakedTransaction.js +1 -1
  102. package/build/utils/BufferHelper.d.ts +11 -0
  103. package/build/utils/BufferHelper.js +50 -0
  104. package/build/utils/types.d.ts +12 -0
  105. package/build/utils/types.js +1 -0
  106. package/build/utxo/OPNetLimitedProvider.d.ts +1 -2
  107. package/build/utxo/OPNetLimitedProvider.js +1 -1
  108. package/build/verification/TapscriptVerificator.js +3 -2
  109. package/package.json +4 -2
  110. package/src/_version.ts +1 -1
  111. package/src/abi/ABICoder.ts +158 -0
  112. package/src/buffer/BinaryReader.ts +260 -0
  113. package/src/buffer/BinaryWriter.ts +331 -0
  114. package/src/deterministic/AddressMap.ts +57 -0
  115. package/src/deterministic/AddressSet.ts +62 -0
  116. package/src/deterministic/DeterministicMap.ts +88 -0
  117. package/src/deterministic/DeterministicSet.ts +56 -0
  118. package/src/deterministic/Map.ts +66 -0
  119. package/src/event/NetEvent.ts +6 -0
  120. package/src/generators/AddressGenerator.ts +15 -1
  121. package/src/keypair/Address.ts +235 -0
  122. package/src/keypair/AddressVerificator.ts +149 -6
  123. package/src/keypair/EcKeyPair.ts +146 -47
  124. package/src/keypair/Wallet.ts +72 -15
  125. package/src/metadata/ContractBaseMetadata.ts +3 -4
  126. package/src/metadata/contracts/wBTC.ts +8 -9
  127. package/src/metadata/tokens.ts +21 -16
  128. package/src/opnet.ts +13 -0
  129. package/src/signer/TweakedSigner.ts +3 -2
  130. package/src/transaction/TransactionFactory.ts +11 -10
  131. package/src/transaction/browser/BrowserSignerBase.ts +3 -4
  132. package/src/transaction/browser/extensions/UnisatSigner.ts +6 -7
  133. package/src/transaction/browser/types/Unisat.ts +2 -3
  134. package/src/transaction/builders/CustomScriptTransaction.ts +10 -10
  135. package/src/transaction/builders/DeploymentTransaction.ts +23 -9
  136. package/src/transaction/builders/FundingTransaction.ts +2 -1
  137. package/src/transaction/builders/MultiSignTransaction.ts +10 -7
  138. package/src/transaction/builders/SharedInteractionTransaction.ts +5 -5
  139. package/src/transaction/builders/TransactionBuilder.ts +26 -17
  140. package/src/transaction/builders/UnwrapSegwitTransaction.ts +10 -4
  141. package/src/transaction/builders/UnwrapTransaction.ts +5 -19
  142. package/src/transaction/builders/WrapTransaction.ts +7 -13
  143. package/src/transaction/interfaces/ITransactionParameters.ts +7 -6
  144. package/src/transaction/processor/PsbtTransaction.ts +3 -4
  145. package/src/transaction/shared/P2TR_MS.ts +2 -3
  146. package/src/transaction/shared/TweakedTransaction.ts +12 -12
  147. package/src/utils/BufferHelper.ts +71 -0
  148. package/src/utils/types.ts +19 -0
  149. package/src/utxo/OPNetLimitedProvider.ts +3 -8
  150. package/src/verification/TapscriptVerificator.ts +4 -2
@@ -0,0 +1,235 @@
1
+ import { Network } from 'bitcoinjs-lib';
2
+ import { EcKeyPair } from './EcKeyPair.js';
3
+ import { ECPairInterface } from 'ecpair';
4
+ import { ADDRESS_BYTE_LENGTH } from '../utils/types.js';
5
+ import { AddressVerificator } from './AddressVerificator.js';
6
+ import { toXOnly } from 'bitcoinjs-lib/src/psbt/bip371.js';
7
+
8
+ export class Address extends Uint8Array {
9
+ private isP2TROnly: boolean = false;
10
+ #p2tr: string | undefined;
11
+ #network: Network | undefined;
12
+
13
+ public constructor(bytes?: ArrayLike<number>) {
14
+ super(ADDRESS_BYTE_LENGTH);
15
+
16
+ if (!bytes) {
17
+ return;
18
+ }
19
+
20
+ this.set(bytes);
21
+ }
22
+
23
+ private _keyPair: ECPairInterface | undefined;
24
+
25
+ /**
26
+ * Get the key pair for the address
27
+ */
28
+ public get keyPair(): ECPairInterface {
29
+ if (!this._keyPair) {
30
+ throw new Error('Public key not set for address');
31
+ }
32
+
33
+ return this._keyPair;
34
+ }
35
+
36
+ public static dead(): Address {
37
+ return Address.fromString(
38
+ '0x04678afdb0fe5548271967f1a67130b7105cd6a828e03909a67962e0ea1f61deb649f6bc3f4cef38c4f35504e51ec112de5c384df7ba0b8d578a4c702b6bf11d5f',
39
+ );
40
+ }
41
+
42
+ /**
43
+ * Create an address from a hex string
44
+ * @param {string} pubKey The public key
45
+ * @returns {Address} The address
46
+ */
47
+ public static fromString(pubKey: string): Address {
48
+ if (pubKey.startsWith('0x')) {
49
+ pubKey = pubKey.slice(2);
50
+ }
51
+
52
+ return new Address(Buffer.from(pubKey, 'hex'));
53
+ }
54
+
55
+ /**
56
+ * Create an address from a public key
57
+ * @returns {Address} The address
58
+ * @param {ArrayLike<number>} bytes The public key
59
+ */
60
+ public static wrap(bytes: ArrayLike<number>): Address {
61
+ return new Address(bytes);
62
+ }
63
+
64
+ /**
65
+ * Converts the address to a hex string
66
+ * @returns {string} The hex string
67
+ */
68
+ public toHex(): string {
69
+ return '0x' + Buffer.from(this).toString('hex');
70
+ }
71
+
72
+ public equals(a: Address): boolean {
73
+ if (a.length !== this.length) {
74
+ return false;
75
+ }
76
+
77
+ for (let i = 0; i < this.length; i++) {
78
+ if (this[i] !== a[i]) {
79
+ return false;
80
+ }
81
+ }
82
+
83
+ return true;
84
+ }
85
+
86
+ /**
87
+ * Check if the address is bigger than another address
88
+ * @returns {boolean} If bigger
89
+ */
90
+ public isBiggerThan(a: Address): boolean {
91
+ for (let i = 0; i < this.length; i++) {
92
+ if (this[i] < a[i]) {
93
+ return false;
94
+ }
95
+ }
96
+
97
+ return true;
98
+ }
99
+
100
+ /**
101
+ * Check if the address is smaller than another address
102
+ * @returns {boolean} If smaller
103
+ */
104
+ public isSmallerThan(a: Address): boolean {
105
+ for (let i = 0; i < this.length; i++) {
106
+ if (this[i] > a[i]) {
107
+ return false;
108
+ }
109
+ }
110
+
111
+ return true;
112
+ }
113
+
114
+ /**
115
+ * Set the public key
116
+ * @param {ArrayLike<number>} publicKey The public key
117
+ * @returns {void}
118
+ */
119
+ public override set(publicKey: ArrayLike<number>): void {
120
+ if (publicKey.length !== 33 && publicKey.length !== 32 && publicKey.length !== 65) {
121
+ throw new Error('Invalid public key length');
122
+ }
123
+
124
+ if (publicKey.length === 32) {
125
+ this.isP2TROnly = true;
126
+
127
+ const buf = Buffer.alloc(32);
128
+ buf.set(publicKey);
129
+
130
+ /*let lowByte: number = 0;
131
+ try {
132
+ EcKeyPair.tweakPublicKey('02' + buf.toString('hex'));
133
+ lowByte = 2;
134
+ } catch (e) {
135
+ console.log(`Invalid pubkey (2) ${e}`);
136
+ }
137
+
138
+ try {
139
+ EcKeyPair.tweakPublicKey('03' + buf.toString('hex'));
140
+ lowByte = 3;
141
+ } catch (e) {
142
+ console.log(`Invalid pubkey (3) ${e}`);
143
+ }
144
+
145
+ if (lowByte === 0) {
146
+ console.log(AddressGenerator.generateTaprootAddress(buf, networks.regtest));
147
+
148
+ throw new Error('Invalid public key');
149
+ }*/
150
+
151
+ super.set(publicKey);
152
+ } else {
153
+ this._keyPair = EcKeyPair.fromPublicKey(Uint8Array.from(publicKey));
154
+
155
+ const tweaked = toXOnly(
156
+ Buffer.from(
157
+ EcKeyPair.tweakPublicKey(this._keyPair.publicKey.toString('hex')),
158
+ 'hex',
159
+ ),
160
+ );
161
+
162
+ super.set(Uint8Array.from(tweaked));
163
+ }
164
+ }
165
+
166
+ /**
167
+ * Check if the public key is valid
168
+ * @param {Network} network The network
169
+ * @returns {boolean} If the public key is valid
170
+ */
171
+ public isValid(network: Network): boolean {
172
+ return AddressVerificator.isValidPublicKey(Buffer.from(this).toString('hex'), network);
173
+ }
174
+
175
+ /**
176
+ * Get the address in p2wpkh format
177
+ * @param {Network} network The network
178
+ */
179
+ public p2wpkh(network: Network): string {
180
+ return EcKeyPair.getP2WPKHAddress(this.keyPair, network);
181
+ }
182
+
183
+ /**
184
+ * Get the address in p2pkh format
185
+ * @param {Network} network The network
186
+ */
187
+ public p2pkh(network: Network): string {
188
+ return EcKeyPair.getLegacyAddress(this.keyPair, network);
189
+ }
190
+
191
+ /**
192
+ * Get the address in p2sh-p2wpkh format
193
+ * @param {Network} network The network
194
+ */
195
+ public p2shp2wpkh(network: Network): string {
196
+ return EcKeyPair.getLegacySegwitAddress(this.keyPair, network);
197
+ }
198
+
199
+ /**
200
+ * Convert the address to a string
201
+ */
202
+ public toString(): string {
203
+ if (this.#p2tr) {
204
+ return this.#p2tr;
205
+ }
206
+
207
+ return this.toHex();
208
+ }
209
+
210
+ /**
211
+ * Get the address in p2tr format
212
+ * @param {Network} network The network
213
+ */
214
+ public p2tr(network: Network): string {
215
+ if (this.#p2tr && this.#network === network) {
216
+ return this.#p2tr;
217
+ }
218
+
219
+ let p2trAddy: string | undefined;
220
+ if (this._keyPair) {
221
+ p2trAddy = EcKeyPair.getTaprootAddress(this.keyPair, network);
222
+ } else if (this.isP2TROnly) {
223
+ p2trAddy = EcKeyPair.tweakedPubKeyBufferToAddress(this, network);
224
+ }
225
+
226
+ if (p2trAddy) {
227
+ this.#network = network;
228
+ this.#p2tr = p2trAddy;
229
+
230
+ return p2trAddy;
231
+ }
232
+
233
+ throw new Error('Public key not set');
234
+ }
235
+ }
@@ -1,11 +1,28 @@
1
- import { address, initEccLib, networks } from 'bitcoinjs-lib';
1
+ import { address, initEccLib, Network } from 'bitcoinjs-lib';
2
2
  import * as ecc from '@bitcoinerlab/secp256k1';
3
- import { Address } from '@btc-vision/bsi-binary';
3
+ import { EcKeyPair } from './EcKeyPair.js';
4
4
 
5
5
  initEccLib(ecc);
6
6
 
7
+ export enum AddressTypes {
8
+ P2PKH = 'P2PKH',
9
+ P2SH = 'P2SH',
10
+ P2SH_P2WPKH = 'P2SH-P2WPKH',
11
+ P2SH_OR_P2SH_P2WPKH = 'P2SH_OR_P2SH-P2WPKH',
12
+ P2PK = 'P2PK',
13
+ P2TR = 'P2TR',
14
+ P2WPKH = 'P2WPKH',
15
+ }
16
+
7
17
  export class AddressVerificator {
8
- public static isValidP2TRAddress(inAddress: Address, network: networks.Network): boolean {
18
+ /**
19
+ * Checks if the given address is a valid P2PKH address.
20
+ * @param inAddress - The address to check.
21
+ * @param network - The network to validate against.
22
+ * @returns - True if the address is a valid P2PKH address, false otherwise.
23
+ * @remarks This method is useful for validating legacy addresses (P2PKH) without
24
+ */
25
+ public static isValidP2TRAddress(inAddress: string, network: Network): boolean {
9
26
  if (!inAddress || inAddress.length < 50) return false;
10
27
 
11
28
  let isValidTapRootAddress: boolean = false;
@@ -14,12 +31,19 @@ export class AddressVerificator {
14
31
 
15
32
  const decodedAddress = address.fromBech32(inAddress);
16
33
  isValidTapRootAddress = decodedAddress.version === 1;
17
- } catch (e) {}
34
+ } catch {}
18
35
 
19
36
  return isValidTapRootAddress;
20
37
  }
21
38
 
22
- public static validatePKHAddress(inAddress: string, network: networks.Network): boolean {
39
+ /**
40
+ * Checks if the given address is a valid P2PKH address.
41
+ * @param inAddress - The address to check.
42
+ * @param network - The network to validate against.
43
+ * @returns - True if the address is a valid P2PKH address, false otherwise.
44
+ * @remarks This method is useful for validating legacy addresses (P2PKH) without
45
+ */
46
+ public static isP2WPKHAddress(inAddress: string, network: Network): boolean {
23
47
  if (!inAddress || inAddress.length < 20 || inAddress.length > 50) return false;
24
48
 
25
49
  let isValidSegWitAddress: boolean = false;
@@ -33,8 +57,127 @@ export class AddressVerificator {
33
57
  // Check if the address is P2WPKH (version 0)
34
58
  isValidSegWitAddress =
35
59
  decodedAddress.version === 0 && decodedAddress.data.length === 20;
36
- } catch (e) {}
60
+ } catch {}
37
61
 
38
62
  return isValidSegWitAddress;
39
63
  }
64
+
65
+ /**
66
+ * Checks if the given address is a valid P2PKH or P2SH address.
67
+ * @param addy - The address to check.
68
+ * @param network - The network to validate against.
69
+ * @returns - True if the address is a valid P2PKH or P2SH address, false otherwise.
70
+ * @remarks This method is useful for validating legacy addresses (P2PKH or P2SH) without
71
+ */
72
+ public static isP2PKHOrP2SH(addy: string, network: Network): boolean {
73
+ try {
74
+ // First, try to decode as a Base58Check address (P2PKH, P2SH, or P2SH-P2WPKH)
75
+ const decodedBase58 = address.fromBase58Check(addy);
76
+
77
+ if (decodedBase58.version === network.pubKeyHash) {
78
+ // P2PKH: Legacy address (starting with '1' for mainnet, 'm/n' for testnet)
79
+ return true;
80
+ }
81
+
82
+ return decodedBase58.version === network.scriptHash;
83
+ } catch (error) {
84
+ // If decoding fails or version is not valid, it's not a valid legacy address
85
+ return false;
86
+ }
87
+ }
88
+
89
+ /**
90
+ * Checks if the input is a valid hexadecimal public key (P2PK).
91
+ * Public keys can be compressed (66 characters) or uncompressed (130 characters).
92
+ *
93
+ * @param input - The input string to check.
94
+ * @param network - The Bitcoin network to validate against (mainnet, testnet, etc.).
95
+ * @returns - True if the input is a valid public key, false otherwise.
96
+ */
97
+ public static isValidPublicKey(input: string, network: Network): boolean {
98
+ try {
99
+ if (input.startsWith('0x')) {
100
+ input = input.slice(2);
101
+ }
102
+
103
+ // Compressed public keys are 66 characters long (0x02 or 0x03 prefix + 32 bytes)
104
+ // Uncompressed public keys are 130 characters long (0x04 prefix + 64 bytes)
105
+ const hexRegex = /^[0-9a-fA-F]+$/;
106
+ if (!hexRegex.test(input)) {
107
+ return false;
108
+ }
109
+
110
+ if (input.length === 64) {
111
+ return true;
112
+ }
113
+
114
+ if (input.length === 66 || input.length === 130) {
115
+ // Check if the input can be parsed as a valid public key
116
+ const pubKeyBuffer = Buffer.from(input, 'hex');
117
+ EcKeyPair.fromPublicKey(pubKeyBuffer, network);
118
+
119
+ return true;
120
+ }
121
+ } catch (error) {
122
+ // If any error occurs (invalid public key, etc.), return false
123
+ return false;
124
+ }
125
+
126
+ return false; // Not a valid public key
127
+ }
128
+
129
+ /**
130
+ * Validates if a given Bitcoin address is of the specified type and network.
131
+ * - P2PKH (Legacy address starting with '1' for mainnet or 'm/n' for testnet)
132
+ * - P2SH (Legacy address starting with '3' for mainnet or '2' for testnet)
133
+ * - P2SH-P2WPKH (Wrapped SegWit)
134
+ * - P2PK (Pay to PubKey, technically treated similarly to P2PKH)
135
+ * - P2WPKH (SegWit address starting with 'bc1q' for mainnet or 'tb1q' for testnet)
136
+ * - P2TR (Taproot address starting with 'bc1p' for mainnet or 'tb1p' for testnet)
137
+ *
138
+ * @param addy - The Bitcoin address to validate.
139
+ * @param network - The Bitcoin network to validate against (mainnet, testnet, etc.).
140
+ * @returns - The type of the valid Bitcoin address, or null if invalid.
141
+ */
142
+ public static validateBitcoinAddress(addy: string, network: Network): AddressTypes | null {
143
+ if (AddressVerificator.isValidPublicKey(addy, network)) {
144
+ return AddressTypes.P2PK;
145
+ }
146
+
147
+ try {
148
+ // First, try to decode as a Base58Check address (P2PKH, P2SH, or P2SH-P2WPKH)
149
+ const decodedBase58 = address.fromBase58Check(addy);
150
+
151
+ if (decodedBase58.version === network.pubKeyHash) {
152
+ // P2PKH: Legacy address (starting with '1' for mainnet, 'm/n' for testnet)
153
+ return AddressTypes.P2PKH;
154
+ }
155
+ if (decodedBase58.version === network.scriptHash) {
156
+ // P2SH: Could be P2SH (general) or P2SH-P2WPKH (wrapped SegWit)
157
+ return AddressTypes.P2SH_OR_P2SH_P2WPKH;
158
+ }
159
+ } catch (error) {
160
+ // Ignore errors from Base58 decoding, it could be a Bech32 address
161
+ }
162
+
163
+ try {
164
+ // Try to decode as a Bech32 or Bech32m address (P2WPKH or P2TR)
165
+ const decodedBech32 = address.fromBech32(addy);
166
+
167
+ if (decodedBech32.prefix === network.bech32) {
168
+ // P2WPKH: SegWit address (starting with 'bc1q' for mainnet, 'tb1q' for testnet)
169
+ if (decodedBech32.version === 0 && decodedBech32.data.length === 20) {
170
+ return AddressTypes.P2WPKH;
171
+ }
172
+ // P2TR: Taproot address (starting with 'bc1p' for mainnet, 'tb1p' for testnet)
173
+ if (decodedBech32.version === 1 && decodedBech32.data.length === 32) {
174
+ return AddressTypes.P2TR;
175
+ }
176
+ }
177
+ } catch (error) {
178
+ // Ignore errors from Bech32/Bech32m decoding
179
+ }
180
+
181
+ return null; // Not a valid or recognized Bitcoin address type
182
+ }
40
183
  }