How to write this encoding algorithm in solidity - javascript

I am trying to implement this encoding
argtypes =["address", "uint256"]
args =[yourAddress, parseEther(gtAmount)]
keccak256(toUtf8Bytes("mint"))+AbiCoder().encode(argtypes, args).slice(2)
This is a JS sudo code. Could you give me some advise how to write it in solidity

Related

Nodejs library without nodejs

How can I integrate a nodejs library into my non nodejs project?
I am particularly needing this library:
https://github.com/greenify/biojs-io-blast
BioJS uses Browserify CDN to automatically generate a single JS file for usage. Either include
<script src="http://wzrd.in/bundle/biojs-io-blast#latest"></script>
in your html or download the JS file via this link.
We also have a live JS Bin example here.
Yes, you can do it using a Publisher/Subscribe pattern and a Queue library, such as RabbitMQ.
In the example below, the author is communicating a python script with a NodeJS one, using the RabbitMQ clients for each platform.
https://github.com/osharim/Communicate-Python-with-NodeJS-through-RabbitMQ
The code for sending from NodeJS:
var amqp = require('amqp');
var amqp_hacks = require('./amqp-hacks');
var connection = amqp.createConnection({ host: "localhost", port: 5672 });
connection.on('ready', function(){
connection.publish('task_queue', 'Hello World!');
console.log(" [x] Sent from nodeJS 'Hello World!'");
amqp_hacks.safeEndConnection(connection);
});
Then, receiving in python:
#!/usr/bin/env python
import pika
import time
connection = pika.BlockingConnection(pika.ConnectionParameters(host='localhost'))
channel = connection.channel()
channel.queue_declare(queue='task_queue', durable=True)
#our callback
def suscriber(ch,method , properties , body):
print "[Y] received %r " % (body,)
time.sleep( body.count('.') )
print " [x] Done"
ch.basic_ack(delivery_tag = method.delivery_tag)
channel.basic_qos(prefetch_count=1)
channel.basic_consume(suscriber, queue = 'task_queue')
print ' [*] Waiting for messages from Python. To exit press CTRL+C'
channel.start_consuming()
to integrate any node library you use the package manager NPM https://www.npmjs.com/ so to integrate your library do as follow
open terminal
cd path/to/your/project_dir
type this line
npm install biojs-io-blast
This is the more common use case. Some of the node.js libraby, i like them too much i want to use it everywhere. But this library, what i see uses core modules of node.js like fs. I dont think you can use it without node dependency || node binary. But as Code Uniquely or others folks says, if you are using webpack as a build/dev. You can try, browserify or BioJS
The node_module which provided is kind of xml parser. You can't add nodejs library (node_module) to non nodejs programs. You can get xml parser for Blast depending on kind of programming language you are using.
For example :
For PHP phpBlastXmlParser and
For java this might helpfull

How do I handle command-line arguments in a mongo script?

