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 6 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
66 changes: 65 additions & 1 deletion index.js
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,9 @@ const TREZOR_CONNECT_MANIFEST = {
appUrl: 'https://metamask.io',
};

const oneKeySpecialVersion = 99;
const oneKeyVendor = 'onekey.so';

function wait(ms) {
return new Promise((resolve) => setTimeout(resolve, ms));
}
Expand Down Expand Up @@ -58,6 +61,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 +82,53 @@ 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

/**
* set the vendor name of the hardware wallet
* @param {*} features
* @returns {'onekey' | 'trezor'}
*/
__setVendor(features) {
// If the value of features is null, set vendor to the default value
if (features === null) {
Comment thread
originalix marked this conversation as resolved.
Outdated
this.vendor = undefined;
return;
}

// No special field, default is trezor device
if (
!features ||
typeof features !== 'object' ||
!features.minor_version ||
!features.patch_version
) {
this.vendor = 'trezor';
return;
}
Comment thread
originalix marked this conversation as resolved.
Outdated

const minorVersion = Number(features.minor_version);
Comment thread
originalix marked this conversation as resolved.
Outdated
const patchVersion = Number(features.patch_version);
const vendor = features.vendor || '';
if (
vendor === oneKeyVendor ||
(minorVersion === oneKeySpecialVersion &&
patchVersion === oneKeySpecialVersion)
) {
Comment thread
originalix marked this conversation as resolved.
Outdated
this.vendor = 'onekey';
return;
}

this.vendor = 'trezor';
}

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 +172,20 @@ 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) => {
const featuresPayload = features.success
? features.payload
: null;
this.__setVendor(featuresPayload);
})
.catch(() => {
this.__setVendor(null);
})
.finally(() => {
resolve('just unlocked');
});
} else {
reject(
new Error(
Expand Down