Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
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
3 changes: 3 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@ This repository contains the following packages [^fn1]:
- [`@metamask/account-api`](packages/account-api)
- [`@metamask/eth-hd-keyring`](packages/keyring-eth-hd)
- [`@metamask/eth-ledger-bridge-keyring`](packages/keyring-eth-ledger-bridge)
- [`@metamask/eth-onekey-keyring`](packages/keyring-eth-onekey)
- [`@metamask/eth-qr-keyring`](packages/keyring-eth-qr)
- [`@metamask/eth-simple-keyring`](packages/keyring-eth-simple)
- [`@metamask/eth-snap-keyring`](packages/keyring-snap-bridge)
Expand All @@ -40,6 +41,7 @@ linkStyle default opacity:0.5
keyring_api(["@metamask/keyring-api"]);
eth_hd_keyring(["@metamask/eth-hd-keyring"]);
eth_ledger_bridge_keyring(["@metamask/eth-ledger-bridge-keyring"]);
eth_onekey_keyring(["@metamask/eth-onekey-keyring"]);
eth_qr_keyring(["@metamask/eth-qr-keyring"]);
eth_simple_keyring(["@metamask/eth-simple-keyring"]);
eth_trezor_keyring(["@metamask/eth-trezor-keyring"]);
Expand All @@ -54,6 +56,7 @@ linkStyle default opacity:0.5
keyring_api --> keyring_utils;
eth_hd_keyring --> keyring_utils;
eth_ledger_bridge_keyring --> keyring_utils;
eth_onekey_keyring --> keyring_utils;
eth_qr_keyring --> keyring_utils;
eth_simple_keyring --> keyring_utils;
eth_trezor_keyring --> keyring_utils;
Expand Down
14 changes: 14 additions & 0 deletions packages/keyring-eth-onekey/CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
# Changelog

All notable changes to this project will be documented in this file.

The format is based on [Keep a Changelog](https://keepachangelog.com/en/1.0.0/),
and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0.html).

## [Unreleased]

### Changed

- Add initial implementation of the OneKey keyring ([#353](https://github.com/MetaMask/accounts/pull/353))

[Unreleased]: https://github.com/MetaMask/accounts/
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: 76.25,
functions: 98.59,
lines: 97.7,
statements: 97.71,
},
},
});
110 changes: 110 additions & 0 deletions packages/keyring-eth-onekey/package.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,110 @@
{
"name": "@metamask/eth-onekey-keyring",
"version": "0.1.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-onekey-keyring",
"changelog:validate": "../../scripts/validate-changelog.sh @metamask/eth-onekey-keyring",
"publish": "yarn npm publish",
"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.17-patch.1",
"@onekeyfe/hd-shared": "1.1.17-patch.1",
"@onekeyfe/hd-transport": "1.1.17-patch.1",
"@onekeyfe/hd-web-sdk": "1.1.17-patch.1",
"hdkey": "^2.1.0"
},
"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/bytebuffer": "^5.0.49",
"@types/ethereumjs-tx": "^1.0.1",
"@types/hdkey": "^2.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-transport>protobufjs": false,
"@onekeyfe/hd-core>@onekeyfe/hd-transport>protobufjs": false,
"@onekeyfe/hd-web-sdk>@onekeyfe/hd-core>@onekeyfe/hd-transport>protobufjs": false
}
}
}
3 changes: 3 additions & 0 deletions packages/keyring-eth-onekey/src/index.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
export * from './onekey-keyring';
export type * from './onekey-bridge';
export * from './onekey-web-bridge';
60 changes: 60 additions & 0 deletions packages/keyring-eth-onekey/src/onekey-bridge.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,60 @@
import type {
Params,
EVMSignedTx,
EVMSignTransactionParams,
EVMSignMessageParams,
EVMSignTypedDataParams,
EVMGetPublicKeyParams,
} from '@onekeyfe/hd-core';
import type { EthereumMessageSignature } from '@onekeyfe/hd-transport';

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

/**
* Hardware UI event payload
*/
export type HardwareUIEvent = {
error: string;
code?: string | number;
};

export type OneKeyBridge = {
model?: string;

init(): Promise<void>;

dispose(): Promise<void>;

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

// 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 }>;

getPassphraseState(): Response<string | undefined>;

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

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

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