@swapkit/wallets 3.0.0-beta.9 → 4.0.0-beta.38

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 (145) hide show
  1. package/dist/chunk-0qaxr89s.js +3 -0
  2. package/dist/chunk-0qaxr89s.js.map +10 -0
  3. package/dist/chunk-2aa1w78q.js +3 -0
  4. package/dist/chunk-2aa1w78q.js.map +10 -0
  5. package/dist/chunk-bexr8da2.js +4 -0
  6. package/dist/chunk-bexr8da2.js.map +10 -0
  7. package/dist/chunk-dcj9twam.js +3 -0
  8. package/dist/chunk-dcj9twam.js.map +10 -0
  9. package/dist/{chunk-p1kdg37m.js → chunk-qwd1kp32.js} +2 -2
  10. package/dist/{chunk-p1kdg37m.js.map → chunk-qwd1kp32.js.map} +1 -1
  11. package/dist/chunk-sn6pgje5.js +3 -0
  12. package/dist/chunk-sn6pgje5.js.map +10 -0
  13. package/dist/chunk-th8ggrmx.js +4 -0
  14. package/dist/{chunk-6ndrbmhg.js.map → chunk-th8ggrmx.js.map} +3 -3
  15. package/dist/chunk-wfattb4a.js +3 -0
  16. package/dist/chunk-wfattb4a.js.map +10 -0
  17. package/dist/chunk-yah6cf33.js +3 -0
  18. package/dist/{chunk-hxt2nqa8.js.map → chunk-yah6cf33.js.map} +3 -3
  19. package/dist/src/bitget/index.cjs +2 -2
  20. package/dist/src/bitget/index.cjs.map +3 -3
  21. package/dist/src/bitget/index.js +2 -2
  22. package/dist/src/bitget/index.js.map +3 -3
  23. package/dist/src/coinbase/index.cjs +2 -2
  24. package/dist/src/coinbase/index.cjs.map +3 -3
  25. package/dist/src/coinbase/index.js +2 -2
  26. package/dist/src/coinbase/index.js.map +3 -3
  27. package/dist/src/cosmostation/index.cjs +3 -0
  28. package/dist/src/cosmostation/index.cjs.map +10 -0
  29. package/dist/src/cosmostation/index.js +3 -0
  30. package/dist/src/cosmostation/index.js.map +10 -0
  31. package/dist/src/ctrl/index.cjs +2 -2
  32. package/dist/src/ctrl/index.cjs.map +4 -4
  33. package/dist/src/ctrl/index.js +2 -2
  34. package/dist/src/ctrl/index.js.map +4 -4
  35. package/dist/src/evm-extensions/index.cjs +2 -2
  36. package/dist/src/evm-extensions/index.cjs.map +3 -3
  37. package/dist/src/evm-extensions/index.js +2 -2
  38. package/dist/src/evm-extensions/index.js.map +3 -3
  39. package/dist/src/exodus/index.cjs +2 -2
  40. package/dist/src/exodus/index.cjs.map +3 -3
  41. package/dist/src/exodus/index.js +2 -2
  42. package/dist/src/exodus/index.js.map +3 -3
  43. package/dist/src/index.js +2 -2
  44. package/dist/src/index.js.map +1 -1
  45. package/dist/src/keepkey/index.cjs +2 -2
  46. package/dist/src/keepkey/index.cjs.map +9 -9
  47. package/dist/src/keepkey/index.js +2 -2
  48. package/dist/src/keepkey/index.js.map +9 -9
  49. package/dist/src/keepkey-bex/index.cjs +2 -2
  50. package/dist/src/keepkey-bex/index.cjs.map +4 -4
  51. package/dist/src/keepkey-bex/index.js +2 -2
  52. package/dist/src/keepkey-bex/index.js.map +4 -4
  53. package/dist/src/keplr/index.cjs +2 -2
  54. package/dist/src/keplr/index.cjs.map +3 -3
  55. package/dist/src/keplr/index.js +2 -2
  56. package/dist/src/keplr/index.js.map +3 -3
  57. package/dist/src/keystore/index.cjs +2 -2
  58. package/dist/src/keystore/index.cjs.map +4 -4
  59. package/dist/src/keystore/index.js +2 -2
  60. package/dist/src/keystore/index.js.map +4 -4
  61. package/dist/src/ledger/index.cjs +3 -3
  62. package/dist/src/ledger/index.cjs.map +17 -15
  63. package/dist/src/ledger/index.js +3 -3
  64. package/dist/src/ledger/index.js.map +17 -15
  65. package/dist/src/okx/index.cjs +2 -2
  66. package/dist/src/okx/index.cjs.map +4 -4
  67. package/dist/src/okx/index.js +2 -2
  68. package/dist/src/okx/index.js.map +4 -4
  69. package/dist/src/onekey/index.cjs +2 -2
  70. package/dist/src/onekey/index.cjs.map +3 -3
  71. package/dist/src/onekey/index.js +2 -2
  72. package/dist/src/onekey/index.js.map +3 -3
  73. package/dist/src/phantom/index.js +2 -2
  74. package/dist/src/phantom/index.js.map +2 -2
  75. package/dist/src/polkadotjs/index.js +2 -2
  76. package/dist/src/polkadotjs/index.js.map +1 -1
  77. package/dist/src/radix/index.cjs +2 -2
  78. package/dist/src/radix/index.cjs.map +3 -3
  79. package/dist/src/radix/index.js +2 -2
  80. package/dist/src/radix/index.js.map +3 -3
  81. package/dist/src/talisman/index.js +2 -2
  82. package/dist/src/talisman/index.js.map +1 -1
  83. package/dist/src/trezor/index.cjs +2 -2
  84. package/dist/src/trezor/index.cjs.map +3 -3
  85. package/dist/src/trezor/index.js +2 -2
  86. package/dist/src/trezor/index.js.map +3 -3
  87. package/dist/src/walletconnect/index.cjs +2 -2
  88. package/dist/src/walletconnect/index.cjs.map +6 -7
  89. package/dist/src/walletconnect/index.js +2 -2
  90. package/dist/src/walletconnect/index.js.map +6 -7
  91. package/package.json +73 -28
  92. package/src/bitget/helpers.ts +4 -4
  93. package/src/coinbase/signer.ts +4 -4
  94. package/src/cosmostation/index.ts +142 -0
  95. package/src/ctrl/index.ts +83 -0
  96. package/src/ctrl/walletHelpers.ts +98 -70
  97. package/src/evm-extensions/index.ts +4 -3
  98. package/src/exodus/index.ts +8 -9
  99. package/src/helpers/near-browser-provider.d.ts +286 -0
  100. package/src/helpers/near.ts +206 -0
  101. package/src/keepkey/chains/cosmos.ts +44 -50
  102. package/src/keepkey/chains/evm.ts +16 -8
  103. package/src/keepkey/chains/mayachain.ts +3 -2
  104. package/src/keepkey/chains/thorchain.ts +3 -2
  105. package/src/keepkey/chains/utxo.ts +14 -3
  106. package/src/keepkey/coins.ts +10 -4
  107. package/src/keepkey/index.ts +15 -7
  108. package/src/keepkey-bex/index.ts +9 -14
  109. package/src/keepkey-bex/walletHelpers.ts +1 -1
  110. package/src/keplr/index.ts +4 -3
  111. package/src/keystore/helpers.ts +2 -4
  112. package/src/keystore/index.ts +2 -0
  113. package/src/ledger/clients/cosmos.ts +5 -4
  114. package/src/ledger/clients/evm.ts +7 -6
  115. package/src/ledger/clients/near.ts +86 -0
  116. package/src/ledger/clients/thorchain/helpers.ts +9 -4
  117. package/src/ledger/clients/thorchain/index.ts +3 -3
  118. package/src/ledger/clients/thorchain/lib.ts +3 -2
  119. package/src/ledger/clients/thorchain/utils.ts +20 -9
  120. package/src/ledger/clients/utxo.ts +3 -5
  121. package/src/ledger/clients/xrp.ts +66 -0
  122. package/src/ledger/cosmosTypes.ts +14 -10
  123. package/src/ledger/helpers/getLedgerAddress.ts +17 -3
  124. package/src/ledger/helpers/getLedgerClient.ts +63 -45
  125. package/src/ledger/helpers/getLedgerTransport.ts +5 -3
  126. package/src/ledger/index.ts +28 -7
  127. package/src/ledger/interfaces/CosmosLedgerInterface.ts +1 -2
  128. package/src/okx/helpers.ts +27 -9
  129. package/src/okx/index.ts +1 -0
  130. package/src/onekey/index.ts +3 -7
  131. package/src/radix/index.ts +4 -4
  132. package/src/trezor/evmSigner.ts +4 -4
  133. package/src/trezor/index.ts +14 -4
  134. package/src/types.ts +14 -0
  135. package/src/utils.ts +4 -0
  136. package/src/walletconnect/constants.ts +2 -0
  137. package/src/walletconnect/evmSigner.ts +8 -4
  138. package/src/walletconnect/helpers.ts +9 -2
  139. package/src/walletconnect/index.ts +134 -24
  140. package/dist/chunk-6ndrbmhg.js +0 -4
  141. package/dist/chunk-bhfpfqc3.js +0 -3
  142. package/dist/chunk-bhfpfqc3.js.map +0 -10
  143. package/dist/chunk-hxt2nqa8.js +0 -3
  144. package/dist/chunk-q81hzyra.js +0 -3
  145. package/dist/chunk-q81hzyra.js.map +0 -10
@@ -0,0 +1,206 @@
1
+ //TBD @towan to be moved somewhere else
2
+ import { SwapKitError } from "@swapkit/helpers";
3
+ import type { NearSigner } from "@swapkit/toolboxes/near";
4
+ import type { Account } from "near-api-js";
5
+ import {
6
+ type Action,
7
+ type Signature,
8
+ SignedTransaction,
9
+ type Transaction,
10
+ } from "near-api-js/lib/transaction";
11
+
12
+ /**
13
+ * NEAR Browser Wallet Provider Interface
14
+ * Common interface implemented by browser extension wallets
15
+ */
16
+ export interface NearBrowserWalletProvider {
17
+ // Connection methods
18
+ connect(params?: { contractId?: string; methodNames?: string[] }): Promise<
19
+ Account[] | { accountId: string }
20
+ >;
21
+ disconnect?(): Promise<void>;
22
+ signOut?(): Promise<void>; // Alternative to disconnect
23
+
24
+ // Account methods
25
+ getAccountId(): string | Promise<string>;
26
+ getAccounts?(): Promise<Account[]>;
27
+ isSignedIn(): boolean;
28
+ getPublicKey?(): Promise<string>;
29
+
30
+ // Signing methods
31
+ signMessage?(params: any): Promise<any>;
32
+ signAndSendTransaction(params: {
33
+ receiverId: string;
34
+ actions: Action[];
35
+ signerId?: string;
36
+ }): Promise<any>;
37
+ signAndSendTransactions?(params: {
38
+ transactions: Transaction[];
39
+ }): Promise<any[]>;
40
+
41
+ // Optional utility methods
42
+ request<T>(params: { method: string; params?: any }): Promise<T>;
43
+ verifyOwner?(params: {
44
+ message: string;
45
+ callbackUrl?: string;
46
+ }): Promise<any>;
47
+ getNetwork?(): Promise<{ networkId: string; nodeUrl: string }>;
48
+
49
+ // Event handlers (optional)
50
+ on?(event: string, handler: Function): void;
51
+ off?(event: string, handler: Function): void;
52
+ }
53
+
54
+ /**
55
+ * Helper to create a NEAR signer from browser extension providers
56
+ */
57
+ export async function createNearSignerFromProvider(
58
+ provider: NearBrowserWalletProvider,
59
+ walletName: string,
60
+ ) {
61
+ const isConnected = provider.isSignedIn ? provider.isSignedIn() : false;
62
+ if (!isConnected) {
63
+ await provider.connect({ contractId: "", methodNames: [] });
64
+ }
65
+
66
+ const signer = {
67
+ ...provider,
68
+ async getPublicKey() {
69
+ // Most browser wallets expose public key through message signing
70
+ const { utils } = await import("near-api-js");
71
+
72
+ if (provider.getPublicKey) {
73
+ const pubKey = await provider.getPublicKey();
74
+ return utils.PublicKey.from(pubKey);
75
+ }
76
+
77
+ if (!provider.signMessage) {
78
+ throw new SwapKitError("wallet_ledger_method_not_supported", {
79
+ wallet: walletName,
80
+ method: "getPublicKey",
81
+ });
82
+ }
83
+
84
+ // Most browser wallets don't expose public key directly
85
+ // Return a dummy public key
86
+ const { PublicKey } = await import("near-api-js/lib/utils");
87
+ // Create a dummy ed25519 public key
88
+ const dummyKeyData = Buffer.alloc(32);
89
+ const dummyKey = `ed25519:${Buffer.from(dummyKeyData).toString("base64")}`;
90
+ return PublicKey.from(dummyKey);
91
+ },
92
+
93
+ async signNep413Message(
94
+ message: string,
95
+ _accountId: string,
96
+ recipient: string,
97
+ nonce: Uint8Array,
98
+ callbackUrl?: string,
99
+ ) {
100
+ if (!provider.signMessage) {
101
+ throw new SwapKitError("wallet_ledger_method_not_supported", {
102
+ wallet: walletName,
103
+ method: "signNep413Message",
104
+ });
105
+ }
106
+
107
+ // We know signMessage exists because we checked above
108
+ const result = await (
109
+ provider as Required<Pick<NearBrowserWalletProvider, "signMessage">>
110
+ ).signMessage({
111
+ message,
112
+ recipient,
113
+ nonce: Buffer.from(nonce),
114
+ callbackUrl,
115
+ });
116
+
117
+ return result;
118
+ },
119
+
120
+ async signTransaction(transaction: Transaction) {
121
+ if (!provider.request) {
122
+ throw new SwapKitError("wallet_near_method_not_supported", {
123
+ wallet: walletName,
124
+ method: "request",
125
+ });
126
+ }
127
+
128
+ // Browser wallets typically sign and send in one operation
129
+ // This is a limitation of browser wallet APIs
130
+ const result = await provider.request<{ signatures: Signature }>({
131
+ method: "near_signTransactions",
132
+ params: {
133
+ transactions: [transaction], // must be Array type
134
+ },
135
+ });
136
+
137
+ const signedTransaction = new SignedTransaction({
138
+ transaction,
139
+ signature: result.signatures,
140
+ });
141
+
142
+ return [result.signatures.data, signedTransaction] as [
143
+ Uint8Array<ArrayBufferLike>,
144
+ SignedTransaction,
145
+ ];
146
+ },
147
+
148
+ signDelegateAction(_delegateAction: any) {
149
+ // Most browser wallets don't support delegate actions yet
150
+ return Promise.reject(
151
+ new SwapKitError("wallet_ledger_method_not_supported", {
152
+ wallet: walletName,
153
+ method: "signDelegateAction",
154
+ }),
155
+ );
156
+ },
157
+
158
+ async getAddress() {
159
+ if (provider.getAccountId) {
160
+ return provider.getAccountId();
161
+ }
162
+
163
+ if (provider.isSignedIn && !provider.isSignedIn()) {
164
+ // Try connect method for wallets that don't have requestSignIn
165
+ const result = await provider.connect();
166
+ if (Array.isArray(result) && result.length > 0 && result[0]) {
167
+ return typeof result[0] === "string" ? result[0] : result[0].accountId;
168
+ }
169
+ throw new SwapKitError("wallet_connection_rejected_by_user", {
170
+ wallet: walletName,
171
+ });
172
+ }
173
+
174
+ throw new SwapKitError("wallet_connection_rejected_by_user", {
175
+ wallet: walletName,
176
+ });
177
+ },
178
+ };
179
+
180
+ return signer as NearSigner;
181
+ }
182
+
183
+ /**
184
+ * Detect if a wallet provider supports NEAR
185
+ */
186
+ export function detectNearProvider(
187
+ window: any,
188
+ providerPath: string,
189
+ ): NearBrowserWalletProvider | null {
190
+ const parts = providerPath.split(".");
191
+ let provider = window;
192
+
193
+ for (const part of parts) {
194
+ provider = provider?.[part];
195
+ if (!provider) return null;
196
+ }
197
+
198
+ return provider;
199
+ }
200
+
201
+ /**
202
+ * Get NEAR chain ID for WalletConnect
203
+ */
204
+ export function getNearChainId(isTestnet: boolean): string {
205
+ return isTestnet ? "near:testnet" : "near:mainnet";
206
+ }
@@ -21,64 +21,58 @@ export const cosmosWalletMethods = async ({
21
21
  getFeeRateFromThorswap,
22
22
  createStargateClient,
23
23
  } = await import("@swapkit/toolboxes/cosmos");
24
+ const derivationPathString = derivationPath
25
+ ? derivationPathToString(derivationPath)
26
+ : `${DerivationPath.GAIA}/0`;
24
27
 
25
- try {
26
- const derivationPathString = derivationPath
27
- ? derivationPathToString(derivationPath)
28
- : `${DerivationPath.GAIA}/0`;
28
+ const { address: fromAddress } = (await sdk.address.cosmosGetAddress({
29
+ address_n: bip32ToAddressNList(derivationPathString),
30
+ })) as { address: string };
29
31
 
30
- const { address: fromAddress } = (await sdk.address.cosmosGetAddress({
31
- address_n: bip32ToAddressNList(derivationPathString),
32
- })) as { address: string };
32
+ const toolbox = await getCosmosToolbox(Chain.Cosmos);
33
33
 
34
- const toolbox = await getCosmosToolbox(Chain.Cosmos);
35
-
36
- if (DEFAULT_COSMOS_FEE_MAINNET.amount[0]) {
37
- DEFAULT_COSMOS_FEE_MAINNET.amount[0].amount = String(
38
- await getFeeRateFromThorswap(ChainId.Cosmos, 500),
39
- );
40
- }
34
+ if (DEFAULT_COSMOS_FEE_MAINNET.amount[0]) {
35
+ DEFAULT_COSMOS_FEE_MAINNET.amount[0].amount = String(
36
+ await getFeeRateFromThorswap(ChainId.Cosmos, 500),
37
+ );
38
+ }
41
39
 
42
- // TODO support other cosmos assets
43
- const transfer = async ({ assetValue, recipient, memo }: GenericTransferParams) => {
44
- const amount = assetValue.getBaseValue("string");
45
- const accountInfo = await toolbox.getAccount(fromAddress);
40
+ // TODO support other cosmos assets
41
+ const transfer = async ({ assetValue, recipient, memo }: GenericTransferParams) => {
42
+ const amount = assetValue.getBaseValue("string");
43
+ const accountInfo = await toolbox.getAccount(fromAddress);
46
44
 
47
- const keepKeySignedTx = await sdk.cosmos.cosmosSignAmino({
48
- signerAddress: fromAddress,
49
- signDoc: {
50
- fee: DEFAULT_COSMOS_FEE_MAINNET,
51
- memo: memo || "",
52
- sequence: accountInfo?.sequence.toString() ?? "",
53
- chain_id: ChainId.Cosmos,
54
- account_number: accountInfo?.accountNumber.toString() ?? "",
55
- msgs: [
56
- {
57
- value: {
58
- amount: [{ denom: "uatom", amount }],
59
- to_address: recipient,
60
- from_address: fromAddress,
61
- },
62
- type: "cosmos-sdk/MsgSend",
45
+ const keepKeySignedTx = await sdk.cosmos.cosmosSignAmino({
46
+ signerAddress: fromAddress,
47
+ signDoc: {
48
+ fee: DEFAULT_COSMOS_FEE_MAINNET,
49
+ memo: memo || "",
50
+ sequence: accountInfo?.sequence.toString() ?? "",
51
+ chain_id: ChainId.Cosmos,
52
+ account_number: accountInfo?.accountNumber.toString() ?? "",
53
+ msgs: [
54
+ {
55
+ value: {
56
+ amount: [{ denom: "uatom", amount }],
57
+ to_address: recipient,
58
+ from_address: fromAddress,
63
59
  },
64
- ],
65
- },
66
- });
60
+ type: "cosmos-sdk/MsgSend",
61
+ },
62
+ ],
63
+ },
64
+ });
67
65
 
68
- const decodedBytes = atob(keepKeySignedTx.serialized);
69
- const uint8Array = new Uint8Array(decodedBytes.length).map((_, i) =>
70
- decodedBytes.charCodeAt(i),
71
- );
66
+ const decodedBytes = atob(keepKeySignedTx.serialized);
67
+ const uint8Array = new Uint8Array(decodedBytes.length).map((_, i) =>
68
+ decodedBytes.charCodeAt(i),
69
+ );
72
70
 
73
- const client = await createStargateClient(SKConfig.get("rpcUrls")[Chain.Cosmos]);
74
- const response = await client.broadcastTx(uint8Array);
71
+ const client = await createStargateClient(SKConfig.get("rpcUrls")[Chain.Cosmos]);
72
+ const response = await client.broadcastTx(uint8Array);
75
73
 
76
- return response.transactionHash;
77
- };
74
+ return response.transactionHash;
75
+ };
78
76
 
79
- return { ...toolbox, transfer, address: fromAddress };
80
- } catch (e) {
81
- console.error(e);
82
- throw e;
83
- }
77
+ return { ...toolbox, transfer, address: fromAddress };
84
78
  };
@@ -4,6 +4,7 @@ import {
4
4
  ChainToChainId,
5
5
  type DerivationPathArray,
6
6
  NetworkDerivationPath,
7
+ SwapKitError,
7
8
  derivationPathToString,
8
9
  } from "@swapkit/helpers";
9
10
  import type { JsonRpcProvider, Provider, TransactionRequest } from "ethers";
@@ -35,7 +36,7 @@ export class KeepKeySigner extends AbstractSigner {
35
36
  }
36
37
 
37
38
  signTypedData(): Promise<string> {
38
- throw new Error("this method is not implemented");
39
+ throw new SwapKitError("wallet_keepkey_method_not_supported", { method: "signTypedData" });
39
40
  }
40
41
 
41
42
  getAddress = async () => {
@@ -62,14 +63,21 @@ export class KeepKeySigner extends AbstractSigner {
62
63
  gasPrice,
63
64
  // biome-ignore lint/complexity/noExcessiveCognitiveComplexity: <explanation>
64
65
  }: TransactionRequest) => {
65
- if (!to) throw new Error("Missing to address");
66
- if (!gasLimit) throw new Error("Missing gasLimit");
67
- if (!data) throw new Error("Missing data");
66
+ if (!to)
67
+ throw new SwapKitError("wallet_keepkey_invalid_params", { reason: "Missing to address" });
68
+ if (!gasLimit)
69
+ throw new SwapKitError("wallet_keepkey_invalid_params", { reason: "Missing gasLimit" });
70
+ if (!data) throw new SwapKitError("wallet_keepkey_invalid_params", { reason: "Missing data" });
68
71
 
69
72
  const isEIP1559 = !!((maxFeePerGas || maxPriorityFeePerGas) && !gasPrice);
70
- if (isEIP1559 && !maxFeePerGas) throw new Error("Missing maxFeePerGas");
71
- if (isEIP1559 && !maxPriorityFeePerGas) throw new Error("Missing maxFeePerGas");
72
- if (!(isEIP1559 || gasPrice)) throw new Error("Missing gasPrice");
73
+ if (isEIP1559 && !maxFeePerGas)
74
+ throw new SwapKitError("wallet_keepkey_invalid_params", { reason: "Missing maxFeePerGas" });
75
+ if (isEIP1559 && !maxPriorityFeePerGas)
76
+ throw new SwapKitError("wallet_keepkey_invalid_params", {
77
+ reason: "Missing maxPriorityFeePerGas",
78
+ });
79
+ if (!(isEIP1559 || gasPrice))
80
+ throw new SwapKitError("wallet_keepkey_invalid_params", { reason: "Missing gasPrice" });
73
81
 
74
82
  const { toHexString } = await import("@swapkit/toolboxes/evm");
75
83
 
@@ -100,7 +108,7 @@ export class KeepKeySigner extends AbstractSigner {
100
108
  };
101
109
 
102
110
  sendTransaction = async (tx: TransactionRequest): Promise<any> => {
103
- if (!this.provider) throw new Error("No provider set");
111
+ if (!this.provider) throw new SwapKitError("wallet_keepkey_no_provider");
104
112
 
105
113
  const signedTxHex = await this.signTransaction(tx);
106
114
 
@@ -7,6 +7,7 @@ import {
7
7
  type DerivationPathArray,
8
8
  type GenericTransferParams,
9
9
  SKConfig,
10
+ SwapKitError,
10
11
  derivationPathToString,
11
12
  } from "@swapkit/helpers";
12
13
  import type { ThorchainDepositParams } from "@swapkit/toolboxes/cosmos";
@@ -44,11 +45,11 @@ export const mayachainWalletMethods = async ({
44
45
  sender,
45
46
  memo,
46
47
  }: SignTransactionParams) => {
47
- const { makeSignDoc } = await import("@cosmjs/amino");
48
+ const { makeSignDoc } = (await import("@cosmjs/amino")).default;
48
49
  const { getDenomWithChain } = await import("@swapkit/toolboxes/cosmos");
49
50
 
50
51
  const account = await toolbox.getAccount(sender);
51
- if (!account) throw new Error("Account not found");
52
+ if (!account) throw new SwapKitError("wallet_keepkey_account_not_found");
52
53
  const { accountNumber, sequence = 0 } = account;
53
54
  const amount = assetValue.getBaseValue("string");
54
55
 
@@ -11,6 +11,7 @@ import {
11
11
  type DerivationPathArray,
12
12
  type GenericTransferParams,
13
13
  SKConfig,
14
+ SwapKitError,
14
15
  derivationPathToString,
15
16
  } from "@swapkit/helpers";
16
17
  import type { ThorchainDepositParams } from "@swapkit/toolboxes/cosmos";
@@ -31,7 +32,7 @@ export const thorchainWalletMethods = async ({
31
32
  sdk: KeepKeySdk;
32
33
  derivationPath?: DerivationPathArray;
33
34
  }) => {
34
- const { makeSignDoc } = await import("@cosmjs/amino");
35
+ const { makeSignDoc } = (await import("@cosmjs/amino")).default;
35
36
  const { buildAminoMsg, getDefaultChainFee, createStargateClient, getCosmosToolbox } =
36
37
  await import("@swapkit/toolboxes/cosmos");
37
38
 
@@ -51,7 +52,7 @@ export const thorchainWalletMethods = async ({
51
52
  memo,
52
53
  }: SignTransactionParams) => {
53
54
  const account = await toolbox.getAccount(sender);
54
- if (!account) throw new Error("Account not found");
55
+ if (!account) throw new SwapKitError("wallet_keepkey_account_not_found");
55
56
  const { accountNumber, sequence = 0 } = account;
56
57
 
57
58
  const isTransfer = recipient && recipient !== "";
@@ -5,6 +5,7 @@ import {
5
5
  type DerivationPathArray,
6
6
  FeeOption,
7
7
  type GenericTransferParams,
8
+ SwapKitError,
8
9
  type UTXOChain,
9
10
  derivationPathToString,
10
11
  } from "@swapkit/helpers";
@@ -26,7 +27,11 @@ export const utxoWalletMethods = async ({
26
27
  sdk,
27
28
  chain,
28
29
  derivationPath,
29
- }: { sdk: KeepKeySdk; chain: UTXOChain; derivationPath?: DerivationPathArray }) => {
30
+ }: {
31
+ sdk: KeepKeySdk;
32
+ chain: Exclude<UTXOChain, Chain.Zcash>;
33
+ derivationPath?: DerivationPathArray;
34
+ }) => {
30
35
  const { getUtxoToolbox } = await import("@swapkit/toolboxes/utxo");
31
36
  // This might not work for BCH
32
37
  const toolbox = await getUtxoToolbox(chain);
@@ -101,8 +106,14 @@ export const utxoWalletMethods = async ({
101
106
  memo,
102
107
  ...rest
103
108
  }: GenericTransferParams) => {
104
- if (!walletAddress) throw new Error("From address must be provided");
105
- if (!recipient) throw new Error("Recipient address must be provided");
109
+ if (!walletAddress)
110
+ throw new SwapKitError("wallet_keepkey_invalid_params", {
111
+ reason: "From address must be provided",
112
+ });
113
+ if (!recipient)
114
+ throw new SwapKitError("wallet_keepkey_invalid_params", {
115
+ reason: "Recipient address must be provided",
116
+ });
106
117
 
107
118
  const createTxMethod =
108
119
  chain === Chain.BitcoinCash
@@ -2,6 +2,8 @@
2
2
  KeepKey Specific bip32 path conventions
3
3
  */
4
4
 
5
+ import { SwapKitError } from "@swapkit/helpers";
6
+
5
7
  const HARDENED = 0x80000000;
6
8
 
7
9
  export enum ChainToKeepKeyName {
@@ -27,7 +29,9 @@ export function bip32ToAddressNList(initPath: string): number[] {
27
29
  let path = initPath;
28
30
 
29
31
  if (!bip32Like(path)) {
30
- throw new Error(`Not a bip32 path: '${path}'`);
32
+ throw new SwapKitError("wallet_keepkey_invalid_params", {
33
+ reason: `Not a bip32 path: '${path}'`,
34
+ });
31
35
  }
32
36
 
33
37
  if (/^m\//i.test(path)) {
@@ -44,18 +48,20 @@ export function bip32ToAddressNList(initPath: string): number[] {
44
48
  const segment = segments[i];
45
49
  if (segment) {
46
50
  const tmp = /(\d+)([hH']?)/.exec(segment);
47
- if (tmp === null) throw new Error("Invalid input");
51
+ if (tmp === null)
52
+ throw new SwapKitError("wallet_keepkey_invalid_params", { reason: "Invalid input" });
48
53
 
49
54
  const [, num = "", modifier = ""] = tmp;
50
55
 
51
56
  ret[i] = Number.parseInt(num, 10);
52
57
 
53
- if (ret[i] >= HARDENED) throw new Error("Invalid child index");
58
+ if (ret[i] >= HARDENED)
59
+ throw new SwapKitError("wallet_keepkey_invalid_params", { reason: "Invalid child index" });
54
60
 
55
61
  if (modifier === "h" || modifier === "H" || modifier === "'") {
56
62
  ret[i] += HARDENED;
57
63
  } else if (modifier.length > 0) {
58
- throw new Error("Invalid modifier");
64
+ throw new SwapKitError("wallet_keepkey_invalid_params", { reason: "Invalid modifier" });
59
65
  }
60
66
  }
61
67
  }
@@ -3,6 +3,7 @@ import {
3
3
  type DerivationPathArray,
4
4
  NetworkDerivationPath,
5
5
  SKConfig,
6
+ SwapKitError,
6
7
  WalletOption,
7
8
  createWallet,
8
9
  filterSupportedChains,
@@ -44,15 +45,22 @@ export const keepkeyWallet = createWallet({
44
45
  derivationPathMap?: Record<Chain, DerivationPathArray>,
45
46
  ) {
46
47
  const filteredChains = filterSupportedChains({ chains, supportedChains, walletType });
47
- const config = SKConfig.get("integrations").keepKey;
48
+ const pairingInfo = SKConfig.get("integrations").keepKey;
49
+ if (!pairingInfo) throw new Error("KeepKey config not found");
48
50
 
49
- if (!config) throw new Error("KeepKey config not found");
51
+ const initialApiKey = SKConfig.get("apiKeys").keepKey || "1234";
50
52
 
51
53
  await checkAndLaunch();
52
54
 
53
- const keepkeyConfig = { ...config, apiKey: SKConfig.get("apiKeys").keepKey };
55
+ // Conform to the expected { apiKey, pairingInfo } structure
56
+ const keepkeyConfig = { apiKey: initialApiKey, pairingInfo };
54
57
  const keepKeySdk = await KeepKeySdk.create(keepkeyConfig);
55
58
 
59
+ // Persist the new API key via SKConfig after pairing
60
+ if (keepkeyConfig.apiKey && keepkeyConfig.apiKey !== initialApiKey) {
61
+ SKConfig.setApiKey("keepKey", keepkeyConfig.apiKey);
62
+ }
63
+
56
64
  await Promise.all(
57
65
  filteredChains.map(async (chain) => {
58
66
  const walletMethods = await getWalletMethods({
@@ -84,6 +92,7 @@ async function getWalletMethods({
84
92
  case Chain.Optimism:
85
93
  case Chain.Polygon:
86
94
  case Chain.Avalanche:
95
+ case Chain.Base:
87
96
  case Chain.Ethereum: {
88
97
  const provider = await getProvider(chain);
89
98
  const signer = new KeepKeySigner({ sdk, chain, derivationPath, provider });
@@ -108,11 +117,11 @@ async function getWalletMethods({
108
117
  return utxoWalletMethods({ sdk, chain, derivationPath });
109
118
  }
110
119
  default:
111
- throw new Error(`Chain not supported ${chain}`);
120
+ throw new SwapKitError("wallet_keepkey_chain_not_supported", { chain });
112
121
  }
113
122
  }
114
123
 
115
- // kk-sdk docs: https://medium.com/@highlander_35968/building-on-the-keepkey-sdk-2023fda41f38
124
+ // kk-sdk docs: https://keepkey.com/blog/building_on_the_keepkey_sdk
116
125
  // test spec: if offline, launch keepkey-bridge
117
126
  async function checkAndLaunch(attempts = 0) {
118
127
  if (attempts >= 3) {
@@ -133,8 +142,7 @@ async function checkKeepkeyAvailability(spec = "http://localhost:1646/spec/swagg
133
142
  try {
134
143
  const response = await fetch(spec);
135
144
  return response.status === 200;
136
- } catch (error) {
137
- console.error(error);
145
+ } catch (_error) {
138
146
  return false;
139
147
  }
140
148
  }
@@ -83,11 +83,11 @@ async function getWalletMethods(chain: (typeof KEEPKEY_BEX_SUPPORTED_CHAINS)[num
83
83
 
84
84
  // @ts-expect-error assumed available connection
85
85
  const signer = window.keepkey?.cosmos?.getOfflineSignerOnlyAmino(ChainIdToChain[chain]);
86
- if (!signer) throw new Error("Could not load signer");
86
+ if (!signer) throw new SwapKitError("wallet_keepkey_signer_not_found");
87
87
  const toolbox = getCosmosToolbox(chain, { signer });
88
88
 
89
89
  const accounts = await signer.getAccounts();
90
- if (!accounts?.[0]?.address) throw new Error("No accounts found");
90
+ if (!accounts?.[0]?.address) throw new SwapKitError("wallet_keepkey_no_accounts");
91
91
 
92
92
  const [{ address }] = accounts;
93
93
 
@@ -103,18 +103,13 @@ async function getWalletMethods(chain: (typeof KEEPKEY_BEX_SUPPORTED_CHAINS)[num
103
103
  const toolbox = await getUtxoToolbox(chain);
104
104
 
105
105
  const getBalance = async () => {
106
- try {
107
- const providerChain = getProviderNameFromChain(chain);
108
- // @ts-expect-error We assuming there chains via switch
109
- const balance = await window?.keepkey?.[providerChain]?.request({
110
- method: "request_balance",
111
- });
112
- const assetValue = AssetValue.from({ chain, value: balance[0].balance });
113
- return [assetValue];
114
- } catch (error) {
115
- console.error("Error fetching balance:", error);
116
- throw error;
117
- }
106
+ const providerChain = getProviderNameFromChain(chain);
107
+ // @ts-expect-error We assuming there chains via switch
108
+ const balance = await window?.keepkey?.[providerChain]?.request({
109
+ method: "request_balance",
110
+ });
111
+ const assetValue = AssetValue.from({ chain, value: balance[0].balance });
112
+ return [assetValue];
118
113
  };
119
114
 
120
115
  return { ...toolbox, getBalance, transfer: walletTransfer };
@@ -60,7 +60,7 @@ export const getProviderNameFromChain = (chain: Chain): string => {
60
60
  case Chain.Litecoin:
61
61
  return "litecoin";
62
62
  default:
63
- throw new Error("Unsupported chain");
63
+ throw new SwapKitError("wallet_keepkey_chain_not_supported", { chain });
64
64
  }
65
65
  };
66
66
 
@@ -2,6 +2,7 @@ import {
2
2
  Chain,
3
3
  ChainId,
4
4
  ChainToChainId,
5
+ SwapKitError,
5
6
  WalletOption,
6
7
  createWallet,
7
8
  filterSupportedChains,
@@ -28,19 +29,19 @@ export const keplrWallet = createWallet({
28
29
 
29
30
  if (!keplrSupportedChainIds.includes(chainId)) {
30
31
  const chainConfig = chainRegistry.get(chainId);
31
- if (!chainConfig) throw new Error(`Unsupported chain ${chain}`);
32
+ if (!chainConfig) throw new SwapKitError("wallet_keplr_chain_not_supported", { chain });
32
33
 
33
34
  await keplrClient.experimentalSuggestChain(chainConfig);
34
35
  }
35
36
 
36
37
  keplrClient?.enable(chainId);
37
38
  const signer = keplrClient?.getOfflineSignerOnlyAmino(chainId);
38
- if (!signer) throw new Error("Could not load signer");
39
+ if (!signer) throw new SwapKitError("wallet_keplr_signer_not_found");
39
40
 
40
41
  const { getCosmosToolbox } = await import("@swapkit/toolboxes/cosmos");
41
42
 
42
43
  const accounts = await signer.getAccounts();
43
- if (!accounts?.[0]?.address) throw new Error("No accounts found");
44
+ if (!accounts?.[0]?.address) throw new SwapKitError("wallet_keplr_no_accounts");
44
45
 
45
46
  const [{ address }] = accounts;
46
47
  const toolbox = getCosmosToolbox(chain, { signer });