16

Is it possible to use the web3.js library in a client's browser to let them generate an address and sign a transaction without them connecting to an ethereum node?

5 Answers 5

11

Not in web3.js. EDIT: As answered by @Andromelus, web3js 1.0 can do it with the following function: web3.eth.accounts.signTransaction(tx, privateKey [, callback]);

There are other libraries such as, https://github.com/ethereumjs/ethereumjs-tx and https://github.com/SilentCicero/ethereumjs-accounts, that can be used. See Create and sign OFFLINE raw transactions?

3
  • 1
    Looks like the data field is sent as Hex. How do I convert my function call and parameter to put it in? For example, if I want call: setName("roger") on the contract.
    – Max
    Jun 22, 2016 at 16:17
  • I'm not seeing the connection between the questions. Please elaborate/clarify and ask a separate question.
    – eth
    Jun 23, 2016 at 9:57
  • It is possible to sign transaction with web3js though. Jan 15, 2018 at 3:28
2

It is possible to sign transaction with web3js, check the 1.0.0 version

0

I would also check out https://github.com/ethjs/ethjs-provider-signer

It has just what you are looking for.

0

I was looking for an answer to the above, figured out the following. The eth module can be used by first instantiating web3 with the default constructor and then calling the sign function.

var web3 = require('web3');
var myWeb3 = new web3();
var signedTx = myWeb3.eth.signTransaction(tx, privateKey [, callback]);
0

You could use this script and adjust it to run in a browser (it's a command line tool for Node).

I'm copying it here in case it becomes unavailable later on:

#!/usr/bin/env node
'use strict';

// ethereum: generate signed offline transactions

const fs        = require('fs-extra');
const rls       = require('readline-sync');
const Accounts  = require('web3-eth-accounts');
const web3utils = require('web3-utils');
const accounts  = new Accounts();

async function main() {
    if (process.argv.length < 6) {
        console.log("usage:\n" +
            "\tsign-offline.js filename recipient amount nonce gasprice\n\n" +
            "\t\t amount is given in ether, gas price is given in gwei");
        process.exit(1);
    }

    let filename  = process.argv[2];
    let recipient = process.argv[3];
    let amount    = web3utils.toWei(process.argv[4], "ether");
    let nonce     = process.argv[5];
    let gasprice  = web3utils.toWei(process.argv[6], "gwei");

    console.log("file %s to %s amount %f eth nonce %d price %f",
        filename, recipient, amount, nonce, gasprice);

    let keystore = await fs.readJson(filename);

    if (!("crypto" in keystore)) {
        // MEW creates capitalized "crypto" property
        // web3 expects it in lowercase
        keystore.crypto = keystore.Crypto;
        delete keystore.Crypto;
    }

    let password  = rls.question('keystore Password? ', { hideEchoBack: true });
    let myaccount = accounts.decrypt(keystore, password);
    let data      = accounts.signTransaction({
        to: recipient,
        value: amount,
        gas: 21000,
        gasPrice: gasprice,
        nonce: nonce,
        chainId: 1 // mainNet
    }, myaccount.privateKey);
    console.log("signed raw transaction: ", data.rawTransaction);
}

main();

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge that you have read and understand our privacy policy and code of conduct.

Not the answer you're looking for? Browse other questions tagged or ask your own question.