Skip to content

Commit 8dab699

Browse files
committed
chore: fix typo
1 parent a4bb524 commit 8dab699

8 files changed

Lines changed: 64 additions & 71 deletions

File tree

README.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -56,6 +56,7 @@ linkStyle default opacity:0.5
5656
keyring_api --> keyring_utils;
5757
eth_hd_keyring --> keyring_utils;
5858
eth_ledger_bridge_keyring --> keyring_utils;
59+
eth_onekey_keyring --> keyring_utils;
5960
eth_qr_keyring --> keyring_utils;
6061
eth_simple_keyring --> keyring_utils;
6162
eth_trezor_keyring --> keyring_utils;

packages/keyring-eth-onekey/jest.config.js

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -23,10 +23,10 @@ module.exports = merge(baseConfig, {
2323
// An object that configures minimum threshold enforcement for coverage results
2424
coverageThreshold: {
2525
global: {
26-
branches: 70.51,
27-
functions: 90.12,
28-
lines: 92.83,
29-
statements: 92.88,
26+
branches: 70.81,
27+
functions: 87.95,
28+
lines: 92.22,
29+
statements: 92.28,
3030
},
3131
},
3232
});

packages/keyring-eth-onekey/package.json

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -50,6 +50,7 @@
5050
"@ethereumjs/tx": "^5.4.0",
5151
"@ethereumjs/util": "^9.1.0",
5252
"@metamask/eth-sig-util": "^8.2.0",
53+
"@metamask/utils": "^11.1.0",
5354
"@noble/hashes": "^1.7.0",
5455
"@onekeyfe/hd-core": "1.1.6-patch.4",
5556
"@onekeyfe/hd-shared": "1.1.6-patch.4",
@@ -62,6 +63,7 @@
6263
"@lavamoat/allow-scripts": "^3.2.1",
6364
"@lavamoat/preinstall-always-fail": "^2.1.0",
6465
"@metamask/auto-changelog": "^3.4.4",
66+
"@metamask/keyring-utils": "workspace:^",
6567
"@ts-bridge/cli": "^0.6.3",
6668
"@types/bytebuffer": "^5.0.49",
6769
"@types/ethereumjs-tx": "^1.0.1",

packages/keyring-eth-onekey/src/onekey-bridge.ts

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,6 @@
22
/* eslint-disable @typescript-eslint/no-explicit-any */
33
/* eslint-disable @typescript-eslint/naming-convention */
44
import type {
5-
ConnectSettings,
65
Params,
76
EVMSignedTx,
87
EVMSignTransactionParams,
@@ -32,7 +31,7 @@ export type OneKeyBridge = {
3231

3332
off(event: string): void;
3433

35-
init(settings: Partial<ConnectSettings>): Promise<void>;
34+
init(): Promise<void>;
3635

3736
dispose(): Promise<void>;
3837

packages/keyring-eth-onekey/src/onekey-keyring.test.ts

Lines changed: 11 additions & 35 deletions
Original file line numberDiff line numberDiff line change
@@ -146,26 +146,6 @@ describe('OneKeyKeyring', function () {
146146
});
147147
});
148148

