MetaMask / snaps

Extend the functionality of MetaMask using Snaps
https://metamask.io/snaps/
Other
726 stars 557 forks source link

Explore APIs needed to be exposed for hardware wallets #498

Closed FrederikBolding closed 2 years ago

FrederikBolding commented 2 years ago

Likely to be WebUSB, WebHID etc.

HarishRocks commented 2 years ago

Even I am really looking forward to it.

FrederikBolding commented 2 years ago

WebHID and WebUSB can be exposed via endowments as long as the execution iframe has allow="hid 'src'".

However, the browser APIs will throw: Failed to execute 'requestDevice' on 'HID': Must be handling a user gesture to show a permission request., since the detection doesn't register a user action even when an actual user action triggers the snap.

FrederikBolding commented 2 years ago

Example code:

/* eslint-disable no-case-declarations */
const { DEFAULT_ETH, LedgerWallet } = require('@mycrypto/wallets');

module.exports.onRpcRequest = async ({ request }) => {
  switch (request.method) {
    case 'hello':
      const ledger = new LedgerWallet();
      const instance = await ledger.getWallet(DEFAULT_ETH, 0);
      return instance.signMessage(request.params[0]);
    default:
      throw new Error('Method not found.');
  }
};
rekmarks commented 2 years ago

We should investigate how the extension overcomes this in its existing hardware wallet integrations. Maybe @darkwing can shed some light.

rekmarks commented 2 years ago

@FrederikBolding please update this issue and document your findings.

FrederikBolding commented 2 years ago

To support hardware wallets as snaps we can expose WebHID and WebUSB via globals. However, we would need to initialize the hardware connection in the MetaMask extension by allowing the user to click a button, triggering a user gesture outside of a snap. After that is done the connection should be available to snaps.

This is the same way the extension is currently doing it. While we are still running snaps in iframes, this seems like the only option.