Skip to content

Commit e77946a

Browse files
hw-{ledger, ledger-transports}, keyring, networks, util, util-crypto, x-{bigint, fetch, global, randomvalues, textdecoder, textencoder, ws} 11.1.2
1 parent 20e2108 commit e77946a

File tree

217 files changed

+396
-393
lines changed

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

217 files changed

+396
-393
lines changed

CHANGELOG.md

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,9 @@
11
# CHANGELOG
22

3+
## master
4+
5+
- hw-{ledger, ledger-transports}, keyring, networks, util, util-crypto, x-{bigint, fetch, global, randomvalues, textdecoder, textencoder, ws} 11.1.2
6+
37
## 0.2.32 Mar 19, 2023
48

59
- api, api-{augment, base, contract, derive}, rpc-{augment, core, provider}, types, types-{augment, codec, create, known} 10.1.4

hw-ledger-transports/browser.ts

Lines changed: 4 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -1,20 +1,9 @@
11

2-
import type { Transport, TransportDef } from './types.ts';
2+
import LedgerHid from 'https://esm.sh/@ledgerhq/hw-transport-webhid@6.27.12';
3+
import LedgerUsb from 'https://esm.sh/@ledgerhq/hw-transport-webusb@6.27.12';
34

4-
import LedgerWebHid from 'https://esm.sh/@ledgerhq/hw-transport-webhid@6.27.12';
5-
import LedgerWebUsb from 'https://esm.sh/@ledgerhq/hw-transport-webusb@6.27.12';
5+
import { createDefs } from './util.ts';
66

77
export { packageInfo } from './packageInfo.ts';
88

9-
export const transports: TransportDef[] = [
10-
{
11-
create: (): Promise<Transport> =>
12-
(LedgerWebUsb as unknown as Transport).create(),
13-
type: 'webusb'
14-
},
15-
{
16-
create: (): Promise<Transport> =>
17-
(LedgerWebHid as unknown as Transport).create(),
18-
type: 'hid'
19-
}
20-
];
9+
export const transports = /*#__PURE__*/ createDefs(['webusb', LedgerUsb], ['hid', LedgerHid]);

hw-ledger-transports/detectPackage.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11

22

3-
import { detectPackage } from 'https://deno.land/x/polkadot@0.2.32/util/mod.ts';
3+
import { detectPackage } from 'https://deno.land/x/polkadot/util/mod.ts';
44

55
import others from './detectOther.ts';
66
import { packageInfo } from './packageInfo.ts';

hw-ledger-transports/empty.ts

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11

2-
import type { TransportDef } from './types.ts';
2+
import { createDefs } from './util.ts';
33

44
export { packageInfo } from './packageInfo.ts';
55

6-
export const transports: TransportDef[] = [];
6+
export const transports = /*#__PURE__*/ createDefs();

hw-ledger-transports/node.ts

Lines changed: 3 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -1,14 +1,8 @@
11

2-
import type { Transport, TransportDef } from './types.ts';
3-
42
import LedgerHid from 'https://esm.sh/@ledgerhq/hw-transport-node-hid-singleton@6.28.9';
53

4+
import { createDefs } from './util.ts';
5+
66
export { packageInfo } from './packageInfo.ts';
77

8-
export const transports: TransportDef[] = [
9-
{
10-
create: (): Promise<Transport> =>
11-
(LedgerHid as unknown as Transport).create(),
12-
type: 'hid'
13-
}
14-
];
8+
export const transports = /*#__PURE__*/ createDefs(['hid', LedgerHid]);
Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,3 @@
11

22

3-
export const packageInfo = { name: '@polkadot/hw-ledger-transports', path: new URL(import.meta.url).pathname, type: 'deno', version: '0.2.32' };
3+
export const packageInfo = { name: '@polkadot/hw-ledger-transports', path: new URL(import.meta.url).pathname, type: 'deno', version: '11.1.2' };

hw-ledger-transports/types.ts

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,13 +1,14 @@
11

2-
export type LedgerTypes = 'hid' | 'u2f' | 'webusb';
2+
export type TransportType = 'hid' | 'webusb';
3+
34

45
export interface Transport {
5-
create (): Promise<Transport>;
6+
// empty on purpose, just a stub
67
}
78

89
export interface TransportDef {
910
/** Create a transport to be used in Ledger operations */
1011
create (): Promise<Transport>;
1112
/** The type of the underlying transport definition */
12-
type: LedgerTypes;
13+
type: TransportType;
1314
}

hw-ledger-transports/util.ts

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,10 @@
1+
2+
import type { Transport, TransportDef, TransportType } from './types.ts';
3+
4+
export function createDefs (...items: readonly [type: TransportType, Clazz: unknown][]): TransportDef[] {
5+
return items.map(([type, Clazz]): TransportDef => ({
6+
create: (): Promise<Transport> =>
7+
(Clazz as Pick<TransportDef, 'create'>).create(),
8+
type
9+
}));
10+
}

hw-ledger/bundle.ts

Lines changed: 40 additions & 33 deletions
Original file line numberDiff line numberDiff line change
@@ -1,11 +1,12 @@
11

2-
import type { SubstrateApp } from 'https://esm.sh/@zondax/ledger-substrate@0.40.5';
3-
import type { AccountOptions, LedgerAddress, LedgerSignature, LedgerTypes, LedgerVersion } from './types.ts';
2+
import type { SubstrateApp } from 'https://esm.sh/@zondax/ledger-substrate@0.40.6';
3+
import type { TransportDef, TransportType } from 'https://deno.land/x/polkadot/hw-ledger-transports/types.ts';
4+
import type { AccountOptions, LedgerAddress, LedgerSignature, LedgerVersion } from './types.ts';
45

5-
import { newSubstrateApp } from 'https://esm.sh/@zondax/ledger-substrate@0.40.5';
6+
import { newSubstrateApp } from 'https://esm.sh/@zondax/ledger-substrate@0.40.6';
67

7-
import { transports } from 'https://deno.land/x/polkadot@0.2.32/hw-ledger-transports/mod.ts';
8-
import { hexAddPrefix, u8aToBuffer } from 'https://deno.land/x/polkadot@0.2.32/util/mod.ts';
8+
import { transports } from 'https://deno.land/x/polkadot/hw-ledger-transports/mod.ts';
9+
import { hexAddPrefix, u8aToBuffer } from 'https://deno.land/x/polkadot/util/mod.ts';
910

1011
import { LEDGER_DEFAULT_ACCOUNT, LEDGER_DEFAULT_CHANGE, LEDGER_DEFAULT_INDEX, LEDGER_SUCCESS_CODE } from './constants.ts';
1112
import { ledgerApps } from './defaults.ts';
@@ -30,22 +31,28 @@ async function wrapError <T extends WrappedResult> (promise: Promise<T>): Promis
3031
export class Ledger {
3132
#app: SubstrateApp | null = null;
3233

33-
#chain: Chain;
34+
#ledgerName: string;
3435

35-
#transport: LedgerTypes;
36+
#transportDef: TransportDef;
3637

37-
constructor (transport: LedgerTypes, chain: Chain) {
38-
// u2f is deprecated
39-
if (!['hid', 'webusb'].includes(transport)) {
40-
throw new Error(`Unsupported transport ${transport}`);
41-
} else if (!Object.keys(ledgerApps).includes(chain)) {
42-
throw new Error(`Unsupported chain ${chain}`);
38+
constructor (transport: TransportType, chain: Chain) {
39+
const ledgerName = ledgerApps[chain];
40+
const transportDef = transports.find(({ type }) => type === transport);
41+
42+
if (!ledgerName) {
43+
throw new Error(`Unsupported Ledger chain ${chain}`);
44+
} else if (!transportDef) {
45+
throw new Error(`Unsupported Ledger transport ${transport}`);
4346
}
4447

45-
this.#chain = chain;
46-
this.#transport = transport;
48+
this.#ledgerName = ledgerName;
49+
this.#transportDef = transportDef;
4750
}
4851

52+
/**
53+
* Returns the address associated with a specific account & address offset. Optionally
54+
* asks for on-device confirmation
55+
*/
4956
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> {
5057
return this.withApp(async (app: SubstrateApp): Promise<LedgerAddress> => {
5158
const { address, pubKey } = await wrapError(app.getAddress(account + accountOffset, change, addressIndex + addressOffset, confirm));
@@ -57,6 +64,9 @@ export class Ledger {
5764
});
5865
}
5966

67+
/**
68+
* Returns the version of the Ledger application on the device
69+
*/
6070
public async getVersion (): Promise<LedgerVersion> {
6171
return this.withApp(async (app: SubstrateApp): Promise<LedgerVersion> => {
6272
const { device_locked: isLocked, major, minor, patch, test_mode: isTestMode } = await wrapError(app.getVersion());
@@ -69,6 +79,9 @@ export class Ledger {
6979
});
7080
}
7181

82+
/**
83+
* Signs a transcation on the Ledger device
84+
*/
7285
public async sign (message: Uint8Array, accountOffset = 0, addressOffset = 0, { account = LEDGER_DEFAULT_ACCOUNT, addressIndex = LEDGER_DEFAULT_INDEX, change = LEDGER_DEFAULT_CHANGE }: Partial<AccountOptions> = {}): Promise<LedgerSignature> {
7386
return this.withApp(async (app: SubstrateApp): Promise<LedgerSignature> => {
7487
const buffer = u8aToBuffer(message);
@@ -80,27 +93,21 @@ export class Ledger {
8093
});
8194
}
8295

83-
async getApp (): Promise<SubstrateApp> {
84-
if (!this.#app) {
85-
const def = transports.find(({ type }) => type === this.#transport);
86-
87-
if (!def) {
88-
throw new Error(`Unable to find a transport for ${this.#transport}`);
89-
}
90-
91-
const transport = await def.create();
92-
93-
this.#app = newSubstrateApp(transport, ledgerApps[this.#chain]);
94-
}
95-
96-
return this.#app;
97-
}
98-
96+
/**
97+
* @internal
98+
*
99+
* Returns a created SubstrateApp to perform operations against. Generally
100+
* this is only used internally, to ensure consistent bahavior.
101+
*/
99102
async withApp <T> (fn: (app: SubstrateApp) => Promise<T>): Promise<T> {
100103
try {
101-
const app = await this.getApp();
104+
if (!this.#app) {
105+
const transport = await this.#transportDef.create();
106+
107+
this.#app = newSubstrateApp(transport, this.#ledgerName);
108+
}
102109

103-
return await fn(app);
110+
return await fn(this.#app);
104111
} catch (error) {
105112
this.#app = null;
106113

hw-ledger/detectOther.ts

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11

2-
import { packageInfo as transportInfo } from 'https://deno.land/x/polkadot@0.2.32/hw-ledger-transports/packageInfo.ts';
3-
import { packageInfo as utilInfo } from 'https://deno.land/x/polkadot@0.2.32/util/packageInfo.ts';
2+
import { packageInfo as transportInfo } from 'https://deno.land/x/polkadot/hw-ledger-transports/packageInfo.ts';
3+
import { packageInfo as utilInfo } from 'https://deno.land/x/polkadot/util/packageInfo.ts';
44

55
export default [transportInfo, utilInfo];

0 commit comments

Comments
 (0)