@swapkit/wallet-hardware 4.11.0 → 5.0.0

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.
@@ -11,6 +11,7 @@ import {
11
11
  NetworkDerivationPath,
12
12
  SwapKitError,
13
13
  type UTXOChain,
14
+ UTXOScriptType,
14
15
  WalletOption,
15
16
  } from "@swapkit/helpers";
16
17
  import {
@@ -19,6 +20,7 @@ import {
19
20
  compileMemo,
20
21
  createHDWalletHelpers,
21
22
  getNetworkForChain,
23
+ getScriptTypeForAddress,
22
24
  getUTXOAccountIndexFromPath,
23
25
  getUTXOAccountPath,
24
26
  getUTXOAddressPath,
@@ -172,6 +174,7 @@ async function getWalletMethods({
172
174
  const signer = await getLedgerClient({ chain, derivationPath, transport });
173
175
 
174
176
  const address = providedAddress ?? (await getLedgerAddress({ chain, ledgerClient: signer }));
177
+ const scriptType = getScriptTypeForAddress(address, utxoChain);
175
178
 
176
179
  // V3 toolbox signer:
177
180
  // - BTC uses the modern `ledger-bitcoin` AppClient with native PSBT signing.
@@ -181,13 +184,19 @@ async function getWalletMethods({
181
184
  // policy APDUs and returns CLA_NOT_SUPPORTED.
182
185
  // - ZEC stays on the bespoke `signPCZT` flow for now.
183
186
  let toolboxSigner:
184
- | { getAddress: () => Promise<string>; signTransaction: (tx: Transaction) => Promise<Transaction> }
187
+ | {
188
+ getAddress: () => Promise<string>;
189
+ publicKey?: Uint8Array;
190
+ signTransaction: (tx: Transaction) => Promise<Transaction>;
191
+ }
185
192
  | undefined;
193
+ let publicKey: Uint8Array | undefined;
186
194
  let signAndBroadcastLegacyPsbtTransaction: ((tx: Transaction) => Promise<string>) | undefined;
187
195
  if (chain === Chain.Bitcoin) {
188
196
  const { BitcoinPsbtLedger } = await import("./clients/utxo-psbt");
189
197
  const psbtClient = BitcoinPsbtLedger(derivationPath, transport);
190
- toolboxSigner = { getAddress: psbtClient.getAddress, signTransaction: psbtClient.signTransaction };
198
+ publicKey = scriptType === UTXOScriptType.P2SH_P2WPKH ? await psbtClient.getPublicKey() : undefined;
199
+ toolboxSigner = { getAddress: psbtClient.getAddress, publicKey, signTransaction: psbtClient.signTransaction };
191
200
  } else if (
192
201
  chain === Chain.BitcoinCash ||
193
202
  chain === Chain.Dogecoin ||
@@ -203,8 +212,8 @@ async function getWalletMethods({
203
212
  }
204
213
 
205
214
  const toolbox = toolboxSigner
206
- ? await getUtxoToolbox(utxoChain, { signer: toolboxSigner })
207
- : getUtxoToolbox(utxoChain);
215
+ ? await getUtxoToolbox(utxoChain, { scriptType, signer: toolboxSigner })
216
+ : getUtxoToolbox(utxoChain, { scriptType });
208
217
  const signAndBroadcastTransaction = signAndBroadcastLegacyPsbtTransaction ?? toolbox.signAndBroadcastTransaction;
209
218
 
210
219
  const transfer = async (params: UTXOBuildTxParams) => {
@@ -216,6 +225,7 @@ async function getWalletMethods({
216
225
  feeRate,
217
226
  fetchTxHex: true,
218
227
  memo,
228
+ publicKey: params.publicKey ?? publicKey,
219
229
  sender: address,
220
230
  });
221
231
 
@@ -10,6 +10,7 @@ import {
10
10
  SKConfig,
11
11
  SwapKitError,
12
12
  type UTXOChain,
13
+ UTXOScriptType,
13
14
  WalletOption,
14
15
  } from "@swapkit/helpers";
15
16
  import {
@@ -208,11 +209,11 @@ function decodeOpReturnData(script: Uint8Array): string | null {
208
209
  function getScriptType(derivationPath: DerivationPathArray) {
209
210
  switch (derivationPath[0]) {
210
211
  case 84:
211
- return { input: "SPENDWITNESS", output: "PAYTOWITNESS" } as const;
212
+ return { input: "SPENDWITNESS", output: "PAYTOWITNESS", utxo: UTXOScriptType.P2WPKH } as const;
212
213
  case 49:
213
- return { input: "SPENDP2SHWITNESS", output: "PAYTOP2SHWITNESS" } as const;
214
+ return { input: "SPENDP2SHWITNESS", output: "PAYTOP2SHWITNESS", utxo: UTXOScriptType.P2SH_P2WPKH } as const;
214
215
  case 44:
215
- return { input: "SPENDADDRESS", output: "PAYTOADDRESS" } as const;
216
+ return { input: "SPENDADDRESS", output: "PAYTOADDRESS", utxo: UTXOScriptType.P2PKH } as const;
216
217
  default:
217
218
  return null;
218
219
  }
@@ -690,6 +691,7 @@ async function getTrezorWallet<T extends Chain>({
690
691
  }
691
692
 
692
693
  const resolvedScriptType = scriptType;
694
+ const toolboxParams = { scriptType: scriptType.utxo };
693
695
  const coin = chain.toLowerCase();
694
696
 
695
697
  const getAddress = async (path: DerivationPathArray = derivationPath) => {
@@ -717,7 +719,7 @@ async function getTrezorWallet<T extends Chain>({
717
719
  };
718
720
 
719
721
  const address = providedAddress ?? (await getAddress());
720
- const baseToolbox = getUtxoToolbox(chain);
722
+ const baseToolbox = getUtxoToolbox(chain, toolboxParams);
721
723
 
722
724
  const signTransaction = async (tx: Transaction, inputs: UTXOType[], memo = "") => {
723
725
  const TrezorConnect = (await import("@trezor/connect-web")).default;
@@ -998,7 +1000,7 @@ async function getTrezorWallet<T extends Chain>({
998
1000
  feeRate?: number;
999
1001
  feeOptionKey?: (typeof FeeOption)[keyof typeof FeeOption];
1000
1002
  }) => {
1001
- const toolbox = getUtxoToolbox(chain);
1003
+ const toolbox = getUtxoToolbox(chain, toolboxParams);
1002
1004
  const txFeeRate = feeRate || (await toolbox.getFeeRates())[feeOptionKey || FeeOption.Fast];
1003
1005
 
1004
1006
  const { tx, inputs: selectedInputs } = await toolbox.createTransaction({
@@ -1038,7 +1040,7 @@ async function getTrezorWallet<T extends Chain>({
1038
1040
  });
1039
1041
  }
1040
1042
 
1041
- const toolbox = getUtxoToolbox(chain);
1043
+ const toolbox = getUtxoToolbox(chain, toolboxParams);
1042
1044
 
1043
1045
  const feeRate = paramFeeRate || (await toolbox.getFeeRates())[feeOptionKey || FeeOption.Fast];
1044
1046
 
@@ -1061,6 +1063,7 @@ async function getTrezorWallet<T extends Chain>({
1061
1063
 
1062
1064
  const toolbox = shouldUseTrezorPsbtSigner(chain)
1063
1065
  ? await getUtxoToolbox(utxoChain, {
1066
+ ...toolboxParams,
1064
1067
  signer: { getAddress: async () => address, signTransaction: signPsbtTransaction },
1065
1068
  })
1066
1069
  : baseToolbox;
@@ -1,4 +0,0 @@
1
- import{c as C}from"./chunk-3yr76n9s.js";import{d as D}from"./chunk-n05bv2n5.js";import{base64 as m}from"@scure/base";import{HDKey as b}from"@scure/bip32";import{derivationPathToString as P,getWalletFormatFor as x,SwapKitError as X}from"@swapkit/helpers";function g(G){switch(G){case"bech32":return"wpkh(@0/**)";case"p2sh":return"sh(wpkh(@0/**))";case"legacy":return"pkh(@0/**)";default:return"wpkh(@0/**)"}}function u(G){return typeof G==="string"?G:P(G)}function k(G){return G.replace(/^m\//,"").replace(/^\/+/,"")}function l(G){return G.replace(/^m\//,"").split("/").filter(Boolean).map((Q)=>{let R=Q.endsWith("'"),V=Number.parseInt(R?Q.slice(0,-1):Q,10);return R?(V|2147483648)>>>0:V})}function s(G,Q){let R=G.getInput(Q);return Boolean(R.bip32Derivation?.length)}var F=({chain:G})=>{return(Q,R)=>{let V,z;async function _(){if(!V){let q=R??await C(),{AppClient:J}=await import("ledger-bitcoin");V=new J(q)}return V}async function S(){if(!z)z=await(await _()).getMasterFingerprint();return z}let $=k(Q?u(Q):"84'/0'/0'/0/0"),W=$.split("/").filter(Boolean),H=W.slice(0,3).join("/"),w=W.slice(3),E=Number(w[0]??0),K=Number(w[1]??0),L=x(`m/${$}`),T=g(L),j,N;async function O(){let q=await _(),J=await S();if(!j)j=await q.getExtendedPubkey(`m/${H}`);let{DefaultWalletPolicy:M}=await import("ledger-bitcoin"),U=new M(T,`[${J}/${H}]${j}`);return{app:q,fpr:J,policy:U,xpub:j}}async function I(){if(!N){let{xpub:q}=await O(),M=b.fromExtendedKey(q).derive(`m/${E}/${K}`);if(!M.publicKey)throw new X("wallet_ledger_get_address_error",{message:`Cannot derive leaf pubkey for ${G}`});N=M.publicKey}return N}return{connect:async()=>{await _()},getAddress:async()=>{let{app:q,policy:J}=await O(),M=await q.getWalletAddress(J,null,E,K,!1);if(!M)throw new X("wallet_ledger_get_address_error",{message:`Cannot get ${G} address from ledger derivation path: ${$}`});return M},getExtendedPublicKey:async(q=`m/${H}`)=>{return(await _()).getExtendedPubkey(`m/${k(q)}`)},signTransaction:async(q)=>{let{app:J,policy:M,fpr:U}=await O(),v=Number.parseInt(U,16)>>>0,A=l($),B=Array.from({length:q.inputsLength},(Y,Z)=>Z).filter((Y)=>!s(q,Y));if(B.length>0){let Y=await I();for(let Z of B)q.updateInput(Z,{bip32Derivation:[[Y,{fingerprint:v,path:A}]]})}let f=m.encode(q.toPSBT(0)),y=await J.signPsbt(f,M,null);for(let[Y,Z]of y)q.updateInput(Y,{partialSig:[[new Uint8Array(Z.pubkey),new Uint8Array(Z.signature)]]});return q}}}},n=F({chain:"bitcoin"}),p=F({chain:"litecoin"});export{p as LitecoinPsbtLedger,n as BitcoinPsbtLedger};
2
-
3
- //# debugId=ACED2AE00D3D0A0F64756E2164756E21
4
- //# sourceMappingURL=chunk-gwx23jdp.js.map