@@ -8,8 +8,11 @@ import TransportWebUSB from "@ledgerhq/hw-transport-webusb";
88
99import { LedgerSigner } from "../ledgersigner" ;
1010
11- declare const window : any ;
12- declare const document : any ;
11+ const getElement = ( id : string ) : HTMLInputElement => {
12+ const e = document . getElementById ( id ) ;
13+ assert ( e instanceof HTMLInputElement , "got the wrong element!" ) ;
14+ return e ;
15+ } ;
1316
1417const accountNumbers = [ 0 , 1 , 2 , 10 ] ;
1518const paths = accountNumbers . map ( makeCosmoshubPath ) ;
@@ -45,7 +48,7 @@ function createSignDoc(accountNumber: number, address: string): string {
4548 return JSON . stringify ( signDoc , null , 2 ) ;
4649}
4750
48- window . updateMessage = ( accountNumberInput : unknown ) => {
51+ const updateMessage = ( accountNumberInput : unknown ) : void => {
4952 assert ( typeof accountNumberInput === "string" ) ;
5053 const accountNumber = Uint53 . fromString ( accountNumberInput ) . toNumber ( ) ;
5154 const account = accounts [ accountNumber ] ;
@@ -54,23 +57,23 @@ window.updateMessage = (accountNumberInput: unknown) => {
5457 }
5558
5659 const address = accounts [ accountNumber ] . address ;
57- const addressInput = document . getElementById ( "address" ) ;
60+ const addressInput = getElement ( "address" ) ;
5861 addressInput . value = address ;
59- const signDocTextArea = document . getElementById ( "sign-doc" ) ;
62+ const signDocTextArea = getElement ( "sign-doc" ) ;
6063 signDocTextArea . textContent = createSignDoc ( accountNumber , address ) ;
6164} ;
6265
63- window . setPath = ( accountNumberInput : unknown ) => {
66+ const setPath = ( accountNumberInput : unknown ) : void => {
6467 assert ( typeof accountNumberInput === "string" ) ;
6568 const accountNumber = Uint53 . fromString ( accountNumberInput ) . toNumber ( ) ;
6669
6770 const path = pathToString ( paths [ accountNumber ] ) ;
68- const pathInput = document . getElementById ( "path" ) ;
71+ const pathInput = getElement ( "path" ) ;
6972 pathInput . value = path ;
7073} ;
7174
7275// This must be called by the user an cannot be done on load (see "TransportWebUSBGestureRequired").
73- window . createSigner = async function createSigner ( ) : Promise < LedgerSigner > {
76+ const createSigner = async function createSigner ( ) : Promise < LedgerSigner > {
7477 const interactiveTimeout = 120_000 ;
7578 const ledgerTransport = await TransportWebUSB . create ( interactiveTimeout , interactiveTimeout ) ;
7679 return new LedgerSigner ( ledgerTransport , {
@@ -79,16 +82,16 @@ window.createSigner = async function createSigner(): Promise<LedgerSigner> {
7982 } ) ;
8083} ;
8184
82- window . getAccounts = async function getAccounts ( signer : LedgerSigner | undefined ) : Promise < void > {
85+ const getAccounts = async function getAccounts ( signer : LedgerSigner | undefined ) : Promise < void > {
8386 if ( signer === undefined ) {
8487 console . error ( "Please wait for transport to connect" ) ;
8588 return ;
8689 }
87- const accountNumberInput1 = document . getElementById ( "account-number1" ) ;
88- const accountNumberInput2 = document . getElementById ( "account-number2" ) ;
89- const addressInput = document . getElementById ( "address" ) ;
90- const accountsDiv = document . getElementById ( "accounts" ) ;
91- const signDocTextArea = document . getElementById ( "sign-doc" ) ;
90+ const accountNumberInput1 = getElement ( "account-number1" ) ;
91+ const accountNumberInput2 = getElement ( "account-number2" ) ;
92+ const addressInput = getElement ( "address" ) ;
93+ const accountsDiv = getElement ( "accounts" ) ;
94+ const signDocTextArea = getElement ( "sign-doc" ) ;
9295 accountsDiv . textContent = "Loading..." ;
9396
9497 try {
@@ -101,46 +104,48 @@ window.getAccounts = async function getAccounts(signer: LedgerSigner | undefined
101104 const accountNumber = 0 ;
102105
103106 // Show address block
104- accountNumberInput1 . max = accounts . length - 1 ;
105- accountNumberInput1 . value = accountNumber ;
106- window . setPath ( accountNumber . toString ( ) ) ;
107+ accountNumberInput1 . max = String ( accounts . length - 1 ) ;
108+ accountNumberInput1 . value = String ( accountNumber ) ;
109+ setPath ( accountNumber . toString ( ) ) ;
107110
108111 // Sign block
109- accountNumberInput2 . max = accounts . length - 1 ;
110- accountNumberInput2 . value = accountNumber ;
112+ accountNumberInput2 . max = String ( accounts . length - 1 ) ;
113+ accountNumberInput2 . value = String ( accountNumber ) ;
111114 const address = accounts [ 0 ] . address ;
112115 addressInput . value = address ;
113116 signDocTextArea . textContent = createSignDoc ( accountNumber , address ) ;
114117 } catch ( error ) {
115118 console . error ( error ) ;
116- accountsDiv . textContent = error ;
119+ accountsDiv . textContent = String ( error ) ;
117120 }
118121} ;
119122
120- window . showAddress = async function showAddress ( signer : LedgerSigner | undefined ) : Promise < void > {
123+ const showAddress = async function showAddress ( signer : LedgerSigner | undefined ) : Promise < void > {
121124 if ( signer === undefined ) {
122125 console . error ( "Please wait for transport to connect" ) ;
123126 return ;
124127 }
125- const path = stringToPath ( document . getElementById ( "path" ) . value ) ;
128+ const path = stringToPath ( getElement ( "path" ) . value ) ;
126129 await signer . showAddress ( path ) ;
127130} ;
128131
129- window . sign = async function sign ( signer : LedgerSigner | undefined ) : Promise < void > {
132+ const sign = async function sign ( signer : LedgerSigner | undefined ) : Promise < void > {
130133 if ( signer === undefined ) {
131134 console . error ( "Please wait for transport to connect" ) ;
132135 return ;
133136 }
134- const signatureDiv = document . getElementById ( "signature" ) ;
137+ const signatureDiv = getElement ( "signature" ) ;
135138 signatureDiv . textContent = "Loading..." ;
136139
137140 try {
138- const address = document . getElementById ( "address" ) . value ;
139- const signDocJson = document . getElementById ( "sign-doc" ) . textContent ;
141+ const address = getElement ( "address" ) . value ;
142+ const signDocJson = getElement ( "sign-doc" ) . textContent ;
140143 const signDoc : StdSignDoc = JSON . parse ( signDocJson ) ;
141144 const signature = await signer . signAmino ( address , signDoc ) ;
142145 signatureDiv . textContent = JSON . stringify ( signature , null , "\t" ) ;
143146 } catch ( error ) {
144- signatureDiv . textContent = error ;
147+ signatureDiv . textContent = String ( error ) ;
145148 }
146149} ;
150+
151+ Object . assign ( window , { updateMessage, setPath, createSigner, getAccounts, showAddress, sign } ) ;
0 commit comments