@pezkuwi/hw-ledger 14.0.4 → 14.0.6

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 (96) hide show
  1. package/README.md +1 -1
  2. package/{Ledger.d.ts → build/Ledger.d.ts} +1 -1
  3. package/build/Ledger.js +112 -0
  4. package/{LedgerGeneric.d.ts → build/LedgerGeneric.d.ts} +1 -1
  5. package/build/LedgerGeneric.js +211 -0
  6. package/build/README.md +2 -0
  7. package/{bundle-polkadot-hw-ledger.js → build/bundle-pezkuwi-hw-ledger.js} +2 -2
  8. package/build/bundle.js +2 -0
  9. package/build/cjs/Ledger.d.ts +43 -0
  10. package/build/cjs/Ledger.js +117 -0
  11. package/build/cjs/LedgerGeneric.d.ts +63 -0
  12. package/build/cjs/LedgerGeneric.js +216 -0
  13. package/build/cjs/bundle.d.ts +2 -0
  14. package/build/cjs/bundle.js +7 -0
  15. package/build/cjs/constants.js +7 -0
  16. package/build/cjs/defaults.js +61 -0
  17. package/build/cjs/index.js +5 -0
  18. package/build/cjs/packageInfo.js +4 -0
  19. package/{types.d.ts → build/cjs/types.d.ts} +1 -1
  20. package/build/cjs/types.js +2 -0
  21. package/build/constants.d.ts +4 -0
  22. package/build/constants.js +4 -0
  23. package/build/defaults.d.ts +14 -0
  24. package/build/defaults.js +58 -0
  25. package/build/index.d.ts +2 -0
  26. package/build/index.js +2 -0
  27. package/build/package.json +178 -0
  28. package/build/packageDetect.d.ts +1 -0
  29. package/build/packageDetect.js +5 -0
  30. package/build/packageInfo.d.ts +6 -0
  31. package/build/packageInfo.js +1 -0
  32. package/build/types.d.ts +34 -0
  33. package/build/types.js +1 -0
  34. package/build-deno/Ledger.ts +144 -0
  35. package/build-deno/LedgerGeneric.ts +271 -0
  36. package/build-deno/README.md +2 -0
  37. package/build-deno/bundle.ts +3 -0
  38. package/build-deno/constants.ts +8 -0
  39. package/build-deno/defaults.ts +61 -0
  40. package/build-deno/index.ts +4 -0
  41. package/build-deno/mod.ts +2 -0
  42. package/build-deno/packageDetect.ts +9 -0
  43. package/build-deno/packageInfo.ts +3 -0
  44. package/build-deno/types.ts +42 -0
  45. package/{cjs → build-tsc}/Ledger.d.ts +1 -1
  46. package/{cjs → build-tsc}/LedgerGeneric.d.ts +1 -1
  47. package/{cjs → build-tsc}/types.d.ts +1 -1
  48. package/build-tsc-cjs/packageDetect.js +7 -0
  49. package/{cjs → build-tsc-cjs}/packageInfo.js +1 -1
  50. package/{packageInfo.js → build-tsc-esm/packageInfo.js} +1 -1
  51. package/package.json +6 -156
  52. package/src/Ledger.ts +144 -0
  53. package/src/LedgerGeneric.ts +275 -0
  54. package/src/bundle.ts +8 -0
  55. package/src/constants.ts +10 -0
  56. package/src/defaults.spec.ts +20 -0
  57. package/src/defaults.ts +69 -0
  58. package/src/index.ts +6 -0
  59. package/src/mod.ts +4 -0
  60. package/src/packageDetect.ts +13 -0
  61. package/src/packageInfo.ts +6 -0
  62. package/src/types.ts +42 -0
  63. package/tsconfig.build.json +16 -0
  64. package/tsconfig.build.tsbuildinfo +1 -0
  65. package/tsconfig.spec.json +16 -0
  66. package/tsconfig.spec.tsbuildinfo +1 -0
  67. /package/{LICENSE → build/LICENSE} +0 -0
  68. /package/{cjs → build}/bundle.d.ts +0 -0
  69. /package/{constants.d.ts → build/cjs/constants.d.ts} +0 -0
  70. /package/{defaults.d.ts → build/cjs/defaults.d.ts} +0 -0
  71. /package/{index.d.ts → build/cjs/index.d.ts} +0 -0
  72. /package/{cjs → build/cjs}/package.json +0 -0
  73. /package/{packageDetect.d.ts → build/cjs/packageDetect.d.ts} +0 -0
  74. /package/{cjs → build/cjs}/packageDetect.js +0 -0
  75. /package/{packageInfo.d.ts → build/cjs/packageInfo.d.ts} +0 -0
  76. /package/{bundle.d.ts → build-tsc/bundle.d.ts} +0 -0
  77. /package/{cjs → build-tsc}/constants.d.ts +0 -0
  78. /package/{cjs → build-tsc}/defaults.d.ts +0 -0
  79. /package/{cjs → build-tsc}/index.d.ts +0 -0
  80. /package/{cjs → build-tsc}/packageDetect.d.ts +0 -0
  81. /package/{cjs → build-tsc}/packageInfo.d.ts +0 -0
  82. /package/{cjs → build-tsc-cjs}/Ledger.js +0 -0
  83. /package/{cjs → build-tsc-cjs}/LedgerGeneric.js +0 -0
  84. /package/{cjs → build-tsc-cjs}/bundle.js +0 -0
  85. /package/{cjs → build-tsc-cjs}/constants.js +0 -0
  86. /package/{cjs → build-tsc-cjs}/defaults.js +0 -0
  87. /package/{cjs → build-tsc-cjs}/index.js +0 -0
  88. /package/{cjs → build-tsc-cjs}/types.js +0 -0
  89. /package/{Ledger.js → build-tsc-esm/Ledger.js} +0 -0
  90. /package/{LedgerGeneric.js → build-tsc-esm/LedgerGeneric.js} +0 -0
  91. /package/{bundle.js → build-tsc-esm/bundle.js} +0 -0
  92. /package/{constants.js → build-tsc-esm/constants.js} +0 -0
  93. /package/{defaults.js → build-tsc-esm/defaults.js} +0 -0
  94. /package/{index.js → build-tsc-esm/index.js} +0 -0
  95. /package/{packageDetect.js → build-tsc-esm/packageDetect.js} +0 -0
  96. /package/{types.js → build-tsc-esm/types.js} +0 -0
