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 8 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
62 changes: 62 additions & 0 deletions 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,64 @@ class TrezorKeyring extends EventEmitter {
return this.model;
}

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

/**
* fetch vendor by call getFeatures
* @param {object} features
* @returns {'onekey' | 'trezor'}
*/
fetchVendor() {
Comment thread
originalix marked this conversation as resolved.
Outdated
return new Promise((resolve) => {
TrezorConnect.getFeatures()
.then((response) => {
if (!response.success) {
resolve(null);
return;
}
const vendor = this.__getVendor(response.payload);
resolve(vendor);
})
.catch(() => {
resolve(null);
});
});
Comment thread
originalix marked this conversation as resolved.
Outdated
}

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

// No special field, default is trezor device
if (!features.minor_version || !features.patch_version) {
return 'trezor';
}

if (
features.vendor === oneKeyVendor ||
(features.minor_version === oneKeySpecialVersion &&
features.patch_version === oneKeySpecialVersion)
) {
return 'onekey';
}

return '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