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
1011import { LEDGER_DEFAULT_ACCOUNT , LEDGER_DEFAULT_CHANGE , LEDGER_DEFAULT_INDEX , LEDGER_SUCCESS_CODE } from './constants.ts' ;
1112import { ledgerApps } from './defaults.ts' ;
@@ -30,22 +31,28 @@ async function wrapError <T extends WrappedResult> (promise: Promise<T>): Promis
3031export 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
0 commit comments