@sidhujag/sysweb3-keyring 1.0.545 → 1.0.547

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 (212) hide show
  1. package/coverage/clover.xml +2875 -0
  2. package/coverage/coverage-final.json +29468 -0
  3. package/coverage/lcov-report/base.css +354 -0
  4. package/coverage/lcov-report/block-navigation.js +85 -0
  5. package/coverage/lcov-report/favicon.png +0 -0
  6. package/coverage/lcov-report/index.html +320 -0
  7. package/coverage/lcov-report/prettify.css +101 -0
  8. package/coverage/lcov-report/prettify.js +1008 -0
  9. package/coverage/lcov-report/sort-arrow-sprite.png +0 -0
  10. package/coverage/lcov-report/sorter.js +191 -0
  11. package/coverage/lcov-report/src/index.html +276 -0
  12. package/coverage/lcov-report/src/index.ts.html +114 -0
  13. package/coverage/lcov-report/src/initial-state.ts.html +558 -0
  14. package/coverage/lcov-report/src/keyring-manager.ts.html +6279 -0
  15. package/coverage/lcov-report/src/ledger/bitcoin_client/index.html +178 -0
  16. package/coverage/lcov-report/src/ledger/bitcoin_client/index.ts.html +144 -0
  17. package/coverage/lcov-report/src/ledger/bitcoin_client/lib/appClient.ts.html +1560 -0
  18. package/coverage/lcov-report/src/ledger/bitcoin_client/lib/bip32.ts.html +276 -0
  19. package/coverage/lcov-report/src/ledger/bitcoin_client/lib/buffertools.ts.html +495 -0
  20. package/coverage/lcov-report/src/ledger/bitcoin_client/lib/clientCommands.ts.html +1138 -0
  21. package/coverage/lcov-report/src/ledger/bitcoin_client/lib/index.html +363 -0
  22. package/coverage/lcov-report/src/ledger/bitcoin_client/lib/merkelizedPsbt.ts.html +289 -0
  23. package/coverage/lcov-report/src/ledger/bitcoin_client/lib/merkle.ts.html +486 -0
  24. package/coverage/lcov-report/src/ledger/bitcoin_client/lib/merkleMap.ts.html +240 -0
  25. package/coverage/lcov-report/src/ledger/bitcoin_client/lib/policy.ts.html +342 -0
  26. package/coverage/lcov-report/src/ledger/bitcoin_client/lib/psbtv2.ts.html +2388 -0
  27. package/coverage/lcov-report/src/ledger/bitcoin_client/lib/varint.ts.html +453 -0
  28. package/coverage/lcov-report/src/ledger/consts.ts.html +177 -0
  29. package/coverage/lcov-report/src/ledger/index.html +216 -0
  30. package/coverage/lcov-report/src/ledger/index.ts.html +1371 -0
  31. package/coverage/lcov-report/src/ledger/utils.ts.html +102 -0
  32. package/coverage/lcov-report/src/signers.ts.html +591 -0
  33. package/coverage/lcov-report/src/storage.ts.html +198 -0
  34. package/coverage/lcov-report/src/transactions/ethereum.ts.html +5826 -0
  35. package/coverage/lcov-report/src/transactions/index.html +216 -0
  36. package/coverage/lcov-report/src/transactions/index.ts.html +93 -0
  37. package/coverage/lcov-report/src/transactions/syscoin.ts.html +1521 -0
  38. package/coverage/lcov-report/src/trezor/index.html +176 -0
  39. package/coverage/lcov-report/src/trezor/index.ts.html +2655 -0
  40. package/coverage/lcov-report/src/types.ts.html +1443 -0
  41. package/coverage/lcov-report/src/utils/derivation-paths.ts.html +486 -0
  42. package/coverage/lcov-report/src/utils/index.html +196 -0
  43. package/coverage/lcov-report/src/utils/psbt.ts.html +159 -0
  44. package/coverage/lcov-report/test/helpers/constants.ts.html +627 -0
  45. package/coverage/lcov-report/test/helpers/index.html +176 -0
  46. package/coverage/lcov.info +4832 -0
  47. package/{cjs → dist/cjs}/ledger/bitcoin_client/lib/appClient.js +1 -124
  48. package/dist/cjs/ledger/bitcoin_client/lib/appClient.js.map +1 -0
  49. package/{cjs → dist/cjs}/transactions/ethereum.js +6 -2
  50. package/dist/cjs/transactions/ethereum.js.map +1 -0
  51. package/dist/package.json +50 -0
  52. package/{types → dist/types}/ledger/bitcoin_client/lib/appClient.d.ts +0 -6
  53. package/examples/basic-usage.js +140 -0
  54. package/jest.config.js +32 -0
  55. package/package.json +31 -13
  56. package/readme.md +201 -0
  57. package/src/declare.d.ts +7 -0
  58. package/src/errorUtils.ts +83 -0
  59. package/src/hardware-wallet-manager.ts +655 -0
  60. package/src/index.ts +12 -0
  61. package/src/initial-state.ts +108 -0
  62. package/src/keyring-manager.ts +2698 -0
  63. package/src/ledger/bitcoin_client/index.ts +19 -0
  64. package/src/ledger/bitcoin_client/lib/appClient.ts +405 -0
  65. package/src/ledger/bitcoin_client/lib/bip32.ts +61 -0
  66. package/src/ledger/bitcoin_client/lib/buffertools.ts +134 -0
  67. package/src/ledger/bitcoin_client/lib/clientCommands.ts +356 -0
  68. package/src/ledger/bitcoin_client/lib/constants.ts +12 -0
  69. package/src/ledger/bitcoin_client/lib/merkelizedPsbt.ts +65 -0
  70. package/src/ledger/bitcoin_client/lib/merkle.ts +136 -0
  71. package/src/ledger/bitcoin_client/lib/merkleMap.ts +49 -0
  72. package/src/ledger/bitcoin_client/lib/policy.ts +91 -0
  73. package/src/ledger/bitcoin_client/lib/psbtv2.ts +768 -0
  74. package/src/ledger/bitcoin_client/lib/varint.ts +120 -0
  75. package/src/ledger/consts.ts +3 -0
  76. package/src/ledger/index.ts +685 -0
  77. package/src/ledger/types.ts +74 -0
  78. package/src/network-utils.ts +99 -0
  79. package/src/providers.ts +345 -0
  80. package/src/signers.ts +158 -0
  81. package/src/storage.ts +63 -0
  82. package/src/transactions/__tests__/integration.test.ts +303 -0
  83. package/src/transactions/__tests__/syscoin.test.ts +409 -0
  84. package/src/transactions/ethereum.ts +2503 -0
  85. package/src/transactions/index.ts +2 -0
  86. package/src/transactions/syscoin.ts +542 -0
  87. package/src/trezor/index.ts +1050 -0
  88. package/src/types.ts +366 -0
  89. package/src/utils/derivation-paths.ts +133 -0
  90. package/src/utils/psbt.ts +24 -0
  91. package/src/utils.ts +191 -0
  92. package/test/README.md +158 -0
  93. package/test/__mocks__/ledger-mock.js +20 -0
  94. package/test/__mocks__/trezor-mock.js +75 -0
  95. package/test/cleanup-summary.md +167 -0
  96. package/test/helpers/README.md +78 -0
  97. package/test/helpers/constants.ts +79 -0
  98. package/test/helpers/setup.ts +714 -0
  99. package/test/integration/import-validation.spec.ts +588 -0
  100. package/test/unit/hardware/ledger.spec.ts +869 -0
  101. package/test/unit/hardware/trezor.spec.ts +828 -0
  102. package/test/unit/keyring-manager/account-management.spec.ts +970 -0
  103. package/test/unit/keyring-manager/import-watchonly.spec.ts +181 -0
  104. package/test/unit/keyring-manager/import-wif.spec.ts +126 -0
  105. package/test/unit/keyring-manager/initialization.spec.ts +782 -0
  106. package/test/unit/keyring-manager/key-derivation.spec.ts +996 -0
  107. package/test/unit/keyring-manager/security.spec.ts +505 -0
  108. package/test/unit/keyring-manager/state-management.spec.ts +375 -0
  109. package/test/unit/network/network-management.spec.ts +372 -0
  110. package/test/unit/transactions/ethereum-transactions.spec.ts +382 -0
  111. package/test/unit/transactions/syscoin-transactions.spec.ts +615 -0
  112. package/tsconfig.json +14 -0
  113. package/cjs/ledger/bitcoin_client/lib/appClient.js.map +0 -1
  114. package/cjs/transactions/ethereum.js.map +0 -1
  115. /package/{README.md → dist/README.md} +0 -0
  116. /package/{cjs → dist/cjs}/errorUtils.js +0 -0
  117. /package/{cjs → dist/cjs}/errorUtils.js.map +0 -0
  118. /package/{cjs → dist/cjs}/hardware-wallet-manager.js +0 -0
  119. /package/{cjs → dist/cjs}/hardware-wallet-manager.js.map +0 -0
  120. /package/{cjs → dist/cjs}/index.js +0 -0
  121. /package/{cjs → dist/cjs}/index.js.map +0 -0
  122. /package/{cjs → dist/cjs}/initial-state.js +0 -0
  123. /package/{cjs → dist/cjs}/initial-state.js.map +0 -0
  124. /package/{cjs → dist/cjs}/keyring-manager.js +0 -0
  125. /package/{cjs → dist/cjs}/keyring-manager.js.map +0 -0
  126. /package/{cjs → dist/cjs}/ledger/bitcoin_client/index.js +0 -0
  127. /package/{cjs → dist/cjs}/ledger/bitcoin_client/index.js.map +0 -0
  128. /package/{cjs → dist/cjs}/ledger/bitcoin_client/lib/bip32.js +0 -0
  129. /package/{cjs → dist/cjs}/ledger/bitcoin_client/lib/bip32.js.map +0 -0
  130. /package/{cjs → dist/cjs}/ledger/bitcoin_client/lib/buffertools.js +0 -0
  131. /package/{cjs → dist/cjs}/ledger/bitcoin_client/lib/buffertools.js.map +0 -0
  132. /package/{cjs → dist/cjs}/ledger/bitcoin_client/lib/clientCommands.js +0 -0
  133. /package/{cjs → dist/cjs}/ledger/bitcoin_client/lib/clientCommands.js.map +0 -0
  134. /package/{cjs → dist/cjs}/ledger/bitcoin_client/lib/constants.js +0 -0
  135. /package/{cjs → dist/cjs}/ledger/bitcoin_client/lib/constants.js.map +0 -0
  136. /package/{cjs → dist/cjs}/ledger/bitcoin_client/lib/merkelizedPsbt.js +0 -0
  137. /package/{cjs → dist/cjs}/ledger/bitcoin_client/lib/merkelizedPsbt.js.map +0 -0
  138. /package/{cjs → dist/cjs}/ledger/bitcoin_client/lib/merkle.js +0 -0
  139. /package/{cjs → dist/cjs}/ledger/bitcoin_client/lib/merkle.js.map +0 -0
  140. /package/{cjs → dist/cjs}/ledger/bitcoin_client/lib/merkleMap.js +0 -0
  141. /package/{cjs → dist/cjs}/ledger/bitcoin_client/lib/merkleMap.js.map +0 -0
  142. /package/{cjs → dist/cjs}/ledger/bitcoin_client/lib/policy.js +0 -0
  143. /package/{cjs → dist/cjs}/ledger/bitcoin_client/lib/policy.js.map +0 -0
  144. /package/{cjs → dist/cjs}/ledger/bitcoin_client/lib/psbtv2.js +0 -0
  145. /package/{cjs → dist/cjs}/ledger/bitcoin_client/lib/psbtv2.js.map +0 -0
  146. /package/{cjs → dist/cjs}/ledger/bitcoin_client/lib/varint.js +0 -0
  147. /package/{cjs → dist/cjs}/ledger/bitcoin_client/lib/varint.js.map +0 -0
  148. /package/{cjs → dist/cjs}/ledger/consts.js +0 -0
  149. /package/{cjs → dist/cjs}/ledger/consts.js.map +0 -0
  150. /package/{cjs → dist/cjs}/ledger/index.js +0 -0
  151. /package/{cjs → dist/cjs}/ledger/index.js.map +0 -0
  152. /package/{cjs → dist/cjs}/ledger/types.js +0 -0
  153. /package/{cjs → dist/cjs}/ledger/types.js.map +0 -0
  154. /package/{cjs → dist/cjs}/network-utils.js +0 -0
  155. /package/{cjs → dist/cjs}/network-utils.js.map +0 -0
  156. /package/{cjs → dist/cjs}/providers.js +0 -0
  157. /package/{cjs → dist/cjs}/providers.js.map +0 -0
  158. /package/{cjs → dist/cjs}/signers.js +0 -0
  159. /package/{cjs → dist/cjs}/signers.js.map +0 -0
  160. /package/{cjs → dist/cjs}/storage.js +0 -0
  161. /package/{cjs → dist/cjs}/storage.js.map +0 -0
  162. /package/{cjs → dist/cjs}/transactions/__tests__/integration.test.js +0 -0
  163. /package/{cjs → dist/cjs}/transactions/__tests__/integration.test.js.map +0 -0
  164. /package/{cjs → dist/cjs}/transactions/__tests__/syscoin.test.js +0 -0
  165. /package/{cjs → dist/cjs}/transactions/__tests__/syscoin.test.js.map +0 -0
  166. /package/{cjs → dist/cjs}/transactions/index.js +0 -0
  167. /package/{cjs → dist/cjs}/transactions/index.js.map +0 -0
  168. /package/{cjs → dist/cjs}/transactions/syscoin.js +0 -0
  169. /package/{cjs → dist/cjs}/transactions/syscoin.js.map +0 -0
  170. /package/{cjs → dist/cjs}/trezor/index.js +0 -0
  171. /package/{cjs → dist/cjs}/trezor/index.js.map +0 -0
  172. /package/{cjs → dist/cjs}/types.js +0 -0
  173. /package/{cjs → dist/cjs}/types.js.map +0 -0
  174. /package/{cjs → dist/cjs}/utils/derivation-paths.js +0 -0
  175. /package/{cjs → dist/cjs}/utils/derivation-paths.js.map +0 -0
  176. /package/{cjs → dist/cjs}/utils/psbt.js +0 -0
  177. /package/{cjs → dist/cjs}/utils/psbt.js.map +0 -0
  178. /package/{cjs → dist/cjs}/utils.js +0 -0
  179. /package/{cjs → dist/cjs}/utils.js.map +0 -0
  180. /package/{types → dist/types}/errorUtils.d.ts +0 -0
  181. /package/{types → dist/types}/hardware-wallet-manager.d.ts +0 -0
  182. /package/{types → dist/types}/index.d.ts +0 -0
  183. /package/{types → dist/types}/initial-state.d.ts +0 -0
  184. /package/{types → dist/types}/keyring-manager.d.ts +0 -0
  185. /package/{types → dist/types}/ledger/bitcoin_client/index.d.ts +0 -0
  186. /package/{types → dist/types}/ledger/bitcoin_client/lib/bip32.d.ts +0 -0
  187. /package/{types → dist/types}/ledger/bitcoin_client/lib/buffertools.d.ts +0 -0
  188. /package/{types → dist/types}/ledger/bitcoin_client/lib/clientCommands.d.ts +0 -0
  189. /package/{types → dist/types}/ledger/bitcoin_client/lib/constants.d.ts +0 -0
  190. /package/{types → dist/types}/ledger/bitcoin_client/lib/merkelizedPsbt.d.ts +0 -0
  191. /package/{types → dist/types}/ledger/bitcoin_client/lib/merkle.d.ts +0 -0
  192. /package/{types → dist/types}/ledger/bitcoin_client/lib/merkleMap.d.ts +0 -0
  193. /package/{types → dist/types}/ledger/bitcoin_client/lib/policy.d.ts +0 -0
  194. /package/{types → dist/types}/ledger/bitcoin_client/lib/psbtv2.d.ts +0 -0
  195. /package/{types → dist/types}/ledger/bitcoin_client/lib/varint.d.ts +0 -0
  196. /package/{types → dist/types}/ledger/consts.d.ts +0 -0
  197. /package/{types → dist/types}/ledger/index.d.ts +0 -0
  198. /package/{types → dist/types}/ledger/types.d.ts +0 -0
  199. /package/{types → dist/types}/network-utils.d.ts +0 -0
  200. /package/{types → dist/types}/providers.d.ts +0 -0
  201. /package/{types → dist/types}/signers.d.ts +0 -0
  202. /package/{types → dist/types}/storage.d.ts +0 -0
  203. /package/{types → dist/types}/transactions/__tests__/integration.test.d.ts +0 -0
  204. /package/{types → dist/types}/transactions/__tests__/syscoin.test.d.ts +0 -0
  205. /package/{types → dist/types}/transactions/ethereum.d.ts +0 -0
  206. /package/{types → dist/types}/transactions/index.d.ts +0 -0
  207. /package/{types → dist/types}/transactions/syscoin.d.ts +0 -0
  208. /package/{types → dist/types}/trezor/index.d.ts +0 -0
  209. /package/{types → dist/types}/types.d.ts +0 -0
  210. /package/{types → dist/types}/utils/derivation-paths.d.ts +0 -0
  211. /package/{types → dist/types}/utils/psbt.d.ts +0 -0
  212. /package/{types → dist/types}/utils.d.ts +0 -0