I've been working on some simple scripts to run on mongo from the bash command-line. Originally, I ran them as follows:
$ mongo dbname script.js
but I recently came across mikemaccana's answer, https://stackoverflow.com/a/23909051/2846766, indicating the use of mongo as an interpreter so I can just execute script.js (or any name I choose, with or without the .js) from the command line.
$ script.js
I think it's brilliant and clean, but now I'd like to pass in a database name as a command line argument.
$ script.js dbname
Here I use the bash-style "$1" to demonstrate what I'm doing in script.js.
#!/usr/bin/env mongo
var db = new Mongo().getDB($1);
// Do other things with db, once I resolve the name from the command line.
This results in a "ReferenceError: $1 is not defined ...", which is not surprising. But how would I reference command line arguments? Is this going to be a mongo convention? a javascript convention? Is it possible? It would make my command-line experience with mongo much better aesthetically.
Currently there is no way to do this using the mongo shell...
https://groups.google.com/forum/#!topic/mongodb-user/-pO7Cec6Sjc
... try using a bash script (or other scripting language you are comfortable with) if you want to get a similar command line experience.
Duplicate of How to pass argument to Mongo Script
In a nutshell, this is not possible but several workarounds are given in the answers (not repeated here).
You can pass args to your script through
mongo --eval 'var databasePassword="password"' script.js
and you can access databasePassword value inside script.js
db.auth({ user: 'testUser, pwd: databasePassword });

How to get Hello Word compiling from Swift to JavaScript using Emscripten

Given the simplest of swift files:
println("lol")
It's trivial to run this on the command line with xcrun swift -i lol.swift or compile to an executable with xcrun swift lol.swift -o lol, but how about a simple proof of concept for emscripten?
I haven't used emscripten before, but got a hello world example using C++ working from http://kripken.github.io/emscripten-site/docs/getting_started/Tutorial.html, and wanted to compile my Swift code too.
I tried
xcrun swift lol.swift -emit-bc -o lol.bc
emcc lol.bc
But get
Value: %1 = call { i8*, i64, i64 } #_TFSS37_convertFromBuiltinUTF16StringLiteralfMSSFTBp17numberOfCodeUnitsBw_SS(i8* bitcast ([4 x i16]* #0 to i8*), i64 3)
LLVM ERROR: Unrecognized struct value
Traceback (most recent call last):
File "/Users/glen/Downloads/emsdk_portable/emscripten/1.16.0/emcc", line 1540, in <module>
shared.Building.llvm_opt(final, link_opts)
File "/Users/glen/Downloads/emsdk_portable/emscripten/1.16.0/tools/shared.py", line 1267, in llvm_opt
assert os.path.exists(target), 'Failed to run llvm optimizations: ' + output
AssertionError: Failed to run llvm optimizations:
Thoughts?
The problem is LLVM can't find a types/symbols, used in that call, during the linking process. These symbols are most likely specific to the swift framework. If you run emcc with the -v option you can get more debug information. You might consider also providing --llvm-opts hinting where that information can be found.
I ran xcrun swift -v test.swift to see what command is actually executed.
Swift version 1.0 (swift-600.0.34.4.5)
Target: x86_64-apple-darwin13.2.0
/Applications/Xcode6-Beta.app/Contents/Developer/Toolchains/XcodeDefault.xctoolchain/usr/bin/swift -frontend -c -primary-file test.swift -enable-objc-attr-requires-objc-module -target x86_64-apple-darwin13.2.0 -module-name test -color-diagnostics -o /var/folders/69/l9w0zkqn38s1td4_gm5c__km0000gn/T/test-d800d3.o
/usr/bin/ld /var/folders/69/l9w0zkqn38s1td4_gm5c__km0000gn/T/test-d800d3.o -force_load /Applications/Xcode6-Beta.app/Contents/Developer/Toolchains/XcodeDefault.xctoolchain/usr/lib/arc/libarclite_macosx.a -lSystem -arch x86_64 -L /Applications/Xcode6-Beta.app/Contents/Developer/Toolchains/XcodeDefault.xctoolchain/usr/lib/swift/macosx -rpath /Applications/Xcode6-Beta.app/Contents/Developer/Toolchains/XcodeDefault.xctoolchain/usr/lib/swift/macosx -macosx_version_min 10.9.0 -no_objc_category_merging -o test
You might consider exploring how to apply these linking options to emscripten to get what you want. There will not be any documentation on this, because I don't think they intended swift to be used like this.
According to this GitHub issue you filed on the Emscripten repo, what you are trying to do isn't possible:
Unfortunately it is not possible to link .dylib files to Emscripten. The reason for that is that the .dylibs already contain native machine code for x86/x64, and Emscripten cannot "go back" and get that to LLVM IR form again.
-- juj (GitHub user), on 2014-06-14

how to generate po file from js file using poedit

i am using Gettext.js library to localize my contents generated from a JS file. Now the situation is, i have to create and write each and every po files manually. I know we can scan php files for gettext strings using PoEdit. So, is it possible to scan JS files for gettext strings using PoEdit?
Achieved this by creating a new parser of python language in PoEdit.
File > Preferences > Parsers > New
Language:
JS
List of extension:
*.js
Parser command:
xgettext --language=Python --force-po -o %o %C %K %F
Item in Keyword List:
-k%k
Item in input files list:
%f
Source code charset:
--from-code=%c
i found this tutorial while searching on this, which helped me to attain the situation Tutorial Here>>
Actually the tutorial is in French and the link is a google translated(to English) one.
Since version xgettext 0.18.3, you can use JavaScript as the language parameter.
This version of xgettext is used in Poedit since at least version 1.6.2.
The xgettext commandline program is used to scan source code and can parse the following languages:
C, C++, ObjectiveC, Shell, Python, Lisp, EmacsLisp, librep, Scheme, Java, C#, awk, Tcl, Perl, PHP, GCC-source, Glade
Although JavaScript is not listed as a language, I just tried it with a few and Perl actually worked. Try this:
echo " testFunc('foo');" > test.js;
xgettext --keyword=testFunc --output=- test.js --language="perl";
To do this from POEdit, open Preferences > Parsers > Perl add ;*.js to the file extensions list and add --language=Perl after xgettext in the Parser command field. This worked for me and I was able to get new strings from a JS file this way.
Although I don't know how gettext.js works a better approach may be to convert PO files to a native JavaScript file format.
xgettext now supports JavaScript natively, so the command is simply:
xgettext --output=output.pot --language=JavaScript *.js

Python and the Spidermonkey Javascript engine on Linux

I have successfully installed Spidermonkey JS engine on my Linux machine ( Ubuntu ).
Basically my goal is to make it execute Ajax (js) scripts and return the result back to my Python script. I'm basically trying to build a good O.O. web scraper. But it's pretty hard for me to get all of this working.
I'm now at the point where when I type JS in my terminal I can start executing Javascript.
I've been Googling and found this little snipet on Stackoverflow :
import urllib2
import spidermonkey
js = spidermonkey.Runtime()
js_ctx = js.new_context()
script = urllib2.urlopen('http://etherhack.co.uk/hashing/whirlpool/js/whirlpool.js').read()
js_ctx.eval_script(script)
js_ctx.eval_script('var s="abc"')
js_ctx.eval_script('print(HexWhirpool(s))')
but it failed to run with the error that module Spidermonkey can not be found.
I'm a bit lost now. Anyone able to help?
I also tried easy_install python-spidermonkey with no luck, for libnspr-dev package is absent.
So, I've built package from source. Instructions from project page (Debian Stretch):
Building
Check out the Python-Spidermonkey module from the SVN repository ( I downloaded it as source archive, direct link )
Unpack, and cd to ./python-spidermonkey/trunk
CPPFLAGS="-Wno-format-security" python setup.py build (these flags for Debian)
Error jsemit.h:508:32: error: expected ‘(’ before ‘)’ token uintN decltype); means that decltype cannot be used as variable (maybe it's a macro or something else), fix it this way:
sed -e 's/decltype/dectyp/' -i.ORIG ./js/src/jsemit.h
sed -e 's/decltype/dectyp/' -i.ORIG ./js/src/jsemit.cpp
Error jsemit.cpp:6490:1: error: narrowing conversion of ‘-1’ from ‘int’ to ‘uint8 {aka unsigned char}’ inside { } [-Wnarrowing] means illegal variable conversion, recompile it manually:
cd js/src
g++ -o Linux_All_DBG.OBJ/jsemit.o -c -Wall -Wno-narrowing -Wno-format -MMD -g3 -DXP_UNIX -DSVR4 -DSYSV -D_BSD_SOURCE -DPOSIX_SOURCE -DHAVE_LOCALTIME_R -DHAVE_VA_COPY -DVA_COPY=va_copy -DPIC -fPIC -DDEBUG -DDEBUG_user -DEDITLINE -ILinux_All_DBG.OBJ jsemit.cpp
Error spidermonkey.c:1:2: error: #error Do not use this file, it is the result of a failed Pyrex compilation. - some trouble with pyrex. There is a patch. Do it this way:
wget -O - https://storage.googleapis.com/google-code-attachments/python-spidermonkey/issue-14/comment-4/cinit.patch | patch -p1 ./spidermonkey.pyx
Installation
su, and python setup.py install as root.
Running
By default, setup script installs libjs.so to /usr/local/lib/, so I did ln -s /usr/local/lib/libjs.so /usr/lib/libjs.so (but you'd better use solution from Seagal82)
Without this step, python keeps complaining about import ImportError: libjs.so: cannot open shared object file: No such file or directory
I also had an error ImportError: cannot import name Runtime after from spidermonkey import Runtime. The reason possibly was in old easy_install data in ~/.local/lib/python2.7/site-packages/spidermonkey/. After removing it, all runs smooth
Recently i got a task need to do something like Web scraping,
and for the javascript part, currently want to try using python-spidermonkey to resolve it and see if this might work for me ...
and i seem to meet situation might alike, after i think i finished install python-spidermonkey, i execute the script above, i got this error:
Traceback (most recent call last):
File "spidermonkeytest.py", line 2, in <module>
import spidermonkey
ImportError: libjs.so: cannot open shared object file: No such file or directory
then after some searching by google...i found the solution probably in the end of here:
http://hi.baidu.com/peizhongyou/item/ec1575c3f0e00e31e80f2e48
i setup these things:
$sudo vi /etc/ld.so.conf.d/libjs.so.conf
fill in this line:
/usr/local/lib/
save & exit, execute ldconfig:
$sudo ldconfig
then i can run the script provided above by #Synbitz Prowduczions
don't know if this is the answer you need, or this still helps?
You need to try libnspr4. If that doesn't work, you can always download it from Mozilla and build the code yourself.
It is not difficult to type ./config && make && make install to build the library yourself after untarring the source. If you build yourself, files will likely be in
/usr/local/{include,lib}
Also just try Googling for "YOUR_OS_NAME install nspr4".
I believe someone wrote a C/C++ header file translator for Python ctypes. Although I can't say much else because I don't use Python.
SpiderMonkey also has its own implementation of ctypes modeled after Python. So technically if you know javascript you could forego using Python altogether since you want to do some ajax with it. You will need to brush up on the NSPR or C runtime sockets to meet the requirements for your projects using only Spidermonkey.
OR a web search for Python +AJAX might turn up exactly what you need.

Categories

Resources