0

I just need a simple way to sign a message offline, preferably using Python or JavaScript. I don't want to have to sign in using Web3. I just want my private key to be used from a variable in the code.

This is only for a proof of concept tool that I'm making, which is why I don't want to use Web3 or any nodes to do this. Is there such a library that will let me sign & verify a message the same way an Ethereum nodes would?

  • web3.py's w3.eth.account.sign() does what you're looking for. Unfortunately, it's only accessible by installing from source, at the moment. It is scheduled to be released as beta in the next week or so. – carver Nov 14 '17 at 17:52
0

This question is a possible duplicate. However, I think this code may be helpful for your POC i.e. signing and verifying messages without the use of Web3js.

Using Java, the ECKey.java class in EthereumJ gives you all the required functionality. Here's an example of it in practice...

Signing:

public void testEthereumSign() throws IOException {
    ECKey key = ECKey.fromPrivate(privateKey);
    System.out.println("Secret\t: " + Hex.toHexString(key.getPrivKeyBytes()));
    System.out.println("Pubkey\t: " + Hex.toHexString(key.getPubKey()));
    System.out.println("Data\t: " + exampleMessage);
    byte[] messageHash = HashUtil.sha3(exampleMessage.getBytes());
    ECDSASignature signature = key.sign(messageHash);
    String output = signature.toBase64();
    System.out.println("Signtr\t: " + output + " (Base64, length: " + output.length() + ")");
    assertEquals(sigBase64, output);
}

Verifying:

public void testVerifySignature1() {
    ECKey key = ECKey.fromPublicOnly(pubKey);
    BigInteger r = new BigInteger("28157690258821599598544026901946453245423343069728565040002908283498585537001");
    BigInteger s = new BigInteger("30212485197630673222315826773656074299979444367665131281281249560925428307087");
    ECDSASignature sig = ECDSASignature.fromComponents(r.toByteArray(), s.toByteArray(), (byte) 28);
    key.verify(HashUtil.sha3(exampleMessage.getBytes()), sig);
}

Python with pyethereum:

sign(key, network_id=None) - signs the transaction with the given key

sha3(data) - computes the SHA3 (or more precisely, keccak256) hash

ecrecover_to_pub(hash, v, r, s) - recovers the public key that made the signature as a 64-byte binary blob of encode_int32(x) + encode_int32(y). Hashing this and taking the last 20 bytes gives the address that signed a message.

ecsign(hash, key) - returns the v, r, s values of a signature

normalize_key(key) - converts a key from many formats into 32-byte binary

privtoaddr(key) - converts a key to an address

  • Thank you. Is there a js or python implementation of this readily available? – Raven Nov 11 '17 at 2:44
  • @Raven yes! Please see updated question for python. – Malone Nov 11 '17 at 17:10

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

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