36

Contracts do not possess private keys in the same way that your ether wallet does. Instead, they have programming code written in the language of the Ethereum Virtual Machine. To determine what a contract "does", all members of the Ethereum network simply run the relevant portion of code every time a transaction or other contract uses the contract's ...


35

You can if and only if a transaction has been sent from the account. When you send a tx, you sign the transaction and it includes these v r and s values. You parse these from the signed tx and then pass these v r and s values and the hash of the transaction back into a function and it'll spit out the public key. This is actually how you get the from address ...


22

ethereumjs-tx is a library with this example: npm install ethereumjs-tx var Tx = require('ethereumjs-tx') var privateKey = new Buffer('e331b6d69882b4cb4ea581d88e0b604039a3de5967688d3dcffdd2270c0fd109', 'hex') var rawTx = { nonce: '0x00', gasPrice: '0x09184e72a000', gasLimit: '0x2710', to: '0x0000000000000000000000000000000000000000', value: '...


19

I was having the same problem earlier, so I am going to give an extensive answer to how this works. I assume you are using geth as a client. There is an open issue where the geth client returns v in the wrong format, so let's keep in mind that if we get a v that is 0 or 1 we should add 27 to it. If you are running node and have connected web3 to your ...


19

v, r, s are the values for the transaction's signature. They can be used as in Get public key of any ethereum account A little more information, r and s are outputs of an ECDSA signature, and v is the recovery id. https://bitcoin.stackexchange.com/questions/38351/ecdsa-v-r-s-what-is-v also applies to Ethereum. For replay attack prevention, Ethereum ...


18

Currently, Ethereum uses elliptic curve cryptography, which is not quantum resistant. In the upcoming Serenity upgrade, however, accounts will be able to specify their own scheme for validating transactions, so individuals could choose to use Lamport signatures or other quantum proof algorithms. The Serenity blog post has a more in depth look at account ...


16

v, r, and s are parameters that can be parsed from the signature. Here's a good example from the ethereumjs utils library: var sig = secp256k1.sign(msgHash, privateKey) var ret = {} ret.r = sig.signature.slice(0, 32) ret.s = sig.signature.slice(32, 64) ret.v = sig.recovery + 27 Note how you can parse each value from a given signature. Even ...


15

The issue was that the chain id was not being set correctly. As a result transactions were signed with the incorrect chain id, so the sender could not be derived correctly. The fix was to also declare a config in the genesis block which specified it. An example: "config": { "chainID" : 10, "homesteadBlock": 0, "eip155Block": 0, "...


14

There's a functionality from the JSON RPC API not yet ported to web3 to sign data directly with one RPC function call, without messing with keys and crypto. If you don't need to sign messages clientside (because this requires an rpc connection and the account needs to be unlocked), then you can use eth_sign. eth_sign accepts two parameters, the address you ...


13

First, you'll need the private key for your account. To generate a brand new key, I use elliptic.js: import {ec as EC} from 'elliptic'; const ec = new EC('secp256k1'); const keypair = ec.genKeyPair(); If you have a key in your Ethereum node, you can use keythereum to import it. This will also give you an elliptic.js key. Once you have a key, you need a ...


13

Currently, Ethereum uses elliptic curve cryptography (ECDSA), the same as Bitcoin. So whatever "unsafe" concerns there are with how Bitcoin transactions are signed, would be the same with Ethereum currently. In Bitcoin and Ethereum, sending from an address will reveal the public key easily. Quantum computers compromise ECDSA and would make it easy to ...


13

Below is working code that calls "increment" on https://programtheblockchain.com/dapps/counter. (More info about that sample here: https://programtheblockchain.com/posts/2017/12/13/building-decentralized-apps-with-ethereum-and-javascript/.) It uses web3.js and ethereumjs-tx: <!-- from https://github.com/ethereumjs/browser-builds/raw/master/dist/...


12

web3 does not support this feature yet, but it might be coming with web3 1.0. In the meantime you can use ethereumjs-utils ecrecover feature. Note that this function expects v to be in {27, 28}, and since your signature comes from geth, (since it doesn't return signatures in the canonical format yet) you will have to add 27 to your v. Given a signature sgn ...


12

In Go creating a signed transaction would look something like this: transaction := types.NewTransaction(nonce, recipient, value, gasLimit, gasPrice, input) signature, _ := crypto.Sign(transaction.SigHash().Bytes(), key) signed, _ := tx.WithSignature(signature) where key is a plain *ecdsa.PrivateKey. If you want to use Ethereum encrypted accounts, you could ...


12

You can sign a string of data using web3.eth.sign The address you use needs to be unlocked and you can sign text like this: > web3.eth.sign(<your address>, web3.sha3("Some text")) 0x32f689696d855dd79c73acd94b2374461261b6f3d00e758fa23d35607c0be3175cbc7a7ea02c7f23cd7ef9334b4718a3363dfe12c1c1de24da5f94eb68a67b6000" The returned 130byte string is ...


12

Using Web3.js 1.0.0 encoded = contractInstance.methods.myMethod(params).encodeABI() var tx = { to : myContractAddress, data : encoded } web3.eth.accounts.signTransaction(tx, privateKey).then(signed => { web3.eth.sendSignedTransaction(signed.rawTransaction).on('receipt', console.log) });


9

Geth adds prefix to the message before siginig it in web3.eth.sign (see JSON-PRC spec). Without this it can be possible to trick user to sign transaction (more here). So, the correct code to sign message with web3.eth.sign and recover address with ethereumjs-util.ecrecover or (Solidity's ecrecover) should add the prefix explicitly. const util = require('...


9

There might be implementations of signing in Solidity, but you shouldn't do it. This is because in order to sign something, you will need a private key. Since every transaction is public on the blockchain, so will your private key that you use to sign something with. Therefore, it doesn't make much sense to sign something in Solidity because everyone will ...


8

Here's a working example I tested out using truffle: Example.sol pragma solidity ^0.4.0; contract Example { function testRecovery(bytes32 h, uint8 v, bytes32 r, bytes32 s) returns (address) { /* prefix might be needed for geth only * https://github.com/ethereum/go-ethereum/issues/3731 */ // bytes memory prefix = "\...


8

I found a way using the pyethereum lib. BTW, this lib has little maintenance and doesn't build in most OS's. Here's the sample code for that: from libs.pyethereum.ethereum import transactions from libs.pyethereum.ethereum import utils # Create the priv keys and addresses. Addresses are returned in bytearray key = utils.sha3("this is an insecure ...


8

You need to unlock your account to be able to operate with it. The third parameter of unlockAccount is a timeout in seconds. web3.personal.unlockAccount("0x..", "<passs>", 1000); But be aware that if you unlock your account, any other program with access to your node can sign whithin the timeout. See this to know how to secure your node and to ...


8

This is basically correct. In (4), any node can receive a transaction, check the signature and relay it to other nodes. The public key can be recovered from the signature, so it doesn't need to be passed around or looked up from anywhere. (This is different from Bitcoin, which wastes bandwidth by passing public keys around in spending transactions; Satoshi ...


8

1 Mainnet 2 Morden (deprecated) 3 Ropsten Testnet


8

ecrecover returns public key, you need to convert it to address with pubToAddress. pub = ethJsUtil.ecrecover(msg, v, r, s); addrBuf = ethJsUtil.pubToAddress(pub); addr = ethJsUtil.bufferToHex(addrBuf); Also, you can use fromRpcSig to get v, r, s sig = web3.eth.sign(myAccount,msg) res = ethJsUtil.fromRpcSig(sig) pub = ethJsUtil.ecrecover(msg, res.v, ...


7

You can use Web3. var Accounts = require('web3-eth-accounts'); // Passing in the eth or web3 package is necessary to allow retrieving chainId, gasPrice and nonce automatically // for accounts.signTransaction(). // var accounts = new Accounts('ws://localhost:8546'); var accounts = new Accounts(); // if nonce, chainId, gas and gasPrice is given it returns ...


7

I don't think this is possible, since you lose information when going from public key to address: Start with the public key (64 bytes) Take the Keccak-256 hash of the public key. You should now have a string that is 32 bytes. (note: SHA3-256 eventually became the standard, but Ethereum uses Keccak) Take the last 20 bytes of this public key (Keccak-...


7

N/A, in that you don't need to unlock an account at all. Yes, see below. The following uses Web3.js version 1.0.0-beta which is now the default installed by npm. Note that no node is attached. > var Web3 = require('web3'); > var web3 = new Web3(); > web3.version '1.0.0-beta.10' The second parameter in the below is the private key: > web3.eth....


6

Every transaction starts with an Externally Owned Account, and it's signed by somebody. As part of that transaction, a contract may call another contract, and some people call this an "internal transaction", but an "internal transaction" isn't really a transaction; It's part of the transaction started by an Externally Owned Account. It's more accurate to ...


6

The function signatures need to be unique only within a contract. The Solidity compiler will detect and prevent collisions if they are not unique in this case. If you have fewer than 2^16 functions in your contract, collisions are unlikely to happen by chance. There was a Reddit thread about this recently, but it seems a bit contrived to me and not a ...


6

You can start ganache with a mnemonic as a parameter ( -m 'jar boss sister abuse equal ....'). Doing this, you'll: be guaranteed to have the same addresses generated with each run be able to derive the private keys from that mnemonic const bip39 = require('bip39'); const hdkey = require('ethereumjs-wallet/hdkey'); const wallet = require('ethereumjs-wallet')...


Only top voted, non community-wiki answers of a minimum length are eligible