Skip to content
Open
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Next Next commit
feat: support onekey
  • Loading branch information
ByteZhang1024 committed Aug 15, 2025
commit ec02eda2bfe52f186e078a2c7290ce9ce89e8e93
15 changes: 15 additions & 0 deletions packages/keyring-eth-onekey/LICENSE
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
ISC License

Copyright (c) 2020 MetaMask

Permission to use, copy, modify, and/or distribute this software for any
purpose with or without fee is hereby granted, provided that the above
copyright notice and this permission notice appear in all copies.

THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES
WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF
MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR
ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES
WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN
ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF
OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
41 changes: 41 additions & 0 deletions packages/keyring-eth-onekey/README.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,41 @@
# eth-onekey-bridge-keyring

An implementation of MetaMask's [Keyring interface](https://github.com/MetaMask/eth-simple-keyring#the-keyring-class-protocol), that uses a OneKey hardware wallet for all cryptographic operations.

In most regards, it works in the same way as
[eth-hd-keyring](https://github.com/MetaMask/eth-hd-keyring), but using a OneKey
device. However there are a number of differences:

- Because the keys are stored in the device, operations that rely on the device
will fail if there is no OneKey device attached, or a different OneKey device
is attached.

- It does not support the `signMessage`, `signTypedData` or `exportAccount`
methods, because OneKey devices do not support these operations.

- Because extensions have limited access to browser features, there's no easy way to interact wth the OneKey Hardware wallet from the MetaMask extension. This library implements a workaround to those restrictions by injecting (on demand) an iframe to the background page of the extension,

## Usage

In addition to all the known methods from the [Keyring class protocol](https://github.com/MetaMask/eth-simple-keyring#the-keyring-class-protocol),
there are a few others:

- **isUnlocked** : Returns true if we have the public key in memory, which allows to generate the list of accounts at any time

- **unlock** : Connects to the OneKey device and exports the extended public key, which is later used to read the available ethereum addresses inside the OneKey account.

- **setAccountToUnlock** : the index of the account that you want to unlock in order to use with the signTransaction and signPersonalMessage methods

- **getFirstPage** : returns the first ordered set of accounts from the OneKey account

- **getNextPage** : returns the next ordered set of accounts from the OneKey account based on the current page

- **getPreviousPage** : returns the previous ordered set of accounts from the OneKey account based on the current page

- **forgetDevice** : removes all the device info from memory so the next interaction with the keyring will prompt the user to connect the OneKey device and export the account information

## Testing and Linting

Run `yarn test` to run the tests once. To run tests on file changes, run `yarn test:watch`.

Run `yarn lint` to run the linter, or run `yarn lint:fix` to run the linter and fix any automatically fixable issues.
32 changes: 32 additions & 0 deletions packages/keyring-eth-onekey/jest.config.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,32 @@
/*
* For a detailed explanation regarding each configuration property and type check, visit:
* https://jestjs.io/docs/configuration
*/

const merge = require('deepmerge');
const path = require('path');

const baseConfig = require('../../jest.config.packages');

const displayName = path.basename(__dirname);

module.exports = merge(baseConfig, {
// The display name when running multiple projects
displayName,

// An array of regexp pattern strings used to skip coverage collection
coveragePathIgnorePatterns: ['./src/tests'],

// The glob patterns Jest uses to detect test files
testMatch: ['**/*.test.[jt]s?(x)'],

// An object that configures minimum threshold enforcement for coverage results
coverageThreshold: {
global: {
branches: 52.38,
functions: 91.22,
lines: 90.15,
statements: 90.35,
},
},
});
107 changes: 107 additions & 0 deletions packages/keyring-eth-onekey/package.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,107 @@
{
"name": "@metamask/eth-onekey-keyring",
"version": "1.0.0",
"description": "A MetaMask compatible keyring, for onekey hardware wallets",
"keywords": [
"ethereum",
"keyring",
"onekey",
"metamask"
],
"homepage": "https://github.com/MetaMask/accounts/blob/main/packages/keyring-eth-onekey/README.md",
"bugs": {
"url": "https://github.com/MetaMask/accounts/issues"
},
"repository": {
"type": "git",
"url": "https://github.com/MetaMask/accounts.git"
},
"license": "ISC",
"exports": {
".": {
"import": {
"types": "./dist/index.d.mts",
"default": "./dist/index.mjs"
},
"require": {
"types": "./dist/index.d.cts",
"default": "./dist/index.cjs"
}
}
},
"main": "./dist/index.cjs",
"types": "./dist/index.d.cts",
"files": [
"dist/"
],
"scripts": {
"build": "ts-bridge --project tsconfig.build.json --no-references",
"build:clean": "yarn build --clean",
"build:docs": "typedoc",
"changelog:update": "../../scripts/update-changelog.sh @metamask/eth-trezor-keyring",
"changelog:validate": "../../scripts/validate-changelog.sh @metamask/eth-trezor-keyring",
"publish:preview": "yarn npm publish --tag preview",
"test": "jest && jest-it-up",
"test:clean": "jest --clearCache",
"test:watch": "jest --watch"
},
"dependencies": {
"@ethereumjs/tx": "^5.4.0",
"@ethereumjs/util": "^9.1.0",
"@metamask/eth-sig-util": "^8.2.0",
"@metamask/utils": "^11.1.0",
"@noble/hashes": "^1.7.0",
"@onekeyfe/hd-core": "^1.1.1",
"@onekeyfe/hd-shared": "^1.1.1",
"@onekeyfe/hd-transport": "^1.1.1",
"@onekeyfe/hd-web-sdk": "^1.1.1",
"bytebuffer": "^5.0.1",
"tslib": "^2.6.2"
},
"devDependencies": {
"@ethereumjs/common": "^4.4.0",
"@lavamoat/allow-scripts": "^3.2.1",
"@lavamoat/preinstall-always-fail": "^2.1.0",
"@metamask/auto-changelog": "^3.4.4",
"@metamask/keyring-utils": "workspace:^",
"@ts-bridge/cli": "^0.6.3",
"@types/ethereumjs-tx": "^1.0.1",
"@types/jest": "^29.5.12",
"@types/node": "^20.12.12",
"@types/sinon": "^17.0.3",
"@types/w3c-web-usb": "^1.0.6",
"deepmerge": "^4.2.2",
"depcheck": "^1.4.7",
"ethereumjs-tx": "^1.3.7",
"jest": "^29.5.0",
"jest-environment-jsdom": "^29.7.0",
"jest-it-up": "^3.1.0",
"sinon": "^19.0.2",
"ts-jest": "^29.0.5",
"ts-node": "^10.9.2",
"typedoc": "^0.25.13",
"typescript": "~5.6.3"
},
"engines": {
"node": "^18.18 || >=20"
},
"publishConfig": {
"access": "public",
"registry": "https://registry.npmjs.org/"
},
"installConfig": {
"hoistingLimits": "workspaces"
},
"lavamoat": {
"allowScripts": {
"@lavamoat/preinstall-always-fail": false,
"ethereumjs-tx>ethereumjs-util>keccak": false,
"ethereumjs-tx>ethereumjs-util>secp256k1": false,
"hdkey>secp256k1": false,
"ethereumjs-tx>ethereumjs-util>ethereum-cryptography>keccak": false,
"ethereumjs-tx>ethereumjs-util>ethereum-cryptography>secp256k1": false,
"@onekeyfe/hd-core>@onekeyfe/hd-transport>protobufjs": false,
"@onekeyfe/hd-web-sdk>@onekeyfe/hd-core>@onekeyfe/hd-transport>protobufjs": false
}
}
}
1 change: 1 addition & 0 deletions packages/keyring-eth-onekey/src/constants.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
export const ONEKEY_HARDWARE_UI_EVENT = 'onekey-hardware-ui-event';
4 changes: 4 additions & 0 deletions packages/keyring-eth-onekey/src/index.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
export * from './onekey-keyring';
export type * from './onekey-bridge';
export * from './onekey-web-bridge';
export * from './constants';
67 changes: 67 additions & 0 deletions packages/keyring-eth-onekey/src/onekey-bridge.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,67 @@
/* eslint-disable @typescript-eslint/no-redundant-type-constituents */
/* eslint-disable @typescript-eslint/no-explicit-any */
/* eslint-disable @typescript-eslint/naming-convention */
import type {
ConnectSettings,
Params,
EVMSignedTx,
EVMSignTransactionParams,
EVMSignMessageParams,
EVMSignTypedDataParams,
EVMGetPublicKeyParams,
Features,
} from '@onekeyfe/hd-core';
import type { EthereumMessageSignature } from '@onekeyfe/hd-transport';

type Unsuccessful = {
success: false;
payload: {
error: string;
code?: string | number;
};
};
type Success<T> = {
success: true;
payload: T;
};
type Response<T> = Promise<Success<T> | Unsuccessful>;

export type OneKeyBridge = {
model?: string;

on(event: string, callback: (event: any) => void): void;

off(event: string): void;

init(settings: Partial<ConnectSettings>): Promise<void>;

dispose(): Promise<void>;

updateTransportMethod(transportType: string): Promise<void>;

getDeviceFeatures(): Response<Features>;

// OneKeySdk.getPublicKey has two overloads
// It is not possible to extract them from the library using utility types
getPublicKey(
params: Params<EVMGetPublicKeyParams>,
): Response<{ publicKey: string; chainCode: string }>;

batchGetPublicKey(
params: Params<any> & { bundle: EVMGetPublicKeyParams[] },
): Response<{ pub: string }[]>;

getPassphraseState(): Response<string | undefined>;

ethereumSignTransaction(
params: Params<EVMSignTransactionParams>,
): Response<EVMSignedTx>;

ethereumSignMessage(
params: Params<EVMSignMessageParams>,
): Response<EthereumMessageSignature>;

ethereumSignTypedData(
params: Params<EVMSignTypedDataParams>,
): Response<EthereumMessageSignature>;
};
Loading