@dynamic-labs-wallet/btc-utils 1.0.118 → 1.0.120

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/index.cjs CHANGED
@@ -290,6 +290,66 @@ var convertObjectToBuffer = function(actualPubkey) {
290
290
  return addressType;
291
291
  };
292
292
 
293
+ /**
294
+ * Infers the Bitcoin address type from a bech32/bech32m account address.
295
+ *
296
+ * The character immediately after the '1' separator encodes the witness
297
+ * version: 'q' => v0 (P2WPKH, native segwit), 'p' => v1 (P2TR, taproot). The
298
+ * type is intrinsic to the address, so this works regardless of wallet
299
+ * metadata and for every network prefix (`bc1…`, `tb1…`, `bcrt1…`). Legacy
300
+ * (`1…`/`3…`) addresses are not supported and return undefined.
301
+ *
302
+ * @param address - The Bitcoin account address
303
+ * @returns The inferred BitcoinAddressType, or undefined if it cannot be determined
304
+ */ var getAddressTypeFromAddress = function(address) {
305
+ var normalizedAddress = address.toLowerCase();
306
+ // The bech32 data charset excludes '1', so the separator is always the last '1'.
307
+ var separatorIndex = normalizedAddress.lastIndexOf('1');
308
+ var witnessVersionIndex = separatorIndex + 1;
309
+ if (separatorIndex === -1 || witnessVersionIndex >= normalizedAddress.length) {
310
+ return undefined;
311
+ }
312
+ switch(normalizedAddress[witnessVersionIndex]){
313
+ case 'q':
314
+ return core.BitcoinAddressType.NATIVE_SEGWIT;
315
+ case 'p':
316
+ return core.BitcoinAddressType.TAPROOT;
317
+ default:
318
+ return undefined;
319
+ }
320
+ };
321
+ /**
322
+ * Resolves the address type a BTC signing operation must use.
323
+ *
324
+ * Imported wallets are non-HD: the MPC server derives them with an empty path
325
+ * and persists `"[]"`, so a derivation path cannot be relied on to carry the
326
+ * address type. The account address always can — the witness version is part
327
+ * of the address — so it backs the path up rather than the other way around.
328
+ *
329
+ * @param addressType - Address type already recorded against the wallet, if any
330
+ * @param derivationPath - Serialized derivation path, `"[]"` for imported wallets
331
+ * @param accountAddress - The wallet's account address
332
+ * @returns The resolved BitcoinAddressType
333
+ * @throws If none of the three inputs can determine the address type
334
+ */ var resolveBitcoinAddressType = function(param) {
335
+ var addressType = param.addressType, derivationPath = param.derivationPath, accountAddress = param.accountAddress;
336
+ if (addressType) {
337
+ return addressType;
338
+ }
339
+ if (derivationPath) {
340
+ try {
341
+ return getAddressTypeFromDerivationPath(derivationPath);
342
+ } catch (e) {
343
+ // Empty or unrecognised path — fall through to the address.
344
+ }
345
+ }
346
+ var fromAddress = accountAddress ? getAddressTypeFromAddress(accountAddress) : undefined;
347
+ if (fromAddress) {
348
+ return fromAddress;
349
+ }
350
+ throw new Error('Unable to determine Bitcoin address type');
351
+ };
352
+
293
353
  /**
294
354
  * Calculates the hash to be signed for a BIP-322 message verification
295
355
  *
@@ -1719,6 +1779,7 @@ exports.encodeBip322Signature = encodeBip322Signature;
1719
1779
  exports.extractPsbtSighashes = extractPsbtSighashes;
1720
1780
  exports.extractPublicKeyHex = extractPublicKeyHex;
1721
1781
  exports.filterInputsBySigningIndexes = filterInputsBySigningIndexes;
1782
+ exports.getAddressTypeFromAddress = getAddressTypeFromAddress;
1722
1783
  exports.getAddressTypeFromDerivationPath = getAddressTypeFromDerivationPath;
1723
1784
  exports.getBitcoinNetwork = getBitcoinNetwork;
1724
1785
  exports.getDefaultRpcUrl = getDefaultRpcUrl;
@@ -1734,6 +1795,7 @@ exports.normalizeForTaproot = normalizeForTaproot;
1734
1795
  exports.normalizePublicKey = normalizePublicKey;
1735
1796
  exports.privateKeyToWIF = privateKeyToWIF;
1736
1797
  exports.publicKeyToBitcoinAddress = publicKeyToBitcoinAddress;
1798
+ exports.resolveBitcoinAddressType = resolveBitcoinAddressType;
1737
1799
  exports.selectUTXOs = selectUTXOs;
1738
1800
  exports.toBitcoinNetwork = toBitcoinNetwork;
1739
1801
  exports.toBuffer = toBuffer;
package/index.esm.js CHANGED
@@ -270,6 +270,66 @@ var convertObjectToBuffer = function(actualPubkey) {
270
270
  return addressType;
271
271
  };
272
272
 
273
+ /**
274
+ * Infers the Bitcoin address type from a bech32/bech32m account address.
275
+ *
276
+ * The character immediately after the '1' separator encodes the witness
277
+ * version: 'q' => v0 (P2WPKH, native segwit), 'p' => v1 (P2TR, taproot). The
278
+ * type is intrinsic to the address, so this works regardless of wallet
279
+ * metadata and for every network prefix (`bc1…`, `tb1…`, `bcrt1…`). Legacy
280
+ * (`1…`/`3…`) addresses are not supported and return undefined.
281
+ *
282
+ * @param address - The Bitcoin account address
283
+ * @returns The inferred BitcoinAddressType, or undefined if it cannot be determined
284
+ */ var getAddressTypeFromAddress = function(address) {
285
+ var normalizedAddress = address.toLowerCase();
286
+ // The bech32 data charset excludes '1', so the separator is always the last '1'.
287
+ var separatorIndex = normalizedAddress.lastIndexOf('1');
288
+ var witnessVersionIndex = separatorIndex + 1;
289
+ if (separatorIndex === -1 || witnessVersionIndex >= normalizedAddress.length) {
290
+ return undefined;
291
+ }
292
+ switch(normalizedAddress[witnessVersionIndex]){
293
+ case 'q':
294
+ return BitcoinAddressType.NATIVE_SEGWIT;
295
+ case 'p':
296
+ return BitcoinAddressType.TAPROOT;
297
+ default:
298
+ return undefined;
299
+ }
300
+ };
301
+ /**
302
+ * Resolves the address type a BTC signing operation must use.
303
+ *
304
+ * Imported wallets are non-HD: the MPC server derives them with an empty path
305
+ * and persists `"[]"`, so a derivation path cannot be relied on to carry the
306
+ * address type. The account address always can — the witness version is part
307
+ * of the address — so it backs the path up rather than the other way around.
308
+ *
309
+ * @param addressType - Address type already recorded against the wallet, if any
310
+ * @param derivationPath - Serialized derivation path, `"[]"` for imported wallets
311
+ * @param accountAddress - The wallet's account address
312
+ * @returns The resolved BitcoinAddressType
313
+ * @throws If none of the three inputs can determine the address type
314
+ */ var resolveBitcoinAddressType = function(param) {
315
+ var addressType = param.addressType, derivationPath = param.derivationPath, accountAddress = param.accountAddress;
316
+ if (addressType) {
317
+ return addressType;
318
+ }
319
+ if (derivationPath) {
320
+ try {
321
+ return getAddressTypeFromDerivationPath(derivationPath);
322
+ } catch (e) {
323
+ // Empty or unrecognised path — fall through to the address.
324
+ }
325
+ }
326
+ var fromAddress = accountAddress ? getAddressTypeFromAddress(accountAddress) : undefined;
327
+ if (fromAddress) {
328
+ return fromAddress;
329
+ }
330
+ throw new Error('Unable to determine Bitcoin address type');
331
+ };
332
+
273
333
  /**
274
334
  * Calculates the hash to be signed for a BIP-322 message verification
275
335
  *
@@ -1683,4 +1743,4 @@ function _type_of(obj) {
1683
1743
  return Buffer.from(formattedMessage).toString('hex');
1684
1744
  };
1685
1745
 
1686
- export { assertSighashAllowed, calculateBip322Hash, calculateTaprootTweak, collectPSBTInputData, computeBip322HashHex, convertSignatureToDER, convertSignatureToTaprootBuffer, createLegacyAddress, createNativeSegWitAddress, createSegWitAddress, createTaprootAddress, doesInputBelongToAddress, encodeBip322Signature, extractPsbtSighashes, extractPublicKeyHex, filterInputsBySigningIndexes, getAddressTypeFromDerivationPath, getBitcoinNetwork, getDefaultRpcUrl, getFeeRates, getPublicKeyFromPrivateKey, getSegwitV0ScriptCode, getSigHashType, getUTXOs, initEccLib, isP2wshOutputScript, normalizeForCompressed, normalizeForTaproot, normalizePublicKey, privateKeyToWIF, publicKeyToBitcoinAddress, selectUTXOs, toBitcoinNetwork, toBuffer, wifToPrivateKey };
1746
+ export { assertSighashAllowed, calculateBip322Hash, calculateTaprootTweak, collectPSBTInputData, computeBip322HashHex, convertSignatureToDER, convertSignatureToTaprootBuffer, createLegacyAddress, createNativeSegWitAddress, createSegWitAddress, createTaprootAddress, doesInputBelongToAddress, encodeBip322Signature, extractPsbtSighashes, extractPublicKeyHex, filterInputsBySigningIndexes, getAddressTypeFromAddress, getAddressTypeFromDerivationPath, getBitcoinNetwork, getDefaultRpcUrl, getFeeRates, getPublicKeyFromPrivateKey, getSegwitV0ScriptCode, getSigHashType, getUTXOs, initEccLib, isP2wshOutputScript, normalizeForCompressed, normalizeForTaproot, normalizePublicKey, privateKeyToWIF, publicKeyToBitcoinAddress, resolveBitcoinAddressType, selectUTXOs, toBitcoinNetwork, toBuffer, wifToPrivateKey };
package/package.json CHANGED
@@ -1,11 +1,11 @@
1
1
  {
2
2
  "name": "@dynamic-labs-wallet/btc-utils",
3
- "version": "1.0.118",
3
+ "version": "1.0.120",
4
4
  "license": "MIT",
5
5
  "type": "module",
6
6
  "sideEffects": false,
7
7
  "dependencies": {
8
- "@dynamic-labs-wallet/core": "1.0.118",
8
+ "@dynamic-labs-wallet/core": "1.0.120",
9
9
  "bitcoinjs-lib": "^7.0.0",
10
10
  "bip322-js": "^3.0.0",
11
11
  "@noble/hashes": "1.7.1",
package/src/index.d.ts CHANGED
@@ -1,6 +1,7 @@
1
1
  export { publicKeyToBitcoinAddress } from './publicKeyToBitcoinAddress/index.js';
2
2
  export { normalizePublicKey } from './normalizePublicKey/index.js';
3
3
  export { getAddressTypeFromDerivationPath } from './getAddressTypeFromDerivationPath/index.js';
4
+ export { getAddressTypeFromAddress, resolveBitcoinAddressType } from './resolveBitcoinAddressType/index.js';
4
5
  export { calculateBip322Hash } from './calculateBip322Hash/index.js';
5
6
  export { encodeBip322Signature } from './encodeBip322Signature/index.js';
6
7
  export { privateKeyToWIF } from './privateKeyToWIF/index.js';
@@ -1 +1 @@
1
- {"version":3,"file":"index.d.ts","sourceRoot":"","sources":["../../packages/src/index.ts"],"names":[],"mappings":"AAAA,OAAO,EAAE,yBAAyB,EAAE,MAAM,sCAAsC,CAAC;AACjF,OAAO,EAAE,kBAAkB,EAAE,MAAM,+BAA+B,CAAC;AACnE,OAAO,EAAE,gCAAgC,EAAE,MAAM,6CAA6C,CAAC;AAC/F,OAAO,EAAE,mBAAmB,EAAE,MAAM,gCAAgC,CAAC;AACrE,OAAO,EAAE,qBAAqB,EAAE,MAAM,kCAAkC,CAAC;AACzE,OAAO,EAAE,eAAe,EAAE,MAAM,4BAA4B,CAAC;AAC7D,OAAO,EAAE,eAAe,EAAE,MAAM,4BAA4B,CAAC;AAC7D,OAAO,EAAE,0BAA0B,EAAE,MAAM,uCAAu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CAAC;AACvE,OAAO,EAAE,wBAAwB,EAAE,MAAM,qCAAqC,CAAC;AAC/E,OAAO,EAAE,mBAAmB,EAAE,MAAM,gCAAgC,CAAC;AACrE,OAAO,EAAE,qBAAqB,EAAE,MAAM,kCAAkC,CAAC;AACzE,OAAO,EAAE,4BAA4B,EAAE,MAAM,yCAAyC,CAAC;AACvF,OAAO,EAAE,iBAAiB,EAAE,MAAM,8BAA8B,CAAC;AACjE,OAAO,EAAE,cAAc,EAAE,MAAM,2BAA2B,CAAC;AAC3D,OAAO,EAAE,oBAAoB,EAAE,MAAM,iCAAiC,CAAC;AACvE,OAAO,EAAE,UAAU,EAAE,MAAM,uBAAuB,CAAC;AACnD,OAAO,EAAE,mBAAmB,EAAE,MAAM,gCAAgC,CAAC;AACrE,YAAY,EAAE,IAAI,EAAE,MAAM,YAAY,CAAC;AAGvC,OAAO,EAAE,mBAAmB,EAAE,MAAM,gCAAgC,CAAC;AACrE,OAAO,EAAE,mBAAmB,EAAE,MAAM,gCAAgC,CAAC;AACrE,OAAO,EAAE,yBAAyB,EAAE,MAAM,sCAAsC,CAAC;AACjF,OAAO,EAAE,oBAAoB,EAAE,MAAM,iCAAiC,CAAC;AAGvE,OAAO,EAAE,QAAQ,EAAE,MAAM,qBAAqB,CAAC;AAC/C,OAAO,EAAE,sBAAsB,EAAE,MAAM,mCAAmC,CAAC;AAC3E,OAAO,EAAE,mBAAmB,EAAE,MAAM,gCAAgC,CAAC;AACrE,OAAO,EAAE,gBAAgB,EAAE,MAAM,6BAA6B,CAAC;AAG/D,OAAO,EAAE,oBAAoB,EAAE,MAAM,iCAAiC,CAAC;AACvE,OAAO,EAAE,oBAAoB,EAAE,MAAM,iCAAiC,CAAC"}
@@ -0,0 +1,2 @@
1
+ export { getAddressTypeFromAddress, resolveBitcoinAddressType } from './resolveBitcoinAddressType.js';
2
+ //# sourceMappingURL=index.d.ts.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"index.d.ts","sourceRoot":"","sources":["../../src/resolveBitcoinAddressType/index.ts"],"names":[],"mappings":"AAAA,OAAO,EAAE,yBAAyB,EAAE,yBAAyB,EAAE,MAAM,gCAAgC,CAAC"}
@@ -0,0 +1,34 @@
1
+ import { BitcoinAddressType } from '@dynamic-labs-wallet/core';
2
+ /**
3
+ * Infers the Bitcoin address type from a bech32/bech32m account address.
4
+ *
5
+ * The character immediately after the '1' separator encodes the witness
6
+ * version: 'q' => v0 (P2WPKH, native segwit), 'p' => v1 (P2TR, taproot). The
7
+ * type is intrinsic to the address, so this works regardless of wallet
8
+ * metadata and for every network prefix (`bc1…`, `tb1…`, `bcrt1…`). Legacy
9
+ * (`1…`/`3…`) addresses are not supported and return undefined.
10
+ *
11
+ * @param address - The Bitcoin account address
12
+ * @returns The inferred BitcoinAddressType, or undefined if it cannot be determined
13
+ */
14
+ export declare const getAddressTypeFromAddress: (address: string) => BitcoinAddressType | undefined;
15
+ /**
16
+ * Resolves the address type a BTC signing operation must use.
17
+ *
18
+ * Imported wallets are non-HD: the MPC server derives them with an empty path
19
+ * and persists `"[]"`, so a derivation path cannot be relied on to carry the
20
+ * address type. The account address always can — the witness version is part
21
+ * of the address — so it backs the path up rather than the other way around.
22
+ *
23
+ * @param addressType - Address type already recorded against the wallet, if any
24
+ * @param derivationPath - Serialized derivation path, `"[]"` for imported wallets
25
+ * @param accountAddress - The wallet's account address
26
+ * @returns The resolved BitcoinAddressType
27
+ * @throws If none of the three inputs can determine the address type
28
+ */
29
+ export declare const resolveBitcoinAddressType: ({ addressType, derivationPath, accountAddress, }: {
30
+ accountAddress?: string;
31
+ addressType?: BitcoinAddressType;
32
+ derivationPath?: string | null;
33
+ }) => BitcoinAddressType;
34
+ //# sourceMappingURL=resolveBitcoinAddressType.d.ts.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"resolveBitcoinAddressType.d.ts","sourceRoot":"","sources":["../../src/resolveBitcoinAddressType/resolveBitcoinAddressType.ts"],"names":[],"mappings":"AAAA,OAAO,EAAE,kBAAkB,EAAE,MAAM,2BAA2B,CAAC;AAI/D;;;;;;;;;;;GAWG;AACH,eAAO,MAAM,yBAAyB,YAAa,MAAM,KAAG,kBAAkB,GAAG,SAkBhF,CAAC;AAEF;;;;;;;;;;;;;GAaG;AACH,eAAO,MAAM,yBAAyB,qDAInC;IACD,cAAc,CAAC,EAAE,MAAM,CAAC;IACxB,WAAW,CAAC,EAAE,kBAAkB,CAAC;IACjC,cAAc,CAAC,EAAE,MAAM,GAAG,IAAI,CAAC;CAChC,KAAG,kBAmBH,CAAC"}