-
-
Notifications
You must be signed in to change notification settings - Fork 1.9k
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Metamask / Mist / Parity Support #59
Comments
This is certainly on the road map. I have a few ideas on this, and will likely have a larger impact on how There will be a more generalized idea of a This is very much planned, and there will soon be a web3-bridge package that will help expose the ethers.js interface over the web3 interface, to make it easier for existing apps to use. This will be what we use in Ethers Wallet to add full support for web3 apps, so it is soon-ish on the roadmap. The only functionality you are looking for from MetaMask is the signing, correct? I can put together a quick cookbook recipe on the documentation fro creating a MetaMaskSigner this week. All other functionality is exposed over the provider interface. |
Good to hear this is on the roadmap, thanks for the update. I don't mind taking a crack at it myself, but there are probably some intricacies that a guide would help with (and would in general be useful documentation for future extensions, hardware wallets?). I'll take at the codebase for the existing signers for now.
For my current requirements, yes, I would prefer to keep control over the read layer in the InfuraProvider. Still, I can imagine some potential projects where the user might want to select their own network (e.g. private network) via MM/Mist/Parity, so ideally ethers.js eventually supports both InjectedWeb3Signer and InjectedWeb3Provider. |
For other readers, I just noticed this: https://docs.ethers.io/ethers.js/html/cookbook.html#transactions-confirm-ui-with-a-custom-signer |
EDITED: Sorry, I was using the wrong library! facepalm |
This is my humble approach to signing / sending transactions via MetaMask (I tested it and it seems to work): https://gist.github.com/chmanie/cdf2697e42bf9adb1dc018731688b894 Some concerns:
|
The reasons to use BN are:
The custom signer should instead of overriding Sorry, I have 4 projects I'm trying to catch up on (ethers.js, the firefly hardware wallet, Ethers Wallet and ethers.io) but am hoping to get at least a cookbook entry this week on how to create a custom signer that bridges into Metamask. We certainly need to add more hours in a day... :) |
Here is a quick and dirty example I've thrown together. Probably needs some flushing out, and obviously not production ready, but give it a try: function MetamaskSigner(web3, provider) {
this.provider = provider;
this.getAddress = function() {
return web3.eth.accounts[0];
}
Object.defineProperty(this, 'address', {
get: function() {
return web3.eth.accounts[0];
}
});
this.sendTransaction = function(transaction) {
var tx = {
from: this.address
};
['to', 'data'].forEach(function(key) {
if (transaction[key] != null) {
tx[key] = transaction[key];
}
});
['gasLimit', 'gasPrice', 'nonce', 'value'].forEach(function(key) {
if (transaction[key] != null) {
tx[key] = ethers.utils.hexlify(transaction[key]);
}
});
return new Promise(function(resolve, reject) {
var payload = {
jsonrpc: "2.0",
method: 'eth_sendTransaction',
id: 1,
params: [ tx ]
};
web3.currentProvider.sendAsync(payload, function(error, hash) {
if (error) {
reject(error);
} else {
tx.hash = hash;
resolve(tx);
}
});
});
}
}
var provider = ethers.getDefaultProvider('homestead');
var wallet = new MetamaskSigner(web3, provider); |
Hello - was wondering is parity compatibility for signing/sending tx in your roadmap ? |
Heya! I think something very similar to the above solution will work with a local parity JSON-RPC will work. I will add a recipe for it as well. Or are you referring to their UI module? I haven’t used that before, but if you point me to some documentation I can figure that out too. The ethers-app API will soon transparently roll Metamask, ethers.io and local Ethereum noses up into one convenient API so that you no longer need to handle various signers in your code. Things will just work, no matter what backend the user uses. Once Ethers Wallet is updated to iOS 11 and for iPhone X (hopefully by the end of this week) I’ll be adding these to ethers-app. |
@ricmoo Thank you for your example! Sadly it doesn't work for me as it complains about a missing signer here: ethers.js/contracts/contract.js Line 120 in d49beb8
|
@chmanie Ack! You are right, I forgot to set a provider, which is how it determines the difference between a signer and provider. I have updated the above comment. Please try that one out. Update the provider to the network you are connecting to. I will update it once I have a Web3Provider, which will proxy all requests through the web3 wrapped provider. |
This should be good to go now. See 67bb0b7. Here is a modified version of the Metamask documentation for you to use ethers.js instead of using web3. window.addEventListener('load', function() {
// Checking if Web3 has been injected by the browser (Mist/MetaMask/EthersWallet)
if (typeof web3 !== 'undefined') {
// Use Mist/MetaMask's provider
provider = new ethers.providers.Web3Provider(web3.currentProvider);
signer = provider.getSigner();
} else {
console.log('No web3? You should consider trying MetaMask!')
// Allow read-only access to the blockchain if no Mist/Metamask/EthersWallet
provider = ethers.providers.getDefaultProvider();
}
// Now you can start your app & access the ethers provider and signer freely:
startApp()
}) |
As you mention in the readme, this library intends to include:
If this is the case, one very common use case is have support Metamask (and even Mist / Parity). Is this feature on the roadmap? If not, is it worth attempting to implement?
As far as I know, it seems like it shouldn't be too complicated, but obviously we don't want to include web3 as a dependency, so we should somehow interact with the injected
web3.currentProvider
directly; https://github.com/MetaMask/faq/blob/master/DEVELOPERS.mdThe text was updated successfully, but these errors were encountered: