Skip to content
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

Wallet Extension doesn't support external private key wallets. #611

Open
jamescarlyle opened this issue Aug 26, 2022 · 2 comments
Open

Wallet Extension doesn't support external private key wallets. #611

jamescarlyle opened this issue Aug 26, 2022 · 2 comments
Assignees

Comments

@jamescarlyle
Copy link
Contributor

jamescarlyle commented Aug 26, 2022

I'd appreciate help. I'm using ethers.js, creating a signer using a specified private key (which belongs to an account used to deploy an ERC20 contract), registering that address with the Wallet Extension, and then sending a transfer() transaction via the WE. I'm expecting the transfer() to succeed, since the ERC20 owner has a substantial balance.

Here's a minimal set of code to reproduce the issue:

import ethers from 'ethers';
import fetch from 'node-fetch';

const USER_ADDRESS = '0x70997970C51812dc3A010C7d01b50e0d17dc79C8';
const OBSCURO_HTTP_ENDPOINT = 'http://127.0.0.1:3000';
const PATH_GENERATE_VK = '/generateviewingkey/';
const PATH_SUBMIT_VK = '/submitviewingkey/';
const JSON_HEADERS = { 'Accept': 'application/json', 'Content-Type': 'application/json' };
const ERC20_ADDRESS = '0x9802F661d17c65527D7ABB59DAAD5439cb125a67';
const ERC20_PRIVATE_KEY = '0xac0974bec39a17e36ba4a6b4d238ff944bacb478cbed5efcae784d7bf4f2ff80';
const rpcProvider = new ethers.providers.JsonRpcProvider(OBSCURO_HTTP_ENDPOINT);
const erc20Issuer = new ethers.Wallet(ERC20_PRIVATE_KEY, rpcProvider);
const ERC20_ISSUER_ADDRESS = erc20Issuer.address;
const ERC20_ABI = [
    {
        "inputs": [
            { "internalType": "address", "name": "receiver", "type": "address" },
            { "internalType": "uint256", "name": "numTokens", "type": "uint256" }
        ],
        "name": "transfer",
        "outputs": [
            { "internalType": "bool", "name": "", "type": "bool" }
        ],
        "stateMutability": "nonpayable",
        "type": "function"
    }
];
const erc20 = new ethers.Contract(ERC20_ADDRESS, ERC20_ABI, rpcProvider);

(async () => {
    const generateViewingKeyResp = await fetch(
        OBSCURO_HTTP_ENDPOINT + PATH_GENERATE_VK, {
        method: 'POST',
        headers: JSON_HEADERS,
        body: JSON.stringify({ 'address': ERC20_ISSUER_ADDRESS })
    }
    );
    if (!generateViewingKeyResp.ok) {
        alert('Failed to generate viewing key for Issuer.' + generateViewingKeyResp);
        return;
    };
    const viewingKey = await generateViewingKeyResp.text();
    console.log(viewingKey);

    const signature = await erc20Issuer.signMessage(JSON.stringify(['vk' + viewingKey, erc20Issuer.address]));
    const signedViewingKeyJson = { "signature": signature, "address": erc20Issuer.address }
    const submitViewingKeyResp = await fetch(
        OBSCURO_HTTP_ENDPOINT + PATH_SUBMIT_VK, {
        method: 'POST',
        headers: JSON_HEADERS,
        body: JSON.stringify(signedViewingKeyJson)
    }
    );
    if (!submitViewingKeyResp.ok) {
        console.log('Failed to submit viewing key for Issuer.' + submitViewingKeyResp.statusText);
        return;
    };

    const tx = await erc20.populateTransaction.transfer(
        USER_ADDRESS,
        ethers.utils.parseUnits('1.0', 18),
        { gasLimit: 400000 }
    );
    const sentTx = await erc20Issuer.sendTransaction(tx);
    const receipt = await sentTx.wait();
    alert('Request accepted. Your receipt is: ' + JSON.stringify(receipt));


})().catch(err => {
    console.error(err);
});```
@jamescarlyle
Copy link
Contributor Author

When I execute this with node test.mjs, I get the following output:

02b5df0525d456d82caae5a2988060e7069b5a6c328a57090b17a684b93ed0adc3
Error: bad response (status=500, headers={"access-control-allow-origin":"*","content-type":"text/plain; charset=utf-8","x-content-type-options":"nosniff","date":"Fri, 26 Aug 2022 19:01:26 GMT","content-length":"148","connection":"close"}, body="rpc request failed: failed to decrypt response for eth_sendRawTransaction call - failed to decrypt result with viewing key - ecies: invalid message\n", requestBody="{\"method\":\"eth_sendRawTransaction\",\"params\":[\"0x02f8b2820309068459682f008459682f0083061a80949802f661d17c65527d7abb59daad5439cb125a6780b844a9059cbb00000000000000000000000070997970c51812dc3a010c7d01b50e0d17dc79c80000000000000000000000000000000000000000000000000de0b6b3a7640000c001a0be4e669fe019db4e7f483b6caa7b38d75b619ae3084f3cd5cd0971649193cb75a032b235dd575067cfab230dd863f597cc290bdf5bed7b04a5038ae7de2b35ee0a\"],\"id\":51,\"jsonrpc\":\"2.0\"}", requestMethod="POST", url="http://127.0.0.1:3000", code=SERVER_ERROR, version=web/5.7.0)
    at Logger.makeError (/home/jwgcarlyle/js/ethers-different-signer/node_modules/@ethersproject/logger/lib/index.js:238:21)
    at Logger.throwError (/home/jwgcarlyle/js/ethers-different-signer/node_modules/@ethersproject/logger/lib/index.js:247:20)
    at /home/jwgcarlyle/js/ethers-different-signer/node_modules/@ethersproject/web/lib/index.js:270:36
    at step (/home/jwgcarlyle/js/ethers-different-signer/node_modules/@ethersproject/web/lib/index.js:33:23)
    at Object.next (/home/jwgcarlyle/js/ethers-different-signer/node_modules/@ethersproject/web/lib/index.js:14:53)
    at fulfilled (/home/jwgcarlyle/js/ethers-different-signer/node_modules/@ethersproject/web/lib/index.js:5:58)
    at processTicksAndRejections (node:internal/process/task_queues:96:5) {
  reason: 'bad response',
  code: 'SERVER_ERROR',
  status: 500,
  headers: {
    'access-control-allow-origin': '*',
    'content-type': 'text/plain; charset=utf-8',
    'x-content-type-options': 'nosniff',
    date: 'Fri, 26 Aug 2022 19:01:26 GMT',
    'content-length': '148',
    connection: 'close'
  },
  body: 'rpc request failed: failed to decrypt response for eth_sendRawTransaction call - failed to decrypt result with viewing key - ecies: invalid message\n',
  requestBody: '{"method":"eth_sendRawTransaction","params":["0x02f8b2820309068459682f008459682f0083061a80949802f661d17c65527d7abb59daad5439cb125a6780b844a9059cbb00000000000000000000000070997970c51812dc3a010c7d01b50e0d17dc79c80000000000000000000000000000000000000000000000000de0b6b3a7640000c001a0be4e669fe019db4e7f483b6caa7b38d75b619ae3084f3cd5cd0971649193cb75a032b235dd575067cfab230dd863f597cc290bdf5bed7b04a5038ae7de2b35ee0a"],"id":51,"jsonrpc":"2.0"}',
  requestMethod: 'POST',
  url: 'http://127.0.0.1:3000',
  transaction: {
    type: 2,
    chainId: 777,
    nonce: 6,
    maxPriorityFeePerGas: BigNumber { _hex: '0x59682f00', _isBigNumber: true },
    maxFeePerGas: BigNumber { _hex: '0x59682f00', _isBigNumber: true },
    gasPrice: null,
    gasLimit: BigNumber { _hex: '0x061a80', _isBigNumber: true },
    to: '0x9802F661d17c65527D7ABB59DAAD5439cb125a67',
    value: BigNumber { _hex: '0x00', _isBigNumber: true },
    data: '0xa9059cbb00000000000000000000000070997970c51812dc3a010c7d01b50e0d17dc79c80000000000000000000000000000000000000000000000000de0b6b3a7640000',
    accessList: [],
    hash: '0x99964081430bdb4fa1b7806e50ead7a3069f00b4fb86554abb1f654549a2128c',
    v: 1,
    r: '0xbe4e669fe019db4e7f483b6caa7b38d75b619ae3084f3cd5cd0971649193cb75',
    s: '0x32b235dd575067cfab230dd863f597cc290bdf5bed7b04a5038ae7de2b35ee0a',
    from: '0xf39Fd6e51aad88F6F4ce6aB8827279cffFb92266',
    confirmations: 0
  },
  transactionHash: '0x99964081430bdb4fa1b7806e50ead7a3069f00b4fb86554abb1f654549a2128c'
}```

@jamescarlyle
Copy link
Contributor Author

This is run from a directory with npm init, npm install ethers and npm install node-fetch, then node test.mjs

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants