locazo
18 December 2021 22:08
1
Hi! I’ve been reading the format that’s expected for --script-data-file here and it seems that it should be possible to have a string, however, when I run:
cardano-cli transaction hash-script-data --script-data-file data.json
, where data.json
is:
{
"constructor": 1,
"fields": [
{
"bytes": "616263"
},
{
"int": 123
},
{
"string": "lol"
}
]
}
cardano-cli barks
Expected a single field named "int", "bytes", "string", "list" or "map".
Unexpected object field(s): {"string":"lol"}
Is there a way to get this metadata in?
as the error say u have both int and string under field… and only one accepted
locazo
18 December 2021 22:30
3
the error is the same even if data.json
is
{
"constructor": 1,
"fields": [
{
"bytes": "616263"
},
{
"string": "lol"
}
]
}
can u show me the documentation link?
Check this topic perhaps it will help u
I wanted to understand a bit better the hash-script-data functionality from cardano-cli. I checked the source code but had no luck in understanding how this hash is made.
From the examples, if we run:
cardano-cli transaction hash-script-data --script-data-value 42
We get the following hash:
9e1199a988ba72ffd6e9c269cadb3b53b5f360ff99f112d9b2ee30c4d74ad88b
However if we try to do a simple SHA256 hash of a 42 value we get:
73475cb40a568e8da8a045ced110137e159f890ac4da883b6b17dc651b3a8049
Also…
locazo
18 December 2021 22:34
5
check the above topic… perhaps it can help you
You seem to be right that there is a bug there.
As far as I can see, the relevant code is:
[ ("constructor", Aeson.Number (fromInteger n))
, ("fields", Aeson.Array (Vector.fromList (map conv vs)))
]
singleFieldObject name v = Aeson.object [(name, v)]
scriptDataFromJsonDetailedSchema :: Aeson.Value
-> Either ScriptDataJsonSchemaError
ScriptData
scriptDataFromJsonDetailedSchema = conv
where
conv :: Aeson.Value
-> Either ScriptDataJsonSchemaError ScriptData
conv (Aeson.Object m) =
case List.sort $ HashMap.toList m of
[("int", Aeson.Number d)] ->
case Scientific.floatingOrInteger d :: Either Double Integer of
Left n -> Left (ScriptDataJsonNumberNotInteger n)
Right n -> Right (ScriptDataNumber n)
And this code is missing the “string” case. Care to file a bug report?
1 Like
locazo
19 December 2021 00:39
9
Thanks! Yeah, I thought it was a bug… I found the workaround, and that’s just creating a list of the bytes that represent the string, i.e.:
import qualified Data.ByteString as BS
import Data.Text.Encoding as TextEncoding
import Data.Text
let toUTF8Bytes = BS.unpack . TextEncoding.encodeUtf8 . Data.Text.pack
toUTF8Bytes lol
which would return:
[108,111,108]
, and so you can make a list of bytes or ints, i.e.
{
"constructor": 1,
"fields": [
{
"int": 108
},
{
"int": 111
},
{
"int": 108
}
]
}
tomdx
19 December 2021 14:20
10
Instead of “string”, try a hex encoded bytes, like this …
{"constructor":0,"fields":[{"bytes":"4173746f72313736"},{"int":1647807616000}]}
You could even put them in a real list if you want to show that they belong together:
{
"constructor": 1,
"fields": [
{
"bytes": "616263"
},
{
"int": 123
},
{
"list": [
{
"int": 108
},
{
"int": 111
},
{
"int": 108
}
]
}
]
}