@@ -0,0 +1,144 @@
1
+ import { Buffer } from 'node:buffer';
2
+
3
+
4
+ import type { SubstrateApp } from 'https://esm.sh/@zondax/ledger-substrate@1.1.1';
5
+ import type { TransportDef, TransportType } from 'https://deno.land/x/pezkuwi/hw-ledger-transports/types.ts';
6
+ import type { AccountOptions, LedgerAddress, LedgerSignature, LedgerVersion } from './types.ts';
7
+
8
+ import { newSubstrateApp } from 'https://esm.sh/@zondax/ledger-substrate@1.1.1';
9
+
10
+ import { transports } from 'https://deno.land/x/pezkuwi/hw-ledger-transports/mod.ts';
11
+ import { hexAddPrefix, u8aToBuffer, u8aWrapBytes } from 'https://deno.land/x/pezkuwi/util/mod.ts';
12
+
13
+ import { LEDGER_DEFAULT_ACCOUNT, LEDGER_DEFAULT_CHANGE, LEDGER_DEFAULT_INDEX, LEDGER_SUCCESS_CODE } from './constants.ts';
14
+ import { ledgerApps } from './defaults.ts';
15
+
16
+ export { packageInfo } from './packageInfo.ts';
17
+
18
+ type Chain = keyof typeof ledgerApps;
19
+
20
+ type WrappedResult = Awaited<ReturnType<SubstrateApp['getAddress' | 'getVersion' | 'sign']>>;
21
+
22
+ /** @internal Wraps a SubstrateApp call, checking the result for any errors which result in a rejection */
23
+ async function wrapError <T extends WrappedResult> (promise: Promise<T>): Promise<T> {
24
+ const result = await promise;
25
+
26
+ if (result.return_code !== LEDGER_SUCCESS_CODE) {
27
+ throw new Error(result.error_message);
28
+ }
29
+
30
+ return result;
31
+ }
32
+
33
+ /** @internal Wraps a sign/signRaw call and returns the associated signature */
34
+ function sign (method: 'sign' | 'signRaw', message: Uint8Array, accountOffset = 0, addressOffset = 0, { account = LEDGER_DEFAULT_ACCOUNT, addressIndex = LEDGER_DEFAULT_INDEX, change = LEDGER_DEFAULT_CHANGE }: Partial<AccountOptions> = {}): (app: SubstrateApp) => Promise<LedgerSignature> {
35
+ return async (app: SubstrateApp): Promise<LedgerSignature> => {
36
+ const { signature } = await wrapError(app[method](account + accountOffset, change, addressIndex + addressOffset, u8aToBuffer(message)));
37
+
38
+ return {
39
+ signature: hexAddPrefix(signature.toString('hex'))
40
+ };
41
+ };
42
+ }
43
+
44
+ /**
45
+ * @name Ledger
46
+ *
47
+ * @description
48
+ * Legacy wrapper for a ledger app -
49
+ * - it connects automatically on use, creating an underlying interface as required
50
+ * - Promises reject with errors (unwrapped errors from @zondax/ledger-substrate)
51
+ * @deprecated Use LedgerGeneric for up to date integration with ledger
52
+ */
53
+ export class Ledger {
54
+ readonly #ledgerName: string;
55
+ readonly #transportDef: TransportDef;
56
+
57
+ #app: SubstrateApp | null = null;
58
+
59
+ constructor (transport: TransportType, chain: Chain) {
60
+ const ledgerName = ledgerApps[chain];
61
+ const transportDef = transports.find(({ type }) => type === transport);
62
+
63
+ if (!ledgerName) {
64
+ throw new Error(`Unsupported Ledger chain ${chain}`);
65
+ } else if (!transportDef) {
66
+ throw new Error(`Unsupported Ledger transport ${transport}`);
67
+ }
68
+
69
+ this.#ledgerName = ledgerName;
70
+ this.#transportDef = transportDef;
71
+ }
72
+
73
+ /**
74
+ * Returns the address associated with a specific account & address offset. Optionally
75
+ * asks for on-device confirmation
76
+ */
77
+ public async getAddress (confirm = false, accountOffset = 0, addressOffset = 0, { account = LEDGER_DEFAULT_ACCOUNT, addressIndex = LEDGER_DEFAULT_INDEX, change = LEDGER_DEFAULT_CHANGE }: Partial<AccountOptions> = {}): Promise<LedgerAddress> {
78
+ return this.withApp(async (app: SubstrateApp): Promise<LedgerAddress> => {
79
+ const { address, pubKey } = await wrapError(app.getAddress(account + accountOffset, change, addressIndex + addressOffset, confirm));
80
+
81
+ return {
82
+ address,
83
+ publicKey: hexAddPrefix(pubKey)
84
+ };
85
+ });
86
+ }
87
+
88
+ /**
89
+ * Returns the version of the Ledger application on the device
90
+ */
91
+ public async getVersion (): Promise<LedgerVersion> {
92
+ return this.withApp(async (app: SubstrateApp): Promise<LedgerVersion> => {
93
+ const { device_locked: isLocked, major, minor, patch, test_mode: isTestMode } = await wrapError(app.getVersion());
94
+
95
+ return {
96
+ isLocked,
97
+ isTestMode,
98
+ version: [major, minor, patch]
99
+ };
100
+ });
101
+ }
102
+
103
+ /**
104
+ * Signs a transaction on the Ledger device
105
+ */
106
+ public async sign (message: Uint8Array, accountOffset?: number, addressOffset?: number, options?: Partial<AccountOptions>): Promise<LedgerSignature> {
107
+ return this.withApp(sign('sign', message, accountOffset, addressOffset, options));
108
+ }
109
+
110
+ /**
111
+ * Signs a message (non-transactional) on the Ledger device
112
+ */
113
+ public async signRaw (message: Uint8Array, accountOffset?: number, addressOffset?: number, options?: Partial<AccountOptions>): Promise<LedgerSignature> {
114
+ return this.withApp(sign('signRaw', u8aWrapBytes(message), accountOffset, addressOffset, options));
115
+ }
116
+
117
+ /**
118
+ * @internal
119
+ *
120
+ * Returns a created SubstrateApp to perform operations against. Generally
121
+ * this is only used internally, to ensure consistent bahavior.
122
+ */
123
+ async withApp <T> (fn: (app: SubstrateApp) => Promise<T>): Promise<T> {
124
+ try {
125
+ if (!this.#app) {
126
+ const transport = await this.#transportDef.create();
127
+
128
+ // We need this override for the actual type passing - the Deno environment
129
+ // is quite a bit stricter and it yields invalids between the two (specifically
130
+ // since we mangle the imports from .default in the types for CJS/ESM and between
131
+ // esm.sh versions this yields problematic outputs)
132
+ //
133
+ // eslint-disable-next-line @typescript-eslint/no-unsafe-argument, @typescript-eslint/no-explicit-any
134
+ this.#app = newSubstrateApp(transport as any, this.#ledgerName);
135
+ }
136
+
137
+ return await fn(this.#app);
138
+ } catch (error) {
139
+ this.#app = null;
140
+
141
+ throw error;
142
+ }
143
+ }
144
+ }
@@ -0,0 +1,271 @@
1
+ import { Buffer } from 'node:buffer';
2
+
3
+
4
+ import type { TransportDef, TransportType } from 'https://deno.land/x/pezkuwi/hw-ledger-transports/types.ts';
5
+ import type { AccountOptionsGeneric, LedgerAddress, LedgerSignature, LedgerVersion } from './types.ts';
6
+
7
+ import { PolkadotGenericApp } from 'https://esm.sh/@zondax/ledger-substrate@1.1.1';
8
+
9
+ import { transports } from 'https://deno.land/x/pezkuwi/hw-ledger-transports/mod.ts';
10
+ import { hexAddPrefix, u8aToBuffer, u8aWrapBytes } from 'https://deno.land/x/pezkuwi/util/mod.ts';
11
+
12
+ import { ledgerApps } from './defaults.ts';
13
+
14
+ export { packageInfo } from './packageInfo.ts';
15
+
16
+ type Chain = keyof typeof ledgerApps;
17
+
18
+ type WrappedResult = Awaited<ReturnType<PolkadotGenericApp['getAddress' | 'getVersion' | 'sign' | 'signWithMetadata']>>;
19
+
20
+ interface ResponseError {
21
+ errorMessage: string
22
+ returnCode: number
23
+ }
24
+
25
+ /** @internal Wraps a PolkadotGenericApp call, checking the result for any errors which result in a rejection */
26
+ async function wrapError <T extends WrappedResult> (promise: Promise<T>): Promise<T> {
27
+ let result: T;
28
+
29
+ try {
30
+ result = await promise;
31
+ } catch (e: unknown) {
32
+ // We check to see if the propogated error is the newer ResponseError type.
33
+ // The response code use to be part of the result, but with the latest breaking changes from 0.42.x
34
+ // the interface and it's types have completely changed.
35
+ if ((e as ResponseError).returnCode) {
36
+ throw new Error(`${(e as ResponseError).returnCode}: ${(e as ResponseError).errorMessage}`);
37
+ }
38
+
39
+ throw new Error((e as Error).message);
40
+ }
41
+
42
+ return result;
43
+ }
44
+
45
+ /** @internal Wraps a signEd25519/signRawEd25519 call and returns the associated signature */
46
+ function sign (method: 'signEd25519' | 'signRawEd25519', message: Uint8Array, slip44: number, accountIndex = 0, addressOffset = 0): (app: PolkadotGenericApp) => Promise<LedgerSignature> {
47
+ const bip42Path = `m/44'/${slip44}'/${accountIndex}'/${0}'/${addressOffset}'`;
48
+
49
+ return async (app: PolkadotGenericApp): Promise<LedgerSignature> => {
50
+ const { signature } = await wrapError(app[method](bip42Path, u8aToBuffer(message)));
51
+
52
+ return {
53
+ signature: hexAddPrefix(signature.toString('hex'))
54
+ };
55
+ };
56
+ }
57
+
58
+ /** @internal Wraps a signEcdsa/signRawEcdsa call and returns the associated signature */
59
+ function signEcdsa (method: 'signEcdsa' | 'signRawEcdsa', message: Uint8Array, slip44: number, accountIndex = 0, addressOffset = 0): (app: PolkadotGenericApp) => Promise<LedgerSignature> {
60
+ const bip42Path = `m/44'/${slip44}'/${accountIndex}'/${0}'/${addressOffset}'`;
61
+
62
+ return async (app: PolkadotGenericApp): Promise<LedgerSignature> => {
63
+ const { r, s, v } = await wrapError(app[method](bip42Path, u8aToBuffer(message)));
64
+
65
+ const signature = Buffer.concat([r, s, v]);
66
+
67
+ return {
68
+ signature: hexAddPrefix(signature.toString('hex'))
69
+ };
70
+ };
71
+ }
72
+
73
+ /** @internal Wraps a signWithMetadataEd25519 call and returns the associated signature */
74
+ function signWithMetadata (message: Uint8Array, slip44: number, accountIndex = 0, addressOffset = 0, { metadata }: Partial<AccountOptionsGeneric> = {}): (app: PolkadotGenericApp) => Promise<LedgerSignature> {
75
+ const bip42Path = `m/44'/${slip44}'/${accountIndex}'/${0}'/${addressOffset}'`;
76
+
77
+ return async (app: PolkadotGenericApp): Promise<LedgerSignature> => {
78
+ if (!metadata) {
79
+ throw new Error('The metadata option must be present when using signWithMetadata');
80
+ }
81
+
82
+ const bufferMsg = Buffer.from(message);
83
+
84
+ const { signature } = await wrapError(app.signWithMetadataEd25519(bip42Path, bufferMsg, metadata));
85
+
86
+ return {
87
+ signature: hexAddPrefix(signature.toString('hex'))
88
+ };
89
+ };
90
+ }
91
+
92
+ /** @internal Wraps a signWithMetadataEcdsa call and returns the associated signature */
93
+ function signWithMetadataEcdsa (message: Uint8Array, slip44: number, accountIndex = 0, addressOffset = 0, { metadata }: Partial<AccountOptionsGeneric> = {}): (app: PolkadotGenericApp) => Promise<LedgerSignature> {
94
+ const bip42Path = `m/44'/${slip44}'/${accountIndex}'/${0}'/${addressOffset}'`;
95
+
96
+ return async (app: PolkadotGenericApp): Promise<LedgerSignature> => {
97
+ if (!metadata) {
98
+ throw new Error('The metadata option must be present when using signWithMetadata');
99
+ }
100
+
101
+ const bufferMsg = Buffer.from(message);
102
+
103
+ const { r, s, v } = await wrapError(app.signWithMetadataEcdsa(bip42Path, bufferMsg, metadata));
104
+
105
+ const signature = Buffer.concat([r, s, v]);
106
+
107
+ return {
108
+ signature: hexAddPrefix(signature.toString('hex'))
109
+ };
110
+ };
111
+ }
112
+
113
+ /**
114
+ * @name Ledger
115
+ *
116
+ * @description
117
+ * A very basic wrapper for a ledger app -
118
+ * - it connects automatically on use, creating an underlying interface as required
119
+ * - Promises reject with errors (unwrapped errors from @zondax/ledger-substrate-js)
120
+ */
121
+ export class LedgerGeneric {
122
+ readonly #transportDef: TransportDef;
123
+ readonly #slip44: number;
124
+ /**
125
+ * The chainId is represented by the chains token in all lowercase. Example: Polkadot -> dot
126
+ */
127
+ readonly #chainId?: string;
128
+ /**
129
+ * The metaUrl is seen as a server url that the underlying `PolkadotGenericApp` will use to
130
+ * retrieve the signature given a tx blob, and a chainId. It is important to note that if you would like to avoid
131
+ * having any network calls made, use `signWithMetadata`, and avoid `sign`.
132
+ */
133
+ readonly #metaUrl?: string;
134
+
135
+ #app: PolkadotGenericApp | null = null;
136
+
137
+ constructor (transport: TransportType, chain: Chain, slip44: number, chainId?: string, metaUrl?: string) {
138
+ const ledgerName = ledgerApps[chain];
139
+ const transportDef = transports.find(({ type }) => type === transport);
140
+
141
+ if (!ledgerName) {
142
+ throw new Error(`Unsupported Ledger chain ${chain}`);
143
+ } else if (!transportDef) {
144
+ throw new Error(`Unsupported Ledger transport ${transport}`);
145
+ }
146
+
147
+ this.#metaUrl = metaUrl;
148
+ this.#chainId = chainId;
149
+ this.#slip44 = slip44;
150
+ this.#transportDef = transportDef;
151
+ }
152
+
153
+ /**
154
+ * @description Returns the address associated with a specific Ed25519 account & address offset. Optionally
155
+ * asks for on-device confirmation
156
+ */
157
+ public async getAddress (ss58Prefix: number, confirm = false, accountIndex = 0, addressOffset = 0): Promise<LedgerAddress> {
158
+ const bip42Path = `m/44'/${this.#slip44}'/${accountIndex}'/${0}'/${addressOffset}'`;
159
+
160
+ return this.withApp(async (app: PolkadotGenericApp): Promise<LedgerAddress> => {
161
+ const { address, pubKey } = await wrapError(app.getAddressEd25519(bip42Path, ss58Prefix, confirm));
162
+
163
+ return {
164
+ address,
165
+ publicKey: hexAddPrefix(pubKey)
166
+ };
167
+ });
168
+ }
169
+
170
+ /**
171
+ * @description Returns the address associated with a specific ecdsa account & address offset. Optionally
172
+ * asks for on-device confirmation
173
+ */
174
+ public async getAddressEcdsa (confirm = false, accountIndex = 0, addressOffset = 0) {
175
+ const bip42Path = `m/44'/${this.#slip44}'/${accountIndex}'/${0}'/${addressOffset}'`;
176
+
177
+ return this.withApp(async (app: PolkadotGenericApp): Promise<LedgerAddress> => {
178
+ const { address, pubKey } = await wrapError(app.getAddressEcdsa(bip42Path, confirm));
179
+
180
+ return {
181
+ address,
182
+ publicKey: hexAddPrefix(pubKey)
183
+ };
184
+ });
185
+ }
186
+
187
+ /**
188
+ * @description Returns the version of the Ledger application on the device
189
+ */
190
+ public async getVersion (): Promise<LedgerVersion> {
191
+ return this.withApp(async (app: PolkadotGenericApp): Promise<LedgerVersion> => {
192
+ const { deviceLocked: isLocked, major, minor, patch, testMode: isTestMode } = await wrapError(app.getVersion());
193
+
194
+ return {
195
+ isLocked: !!isLocked,
196
+ isTestMode: !!isTestMode,
197
+ version: [major || 0, minor || 0, patch || 0]
198
+ };
199
+ });
200
+ }
201
+
202
+ /**
203
+ * @description Signs a transaction on the Ledger device. This requires the LedgerGeneric class to be instantiated with `chainId`, and `metaUrl`
204
+ */
205
+ public async sign (message: Uint8Array, accountIndex?: number, addressOffset?: number): Promise<LedgerSignature> {
206
+ return this.withApp(sign('signEd25519', message, this.#slip44, accountIndex, addressOffset));
207
+ }
208
+
209
+ /**
210
+ * @description Signs a message (non-transactional) on the Ledger device
211
+ */
212
+ public async signRaw (message: Uint8Array, accountIndex?: number, addressOffset?: number): Promise<LedgerSignature> {
213
+ return this.withApp(sign('signRawEd25519', u8aWrapBytes(message), this.#slip44, accountIndex, addressOffset));
214
+ }
215
+
216
+ /**
217
+ * @description Signs a transaction on the Ledger device with Ecdsa. This requires the LedgerGeneric class to be instantiated with `chainId`, and `metaUrl`
218
+ */
219
+ public async signEcdsa (message: Uint8Array, accountIndex?: number, addressOffset?: number): Promise<LedgerSignature> {
220
+ return this.withApp(signEcdsa('signEcdsa', u8aWrapBytes(message), this.#slip44, accountIndex, addressOffset));
221
+ }
222
+
223
+ /**
224
+ * @description Signs a message with Ecdsa (non-transactional) on the Ledger device
225
+ */
226
+ public async signRawEcdsa (message: Uint8Array, accountIndex?: number, addressOffset?: number): Promise<LedgerSignature> {
227
+ return this.withApp(signEcdsa('signRawEcdsa', u8aWrapBytes(message), this.#slip44, accountIndex, addressOffset));
228
+ }
229
+
230
+ /**
231
+ * @description Signs a transaction on the ledger device provided some metadata.
232
+ */
233
+ public async signWithMetadata (message: Uint8Array, accountIndex?: number, addressOffset?: number, options?: Partial<AccountOptionsGeneric>): Promise<LedgerSignature> {
234
+ return this.withApp(signWithMetadata(message, this.#slip44, accountIndex, addressOffset, options));
235
+ }
236
+
237
+ /**
238
+ * @description Signs a transaction on the ledger device for an ecdsa signature provided some metadata.
239
+ */
240
+ public async signWithMetadataEcdsa (message: Uint8Array, accountIndex?: number, addressOffset?: number, options?: Partial<AccountOptionsGeneric>) {
241
+ return this.withApp(signWithMetadataEcdsa(message, this.#slip44, accountIndex, addressOffset, options));
242
+ }
243
+
244
+ /**
245
+ * @internal
246
+ *
247
+ * Returns a created PolkadotGenericApp to perform operations against. Generally
248
+ * this is only used internally, to ensure consistent bahavior.
249
+ */
250
+ async withApp <T> (fn: (app: PolkadotGenericApp) => Promise<T>): Promise<T> {
251
+ try {
252
+ if (!this.#app) {
253
+ const transport = await this.#transportDef.create();
254
+
255
+ // We need this override for the actual type passing - the Deno environment
256
+ // is quite a bit stricter and it yields invalids between the two (specifically
257
+ // since we mangle the imports from .default in the types for CJS/ESM and between
258
+ // esm.sh versions this yields problematic outputs)
259
+ //
260
+ // eslint-disable-next-line @typescript-eslint/no-unsafe-argument, @typescript-eslint/no-explicit-any
261
+ this.#app = new PolkadotGenericApp(transport as any, this.#chainId, this.#metaUrl);
262
+ }
263
+
264
+ return await fn(this.#app);
265
+ } catch (error) {
266
+ this.#app = null;
267
+
268
+ throw error;
269
+ }
270
+ }
271
+ }
@@ -0,0 +1,2 @@
1
+ # @pezkuwi/hw-ledger
2
+
@@ -0,0 +1,3 @@
1
+
2
+ export { Ledger } from './Ledger.ts';
3
+ export { LedgerGeneric } from './LedgerGeneric.ts';
@@ -0,0 +1,8 @@
1
+
2
+ export const LEDGER_DEFAULT_ACCOUNT = 0x80000000;
3
+
4
+ export const LEDGER_DEFAULT_CHANGE = 0x80000000;
5
+
6
+ export const LEDGER_DEFAULT_INDEX = 0x80000000;
7
+
8
+ export const LEDGER_SUCCESS_CODE = 0x9000;
@@ -0,0 +1,61 @@
1
+
2
+ export const prevLedgerRecord: Record<string, string> = {
3
+ acala: 'Acala',
4
+ ajuna: 'Ajuna',
5
+ 'aleph-node': 'AlephZero',
6
+ astar: 'Astar',
7
+ bifrost: 'Bifrost',
8
+ 'bifrost-kusama': 'BifrostKusama',
9
+ centrifuge: 'Centrifuge',
10
+ composable: 'Composable',
11
+ darwinia: 'Darwinia',
12
+ 'dock-mainnet': 'Dock',
13
+ edgeware: 'Edgeware',
14
+ enjin: 'Enjin',
15
+ equilibrium: 'Equilibrium',
16
+ genshiro: 'Genshiro',
17
+ hydradx: 'HydraDX',
18
+ 'interlay-parachain': 'Interlay',
19
+ karura: 'Karura',
20
+ khala: 'Khala',
21
+ kusama: 'Kusama',
22
+ matrixchain: 'Matrixchain',
23
+ nodle: 'Nodle',
24
+ origintrail: 'OriginTrail',
25
+ parallel: 'Parallel',
26
+ peaq: 'Peaq',
27
+ pendulum: 'Pendulum',
28
+ phala: 'Phala',
29
+ picasso: 'Picasso',
30
+ polkadex: 'Polkadex',
31
+ polkadot: 'Polkadot',
32
+ polymesh: 'Polymesh',
33
+ quartz: 'Quartz',
34
+ sora: 'Sora',
35
+ stafi: 'Stafi',
36
+ statemine: 'Statemine',
37
+ statemint: 'Statemint',
38
+ ternoa: 'Ternoa',
39
+ unique: 'Unique',
40
+ vtb: 'VTB',
41
+ xxnetwork: 'XXNetwork',
42
+ zeitgeist: 'Zeitgeist'
43
+ };
44
+
45
+ export const genericLedgerApps = {
46
+ bittensor: 'Bittensor',
47
+ creditcoin3: 'Creditcoin3',
48
+ dentnet: 'DENTNet',
49
+ encointer: 'Encointer',
50
+ frequency: 'Frequency',
51
+ integritee: 'Integritee',
52
+ liberland: 'Liberland',
53
+ mythos: 'Mythos',
54
+ polimec: 'Polimec',
55
+ vara: 'Vara'
56
+ };
57
+
58
+ export const ledgerApps: Record<string, string> = {
59
+ ...prevLedgerRecord,
60
+ ...genericLedgerApps
61
+ };
@@ -0,0 +1,4 @@
1
+
2
+ import './packageDetect.ts';
3
+
4
+ export * from './bundle.ts';
@@ -0,0 +1,2 @@
1
+
2
+ export * from './index.ts';
@@ -0,0 +1,9 @@
1
+
2
+
3
+ import { packageInfo as transportInfo } from 'https://deno.land/x/pezkuwi/hw-ledger-transports/packageInfo.ts';
4
+ import { detectPackage } from 'https://deno.land/x/pezkuwi/util/mod.ts';
5
+ import { packageInfo as utilInfo } from 'https://deno.land/x/pezkuwi/util/packageInfo.ts';
6
+
7
+ import { packageInfo } from './packageInfo.ts';
8
+
9
+ detectPackage(packageInfo, null, [transportInfo, utilInfo]);
@@ -0,0 +1,3 @@
1
+
2
+
3
+ export const packageInfo = { name: '@pezkuwi/hw-ledger', path: new URL(import.meta.url).pathname, type: 'deno', version: '14.0.6' };
@@ -0,0 +1,42 @@
1
+ import { Buffer } from 'node:buffer';
2
+
3
+
4
+ import type { HexString } from 'https://deno.land/x/pezkuwi/util/types.ts';
5
+
6
+ /**
7
+ * Legacy Type that works with the `Ledger` class.
8
+ */
9
+ export interface AccountOptions {
10
+ /** The index of the account */
11
+ account: number;
12
+ /** The index of the address */
13
+ addressIndex: number;
14
+ /** The change to apply */
15
+ change: number;
16
+ }
17
+
18
+ export interface AccountOptionsGeneric extends AccountOptions {
19
+ /** Option for PolkadotGenericApp.signWithMetadata */
20
+ metadata: Buffer;
21
+ }
22
+
23
+ export interface LedgerAddress {
24
+ /** The ss58 encoded address */
25
+ address: string;
26
+ /** The hex-encoded publicKey */
27
+ publicKey: HexString;
28
+ }
29
+
30
+ export interface LedgerSignature {
31
+ /** A hex-encoded signature, as generated by the device */
32
+ signature: HexString;
33
+ }
34
+
35
+ export interface LedgerVersion {
36
+ /** Indicator flag for locked status */
37
+ isLocked: boolean;
38
+ /** Indicator flag for testmode status */
39
+ isTestMode: boolean;
40
+ /** The software version for this device */
41
+ version: [major: number, minor: number, patch: number];
42
+ }
@@ -1,5 +1,5 @@
1
1
  import type { SubstrateApp } from '@zondax/ledger-substrate';
2
- import type { TransportType } from '@polkadot/hw-ledger-transports/types';
2
+ import type { TransportType } from '@pezkuwi/hw-ledger-transports/types';
3
3
  import type { AccountOptions, LedgerAddress, LedgerSignature, LedgerVersion } from './types.js';
4
4
  import { ledgerApps } from './defaults.js';
5
5
  export { packageInfo } from './packageInfo.js';
@@ -1,4 +1,4 @@
1
- import type { TransportType } from '@polkadot/hw-ledger-transports/types';
1
+ import type { TransportType } from '@pezkuwi/hw-ledger-transports/types';
2
2
  import type { AccountOptionsGeneric, LedgerAddress, LedgerSignature, LedgerVersion } from './types.js';
3
3
  import { PolkadotGenericApp } from '@zondax/ledger-substrate';
4
4
  import { ledgerApps } from './defaults.js';
@@ -1,4 +1,4 @@
1
- import type { HexString } from '@polkadot/util/types';
1
+ import type { HexString } from '@pezkuwi/util/types';
2
2
  /**
3
3
  * Legacy Type that works with the `Ledger` class.
4
4
  */
@@ -0,0 +1,7 @@
1
+ "use strict";
2
+ Object.defineProperty(exports, "__esModule", { value: true });
3
+ const packageInfo_1 = require("@pezkuwi/hw-ledger-transports/packageInfo");
4
+ const util_1 = require("@pezkuwi/util");
5
+ const packageInfo_2 = require("@pezkuwi/util/packageInfo");
6
+ const packageInfo_js_1 = require("./packageInfo.js");
7
+ (0, util_1.detectPackage)(packageInfo_js_1.packageInfo, null, [packageInfo_1.packageInfo, packageInfo_2.packageInfo]);
@@ -1,4 +1,4 @@
1
1
  "use strict";
2
2
  Object.defineProperty(exports, "__esModule", { value: true });
3
3
  exports.packageInfo = void 0;
4
- exports.packageInfo = { name: '@pezkuwi/hw-ledger', path: typeof __dirname === 'string' ? __dirname : 'auto', type: 'cjs', version: '14.0.1' };
4
+ exports.packageInfo = { name: '@pezkuwi/hw-ledger', path: typeof __dirname === 'string' ? __dirname : 'auto', type: 'cjs', version: '14.0.6' };
@@ -1 +1 @@
1
- export const packageInfo = { name: '@pezkuwi/hw-ledger', path: (import.meta && import.meta.url) ? new URL(import.meta.url).pathname.substring(0, new URL(import.meta.url).pathname.lastIndexOf('/') + 1) : 'auto', type: 'esm', version: '14.0.1' };
1
+ export const packageInfo = { name: '@pezkuwi/hw-ledger', path: (import.meta && import.meta.url) ? new URL(import.meta.url).pathname.substring(0, new URL(import.meta.url).pathname.lastIndexOf('/') + 1) : 'auto', type: 'esm', version: '14.0.6' };