Skip to content
This repository was archived by the owner on Oct 7, 2024. It is now read-only.
Open
Show file tree
Hide file tree
Changes from 5 commits
Commits
Show all changes
21 commits
Select commit Hold shift + click to select a range
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 CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,9 @@ and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0
### Changed
- **BREAKING:** Removed support for Node v12 in favor of v14 ([#137](https://github.com/MetaMask/eth-json-rpc-middleware/pull/137))

## [0.10.1]
Comment thread
originalix marked this conversation as resolved.
Outdated
- Support for differentiating hardware wallet vendors

## [0.10.0]
### Added
- Support for EIP-721 signTypedData_v4 ([#117](https://github.com/MetaMask/eth-trezor-keyring/pull/117))
Expand Down
50 changes: 49 additions & 1 deletion index.js
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,35 @@ const TREZOR_CONNECT_MANIFEST = {
appUrl: 'https://metamask.io',
};

const oneKeySpecialVersion = 99;
const oneKeyVendor = 'onekey.so';
/**
* Distinguish the OneKey hardware wallet by the serialNo prefix
* @param {*} features
* @returns {'mini' | 'touch' | 'classic' | 'trezor'}
*/
function isOneKeyDevice(features) {
Comment thread
originalix marked this conversation as resolved.
Outdated
if (
!features ||
typeof features !== 'object' ||
!features.minor_version ||
!features.patch_version
) {
return 'trezor';
}
const minorVersion = Number(features.minor_version);
const patchVersion = Number(features.patch_version);
const vendor = features.vendor || '';
if (
vendor === oneKeyVendor ||
(minorVersion === oneKeySpecialVersion &&
patchVersion === oneKeySpecialVersion)
) {
return 'onekey';
}
return 'trezor';
}

function wait(ms) {
return new Promise((resolve) => setTimeout(resolve, ms));
}
Expand Down Expand Up @@ -58,6 +87,7 @@ class TrezorKeyring extends EventEmitter {
this.perPage = 5;
this.unlockedAccount = 0;
this.paths = {};
this.vendor = undefined;
this.deserialize(opts);

TrezorConnect.on('DEVICE_EVENT', (event) => {
Expand All @@ -78,6 +108,15 @@ class TrezorKeyring extends EventEmitter {
return this.model;
}

/**
* Gets the vendor, if known.
*
* @returns {"trezor" | "onekey"}
Comment thread
originalix marked this conversation as resolved.
Outdated
*/
getVendor() {
return this.vendor;
}
Comment thread
originalix marked this conversation as resolved.
Outdated

dispose() {
// This removes the Trezor Connect iframe from the DOM
// This method is not well documented, but the code it calls can be seen
Expand Down Expand Up @@ -121,7 +160,16 @@ class TrezorKeyring extends EventEmitter {
if (response.success) {
this.hdk.publicKey = Buffer.from(response.payload.publicKey, 'hex');
this.hdk.chainCode = Buffer.from(response.payload.chainCode, 'hex');
resolve('just unlocked');
// Determine the vendor for statistics
TrezorConnect.getFeatures()
Comment thread
originalix marked this conversation as resolved.
Outdated
.then((features) => {
if (features.success) {
this.vendor = isOneKeyDevice(features.payload);
}
})
.finally(() => {
resolve('just unlocked');
});
} else {
reject(
new Error(
Expand Down