Skip to content
This repository has been archived by the owner on Sep 13, 2024. It is now read-only.

bytes32 as parameters #184

Closed
GabCas opened this issue Jul 6, 2018 · 2 comments
Closed

bytes32 as parameters #184

GabCas opened this issue Jul 6, 2018 · 2 comments

Comments

@GabCas
Copy link

GabCas commented Jul 6, 2018

Hi,

I have a method in my contract that expects a bytes32 parameter. I've seen how this is done in Issue #57 and Issue #63 but haven't been able to make it work.
I have a sha string that I turn to bytes32 like this:
let sha:Data = self.sha!.data(using: .utf8)![0..<32]

self.sha is a string like this: "4621c5cb24cca81ab0329089464ad54cd10e071bd21e86a237666228c6d2c04f"

I also have the bytes32 in a string representation from the server that looks like this: "b\'F!\\xc5\\xcb$\\xcc\\xa8\\x1a\\xb02\\x90\\x89FJ\\xd5L\\xd1\\x0e\\x07\\x1b\\xd2\\x1e\\x86\\xa27fb(\\xc6\\xd2\\xc0O\'"

However, the constructed Data (for bytes32) in swift comes out looking like this:
[52, 54, 50, 49, 99, 53, 99, 98, 50, 52, 99, 99, 97, 56, 49, 97, 98, 48, 51, 50, 57, 48, 56, 57, 52, 54, 52, 97, 100, 53, 52, 99]

As you can see the bytes32 created in swift is fairly different from the one my server provides in string representation.

Finally, I add it as the parameters for my function call like so:
let parameters = [sha] as [AnyObject]

Is there something I'm missing? The transactionIntermediate is getting created correctly so at least the parameter is good. I'm always getting 'revert' from my contract...

Any help is greatly appreciated.

Thanks,

@shamatar
Copy link
Contributor

shamatar commented Jul 9, 2018

Hello @GabCas

A library takes care of parameter serialization for you. If you want to pass a hex string as "bytes32" parameter then you just need to prepend it with "0x" prefix, so passing

let hexData = "0x" + self.sha!
let parameters = [hexData] as [AnyObject]

should be enough for you. Just in case if you want to pass utf8 encoding of arbitrary string as bytes32 - just leave is without prefix, conversion will be handled automatically too.

Sincerely, Alex

@GabCas
Copy link
Author

GabCas commented Jul 9, 2018

Thanks @shamatar ! That's exactly what I needed to do.

@GabCas GabCas closed this as completed Jul 9, 2018
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants