I'm trying to store the following string in a contract


Its size is 337 bytes.

The relevant contract code is

contract User {
    bytes32 publicKey;

    function setPublicKey(bytes32 _publicKey)
        publicKey = _publicKey;

    function getPublicKey() returns (bytes32 _publicKey) {
        return publicKey;


When I call getPublicKey() after setting it already, using (again only showing the relevant part)

contract.getPublicKey.call((err, res) => {
        if (err) {
        } else {
            const k = web3.toAscii(res);

I just get back 50304803898475040496394770699619, which is the beginning of the string/key. I figure this to be related to exceeding the bytes32 size, but what's the recommended way to get around this?

Also, I should mention I don't want to store this offchain. And also, I'm aware that gas costs to store large data in the blockchain are high but (1) I figure this would still be within an acceptable size and (2) it's a private chain for testing so I am not too concerned with that, for now.

  • why not to just use bytes or string instead bytes32
    – Badr Bellaj
    Aug 11, 2017 at 10:44
  • I tried using string but it didn't seem to work either. I'll try bytes though.
    – mcansado
    Aug 11, 2017 at 10:57

1 Answer 1


My suggestion would be to use either string or bytes and just to mention there is no limit on the number of characters to store. If you are storing a lot of characters then you may consider increasing gas value. Once you increase gas value then string or bytes both should work.

If you would want to estimate Gas of a transaction then you can take a look at this web3js API -


  • Thanks for the reply. I've tried using string but when I run getPublicKey() I simply get an empty answer back which is why I switched to bytes.
    – mcansado
    Aug 11, 2017 at 12:58
  • I actually asked a follow-up question here since it seems to be a different issue.
    – mcansado
    Aug 11, 2017 at 13:12

Your Answer

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

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