Hot answers tagged

17

ethereumjs-wallet can be used to get public key from private key: > const hdkey = require('ethereumjs-wallet/hdkey') > const privateKey = hdkey.fromMasterSeed('random')._hdkey._privateKey > const Wallet = require('ethereumjs-wallet') > const wallet = Wallet.fromPrivateKey(privateKey) > wallet.getPublicKeyString() '...


5

It's nice to see those questions, this demonstrates that people are interested in really learning how all this world works! I asked a few months ago to one of my college professors for the same thing and he gave me this two titles: Applied cryptography. Alfred J. Menezes. It's a really nice title, the chapters are free, so download them one by one. The ...


4

This library is a good starting point: https://github.com/HarryR/solcrypto As Richard points out symmetric key crypto and private key operation are rarely sensible in a smart contract as the smart contract has no privacy. However public key operations like checking signatures, proofs and hashes can be very useful and got more efficient with the addition of ...


4

Version 4.x of web3.py requires all addresses use a proper EIP 55 checksum. In my testing, I receive the same error as you until I convert the address to the correct checksummed form (note the capitalization): 0xe980E77404ae62aB0F2d6b8510BD951e25185414 (Note that you also have an extra comma that makes your code syntactically invalid, but I assume that's ...


3

I think I had similar problem. I couldn't get correct address from ecrecover after message was signed with personal.sign, but it works fine after it was sign with eth.sign. Then I found this on web3 docs: https://web3js.readthedocs.io/en/1.0/web3-eth-personal.html#sign "Signs data using a specific account. This data is before UTF-8 HEX decoded and ...


3

ECDSA works with numbers so to sign a message first you have to encode it as an number. The precise meaning of RLP is defined in the Yellow Paper (Appendix B Recursive Length Prefix). It is a function that will encode something structured (a transaction for example) as a sequence of bytes. Given a transaction Transaction then RLP(Transaction) returns a ...


3

I never found any proper documentation about the Recovery ID but I did talk with somebody on Reddit and they gave me my answer: id = y1 & 1; // Where (x1,y1) = k x G; if (s > curve.n / 2) id = id ^ 1; // Invert id if s of signature is over half the n I had to modify the mbedtls library to pass back the Recovery ID but when I did I could generate ...


3

You need to hash the bytes of the public key (not the public key string). In Javascript https://runkit.com/embed/2w4f7dvkz2lg keccak256 = require('js-sha3').keccak256; keccak256(new Buffer('25b867253fe38ac7ed594f54b4f55fa18c49ced332fc352991e48a77ab7816c759d46cab51a59d9833cd6d958cc752ad95f10bbe469b364db2de5d3535417966', 'hex')).slice(24) "...


3

If I generate a private key on mainnet now, would I be able to use it at the same address on Ethereum Classic? What about testnets? Yes, this is exactly how it works - Ethereum, ETC, and all the test nets (Ropsten, Kovan, Rinkeby) generate the address from the private key in the same way. Also other Ethereum-based blockchains, such as Expanse or Ubiq. ...


3

Unfortunately, the precompile that allows for ECDSA signature verification only works for the secp256k1 curve. There is no practical way to do EC operations on other curves, like prime256v1, except to build the operations yourself, which would likely be quite expensive.


3

I don't know if this will help in any way at all but I had something I wrote just before abigen was introduced. I have tried to keep it updated (no 100% guarantee) github.com/DaveAppleton/ether_go Maybe it will help


3

Since you mention it's an ECDSA key, I assume you're talking about using the same crypto that Ethereum uses for signatures. If you want to do this in Solidity, the simplest and most efficient thing will still be to use ecrecover. As you say ecrecover returns an address, not a public key. But an Ethereum address is derived from the public key, so if you want ...


2

This is an imporant question, since all of the other world (mobile, smartcards, HSM's etc) uses secp256r1. There's an article at http://blog.enuma.io/update/2016/11/01/a-tale-of-two-curves-hardware-signing-for-ethereum.html but unfortunately the codes actually implementing the functionality have disappeared. Planning to reverse engineer this from the ...


2

Remember that anything done inside a smart contract has to be paid for with gas: computational expense equates to gas expense. Even if high gas costs aren't a deterrent, there's still a per-block gas limit that would need to be considered. (I don't know much about implementing crypto alogorithms, but I checked the OpenSSL C implementation of ECDSA, and it's ...


2

The rawhash you used is not right! Read this: How to generate the rawhash in python. Since you have the package ethereum in you code, the simplest way to get public key is: #!/usr/bin/env python import rlp import ethereum import ethereum.transactions class MyTransaction(ethereum.transactions.Transaction): _publickey = None @property def ...


2

Looks like the docs should be updated. publicKey is a non-standard key returned by parity only, I believe.


2

Use 0xfdba67e41f7c6767100969ae3f045d10a59e35d380acf5b37a3b208bd2969347 instead (with a 0x prefix) so web3 knows the format of the private key: web3.eth.accounts.privateKeyToAccount('0xfdba67e41f7c6767100969ae3f045d10a59e35d380acf5b37a3b208bd2969347').address "0xa6CDA44CEA3Ac87435d9fDF548B051dDE90D128F"


2

If you are using web3, you could easily sign any transactions or just messages: https://web3js.readthedocs.io/en/1.0/web3-eth-accounts.html#sign If you don't want to use web3 (for example because you dont want to initialize with an ethereum endpoint), you can find some alternatives: https://docs.ethers.io/ethers.js/html/api-wallet.html https://github.com/...


2

Any package that supports eliptic curve will do as log as you have: s,v,r, and the message. The recovery will return the publickey and the address is the first 20 bytes of the hash of the public key. See this answer for more details on how the recovery is done and how the values s,r, and v are used. (https://bitcoin.stackexchange.com/questions/38351/ecdsa-v-...


2

I'm also not a Rust programmer, so someone will probably have a better answer, but have a look in Parity's keypair.rs, which itself uses rust-secp256k1. Of interest is probably the KeyPair implementation. impl KeyPair { /// Create a pair from secret key pub fn from_secret(secret: Secret) -> Result<KeyPair, Error> { let context = &...


2

if I finish following the tutorial and build a signed TXN with these wrong R+S values, the txn works This seems to suggest the signing by secp256k1.ecdsaSign is correct, but just not deterministic, i.e. it changes every time you sign, even with the same key and data. Notice the tutorial uses private_key.sign_deterministic, so, to produce the same result, ...


1

Formulating an answer of some sorts based on the comments. So in general hashes can't be reversed. Once you calculate hash(A) = B there is no way to calculate A if you know B. However if A is something short enough (for example a phonenumber) or something guessable (for example your first name) one can simply try all combinations of A and apply the hash ...


1

I solved this problem. Each byte in secret key(sk) must be a number. void skstr_to_sk(const unsigned char *sk_str, unsigned char *sk) { const unsigned char *sk_pos = sk_str; int i; for (i = 0; i < PRIVATE_KEY_SIZE; i++) { sscanf(sk_pos, "%2hhx", &sk[i]); sk_pos += 2; } } int main(void) { ... unsigned char *sk_str = "...


1

I did some research regarding GCP KMS, unfortunately it does not support ECDSA secp256k1 which used in Ethereum, so it is not possible to use it in the case. You may find more details here https://stackoverflow.com/questions/58053715/how-to-set-up-a-keyvault-using-secp256k1-algorithm-in-gcp


1

Ethereum has had this requirement since the homestead hard fork. This requirement was introduced as part of EIP-2 (see point 2 of https://github.com/ethereum/EIPs/blob/master/EIPS/eip-2.md#specification) Rather than me repeat what others have already answered, here is an answer which explains concisely why the low s requirement is needed: https://bitcoin....


1

Geth uses https://github.com/bitcoin-core/secp256k1. And that implementation will apply option 2 https://github.com/bitcoin-core/secp256k1/blob/544435fc90a5672d862e2a51f44c10251893b97d/src/ecdsa_impl.h#L310-L315 if (secp256k1_scalar_is_high(sigs)) { secp256k1_scalar_negate(sigs, sigs); if (recid) { *recid ^= 1; } }


1

This function verifies a signature. When the signature is generated, the standard practice, as defined in this EIP, is to add this (standard) prefix"\x19Ethereum Signed Message:\n" + len(message) (notice in this case, the length is 32). As a result, to verify the signature, the same prefix needs to be appended to the original message (msgHash), in order to ...


1

After much head-banging (against the wall) and isolating the issue to the hashing of the resultant public key, (nicely summarized in my other question, here: Why doesn't my private key's public key generate the correct public address?), it turns out one must pass the bytes, not the hex-hash itself, of the public key to the keccak hashing algorithm. $...


1

If you are still interested in working with SECP256R1 signatures for Ethereum, check out https://github.com/tdrerup/elliptic-curve-solidity; The repo contains a pure Solidity implementation of curve SECP256R1.


1

Here's a full example of how to sign a transaction and broadcast in Go (from the Ethereum Development with Go book). package main import ( "context" "crypto/ecdsa" "fmt" "log" "math/big" "github.com/ethereum/go-ethereum/common" "github.com/ethereum/go-ethereum/core/types" "github.com/ethereum/go-ethereum/crypto" "github....


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