149-
describe('init', function () {
150-
it('initialises the bridge', async function () {
151-
const initStub = sinon.stub().resolves();
152-
bridge.init = initStub;
153-
154-
await keyring.init({
155-
fetchConfig: true,
156-
connectSrc: CONNECT_SRC,
157-
env: 'web',
158-
});
159-
160-
expect(initStub.calledOnce).toBe(true);
161-
sinon.assert.calledWithExactly(initStub, {
162-
fetchConfig: true,
163-
connectSrc: CONNECT_SRC,
164-
env: 'web',
165-
});
166-
});
167-
});
168-
169149
describe('destroy', function () {
170150
it('calls dispose on bridge', async function () {
171151
const disposeStub = sinon.stub().resolves();
@@ -447,14 +427,16 @@ describe('OneKeyKeyring', function () {
447427
Buffer.from(Address.fromString(fakeAccounts[0]).bytes),
448428
);
449429

430+
// @ts-expect-error: intentionally using an old library that doesn't comply with TypedTransaction
450431
const returnedTx = await keyring.signTransaction(fakeAccounts[0], fakeTx);
451432
// assert that the v,r,s values got assigned to tx.
452433
expect(returnedTx.v).toBeDefined();
453434
expect(returnedTx.r).toBeDefined();
454435
expect(returnedTx.s).toBeDefined();
455436
// ensure we get a older version transaction back
437+
// @ts-expect-error: intentionally using an old library that doesn't comply with TypedTransaction
456438
expect((returnedTx as EthereumTx).getChainId()).toBe(1);
457-
expect((returnedTx as TypedTransaction).common).toBeUndefined();
439+
expect(returnedTx.common).toBeUndefined();
458440
expect(ethereumSignTransactionStub.calledOnce).toBe(true);
459441
});
460442

@@ -482,11 +464,10 @@ describe('OneKeyKeyring', function () {
482464
newFakeTx,
483465
);
484466
// ensure we get a new version transaction back
467+
// @ts-expect-error: intentionally using an old library that doesn't comply with TypedTransaction
485468
// eslint-disable-next-line @typescript-eslint/unbound-method
486469
expect((returnedTx as EthereumTx).getChainId).toBeUndefined();
487-
expect(
488-
(returnedTx as TypedTransaction).common.chainId().toString(16),
489-
).toBe('1');
470+
expect(returnedTx.common.chainId().toString(16)).toBe('1');
490471
expect(ethereumSignTransactionStub.calledOnce).toBe(true);
491472
});
492473

@@ -517,11 +498,10 @@ describe('OneKeyKeyring', function () {
517498
contractDeploymentFakeTx,
518499
);
519500
// ensure we get a new version transaction back
501+
// @ts-expect-error: intentionally using an old library that doesn't comply with TypedTransaction
520502
// eslint-disable-next-line @typescript-eslint/unbound-method
521503
expect((returnedTx as EthereumTx).getChainId).toBeUndefined();
522-
expect(
523-
(returnedTx as TypedTransaction).common.chainId().toString(16),
524-
).toBe('1');
504+
expect(returnedTx.common.chainId().toString(16)).toBe('1');
525505
expect(ethereumSignTransactionStub.calledOnce).toBe(true);
526506
expect(ethereumSignTransactionStub.getCall(0).args[0]).toStrictEqual({
527507
passphraseState: '',
@@ -771,6 +751,7 @@ describe('OneKeyKeyring', function () {
771751
bridge.ethereumSignTransaction = sinon.stub().resolves(errorResponse);
772752

773753
try {
754+
// @ts-expect-error: intentionally using an old library that doesn't comply with TypedTransaction
774755
await keyring.signTransaction(fakeAccounts[0], fakeTx);
775756
throw new Error('Expected error was not thrown');
776757
} catch (error) {
@@ -911,6 +892,7 @@ describe('OneKeyKeyring', function () {
911892
// @ts-expect-error - for testing purposes
912893
fakeTx.to = null;
913894

895+
// @ts-expect-error: intentionally using an old library that doesn't comply with TypedTransaction
914896
const result = await keyring.signTransaction(fakeAccounts[0], fakeTx);
915897
expect(result).toBeDefined();
916898

@@ -1006,7 +988,7 @@ describe('OneKeyKeyring', function () {
1006988
it('should handle init bridge method', async function () {
1007989
const initSpy = sinon.stub(bridge, 'init').resolves();
1008990

1009-
await keyring.init({ debug: true });
991+
await keyring.init();
1010992
expect(initSpy.calledOnce).toBe(true);
1011993

1012994
// Test destroy method by calling keyring destroy
@@ -1052,6 +1034,7 @@ describe('OneKeyKeyring', function () {
10521034
Buffer.from(Address.fromString(fakeAccounts[0]).bytes),
10531035
);
10541036

1037+
// @ts-expect-error: intentionally using an old library that doesn't comply with TypedTransaction
10551038
const result = await keyring.signTransaction(fakeAccounts[0], fakeTx);
10561039
expect(result).toBeDefined();
10571040
expect(result.v).toBeDefined();
@@ -1086,13 +1069,6 @@ describe('OneKeyKeyring', function () {
10861069
}).toThrow('Bridge is a required dependency for the keyring');
10871070
});
10881071

1089-
it('should handle event emission from bridge', function () {
1090-
const eventSpy = sinon.stub(keyring, 'emit');
1091-
1092-
expect(keyring.bridge).toBe(bridge);
1093-
expect(eventSpy.called).toBe(false);
1094-
});
1095-
10961072
describe('HD path variations', function () {
10971073
it('should handle Ledger Legacy HD path in setHdPath', function () {
10981074
// Cover branches in #isSameHdPath for Ledger Legacy

packages/keyring-eth-onekey/src/onekey-keyring.ts

Lines changed: 43 additions & 29 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,8 @@ import { TransactionFactory } from '@ethereumjs/tx';
55
import * as ethUtil from '@ethereumjs/util';
66
import type { MessageTypes, TypedMessage } from '@metamask/eth-sig-util';
77
import { SignTypedDataVersion, TypedDataUtils } from '@metamask/eth-sig-util';
8+
import type { Keyring } from '@metamask/keyring-utils';
9+
import type { Hex, Json } from '@metamask/utils';
810
import type {
911
ConnectSettings,
1012
EthereumSignTypedDataMessage,
@@ -57,16 +59,16 @@ export type AccountPage = AccountPageEntry[];
5759

5860
export 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

6668
export 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
}

packages/keyring-utils/src/keyring.ts

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -77,7 +77,6 @@ export type Keyring = {
7777
* @param state - A JSON-serializable representation of the keyring state.
7878
*/
7979
deserialize(state: Json): Promise<void>;
80-
8180
/**
8281
* Method to include asynchronous configuration.
8382
*/

yarn.lock

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1753,6 +1753,8 @@ __metadata:
17531753
"@lavamoat/preinstall-always-fail": "npm:^2.1.0"
17541754
"@metamask/auto-changelog": "npm:^3.4.4"
17551755
"@metamask/eth-sig-util": "npm:^8.2.0"
1756+
"@metamask/keyring-utils": "workspace:^"
1757+
"@metamask/utils": "npm:^11.1.0"
17561758
"@noble/hashes": "npm:^1.7.0"
17571759
"@onekeyfe/hd-core": "npm:1.1.6-patch.4"
17581760
"@onekeyfe/hd-shared": "npm:1.1.6-patch.4"

0 commit comments

Comments
 (0)