I'm writing a smart contract where the user send a hashed string : the answer of an asked question (because I don"t want it to be public in the blockchain).
It is compared to the correct hashed answer:
function answerQuestion(bytes32 _answer) notAnswered returns (string){
if(_answer == keccak256(answer)){
isAnswered = true;
winner = msg.sender;
return pos;
}
return "WRONG";
}
To check if it works I hash the correct answer in an online tool (https://emn178.github.io/online-tools/keccak_256.html)
It gives me something like 57315cf71be5ffcaf957b9cc196b322e1c4d5a1832396abcee71d05d8caf41a6
and I parse it as the parameter in the browser solidity. But it returns:
Error encoding arguments: SyntaxError: Unexpected token c in JSON at position 6
Any idea how should I fix this?
I am developing a smart contract using the Remix IDE and I ran into the same issue. I solved this issue by appending 0x to the beginning of the hash.
0x57315cf71be5ffcaf957b9cc196b322e1c4d5a1832396abcee71d05d8caf41a6
Related
I've recently been migrating my Flutter/Dart apps to be null safety compatible/sound. To this end I came across a situation I can't really figure out a 'best practice' for.
I have a few functions similar to the following:
String defaultErrorMessage({String? message = 'Please try again later.'}) {
return "Error. $message";
}
Fairly straightforward - just meant to standardize error messages and provide a default one for situations where there may not be an error message.
The question is this:
The parameter 'message' is not required and has a default value. If I pass it a null string it will simply print "Error. null" - this is shown in this dartpad
Is there a simple way or special syntax I should be using for default parameters? I recognize there are simple ways to rewrite this I feel like I shouldn't have to - isn't the point of all these null features/checks to automatically do things like this? Specifically I could do the following:
String defaultErrorMessage({String? message}) {
message ??= 'Please try again later.';
return "Error. $message";
}
But I'd much prefer to handle that in the function definition itself.
Thank you in advance for the help!
So basically, you want to make it so that if you pass a null value, it uses the default message you have set? I am sorry to say that the code you already posted is probably the best to do that.
But if you want null values to be the same as passing no value, why even make the variable nullable at all? Would it not make more sense to do something like this?
String defaultErrorMessage({String message = 'Please try again later'}) {
return "Error. $message";
}
Which will of course give you some compilation errors if someone did something like this:
defaultErrorMessage(message: null);
so you will have to go to every time this happened and change it to
defaultErrorMessage();
But well, that's kinda how null safety migration has always gone for me
Im working on a script to hash a "fingerprint" for communicating with the secure Pay Direct Post API.
The issue I have is im trying to create a SHA-1 String that matches the sample code provided so that i can ensure things get posted accurately.
the example Sha-1 string appears encoded like
01a1edbb159aa01b99740508d79620251c2f871d
However my string when converted appears as
7871D5C9A366339DA848FC64CB32F6A9AD8FCADD
completely different...
my code for this is as follows..
<cfset variables.finger_print = "ABC0010|txnpassword|0|Test Reference|1.00|20110616221931">
<cfset variables.finger_print = hash(variables.finger_print,'SHA-1')>
<cfoutput>
#variables.finger_print#
</cfoutput>
Im using Coldfusion 8 to do this
it generates a 40 character hash, but i can see its generating completely different strings.
Hopefully someone out there has done this before and can point me in the right direction...
thanks in advance
** EDIT
The article for creating the Hash only contains the following information.
Example: Setting the fingerprint Fields joined with a | separator:
ABC0010|txnpassword|0|Test Reference|1.00|20110616221931
SHA1 the above string: 01a1edbb159aa01b99740508d79620251c2f871d
When generating the above example string using coldfusion hash it turns it into this
7871D5C9A366339DA848FC64CB32F6A9AD8FCADD
01a1edbb159aa01b99740508d79620251c2f871d
Sorry, but I do not see how the sample string could possibly produce that result given that php, CF and java all say otherwise. I suspect an error in the documentation. The one thing that stands out is the use of "txnpassword" instead of a sample value, like with the other fields. Perhaps they used a different value to produce the string and forgot to plug it into the actual example?
Update:
Example 5.2.1.12, on page 27, makes more sense. Ignoring case, the results from ColdFusion match exactly. I noticed the description also mentions something about a summarycode value, which is absent from the example in section 3.3.6. So that tends to support the theory of documentation error with the earlier example.
Code:
<cfset input = "ABC0010|mytxnpasswd|MyReference|1000|201105231545|1">
<cfoutput>#hash(input, "sha-1")#</cfoutput>
Result:
3F97240C9607E86F87C405AF340608828D331E10
I am learning ADO.NET and now I am trying to understand the SqlDataReader. I am trying to learning by using this tutorial and I am facing some difficulties now in understanding the following part of the code mentioned HERE:
while (rdr.Read())
{
// get the results of each column
string contact = (string)rdr["ContactName"];
string company = (string)rdr["CompanyName"];
string city = (string)rdr["City"];
// print out the results
Console.Write("{0,-25}", contact);
Console.Write("{0,-20}", city);
Console.Write("{0,-25}", company);
Console.WriteLine();
}
I want to understand the meaning of "{0, -25}"
This means that the WriteLine method schould print the value of the first parameter, in your case contact, to a width of 25 characters. The minus in front of the 25 indicates a left justified output.
That is a format specifier for .NET Console.Write().
See documentation explaining here:
http://msdn.microsoft.com/en-us/library/9xdyw6yk.aspx
IN SqlDataReader, it reads record from database based on query.
sqlDataReader read record at a time single row. it means rdr["ContactName"] is one value and it read and move to string contact and so on every fields.
It fetch all record in while loop.
And Console.Write("{0,-25}", contact) is used to format output.
When I write validation code for a web form, I usually assume that the content of a field is valid and attempt to prove that it is invalid. Is it a better practice to assume that the content of the field is invalid and then attempt to prove that it is valid?
A very simple example (pseudo code):
function isValid( formFieldValue, minLength, maxLength ) {
valid = true;
fieldLength = length( formFieldValue );
if( fieldLength < minLength ) {
valid = false;
}
if( fieldLength > maxLength ) {
valid = false;
}
return valid;
}
Would it be better to assume that the field in question is invalid and modify my checks accordingly?
Please note - I'm not talking about XSS protection or input filtering. We can never assume that user input is safe. I am talking about validating things like minimum/maximum length or a valid e-mail address in a form field.
I think when you just talk about things like length etc. it makes no big difference. But I would ever assume that the input is invalid and prove that it's not, because I do the same with probably XSS input.
I think that better idea is to assume wrong input and to prove validity. It's easier.
For javascript allready exists number of libraries that solves your problem.
e.g.
Backbone.Forms https://github.com/powmedia/backbone-forms
jQuery validatin plugin http://bassistance.de/jquery-plugins/jquery-plugin-validation/
The point is whether your all conditions run or not.
Case1: Assume that a form is valid and then check for its invalidity by checking for example 2 conditions.
Case2. Assume that a form in invalid and then check 2 conditions whether it is invalid?
In both cases you will have to check for all conditions to satisfy because you want to validate all your fields. So whether you assume it is valid or invalid at start doesn't matter we mostly check for invalidity.
Update: I tried clearing the created cookie in the browser and trying it again, and it didn't happen. Conceivably I set a cookie with the value "null" at some point.
(Ok, this is probably a retorical question, so I'm making it CW)
The documentation for Google Web Toolkit says this about Cookies.getCookie:
public static java.lang.String getCookie(java.lang.String name)
Gets the cookie associated with the given name.
Parameters:
name - the name of the cookie to be retrieved
Returns:
the cookie's value, or null if the cookie doesn't exist
Well, I've just spent a number of hours beating my head against a wall because at least in the hosted mode browser (I haven't tested with a real browser yet), it doesn't return null, it returns "null", ie the literal string, 4 characters long starting with "n".
Both null and "null" look remarkably similar if you print them out, but only one responds to a if (cookie == null) Cookies.setCookie(cookie, newValue);
Is there any conceivable reason why Google did it this way, or is somebody just screwing me around?
I can understand your headache (I posted a bug about gwt cookie documentation a while ago: http://code.google.com/p/google-web-toolkit/issues/detail?id=387&can=1 )
Which version of GWT are you using?
Which browser did you test in?
I just looked at the code for 1.6.4 (they ship the source), and I'd encourage you to file this as a bug. See issue 2994 for something close, but I think this is different enough to warrent its own bug filing.
It looks like GWT handles hashmaps in a different manner (for performance reasons?) than regular hashmaps; see java.util.AbstractHashMap in the com/google/gwt/emul directory when you unpack the gwt-user.jar file. Here's the get() impelementation.
return (key == null) ? nullSlot : (!(key instanceof String) ? getHashValue(
key, getHashCode(key)) : getStringValue((String) key));
And maybe this is the issue.
Hope this helps.
Dan
are you sure there isn't a cookie set to a value of "null"? You should have a look at the headers on the response, just to make sure. Depending on the version of GWT this is possible in different ways -- easiest might be hitting "Compile" and trying a real browser, they make it easy to see the headers.
I think setting a cookie null makes the value of the cookie "null" (String)
You should remove the cookie with Cookies.removeCookie("CookieName") which should delete the cookie and your query will return the real null not the string one.
Maybe trying with a duration can change the situation. Try this:
Date now = new Date();
long nowLong = now.getTime();
nowLong = nowLong + (1000 * 60 * 60 * 24 * 7);//seven days
now.setTime(nowLong);
Cookies.setCookie("sampleCookieName", "sampleCookiValue", now);