0

I encoded a value using the Oraclize CBOR library like so:

    Buffer.buffer memory cborArgs;
    Buffer.init(cborArgs, 64);
    cborArgs.startArray();
    cborArgs.encodeUInt(10);
    cborArgs.endSequence();

Which produces:

\\xef\\xbf\\xbd\\\\n

But it does not seem to match the result of the same encoding in python3.

print(str(cbor2.dumps([10]))

produces

\x81\n

What's going on?

1

You probably want to refer to the hex representation of each. When using the hex representation, you see they are almost identical, differing only in the fact that python's cbor2 defines a static array of length 3, whilst the cbor library, simply uses a dynamically-sized array for efficiency and adaptability on-chain. Both conform to the cbor standard.

Solidity

    Buffer.buffer memory cborArgs;
    Buffer.init(cborArgs, 64);
    cborArgs.startArray();
    cborArgs.encodeUInt(1);
    cborArgs.encodeUInt(222);
    cborArgs.encodeUInt(9000);
    cborArgs.endSequence();
    debug(cborArgs.buf);

debug[ "0x9f0118de192328ff" ]

Python

import cbor2 import binascii print(binascii.b2a_hex(cbor2.dumps([1, 222, 9000])))

b'830118de192328'

As you can see, only difference is in the first byte, specifying dynamic, rather than static 3 element array. This in turn required an additional FF byte for terminating it.

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.