package/src/types.ts ADDED
@@ -0,0 +1,366 @@
1
+ import { TransactionResponse } from '@ethersproject/abstract-provider';
2
+ import { BigNumber, BigNumberish } from '@ethersproject/bignumber';
3
+ import { BytesLike } from '@ethersproject/bytes';
4
+ import { AccessListish } from '@ethersproject/transactions';
5
+ import { Wallet } from '@ethersproject/wallet';
6
+ import {
7
+ TypedDataV1,
8
+ TypedMessage,
9
+ SignTypedDataVersion,
10
+ } from '@metamask/eth-sig-util';
11
+ import { INetwork, INetworkType } from '@sidhujag/sysweb3-network';
12
+ import { ITxid } from '@sidhujag/sysweb3-utils';
13
+ import { CustomJsonRpcProvider, CustomL2JsonRpcProvider } from 'providers';
14
+
15
+ import { LedgerKeyring } from './ledger';
16
+ import { SyscoinHDSigner } from './signers';
17
+ import { TrezorKeyring } from './trezor';
18
+
19
+ export type SimpleTransactionRequest = {
20
+ accessList?: AccessListish;
21
+ ccipReadEnabled?: boolean;
22
+ chainId: number;
23
+ customData?: Record<string, any>;
24
+ data?: BytesLike;
25
+
26
+ from: string;
27
+ gasLimit?: BigNumberish;
28
+ gasPrice?: BigNumberish;
29
+
30
+ maxFeePerGas: BigNumberish;
31
+ maxPriorityFeePerGas: BigNumberish;
32
+
33
+ nonce?: BigNumberish;
34
+ r?: string;
35
+
36
+ s?: string;
37
+ to: string;
38
+ type?: number;
39
+ v?: string;
40
+ value?: BigNumberish;
41
+ };
42
+
43
+ // Version type is now replaced by SignTypedDataVersion from @metamask/eth-sig-util
44
+
45
+ export interface IEthereumTransactions {
46
+ cancelSentTransaction: (
47
+ txHash: string,
48
+ isLegacy?: boolean,
49
+ fallbackNonce?: number
50
+ ) => Promise<{
51
+ error?: boolean;
52
+ isCanceled: boolean;
53
+ transaction?: TransactionResponse;
54
+ }>;
55
+ decryptMessage: (msgParams: string[]) => string;
56
+ ethSign: (params: string[]) => Promise<string>;
57
+ getBalance: (address: string) => Promise<number>;
58
+ getEncryptedPubKey: () => string;
59
+ getErc20TokensByAddress?: (
60
+ address: string,
61
+ isSupported: boolean,
62
+ apiUrl: string
63
+ ) => Promise<any[]>;
64
+ getFeeByType: (type: string) => Promise<string>;
65
+ getFeeDataWithDynamicMaxPriorityFeePerGas: () => Promise<any>;
66
+ getGasLimit: (toAddress: string) => Promise<number>;
67
+ getGasOracle?: () => Promise<any>;
68
+ getRecommendedNonce: (address: string) => Promise<number>;
69
+ signTypedData: (
70
+ addr: string,
71
+ typedData: TypedDataV1 | TypedMessage<any>,
72
+ version: SignTypedDataVersion
73
+ ) => Promise<string>;
74
+
75
+ importAccount: (mnemonicOrPrivKey: string) => Wallet;
76
+ parsePersonalMessage: (hexMsg: string) => string;
77
+ sendFormattedTransaction: (
78
+ params: SimpleTransactionRequest,
79
+ isLegacy?: boolean
80
+ ) => Promise<TransactionResponse>;
81
+ sendSignedErc1155Transaction: ({
82
+ receiver,
83
+ tokenAddress,
84
+ tokenId,
85
+ isLegacy,
86
+ gasPrice,
87
+ gasLimit,
88
+ maxFeePerGas,
89
+ maxPriorityFeePerGas,
90
+ }: ISendSignedErcTransactionProps) => Promise<IResponseFromSendErcSignedTransaction>;
91
+ verifyPersonalMessage: (msg: string, sign: string) => string;
92
+ toBigNumber: (aBigNumberish: string | number) => BigNumber;
93
+ sendSignedErc20Transaction: ({
94
+ networkUrl,
95
+ receiver,
96
+ tokenAddress,
97
+ tokenAmount,
98
+ }: ISendSignedErcTransactionProps) => Promise<IResponseFromSendErcSignedTransaction>;
99
+
100
+ sendSignedErc721Transaction: ({
101
+ networkUrl,
102
+ receiver,
103
+ tokenAddress,
104
+ tokenId,
105
+ }: ISendSignedErcTransactionProps) => Promise<IResponseFromSendErcSignedTransaction>;
106
+
107
+ sendTransactionWithEditedFee: (
108
+ txHash: string,
109
+ isLegacy?: boolean
110
+ ) => Promise<{
111
+ isSpeedUp: boolean;
112
+ transaction?: TransactionResponse;
113
+ error?: boolean;
114
+ }>;
115
+
116
+ signPersonalMessage: (params: string[]) => Promise<string>;
117
+ verifyTypedSignature: (
118
+ data: TypedDataV1 | TypedMessage<any>,
119
+ signature: string,
120
+ version: SignTypedDataVersion
121
+ ) => string;
122
+ setWeb3Provider: (network: INetwork) => void;
123
+ getRecommendedGasPrice: (formatted?: boolean) => Promise<
124
+ | string
125
+ | {
126
+ ethers: string;
127
+ gwei: string;
128
+ }
129
+ >;
130
+ web3Provider: CustomJsonRpcProvider | CustomL2JsonRpcProvider;
131
+ getTxGasLimit: (tx: SimpleTransactionRequest) => Promise<BigNumber>;
132
+ }
133
+
134
+ export interface ISyscoinTransactions {
135
+ getEstimateSysTransactionFee: ({
136
+ txOptions,
137
+ amount,
138
+ receivingAddress,
139
+ feeRate,
140
+ token,
141
+ isMax,
142
+ }: {
143
+ amount: number;
144
+ feeRate?: number;
145
+ receivingAddress: string;
146
+ token?: { guid: string; symbol?: string } | null;
147
+ txOptions?: any;
148
+ isMax?: boolean | false;
149
+ }) => Promise<{ fee: number; psbt: any }>; // Returns UNSIGNED psbt - may throw ISyscoinTransactionError
150
+ getRecommendedFee: (explorerUrl: string) => Promise<number>;
151
+ decodeRawTransaction: (psbtOrHex: any, isRawHex?: boolean) => any;
152
+ // Sign PSBT separately
153
+ sendTransaction: (psbt: any) => Promise<ITxid>;
154
+ signPSBT: ({
155
+ psbt,
156
+ isTrezor,
157
+ isLedger,
158
+ }: {
159
+ isLedger?: boolean;
160
+ isTrezor?: boolean;
161
+ psbt: any;
162
+ }) => Promise<any>;
163
+ }
164
+
165
+ export interface IKeyringManager {
166
+ // Core keyring functionality
167
+ addNewAccount: (label?: string) => Promise<IKeyringAccountState>;
168
+ ethereumTransaction: IEthereumTransactions;
169
+ forgetMainWallet: (pwd: string) => void;
170
+ getAccountById: (
171
+ id: number,
172
+ accountType: KeyringAccountType
173
+ ) => Omit<IKeyringAccountState, 'xprv'>;
174
+ getAccountXpub: () => string;
175
+ getEncryptedXprv: (hd: SyscoinHDSigner) => string;
176
+ unlock: (
177
+ password: string,
178
+ isForPvtKey?: boolean
179
+ ) => Promise<{
180
+ canLogin: boolean;
181
+ needsAccountCreation?: boolean;
182
+ }>;
183
+ isUnlocked: () => boolean;
184
+ logout: () => void;
185
+ ledgerSigner: LedgerKeyring;
186
+ trezorSigner: TrezorKeyring;
187
+ setSignerNetwork: (network: INetwork) => Promise<{
188
+ success: boolean;
189
+ }>;
190
+ getPrivateKeyByAccountId: (
191
+ id: number,
192
+ accountType: KeyringAccountType,
193
+ pwd: string
194
+ ) => Promise<string>;
195
+ setStorage: (client: any) => void;
196
+ syscoinTransaction: ISyscoinTransactions;
197
+ isSeedValid: (seedPhrase: string) => boolean;
198
+ getSeed: (pwd: string) => Promise<string>;
199
+ importTrezorAccount: (label?: string) => Promise<IKeyringAccountState>;
200
+ utf8Error: boolean;
201
+ validateZprv: (
202
+ zprv: string,
203
+ targetNetwork?: INetwork
204
+ ) => IValidateZprvResponse;
205
+ validateWif: (
206
+ wif: string,
207
+ targetNetwork?: INetwork
208
+ ) => { isValid: boolean; message?: string };
209
+ // Account management
210
+ importAccount: (
211
+ privKey: string,
212
+ label?: string,
213
+ options?: { utxoAddressType?: 'p2wpkh' | 'p2pkh' }
214
+ ) => Promise<IKeyringAccountState>;
215
+ getNewChangeAddress: () => Promise<string>;
216
+ getChangeAddress: (id: number) => Promise<string>;
217
+ getPubkey: (id: number, isChangeAddress: boolean) => Promise<string>;
218
+ getBip32Path: (id: number, isChangeAddress: boolean) => Promise<string>;
219
+ updateReceivingAddress: () => Promise<string>;
220
+ getActiveAccount: () => {
221
+ activeAccount: Omit<IKeyringAccountState, 'xprv'>;
222
+ activeAccountType: KeyringAccountType;
223
+ };
224
+ importWeb3Account: (mnemonicOrPrivKey: string) => any;
225
+ createNewSeed: (wordCount?: number) => string;
226
+ getUTXOState: () => any;
227
+ importLedgerAccount: (
228
+ label?: string
229
+ ) => Promise<IKeyringAccountState | undefined>;
230
+ getActiveUTXOAccountState: () => any;
231
+ createEthAccount: (privateKey: string) => any;
232
+ getAddress: (xpub: string, isChangeAddress: boolean) => Promise<string>;
233
+ // Secure initialization and password management
234
+ initializeWalletSecurely: (
235
+ seedPhrase: string,
236
+ password: string,
237
+ prvPassword?: string
238
+ ) => Promise<IKeyringAccountState>;
239
+ // NEW: Separated initialization methods
240
+ initializeSession: (seedPhrase: string, password: string) => Promise<void>;
241
+ createFirstAccount: (label?: string) => Promise<IKeyringAccountState>;
242
+ transferSessionTo: (targetKeyring: IKeyringManager) => void;
243
+ receiveSessionOwnership: (sessionPassword: any, sessionMnemonic: any) => void;
244
+ lockWallet: () => void;
245
+ // NEW: Store access for stateless keyring
246
+ setVaultStateGetter: (getter: () => any) => void;
247
+ }
248
+
249
+ export enum KeyringAccountType {
250
+ HDAccount = 'HDAccount',
251
+ Imported = 'Imported',
252
+ Ledger = 'Ledger',
253
+ Trezor = 'Trezor',
254
+ }
255
+
256
+ export type IKeyringDApp = {
257
+ active: boolean;
258
+ id: number;
259
+ url: string;
260
+ };
261
+
262
+ export type accountType = {
263
+ [id: number]: IKeyringAccountState;
264
+ };
265
+
266
+ export type IKeyringBalances = {
267
+ [INetworkType.Syscoin]: number;
268
+ [INetworkType.Ethereum]: number;
269
+ };
270
+
271
+ interface INetworkParams {
272
+ bech32: string;
273
+ bip32: {
274
+ private: number;
275
+ public: number;
276
+ };
277
+ messagePrefix: string;
278
+ pubKeyHash: number;
279
+ scriptHash: number;
280
+ slip44: number;
281
+ wif: number;
282
+ }
283
+
284
+ interface IValidateZprvResponse {
285
+ isValid: boolean;
286
+ message: string;
287
+ network?: INetworkParams | null;
288
+ node?: any;
289
+ }
290
+
291
+ export interface IKeyringAccountState {
292
+ address: string;
293
+ balances: IKeyringBalances;
294
+ id: number;
295
+ isImported: boolean;
296
+ isLedgerWallet: boolean;
297
+ isTrezorWallet: boolean;
298
+ label: string;
299
+ xprv: string;
300
+ xpub: string;
301
+ }
302
+
303
+ export interface ISyscoinBackendAccount {
304
+ address: string;
305
+ balance: string;
306
+ itemsOnPage: number;
307
+ page: number;
308
+ totalPages: number;
309
+ totalReceived: string;
310
+ totalSent: string;
311
+ txs: number;
312
+ unconfirmedBalance: string;
313
+ unconfirmedTxs: number;
314
+ }
315
+
316
+ export interface ILatestUpdateForSysAccount {
317
+ balances: {
318
+ ethereum: number;
319
+ syscoin: number;
320
+ };
321
+ receivingAddress: any;
322
+ xpub: any;
323
+ }
324
+
325
+ export interface ISendSignedErcTransactionProps {
326
+ decimals?: number;
327
+ gasLimit?: BigNumberish;
328
+ gasPrice?: BigNumberish;
329
+ isLegacy?: boolean;
330
+ maxFeePerGas?: BigNumberish;
331
+ maxPriorityFeePerGas?: BigNumberish;
332
+ networkUrl: string;
333
+ receiver: string;
334
+ saveTrezorTx?: (tx: any) => void;
335
+ tokenAddress: string;
336
+ tokenAmount?: string;
337
+ tokenId?: number;
338
+ }
339
+
340
+ export interface IResponseFromSendErcSignedTransaction {
341
+ accessList: any[];
342
+ chainId: number;
343
+ confirmations: number | null;
344
+ data: string;
345
+ from: string;
346
+ gasLimit: BigNumber;
347
+ gasPrice: BigNumber | null;
348
+ hash: string;
349
+ maxFeePerGas: BigNumber;
350
+ maxPriorityFeePerGas: BigNumber;
351
+ nonce: number;
352
+ r: string;
353
+ s: string;
354
+ to: string;
355
+ type: number;
356
+ v: number | null;
357
+ value: BigNumber;
358
+ wait: any;
359
+ }
360
+
361
+ export interface IGasParams {
362
+ gasLimit?: BigNumber;
363
+ gasPrice?: BigNumber;
364
+ maxFeePerGas?: BigNumber;
365
+ maxPriorityFeePerGas?: BigNumber;
366
+ }
@@ -0,0 +1,133 @@
1
+ // Import coins data from the relative package in the monorepo
2
+ import { findCoin } from '@sidhujag/sysweb3-network';
3
+
4
+ /**
5
+ * Get coin information by coin shortcut or slip44
6
+ * Priority: 1) Check if slip44=60 (EVM), 2) Check coins.ts (UTXO), 3) Default to unknown
7
+ */
8
+ export function getCoinInfo(coinShortcut: string, slip44: number) {
9
+ // slip44=60: Ethereum and most EVM-compatible networks (BSC, Polygon, Arbitrum, etc.)
10
+ // This must be checked FIRST to prevent any UTXO coin conflicts
11
+ if (slip44 === 60) {
12
+ return {
13
+ slip44,
14
+ segwit: false,
15
+ isEvm: true,
16
+ };
17
+ }
18
+
19
+ // Use the shared findCoin utility to search for UTXO coins
20
+ const coin = findCoin({ slip44, name: coinShortcut });
21
+
22
+ if (coin) {
23
+ return {
24
+ slip44: coin.slip44,
25
+ segwit: coin.segwit || false,
26
+ isEvm: false, // UTXO coins from coins.ts are not EVM
27
+ };
28
+ }
29
+
30
+ // For unknown slip44 values, we cannot safely assume EVM
31
+ // Return as unknown/unsupported rather than defaulting to EVM
32
+ return {
33
+ slip44,
34
+ segwit: false,
35
+ isEvm: false, // Changed from true to false - unknown slip44 should not default to EVM
36
+ };
37
+ }
38
+
39
+ /**
40
+ * Determine if a coin is EVM-based (Ethereum-like)
41
+ */
42
+ export function isEvmCoin(coinShortcut: string, slip44: number): boolean {
43
+ const coinInfo = getCoinInfo(coinShortcut, slip44);
44
+ return coinInfo.isEvm;
45
+ }
46
+
47
+ /**
48
+ * Determine if a coin uses SegWit (BIP84 - m/84')
49
+ */
50
+ export function isSegwitCoin(coinShortcut: string, slip44: number): boolean {
51
+ const coinInfo = getCoinInfo(coinShortcut, slip44);
52
+ return coinInfo.segwit === true;
53
+ }
54
+
55
+ /**
56
+ * Determine if a coin uses legacy derivation (BIP44 - m/44')
57
+ */
58
+ export function isLegacyCoin(coinShortcut: string, slip44: number): boolean {
59
+ return !isSegwitCoin(coinShortcut, slip44);
60
+ }
61
+
62
+ /**
63
+ * Get the appropriate BIP standard for a coin
64
+ */
65
+ export function getBipStandard(coinShortcut: string, slip44: number): number {
66
+ if (isEvmCoin(coinShortcut, slip44)) {
67
+ return 44; // EVM coins use BIP44
68
+ }
69
+
70
+ if (isSegwitCoin(coinShortcut, slip44)) {
71
+ return 84; // SegWit coins use BIP84
72
+ }
73
+
74
+ return 44; // Default to BIP44 for legacy coins
75
+ }
76
+
77
+ /**
78
+ * Generate derivation path for account level (ending with account index)
79
+ * Example: m/84'/57'/0' for Syscoin account derivation
80
+ */
81
+ export function getAccountDerivationPath(
82
+ coinShortcut: string,
83
+ slip44: number,
84
+ accountIndex = 0
85
+ ): string {
86
+ const bip = getBipStandard(coinShortcut, slip44);
87
+ return `m/${bip}'/${slip44}'/${accountIndex}'`;
88
+ }
89
+
90
+ /**
91
+ * Generate derivation path for address level (full path to specific address)
92
+ * Example: m/84'/57'/0'/0/0 for first address of first Syscoin account
93
+ */
94
+ export function getAddressDerivationPath(
95
+ coinShortcut: string,
96
+ slip44: number,
97
+ accountIndex = 0,
98
+ isChangeAddress = false,
99
+ addressIndex = 0
100
+ ): string {
101
+ const bip = getBipStandard(coinShortcut, slip44);
102
+
103
+ if (isEvmCoin(coinShortcut, slip44)) {
104
+ // EVM coins typically use: m/44'/60'/0'/0/addressIndex
105
+ return `m/${bip}'/${slip44}'/0'/0/${addressIndex}`;
106
+ } else {
107
+ // UTXO coins use: m/84'/slip44'/account'/change/addressIndex
108
+ const changeValue = isChangeAddress ? 1 : 0;
109
+ return `m/${bip}'/${slip44}'/${accountIndex}'/${changeValue}/${addressIndex}`;
110
+ }
111
+ }
112
+
113
+ /**
114
+ * Convert an extended key's version bytes to a target version (e.g., xpub->zpub or tpub->vpub)
115
+ * - Pass targetVersionHex as a 4-byte hex string (e.g., '04b24746' for zpub, '045f1cf6' for vpub)
116
+ */
117
+ export function convertExtendedKeyVersion(
118
+ extendedKey: string,
119
+ targetVersionHex: string
120
+ ): string {
121
+ try {
122
+ // Lazy import to avoid bundling when unused
123
+ /* eslint-disable @typescript-eslint/no-var-requires */
124
+ const bs58check = require('bs58check');
125
+ const decoded: Buffer = Buffer.from(bs58check.decode(extendedKey));
126
+ const target = Buffer.from(targetVersionHex, 'hex');
127
+ target.copy(decoded, 0, 0, 4);
128
+ return bs58check.encode(decoded);
129
+ } catch (e) {
130
+ // If conversion fails, return original key
131
+ return extendedKey;
132
+ }
133
+ }
@@ -0,0 +1,24 @@
1
+ import * as syscoinjs from 'syscoinjs-lib';
2
+
3
+ /**
4
+ * Utility functions for PSBT conversion between Pali and syscoinjs formats
5
+ */
6
+ export class PsbtUtils {
7
+ /**
8
+ * Import PSBT from Pali's exported format to syscoinjs PSBT object
9
+ * @param psbtFromPali - PSBT data exported from Pali
10
+ * @returns syscoinjs PSBT object
11
+ */
12
+ static fromPali(psbtFromPali: any): any {
13
+ return syscoinjs.utils.importPsbtFromJson(psbtFromPali).psbt;
14
+ }
15
+
16
+ /**
17
+ * Export syscoinjs PSBT object to Pali's expected format
18
+ * @param psbt - syscoinjs PSBT object
19
+ * @returns PSBT data in Pali's expected format
20
+ */
21
+ static toPali(psbt: any): any {
22
+ return syscoinjs.utils.exportPsbtToJson(psbt, undefined);
23
+ }
24
+ }
package/src/utils.ts ADDED
@@ -0,0 +1,191 @@
1
+ import { isHexString } from '@ethersproject/bytes';
2
+ import { Logger } from '@ethersproject/logger';
3
+
4
+ const logger = new Logger('utils');
5
+
6
+ const errorGas = ['call', 'estimateGas'];
7
+
8
+ function spelunk(
9
+ value: any,
10
+ requireData: boolean
11
+ ): null | { data: null | string; message: string } {
12
+ if (value == null) {
13
+ return null;
14
+ }
15
+
16
+ // These *are* the droids we're looking for.
17
+ if (typeof value.message === 'string' && value.message.match('reverted')) {
18
+ const data = isHexString(value.data) ? value.data : null;
19
+ if (!requireData || data) {
20
+ return { message: value.message, data };
21
+ }
22
+ }
23
+
24
+ // Spelunk further...
25
+ if (typeof value === 'object') {
26
+ for (const key in value) {
27
+ const result = spelunk(value[key], requireData);
28
+ if (result) {
29
+ return result;
30
+ }
31
+ }
32
+ return null;
33
+ }
34
+
35
+ // Might be a JSON string we can further descend...
36
+ if (typeof value === 'string') {
37
+ try {
38
+ return spelunk(JSON.parse(value), requireData);
39
+ // eslint-disable-next-line
40
+ } catch (error) {}
41
+ }
42
+
43
+ return null;
44
+ }
45
+
46
+ export function checkError(method: string, error: any, params: any): any {
47
+ const transaction = params.transaction || params.signedTransaction;
48
+
49
+ // Undo the "convenience" some nodes are attempting to prevent backwards
50
+ // incompatibility; maybe for v6 consider forwarding reverts as errors
51
+ if (method === 'call') {
52
+ const result = spelunk(error, true);
53
+ if (result) {
54
+ return result.data;
55
+ }
56
+
57
+ // Nothing descriptive..
58
+ logger.throwError(
59
+ 'missing revert data in call exception; Transaction reverted without a reason string',
60
+ Logger.errors.CALL_EXCEPTION,
61
+ {
62
+ data: '0x',
63
+ transaction,
64
+ error,
65
+ }
66
+ );
67
+ }
68
+
69
+ if (method === 'estimateGas') {
70
+ // Try to find something, with a preference on SERVER_ERROR body
71
+ let result = spelunk(error.body, false);
72
+ if (result == null) {
73
+ result = spelunk(error, false);
74
+ }
75
+
76
+ // Found "reverted", this is a CALL_EXCEPTION
77
+ if (result) {
78
+ logger.throwError(
79
+ 'cannot estimate gas; transaction may fail or may require manual gas limit',
80
+ Logger.errors.UNPREDICTABLE_GAS_LIMIT,
81
+ {
82
+ reason: result.message,
83
+ method,
84
+ transaction,
85
+ error,
86
+ }
87
+ );
88
+ }
89
+ }
90
+
91
+ // @TODO: Should we spelunk for message too?
92
+
93
+ let message = error.message;
94
+ if (
95
+ error.code === Logger.errors.SERVER_ERROR &&
96
+ error.error &&
97
+ typeof error.error.message === 'string'
98
+ ) {
99
+ message = error.error.message;
100
+ } else if (typeof error.body === 'string') {
101
+ message = error.body;
102
+ } else if (typeof error.responseText === 'string') {
103
+ message = error.responseText;
104
+ }
105
+ message = (message || '').toLowerCase();
106
+
107
+ // "insufficient funds for gas * price + value + cost(data)"
108
+ if (
109
+ message.match(
110
+ /insufficient funds|base fee exceeds gas limit|InsufficientFunds/i
111
+ )
112
+ ) {
113
+ logger.throwError(
114
+ 'insufficient funds for intrinsic transaction cost',
115
+ Logger.errors.INSUFFICIENT_FUNDS,
116
+ {
117
+ error,
118
+ method,
119
+ transaction,
120
+ }
121
+ );
122
+ }
123
+
124
+ // "nonce too low"
125
+ if (message.match(/nonce (is )?too low/i)) {
126
+ logger.throwError(
127
+ 'nonce has already been used',
128
+ Logger.errors.NONCE_EXPIRED,
129
+ {
130
+ error,
131
+ method,
132
+ transaction,
133
+ }
134
+ );
135
+ }
136
+
137
+ // "replacement transaction underpriced"
138
+ if (
139
+ message.match(
140
+ /replacement transaction underpriced|transaction gas price.*too low/i
141
+ )
142
+ ) {
143
+ logger.throwError(
144
+ 'replacement fee too low',
145
+ Logger.errors.REPLACEMENT_UNDERPRICED,
146
+ {
147
+ error,
148
+ method,
149
+ transaction,
150
+ }
151
+ );
152
+ }
153
+
154
+ // "replacement transaction underpriced"
155
+ if (message.match(/only replay-protected/i)) {
156
+ logger.throwError(
157
+ 'legacy pre-eip-155 transactions not supported',
158
+ Logger.errors.UNSUPPORTED_OPERATION,
159
+ {
160
+ error,
161
+ method,
162
+ transaction,
163
+ }
164
+ );
165
+ }
166
+
167
+ if (
168
+ errorGas.indexOf(method) >= 0 &&
169
+ message.match(
170
+ /gas required exceeds allowance|always failing transaction|execution reverted|revert/
171
+ )
172
+ ) {
173
+ logger.throwError(
174
+ 'cannot estimate gas; transaction may fail or may require manual gas limit',
175
+ Logger.errors.UNPREDICTABLE_GAS_LIMIT,
176
+ {
177
+ error,
178
+ method,
179
+ transaction,
180
+ }
181
+ );
182
+ }
183
+
184
+ throw error;
185
+ }
186
+
187
+ export const SYSCOIN_BASIC_FEE = 0.00001;
188
+
189
+ export const ONE_HUNDRED_MILLION = 1e8;
190
+
191
+ export const BIP_84 = 84;