@@ -5,6 +5,8 @@ import { TransactionFactory } from '@ethereumjs/tx';
55import * as ethUtil from '@ethereumjs/util' ;
66import type { MessageTypes , TypedMessage } from '@metamask/eth-sig-util' ;
77import { SignTypedDataVersion , TypedDataUtils } from '@metamask/eth-sig-util' ;
8+ import type { Keyring } from '@metamask/keyring-utils' ;
9+ import type { Hex , Json } from '@metamask/utils' ;
810import type {
911 ConnectSettings ,
1012 EthereumSignTypedDataMessage ,
@@ -57,16 +59,16 @@ export type AccountPage = AccountPageEntry[];
5759
5860export type OneKeyControllerOptions = {
5961 hdPath ?: string ;
60- accounts ?: string [ ] ;
62+ accounts ?: Hex [ ] ;
6163 accountDetails ?: Readonly < Record < string , AccountDetails > > ;
6264 page ?: number ;
6365 passphraseState ?: string ;
6466} ;
6567
6668export type OneKeyControllerState = {
6769 hdPath : string ;
68- accounts : readonly string [ ] ;
69- accountDetails : Readonly < Record < string , AccountDetails > > ;
70+ accounts : string [ ] ;
71+ accountDetails : Record < string , AccountDetails > ;
7072 page : number ;
7173 passphraseState ?: string ;
7274} ;
@@ -127,7 +129,7 @@ function isEmptyPassphrase(passphraseState: string | undefined): boolean {
127129 ) ;
128130}
129131
130- export class OneKeyKeyring extends EventEmitter {
132+ export class OneKeyKeyring implements Keyring {
131133 readonly type : string = keyringType ;
132134
133135 static type : string = keyringType ;
@@ -140,7 +142,7 @@ export class OneKeyKeyring extends EventEmitter {
140142
141143 hdk = new HDKey ( ) ;
142144
143- accounts : readonly string [ ] = [ ] ;
145+ accounts : readonly Hex [ ] = [ ] ;
144146
145147 accountDetails : Record < string , AccountDetails > = { } ;
146148
@@ -154,43 +156,53 @@ export class OneKeyKeyring extends EventEmitter {
154156
155157 bridge : OneKeyBridge ;
156158
157- constructor ( { bridge } : { bridge : OneKeyBridge } ) {
158- super ( ) ;
159+ eventEmitter : EventEmitter ;
159160
161+ constructor ( { bridge } : { bridge : OneKeyBridge } ) {
160162 if ( ! bridge ) {
161163 throw new Error ( 'Bridge is a required dependency for the keyring' ) ;
162164 }
163165
166+ this . eventEmitter = new EventEmitter ( ) ;
167+
164168 this . bridge = bridge ;
165169 this . bridge . on ( ONEKEY_HARDWARE_UI_EVENT , ( _event : any ) => {
166- this . emit ( ONEKEY_HARDWARE_UI_EVENT , _event ) ;
170+ this . eventEmitter . emit ( ONEKEY_HARDWARE_UI_EVENT , _event ) ;
167171 } ) ;
168172 }
169173
170- async init ( settings : Partial < ConnectSettings > ) : Promise < void > {
171- return this . bridge . init ( settings ) ;
174+ async init ( ) : Promise < void > {
175+ return this . bridge . init ( ) ;
172176 }
173177
174178 async destroy ( ) : Promise < void > {
175179 this . bridge . off ( ONEKEY_HARDWARE_UI_EVENT ) ;
176180 return this . bridge . dispose ( ) ;
177181 }
178182
183+ on ( event : string , callback : ( event : any ) => void ) : void {
184+ this . eventEmitter . on ( event , callback ) ;
185+ }
186+
187+ off ( event : string , callback : ( event : any ) => void ) : void {
188+ this . eventEmitter . off ( event , callback ) ;
189+ }
190+
179191 async serialize ( ) : Promise < OneKeyControllerState > {
180- return Promise . resolve ( {
192+ return {
181193 hdPath : this . hdPath ,
182- accounts : this . accounts ,
183- accountDetails : this . accountDetails ,
194+ accounts : [ ... this . accounts ] ,
195+ accountDetails : { ... this . accountDetails } ,
184196 page : this . page ,
185- } ) ;
197+ } ;
186198 }
187199
188- async deserialize ( opts : OneKeyControllerOptions = { } ) : Promise < void > {
200+ async deserialize ( state : Json ) : Promise < void > {
201+ const opts = state as OneKeyControllerOptions ;
189202 this . hdPath = opts . hdPath ?? defaultHdPath ;
190203 this . accounts = opts . accounts ?? [ ] ;
191204 this . accountDetails = opts . accountDetails ?? { } ;
192205 this . page = opts . page ?? 0 ;
193- return Promise . resolve ( ) ;
194206 }
195207
196208 getModel ( ) : string | undefined {
@@ -273,15 +285,15 @@ export class OneKeyKeyring extends EventEmitter {
273285 } ) ;
274286 }
275287
276- async addAccounts ( numberOfAccounts = 1 ) : Promise < readonly string [ ] > {
288+ async addAccounts ( numberOfAccounts = 1 ) : Promise < Hex [ ] > {
277289 await this . unlock ( ) . catch ( ( error ) => {
278290 throw new Error ( error ?. toString ( ) || 'Unknown error' ) ;
279291 } ) ;
280292
281293 return new Promise ( ( resolve , reject ) => {
282294 const from = this . unlockedAccount ;
283295 const to = from + numberOfAccounts ;
284- const newAccounts : string [ ] = [ ] ;
296+ const newAccounts : Hex [ ] = [ ] ;
285297
286298 try {
287299 for ( let i = from ; i < to ; i ++ ) {
@@ -329,7 +341,7 @@ export class OneKeyKeyring extends EventEmitter {
329341 return this . #getPage( - 1 ) ;
330342 }
331343
332- async getAccounts ( ) : Promise < readonly string [ ] > {
344+ async getAccounts ( ) : Promise < Hex [ ] > {
333345 return Promise . resolve ( this . accounts . slice ( ) ) ;
334346 }
335347
@@ -353,7 +365,7 @@ export class OneKeyKeyring extends EventEmitter {
353365 }
354366
355367 #normalize( buffer : Buffer ) : string {
356- return ethUtil . bytesToHex ( buffer ) ;
368+ return ethUtil . bytesToHex ( new Uint8Array ( buffer ) ) ;
357369 }
358370
359371 /**
@@ -368,15 +380,16 @@ export class OneKeyKeyring extends EventEmitter {
368380 * ethereumjs transaction.
369381 */
370382 async signTransaction (
371- address : string ,
372- tx : TypedTransaction | OldEthJsTransaction ,
373- ) : Promise < TypedTransaction | OldEthJsTransaction > {
383+ address : Hex ,
384+ tx : TypedTransaction ,
385+ ) : Promise < TypedTransaction > {
374386 if ( isOldStyleEthereumjsTx ( tx ) ) {
375387 // In this version of ethereumjs-tx we must add the chainId in hex format
376388 // to the initial v value. The chainId must be included in the serialized
377389 // transaction which is only communicated to ethereumjs-tx in this
378390 // value. In newer versions the chainId is communicated via the 'Common'
379391 // object.
392+ // @ts -expect-error - support old transactions
380393 return this . #signTransaction(
381394 address ,
382395 // @types /ethereumjs-tx and old ethereumjs-tx versions document
@@ -386,10 +399,11 @@ export class OneKeyKeyring extends EventEmitter {
386399 tx . getChainId ( ) as unknown as number ,
387400 tx ,
388401 ( payload ) => {
389- tx . v = Buffer . from ( payload . v , 'hex' ) ;
390- tx . r = Buffer . from ( payload . r , 'hex' ) ;
391- tx . s = Buffer . from ( payload . s , 'hex' ) ;
392- return tx ;
402+ const newTx = tx as OldEthJsTransaction ;
403+ newTx . v = Buffer . from ( payload . v , 'hex' ) ;
404+ newTx . r = Buffer . from ( payload . r , 'hex' ) ;
405+ newTx . s = Buffer . from ( payload . s , 'hex' ) ;
406+ return newTx ;
393407 } ,
394408 ) ;
395409 }
@@ -632,10 +646,10 @@ export class OneKeyKeyring extends EventEmitter {
632646 return accountDetails ;
633647 }
634648
635- #addressFromIndex( i : number ) : string {
649+ #addressFromIndex( i : number ) : Hex {
636650 const dkey = this . hdk . derive ( this . #getDerivePath( i ) ) ;
637651 const address = ethUtil . bytesToHex (
638- ethUtil . publicToAddress ( dkey . publicKey , true ) ,
652+ ethUtil . publicToAddress ( new Uint8Array ( dkey . publicKey ) , true ) ,
639653 ) ;
640654 return ethUtil . toChecksumAddress ( address ) ;
641655 }
0 commit comments