diff --git a/.gitlab-ci.yml b/.gitlab-ci.yml index a23f4c962..1db0cc49e 100644 --- a/.gitlab-ci.yml +++ b/.gitlab-ci.yml @@ -308,3 +308,46 @@ test-linux: - rust-test dependencies: - linux-stable +js-release: + stage: build + image: ethcore/javascript:latest + only: + - js + - master + - beta + - tags + - stable + before_script: + - ./js/scripts/install-deps.sh + script: + - ./js/scripts/build.sh + - ./js/scripts/release.sh + tags: + - javascript +js-lint: + stage: test + image: ethcore/javascript:latest + before_script: + - ./js/scripts/install-deps.sh + script: + - ./js/scripts/lint.sh + tags: + - javascript-test +js-test: + stage: test + image: ethcore/javascript:latest + before_script: + - ./js/scripts/install-deps.sh + script: + - ./js/scripts/test.sh + tags: + - javascript-test +js-pack: + stage: test + image: ethcore/javascript:latest + before_script: + - ./js/scripts/install-deps.sh + script: + - ./js/scripts/build.sh + tags: + - javascript-test diff --git a/js/.babelrc b/js/.babelrc new file mode 100644 index 000000000..27c697885 --- /dev/null +++ b/js/.babelrc @@ -0,0 +1,15 @@ +{ + "presets": ["es2017", "es2016", "es2015", "stage-0", "react"], + "plugins": [ + "transform-runtime", + "transform-decorators-legacy", + "transform-class-properties", + "lodash" + ], + "retainLines": true, + "env": { + "production": { + "plugins": ["transform-react-remove-prop-types"] + } + } +} diff --git a/js/.codeclimate.yml b/js/.codeclimate.yml new file mode 100644 index 000000000..ca6414f58 --- /dev/null +++ b/js/.codeclimate.yml @@ -0,0 +1,8 @@ +engines: + eslint: + enabled: true + channel: "eslint-2" + +ratings: + paths: + - "**.js" diff --git a/js/.editorconfig b/js/.editorconfig new file mode 100644 index 000000000..3c229b5dc --- /dev/null +++ b/js/.editorconfig @@ -0,0 +1,10 @@ +root = true +[*] +indent_style=space +indent_size=2 +tab_width=2 +end_of_line=lf +charset=utf-8 +trim_trailing_whitespace=true +max_line_length=120 +insert_final_newline=true diff --git a/js/.eslintrc.json b/js/.eslintrc.json new file mode 100644 index 000000000..b649a1bea --- /dev/null +++ b/js/.eslintrc.json @@ -0,0 +1,20 @@ +{ + "extends": ["semistandard", "standard-react"], + "parser": "babel-eslint", + "env": { + "browser": true, + "mocha": true, + "node": true + }, + "globals": { + "expect": true, + "FileReader": true + }, + "rules": { + "object-curly-spacing": ["error", "always"], + "no-debugger": "error", + "no-alert": "error", + "jsx-quotes": ["error", "prefer-single"], + "react/jsx-curly-spacing": ["error", "always"] + } +} diff --git a/js/.gitignore b/js/.gitignore new file mode 100644 index 000000000..c846423fc --- /dev/null +++ b/js/.gitignore @@ -0,0 +1,5 @@ +node_modules +npm-debug.log +.build +.coverage +.happypack diff --git a/js/.istanbul.yml b/js/.istanbul.yml new file mode 100644 index 000000000..4535167db --- /dev/null +++ b/js/.istanbul.yml @@ -0,0 +1,6 @@ +instrumentation: + root: src + extensions: + - .js +reporting: + dir: ./.coverage diff --git a/js/.npmignore b/js/.npmignore new file mode 100644 index 000000000..5093a29b8 --- /dev/null +++ b/js/.npmignore @@ -0,0 +1 @@ +scripts/ diff --git a/js/LICENSE b/js/LICENSE new file mode 100644 index 000000000..9cecc1d46 --- /dev/null +++ b/js/LICENSE @@ -0,0 +1,674 @@ + GNU GENERAL PUBLIC LICENSE + Version 3, 29 June 2007 + + Copyright (C) 2007 Free Software Foundation, Inc. + Everyone is permitted to copy and distribute verbatim copies + of this license document, but changing it is not allowed. + + Preamble + + The GNU General Public License is a free, copyleft license for +software and other kinds of works. + + The licenses for most software and other practical works are designed +to take away your freedom to share and change the works. By contrast, +the GNU General Public License is intended to guarantee your freedom to +share and change all versions of a program--to make sure it remains free +software for all its users. We, the Free Software Foundation, use the +GNU General Public License for most of our software; it applies also to +any other work released this way by its authors. You can apply it to +your programs, too. + + When we speak of free software, we are referring to freedom, not +price. Our General Public Licenses are designed to make sure that you +have the freedom to distribute copies of free software (and charge for +them if you wish), that you receive source code or can get it if you +want it, that you can change the software or use pieces of it in new +free programs, and that you know you can do these things. + + To protect your rights, we need to prevent others from denying you +these rights or asking you to surrender the rights. Therefore, you have +certain responsibilities if you distribute copies of the software, or if +you modify it: responsibilities to respect the freedom of others. + + For example, if you distribute copies of such a program, whether +gratis or for a fee, you must pass on to the recipients the same +freedoms that you received. You must make sure that they, too, receive +or can get the source code. And you must show them these terms so they +know their rights. + + Developers that use the GNU GPL protect your rights with two steps: +(1) assert copyright on the software, and (2) offer you this License +giving you legal permission to copy, distribute and/or modify it. + + For the developers' and authors' protection, the GPL clearly explains +that there is no warranty for this free software. For both users' and +authors' sake, the GPL requires that modified versions be marked as +changed, so that their problems will not be attributed erroneously to +authors of previous versions. + + Some devices are designed to deny users access to install or run +modified versions of the software inside them, although the manufacturer +can do so. This is fundamentally incompatible with the aim of +protecting users' freedom to change the software. The systematic +pattern of such abuse occurs in the area of products for individuals to +use, which is precisely where it is most unacceptable. Therefore, we +have designed this version of the GPL to prohibit the practice for those +products. If such problems arise substantially in other domains, we +stand ready to extend this provision to those domains in future versions +of the GPL, as needed to protect the freedom of users. + + Finally, every program is threatened constantly by software patents. +States should not allow patents to restrict development and use of +software on general-purpose computers, but in those that do, we wish to +avoid the special danger that patents applied to a free program could +make it effectively proprietary. To prevent this, the GPL assures that +patents cannot be used to render the program non-free. + + The precise terms and conditions for copying, distribution and +modification follow. + + TERMS AND CONDITIONS + + 0. Definitions. + + "This License" refers to version 3 of the GNU General Public License. + + "Copyright" also means copyright-like laws that apply to other kinds of +works, such as semiconductor masks. + + "The Program" refers to any copyrightable work licensed under this +License. Each licensee is addressed as "you". "Licensees" and +"recipients" may be individuals or organizations. + + To "modify" a work means to copy from or adapt all or part of the work +in a fashion requiring copyright permission, other than the making of an +exact copy. The resulting work is called a "modified version" of the +earlier work or a work "based on" the earlier work. + + A "covered work" means either the unmodified Program or a work based +on the Program. + + To "propagate" a work means to do anything with it that, without +permission, would make you directly or secondarily liable for +infringement under applicable copyright law, except executing it on a +computer or modifying a private copy. Propagation includes copying, +distribution (with or without modification), making available to the +public, and in some countries other activities as well. + + To "convey" a work means any kind of propagation that enables other +parties to make or receive copies. Mere interaction with a user through +a computer network, with no transfer of a copy, is not conveying. + + An interactive user interface displays "Appropriate Legal Notices" +to the extent that it includes a convenient and prominently visible +feature that (1) displays an appropriate copyright notice, and (2) +tells the user that there is no warranty for the work (except to the +extent that warranties are provided), that licensees may convey the +work under this License, and how to view a copy of this License. If +the interface presents a list of user commands or options, such as a +menu, a prominent item in the list meets this criterion. + + 1. Source Code. + + The "source code" for a work means the preferred form of the work +for making modifications to it. "Object code" means any non-source +form of a work. + + A "Standard Interface" means an interface that either is an official +standard defined by a recognized standards body, or, in the case of +interfaces specified for a particular programming language, one that +is widely used among developers working in that language. + + The "System Libraries" of an executable work include anything, other +than the work as a whole, that (a) is included in the normal form of +packaging a Major Component, but which is not part of that Major +Component, and (b) serves only to enable use of the work with that +Major Component, or to implement a Standard Interface for which an +implementation is available to the public in source code form. A +"Major Component", in this context, means a major essential component +(kernel, window system, and so on) of the specific operating system +(if any) on which the executable work runs, or a compiler used to +produce the work, or an object code interpreter used to run it. + + The "Corresponding Source" for a work in object code form means all +the source code needed to generate, install, and (for an executable +work) run the object code and to modify the work, including scripts to +control those activities. However, it does not include the work's +System Libraries, or general-purpose tools or generally available free +programs which are used unmodified in performing those activities but +which are not part of the work. For example, Corresponding Source +includes interface definition files associated with source files for +the work, and the source code for shared libraries and dynamically +linked subprograms that the work is specifically designed to require, +such as by intimate data communication or control flow between those +subprograms and other parts of the work. + + The Corresponding Source need not include anything that users +can regenerate automatically from other parts of the Corresponding +Source. + + The Corresponding Source for a work in source code form is that +same work. + + 2. Basic Permissions. + + All rights granted under this License are granted for the term of +copyright on the Program, and are irrevocable provided the stated +conditions are met. This License explicitly affirms your unlimited +permission to run the unmodified Program. The output from running a +covered work is covered by this License only if the output, given its +content, constitutes a covered work. This License acknowledges your +rights of fair use or other equivalent, as provided by copyright law. + + You may make, run and propagate covered works that you do not +convey, without conditions so long as your license otherwise remains +in force. You may convey covered works to others for the sole purpose +of having them make modifications exclusively for you, or provide you +with facilities for running those works, provided that you comply with +the terms of this License in conveying all material for which you do +not control copyright. Those thus making or running the covered works +for you must do so exclusively on your behalf, under your direction +and control, on terms that prohibit them from making any copies of +your copyrighted material outside their relationship with you. + + Conveying under any other circumstances is permitted solely under +the conditions stated below. Sublicensing is not allowed; section 10 +makes it unnecessary. + + 3. Protecting Users' Legal Rights From Anti-Circumvention Law. + + No covered work shall be deemed part of an effective technological +measure under any applicable law fulfilling obligations under article +11 of the WIPO copyright treaty adopted on 20 December 1996, or +similar laws prohibiting or restricting circumvention of such +measures. + + When you convey a covered work, you waive any legal power to forbid +circumvention of technological measures to the extent such circumvention +is effected by exercising rights under this License with respect to +the covered work, and you disclaim any intention to limit operation or +modification of the work as a means of enforcing, against the work's +users, your or third parties' legal rights to forbid circumvention of +technological measures. + + 4. Conveying Verbatim Copies. + + You may convey verbatim copies of the Program's source code as you +receive it, in any medium, provided that you conspicuously and +appropriately publish on each copy an appropriate copyright notice; +keep intact all notices stating that this License and any +non-permissive terms added in accord with section 7 apply to the code; +keep intact all notices of the absence of any warranty; and give all +recipients a copy of this License along with the Program. + + You may charge any price or no price for each copy that you convey, +and you may offer support or warranty protection for a fee. + + 5. Conveying Modified Source Versions. + + You may convey a work based on the Program, or the modifications to +produce it from the Program, in the form of source code under the +terms of section 4, provided that you also meet all of these conditions: + + a) The work must carry prominent notices stating that you modified + it, and giving a relevant date. + + b) The work must carry prominent notices stating that it is + released under this License and any conditions added under section + 7. This requirement modifies the requirement in section 4 to + "keep intact all notices". + + c) You must license the entire work, as a whole, under this + License to anyone who comes into possession of a copy. This + License will therefore apply, along with any applicable section 7 + additional terms, to the whole of the work, and all its parts, + regardless of how they are packaged. This License gives no + permission to license the work in any other way, but it does not + invalidate such permission if you have separately received it. + + d) If the work has interactive user interfaces, each must display + Appropriate Legal Notices; however, if the Program has interactive + interfaces that do not display Appropriate Legal Notices, your + work need not make them do so. + + A compilation of a covered work with other separate and independent +works, which are not by their nature extensions of the covered work, +and which are not combined with it such as to form a larger program, +in or on a volume of a storage or distribution medium, is called an +"aggregate" if the compilation and its resulting copyright are not +used to limit the access or legal rights of the compilation's users +beyond what the individual works permit. Inclusion of a covered work +in an aggregate does not cause this License to apply to the other +parts of the aggregate. + + 6. Conveying Non-Source Forms. + + You may convey a covered work in object code form under the terms +of sections 4 and 5, provided that you also convey the +machine-readable Corresponding Source under the terms of this License, +in one of these ways: + + a) Convey the object code in, or embodied in, a physical product + (including a physical distribution medium), accompanied by the + Corresponding Source fixed on a durable physical medium + customarily used for software interchange. + + b) Convey the object code in, or embodied in, a physical product + (including a physical distribution medium), accompanied by a + written offer, valid for at least three years and valid for as + long as you offer spare parts or customer support for that product + model, to give anyone who possesses the object code either (1) a + copy of the Corresponding Source for all the software in the + product that is covered by this License, on a durable physical + medium customarily used for software interchange, for a price no + more than your reasonable cost of physically performing this + conveying of source, or (2) access to copy the + Corresponding Source from a network server at no charge. + + c) Convey individual copies of the object code with a copy of the + written offer to provide the Corresponding Source. This + alternative is allowed only occasionally and noncommercially, and + only if you received the object code with such an offer, in accord + with subsection 6b. + + d) Convey the object code by offering access from a designated + place (gratis or for a charge), and offer equivalent access to the + Corresponding Source in the same way through the same place at no + further charge. You need not require recipients to copy the + Corresponding Source along with the object code. If the place to + copy the object code is a network server, the Corresponding Source + may be on a different server (operated by you or a third party) + that supports equivalent copying facilities, provided you maintain + clear directions next to the object code saying where to find the + Corresponding Source. Regardless of what server hosts the + Corresponding Source, you remain obligated to ensure that it is + available for as long as needed to satisfy these requirements. + + e) Convey the object code using peer-to-peer transmission, provided + you inform other peers where the object code and Corresponding + Source of the work are being offered to the general public at no + charge under subsection 6d. + + A separable portion of the object code, whose source code is excluded +from the Corresponding Source as a System Library, need not be +included in conveying the object code work. + + A "User Product" is either (1) a "consumer product", which means any +tangible personal property which is normally used for personal, family, +or household purposes, or (2) anything designed or sold for incorporation +into a dwelling. In determining whether a product is a consumer product, +doubtful cases shall be resolved in favor of coverage. For a particular +product received by a particular user, "normally used" refers to a +typical or common use of that class of product, regardless of the status +of the particular user or of the way in which the particular user +actually uses, or expects or is expected to use, the product. A product +is a consumer product regardless of whether the product has substantial +commercial, industrial or non-consumer uses, unless such uses represent +the only significant mode of use of the product. + + "Installation Information" for a User Product means any methods, +procedures, authorization keys, or other information required to install +and execute modified versions of a covered work in that User Product from +a modified version of its Corresponding Source. The information must +suffice to ensure that the continued functioning of the modified object +code is in no case prevented or interfered with solely because +modification has been made. + + If you convey an object code work under this section in, or with, or +specifically for use in, a User Product, and the conveying occurs as +part of a transaction in which the right of possession and use of the +User Product is transferred to the recipient in perpetuity or for a +fixed term (regardless of how the transaction is characterized), the +Corresponding Source conveyed under this section must be accompanied +by the Installation Information. But this requirement does not apply +if neither you nor any third party retains the ability to install +modified object code on the User Product (for example, the work has +been installed in ROM). + + The requirement to provide Installation Information does not include a +requirement to continue to provide support service, warranty, or updates +for a work that has been modified or installed by the recipient, or for +the User Product in which it has been modified or installed. Access to a +network may be denied when the modification itself materially and +adversely affects the operation of the network or violates the rules and +protocols for communication across the network. + + Corresponding Source conveyed, and Installation Information provided, +in accord with this section must be in a format that is publicly +documented (and with an implementation available to the public in +source code form), and must require no special password or key for +unpacking, reading or copying. + + 7. Additional Terms. + + "Additional permissions" are terms that supplement the terms of this +License by making exceptions from one or more of its conditions. +Additional permissions that are applicable to the entire Program shall +be treated as though they were included in this License, to the extent +that they are valid under applicable law. If additional permissions +apply only to part of the Program, that part may be used separately +under those permissions, but the entire Program remains governed by +this License without regard to the additional permissions. + + When you convey a copy of a covered work, you may at your option +remove any additional permissions from that copy, or from any part of +it. (Additional permissions may be written to require their own +removal in certain cases when you modify the work.) You may place +additional permissions on material, added by you to a covered work, +for which you have or can give appropriate copyright permission. + + Notwithstanding any other provision of this License, for material you +add to a covered work, you may (if authorized by the copyright holders of +that material) supplement the terms of this License with terms: + + a) Disclaiming warranty or limiting liability differently from the + terms of sections 15 and 16 of this License; or + + b) Requiring preservation of specified reasonable legal notices or + author attributions in that material or in the Appropriate Legal + Notices displayed by works containing it; or + + c) Prohibiting misrepresentation of the origin of that material, or + requiring that modified versions of such material be marked in + reasonable ways as different from the original version; or + + d) Limiting the use for publicity purposes of names of licensors or + authors of the material; or + + e) Declining to grant rights under trademark law for use of some + trade names, trademarks, or service marks; or + + f) Requiring indemnification of licensors and authors of that + material by anyone who conveys the material (or modified versions of + it) with contractual assumptions of liability to the recipient, for + any liability that these contractual assumptions directly impose on + those licensors and authors. + + All other non-permissive additional terms are considered "further +restrictions" within the meaning of section 10. If the Program as you +received it, or any part of it, contains a notice stating that it is +governed by this License along with a term that is a further +restriction, you may remove that term. If a license document contains +a further restriction but permits relicensing or conveying under this +License, you may add to a covered work material governed by the terms +of that license document, provided that the further restriction does +not survive such relicensing or conveying. + + If you add terms to a covered work in accord with this section, you +must place, in the relevant source files, a statement of the +additional terms that apply to those files, or a notice indicating +where to find the applicable terms. + + Additional terms, permissive or non-permissive, may be stated in the +form of a separately written license, or stated as exceptions; +the above requirements apply either way. + + 8. Termination. + + You may not propagate or modify a covered work except as expressly +provided under this License. Any attempt otherwise to propagate or +modify it is void, and will automatically terminate your rights under +this License (including any patent licenses granted under the third +paragraph of section 11). + + However, if you cease all violation of this License, then your +license from a particular copyright holder is reinstated (a) +provisionally, unless and until the copyright holder explicitly and +finally terminates your license, and (b) permanently, if the copyright +holder fails to notify you of the violation by some reasonable means +prior to 60 days after the cessation. + + Moreover, your license from a particular copyright holder is +reinstated permanently if the copyright holder notifies you of the +violation by some reasonable means, this is the first time you have +received notice of violation of this License (for any work) from that +copyright holder, and you cure the violation prior to 30 days after +your receipt of the notice. + + Termination of your rights under this section does not terminate the +licenses of parties who have received copies or rights from you under +this License. If your rights have been terminated and not permanently +reinstated, you do not qualify to receive new licenses for the same +material under section 10. + + 9. Acceptance Not Required for Having Copies. + + You are not required to accept this License in order to receive or +run a copy of the Program. Ancillary propagation of a covered work +occurring solely as a consequence of using peer-to-peer transmission +to receive a copy likewise does not require acceptance. However, +nothing other than this License grants you permission to propagate or +modify any covered work. These actions infringe copyright if you do +not accept this License. Therefore, by modifying or propagating a +covered work, you indicate your acceptance of this License to do so. + + 10. Automatic Licensing of Downstream Recipients. + + Each time you convey a covered work, the recipient automatically +receives a license from the original licensors, to run, modify and +propagate that work, subject to this License. You are not responsible +for enforcing compliance by third parties with this License. + + An "entity transaction" is a transaction transferring control of an +organization, or substantially all assets of one, or subdividing an +organization, or merging organizations. If propagation of a covered +work results from an entity transaction, each party to that +transaction who receives a copy of the work also receives whatever +licenses to the work the party's predecessor in interest had or could +give under the previous paragraph, plus a right to possession of the +Corresponding Source of the work from the predecessor in interest, if +the predecessor has it or can get it with reasonable efforts. + + You may not impose any further restrictions on the exercise of the +rights granted or affirmed under this License. For example, you may +not impose a license fee, royalty, or other charge for exercise of +rights granted under this License, and you may not initiate litigation +(including a cross-claim or counterclaim in a lawsuit) alleging that +any patent claim is infringed by making, using, selling, offering for +sale, or importing the Program or any portion of it. + + 11. Patents. + + A "contributor" is a copyright holder who authorizes use under this +License of the Program or a work on which the Program is based. The +work thus licensed is called the contributor's "contributor version". + + A contributor's "essential patent claims" are all patent claims +owned or controlled by the contributor, whether already acquired or +hereafter acquired, that would be infringed by some manner, permitted +by this License, of making, using, or selling its contributor version, +but do not include claims that would be infringed only as a +consequence of further modification of the contributor version. For +purposes of this definition, "control" includes the right to grant +patent sublicenses in a manner consistent with the requirements of +this License. + + Each contributor grants you a non-exclusive, worldwide, royalty-free +patent license under the contributor's essential patent claims, to +make, use, sell, offer for sale, import and otherwise run, modify and +propagate the contents of its contributor version. + + In the following three paragraphs, a "patent license" is any express +agreement or commitment, however denominated, not to enforce a patent +(such as an express permission to practice a patent or covenant not to +sue for patent infringement). To "grant" such a patent license to a +party means to make such an agreement or commitment not to enforce a +patent against the party. + + If you convey a covered work, knowingly relying on a patent license, +and the Corresponding Source of the work is not available for anyone +to copy, free of charge and under the terms of this License, through a +publicly available network server or other readily accessible means, +then you must either (1) cause the Corresponding Source to be so +available, or (2) arrange to deprive yourself of the benefit of the +patent license for this particular work, or (3) arrange, in a manner +consistent with the requirements of this License, to extend the patent +license to downstream recipients. "Knowingly relying" means you have +actual knowledge that, but for the patent license, your conveying the +covered work in a country, or your recipient's use of the covered work +in a country, would infringe one or more identifiable patents in that +country that you have reason to believe are valid. + + If, pursuant to or in connection with a single transaction or +arrangement, you convey, or propagate by procuring conveyance of, a +covered work, and grant a patent license to some of the parties +receiving the covered work authorizing them to use, propagate, modify +or convey a specific copy of the covered work, then the patent license +you grant is automatically extended to all recipients of the covered +work and works based on it. + + A patent license is "discriminatory" if it does not include within +the scope of its coverage, prohibits the exercise of, or is +conditioned on the non-exercise of one or more of the rights that are +specifically granted under this License. You may not convey a covered +work if you are a party to an arrangement with a third party that is +in the business of distributing software, under which you make payment +to the third party based on the extent of your activity of conveying +the work, and under which the third party grants, to any of the +parties who would receive the covered work from you, a discriminatory +patent license (a) in connection with copies of the covered work +conveyed by you (or copies made from those copies), or (b) primarily +for and in connection with specific products or compilations that +contain the covered work, unless you entered into that arrangement, +or that patent license was granted, prior to 28 March 2007. + + Nothing in this License shall be construed as excluding or limiting +any implied license or other defenses to infringement that may +otherwise be available to you under applicable patent law. + + 12. No Surrender of Others' Freedom. + + If conditions are imposed on you (whether by court order, agreement or +otherwise) that contradict the conditions of this License, they do not +excuse you from the conditions of this License. If you cannot convey a +covered work so as to satisfy simultaneously your obligations under this +License and any other pertinent obligations, then as a consequence you may +not convey it at all. For example, if you agree to terms that obligate you +to collect a royalty for further conveying from those to whom you convey +the Program, the only way you could satisfy both those terms and this +License would be to refrain entirely from conveying the Program. + + 13. Use with the GNU Affero General Public License. + + Notwithstanding any other provision of this License, you have +permission to link or combine any covered work with a work licensed +under version 3 of the GNU Affero General Public License into a single +combined work, and to convey the resulting work. The terms of this +License will continue to apply to the part which is the covered work, +but the special requirements of the GNU Affero General Public License, +section 13, concerning interaction through a network will apply to the +combination as such. + + 14. Revised Versions of this License. + + The Free Software Foundation may publish revised and/or new versions of +the GNU General Public License from time to time. Such new versions will +be similar in spirit to the present version, but may differ in detail to +address new problems or concerns. + + Each version is given a distinguishing version number. If the +Program specifies that a certain numbered version of the GNU General +Public License "or any later version" applies to it, you have the +option of following the terms and conditions either of that numbered +version or of any later version published by the Free Software +Foundation. If the Program does not specify a version number of the +GNU General Public License, you may choose any version ever published +by the Free Software Foundation. + + If the Program specifies that a proxy can decide which future +versions of the GNU General Public License can be used, that proxy's +public statement of acceptance of a version permanently authorizes you +to choose that version for the Program. + + Later license versions may give you additional or different +permissions. However, no additional obligations are imposed on any +author or copyright holder as a result of your choosing to follow a +later version. + + 15. Disclaimer of Warranty. + + THERE IS NO WARRANTY FOR THE PROGRAM, TO THE EXTENT PERMITTED BY +APPLICABLE LAW. EXCEPT WHEN OTHERWISE STATED IN WRITING THE COPYRIGHT +HOLDERS AND/OR OTHER PARTIES PROVIDE THE PROGRAM "AS IS" WITHOUT WARRANTY +OF ANY KIND, EITHER EXPRESSED OR IMPLIED, INCLUDING, BUT NOT LIMITED TO, +THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR +PURPOSE. THE ENTIRE RISK AS TO THE QUALITY AND PERFORMANCE OF THE PROGRAM +IS WITH YOU. SHOULD THE PROGRAM PROVE DEFECTIVE, YOU ASSUME THE COST OF +ALL NECESSARY SERVICING, REPAIR OR CORRECTION. + + 16. Limitation of Liability. + + IN NO EVENT UNLESS REQUIRED BY APPLICABLE LAW OR AGREED TO IN WRITING +WILL ANY COPYRIGHT HOLDER, OR ANY OTHER PARTY WHO MODIFIES AND/OR CONVEYS +THE PROGRAM AS PERMITTED ABOVE, BE LIABLE TO YOU FOR DAMAGES, INCLUDING ANY +GENERAL, SPECIAL, INCIDENTAL OR CONSEQUENTIAL DAMAGES ARISING OUT OF THE +USE OR INABILITY TO USE THE PROGRAM (INCLUDING BUT NOT LIMITED TO LOSS OF +DATA OR DATA BEING RENDERED INACCURATE OR LOSSES SUSTAINED BY YOU OR THIRD +PARTIES OR A FAILURE OF THE PROGRAM TO OPERATE WITH ANY OTHER PROGRAMS), +EVEN IF SUCH HOLDER OR OTHER PARTY HAS BEEN ADVISED OF THE POSSIBILITY OF +SUCH DAMAGES. + + 17. Interpretation of Sections 15 and 16. + + If the disclaimer of warranty and limitation of liability provided +above cannot be given local legal effect according to their terms, +reviewing courts shall apply local law that most closely approximates +an absolute waiver of all civil liability in connection with the +Program, unless a warranty or assumption of liability accompanies a +copy of the Program in return for a fee. + + END OF TERMS AND CONDITIONS + + How to Apply These Terms to Your New Programs + + If you develop a new program, and you want it to be of the greatest +possible use to the public, the best way to achieve this is to make it +free software which everyone can redistribute and change under these terms. + + To do so, attach the following notices to the program. It is safest +to attach them to the start of each source file to most effectively +state the exclusion of warranty; and each file should have at least +the "copyright" line and a pointer to where the full notice is found. + + {one line to give the program's name and a brief idea of what it does.} + Copyright (C) {year} {name of author} + + This program is free software: you can redistribute it and/or modify + it under the terms of the GNU General Public License as published by + the Free Software Foundation, either version 3 of the License, or + (at your option) any later version. + + This program is distributed in the hope that it will be useful, + but WITHOUT ANY WARRANTY; without even the implied warranty of + MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + GNU General Public License for more details. + + You should have received a copy of the GNU General Public License + along with this program. If not, see . + +Also add information on how to contact you by electronic and paper mail. + + If the program does terminal interaction, make it output a short +notice like this when it starts in an interactive mode: + + {project} Copyright (C) {year} {fullname} + This program comes with ABSOLUTELY NO WARRANTY; for details type `show w'. + This is free software, and you are welcome to redistribute it + under certain conditions; type `show c' for details. + +The hypothetical commands `show w' and `show c' should show the appropriate +parts of the General Public License. Of course, your program's commands +might be different; for a GUI interface, you would use an "about box". + + You should also get your employer (if you work as a programmer) or school, +if any, to sign a "copyright disclaimer" for the program, if necessary. +For more information on this, and how to apply and follow the GNU GPL, see +. + + The GNU General Public License does not permit incorporating your program +into proprietary programs. If your program is a subroutine library, you +may consider it more useful to permit linking proprietary applications with +the library. If this is what you want to do, use the GNU Lesser General +Public License instead of this License. But first, please read +. diff --git a/js/README.md b/js/README.md new file mode 100644 index 000000000..41252bc91 --- /dev/null +++ b/js/README.md @@ -0,0 +1,12 @@ +# parity.js + +JavaScript APIs and UIs for Parity. + +## development + +0. Install [Node](https://nodejs.org/) if not already available +0. Change to the `js` directory inside `parity/` +0. Install the npm modules via `npm install` +0. Parity should be run with `parity --signer-no-validation [...options]` (where `options` can be `--chain testnet`) +0. Start the development environment via `npm start` +0. Connect to the [UI](http://localhost:3000) diff --git a/js/assets/fonts/Roboto/LICENSE.txt b/js/assets/fonts/Roboto/LICENSE.txt new file mode 100755 index 000000000..75b52484e --- /dev/null +++ b/js/assets/fonts/Roboto/LICENSE.txt @@ -0,0 +1,202 @@ + + Apache License + Version 2.0, January 2004 + http://www.apache.org/licenses/ + + TERMS AND CONDITIONS FOR USE, REPRODUCTION, AND DISTRIBUTION + + 1. Definitions. + + "License" shall mean the terms and conditions for use, reproduction, + and distribution as defined by Sections 1 through 9 of this document. + + "Licensor" shall mean the copyright owner or entity authorized by + the copyright owner that is granting the License. + + "Legal Entity" shall mean the union of the acting entity and all + other entities that control, are controlled by, or are under common + control with that entity. For the purposes of this definition, + "control" means (i) the power, direct or indirect, to cause the + direction or management of such entity, whether by contract or + otherwise, or (ii) ownership of fifty percent (50%) or more of the + outstanding shares, or (iii) beneficial ownership of such entity. + + "You" (or "Your") shall mean an individual or Legal Entity + exercising permissions granted by this License. + + "Source" form shall mean the preferred form for making modifications, + including but not limited to software source code, documentation + source, and configuration files. + + "Object" form shall mean any form resulting from mechanical + transformation or translation of a Source form, including but + not limited to compiled object code, generated documentation, + and conversions to other media types. + + "Work" shall mean the work of authorship, whether in Source or + Object form, made available under the License, as indicated by a + copyright notice that is included in or attached to the work + (an example is provided in the Appendix below). + + "Derivative Works" shall mean any work, whether in Source or Object + form, that is based on (or derived from) the Work and for which the + editorial revisions, annotations, elaborations, or other modifications + represent, as a whole, an original work of authorship. For the purposes + of this License, Derivative Works shall not include works that remain + separable from, or merely link (or bind by name) to the interfaces of, + the Work and Derivative Works thereof. + + "Contribution" shall mean any work of authorship, including + the original version of the Work and any modifications or additions + to that Work or Derivative Works thereof, that is intentionally + submitted to Licensor for inclusion in the Work by the copyright owner + or by an individual or Legal Entity authorized to submit on behalf of + the copyright owner. For the purposes of this definition, "submitted" + means any form of electronic, verbal, or written communication sent + to the Licensor or its representatives, including but not limited to + communication on electronic mailing lists, source code control systems, + and issue tracking systems that are managed by, or on behalf of, the + Licensor for the purpose of discussing and improving the Work, but + excluding communication that is conspicuously marked or otherwise + designated in writing by the copyright owner as "Not a Contribution." + + "Contributor" shall mean Licensor and any individual or Legal Entity + on behalf of whom a Contribution has been received by Licensor and + subsequently incorporated within the Work. + + 2. Grant of Copyright License. Subject to the terms and conditions of + this License, each Contributor hereby grants to You a perpetual, + worldwide, non-exclusive, no-charge, royalty-free, irrevocable + copyright license to reproduce, prepare Derivative Works of, + publicly display, publicly perform, sublicense, and distribute the + Work and such Derivative Works in Source or Object form. + + 3. Grant of Patent License. Subject to the terms and conditions of + this License, each Contributor hereby grants to You a perpetual, + worldwide, non-exclusive, no-charge, royalty-free, irrevocable + (except as stated in this section) patent license to make, have made, + use, offer to sell, sell, import, and otherwise transfer the Work, + where such license applies only to those patent claims licensable + by such Contributor that are necessarily infringed by their + Contribution(s) alone or by combination of their Contribution(s) + with the Work to which such Contribution(s) was submitted. If You + institute patent litigation against any entity (including a + cross-claim or counterclaim in a lawsuit) alleging that the Work + or a Contribution incorporated within the Work constitutes direct + or contributory patent infringement, then any patent licenses + granted to You under this License for that Work shall terminate + as of the date such litigation is filed. + + 4. Redistribution. You may reproduce and distribute copies of the + Work or Derivative Works thereof in any medium, with or without + modifications, and in Source or Object form, provided that You + meet the following conditions: + + (a) You must give any other recipients of the Work or + Derivative Works a copy of this License; and + + (b) You must cause any modified files to carry prominent notices + stating that You changed the files; and + + (c) You must retain, in the Source form of any Derivative Works + that You distribute, all copyright, patent, trademark, and + attribution notices from the Source form of the Work, + excluding those notices that do not pertain to any part of + the Derivative Works; and + + (d) If the Work includes a "NOTICE" text file as part of its + distribution, then any Derivative Works that You distribute must + include a readable copy of the attribution notices contained + within such NOTICE file, excluding those notices that do not + pertain to any part of the Derivative Works, in at least one + of the following places: within a NOTICE text file distributed + as part of the Derivative Works; within the Source form or + documentation, if provided along with the Derivative Works; or, + within a display generated by the Derivative Works, if and + wherever such third-party notices normally appear. The contents + of the NOTICE file are for informational purposes only and + do not modify the License. You may add Your own attribution + notices within Derivative Works that You distribute, alongside + or as an addendum to the NOTICE text from the Work, provided + that such additional attribution notices cannot be construed + as modifying the License. + + You may add Your own copyright statement to Your modifications and + may provide additional or different license terms and conditions + for use, reproduction, or distribution of Your modifications, or + for any such Derivative Works as a whole, provided Your use, + reproduction, and distribution of the Work otherwise complies with + the conditions stated in this License. + + 5. Submission of Contributions. Unless You explicitly state otherwise, + any Contribution intentionally submitted for inclusion in the Work + by You to the Licensor shall be under the terms and conditions of + this License, without any additional terms or conditions. + Notwithstanding the above, nothing herein shall supersede or modify + the terms of any separate license agreement you may have executed + with Licensor regarding such Contributions. + + 6. Trademarks. This License does not grant permission to use the trade + names, trademarks, service marks, or product names of the Licensor, + except as required for reasonable and customary use in describing the + origin of the Work and reproducing the content of the NOTICE file. + + 7. Disclaimer of Warranty. Unless required by applicable law or + agreed to in writing, Licensor provides the Work (and each + Contributor provides its Contributions) on an "AS IS" BASIS, + WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or + implied, including, without limitation, any warranties or conditions + of TITLE, NON-INFRINGEMENT, MERCHANTABILITY, or FITNESS FOR A + PARTICULAR PURPOSE. You are solely responsible for determining the + appropriateness of using or redistributing the Work and assume any + risks associated with Your exercise of permissions under this License. + + 8. Limitation of Liability. In no event and under no legal theory, + whether in tort (including negligence), contract, or otherwise, + unless required by applicable law (such as deliberate and grossly + negligent acts) or agreed to in writing, shall any Contributor be + liable to You for damages, including any direct, indirect, special, + incidental, or consequential damages of any character arising as a + result of this License or out of the use or inability to use the + Work (including but not limited to damages for loss of goodwill, + work stoppage, computer failure or malfunction, or any and all + other commercial damages or losses), even if such Contributor + has been advised of the possibility of such damages. + + 9. Accepting Warranty or Additional Liability. While redistributing + the Work or Derivative Works thereof, You may choose to offer, + and charge a fee for, acceptance of support, warranty, indemnity, + or other liability obligations and/or rights consistent with this + License. However, in accepting such obligations, You may act only + on Your own behalf and on Your sole responsibility, not on behalf + of any other Contributor, and only if You agree to indemnify, + defend, and hold each Contributor harmless for any liability + incurred by, or claims asserted against, such Contributor by reason + of your accepting any such warranty or additional liability. + + END OF TERMS AND CONDITIONS + + APPENDIX: How to apply the Apache License to your work. + + To apply the Apache License to your work, attach the following + boilerplate notice, with the fields enclosed by brackets "[]" + replaced with your own identifying information. (Don't include + the brackets!) The text should be enclosed in the appropriate + comment syntax for the file format. We also recommend that a + file or class name and description of purpose be included on the + same "printed page" as the copyright notice for easier + identification within third-party archives. + + Copyright [yyyy] [name of copyright owner] + + Licensed under the Apache License, Version 2.0 (the "License"); + you may not use this file except in compliance with the License. + You may obtain a copy of the License at + + http://www.apache.org/licenses/LICENSE-2.0 + + Unless required by applicable law or agreed to in writing, software + distributed under the License is distributed on an "AS IS" BASIS, + WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + See the License for the specific language governing permissions and + limitations under the License. diff --git a/js/assets/fonts/Roboto/font.css b/js/assets/fonts/Roboto/font.css new file mode 100644 index 000000000..5eea2bfd1 --- /dev/null +++ b/js/assets/fonts/Roboto/font.css @@ -0,0 +1,56 @@ +/* cyrillic-ext */ +@font-face { + font-family: 'Roboto'; + font-style: normal; + font-weight: 300; + src: local('Roboto Light'), local('Roboto-Light'), url(v15/0eC6fl06luXEYWpBSJvXCIX0hVgzZQUfRDuZrPvH3D8.woff2) format('woff2'); + unicode-range: U+0460-052F, U+20B4, U+2DE0-2DFF, U+A640-A69F; +} +/* cyrillic */ +@font-face { + font-family: 'Roboto'; + font-style: normal; + font-weight: 300; + src: local('Roboto Light'), local('Roboto-Light'), url(v15/Fl4y0QdOxyyTHEGMXX8kcYX0hVgzZQUfRDuZrPvH3D8.woff2) format('woff2'); + unicode-range: U+0400-045F, U+0490-0491, U+04B0-04B1, U+2116; +} +/* greek-ext */ +@font-face { + font-family: 'Roboto'; + font-style: normal; + font-weight: 300; + src: local('Roboto Light'), local('Roboto-Light'), url(v15/-L14Jk06m6pUHB-5mXQQnYX0hVgzZQUfRDuZrPvH3D8.woff2) format('woff2'); + unicode-range: U+1F00-1FFF; +} +/* greek */ +@font-face { + font-family: 'Roboto'; + font-style: normal; + font-weight: 300; + src: local('Roboto Light'), local('Roboto-Light'), url(v15/I3S1wsgSg9YCurV6PUkTOYX0hVgzZQUfRDuZrPvH3D8.woff2) format('woff2'); + unicode-range: U+0370-03FF; +} +/* vietnamese */ +@font-face { + font-family: 'Roboto'; + font-style: normal; + font-weight: 300; + src: local('Roboto Light'), local('Roboto-Light'), url(v15/NYDWBdD4gIq26G5XYbHsFIX0hVgzZQUfRDuZrPvH3D8.woff2) format('woff2'); + unicode-range: U+0102-0103, U+1EA0-1EF9, U+20AB; +} +/* latin-ext */ +@font-face { + font-family: 'Roboto'; + font-style: normal; + font-weight: 300; + src: local('Roboto Light'), local('Roboto-Light'), url(v15/Pru33qjShpZSmG3z6VYwnYX0hVgzZQUfRDuZrPvH3D8.woff2) format('woff2'); + unicode-range: U+0100-024F, U+1E00-1EFF, U+20A0-20AB, U+20AD-20CF, U+2C60-2C7F, U+A720-A7FF; +} +/* latin */ +@font-face { + font-family: 'Roboto'; + font-style: normal; + font-weight: 300; + src: local('Roboto Light'), local('Roboto-Light'), url(v15/Hgo13k-tfSpn0qi1SFdUfZBw1xU1rKptJj_0jans920.woff2) format('woff2'); + unicode-range: U+0000-00FF, U+0131, U+0152-0153, U+02C6, U+02DA, U+02DC, U+2000-206F, U+2074, U+20AC, U+2212, U+2215, U+E0FF, U+EFFD, U+F000; +} diff --git a/js/assets/fonts/Roboto/ttf/Roboto-Black.ttf b/js/assets/fonts/Roboto/ttf/Roboto-Black.ttf new file mode 100755 index 000000000..fbde625d4 Binary files /dev/null and b/js/assets/fonts/Roboto/ttf/Roboto-Black.ttf differ diff --git a/js/assets/fonts/Roboto/ttf/Roboto-BlackItalic.ttf b/js/assets/fonts/Roboto/ttf/Roboto-BlackItalic.ttf new file mode 100755 index 000000000..60f7782a2 Binary files /dev/null and b/js/assets/fonts/Roboto/ttf/Roboto-BlackItalic.ttf differ diff --git a/js/assets/fonts/Roboto/ttf/Roboto-Bold.ttf b/js/assets/fonts/Roboto/ttf/Roboto-Bold.ttf new file mode 100755 index 000000000..a355c27cd Binary files /dev/null and b/js/assets/fonts/Roboto/ttf/Roboto-Bold.ttf differ diff --git a/js/assets/fonts/Roboto/ttf/Roboto-BoldItalic.ttf b/js/assets/fonts/Roboto/ttf/Roboto-BoldItalic.ttf new file mode 100755 index 000000000..3c9a7a373 Binary files /dev/null and b/js/assets/fonts/Roboto/ttf/Roboto-BoldItalic.ttf differ diff --git a/js/assets/fonts/Roboto/ttf/Roboto-Italic.ttf b/js/assets/fonts/Roboto/ttf/Roboto-Italic.ttf new file mode 100755 index 000000000..ff6046d5b Binary files /dev/null and b/js/assets/fonts/Roboto/ttf/Roboto-Italic.ttf differ diff --git a/js/assets/fonts/Roboto/ttf/Roboto-Light.ttf b/js/assets/fonts/Roboto/ttf/Roboto-Light.ttf new file mode 100755 index 000000000..94c6bcc67 Binary files /dev/null and b/js/assets/fonts/Roboto/ttf/Roboto-Light.ttf differ diff --git a/js/assets/fonts/Roboto/ttf/Roboto-LightItalic.ttf b/js/assets/fonts/Roboto/ttf/Roboto-LightItalic.ttf new file mode 100755 index 000000000..04cc00230 Binary files /dev/null and b/js/assets/fonts/Roboto/ttf/Roboto-LightItalic.ttf differ diff --git a/js/assets/fonts/Roboto/ttf/Roboto-Medium.ttf b/js/assets/fonts/Roboto/ttf/Roboto-Medium.ttf new file mode 100755 index 000000000..39c63d746 Binary files /dev/null and b/js/assets/fonts/Roboto/ttf/Roboto-Medium.ttf differ diff --git a/js/assets/fonts/Roboto/ttf/Roboto-MediumItalic.ttf b/js/assets/fonts/Roboto/ttf/Roboto-MediumItalic.ttf new file mode 100755 index 000000000..dc743f0a6 Binary files /dev/null and b/js/assets/fonts/Roboto/ttf/Roboto-MediumItalic.ttf differ diff --git a/js/assets/fonts/Roboto/ttf/Roboto-Regular.ttf b/js/assets/fonts/Roboto/ttf/Roboto-Regular.ttf new file mode 100755 index 000000000..8c082c8de Binary files /dev/null and b/js/assets/fonts/Roboto/ttf/Roboto-Regular.ttf differ diff --git a/js/assets/fonts/Roboto/ttf/Roboto-Thin.ttf b/js/assets/fonts/Roboto/ttf/Roboto-Thin.ttf new file mode 100755 index 000000000..d69555029 Binary files /dev/null and b/js/assets/fonts/Roboto/ttf/Roboto-Thin.ttf differ diff --git a/js/assets/fonts/Roboto/ttf/Roboto-ThinItalic.ttf b/js/assets/fonts/Roboto/ttf/Roboto-ThinItalic.ttf new file mode 100755 index 000000000..07172ff66 Binary files /dev/null and b/js/assets/fonts/Roboto/ttf/Roboto-ThinItalic.ttf differ diff --git a/js/assets/fonts/Roboto/v15/-L14Jk06m6pUHB-5mXQQnYX0hVgzZQUfRDuZrPvH3D8.woff2 b/js/assets/fonts/Roboto/v15/-L14Jk06m6pUHB-5mXQQnYX0hVgzZQUfRDuZrPvH3D8.woff2 new file mode 100644 index 000000000..58fd4bcd2 Binary files /dev/null and b/js/assets/fonts/Roboto/v15/-L14Jk06m6pUHB-5mXQQnYX0hVgzZQUfRDuZrPvH3D8.woff2 differ diff --git a/js/assets/fonts/Roboto/v15/0eC6fl06luXEYWpBSJvXCIX0hVgzZQUfRDuZrPvH3D8.woff2 b/js/assets/fonts/Roboto/v15/0eC6fl06luXEYWpBSJvXCIX0hVgzZQUfRDuZrPvH3D8.woff2 new file mode 100644 index 000000000..eacda321c Binary files /dev/null and b/js/assets/fonts/Roboto/v15/0eC6fl06luXEYWpBSJvXCIX0hVgzZQUfRDuZrPvH3D8.woff2 differ diff --git a/js/assets/fonts/Roboto/v15/Fl4y0QdOxyyTHEGMXX8kcYX0hVgzZQUfRDuZrPvH3D8.woff2 b/js/assets/fonts/Roboto/v15/Fl4y0QdOxyyTHEGMXX8kcYX0hVgzZQUfRDuZrPvH3D8.woff2 new file mode 100644 index 000000000..cc16b36c5 Binary files /dev/null and b/js/assets/fonts/Roboto/v15/Fl4y0QdOxyyTHEGMXX8kcYX0hVgzZQUfRDuZrPvH3D8.woff2 differ diff --git a/js/assets/fonts/Roboto/v15/Hgo13k-tfSpn0qi1SFdUfZBw1xU1rKptJj_0jans920.woff2 b/js/assets/fonts/Roboto/v15/Hgo13k-tfSpn0qi1SFdUfZBw1xU1rKptJj_0jans920.woff2 new file mode 100644 index 000000000..4411cbc87 Binary files /dev/null and b/js/assets/fonts/Roboto/v15/Hgo13k-tfSpn0qi1SFdUfZBw1xU1rKptJj_0jans920.woff2 differ diff --git a/js/assets/fonts/Roboto/v15/I3S1wsgSg9YCurV6PUkTOYX0hVgzZQUfRDuZrPvH3D8.woff2 b/js/assets/fonts/Roboto/v15/I3S1wsgSg9YCurV6PUkTOYX0hVgzZQUfRDuZrPvH3D8.woff2 new file mode 100644 index 000000000..0028bd81e Binary files /dev/null and b/js/assets/fonts/Roboto/v15/I3S1wsgSg9YCurV6PUkTOYX0hVgzZQUfRDuZrPvH3D8.woff2 differ diff --git a/js/assets/fonts/Roboto/v15/NYDWBdD4gIq26G5XYbHsFIX0hVgzZQUfRDuZrPvH3D8.woff2 b/js/assets/fonts/Roboto/v15/NYDWBdD4gIq26G5XYbHsFIX0hVgzZQUfRDuZrPvH3D8.woff2 new file mode 100644 index 000000000..a5cefc9c6 Binary files /dev/null and b/js/assets/fonts/Roboto/v15/NYDWBdD4gIq26G5XYbHsFIX0hVgzZQUfRDuZrPvH3D8.woff2 differ diff --git a/js/assets/fonts/Roboto/v15/Pru33qjShpZSmG3z6VYwnYX0hVgzZQUfRDuZrPvH3D8.woff2 b/js/assets/fonts/Roboto/v15/Pru33qjShpZSmG3z6VYwnYX0hVgzZQUfRDuZrPvH3D8.woff2 new file mode 100644 index 000000000..feec9f1e6 Binary files /dev/null and b/js/assets/fonts/Roboto/v15/Pru33qjShpZSmG3z6VYwnYX0hVgzZQUfRDuZrPvH3D8.woff2 differ diff --git a/js/assets/fonts/RobotoMono/LICENSE.txt b/js/assets/fonts/RobotoMono/LICENSE.txt new file mode 100755 index 000000000..75b52484e --- /dev/null +++ b/js/assets/fonts/RobotoMono/LICENSE.txt @@ -0,0 +1,202 @@ + + Apache License + Version 2.0, January 2004 + http://www.apache.org/licenses/ + + TERMS AND CONDITIONS FOR USE, REPRODUCTION, AND DISTRIBUTION + + 1. Definitions. + + "License" shall mean the terms and conditions for use, reproduction, + and distribution as defined by Sections 1 through 9 of this document. + + "Licensor" shall mean the copyright owner or entity authorized by + the copyright owner that is granting the License. + + "Legal Entity" shall mean the union of the acting entity and all + other entities that control, are controlled by, or are under common + control with that entity. For the purposes of this definition, + "control" means (i) the power, direct or indirect, to cause the + direction or management of such entity, whether by contract or + otherwise, or (ii) ownership of fifty percent (50%) or more of the + outstanding shares, or (iii) beneficial ownership of such entity. + + "You" (or "Your") shall mean an individual or Legal Entity + exercising permissions granted by this License. + + "Source" form shall mean the preferred form for making modifications, + including but not limited to software source code, documentation + source, and configuration files. + + "Object" form shall mean any form resulting from mechanical + transformation or translation of a Source form, including but + not limited to compiled object code, generated documentation, + and conversions to other media types. + + "Work" shall mean the work of authorship, whether in Source or + Object form, made available under the License, as indicated by a + copyright notice that is included in or attached to the work + (an example is provided in the Appendix below). + + "Derivative Works" shall mean any work, whether in Source or Object + form, that is based on (or derived from) the Work and for which the + editorial revisions, annotations, elaborations, or other modifications + represent, as a whole, an original work of authorship. For the purposes + of this License, Derivative Works shall not include works that remain + separable from, or merely link (or bind by name) to the interfaces of, + the Work and Derivative Works thereof. + + "Contribution" shall mean any work of authorship, including + the original version of the Work and any modifications or additions + to that Work or Derivative Works thereof, that is intentionally + submitted to Licensor for inclusion in the Work by the copyright owner + or by an individual or Legal Entity authorized to submit on behalf of + the copyright owner. For the purposes of this definition, "submitted" + means any form of electronic, verbal, or written communication sent + to the Licensor or its representatives, including but not limited to + communication on electronic mailing lists, source code control systems, + and issue tracking systems that are managed by, or on behalf of, the + Licensor for the purpose of discussing and improving the Work, but + excluding communication that is conspicuously marked or otherwise + designated in writing by the copyright owner as "Not a Contribution." + + "Contributor" shall mean Licensor and any individual or Legal Entity + on behalf of whom a Contribution has been received by Licensor and + subsequently incorporated within the Work. + + 2. Grant of Copyright License. Subject to the terms and conditions of + this License, each Contributor hereby grants to You a perpetual, + worldwide, non-exclusive, no-charge, royalty-free, irrevocable + copyright license to reproduce, prepare Derivative Works of, + publicly display, publicly perform, sublicense, and distribute the + Work and such Derivative Works in Source or Object form. + + 3. Grant of Patent License. Subject to the terms and conditions of + this License, each Contributor hereby grants to You a perpetual, + worldwide, non-exclusive, no-charge, royalty-free, irrevocable + (except as stated in this section) patent license to make, have made, + use, offer to sell, sell, import, and otherwise transfer the Work, + where such license applies only to those patent claims licensable + by such Contributor that are necessarily infringed by their + Contribution(s) alone or by combination of their Contribution(s) + with the Work to which such Contribution(s) was submitted. If You + institute patent litigation against any entity (including a + cross-claim or counterclaim in a lawsuit) alleging that the Work + or a Contribution incorporated within the Work constitutes direct + or contributory patent infringement, then any patent licenses + granted to You under this License for that Work shall terminate + as of the date such litigation is filed. + + 4. Redistribution. You may reproduce and distribute copies of the + Work or Derivative Works thereof in any medium, with or without + modifications, and in Source or Object form, provided that You + meet the following conditions: + + (a) You must give any other recipients of the Work or + Derivative Works a copy of this License; and + + (b) You must cause any modified files to carry prominent notices + stating that You changed the files; and + + (c) You must retain, in the Source form of any Derivative Works + that You distribute, all copyright, patent, trademark, and + attribution notices from the Source form of the Work, + excluding those notices that do not pertain to any part of + the Derivative Works; and + + (d) If the Work includes a "NOTICE" text file as part of its + distribution, then any Derivative Works that You distribute must + include a readable copy of the attribution notices contained + within such NOTICE file, excluding those notices that do not + pertain to any part of the Derivative Works, in at least one + of the following places: within a NOTICE text file distributed + as part of the Derivative Works; within the Source form or + documentation, if provided along with the Derivative Works; or, + within a display generated by the Derivative Works, if and + wherever such third-party notices normally appear. The contents + of the NOTICE file are for informational purposes only and + do not modify the License. You may add Your own attribution + notices within Derivative Works that You distribute, alongside + or as an addendum to the NOTICE text from the Work, provided + that such additional attribution notices cannot be construed + as modifying the License. + + You may add Your own copyright statement to Your modifications and + may provide additional or different license terms and conditions + for use, reproduction, or distribution of Your modifications, or + for any such Derivative Works as a whole, provided Your use, + reproduction, and distribution of the Work otherwise complies with + the conditions stated in this License. + + 5. Submission of Contributions. Unless You explicitly state otherwise, + any Contribution intentionally submitted for inclusion in the Work + by You to the Licensor shall be under the terms and conditions of + this License, without any additional terms or conditions. + Notwithstanding the above, nothing herein shall supersede or modify + the terms of any separate license agreement you may have executed + with Licensor regarding such Contributions. + + 6. Trademarks. This License does not grant permission to use the trade + names, trademarks, service marks, or product names of the Licensor, + except as required for reasonable and customary use in describing the + origin of the Work and reproducing the content of the NOTICE file. + + 7. Disclaimer of Warranty. Unless required by applicable law or + agreed to in writing, Licensor provides the Work (and each + Contributor provides its Contributions) on an "AS IS" BASIS, + WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or + implied, including, without limitation, any warranties or conditions + of TITLE, NON-INFRINGEMENT, MERCHANTABILITY, or FITNESS FOR A + PARTICULAR PURPOSE. You are solely responsible for determining the + appropriateness of using or redistributing the Work and assume any + risks associated with Your exercise of permissions under this License. + + 8. Limitation of Liability. In no event and under no legal theory, + whether in tort (including negligence), contract, or otherwise, + unless required by applicable law (such as deliberate and grossly + negligent acts) or agreed to in writing, shall any Contributor be + liable to You for damages, including any direct, indirect, special, + incidental, or consequential damages of any character arising as a + result of this License or out of the use or inability to use the + Work (including but not limited to damages for loss of goodwill, + work stoppage, computer failure or malfunction, or any and all + other commercial damages or losses), even if such Contributor + has been advised of the possibility of such damages. + + 9. Accepting Warranty or Additional Liability. While redistributing + the Work or Derivative Works thereof, You may choose to offer, + and charge a fee for, acceptance of support, warranty, indemnity, + or other liability obligations and/or rights consistent with this + License. However, in accepting such obligations, You may act only + on Your own behalf and on Your sole responsibility, not on behalf + of any other Contributor, and only if You agree to indemnify, + defend, and hold each Contributor harmless for any liability + incurred by, or claims asserted against, such Contributor by reason + of your accepting any such warranty or additional liability. + + END OF TERMS AND CONDITIONS + + APPENDIX: How to apply the Apache License to your work. + + To apply the Apache License to your work, attach the following + boilerplate notice, with the fields enclosed by brackets "[]" + replaced with your own identifying information. (Don't include + the brackets!) The text should be enclosed in the appropriate + comment syntax for the file format. We also recommend that a + file or class name and description of purpose be included on the + same "printed page" as the copyright notice for easier + identification within third-party archives. + + Copyright [yyyy] [name of copyright owner] + + Licensed under the Apache License, Version 2.0 (the "License"); + you may not use this file except in compliance with the License. + You may obtain a copy of the License at + + http://www.apache.org/licenses/LICENSE-2.0 + + Unless required by applicable law or agreed to in writing, software + distributed under the License is distributed on an "AS IS" BASIS, + WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + See the License for the specific language governing permissions and + limitations under the License. diff --git a/js/assets/fonts/RobotoMono/font.css b/js/assets/fonts/RobotoMono/font.css new file mode 100644 index 000000000..5e9ab5721 --- /dev/null +++ b/js/assets/fonts/RobotoMono/font.css @@ -0,0 +1,56 @@ +/* cyrillic-ext */ +@font-face { + font-family: 'Roboto Mono'; + font-style: normal; + font-weight: 300; + src: local('Roboto Mono Light'), local('RobotoMono-Light'), url(v4/N4duVc9C58uwPiY8_59Fz0ExlR2MysFCBK8OirNw2kM.woff2) format('woff2'); + unicode-range: U+0460-052F, U+20B4, U+2DE0-2DFF, U+A640-A69F; +} +/* cyrillic */ +@font-face { + font-family: 'Roboto Mono'; + font-style: normal; + font-weight: 300; + src: local('Roboto Mono Light'), local('RobotoMono-Light'), url(v4/N4duVc9C58uwPiY8_59Fz2dsm03krrxlabhmVQFB99s.woff2) format('woff2'); + unicode-range: U+0400-045F, U+0490-0491, U+04B0-04B1, U+2116; +} +/* greek-ext */ +@font-face { + font-family: 'Roboto Mono'; + font-style: normal; + font-weight: 300; + src: local('Roboto Mono Light'), local('RobotoMono-Light'), url(v4/N4duVc9C58uwPiY8_59FzyJ0caWjaSBdV-xZbEgst_k.woff2) format('woff2'); + unicode-range: U+1F00-1FFF; +} +/* greek */ +@font-face { + font-family: 'Roboto Mono'; + font-style: normal; + font-weight: 300; + src: local('Roboto Mono Light'), local('RobotoMono-Light'), url(v4/N4duVc9C58uwPiY8_59Fz2MSHb9EAJwuSzGfuRChQzQ.woff2) format('woff2'); + unicode-range: U+0370-03FF; +} +/* vietnamese */ +@font-face { + font-family: 'Roboto Mono'; + font-style: normal; + font-weight: 300; + src: local('Roboto Mono Light'), local('RobotoMono-Light'), url(v4/N4duVc9C58uwPiY8_59Fz-pRBTtN4E2_qSPBnw6AgMc.woff2) format('woff2'); + unicode-range: U+0102-0103, U+1EA0-1EF9, U+20AB; +} +/* latin-ext */ +@font-face { + font-family: 'Roboto Mono'; + font-style: normal; + font-weight: 300; + src: local('Roboto Mono Light'), local('RobotoMono-Light'), url(v4/N4duVc9C58uwPiY8_59Fz9Dnm4qiMZlH5rhYv_7LI2Y.woff2) format('woff2'); + unicode-range: U+0100-024F, U+1E00-1EFF, U+20A0-20AB, U+20AD-20CF, U+2C60-2C7F, U+A720-A7FF; +} +/* latin */ +@font-face { + font-family: 'Roboto Mono'; + font-style: normal; + font-weight: 300; + src: local('Roboto Mono Light'), local('RobotoMono-Light'), url(v4/N4duVc9C58uwPiY8_59Fz9TIkQYohD4BpHvJ3NvbHoA.woff2) format('woff2'); + unicode-range: U+0000-00FF, U+0131, U+0152-0153, U+02C6, U+02DA, U+02DC, U+2000-206F, U+2074, U+20AC, U+2212, U+2215, U+E0FF, U+EFFD, U+F000; +} diff --git a/js/assets/fonts/RobotoMono/ttf/RobotoMono-Bold.ttf b/js/assets/fonts/RobotoMono/ttf/RobotoMono-Bold.ttf new file mode 100755 index 000000000..07ef607d5 Binary files /dev/null and b/js/assets/fonts/RobotoMono/ttf/RobotoMono-Bold.ttf differ diff --git a/js/assets/fonts/RobotoMono/ttf/RobotoMono-BoldItalic.ttf b/js/assets/fonts/RobotoMono/ttf/RobotoMono-BoldItalic.ttf new file mode 100755 index 000000000..1cca0bf45 Binary files /dev/null and b/js/assets/fonts/RobotoMono/ttf/RobotoMono-BoldItalic.ttf differ diff --git a/js/assets/fonts/RobotoMono/ttf/RobotoMono-Italic.ttf b/js/assets/fonts/RobotoMono/ttf/RobotoMono-Italic.ttf new file mode 100755 index 000000000..ef92c372c Binary files /dev/null and b/js/assets/fonts/RobotoMono/ttf/RobotoMono-Italic.ttf differ diff --git a/js/assets/fonts/RobotoMono/ttf/RobotoMono-Light.ttf b/js/assets/fonts/RobotoMono/ttf/RobotoMono-Light.ttf new file mode 100755 index 000000000..63229b280 Binary files /dev/null and b/js/assets/fonts/RobotoMono/ttf/RobotoMono-Light.ttf differ diff --git a/js/assets/fonts/RobotoMono/ttf/RobotoMono-LightItalic.ttf b/js/assets/fonts/RobotoMono/ttf/RobotoMono-LightItalic.ttf new file mode 100755 index 000000000..f25bed56a Binary files /dev/null and b/js/assets/fonts/RobotoMono/ttf/RobotoMono-LightItalic.ttf differ diff --git a/js/assets/fonts/RobotoMono/ttf/RobotoMono-Medium.ttf b/js/assets/fonts/RobotoMono/ttf/RobotoMono-Medium.ttf new file mode 100755 index 000000000..88ff0c15a Binary files /dev/null and b/js/assets/fonts/RobotoMono/ttf/RobotoMono-Medium.ttf differ diff --git a/js/assets/fonts/RobotoMono/ttf/RobotoMono-MediumItalic.ttf b/js/assets/fonts/RobotoMono/ttf/RobotoMono-MediumItalic.ttf new file mode 100755 index 000000000..307efad8f Binary files /dev/null and b/js/assets/fonts/RobotoMono/ttf/RobotoMono-MediumItalic.ttf differ diff --git a/js/assets/fonts/RobotoMono/ttf/RobotoMono-Regular.ttf b/js/assets/fonts/RobotoMono/ttf/RobotoMono-Regular.ttf new file mode 100755 index 000000000..b158a334e Binary files /dev/null and b/js/assets/fonts/RobotoMono/ttf/RobotoMono-Regular.ttf differ diff --git a/js/assets/fonts/RobotoMono/ttf/RobotoMono-Thin.ttf b/js/assets/fonts/RobotoMono/ttf/RobotoMono-Thin.ttf new file mode 100755 index 000000000..309484d32 Binary files /dev/null and b/js/assets/fonts/RobotoMono/ttf/RobotoMono-Thin.ttf differ diff --git a/js/assets/fonts/RobotoMono/ttf/RobotoMono-ThinItalic.ttf b/js/assets/fonts/RobotoMono/ttf/RobotoMono-ThinItalic.ttf new file mode 100755 index 000000000..e1bb9121e Binary files /dev/null and b/js/assets/fonts/RobotoMono/ttf/RobotoMono-ThinItalic.ttf differ diff --git a/js/assets/fonts/RobotoMono/v4/N4duVc9C58uwPiY8_59Fz-pRBTtN4E2_qSPBnw6AgMc.woff2 b/js/assets/fonts/RobotoMono/v4/N4duVc9C58uwPiY8_59Fz-pRBTtN4E2_qSPBnw6AgMc.woff2 new file mode 100644 index 000000000..f1fdb9352 Binary files /dev/null and b/js/assets/fonts/RobotoMono/v4/N4duVc9C58uwPiY8_59Fz-pRBTtN4E2_qSPBnw6AgMc.woff2 differ diff --git a/js/assets/fonts/RobotoMono/v4/N4duVc9C58uwPiY8_59Fz0ExlR2MysFCBK8OirNw2kM.woff2 b/js/assets/fonts/RobotoMono/v4/N4duVc9C58uwPiY8_59Fz0ExlR2MysFCBK8OirNw2kM.woff2 new file mode 100644 index 000000000..501a4d777 Binary files /dev/null and b/js/assets/fonts/RobotoMono/v4/N4duVc9C58uwPiY8_59Fz0ExlR2MysFCBK8OirNw2kM.woff2 differ diff --git a/js/assets/fonts/RobotoMono/v4/N4duVc9C58uwPiY8_59Fz2MSHb9EAJwuSzGfuRChQzQ.woff2 b/js/assets/fonts/RobotoMono/v4/N4duVc9C58uwPiY8_59Fz2MSHb9EAJwuSzGfuRChQzQ.woff2 new file mode 100644 index 000000000..54484b1e3 Binary files /dev/null and b/js/assets/fonts/RobotoMono/v4/N4duVc9C58uwPiY8_59Fz2MSHb9EAJwuSzGfuRChQzQ.woff2 differ diff --git a/js/assets/fonts/RobotoMono/v4/N4duVc9C58uwPiY8_59Fz2dsm03krrxlabhmVQFB99s.woff2 b/js/assets/fonts/RobotoMono/v4/N4duVc9C58uwPiY8_59Fz2dsm03krrxlabhmVQFB99s.woff2 new file mode 100644 index 000000000..c3f35ca29 Binary files /dev/null and b/js/assets/fonts/RobotoMono/v4/N4duVc9C58uwPiY8_59Fz2dsm03krrxlabhmVQFB99s.woff2 differ diff --git a/js/assets/fonts/RobotoMono/v4/N4duVc9C58uwPiY8_59Fz9Dnm4qiMZlH5rhYv_7LI2Y.woff2 b/js/assets/fonts/RobotoMono/v4/N4duVc9C58uwPiY8_59Fz9Dnm4qiMZlH5rhYv_7LI2Y.woff2 new file mode 100644 index 000000000..72a43ab3c Binary files /dev/null and b/js/assets/fonts/RobotoMono/v4/N4duVc9C58uwPiY8_59Fz9Dnm4qiMZlH5rhYv_7LI2Y.woff2 differ diff --git a/js/assets/fonts/RobotoMono/v4/N4duVc9C58uwPiY8_59Fz9TIkQYohD4BpHvJ3NvbHoA.woff2 b/js/assets/fonts/RobotoMono/v4/N4duVc9C58uwPiY8_59Fz9TIkQYohD4BpHvJ3NvbHoA.woff2 new file mode 100644 index 000000000..62b3a15f6 Binary files /dev/null and b/js/assets/fonts/RobotoMono/v4/N4duVc9C58uwPiY8_59Fz9TIkQYohD4BpHvJ3NvbHoA.woff2 differ diff --git a/js/assets/fonts/RobotoMono/v4/N4duVc9C58uwPiY8_59FzyJ0caWjaSBdV-xZbEgst_k.woff2 b/js/assets/fonts/RobotoMono/v4/N4duVc9C58uwPiY8_59FzyJ0caWjaSBdV-xZbEgst_k.woff2 new file mode 100644 index 000000000..2d4fda538 Binary files /dev/null and b/js/assets/fonts/RobotoMono/v4/N4duVc9C58uwPiY8_59FzyJ0caWjaSBdV-xZbEgst_k.woff2 differ diff --git a/js/assets/images/contracts/augur-68x68.png b/js/assets/images/contracts/augur-68x68.png new file mode 100644 index 000000000..962c3acf2 Binary files /dev/null and b/js/assets/images/contracts/augur-68x68.png differ diff --git a/js/assets/images/contracts/beer-64x64.png b/js/assets/images/contracts/beer-64x64.png new file mode 100644 index 000000000..04e91e459 Binary files /dev/null and b/js/assets/images/contracts/beer-64x64.png differ diff --git a/js/assets/images/contracts/beer.png b/js/assets/images/contracts/beer.png new file mode 100644 index 000000000..dd0125fc8 Binary files /dev/null and b/js/assets/images/contracts/beer.png differ diff --git a/js/assets/images/contracts/dgd-64x64.png b/js/assets/images/contracts/dgd-64x64.png new file mode 100644 index 000000000..fa1bd308f Binary files /dev/null and b/js/assets/images/contracts/dgd-64x64.png differ diff --git a/js/assets/images/contracts/dgd.png b/js/assets/images/contracts/dgd.png new file mode 100644 index 000000000..09b2831af Binary files /dev/null and b/js/assets/images/contracts/dgd.png differ diff --git a/js/assets/images/contracts/dgx-64x64.png b/js/assets/images/contracts/dgx-64x64.png new file mode 100644 index 000000000..c81c3387c Binary files /dev/null and b/js/assets/images/contracts/dgx-64x64.png differ diff --git a/js/assets/images/contracts/dgx.png b/js/assets/images/contracts/dgx.png new file mode 100644 index 000000000..9ac5dff1b Binary files /dev/null and b/js/assets/images/contracts/dgx.png differ diff --git a/js/assets/images/contracts/ethereum-black-64x64.png b/js/assets/images/contracts/ethereum-black-64x64.png new file mode 100644 index 000000000..8e80e4ff1 Binary files /dev/null and b/js/assets/images/contracts/ethereum-black-64x64.png differ diff --git a/js/assets/images/contracts/ethereum-black.png b/js/assets/images/contracts/ethereum-black.png new file mode 100644 index 000000000..d7bdc4c90 Binary files /dev/null and b/js/assets/images/contracts/ethereum-black.png differ diff --git a/js/assets/images/contracts/ethereum-white.png b/js/assets/images/contracts/ethereum-white.png new file mode 100644 index 000000000..0691d09ae Binary files /dev/null and b/js/assets/images/contracts/ethereum-white.png differ diff --git a/js/assets/images/contracts/firstblood-64x64.png b/js/assets/images/contracts/firstblood-64x64.png new file mode 100644 index 000000000..338ce6b28 Binary files /dev/null and b/js/assets/images/contracts/firstblood-64x64.png differ diff --git a/js/assets/images/contracts/firstblood.png b/js/assets/images/contracts/firstblood.png new file mode 100644 index 000000000..8694cb6eb Binary files /dev/null and b/js/assets/images/contracts/firstblood.png differ diff --git a/js/assets/images/contracts/gavcoin-64x64.png b/js/assets/images/contracts/gavcoin-64x64.png new file mode 100644 index 000000000..13b25a341 Binary files /dev/null and b/js/assets/images/contracts/gavcoin-64x64.png differ diff --git a/js/assets/images/contracts/gavcoin.png b/js/assets/images/contracts/gavcoin.png new file mode 100644 index 000000000..20e686f20 Binary files /dev/null and b/js/assets/images/contracts/gavcoin.png differ diff --git a/js/assets/images/contracts/maker.svg b/js/assets/images/contracts/maker.svg new file mode 100644 index 000000000..3c2c6e634 --- /dev/null +++ b/js/assets/images/contracts/maker.svg @@ -0,0 +1,18 @@ + + + + logo-maker + Created with Sketch. + + + + + + + + + + + + + \ No newline at end of file diff --git a/js/assets/images/contracts/pluton.svg b/js/assets/images/contracts/pluton.svg new file mode 100644 index 000000000..ecbcee82e --- /dev/null +++ b/js/assets/images/contracts/pluton.svg @@ -0,0 +1,157 @@ + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + diff --git a/js/assets/images/contracts/singular.svg b/js/assets/images/contracts/singular.svg new file mode 100644 index 000000000..a735ca7e1 --- /dev/null +++ b/js/assets/images/contracts/singular.svg @@ -0,0 +1,3 @@ + diff --git a/js/assets/images/contracts/unicorn-64x64.png b/js/assets/images/contracts/unicorn-64x64.png new file mode 100644 index 000000000..be9dacf33 Binary files /dev/null and b/js/assets/images/contracts/unicorn-64x64.png differ diff --git a/js/assets/images/contracts/unicorn.png b/js/assets/images/contracts/unicorn.png new file mode 100644 index 000000000..b94d476cb Binary files /dev/null and b/js/assets/images/contracts/unicorn.png differ diff --git a/js/assets/images/contracts/unknown-64x64.png b/js/assets/images/contracts/unknown-64x64.png new file mode 100644 index 000000000..c2b39a80a Binary files /dev/null and b/js/assets/images/contracts/unknown-64x64.png differ diff --git a/js/assets/images/contracts/unknown.png b/js/assets/images/contracts/unknown.png new file mode 100644 index 000000000..204ffa486 Binary files /dev/null and b/js/assets/images/contracts/unknown.png differ diff --git a/js/assets/images/dapps/blocks-350.jpg b/js/assets/images/dapps/blocks-350.jpg new file mode 100644 index 000000000..524f9267c Binary files /dev/null and b/js/assets/images/dapps/blocks-350.jpg differ diff --git a/js/assets/images/dapps/coins-64x64.jpg b/js/assets/images/dapps/coins-64x64.jpg new file mode 100644 index 000000000..987d7400e Binary files /dev/null and b/js/assets/images/dapps/coins-64x64.jpg differ diff --git a/js/assets/images/dapps/coins.jpg b/js/assets/images/dapps/coins.jpg new file mode 100644 index 000000000..8fba5e65c Binary files /dev/null and b/js/assets/images/dapps/coins.jpg differ diff --git a/js/assets/images/dapps/hex-64x64.jpg b/js/assets/images/dapps/hex-64x64.jpg new file mode 100644 index 000000000..8c6a1d5cc Binary files /dev/null and b/js/assets/images/dapps/hex-64x64.jpg differ diff --git a/js/assets/images/dapps/hex.jpg b/js/assets/images/dapps/hex.jpg new file mode 100644 index 000000000..8e09f49aa Binary files /dev/null and b/js/assets/images/dapps/hex.jpg differ diff --git a/js/assets/images/dapps/interlock-64x64.png b/js/assets/images/dapps/interlock-64x64.png new file mode 100644 index 000000000..f4b342489 Binary files /dev/null and b/js/assets/images/dapps/interlock-64x64.png differ diff --git a/js/assets/images/dapps/interlock.png b/js/assets/images/dapps/interlock.png new file mode 100644 index 000000000..e8aeb275d Binary files /dev/null and b/js/assets/images/dapps/interlock.png differ diff --git a/js/assets/images/dapps/link-64x64.jpg b/js/assets/images/dapps/link-64x64.jpg new file mode 100644 index 000000000..14f9c1190 Binary files /dev/null and b/js/assets/images/dapps/link-64x64.jpg differ diff --git a/js/assets/images/dapps/link.jpg b/js/assets/images/dapps/link.jpg new file mode 100644 index 000000000..546c67e20 Binary files /dev/null and b/js/assets/images/dapps/link.jpg differ diff --git a/js/assets/images/dapps/register-64x64.jpg b/js/assets/images/dapps/register-64x64.jpg new file mode 100644 index 000000000..ff8fc6c2f Binary files /dev/null and b/js/assets/images/dapps/register-64x64.jpg differ diff --git a/js/assets/images/dapps/register.jpg b/js/assets/images/dapps/register.jpg new file mode 100644 index 000000000..cd47bd81e Binary files /dev/null and b/js/assets/images/dapps/register.jpg differ diff --git a/js/assets/images/dapps/signature.png b/js/assets/images/dapps/signature.png new file mode 100644 index 000000000..36b341a03 Binary files /dev/null and b/js/assets/images/dapps/signature.png differ diff --git a/js/assets/images/ethcore-block-blue.png b/js/assets/images/ethcore-block-blue.png new file mode 100644 index 000000000..fd93635ec Binary files /dev/null and b/js/assets/images/ethcore-block-blue.png differ diff --git a/js/assets/images/ethcore-block.png b/js/assets/images/ethcore-block.png new file mode 100644 index 000000000..bac46e5c9 Binary files /dev/null and b/js/assets/images/ethcore-block.png differ diff --git a/js/assets/images/ethcore-logo-white-square.png b/js/assets/images/ethcore-logo-white-square.png new file mode 100644 index 000000000..a300b5a0a Binary files /dev/null and b/js/assets/images/ethcore-logo-white-square.png differ diff --git a/js/assets/images/parity-x56.png b/js/assets/images/parity-x56.png new file mode 100644 index 000000000..e303427a4 Binary files /dev/null and b/js/assets/images/parity-x56.png differ diff --git a/js/assets/images/parity.xcf b/js/assets/images/parity.xcf new file mode 100644 index 000000000..37fa206c0 Binary files /dev/null and b/js/assets/images/parity.xcf differ diff --git a/js/assets/images/paritybar.png b/js/assets/images/paritybar.png new file mode 100644 index 000000000..f0b0e1457 Binary files /dev/null and b/js/assets/images/paritybar.png differ diff --git a/js/assets/images/shapeshift-btn.png b/js/assets/images/shapeshift-btn.png new file mode 100644 index 000000000..1650ecf37 Binary files /dev/null and b/js/assets/images/shapeshift-btn.png differ diff --git a/js/assets/images/shapeshift-logo.png b/js/assets/images/shapeshift-logo.png new file mode 100644 index 000000000..586a01ef1 Binary files /dev/null and b/js/assets/images/shapeshift-logo.png differ diff --git a/js/build-server.js b/js/build-server.js new file mode 100644 index 000000000..797e89183 --- /dev/null +++ b/js/build-server.js @@ -0,0 +1,40 @@ +// Copyright 2015, 2016 Ethcore (UK) Ltd. +// This file is part of Parity. + +// Parity is free software: you can redistribute it and/or modify +// it under the terms of the GNU General Public License as published by +// the Free Software Foundation, either version 3 of the License, or +// (at your option) any later version. + +// Parity is distributed in the hope that it will be useful, +// but WITHOUT ANY WARRANTY; without even the implied warranty of +// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the +// GNU General Public License for more details. + +// You should have received a copy of the GNU General Public License +// along with Parity. If not, see . + +/** + * Run `PARITY_URL="127.0.0.1:8180" NODE_ENV="production" npm run build` + * to build the project ; use this server to test that the minifed + * version is working (this is a simple proxy server) + */ + +var express = require('express'); +var proxy = require('http-proxy-middleware'); + +var app = express(); + +app.use(express.static('build')); + +app.use('/api/*', proxy({ + target: 'http://127.0.0.1:8080', + changeOrigin: true +})); + +app.use('/rpc/*', proxy({ + target: 'http://127.0.0.1:8080', + changeOrigin: true +})); + +app.listen(3000); diff --git a/js/package.json b/js/package.json new file mode 100644 index 000000000..04c29ac7a --- /dev/null +++ b/js/package.json @@ -0,0 +1,140 @@ +{ + "name": "parity.js", + "version": "0.0.1", + "main": "release/index.js", + "jsnext:main": "src/index.js", + "author": "Ethcore Team ", + "maintainers": [ + "Jaco Greeff" + ], + "contributors": [], + "license": "GPL-3.0", + "repository": { + "type": "git", + "url": "git+https://github.com/ethcore/parity.js.git" + }, + "keywords": [ + "Ethereum", + "ABI", + "API", + "Web3", + "RPC", + "Parity", + "Promise" + ], + "scripts": { + "build": "npm run build:dll && npm run build:app", + "build:app": "webpack --progress", + "build:dll": "webpack --config webpack.vendor.js --progress", + "ci:build": "npm run ci:build:dll && npm run ci:build:app", + "ci:build:app": "NODE_ENV=production webpack", + "ci:build:dll": "NODE_ENV=production webpack --config webpack.vendor.js", + "clean": "rm -rf ./build ./coverage", + "coveralls": "npm run testCoverage && coveralls < coverage/lcov.info", + "lint": "eslint --ignore-path .gitignore ./src/", + "start": "npm install && npm run build:dll && npm run start:app", + "start:app": "webpack-dev-server -d --history-api-fallback --open --hot --inline --progress --colors --port 3000", + "test": "mocha 'src/**/*.spec.js'", + "test:coverage": "istanbul cover _mocha -- 'src/**/*.spec.js'", + "test:e2e": "mocha 'src/**/*.e2e.js'" + }, + "devDependencies": { + "babel-cli": "^6.10.1", + "babel-core": "^6.10.4", + "babel-eslint": "^6.1.2", + "babel-loader": "^6.2.3", + "babel-plugin-lodash": "^3.2.2", + "babel-plugin-transform-class-properties": "^6.11.5", + "babel-plugin-transform-decorators-legacy": "^1.3.4", + "babel-plugin-transform-react-remove-prop-types": "^0.2.9", + "babel-plugin-transform-runtime": "^6.9.0", + "babel-polyfill": "^6.13.0", + "babel-preset-es2015": "^6.9.0", + "babel-preset-es2015-rollup": "^1.1.1", + "babel-preset-es2016": "^6.11.3", + "babel-preset-es2017": "^6.14.0", + "babel-preset-react": "^6.5.0", + "babel-preset-stage-0": "^6.5.0", + "babel-register": "6.9.0", + "babel-runtime": "^6.9.2", + "chai": "^3.5.0", + "chai-enzyme": "0.4.2", + "cheerio": "0.20.0", + "copy-webpack-plugin": "^3.0.1", + "core-js": "^2.4.1", + "coveralls": "^2.11.11", + "css-loader": "^0.23.1", + "enzyme": "2.3.0", + "eslint": "^3.1.0", + "eslint-config-semistandard": "^6.0.2", + "eslint-config-standard": "^5.3.5", + "eslint-config-standard-react": "^3.0.0", + "eslint-plugin-promise": "^2.0.0", + "eslint-plugin-react": "^5.1.1", + "eslint-plugin-standard": "^2.0.0", + "extract-text-webpack-plugin": "^1.0.1", + "file-loader": "^0.8.5", + "happypack": "^2.2.1", + "history": "^2.0.0", + "html-loader": "^0.4.3", + "ignore-styles": "2.0.0", + "image-webpack-loader": "^1.8.0", + "istanbul": "^1.0.0-alpha.2", + "jsdom": "9.2.1", + "json-loader": "^0.5.4", + "less": "^2.7.1", + "less-loader": "^2.2.3", + "mocha": "^3.0.0-1", + "mock-local-storage": "1.0.2", + "mock-socket": "^3.0.1", + "nock": "^8.0.0", + "postcss-import": "^8.1.2", + "postcss-loader": "^0.8.1", + "postcss-simple-vars": "^3.0.0", + "react-addons-test-utils": "^15.3.0", + "react-copy-to-clipboard": "^4.2.3", + "react-hot-loader": "^1.3.0", + "rucksack-css": "^0.8.6", + "sinon": "^1.17.4", + "sinon-as-promised": "^4.0.2", + "sinon-chai": "^2.8.0", + "style-loader": "^0.13.0", + "url-loader": "^0.5.7", + "webpack": "^1.13.2", + "webpack-dev-server": "^1.15.2", + "webpack-error-notification": "0.1.6", + "webpack-hot-middleware": "^2.7.1", + "websocket": "^1.0.23" + }, + "dependencies": { + "bignumber.js": "^2.3.0", + "blockies": "0.0.2", + "bytes": "^2.4.0", + "es6-promise": "^3.2.1", + "format-json": "^1.0.3", + "format-number": "^2.0.1", + "geopattern": "^1.2.3", + "isomorphic-fetch": "^2.2.1", + "js-sha3": "^0.5.2", + "lodash": "^4.11.1", + "marked": "^0.3.6", + "material-ui": "^0.15.4", + "moment": "^2.14.1", + "react": "^15.2.1", + "react-addons-css-transition-group": "^15.2.1", + "react-dom": "^15.2.1", + "react-redux": "^4.4.5", + "react-router": "^2.6.1", + "react-router-redux": "^4.0.5", + "react-tap-event-plugin": "^1.0.0", + "react-tooltip": "^2.0.3", + "redux": "^3.5.2", + "redux-actions": "^0.10.1", + "redux-thunk": "^2.1.0", + "rlp": "^2.0.0", + "store": "^1.3.20", + "utf8": "^2.1.1", + "validator": "^5.7.0", + "whatwg-fetch": "^1.0.0" + } +} diff --git a/js/scripts/build.sh b/js/scripts/build.sh new file mode 100755 index 000000000..07487ffd5 --- /dev/null +++ b/js/scripts/build.sh @@ -0,0 +1,15 @@ +#!/bin/bash + +# change into the js directory (one down from scripts) +pushd `dirname $0` +cd .. + +# run build (production) and store the exit code +EXITCODE=0 +npm run ci:build || EXITCODE=1 + +# back to root +popd + +# exit with exit code +exit $EXITCODE diff --git a/js/scripts/install-deps.sh b/js/scripts/install-deps.sh new file mode 100755 index 000000000..67113f27e --- /dev/null +++ b/js/scripts/install-deps.sh @@ -0,0 +1,15 @@ +#!/bin/bash + +# change into the js directory (one down from scripts) +pushd `dirname $0` +cd .. + +# install deps and store the exit code +EXITCODE=0 +npm install --progress=false || EXITCODE=1 + +# back to root +popd + +# exit with exit code +exit $EXITCODE diff --git a/js/scripts/lint.sh b/js/scripts/lint.sh new file mode 100755 index 000000000..147bb0a2d --- /dev/null +++ b/js/scripts/lint.sh @@ -0,0 +1,15 @@ +#!/bin/bash + +# change into the js directory (one down from scripts) +pushd `dirname $0` +cd .. + +# run lint & tests and store the exit code +EXITCODE=0 +npm run lint || EXITCODE=1 + +# back to root +popd + +# exit with exit code +exit $EXITCODE diff --git a/js/scripts/release.sh b/js/scripts/release.sh new file mode 100755 index 000000000..05dc1d784 --- /dev/null +++ b/js/scripts/release.sh @@ -0,0 +1,33 @@ +#!/bin/bash + +# change into the build directory +pushd `dirname $0` +cd ../.build + +# variables +UTCDATE=`date -u "+%Y%m%d-%H%M%S"` + +# init git +rm -rf ./.git +git init + +# our user details +git config push.default simple +git config merge.ours.driver true +git config user.email "jaco+gitlab@ethcore.io" +git config user.name "GitLab Build Bot" + +# add local files and send it up +git remote add origin https://${GITHUB_JS_PRECOMPILED}:@github.com/ethcore/js-precompiled.git +git fetch origin +git checkout -b $CI_BUILD_REF_NAME +git add . +git commit -m "$UTCDATE [compiled]" +git merge origin/$CI_BUILD_REF_NAME -X ours --commit -m "$UTCDATE [release]" +git push origin $CI_BUILD_REF_NAME + +# back to root +popd + +# exit with exit code +exit 0 diff --git a/js/scripts/test.sh b/js/scripts/test.sh new file mode 100755 index 000000000..6827b243c --- /dev/null +++ b/js/scripts/test.sh @@ -0,0 +1,15 @@ +#!/bin/bash + +# change into the js directory (one down from scripts) +pushd `dirname $0` +cd .. + +# run lint & tests and store the exit code +EXITCODE=0 +npm run test || EXITCODE=1 + +# back to root +popd + +# exit with exit code +exit $EXITCODE diff --git a/js/scripts/update-precompiled.sh b/js/scripts/update-precompiled.sh new file mode 100755 index 000000000..2d1fa9396 --- /dev/null +++ b/js/scripts/update-precompiled.sh @@ -0,0 +1,18 @@ +#!/bin/bash + + +# change into the submodule build directory +pushd `dirname $0` +cd ../build + +if [ -z "$1" ]; then + popd + echo "Usage: $0 " + exit 1 +fi + +git fetch origin $1 +git merge $1 -X theirs + +popd +exit 0 diff --git a/js/src/3rdparty/etherscan/account.js b/js/src/3rdparty/etherscan/account.js new file mode 100644 index 000000000..596e6d9cb --- /dev/null +++ b/js/src/3rdparty/etherscan/account.js @@ -0,0 +1,73 @@ +// Copyright 2015, 2016 Ethcore (UK) Ltd. +// This file is part of Parity. + +// Parity is free software: you can redistribute it and/or modify +// it under the terms of the GNU General Public License as published by +// the Free Software Foundation, either version 3 of the License, or +// (at your option) any later version. + +// Parity is distributed in the hope that it will be useful, +// but WITHOUT ANY WARRANTY; without even the implied warranty of +// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the +// GNU General Public License for more details. + +// You should have received a copy of the GNU General Public License +// along with Parity. If not, see . + +const PAGE_SIZE = 25; + +import util from '../../api/util'; +import { call } from './call'; + +function _call (method, params, test) { + return call('account', method, params, test); +} + +function balance (address, test = false) { + return _call('balance', { + address: address, + tag: 'latest' + }, test).then((balance) => { + // same format as balancemulti below + return { + account: address, + balance: balance + }; + }); +} + +function balances (addresses, test = false) { + return _call('balancemulti', { + address: addresses.join(','), + tag: 'latest' + }, test); +} + +function transactions (address, page, test = false) { + // page offset from 0 + return _call('txlist', { + address: address, + page: (page || 0) + 1, + offset: PAGE_SIZE, + sort: 'desc' + }, test).then((transactions) => { + return transactions.map((tx) => { + return { + from: util.toChecksumAddress(tx.from), + to: util.toChecksumAddress(tx.to), + hash: tx.hash, + blockNumber: tx.blockNumber, + timeStamp: tx.timeStamp, + value: tx.value + }; + }); + }); +} + +const account = { + balance: balance, + balances: balances, + transactions: transactions +}; + +export { account }; diff --git a/js/src/3rdparty/etherscan/account.spec.js b/js/src/3rdparty/etherscan/account.spec.js new file mode 100644 index 000000000..283fab3d2 --- /dev/null +++ b/js/src/3rdparty/etherscan/account.spec.js @@ -0,0 +1,69 @@ +// Copyright 2015, 2016 Ethcore (UK) Ltd. +// This file is part of Parity. + +// Parity is free software: you can redistribute it and/or modify +// it under the terms of the GNU General Public License as published by +// the Free Software Foundation, either version 3 of the License, or +// (at your option) any later version. + +// Parity is distributed in the hope that it will be useful, +// but WITHOUT ANY WARRANTY; without even the implied warranty of +// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the +// GNU General Public License for more details. + +// You should have received a copy of the GNU General Public License +// along with Parity. If not, see . + +import etherscan from './'; + +const TESTADDR = '0xbf885e2b55c6bcc84556a3c5f07d3040833c8d00'; + +describe.skip('etherscan/account', () => { + const checkBalance = function (balance, addr) { + expect(balance).to.be.ok; + expect(balance.account).to.equal(addr); + expect(balance.balance).to.be.ok; + }; + + it('retrieves an account balance', () => { + return etherscan.account + .balance(TESTADDR) + .then((balance) => { + checkBalance(balance, TESTADDR); + }); + }); + + it('retrieves multi account balances', () => { + const addresses = ['0xde0b295669a9fd93d5f28d9ec85e40f4cb697bae', TESTADDR]; + + return etherscan.account + .balances(addresses) + .then((balances) => { + expect(balances).to.be.ok; + expect(balances.length).to.equal(2); + balances.forEach((balance, idx) => { + checkBalance(balance, addresses[idx]); + }); + }); + }); + + describe('transactions', () => { + it('retrieves a list of transactions (default)', () => { + return etherscan.account + .transactions(TESTADDR) + .then((transactions) => { + expect(transactions).to.be.ok; + expect(transactions.length).to.equal(25); + }); + }); + + it('retrieves a list of transactions (page 1)', () => { + return etherscan.account + .transactions(TESTADDR, 1) + .then((transactions) => { + expect(transactions).to.be.ok; + expect(transactions.length).to.equal(25); + }); + }); + }); +}); diff --git a/js/src/3rdparty/etherscan/call.js b/js/src/3rdparty/etherscan/call.js new file mode 100644 index 000000000..1324bcc9d --- /dev/null +++ b/js/src/3rdparty/etherscan/call.js @@ -0,0 +1,51 @@ +// Copyright 2015, 2016 Ethcore (UK) Ltd. +// This file is part of Parity. + +// Parity is free software: you can redistribute it and/or modify +// it under the terms of the GNU General Public License as published by +// the Free Software Foundation, either version 3 of the License, or +// (at your option) any later version. + +// Parity is distributed in the hope that it will be useful, +// but WITHOUT ANY WARRANTY; without even the implied warranty of +// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the +// GNU General Public License for more details. + +// You should have received a copy of the GNU General Public License +// along with Parity. If not, see . + +const options = { + method: 'GET', + headers: { + 'Accept': 'application/json' + } +}; + +export function call (module, action, _params, test) { + const host = test ? 'testnet.etherscan.io' : 'api.etherscan.io'; + let params = ''; + + if (_params) { + Object.keys(_params).map((param) => { + const value = _params[param]; + + params = `${params}&${param}=${value}`; + }); + } + + return fetch(`http://${host}/api?module=${module}&action=${action}${params}`, options) + .then((response) => { + if (response.status !== 200) { + throw { code: response.status, message: response.statusText }; // eslint-disable-line + } + + return response.json(); + }) + .then((result) => { + if (result.message === 'NOTOK') { + throw { code: -1, message: result.result }; // eslint-disable-line + } + + return result.result; + }); +} diff --git a/js/src/3rdparty/etherscan/index.js b/js/src/3rdparty/etherscan/index.js new file mode 100644 index 000000000..55aeba473 --- /dev/null +++ b/js/src/3rdparty/etherscan/index.js @@ -0,0 +1,25 @@ +// Copyright 2015, 2016 Ethcore (UK) Ltd. +// This file is part of Parity. + +// Parity is free software: you can redistribute it and/or modify +// it under the terms of the GNU General Public License as published by +// the Free Software Foundation, either version 3 of the License, or +// (at your option) any later version. + +// Parity is distributed in the hope that it will be useful, +// but WITHOUT ANY WARRANTY; without even the implied warranty of +// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the +// GNU General Public License for more details. + +// You should have received a copy of the GNU General Public License +// along with Parity. If not, see . + +import { account } from './account'; +import { stats } from './stats'; + +const etherscan = { + account: account, + stats: stats +}; + +export default etherscan; diff --git a/js/src/3rdparty/etherscan/stats.js b/js/src/3rdparty/etherscan/stats.js new file mode 100644 index 000000000..ecefefe7d --- /dev/null +++ b/js/src/3rdparty/etherscan/stats.js @@ -0,0 +1,36 @@ +// Copyright 2015, 2016 Ethcore (UK) Ltd. +// This file is part of Parity. + +// Parity is free software: you can redistribute it and/or modify +// it under the terms of the GNU General Public License as published by +// the Free Software Foundation, either version 3 of the License, or +// (at your option) any later version. + +// Parity is distributed in the hope that it will be useful, +// but WITHOUT ANY WARRANTY; without even the implied warranty of +// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the +// GNU General Public License for more details. + +// You should have received a copy of the GNU General Public License +// along with Parity. If not, see . + +import { call } from './call'; + +function _call (action, test) { + return call('stats', action, null, test); +} + +function price (test = false) { + return _call('ethprice', test); +} + +function supply (test = false) { + return _call('ethsupply', test); +} + +const stats = { + price: price, + supply: supply +}; + +export { stats }; diff --git a/js/src/3rdparty/etherscan/stats.spec.js b/js/src/3rdparty/etherscan/stats.spec.js new file mode 100644 index 000000000..fde2b035c --- /dev/null +++ b/js/src/3rdparty/etherscan/stats.spec.js @@ -0,0 +1,35 @@ +// Copyright 2015, 2016 Ethcore (UK) Ltd. +// This file is part of Parity. + +// Parity is free software: you can redistribute it and/or modify +// it under the terms of the GNU General Public License as published by +// the Free Software Foundation, either version 3 of the License, or +// (at your option) any later version. + +// Parity is distributed in the hope that it will be useful, +// but WITHOUT ANY WARRANTY; without even the implied warranty of +// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the +// GNU General Public License for more details. + +// You should have received a copy of the GNU General Public License +// along with Parity. If not, see . + +import etherscan from './'; + +describe.skip('etherscan/stats', () => { + it('retrieves the latest price', () => { + return etherscan.stats + .price() + .then((price) => { + expect(price).to.be.ok; + }); + }); + + it('retrieves the ether total', () => { + return etherscan.stats + .supply() + .then((supply) => { + expect(supply).to.be.ok; + }); + }); +}); diff --git a/js/src/3rdparty/shapeshift/helpers.spec.js b/js/src/3rdparty/shapeshift/helpers.spec.js new file mode 100644 index 000000000..a82b2f6c3 --- /dev/null +++ b/js/src/3rdparty/shapeshift/helpers.spec.js @@ -0,0 +1,71 @@ +// Copyright 2015, 2016 Ethcore (UK) Ltd. +// This file is part of Parity. + +// Parity is free software: you can redistribute it and/or modify +// it under the terms of the GNU General Public License as published by +// the Free Software Foundation, either version 3 of the License, or +// (at your option) any later version. + +// Parity is distributed in the hope that it will be useful, +// but WITHOUT ANY WARRANTY; without even the implied warranty of +// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the +// GNU General Public License for more details. + +// You should have received a copy of the GNU General Public License +// along with Parity. If not, see . + +import chai from 'chai'; +import nock from 'nock'; + +global.expect = chai.expect; // eslint-disable-line no-undef + +import 'isomorphic-fetch'; +import es6Promise from 'es6-promise'; +es6Promise.polyfill(); + +import initShapeshift from './'; +import initRpc from './rpc'; + +const APIKEY = '0x123454321'; + +const shapeshift = initShapeshift(APIKEY); +const rpc = initRpc(APIKEY); + +function mockget (requests) { + let scope = nock(rpc.ENDPOINT); + + requests.forEach((request) => { + scope = scope + .get(`/${request.path}`) + .reply(request.code || 200, () => { + return request.reply; + }); + }); + + return scope; +} + +function mockpost (requests) { + let scope = nock(rpc.ENDPOINT); + + requests.forEach((request) => { + scope = scope + .post(`/${request.path}`) + .reply(request.code || 200, (uri, body) => { + scope.body = scope.body || {}; + scope.body[request.path] = body; + + return request.reply; + }); + }); + + return scope; +} + +export { + APIKEY, + mockget, + mockpost, + shapeshift, + rpc +}; diff --git a/js/src/3rdparty/shapeshift/index.js b/js/src/3rdparty/shapeshift/index.js new file mode 100644 index 000000000..5dd91e4f4 --- /dev/null +++ b/js/src/3rdparty/shapeshift/index.js @@ -0,0 +1,22 @@ +// Copyright 2015, 2016 Ethcore (UK) Ltd. +// This file is part of Parity. + +// Parity is free software: you can redistribute it and/or modify +// it under the terms of the GNU General Public License as published by +// the Free Software Foundation, either version 3 of the License, or +// (at your option) any later version. + +// Parity is distributed in the hope that it will be useful, +// but WITHOUT ANY WARRANTY; without even the implied warranty of +// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the +// GNU General Public License for more details. + +// You should have received a copy of the GNU General Public License +// along with Parity. If not, see . + +import initRpc from './rpc'; +import initShapeshift from './shapeshift'; + +export default function (apikey) { + return initShapeshift(initRpc(apikey)); +} diff --git a/js/src/3rdparty/shapeshift/rpc.js b/js/src/3rdparty/shapeshift/rpc.js new file mode 100644 index 000000000..919f0b07f --- /dev/null +++ b/js/src/3rdparty/shapeshift/rpc.js @@ -0,0 +1,67 @@ +// Copyright 2015, 2016 Ethcore (UK) Ltd. +// This file is part of Parity. + +// Parity is free software: you can redistribute it and/or modify +// it under the terms of the GNU General Public License as published by +// the Free Software Foundation, either version 3 of the License, or +// (at your option) any later version. + +// Parity is distributed in the hope that it will be useful, +// but WITHOUT ANY WARRANTY; without even the implied warranty of +// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the +// GNU General Public License for more details. + +// You should have received a copy of the GNU General Public License +// along with Parity. If not, see . + +const ENDPOINT = 'https://cors.shapeshift.io'; + +function call (method, options) { + return fetch(`${ENDPOINT}/${method}`, options) + .then((response) => { + if (!response.ok) { + throw new Error(response.statusText); + } + + return response.json(); + }) + .then((result) => { + if (result.error) { + throw new Error(result.error); + } + + return result; + }); +} + +export default function (apiKey) { + function get (method) { + return call(method, { + method: 'GET', + headers: { + 'Accept': 'application/json' + } + }); + } + + function post (method, data) { + const params = Object.assign({}, { apiKey }, data); + const body = JSON.stringify(params); + + return call(method, { + method: 'POST', + headers: { + 'Accept': 'application/json', + 'Content-Type': 'application/json', + 'Content-Length': body.length + }, + body + }); + } + + return { + ENDPOINT, + get, + post + }; +} diff --git a/js/src/3rdparty/shapeshift/rpc.spec.js b/js/src/3rdparty/shapeshift/rpc.spec.js new file mode 100644 index 000000000..8de9e8641 --- /dev/null +++ b/js/src/3rdparty/shapeshift/rpc.spec.js @@ -0,0 +1,77 @@ +// Copyright 2015, 2016 Ethcore (UK) Ltd. +// This file is part of Parity. + +// Parity is free software: you can redistribute it and/or modify +// it under the terms of the GNU General Public License as published by +// the Free Software Foundation, either version 3 of the License, or +// (at your option) any later version. + +// Parity is distributed in the hope that it will be useful, +// but WITHOUT ANY WARRANTY; without even the implied warranty of +// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the +// GNU General Public License for more details. + +// You should have received a copy of the GNU General Public License +// along with Parity. If not, see . + +import { APIKEY, mockget, mockpost, rpc } from './helpers.spec.js'; + +describe('shapeshift/rpc', () => { + describe('GET', () => { + const REPLY = { test: 'this is some result' }; + + let scope; + let result; + + beforeEach(() => { + scope = mockget([{ path: 'test', reply: REPLY }]); + + return rpc + .get('test') + .then((_result) => { + result = _result; + }); + }); + + it('does GET', () => { + expect(scope.isDone()).to.be.true; + }); + + it('retrieves the info', () => { + expect(result).to.deep.equal(REPLY); + }); + }); + + describe('POST', () => { + const REPLY = { test: 'this is some result' }; + + let scope; + let result; + + beforeEach(() => { + scope = mockpost([{ path: 'test', reply: REPLY }]); + + return rpc + .post('test', { input: 'stuff' }) + .then((_result) => { + result = _result; + }); + }); + + it('does POST', () => { + expect(scope.isDone()).to.be.true; + }); + + it('retrieves the info', () => { + expect(result).to.deep.equal(REPLY); + }); + + it('passes the input object', () => { + expect(scope.body.test.input).to.equal('stuff'); + }); + + it('passes the apikey specified', () => { + expect(scope.body.test.apiKey).to.equal(APIKEY); + }); + }); +}); diff --git a/js/src/3rdparty/shapeshift/shapeshift.js b/js/src/3rdparty/shapeshift/shapeshift.js new file mode 100644 index 000000000..344a44802 --- /dev/null +++ b/js/src/3rdparty/shapeshift/shapeshift.js @@ -0,0 +1,93 @@ +// Copyright 2015, 2016 Ethcore (UK) Ltd. +// This file is part of Parity. + +// Parity is free software: you can redistribute it and/or modify +// it under the terms of the GNU General Public License as published by +// the Free Software Foundation, either version 3 of the License, or +// (at your option) any later version. + +// Parity is distributed in the hope that it will be useful, +// but WITHOUT ANY WARRANTY; without even the implied warranty of +// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the +// GNU General Public License for more details. + +// You should have received a copy of the GNU General Public License +// along with Parity. If not, see . + +export default function (rpc) { + const subscriptions = []; + + function getCoins () { + return rpc.get('getcoins'); + } + + function getMarketInfo (pair) { + return rpc.get(`marketinfo/${pair}`); + } + + function getStatus (depositAddress) { + return rpc.get(`txStat/${depositAddress}`); + } + + function shift (toAddress, returnAddress, pair) { + return rpc.post('shift', { + withdrawal: toAddress, + pair: pair, + returnAddress: returnAddress + }); + } + + function subscribe (depositAddress, callback) { + const idx = subscriptions.length; + + subscriptions.push({ + depositAddress, + callback, + idx + }); + } + + function _getSubscriptionStatus (subscription) { + if (!subscription) { + return; + } + + getStatus(subscription.depositAddress) + .then((result) => { + switch (result.status) { + case 'no_deposits': + case 'received': + subscription.callback(null, result); + return; + + case 'complete': + subscription.callback(null, result); + subscriptions[subscription.idx] = null; + return; + + case 'failed': + subscription.callback({ + message: status.error, + fatal: true + }); + subscriptions[subscription.idx] = null; + return; + } + }) + .catch(subscription.callback); + } + + function _pollStatus () { + subscriptions.forEach(_getSubscriptionStatus); + } + + setInterval(_pollStatus, 2000); + + return { + getCoins, + getMarketInfo, + getStatus, + shift, + subscribe + }; +} diff --git a/js/src/3rdparty/shapeshift/shapeshift.spec.js b/js/src/3rdparty/shapeshift/shapeshift.spec.js new file mode 100644 index 000000000..36b1506a2 --- /dev/null +++ b/js/src/3rdparty/shapeshift/shapeshift.spec.js @@ -0,0 +1,124 @@ +// Copyright 2015, 2016 Ethcore (UK) Ltd. +// This file is part of Parity. + +// Parity is free software: you can redistribute it and/or modify +// it under the terms of the GNU General Public License as published by +// the Free Software Foundation, either version 3 of the License, or +// (at your option) any later version. + +// Parity is distributed in the hope that it will be useful, +// but WITHOUT ANY WARRANTY; without even the implied warranty of +// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the +// GNU General Public License for more details. + +// You should have received a copy of the GNU General Public License +// along with Parity. If not, see . + +import { mockget, mockpost, shapeshift } from './helpers.spec.js'; + +describe('shapeshift/calls', () => { + describe('getCoins', () => { + const REPLY = { + BTC: { + name: 'Bitcoin', + symbol: 'BTC', + image: 'https://shapeshift.io/images/coins/bitcoin.png', + status: 'available' + }, + ETH: { + name: 'Ether', + symbol: 'ETH', + image: 'https://shapeshift.io/images/coins/ether.png', + status: 'available' + } + }; + + let scope; + + before(() => { + scope = mockget([{ path: 'getcoins', reply: REPLY }]); + + return shapeshift.getCoins(); + }); + + it('makes the call', () => { + expect(scope.isDone()).to.be.ok; + }); + }); + + describe('getMarketInfo', () => { + const REPLY = { + pair: 'btc_ltc', + rate: 128.17959917, + minerFee: 0.003, + limit: 0, + minimum: 0.00004632 + }; + + let scope; + + before(() => { + scope = mockget([{ path: 'marketinfo/btc_ltc', reply: REPLY }]); + + return shapeshift.getMarketInfo('btc_ltc'); + }); + + it('makes the call', () => { + expect(scope.isDone()).to.be.ok; + }); + }); + + describe('getStatus', () => { + const REPLY = { + status: '0x123', + address: '0x123' + }; + + let scope; + + before(() => { + scope = mockget([{ path: 'txStat/0x123', reply: REPLY }]); + + return shapeshift.getStatus('0x123'); + }); + + it('makes the call', () => { + expect(scope.isDone()).to.be.ok; + }); + }); + + describe('shift', () => { + const REPLY = { + deposit: '1BTC', + depositType: 'btc', + withdrawal: '0x456', + withdrawalType: 'eth' + }; + + let scope; + + before(() => { + scope = mockpost([{ path: 'shift', reply: REPLY }]); + + return shapeshift.shift('0x456', '1BTC', 'btc_eth'); + }); + + it('makes the call', () => { + expect(scope.isDone()).to.be.ok; + }); + + describe('body', () => { + it('has withdrawal set', () => { + expect(scope.body.shift.withdrawal).to.equal('0x456'); + }); + + it('has returnAddress set', () => { + expect(scope.body.shift.returnAddress).to.equal('1BTC'); + }); + + it('has pair set', () => { + expect(scope.body.shift.pair).to.equal('btc_eth'); + }); + }); + }); +}); diff --git a/js/src/abi/README.md b/js/src/abi/README.md new file mode 100644 index 000000000..6ad609859 --- /dev/null +++ b/js/src/abi/README.md @@ -0,0 +1,32 @@ +# ethabi-js + +A very early, very POC-type port of [https://github.com/ethcore/ethabi](https://github.com/ethcore/ethabi) to JavaScript + +[![Build Status](https://travis-ci.org/jacogr/ethabi-js.svg?branch=master)](https://travis-ci.org/jacogr/ethabi-js) +[![Coverage Status](https://coveralls.io/repos/github/jacogr/ethabi-js/badge.svg?branch=master)](https://coveralls.io/github/jacogr/ethabi-js?branch=master) +[![Dependency Status](https://david-dm.org/jacogr/ethabi-js.svg)](https://david-dm.org/jacogr/ethabi-js) +[![devDependency Status](https://david-dm.org/jacogr/ethabi-js/dev-status.svg)](https://david-dm.org/jacogr/ethabi-js#info=devDependencies) + +## contributing + +Clone the repo and install dependencies via `npm install`. Tests can be executed via + +- `npm run testOnce` (100% covered unit tests) + +## installation + +Install the package with `npm install --save ethabi-js` from the [npm registry ethabi-js](https://www.npmjs.com/package/ethabi-js) + + +## implementation +### approach + +- this version tries to stay as close to the original Rust version in intent, function names & purpose +- it is a basic port of the Rust version, relying on effectively the same test-suite (expanded where deemed appropriate) +- it is meant as a library to be used in other projects, i.e. [ethapi-js](https://www.npmjs.com/package/ethapi-js) + +### differences to original Rust version + +- internally the library operates on string binary representations as opposed to Vector bytes, lengths are therefore 64 bytes as opposed to 32 bytes +- function names are adapted from the Rust standard snake_case to the JavaScript standard camelCase +- due to the initial library focus, the cli component (as implemented by the original) is not supported nor mplemented diff --git a/js/src/abi/abi.js b/js/src/abi/abi.js new file mode 100644 index 000000000..cdf3b1f63 --- /dev/null +++ b/js/src/abi/abi.js @@ -0,0 +1,20 @@ +// Copyright 2015, 2016 Ethcore (UK) Ltd. +// This file is part of Parity. + +// Parity is free software: you can redistribute it and/or modify +// it under the terms of the GNU General Public License as published by +// the Free Software Foundation, either version 3 of the License, or +// (at your option) any later version. + +// Parity is distributed in the hope that it will be useful, +// but WITHOUT ANY WARRANTY; without even the implied warranty of +// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the +// GNU General Public License for more details. + +// You should have received a copy of the GNU General Public License +// along with Parity. If not, see . + +import Interface from './spec/interface'; + +export default class Abi extends Interface { +} diff --git a/js/src/abi/decoder/bytesTaken.js b/js/src/abi/decoder/bytesTaken.js new file mode 100644 index 000000000..49b443d21 --- /dev/null +++ b/js/src/abi/decoder/bytesTaken.js @@ -0,0 +1,30 @@ +// Copyright 2015, 2016 Ethcore (UK) Ltd. +// This file is part of Parity. + +// Parity is free software: you can redistribute it and/or modify +// it under the terms of the GNU General Public License as published by +// the Free Software Foundation, either version 3 of the License, or +// (at your option) any later version. + +// Parity is distributed in the hope that it will be useful, +// but WITHOUT ANY WARRANTY; without even the implied warranty of +// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the +// GNU General Public License for more details. + +// You should have received a copy of the GNU General Public License +// along with Parity. If not, see . + +export default class BytesTaken { + constructor (bytes, newOffset) { + this._bytes = bytes; + this._newOffset = newOffset; + } + + get bytes () { + return this._bytes; + } + + get newOffset () { + return this._newOffset; + } +} diff --git a/js/src/abi/decoder/bytesTaken.spec.js b/js/src/abi/decoder/bytesTaken.spec.js new file mode 100644 index 000000000..65bb9e1b6 --- /dev/null +++ b/js/src/abi/decoder/bytesTaken.spec.js @@ -0,0 +1,29 @@ +// Copyright 2015, 2016 Ethcore (UK) Ltd. +// This file is part of Parity. + +// Parity is free software: you can redistribute it and/or modify +// it under the terms of the GNU General Public License as published by +// the Free Software Foundation, either version 3 of the License, or +// (at your option) any later version. + +// Parity is distributed in the hope that it will be useful, +// but WITHOUT ANY WARRANTY; without even the implied warranty of +// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the +// GNU General Public License for more details. + +// You should have received a copy of the GNU General Public License +// along with Parity. If not, see . + +import BytesTaken from './bytesTaken'; + +describe('abi/decoder/BytesTaken', () => { + describe('constructor', () => { + it('sets the bytes of the object', () => { + expect((new BytesTaken(1, 2)).bytes).to.equal(1); + }); + + it('sets the newOffset of the object', () => { + expect((new BytesTaken(3, 4)).newOffset).to.equal(4); + }); + }); +}); diff --git a/js/src/abi/decoder/decodeResult.js b/js/src/abi/decoder/decodeResult.js new file mode 100644 index 000000000..8595642df --- /dev/null +++ b/js/src/abi/decoder/decodeResult.js @@ -0,0 +1,30 @@ +// Copyright 2015, 2016 Ethcore (UK) Ltd. +// This file is part of Parity. + +// Parity is free software: you can redistribute it and/or modify +// it under the terms of the GNU General Public License as published by +// the Free Software Foundation, either version 3 of the License, or +// (at your option) any later version. + +// Parity is distributed in the hope that it will be useful, +// but WITHOUT ANY WARRANTY; without even the implied warranty of +// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the +// GNU General Public License for more details. + +// You should have received a copy of the GNU General Public License +// along with Parity. If not, see . + +export default class DecodeResult { + constructor (token, newOffset) { + this._token = token; + this._newOffset = newOffset; + } + + get token () { + return this._token; + } + + get newOffset () { + return this._newOffset; + } +} diff --git a/js/src/abi/decoder/decodeResult.spec.js b/js/src/abi/decoder/decodeResult.spec.js new file mode 100644 index 000000000..892836d07 --- /dev/null +++ b/js/src/abi/decoder/decodeResult.spec.js @@ -0,0 +1,29 @@ +// Copyright 2015, 2016 Ethcore (UK) Ltd. +// This file is part of Parity. + +// Parity is free software: you can redistribute it and/or modify +// it under the terms of the GNU General Public License as published by +// the Free Software Foundation, either version 3 of the License, or +// (at your option) any later version. + +// Parity is distributed in the hope that it will be useful, +// but WITHOUT ANY WARRANTY; without even the implied warranty of +// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the +// GNU General Public License for more details. + +// You should have received a copy of the GNU General Public License +// along with Parity. If not, see . + +import DecodeResult from './decodeResult'; + +describe('abi/decoder/DecodeResult', () => { + describe('constructor', () => { + it('sets the token of the object', () => { + expect((new DecodeResult('token', 2)).token).to.equal('token'); + }); + + it('sets the newOffset of the object', () => { + expect((new DecodeResult('baz', 4)).newOffset).to.equal(4); + }); + }); +}); diff --git a/js/src/abi/decoder/decoder.js b/js/src/abi/decoder/decoder.js new file mode 100644 index 000000000..0d2183122 --- /dev/null +++ b/js/src/abi/decoder/decoder.js @@ -0,0 +1,145 @@ +// Copyright 2015, 2016 Ethcore (UK) Ltd. +// This file is part of Parity. + +// Parity is free software: you can redistribute it and/or modify +// it under the terms of the GNU General Public License as published by +// the Free Software Foundation, either version 3 of the License, or +// (at your option) any later version. + +// Parity is distributed in the hope that it will be useful, +// but WITHOUT ANY WARRANTY; without even the implied warranty of +// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the +// GNU General Public License for more details. + +// You should have received a copy of the GNU General Public License +// along with Parity. If not, see . + +import utf8 from 'utf8'; + +import Token from '../token/token'; +import BytesTaken from './bytesTaken'; +import DecodeResult from './decodeResult'; +import ParamType from '../spec/paramType/paramType'; +import { sliceData } from '../util/slice'; +import { asAddress, asBool, asI32, asU32 } from '../util/sliceAs'; +import { isArray, isInstanceOf } from '../util/types'; + +const NULL = '0000000000000000000000000000000000000000000000000000000000000000'; + +export default class Decoder { + static decode (params, data) { + if (!isArray(params)) { + throw new Error('Parameters should be array of ParamType'); + } + + const slices = sliceData(data); + let offset = 0; + + return params.map((param) => { + const result = Decoder.decodeParam(param, slices, offset); + offset = result.newOffset; + return result.token; + }); + } + + static peek (slices, position) { + if (!slices || !slices[position]) { + return NULL; + } + + return slices[position]; + } + + static takeBytes (slices, position, length) { + const slicesLength = Math.floor((length + 31) / 32); + let bytesStr = ''; + + for (let idx = 0; idx < slicesLength; idx++) { + bytesStr = `${bytesStr}${Decoder.peek(slices, position + idx)}`; + } + + const bytes = (bytesStr.substr(0, length * 2).match(/.{1,2}/g) || []).map((code) => parseInt(code, 16)); + + return new BytesTaken(bytes, position + slicesLength); + } + + static decodeParam (param, slices, offset) { + if (!isInstanceOf(param, ParamType)) { + throw new Error('param should be instanceof ParamType'); + } + + const tokens = []; + let taken; + let lengthOffset; + let length; + let newOffset; + + switch (param.type) { + case 'address': + return new DecodeResult(new Token(param.type, asAddress(Decoder.peek(slices, offset))), offset + 1); + + case 'bool': + return new DecodeResult(new Token(param.type, asBool(Decoder.peek(slices, offset))), offset + 1); + + case 'int': + return new DecodeResult(new Token(param.type, asI32(Decoder.peek(slices, offset))), offset + 1); + + case 'uint': + return new DecodeResult(new Token(param.type, asU32(Decoder.peek(slices, offset))), offset + 1); + + case 'fixedBytes': + taken = Decoder.takeBytes(slices, offset, param.length); + + return new DecodeResult(new Token(param.type, taken.bytes), taken.newOffset); + + case 'bytes': + lengthOffset = asU32(Decoder.peek(slices, offset)).div(32).toNumber(); + length = asU32(Decoder.peek(slices, lengthOffset)).toNumber(); + taken = Decoder.takeBytes(slices, lengthOffset + 1, length); + + return new DecodeResult(new Token(param.type, taken.bytes), offset + 1); + + case 'string': + if (param.indexed) { + taken = Decoder.takeBytes(slices, offset, 32); + + return new DecodeResult(new Token('fixedBytes', taken.bytes), offset + 1); + } + + lengthOffset = asU32(Decoder.peek(slices, offset)).div(32).toNumber(); + length = asU32(Decoder.peek(slices, lengthOffset)).toNumber(); + taken = Decoder.takeBytes(slices, lengthOffset + 1, length); + + const str = taken.bytes.map((code) => String.fromCharCode(code)).join(''); + + return new DecodeResult(new Token(param.type, utf8.decode(str)), offset + 1); + + case 'array': + lengthOffset = asU32(Decoder.peek(slices, offset)).div(32).toNumber(); + length = asU32(Decoder.peek(slices, lengthOffset)).toNumber(); + newOffset = lengthOffset + 1; + + for (let idx = 0; idx < length; idx++) { + const result = Decoder.decodeParam(param.subtype, slices, newOffset); + newOffset = result.newOffset; + tokens.push(result.token); + } + + return new DecodeResult(new Token(param.type, tokens), offset + 1); + + case 'fixedArray': + newOffset = offset; + + for (let idx = 0; idx < param.length; idx++) { + const result = Decoder.decodeParam(param.subtype, slices, newOffset); + newOffset = result.newOffset; + tokens.push(result.token); + } + + return new DecodeResult(new Token(param.type, tokens), newOffset); + + default: + throw new Error(`Invalid param type ${param.type} in decodeParam`); + } + } +} diff --git a/js/src/abi/decoder/decoder.spec.js b/js/src/abi/decoder/decoder.spec.js new file mode 100644 index 000000000..eeaf716c8 --- /dev/null +++ b/js/src/abi/decoder/decoder.spec.js @@ -0,0 +1,310 @@ +// Copyright 2015, 2016 Ethcore (UK) Ltd. +// This file is part of Parity. + +// Parity is free software: you can redistribute it and/or modify +// it under the terms of the GNU General Public License as published by +// the Free Software Foundation, either version 3 of the License, or +// (at your option) any later version. + +// Parity is distributed in the hope that it will be useful, +// but WITHOUT ANY WARRANTY; without even the implied warranty of +// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the +// GNU General Public License for more details. + +// You should have received a copy of the GNU General Public License +// along with Parity. If not, see . + +import BigNumber from 'bignumber.js'; + +import Decoder from './decoder'; +import ParamType from '../spec/paramType'; +import Token from '../token'; +import { padU32 } from '../util/pad'; + +describe('abi/decoder/Decoder', () => { + const stringToBytes = function (str) { + return str.match(/.{1,2}/g).map((code) => parseInt(code, 16)); + }; + + const address1 = '0000000000000000000000001111111111111111111111111111111111111111'; + const address2 = '0000000000000000000000002222222222222222222222222222222222222222'; + const address3 = '0000000000000000000000003333333333333333333333333333333333333333'; + const address4 = '0000000000000000000000004444444444444444444444444444444444444444'; + const bool1 = '0000000000000000000000000000000000000000000000000000000000000001'; + const bytes1 = '1234000000000000000000000000000000000000000000000000000000000000'; + const bytes2 = '1000000000000000000000000000000000000000000000000000000000000000'; + const bytes3 = '10000000000000000000000000000000000000000000000000000000000002'; + const bytes4 = '0010000000000000000000000000000000000000000000000000000000000002'; + const int1 = '0111111111111111111111111111111111111111111111111111111111111111'; + const intn = 'ffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffff85'; + const string1 = '6761766f66796f726b0000000000000000000000000000000000000000000000'; + const tokenAddress1 = new Token('address', `0x${address1.slice(-40)}`); + const tokenAddress2 = new Token('address', `0x${address2.slice(-40)}`); + const tokenAddress3 = new Token('address', `0x${address3.slice(-40)}`); + const tokenAddress4 = new Token('address', `0x${address4.slice(-40)}`); + const tokenBool1 = new Token('bool', true); + const tokenFixedBytes1 = new Token('fixedBytes', [0x12, 0x34]); + const tokenBytes1 = new Token('bytes', [0x12, 0x34]); + const tokenBytes2 = new Token('bytes', stringToBytes(bytes2).concat(stringToBytes(bytes2))); + const tokenBytes3 = new Token('bytes', stringToBytes(bytes3)); + const tokenBytes4 = new Token('bytes', stringToBytes(bytes4)); + const tokenInt1 = new Token('int', new BigNumber(int1, 16)); + const tokenIntn = new Token('int', new BigNumber(-123)); + const tokenUint1 = new Token('uint', new BigNumber(int1, 16)); + const tokenUintn = new Token('uint', new BigNumber(intn, 16)); + const tokenString1 = new Token('string', 'gavofyork'); + const slices = [ address1, address2, address3, address4 ]; + + describe('peek', () => { + it('returns the slice at the correct position', () => { + expect(Decoder.peek(slices, 1)).to.equal(slices[1]); + }); + + it('returns empty on invalid slices', () => { + expect(Decoder.peek(null, 4)).to.equal('0000000000000000000000000000000000000000000000000000000000000000'); + }); + }); + + describe('takeBytes', () => { + it('returns a single slice', () => { + expect(Decoder.takeBytes(slices, 0, 32).bytes).to.deep.equal(stringToBytes(slices[0])); + }); + + it('returns a single partial slice', () => { + expect(Decoder.takeBytes(slices, 0, 20).bytes).to.deep.equal(stringToBytes(slices[0].substr(0, 40))); + }); + + it('returns multiple slices', () => { + expect(Decoder.takeBytes(slices, 0, 64).bytes).to.deep.equal(stringToBytes(`${slices[0]}${slices[1]}`)); + }); + + it('returns a single offset slice', () => { + expect(Decoder.takeBytes(slices, 1, 32).bytes).to.deep.equal(stringToBytes(slices[1])); + }); + + it('returns multiple offset slices', () => { + expect(Decoder.takeBytes(slices, 1, 64).bytes).to.deep.equal(stringToBytes(`${slices[1]}${slices[2]}`)); + }); + + it('returns the requires length from slices', () => { + expect( + Decoder.takeBytes(slices, 1, 75).bytes + ).to.deep.equal(stringToBytes(`${slices[1]}${slices[2]}${slices[3]}`.substr(0, 150))); + }); + }); + + describe('decodeParam', () => { + it('throws an error on non ParamType param', () => { + expect(() => Decoder.decodeParam({})).to.throw(/ParamType/); + }); + + it('throws an error on invalid param type', () => { + const pt = new ParamType('address'); + pt._type = 'noMatch'; + + expect(() => Decoder.decodeParam(pt)).to.throw(/noMatch/); + }); + + it('decodes an address', () => { + expect( + Decoder.decodeParam(new ParamType('address'), [address1], 0).token + ).to.deep.equal(tokenAddress1); + }); + + it('decodes a bool', () => { + expect( + Decoder.decodeParam(new ParamType('bool'), [bool1], 0).token + ).to.deep.equal(tokenBool1); + }); + + it('decodes an int', () => { + expect( + Decoder.decodeParam(new ParamType('int'), [int1], 0).token + ).to.deep.equal(tokenInt1); + }); + + it('decodes a negative int', () => { + expect( + Decoder.decodeParam(new ParamType('int'), [intn], 0).token + ).to.deep.equal(tokenIntn); + }); + + it('decodes an uint', () => { + expect( + Decoder.decodeParam(new ParamType('uint'), [int1], 0).token + ).to.deep.equal(tokenUint1); + }); + + it('decodes an uint (negative as int)', () => { + expect( + Decoder.decodeParam(new ParamType('uint'), [intn], 0).token + ).to.deep.equal(tokenUintn); + }); + + it('decodes fixedBytes', () => { + expect( + Decoder.decodeParam(new ParamType('fixedBytes', null, 2), [bytes1], 0).token + ).to.deep.equal(tokenFixedBytes1); + }); + + it('decodes bytes', () => { + expect( + Decoder.decodeParam(new ParamType('bytes'), [padU32(0x20), padU32(2), bytes1], 0).token + ).to.deep.equal(tokenBytes1); + }); + + it('decodes string', () => { + expect( + Decoder.decodeParam(new ParamType('string'), [padU32(0x20), padU32(9), string1], 0).token + ).to.deep.equal(tokenString1); + }); + + it('decodes string (indexed)', () => { + expect( + Decoder.decodeParam(new ParamType('string', null, 0, true), [bytes1], 0) + ).to.deep.equal(Decoder.decodeParam(new ParamType('fixedBytes', null, 32, true), [bytes1], 0)); + }); + }); + + describe('decode', () => { + it('throws an error on invalid params', () => { + expect(() => Decoder.decode(null, '123')).to.throw(/array/); + }); + + describe('address', () => { + it('decodes an address', () => { + expect( + Decoder.decode( + [new ParamType('address')], + `${address1}` + ) + ).to.deep.equal([tokenAddress1]); + }); + + it('decodes 2 addresses', () => { + expect( + Decoder.decode( + [new ParamType('address'), new ParamType('address')], + `${address1}${address2}` + ) + ).to.deep.equal([tokenAddress1, tokenAddress2]); + }); + + it('decodes a fixedArray of addresses', () => { + expect( + Decoder.decode( + [new ParamType('fixedArray', new ParamType('address'), 2)], + `${address1}${address2}` + ) + ).to.deep.equal([new Token('fixedArray', [tokenAddress1, tokenAddress2])]); + }); + + it('decodes a dynamic array of addresses', () => { + expect( + Decoder.decode( + [new ParamType('array', new ParamType('address'))], + `${padU32(0x20)}${padU32(2)}${address1}${address2}` + ) + ).to.deep.equal([new Token('array', [tokenAddress1, tokenAddress2])]); + }); + + it('decodes a dynamic array of fixed arrays', () => { + expect( + Decoder.decode( + [new ParamType('array', new ParamType('fixedArray', new ParamType('address'), 2))], + `${padU32(0x20)}${padU32(2)}${address1}${address2}${address3}${address4}` + ) + ).to.deep.equal([ + new Token('array', [ + new Token('fixedArray', [tokenAddress1, tokenAddress2]), + new Token('fixedArray', [tokenAddress3, tokenAddress4]) + ]) + ]); + }); + }); + + describe('int', () => { + it('decodes an int', () => { + expect( + Decoder.decode( + [new ParamType('int')], + `${int1}` + ) + ).to.deep.equal([tokenInt1]); + }); + }); + + describe('uint', () => { + it('decodes an uint', () => { + expect( + Decoder.decode( + [new ParamType('uint')], + `${int1}` + ) + ).to.deep.equal([tokenUint1]); + }); + }); + + describe('fixedBytes', () => { + it('decodes fixedBytes', () => { + expect( + Decoder.decode( + [new ParamType('fixedBytes', null, 2)], + `${bytes1}` + ) + ).to.deep.equal([tokenFixedBytes1]); + }); + }); + + describe('bytes', () => { + it('decodes bytes', () => { + expect( + Decoder.decode( + [new ParamType('bytes')], + `${padU32(0x20)}${padU32(2)}${bytes1}` + ) + ).to.deep.equal([tokenBytes1]); + }); + + it('decodes bytes sequence', () => { + expect( + Decoder.decode( + [new ParamType('bytes')], + `${padU32(0x20)}${padU32(0x40)}${bytes2}${bytes2}` + ) + ).to.deep.equal([tokenBytes2]); + }); + + it('decodes bytes seuence (2)', () => { + expect( + Decoder.decode( + [new ParamType('bytes'), new ParamType('bytes')], + `${padU32(0x40)}${padU32(0x80)}${padU32(0x1f)}${bytes3}00${padU32(0x20)}${bytes4}` + ) + ).to.deep.equal([tokenBytes3, tokenBytes4]); + }); + }); + + describe('bool', () => { + it('decodes a single bool', () => { + expect( + Decoder.decode( + [new ParamType('bool')], + bool1 + ) + ).to.deep.equal([tokenBool1]); + }); + }); + + describe('string', () => { + it('decodes a string', () => { + expect( + Decoder.decode( + [new ParamType('string')], + `${padU32(0x20)}${padU32(9)}${string1}` + ) + ).to.deep.equal([tokenString1]); + }); + }); + }); +}); diff --git a/js/src/abi/decoder/index.js b/js/src/abi/decoder/index.js new file mode 100644 index 000000000..f9839c10d --- /dev/null +++ b/js/src/abi/decoder/index.js @@ -0,0 +1,17 @@ +// Copyright 2015, 2016 Ethcore (UK) Ltd. +// This file is part of Parity. + +// Parity is free software: you can redistribute it and/or modify +// it under the terms of the GNU General Public License as published by +// the Free Software Foundation, either version 3 of the License, or +// (at your option) any later version. + +// Parity is distributed in the hope that it will be useful, +// but WITHOUT ANY WARRANTY; without even the implied warranty of +// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the +// GNU General Public License for more details. + +// You should have received a copy of the GNU General Public License +// along with Parity. If not, see . + +export default from './decoder'; diff --git a/js/src/abi/encoder/encoder.js b/js/src/abi/encoder/encoder.js new file mode 100644 index 000000000..bb3a17d26 --- /dev/null +++ b/js/src/abi/encoder/encoder.js @@ -0,0 +1,72 @@ +// Copyright 2015, 2016 Ethcore (UK) Ltd. +// This file is part of Parity. + +// Parity is free software: you can redistribute it and/or modify +// it under the terms of the GNU General Public License as published by +// the Free Software Foundation, either version 3 of the License, or +// (at your option) any later version. + +// Parity is distributed in the hope that it will be useful, +// but WITHOUT ANY WARRANTY; without even the implied warranty of +// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the +// GNU General Public License for more details. + +// You should have received a copy of the GNU General Public License +// along with Parity. If not, see . + +import { padAddress, padBool, padBytes, padFixedBytes, padU32, padString } from '../util/pad'; +import Mediate from './mediate'; +import Token from '../token/token'; +import { isArray, isInstanceOf } from '../util/types'; + +export default class Encoder { + static encode (tokens) { + if (!isArray(tokens)) { + throw new Error('tokens should be array of Token'); + } + + const mediates = tokens.map((token) => Encoder.encodeToken(token)); + const inits = mediates + .map((mediate, idx) => mediate.init(Mediate.offsetFor(mediates, idx))) + .join(''); + const closings = mediates + .map((mediate, idx) => mediate.closing(Mediate.offsetFor(mediates, idx))) + .join(''); + + return `${inits}${closings}`; + } + + static encodeToken (token) { + if (!isInstanceOf(token, Token)) { + throw new Error('token should be instanceof Token'); + } + + switch (token.type) { + case 'address': + return new Mediate('raw', padAddress(token.value)); + + case 'int': + case 'uint': + return new Mediate('raw', padU32(token.value)); + + case 'bool': + return new Mediate('raw', padBool(token.value)); + + case 'fixedBytes': + return new Mediate('raw', padFixedBytes(token.value)); + + case 'bytes': + return new Mediate('prefixed', padBytes(token.value)); + + case 'string': + return new Mediate('prefixed', padString(token.value)); + + case 'fixedArray': + case 'array': + return new Mediate(token.type, token.value.map((token) => Encoder.encodeToken(token))); + + default: + throw new Error(`Invalid token type ${token.type} in encodeToken`); + } + } +} diff --git a/js/src/abi/encoder/encoder.spec.js b/js/src/abi/encoder/encoder.spec.js new file mode 100644 index 000000000..008861602 --- /dev/null +++ b/js/src/abi/encoder/encoder.spec.js @@ -0,0 +1,290 @@ +// Copyright 2015, 2016 Ethcore (UK) Ltd. +// This file is part of Parity. + +// Parity is free software: you can redistribute it and/or modify +// it under the terms of the GNU General Public License as published by +// the Free Software Foundation, either version 3 of the License, or +// (at your option) any later version. + +// Parity is distributed in the hope that it will be useful, +// but WITHOUT ANY WARRANTY; without even the implied warranty of +// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the +// GNU General Public License for more details. + +// You should have received a copy of the GNU General Public License +// along with Parity. If not, see . + +import Encoder from './encoder'; +import Token from '../token'; +import { padAddress, padFixedBytes, padU32 } from '../util/pad'; + +describe('abi/encoder/Encoder', () => { + describe('encodeToken', () => { + it('requires token as Token', () => { + expect(() => Encoder.encodeToken()).to.throw(/Token/); + }); + + it('encodes address tokens in Mediate(raw)', () => { + const mediate = Encoder.encodeToken(new Token('address', '123')); + + expect(mediate.type).to.equal('raw'); + expect(mediate.value).to.be.ok; + }); + + it('encodes bool tokens in Mediate(raw)', () => { + const mediatet = Encoder.encodeToken(new Token('bool', true)); + const mediatef = Encoder.encodeToken(new Token('bool', false)); + + expect(mediatet.type).to.equal('raw'); + expect(mediatet.value).to.be.ok; + + expect(mediatef.type).to.equal('raw'); + expect(mediatef.value).to.be.ok; + }); + + it('encodes int tokens in Mediate(raw)', () => { + const mediate = Encoder.encodeToken(new Token('int', '123')); + + expect(mediate.type).to.equal('raw'); + expect(mediate.value).to.be.ok; + }); + + it('encodes uint tokens in Mediate(raw)', () => { + const mediate = Encoder.encodeToken(new Token('uint', '123')); + + expect(mediate.type).to.equal('raw'); + expect(mediate.value).to.be.ok; + }); + + it('encodes fixedBytes tokens in Mediate(raw)', () => { + const mediate = Encoder.encodeToken(new Token('fixedBytes', '123')); + + expect(mediate.type).to.equal('raw'); + expect(mediate.value).to.be.ok; + }); + + it('encodes bytes tokens in Mediate(prefixed)', () => { + const mediate = Encoder.encodeToken(new Token('bytes', '123')); + + expect(mediate.type).to.equal('prefixed'); + expect(mediate.value).to.be.ok; + }); + + it('encodes string tokens in Mediate(prefixed)', () => { + const mediate = Encoder.encodeToken(new Token('string', '123')); + + expect(mediate.type).to.equal('prefixed'); + expect(mediate.value).to.be.ok; + }); + + it('encodes fixedArray tokens in Mediate(fixedArray)', () => { + const mediate = Encoder.encodeToken(new Token('fixedArray', [new Token('uint', '123')])); + + expect(mediate.type).to.equal('fixedArray'); + expect(mediate.value).to.be.ok; + }); + + it('encodes array tokens in Mediate(array)', () => { + const mediate = Encoder.encodeToken(new Token('array', [new Token('uint', '123')])); + + expect(mediate.type).to.equal('array'); + expect(mediate.value).to.be.ok; + }); + + it('throws an Error on invalid tokens', () => { + const token = new Token('address'); + token._type = 'noMatch'; + + expect(() => Encoder.encodeToken(token)).to.throw(/noMatch/); + }); + }); + + describe('encode', () => { + it('requires tokens array', () => { + expect(() => Encoder.encode()).to.throw(/array/); + }); + + describe('addresses', () => { + const address1 = '1111111111111111111111111111111111111111'; + const address2 = '2222222222222222222222222222222222222222'; + const address3 = '3333333333333333333333333333333333333333'; + const address4 = '4444444444444444444444444444444444444444'; + const encAddress1 = padAddress(address1); + const encAddress2 = padAddress(address2); + const encAddress3 = padAddress(address3); + const encAddress4 = padAddress(address4); + const tokenAddress1 = new Token('address', address1); + const tokenAddress2 = new Token('address', address2); + const tokenAddress3 = new Token('address', address3); + const tokenAddress4 = new Token('address', address4); + + it('encodes an address', () => { + const token = tokenAddress1; + + expect(Encoder.encode([token])).to.equal(encAddress1); + }); + + it('encodes an array of addresses', () => { + const expected = `${padU32(0x20)}${padU32(2)}${encAddress1}${encAddress2}`; + const token = new Token('array', [tokenAddress1, tokenAddress2]); + + expect(Encoder.encode([token])).to.equal(expected); + }); + + it('encodes an fixedArray of addresses', () => { + const expected = `${encAddress1}${encAddress2}`; + const token = new Token('fixedArray', [tokenAddress1, tokenAddress2]); + + expect(Encoder.encode([token])).to.equal(expected); + }); + + it('encodes two addresses', () => { + const expected = `${encAddress1}${encAddress2}`; + const tokens = [tokenAddress1, tokenAddress2]; + + expect(Encoder.encode(tokens)).to.equal(expected); + }); + + it('encodes fixed array of dynamic array addresses', () => { + const tokens1 = new Token('array', [tokenAddress1, tokenAddress2]); + const tokens2 = new Token('array', [tokenAddress3, tokenAddress4]); + const fixed = new Token('fixedArray', [tokens1, tokens2]); + const expected = `${padU32(0x40)}${padU32(0xa0)}${padU32(2)}${encAddress1}${encAddress2}${padU32(2)}${encAddress3}${encAddress4}`; + + expect(Encoder.encode([fixed])).to.equal(expected); + }); + + it('encodes dynamic array of fixed array addresses', () => { + const tokens1 = new Token('fixedArray', [tokenAddress1, tokenAddress2]); + const tokens2 = new Token('fixedArray', [tokenAddress3, tokenAddress4]); + const dynamic = new Token('array', [tokens1, tokens2]); + const expected = `${padU32(0x20)}${padU32(2)}${encAddress1}${encAddress2}${encAddress3}${encAddress4}`; + + expect(Encoder.encode([dynamic])).to.equal(expected); + }); + + it('encodes dynamic array of dynamic array addresses', () => { + const tokens1 = new Token('array', [tokenAddress1]); + const tokens2 = new Token('array', [tokenAddress2]); + const dynamic = new Token('array', [tokens1, tokens2]); + const expected = `${padU32(0x20)}${padU32(2)}${padU32(0x80)}${padU32(0xc0)}${padU32(1)}${encAddress1}${padU32(1)}${encAddress2}`; + + expect(Encoder.encode([dynamic])).to.equal(expected); + }); + + it('encodes dynamic array of dynamic array addresses (2)', () => { + const tokens1 = new Token('array', [tokenAddress1, tokenAddress2]); + const tokens2 = new Token('array', [tokenAddress3, tokenAddress4]); + const dynamic = new Token('array', [tokens1, tokens2]); + const expected = `${padU32(0x20)}${padU32(2)}${padU32(0x80)}${padU32(0xe0)}${padU32(2)}${encAddress1}${encAddress2}${padU32(2)}${encAddress3}${encAddress4}`; + + expect(Encoder.encode([dynamic])).to.equal(expected); + }); + + it('encodes fixed array of fixed array addresses', () => { + const tokens1 = new Token('fixedArray', [tokenAddress1, tokenAddress2]); + const tokens2 = new Token('fixedArray', [tokenAddress3, tokenAddress4]); + const dynamic = new Token('fixedArray', [tokens1, tokens2]); + const expected = `${encAddress1}${encAddress2}${encAddress3}${encAddress4}`; + + expect(Encoder.encode([dynamic])).to.equal(expected); + }); + }); + + describe('bytes', () => { + const bytes1 = '0x1234'; + const bytes2 = '0x10000000000000000000000000000000000000000000000000000000000002'; + const bytes3 = '0x1000000000000000000000000000000000000000000000000000000000000000'; + + it('encodes fixed bytes', () => { + const token = new Token('fixedBytes', bytes1); + + expect(Encoder.encode([token])).to.equal(padFixedBytes(bytes1)); + }); + + it('encodes bytes', () => { + const token = new Token('bytes', bytes1); + + expect(Encoder.encode([token])).to.equal(`${padU32(0x20)}${padU32(2)}${padFixedBytes(bytes1)}`); + }); + + it('encodes bytes (short of boundary)', () => { + const token = new Token('bytes', bytes2); + + expect(Encoder.encode([token])).to.equal(`${padU32(0x20)}${padU32(0x1f)}${padFixedBytes(bytes2)}`); + }); + + it('encodes bytes (two blocks)', () => { + const input = `${bytes3}${bytes3.slice(-64)}`; + const token = new Token('bytes', input); + + expect(Encoder.encode([token])).to.equal(`${padU32(0x20)}${padU32(0x40)}${padFixedBytes(input)}`); + }); + + it('encodes two consecutive bytes', () => { + const in1 = '0x10000000000000000000000000000000000000000000000000000000000002'; + const in2 = '0x0010000000000000000000000000000000000000000000000000000000000002'; + const tokens = [new Token('bytes', in1), new Token('bytes', in2)]; + + expect(Encoder.encode(tokens)).to.equal(`${padU32(0x40)}${padU32(0x80)}${padU32(0x1f)}${padFixedBytes(in1)}${padU32(0x20)}${padFixedBytes(in2)}`); + }); + }); + + describe('string', () => { + it('encodes a string', () => { + const string = 'gavofyork'; + const stringEnc = padFixedBytes('0x6761766f66796f726b'); + const token = new Token('string', string); + + expect(Encoder.encode([token])).to.equal(`${padU32(0x20)}${padU32(string.length.toString(16))}${stringEnc}`); + }); + }); + + describe('uint', () => { + it('encodes a uint', () => { + const token = new Token('uint', 4); + + expect(Encoder.encode([token])).to.equal(padU32(4)); + }); + }); + + describe('int', () => { + it('encodes a int', () => { + const token = new Token('int', 4); + + expect(Encoder.encode([token])).to.equal(padU32(4)); + }); + }); + + describe('bool', () => { + it('encodes a bool (true)', () => { + const token = new Token('bool', true); + + expect(Encoder.encode([token])).to.equal(padU32(1)); + }); + + it('encodes a bool (false)', () => { + const token = new Token('bool', false); + + expect(Encoder.encode([token])).to.equal(padU32(0)); + }); + }); + + describe('comprehensive test', () => { + it('encodes a complex sequence', () => { + const bytes = '0x131a3afc00d1b1e3461b955e53fc866dcf303b3eb9f4c16f89e388930f48134b131a3afc00d1b1e3461b955e53fc866dcf303b3eb9f4c16f89e388930f48134b'; + const tokens = [new Token('int', 5), new Token('bytes', bytes), new Token('int', 3), new Token('bytes', bytes)]; + + expect(Encoder.encode(tokens)).to.equal(`${padU32(5)}${padU32(0x80)}${padU32(3)}${padU32(0xe0)}${padU32(0x40)}${bytes.substr(2)}${padU32(0x40)}${bytes.substr(2)}`); + }); + + it('encodes a complex sequence (nested)', () => { + const array = [new Token('int', 5), new Token('int', 6), new Token('int', 7)]; + const tokens = [new Token('int', 1), new Token('string', 'gavofyork'), new Token('int', 2), new Token('int', 3), new Token('int', 4), new Token('array', array)]; + const stringEnc = padFixedBytes('0x6761766f66796f726b'); + + expect(Encoder.encode(tokens)).to.equal(`${padU32(1)}${padU32(0xc0)}${padU32(2)}${padU32(3)}${padU32(4)}${padU32(0x100)}${padU32(9)}${stringEnc}${padU32(3)}${padU32(5)}${padU32(6)}${padU32(7)}`); + }); + }); + }); +}); diff --git a/js/src/abi/encoder/index.js b/js/src/abi/encoder/index.js new file mode 100644 index 000000000..e19ff81e6 --- /dev/null +++ b/js/src/abi/encoder/index.js @@ -0,0 +1,17 @@ +// Copyright 2015, 2016 Ethcore (UK) Ltd. +// This file is part of Parity. + +// Parity is free software: you can redistribute it and/or modify +// it under the terms of the GNU General Public License as published by +// the Free Software Foundation, either version 3 of the License, or +// (at your option) any later version. + +// Parity is distributed in the hope that it will be useful, +// but WITHOUT ANY WARRANTY; without even the implied warranty of +// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the +// GNU General Public License for more details. + +// You should have received a copy of the GNU General Public License +// along with Parity. If not, see . + +export default from './encoder'; diff --git a/js/src/abi/encoder/mediate.js b/js/src/abi/encoder/mediate.js new file mode 100644 index 000000000..cb7fce6a7 --- /dev/null +++ b/js/src/abi/encoder/mediate.js @@ -0,0 +1,142 @@ +// Copyright 2015, 2016 Ethcore (UK) Ltd. +// This file is part of Parity. + +// Parity is free software: you can redistribute it and/or modify +// it under the terms of the GNU General Public License as published by +// the Free Software Foundation, either version 3 of the License, or +// (at your option) any later version. + +// Parity is distributed in the hope that it will be useful, +// but WITHOUT ANY WARRANTY; without even the implied warranty of +// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the +// GNU General Public License for more details. + +// You should have received a copy of the GNU General Public License +// along with Parity. If not, see . + +const TYPES = ['raw', 'prefixed', 'fixedArray', 'array']; + +import { padU32 } from '../util/pad'; + +export default class Mediate { + constructor (type, value) { + Mediate.validateType(type); + + this._type = type; + this._value = value; + } + + initLength () { + switch (this._type) { + case 'raw': + return this._value.length / 2; + + case 'array': + case 'prefixed': + return 32; + + case 'fixedArray': + return this._value + .reduce((total, mediate) => { + return total + mediate.initLength(); + }, 0); + } + } + + closingLength () { + switch (this._type) { + case 'raw': + return 0; + + case 'prefixed': + return this._value.length / 2; + + case 'array': + return this._value + .reduce((total, mediate) => { + return total + mediate.initLength(); + }, 32); + + case 'fixedArray': + return this._value + .reduce((total, mediate) => { + return total + mediate.initLength() + mediate.closingLength(); + }, 0); + } + } + + init (suffixOffset) { + switch (this._type) { + case 'raw': + return this._value; + + case 'fixedArray': + return this._value + .map((mediate, idx) => mediate.init(Mediate.offsetFor(this._value, idx)).toString(16)) + .join(''); + + case 'prefixed': + case 'array': + return padU32(suffixOffset); + } + } + + closing (offset) { + switch (this._type) { + case 'raw': + return ''; + + case 'prefixed': + return this._value; + + case 'fixedArray': + return this._value + .map((mediate, idx) => mediate.closing(Mediate.offsetFor(this._value, idx)).toString(16)) + .join(''); + + case 'array': + const prefix = padU32(this._value.length); + const inits = this._value + .map((mediate, idx) => mediate.init(offset + Mediate.offsetFor(this._value, idx) + 32).toString(16)) + .join(''); + const closings = this._value + .map((mediate, idx) => mediate.closing(offset + Mediate.offsetFor(this._value, idx)).toString(16)) + .join(''); + + return `${prefix}${inits}${closings}`; + } + } + + get type () { + return this._type; + } + + get value () { + return this._value; + } + + static offsetFor (mediates, position) { + if (position < 0 || position >= mediates.length) { + throw new Error(`Invalid position ${position} specified for Mediate.offsetFor`); + } + + const initLength = mediates + .reduce((total, mediate) => { + return total + mediate.initLength(); + }, 0); + + return mediates + .slice(0, position) + .reduce((total, mediate) => { + return total + mediate.closingLength(); + }, initLength); + } + + static validateType (type) { + if (TYPES.filter((_type) => type === _type).length) { + return true; + } + + throw new Error(`Invalid type ${type} received for Mediate.validateType`); + } +} diff --git a/js/src/abi/encoder/mediate.spec.js b/js/src/abi/encoder/mediate.spec.js new file mode 100644 index 000000000..cfd0211f2 --- /dev/null +++ b/js/src/abi/encoder/mediate.spec.js @@ -0,0 +1,105 @@ +// Copyright 2015, 2016 Ethcore (UK) Ltd. +// This file is part of Parity. + +// Parity is free software: you can redistribute it and/or modify +// it under the terms of the GNU General Public License as published by +// the Free Software Foundation, either version 3 of the License, or +// (at your option) any later version. + +// Parity is distributed in the hope that it will be useful, +// but WITHOUT ANY WARRANTY; without even the implied warranty of +// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the +// GNU General Public License for more details. + +// You should have received a copy of the GNU General Public License +// along with Parity. If not, see . + +import Mediate from './mediate'; + +describe('abi/encoder/Mediate', () => { + const LONG15 = '1234567890abcdef000000000000000000000000000000000000000000000000'; + const DOUBLE15 = `${LONG15}${LONG15}`; + const ARRAY = [new Mediate('raw', DOUBLE15), new Mediate('raw', LONG15)]; + + describe('validateType', () => { + it('validates raw', () => { + expect(Mediate.validateType('raw')).to.be.true; + }); + + it('validates prefixed', () => { + expect(Mediate.validateType('prefixed')).to.be.true; + }); + + it('validates fixedArray', () => { + expect(Mediate.validateType('fixedArray')).to.be.true; + }); + + it('validates array', () => { + expect(Mediate.validateType('array')).to.be.true; + }); + + it('throws an error on invalid types', () => { + expect(() => Mediate.validateType('noMatch')).to.throw(/noMatch/); + }); + }); + + describe('offsetFor', () => { + it('thows an error when offset < 0', () => { + expect(() => Mediate.offsetFor([1], -1)).to.throw(/Invalid position/); + }); + + it('throws an error when offset >= length', () => { + expect(() => Mediate.offsetFor([1], 1)).to.throw(/Invalid position/); + }); + }); + + describe('constructor', () => { + it('throws an error on invalid types', () => { + expect(() => new Mediate('noMatch', '1')).to.throw(/noMatch/); + }); + + it('sets the type of the object', () => { + expect((new Mediate('raw', '1')).type).to.equal('raw'); + }); + + it('sets the value of the object', () => { + expect((new Mediate('raw', '1')).value).to.equal('1'); + }); + }); + + describe('initLength', () => { + it('returns correct variable byte length for raw', () => { + expect(new Mediate('raw', DOUBLE15).initLength()).to.equal(64); + }); + + it('returns correct fixed byte length for array', () => { + expect(new Mediate('array', [1, 2, 3, 4]).initLength()).to.equal(32); + }); + + it('returns correct fixed byte length for prefixed', () => { + expect(new Mediate('prefixed', 0).initLength()).to.equal(32); + }); + + it('returns correct variable byte length for fixedArray', () => { + expect(new Mediate('fixedArray', ARRAY).initLength()).to.equal(96); + }); + }); + + describe('closingLength', () => { + it('returns 0 byte length for raw', () => { + expect(new Mediate('raw', DOUBLE15).closingLength()).to.equal(0); + }); + + it('returns prefix + size for prefixed', () => { + expect(new Mediate('prefixed', DOUBLE15).closingLength()).to.equal(64); + }); + + it('returns prefix + size for array', () => { + expect(new Mediate('array', ARRAY).closingLength()).to.equal(128); + }); + + it('returns total length for fixedArray', () => { + expect(new Mediate('fixedArray', ARRAY).closingLength()).to.equal(96); + }); + }); +}); diff --git a/js/src/abi/index.js b/js/src/abi/index.js new file mode 100644 index 000000000..f055a6913 --- /dev/null +++ b/js/src/abi/index.js @@ -0,0 +1,17 @@ +// Copyright 2015, 2016 Ethcore (UK) Ltd. +// This file is part of Parity. + +// Parity is free software: you can redistribute it and/or modify +// it under the terms of the GNU General Public License as published by +// the Free Software Foundation, either version 3 of the License, or +// (at your option) any later version. + +// Parity is distributed in the hope that it will be useful, +// but WITHOUT ANY WARRANTY; without even the implied warranty of +// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the +// GNU General Public License for more details. + +// You should have received a copy of the GNU General Public License +// along with Parity. If not, see . + +export default from './abi'; diff --git a/js/src/abi/spec/constructor.js b/js/src/abi/spec/constructor.js new file mode 100644 index 000000000..89ea99610 --- /dev/null +++ b/js/src/abi/spec/constructor.js @@ -0,0 +1,36 @@ +// Copyright 2015, 2016 Ethcore (UK) Ltd. +// This file is part of Parity. + +// Parity is free software: you can redistribute it and/or modify +// it under the terms of the GNU General Public License as published by +// the Free Software Foundation, either version 3 of the License, or +// (at your option) any later version. + +// Parity is distributed in the hope that it will be useful, +// but WITHOUT ANY WARRANTY; without even the implied warranty of +// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the +// GNU General Public License for more details. + +// You should have received a copy of the GNU General Public License +// along with Parity. If not, see . + +import Encoder from '../encoder/encoder'; +import Param from './param'; + +export default class Constructor { + constructor (abi) { + this._inputs = Param.toParams(abi.inputs || []); + } + + get inputs () { + return this._inputs; + } + + inputParamTypes () { + return this._inputs.map((input) => input.kind); + } + + encodeCall (tokens) { + return Encoder.encode(tokens); + } +} diff --git a/js/src/abi/spec/constructor.spec.js b/js/src/abi/spec/constructor.spec.js new file mode 100644 index 000000000..e02f6cf71 --- /dev/null +++ b/js/src/abi/spec/constructor.spec.js @@ -0,0 +1,52 @@ +// Copyright 2015, 2016 Ethcore (UK) Ltd. +// This file is part of Parity. + +// Parity is free software: you can redistribute it and/or modify +// it under the terms of the GNU General Public License as published by +// the Free Software Foundation, either version 3 of the License, or +// (at your option) any later version. + +// Parity is distributed in the hope that it will be useful, +// but WITHOUT ANY WARRANTY; without even the implied warranty of +// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the +// GNU General Public License for more details. + +// You should have received a copy of the GNU General Public License +// along with Parity. If not, see . + +import Constructor from './constructor'; +import Param from './param'; +import Token from '../token'; + +describe('abi/spec/Constructor', () => { + const inputsArr = [{ name: 'boolin', type: 'bool' }, { name: 'stringin', type: 'string' }]; + const bool = new Param('boolin', 'bool'); + const string = new Param('stringin', 'string'); + + const inputs = [bool, string]; + const cr = new Constructor({ inputs: inputsArr }); + + describe('constructor', () => { + it('stores the inputs as received', () => { + expect(cr.inputs).to.deep.equal(inputs); + }); + + it('matches empty inputs with []', () => { + expect(new Constructor({}).inputs).to.deep.equal([]); + }); + }); + + describe('inputParamTypes', () => { + it('retrieves the input types as received', () => { + expect(cr.inputParamTypes()).to.deep.equal([bool.kind, string.kind]); + }); + }); + + describe('encodeCall', () => { + it('encodes correctly', () => { + const result = cr.encodeCall([new Token('bool', true), new Token('string', 'jacogr')]); + + expect(result).to.equal('0000000000000000000000000000000000000000000000000000000000000001000000000000000000000000000000000000000000000000000000000000004000000000000000000000000000000000000000000000000000000000000000066a61636f67720000000000000000000000000000000000000000000000000000'); + }); + }); +}); diff --git a/js/src/abi/spec/event/decodedLog.js b/js/src/abi/spec/event/decodedLog.js new file mode 100644 index 000000000..3993b527b --- /dev/null +++ b/js/src/abi/spec/event/decodedLog.js @@ -0,0 +1,30 @@ +// Copyright 2015, 2016 Ethcore (UK) Ltd. +// This file is part of Parity. + +// Parity is free software: you can redistribute it and/or modify +// it under the terms of the GNU General Public License as published by +// the Free Software Foundation, either version 3 of the License, or +// (at your option) any later version. + +// Parity is distributed in the hope that it will be useful, +// but WITHOUT ANY WARRANTY; without even the implied warranty of +// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the +// GNU General Public License for more details. + +// You should have received a copy of the GNU General Public License +// along with Parity. If not, see . + +export default class DecodedLog { + constructor (params, address) { + this._params = params; + this._address = address; + } + + get address () { + return this._address; + } + + get params () { + return this._params; + } +} diff --git a/js/src/abi/spec/event/decodedLog.spec.js b/js/src/abi/spec/event/decodedLog.spec.js new file mode 100644 index 000000000..e6ed1a022 --- /dev/null +++ b/js/src/abi/spec/event/decodedLog.spec.js @@ -0,0 +1,28 @@ +// Copyright 2015, 2016 Ethcore (UK) Ltd. +// This file is part of Parity. + +// Parity is free software: you can redistribute it and/or modify +// it under the terms of the GNU General Public License as published by +// the Free Software Foundation, either version 3 of the License, or +// (at your option) any later version. + +// Parity is distributed in the hope that it will be useful, +// but WITHOUT ANY WARRANTY; without even the implied warranty of +// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the +// GNU General Public License for more details. + +// You should have received a copy of the GNU General Public License +// along with Parity. If not, see . + +import DecodedLog from './decodedLog'; + +const log = new DecodedLog('someParams', 'someAddress'); + +describe('abi/spec/event/DecodedLog', () => { + describe('constructor', () => { + it('sets internal state', () => { + expect(log.params).to.equal('someParams'); + expect(log.address).to.equal('someAddress'); + }); + }); +}); diff --git a/js/src/abi/spec/event/decodedLogParam.js b/js/src/abi/spec/event/decodedLogParam.js new file mode 100644 index 000000000..ed456fcce --- /dev/null +++ b/js/src/abi/spec/event/decodedLogParam.js @@ -0,0 +1,45 @@ +// Copyright 2015, 2016 Ethcore (UK) Ltd. +// This file is part of Parity. + +// Parity is free software: you can redistribute it and/or modify +// it under the terms of the GNU General Public License as published by +// the Free Software Foundation, either version 3 of the License, or +// (at your option) any later version. + +// Parity is distributed in the hope that it will be useful, +// but WITHOUT ANY WARRANTY; without even the implied warranty of +// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the +// GNU General Public License for more details. + +// You should have received a copy of the GNU General Public License +// along with Parity. If not, see . + +import ParamType from '../paramType/paramType'; +import Token from '../../token/token'; +import { isInstanceOf } from '../../util/types'; + +export default class DecodedLogParam { + constructor (name, kind, token) { + if (!isInstanceOf(kind, ParamType)) { + throw new Error('kind not instanceof ParamType'); + } else if (!isInstanceOf(token, Token)) { + throw new Error('token not instanceof Token'); + } + + this._name = name; + this._kind = kind; + this._token = token; + } + + get name () { + return this._name; + } + + get kind () { + return this._kind; + } + + get token () { + return this._token; + } +} diff --git a/js/src/abi/spec/event/decodedLogParam.spec.js b/js/src/abi/spec/event/decodedLogParam.spec.js new file mode 100644 index 000000000..a031282ad --- /dev/null +++ b/js/src/abi/spec/event/decodedLogParam.spec.js @@ -0,0 +1,42 @@ +// Copyright 2015, 2016 Ethcore (UK) Ltd. +// This file is part of Parity. + +// Parity is free software: you can redistribute it and/or modify +// it under the terms of the GNU General Public License as published by +// the Free Software Foundation, either version 3 of the License, or +// (at your option) any later version. + +// Parity is distributed in the hope that it will be useful, +// but WITHOUT ANY WARRANTY; without even the implied warranty of +// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the +// GNU General Public License for more details. + +// You should have received a copy of the GNU General Public License +// along with Parity. If not, see . + +import DecodedLogParam from './decodedLogParam'; +import ParamType from '../paramType'; +import Token from '../../token'; + +describe('abi/spec/event/DecodedLogParam', () => { + describe('constructor', () => { + const pt = new ParamType('bool'); + const tk = new Token('bool'); + + it('disallows kind not instanceof ParamType', () => { + expect(() => new DecodedLogParam('test', 'param')).to.throw(/ParamType/); + }); + + it('disallows token not instanceof Token', () => { + expect(() => new DecodedLogParam('test', pt, 'token')).to.throw(/Token/); + }); + + it('stores all parameters received', () => { + const log = new DecodedLogParam('test', pt, tk); + + expect(log.name).to.equal('test'); + expect(log.kind).to.equal(pt); + expect(log.token).to.equal(tk); + }); + }); +}); diff --git a/js/src/abi/spec/event/event.js b/js/src/abi/spec/event/event.js new file mode 100644 index 000000000..f64fe0498 --- /dev/null +++ b/js/src/abi/spec/event/event.js @@ -0,0 +1,113 @@ +// Copyright 2015, 2016 Ethcore (UK) Ltd. +// This file is part of Parity. + +// Parity is free software: you can redistribute it and/or modify +// it under the terms of the GNU General Public License as published by +// the Free Software Foundation, either version 3 of the License, or +// (at your option) any later version. + +// Parity is distributed in the hope that it will be useful, +// but WITHOUT ANY WARRANTY; without even the implied warranty of +// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the +// GNU General Public License for more details. + +// You should have received a copy of the GNU General Public License +// along with Parity. If not, see . + +import Decoder from '../../decoder/decoder'; +import DecodedLog from './decodedLog'; +import DecodedLogParam from './decodedLogParam'; +import EventParam from './eventParam'; +import { asAddress } from '../../util/sliceAs'; +import { eventSignature } from '../../util/signature'; + +export default class Event { + constructor (abi) { + this._name = abi.name; + this._inputs = EventParam.toEventParams(abi.inputs || []); + this._anonymous = !!abi.anonymous; + + const { id, signature } = eventSignature(this._name, this.inputParamTypes()); + this._id = id; + this._signature = signature; + } + + get name () { + return this._name; + } + + get id () { + return this._id; + } + + get inputs () { + return this._inputs; + } + + get anonymous () { + return this._anonymous; + } + + get signature () { + return this._signature; + } + + inputParamTypes () { + return this._inputs.map((input) => input.kind); + } + + inputParamNames () { + return this._inputs.map((input) => input.name); + } + + indexedParams (indexed) { + return this._inputs.filter((input) => input.indexed === indexed); + } + + decodeLog (topics, data) { + const topicParams = this.indexedParams(true); + const dataParams = this.indexedParams(false); + + let address; + let toSkip; + + if (!this.anonymous) { + address = asAddress(topics[0]); + toSkip = 1; + } else { + toSkip = 0; + } + + const topicTypes = topicParams.map((param) => param.kind); + const flatTopics = topics + .filter((topic, idx) => idx >= toSkip) + .map((topic) => { + return (topic.substr(0, 2) === '0x') + ? topic.substr(2) + : topic; + }).join(''); + const topicTokens = Decoder.decode(topicTypes, flatTopics); + + if (topicTokens.length !== (topics.length - toSkip)) { + throw new Error('Invalid topic data'); + } + + const dataTypes = dataParams.map((param) => param.kind); + const dataTokens = Decoder.decode(dataTypes, data); + + const namedTokens = {}; + + topicParams.forEach((param, idx) => { + namedTokens[param.name] = topicTokens[idx]; + }); + dataParams.forEach((param, idx) => { + namedTokens[param.name] = dataTokens[idx]; + }); + + const inputParamTypes = this.inputParamTypes(); + const decodedParams = this.inputParamNames() + .map((name, idx) => new DecodedLogParam(name, inputParamTypes[idx], namedTokens[name])); + + return new DecodedLog(decodedParams, address); + } +} diff --git a/js/src/abi/spec/event/event.spec.js b/js/src/abi/spec/event/event.spec.js new file mode 100644 index 000000000..f7252a9d2 --- /dev/null +++ b/js/src/abi/spec/event/event.spec.js @@ -0,0 +1,111 @@ +// Copyright 2015, 2016 Ethcore (UK) Ltd. +// This file is part of Parity. + +// Parity is free software: you can redistribute it and/or modify +// it under the terms of the GNU General Public License as published by +// the Free Software Foundation, either version 3 of the License, or +// (at your option) any later version. + +// Parity is distributed in the hope that it will be useful, +// but WITHOUT ANY WARRANTY; without even the implied warranty of +// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the +// GNU General Public License for more details. + +// You should have received a copy of the GNU General Public License +// along with Parity. If not, see . + +import BigNumber from 'bignumber.js'; + +import Event from './event'; +import EventParam from './eventParam'; +import DecodedLogParam from './decodedLogParam'; +import ParamType from '../paramType'; +import Token from '../../token'; + +describe('abi/spec/event/Event', () => { + const inputArr = [{ name: 'a', type: 'bool' }, { name: 'b', type: 'uint', indexed: true }]; + const inputs = [new EventParam('a', 'bool', false), new EventParam('b', 'uint', true)]; + const event = new Event({ name: 'test', inputs: inputArr, anonymous: true }); + + describe('constructor', () => { + it('stores the parameters as received', () => { + expect(event.name).to.equal('test'); + expect(event.inputs).to.deep.equal(inputs); + expect(event.anonymous).to.be.true; + }); + + it('matches empty inputs with []', () => { + expect(new Event({ name: 'test' }).inputs).to.deep.equal([]); + }); + + it('sets the event signature', () => { + expect(new Event({ name: 'baz' }).signature) + .to.equal('a7916fac4f538170f7cd12c148552e2cba9fcd72329a2dd5b07a6fa906488ddf'); + }); + }); + + describe('inputParamTypes', () => { + it('returns all the types', () => { + expect(event.inputParamTypes()).to.deep.equal([new ParamType('bool'), new ParamType('uint', null, 256, true)]); + }); + }); + + describe('inputParamNames', () => { + it('returns all the names', () => { + expect(event.inputParamNames()).to.deep.equal(['a', 'b']); + }); + }); + + describe('indexedParams', () => { + it('returns all indexed parameters (indexed)', () => { + expect(event.indexedParams(true)).to.deep.equal([inputs[1]]); + }); + + it('returns all indexed parameters (non-indexed)', () => { + expect(event.indexedParams(false)).to.deep.equal([inputs[0]]); + }); + }); + + describe('decodeLog', () => { + it('decodes an event', () => { + const event = new Event({ + name: 'foo', + inputs: [ + { name: 'a', type: 'int' }, + { name: 'b', type: 'int', indexed: true }, + { name: 'c', type: 'address' }, + { name: 'd', type: 'address', indexed: true } + ] + }); + const decoded = event.decodeLog([ + '0000000000000000000000004444444444444444444444444444444444444444', + '0000000000000000000000000000000000000000000000000000000000000002', + '0000000000000000000000001111111111111111111111111111111111111111' ], + '00000000000000000000000000000000000000000000000000000000000000030000000000000000000000002222222222222222222222222222222222222222'); + + expect(decoded.address).to.equal('0x4444444444444444444444444444444444444444'); + expect(decoded.params).to.deep.equal([ + new DecodedLogParam('a', new ParamType('int', null, 256), new Token('int', new BigNumber(3))), + new DecodedLogParam('b', new ParamType('int', null, 256, true), new Token('int', new BigNumber(2))), + new DecodedLogParam('c', new ParamType('address'), new Token('address', '0x2222222222222222222222222222222222222222')), + new DecodedLogParam('d', new ParamType('address', null, 0, true), new Token('address', '0x1111111111111111111111111111111111111111')) + ]); + }); + + it('decodes an anonymous event', () => { + const event = new Event({ name: 'foo', inputs: [{ name: 'a', type: 'int' }], anonymous: true }); + const decoded = event.decodeLog([], '0000000000000000000000000000000000000000000000000000000000000003'); + + expect(decoded.address).to.not.be.ok; + expect(decoded.params).to.deep.equal([ + new DecodedLogParam('a', new ParamType('int', null, 256), new Token('int', new BigNumber(3))) + ]); + }); + + it('throws on invalid topics', () => { + const event = new Event({ name: 'foo', inputs: [{ name: 'a', type: 'int' }], anonymous: true }); + + expect(() => event.decodeLog(['0000000000000000000000004444444444444444444444444444444444444444'], '0000000000000000000000000000000000000000000000000000000000000003')).to.throw(/Invalid/); + }); + }); +}); diff --git a/js/src/abi/spec/event/eventParam.js b/js/src/abi/spec/event/eventParam.js new file mode 100644 index 000000000..d8a3b585d --- /dev/null +++ b/js/src/abi/spec/event/eventParam.js @@ -0,0 +1,41 @@ +// Copyright 2015, 2016 Ethcore (UK) Ltd. +// This file is part of Parity. + +// Parity is free software: you can redistribute it and/or modify +// it under the terms of the GNU General Public License as published by +// the Free Software Foundation, either version 3 of the License, or +// (at your option) any later version. + +// Parity is distributed in the hope that it will be useful, +// but WITHOUT ANY WARRANTY; without even the implied warranty of +// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the +// GNU General Public License for more details. + +// You should have received a copy of the GNU General Public License +// along with Parity. If not, see . + +import { toParamType } from '../paramType/format'; + +export default class EventParam { + constructor (name, type, indexed = false) { + this._name = name; + this._indexed = indexed; + this._kind = toParamType(type, indexed); + } + + get name () { + return this._name; + } + + get kind () { + return this._kind; + } + + get indexed () { + return this._indexed; + } + + static toEventParams (params) { + return params.map((param) => new EventParam(param.name, param.type, param.indexed)); + } +} diff --git a/js/src/abi/spec/event/eventParam.spec.js b/js/src/abi/spec/event/eventParam.spec.js new file mode 100644 index 000000000..6858e9800 --- /dev/null +++ b/js/src/abi/spec/event/eventParam.spec.js @@ -0,0 +1,43 @@ +// Copyright 2015, 2016 Ethcore (UK) Ltd. +// This file is part of Parity. + +// Parity is free software: you can redistribute it and/or modify +// it under the terms of the GNU General Public License as published by +// the Free Software Foundation, either version 3 of the License, or +// (at your option) any later version. + +// Parity is distributed in the hope that it will be useful, +// but WITHOUT ANY WARRANTY; without even the implied warranty of +// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the +// GNU General Public License for more details. + +// You should have received a copy of the GNU General Public License +// along with Parity. If not, see . + +import EventParam from './eventParam'; + +describe('abi/spec/event/EventParam', () => { + describe('constructor', () => { + it('sets the properties', () => { + const param = new EventParam('foo', 'uint', true); + expect(param.name).to.equal('foo'); + expect(param.kind.type).to.equal('uint'); + expect(param.indexed).to.be.true; + }); + + it('uses defaults for indexed', () => { + expect(new EventParam('foo', 'uint').indexed).to.be.false; + }); + }); + + describe('toEventParams', () => { + it('maps an array of params', () => { + const params = EventParam.toEventParams([{ name: 'foo', type: 'uint' }]); + + expect(params.length).to.equal(1); + expect(params[0].indexed).to.be.false; + expect(params[0].name).to.equal('foo'); + expect(params[0].kind.type).to.equal('uint'); + }); + }); +}); diff --git a/js/src/abi/spec/event/index.js b/js/src/abi/spec/event/index.js new file mode 100644 index 000000000..0925882d3 --- /dev/null +++ b/js/src/abi/spec/event/index.js @@ -0,0 +1,17 @@ +// Copyright 2015, 2016 Ethcore (UK) Ltd. +// This file is part of Parity. + +// Parity is free software: you can redistribute it and/or modify +// it under the terms of the GNU General Public License as published by +// the Free Software Foundation, either version 3 of the License, or +// (at your option) any later version. + +// Parity is distributed in the hope that it will be useful, +// but WITHOUT ANY WARRANTY; without even the implied warranty of +// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the +// GNU General Public License for more details. + +// You should have received a copy of the GNU General Public License +// along with Parity. If not, see . + +export default from './event'; diff --git a/js/src/abi/spec/function.js b/js/src/abi/spec/function.js new file mode 100644 index 000000000..0c91a9b6f --- /dev/null +++ b/js/src/abi/spec/function.js @@ -0,0 +1,87 @@ +// Copyright 2015, 2016 Ethcore (UK) Ltd. +// This file is part of Parity. + +// Parity is free software: you can redistribute it and/or modify +// it under the terms of the GNU General Public License as published by +// the Free Software Foundation, either version 3 of the License, or +// (at your option) any later version. + +// Parity is distributed in the hope that it will be useful, +// but WITHOUT ANY WARRANTY; without even the implied warranty of +// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the +// GNU General Public License for more details. + +// You should have received a copy of the GNU General Public License +// along with Parity. If not, see . + +import Decoder from '../decoder/decoder'; +import Encoder from '../encoder/encoder'; +import Param from './param'; +import { methodSignature } from '../util/signature'; + +export default class Func { + constructor (abi) { + this._abi = abi; + this._name = abi.name; + this._constant = !!abi.constant; + this._payable = abi.payable; + this._inputs = Param.toParams(abi.inputs || []); + this._outputs = Param.toParams(abi.outputs || []); + + const { id, signature } = methodSignature(this._name, this.inputParamTypes()); + this._id = id; + this._signature = signature; + } + + get abi () { + return this._abi; + } + + get constant () { + return this._constant; + } + + get name () { + return this._name; + } + + get id () { + return this._id; + } + + get payable () { + return this._payable; + } + + get inputs () { + return this._inputs; + } + + get outputs () { + return this._outputs; + } + + get signature () { + return this._signature; + } + + inputParamTypes () { + return this._inputs.map((input) => input.kind); + } + + outputParamTypes () { + return this._outputs.map((output) => output.kind); + } + + encodeCall (tokens) { + return `${this._signature}${Encoder.encode(tokens)}`; + } + + decodeInput (data) { + return Decoder.decode(this.inputParamTypes(), data); + } + + decodeOutput (data) { + return Decoder.decode(this.outputParamTypes(), data); + } +} diff --git a/js/src/abi/spec/function.spec.js b/js/src/abi/spec/function.spec.js new file mode 100644 index 000000000..1650bd314 --- /dev/null +++ b/js/src/abi/spec/function.spec.js @@ -0,0 +1,89 @@ +// Copyright 2015, 2016 Ethcore (UK) Ltd. +// This file is part of Parity. + +// Parity is free software: you can redistribute it and/or modify +// it under the terms of the GNU General Public License as published by +// the Free Software Foundation, either version 3 of the License, or +// (at your option) any later version. + +// Parity is distributed in the hope that it will be useful, +// but WITHOUT ANY WARRANTY; without even the implied warranty of +// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the +// GNU General Public License for more details. + +// You should have received a copy of the GNU General Public License +// along with Parity. If not, see . + +import Func from './function'; +import Param from './param'; +import Token from '../token'; + +describe('abi/spec/Function', () => { + const inputsArr = [{ name: 'boolin', type: 'bool' }, { name: 'stringin', type: 'string' }]; + const outputsArr = [{ name: 'output', type: 'uint' }]; + + const uint = new Param('output', 'uint'); + const bool = new Param('boolin', 'bool'); + const string = new Param('stringin', 'string'); + const inputs = [bool, string]; + const outputs = [uint]; + + const func = new Func({ + name: 'test', + inputs: inputsArr, + outputs: outputsArr + }); + + describe('constructor', () => { + it('stores the parameters as received', () => { + expect(func.name).to.equal('test'); + expect(func.constant).to.be.false; + expect(func.inputs).to.deep.equal(inputs); + expect(func.outputs).to.deep.equal(outputs); + }); + + it('matches empty inputs with []', () => { + expect(new Func({ name: 'test', outputs: outputsArr }).inputs).to.deep.equal([]); + }); + + it('matches empty outputs with []', () => { + expect(new Func({ name: 'test', inputs: inputsArr }).outputs).to.deep.equal([]); + }); + + it('sets the method signature', () => { + expect(new Func({ name: 'baz' }).signature).to.equal('a7916fac'); + }); + + it('allows constant functions', () => { + expect(new Func({ name: 'baz', constant: true }).constant).to.be.true; + }); + }); + + describe('inputParamTypes', () => { + it('retrieves the input types as received', () => { + expect(func.inputParamTypes()).to.deep.equal([bool.kind, string.kind]); + }); + }); + + describe('outputParamTypes', () => { + it('retrieves the output types as received', () => { + expect(func.outputParamTypes()).to.deep.equal([uint.kind]); + }); + }); + + describe('encodeCall', () => { + it('encodes the call correctly', () => { + const result = func.encodeCall([new Token('bool', true), new Token('string', 'jacogr')]); + + expect(result).to.equal('023562050000000000000000000000000000000000000000000000000000000000000001000000000000000000000000000000000000000000000000000000000000004000000000000000000000000000000000000000000000000000000000000000066a61636f67720000000000000000000000000000000000000000000000000000'); + }); + }); + + describe('decodeOutput', () => { + it('decodes the result correctly', () => { + const result = func.decodeOutput('1111111111111111111111111111111111111111111111111111111111111111'); + + expect(result[0].value.toString(16)).to.equal('1111111111111111111111111111111111111111111111111111111111111111'); + }); + }); +}); diff --git a/js/src/abi/spec/index.js b/js/src/abi/spec/index.js new file mode 100644 index 000000000..89354d49b --- /dev/null +++ b/js/src/abi/spec/index.js @@ -0,0 +1,17 @@ +// Copyright 2015, 2016 Ethcore (UK) Ltd. +// This file is part of Parity. + +// Parity is free software: you can redistribute it and/or modify +// it under the terms of the GNU General Public License as published by +// the Free Software Foundation, either version 3 of the License, or +// (at your option) any later version. + +// Parity is distributed in the hope that it will be useful, +// but WITHOUT ANY WARRANTY; without even the implied warranty of +// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the +// GNU General Public License for more details. + +// You should have received a copy of the GNU General Public License +// along with Parity. If not, see . + +export default from './interface'; diff --git a/js/src/abi/spec/interface.js b/js/src/abi/spec/interface.js new file mode 100644 index 000000000..1ea32e9a9 --- /dev/null +++ b/js/src/abi/spec/interface.js @@ -0,0 +1,73 @@ +// Copyright 2015, 2016 Ethcore (UK) Ltd. +// This file is part of Parity. + +// Parity is free software: you can redistribute it and/or modify +// it under the terms of the GNU General Public License as published by +// the Free Software Foundation, either version 3 of the License, or +// (at your option) any later version. + +// Parity is distributed in the hope that it will be useful, +// but WITHOUT ANY WARRANTY; without even the implied warranty of +// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the +// GNU General Public License for more details. + +// You should have received a copy of the GNU General Public License +// along with Parity. If not, see . + +import Constructor from './constructor'; +import Event from './event/event'; +import Func from './function'; +import Token from '../token'; + +export default class Interface { + constructor (abi) { + this._interface = Interface.parseABI(abi); + } + + get interface () { + return this._interface; + } + + get constructors () { + return this._interface.filter((item) => item instanceof Constructor); + } + + get events () { + return this._interface.filter((item) => item instanceof Event); + } + + get functions () { + return this._interface.filter((item) => item instanceof Func); + } + + encodeTokens (paramTypes, values) { + const createToken = function (paramType, value) { + if (paramType.subtype) { + return new Token(paramType.type, value.map((entry) => createToken(paramType.subtype, entry))); + } + + return new Token(paramType.type, value); + }; + + return paramTypes.map((paramType, idx) => createToken(paramType, values[idx])); + } + + static parseABI (abi) { + return abi.map((item) => { + switch (item.type) { + case 'constructor': + return new Constructor(item); + + case 'event': + return new Event(item); + + case 'function': + case 'fallback': + return new Func(item); + + default: + throw new Error(`Unknown ABI type ${item.type}`); + } + }); + } +} diff --git a/js/src/abi/spec/interface.spec.js b/js/src/abi/spec/interface.spec.js new file mode 100644 index 000000000..ba5c38a28 --- /dev/null +++ b/js/src/abi/spec/interface.spec.js @@ -0,0 +1,126 @@ +// Copyright 2015, 2016 Ethcore (UK) Ltd. +// This file is part of Parity. + +// Parity is free software: you can redistribute it and/or modify +// it under the terms of the GNU General Public License as published by +// the Free Software Foundation, either version 3 of the License, or +// (at your option) any later version. + +// Parity is distributed in the hope that it will be useful, +// but WITHOUT ANY WARRANTY; without even the implied warranty of +// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the +// GNU General Public License for more details. + +// You should have received a copy of the GNU General Public License +// along with Parity. If not, see . + +import Interface from './interface'; +import ParamType from './paramType'; +import Token from '../token'; + +describe('abi/spec/Interface', () => { + const construct = { + type: 'constructor', + inputs: [] + }; + const event = { + type: 'event', + name: 'Event2', + anonymous: false, + inputs: [{ name: 'a', type: 'uint256', indexed: true }, { name: 'b', type: 'bytes32', indexed: false }] + }; + const func = { + type: 'function', + name: 'foo', + inputs: [{ name: 'a', type: 'uint256' }], + outputs: [] + }; + + describe('parseABI', () => { + it('throws on invalid types', () => { + expect(() => Interface.parseABI([{ type: 'noMatch' }])).to.throw(/noMatch/); + }); + + it('creates constructors', () => { + expect(Interface.parseABI([ construct ])).to.deep.equal([{ _inputs: [] }]); + }); + + it('creates events', () => { + expect(Interface.parseABI([ event ])[0].name).to.equal('Event2'); + }); + + it('creates functions', () => { + expect(Interface.parseABI([ func ])[0].name).to.equal('foo'); + }); + + it('parse complex interfaces', () => { + expect(Interface.parseABI([ construct, event, func ]).length).to.equal(3); + }); + }); + + describe('constructor', () => { + const int = new Interface([ construct, event, func ]); + + it('contains the full interface', () => { + expect(int.interface.length).to.equal(3); + }); + + it('contains the constructors', () => { + expect(int.constructors.length).to.equal(1); + }); + + it('contains the events', () => { + expect(int.events.length).to.equal(1); + }); + + it('contains the functions', () => { + expect(int.functions.length).to.equal(1); + }); + }); + + describe('encodeTokens', () => { + const int = new Interface([ construct, event, func ]); + + it('encodes simple types', () => { + expect( + int.encodeTokens( + [new ParamType('bool'), new ParamType('string'), new ParamType('int'), new ParamType('uint')], + [true, 'gavofyork', -123, 123] + ) + ).to.deep.equal([ + new Token('bool', true), new Token('string', 'gavofyork'), new Token('int', -123), new Token('uint', 123) + ]); + }); + + it('encodes array', () => { + expect( + int.encodeTokens( + [new ParamType('array', new ParamType('bool'))], + [[true, false, true]] + ) + ).to.deep.equal([ + new Token('array', [ + new Token('bool', true), new Token('bool', false), new Token('bool', true) + ]) + ]); + }); + + it('encodes simple with array of array', () => { + expect( + int.encodeTokens( + [ + new ParamType('bool'), + new ParamType('fixedArray', new ParamType('array', new ParamType('uint')), 2) + ], + [true, [[0, 1], [2, 3]]] + ) + ).to.deep.equal([ + new Token('bool', true), + new Token('fixedArray', [ + new Token('array', [new Token('uint', 0), new Token('uint', 1)]), + new Token('array', [new Token('uint', 2), new Token('uint', 3)]) + ]) + ]); + }); + }); +}); diff --git a/js/src/abi/spec/param.js b/js/src/abi/spec/param.js new file mode 100644 index 000000000..95c3b9d18 --- /dev/null +++ b/js/src/abi/spec/param.js @@ -0,0 +1,36 @@ +// Copyright 2015, 2016 Ethcore (UK) Ltd. +// This file is part of Parity. + +// Parity is free software: you can redistribute it and/or modify +// it under the terms of the GNU General Public License as published by +// the Free Software Foundation, either version 3 of the License, or +// (at your option) any later version. + +// Parity is distributed in the hope that it will be useful, +// but WITHOUT ANY WARRANTY; without even the implied warranty of +// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the +// GNU General Public License for more details. + +// You should have received a copy of the GNU General Public License +// along with Parity. If not, see . + +import { toParamType } from './paramType/format'; + +export default class Param { + constructor (name, type) { + this._name = name; + this._kind = toParamType(type); + } + + get name () { + return this._name; + } + + get kind () { + return this._kind; + } + + static toParams (params) { + return params.map((param) => new Param(param.name, param.type)); + } +} diff --git a/js/src/abi/spec/param.spec.js b/js/src/abi/spec/param.spec.js new file mode 100644 index 000000000..bd172e4a6 --- /dev/null +++ b/js/src/abi/spec/param.spec.js @@ -0,0 +1,38 @@ +// Copyright 2015, 2016 Ethcore (UK) Ltd. +// This file is part of Parity. + +// Parity is free software: you can redistribute it and/or modify +// it under the terms of the GNU General Public License as published by +// the Free Software Foundation, either version 3 of the License, or +// (at your option) any later version. + +// Parity is distributed in the hope that it will be useful, +// but WITHOUT ANY WARRANTY; without even the implied warranty of +// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the +// GNU General Public License for more details. + +// You should have received a copy of the GNU General Public License +// along with Parity. If not, see . + +import Param from './param'; + +describe('abi/spec/Param', () => { + describe('constructor', () => { + const param = new Param('foo', 'uint'); + + it('sets the properties', () => { + expect(param.name).to.equal('foo'); + expect(param.kind.type).to.equal('uint'); + }); + }); + + describe('toParams', () => { + it('maps an array of params', () => { + const params = Param.toParams([{ name: 'foo', type: 'uint' }]); + + expect(params.length).to.equal(1); + expect(params[0].name).to.equal('foo'); + expect(params[0].kind.type).to.equal('uint'); + }); + }); +}); diff --git a/js/src/abi/spec/paramType/format.js b/js/src/abi/spec/paramType/format.js new file mode 100644 index 000000000..459eb15f4 --- /dev/null +++ b/js/src/abi/spec/paramType/format.js @@ -0,0 +1,80 @@ +// Copyright 2015, 2016 Ethcore (UK) Ltd. +// This file is part of Parity. + +// Parity is free software: you can redistribute it and/or modify +// it under the terms of the GNU General Public License as published by +// the Free Software Foundation, either version 3 of the License, or +// (at your option) any later version. + +// Parity is distributed in the hope that it will be useful, +// but WITHOUT ANY WARRANTY; without even the implied warranty of +// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the +// GNU General Public License for more details. + +// You should have received a copy of the GNU General Public License +// along with Parity. If not, see . + +import ParamType from './paramType'; + +export function toParamType (type, indexed) { + if (type[type.length - 1] === ']') { + const last = type.lastIndexOf('['); + const length = type.substr(last + 1, type.length - last - 2); + const subtype = toParamType(type.substr(0, last)); + + if (length.length === 0) { + return new ParamType('array', subtype, 0, indexed); + } + + return new ParamType('fixedArray', subtype, parseInt(length, 10), indexed); + } + + switch (type) { + case 'address': + case 'bool': + case 'bytes': + case 'string': + return new ParamType(type, null, 0, indexed); + + case 'int': + case 'uint': + return new ParamType(type, null, 256, indexed); + + default: + if (type.indexOf('uint') === 0) { + return new ParamType('uint', null, parseInt(type.substr(4), 10), indexed); + } else if (type.indexOf('int') === 0) { + return new ParamType('int', null, parseInt(type.substr(3), 10), indexed); + } else if (type.indexOf('bytes') === 0) { + return new ParamType('fixedBytes', null, parseInt(type.substr(5), 10), indexed); + } + + throw new Error(`Cannot convert ${type} to valid ParamType`); + } +} + +export function fromParamType (paramType) { + switch (paramType.type) { + case 'address': + case 'bool': + case 'bytes': + case 'string': + return paramType.type; + + case 'int': + case 'uint': + return `${paramType.type}${paramType.length}`; + + case 'fixedBytes': + return `bytes${paramType.length}`; + + case 'fixedArray': + return `${fromParamType(paramType.subtype)}[${paramType.length}]`; + + case 'array': + return `${fromParamType(paramType.subtype)}[]`; + + default: + throw new Error(`Cannot convert from ParamType ${paramType.type}`); + } +} diff --git a/js/src/abi/spec/paramType/format.spec.js b/js/src/abi/spec/paramType/format.spec.js new file mode 100644 index 000000000..90e5229d5 --- /dev/null +++ b/js/src/abi/spec/paramType/format.spec.js @@ -0,0 +1,228 @@ +// Copyright 2015, 2016 Ethcore (UK) Ltd. +// This file is part of Parity. + +// Parity is free software: you can redistribute it and/or modify +// it under the terms of the GNU General Public License as published by +// the Free Software Foundation, either version 3 of the License, or +// (at your option) any later version. + +// Parity is distributed in the hope that it will be useful, +// but WITHOUT ANY WARRANTY; without even the implied warranty of +// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the +// GNU General Public License for more details. + +// You should have received a copy of the GNU General Public License +// along with Parity. If not, see . + +import ParamType from './paramType'; +import { fromParamType, toParamType } from './format'; + +describe('abi/spec/paramType/format', () => { + describe('fromParamType', () => { + it('errors on invalid types', () => { + expect(() => fromParamType({ type: 'noMatch' })).to.throw(/noMatch/); + }); + + describe('simple types', () => { + it('converts address to address', () => { + const pt = new ParamType('address'); + + expect(fromParamType(pt)).to.equal('address'); + }); + + it('converts bool to bool', () => { + const pt = new ParamType('bool'); + + expect(fromParamType(pt)).to.equal('bool'); + }); + + it('converts bytes to bytes', () => { + const pt = new ParamType('bytes'); + + expect(fromParamType(pt)).to.equal('bytes'); + }); + + it('converts string to string', () => { + const pt = new ParamType('string'); + + expect(fromParamType(pt)).to.equal('string'); + }); + }); + + describe('length types', () => { + it('converts int32 to int32', () => { + const pt = new ParamType('int', null, 32); + + expect(fromParamType(pt)).to.equal('int32'); + }); + + it('converts uint64 to int64', () => { + const pt = new ParamType('uint', null, 64); + + expect(fromParamType(pt)).to.equal('uint64'); + }); + + it('converts fixedBytes8 to bytes8', () => { + const pt = new ParamType('fixedBytes', null, 8); + + expect(fromParamType(pt)).to.equal('bytes8'); + }); + }); + + describe('arrays', () => { + it('converts string[2] to string[2]', () => { + const pt = new ParamType('fixedArray', new ParamType('string'), 2); + + expect(fromParamType(pt)).to.equal('string[2]'); + }); + + it('converts bool[] to bool[]', () => { + const pt = new ParamType('array', new ParamType('bool')); + + expect(fromParamType(pt)).to.equal('bool[]'); + }); + + it('converts bool[][2] to bool[][2]', () => { + const pt = new ParamType('fixedArray', new ParamType('array', new ParamType('bool')), 2); + + expect(fromParamType(pt)).to.equal('bool[][2]'); + }); + + it('converts bool[2][] to bool[2][]', () => { + const pt = new ParamType('array', new ParamType('fixedArray', new ParamType('bool'), 2)); + + expect(fromParamType(pt)).to.equal('bool[2][]'); + }); + }); + }); + + describe('toParamType', () => { + it('errors on invalid types', () => { + expect(() => toParamType('noMatch')).to.throw(/noMatch/); + }); + + describe('simple mapping', () => { + it('converts address to address', () => { + const pt = toParamType('address'); + + expect(pt.type).to.equal('address'); + }); + + it('converts bool to bool', () => { + const pt = toParamType('bool'); + + expect(pt.type).to.equal('bool'); + }); + + it('converts bytes to bytes', () => { + const pt = toParamType('bytes'); + + expect(pt.type).to.equal('bytes'); + }); + + it('converts string to string', () => { + const pt = toParamType('string'); + + expect(pt.type).to.equal('string'); + }); + }); + + describe('number', () => { + it('converts int to int256', () => { + const pt = toParamType('int'); + + expect(pt.type).to.equal('int'); + expect(pt.length).to.equal(256); + }); + + it('converts uint to uint256', () => { + const pt = toParamType('uint'); + + expect(pt.type).to.equal('uint'); + expect(pt.length).to.equal(256); + }); + }); + + describe('sized types', () => { + it('converts int32 to int32', () => { + const pt = toParamType('int32'); + + expect(pt.type).to.equal('int'); + expect(pt.length).to.equal(32); + }); + + it('converts uint16 to uint16', () => { + const pt = toParamType('uint32'); + + expect(pt.type).to.equal('uint'); + expect(pt.length).to.equal(32); + }); + + it('converts bytes8 to fixedBytes8', () => { + const pt = toParamType('bytes8'); + + expect(pt.type).to.equal('fixedBytes'); + expect(pt.length).to.equal(8); + }); + }); + + describe('arrays', () => { + describe('fixed arrays', () => { + it('creates fixed array', () => { + const pt = toParamType('bytes[8]'); + + expect(pt.type).to.equal('fixedArray'); + expect(pt.subtype.type).to.equal('bytes'); + expect(pt.length).to.equal(8); + }); + + it('creates fixed arrays of fixed arrays', () => { + const pt = toParamType('bytes[45][3]'); + + expect(pt.type).to.equal('fixedArray'); + expect(pt.length).to.equal(3); + expect(pt.subtype.type).to.equal('fixedArray'); + expect(pt.subtype.length).to.equal(45); + expect(pt.subtype.subtype.type).to.equal('bytes'); + }); + }); + + describe('dynamic arrays', () => { + it('creates a dynamic array', () => { + const pt = toParamType('bytes[]'); + + expect(pt.type).to.equal('array'); + expect(pt.subtype.type).to.equal('bytes'); + }); + + it('creates a dynamic array of dynamic arrays', () => { + const pt = toParamType('bool[][]'); + + expect(pt.type).to.equal('array'); + expect(pt.subtype.type).to.equal('array'); + expect(pt.subtype.subtype.type).to.equal('bool'); + }); + }); + + describe('mixed arrays', () => { + it('creates a fixed dynamic array', () => { + const pt = toParamType('bool[][3]'); + + expect(pt.type).to.equal('fixedArray'); + expect(pt.length).to.equal(3); + expect(pt.subtype.type).to.equal('array'); + expect(pt.subtype.subtype.type).to.equal('bool'); + }); + + it('creates a dynamic fixed array', () => { + const pt = toParamType('bool[3][]'); + + expect(pt.type).to.equal('array'); + expect(pt.subtype.type).to.equal('fixedArray'); + expect(pt.subtype.length).to.equal(3); + expect(pt.subtype.subtype.type).to.equal('bool'); + }); + }); + }); + }); +}); diff --git a/js/src/abi/spec/paramType/index.js b/js/src/abi/spec/paramType/index.js new file mode 100644 index 000000000..23bb83f06 --- /dev/null +++ b/js/src/abi/spec/paramType/index.js @@ -0,0 +1,17 @@ +// Copyright 2015, 2016 Ethcore (UK) Ltd. +// This file is part of Parity. + +// Parity is free software: you can redistribute it and/or modify +// it under the terms of the GNU General Public License as published by +// the Free Software Foundation, either version 3 of the License, or +// (at your option) any later version. + +// Parity is distributed in the hope that it will be useful, +// but WITHOUT ANY WARRANTY; without even the implied warranty of +// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the +// GNU General Public License for more details. + +// You should have received a copy of the GNU General Public License +// along with Parity. If not, see . + +export default from './paramType'; diff --git a/js/src/abi/spec/paramType/paramType.js b/js/src/abi/spec/paramType/paramType.js new file mode 100644 index 000000000..99a2915d6 --- /dev/null +++ b/js/src/abi/spec/paramType/paramType.js @@ -0,0 +1,52 @@ +// Copyright 2015, 2016 Ethcore (UK) Ltd. +// This file is part of Parity. + +// Parity is free software: you can redistribute it and/or modify +// it under the terms of the GNU General Public License as published by +// the Free Software Foundation, either version 3 of the License, or +// (at your option) any later version. + +// Parity is distributed in the hope that it will be useful, +// but WITHOUT ANY WARRANTY; without even the implied warranty of +// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the +// GNU General Public License for more details. + +// You should have received a copy of the GNU General Public License +// along with Parity. If not, see . + +import TYPES from './types'; + +export default class ParamType { + constructor (type, subtype = null, length = 0, indexed = false) { + ParamType.validateType(type); + + this._type = type; + this._subtype = subtype; + this._length = length; + this._indexed = indexed; + } + + get type () { + return this._type; + } + + get subtype () { + return this._subtype; + } + + get length () { + return this._length; + } + + get indexed () { + return this._indexed; + } + + static validateType (type) { + if (TYPES.filter((_type) => type === _type).length) { + return true; + } + + throw new Error(`Invalid type ${type} received for ParamType`); + } +} diff --git a/js/src/abi/spec/paramType/paramType.spec.js b/js/src/abi/spec/paramType/paramType.spec.js new file mode 100644 index 000000000..e8d8c3254 --- /dev/null +++ b/js/src/abi/spec/paramType/paramType.spec.js @@ -0,0 +1,87 @@ +// Copyright 2015, 2016 Ethcore (UK) Ltd. +// This file is part of Parity. + +// Parity is free software: you can redistribute it and/or modify +// it under the terms of the GNU General Public License as published by +// the Free Software Foundation, either version 3 of the License, or +// (at your option) any later version. + +// Parity is distributed in the hope that it will be useful, +// but WITHOUT ANY WARRANTY; without even the implied warranty of +// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the +// GNU General Public License for more details. + +// You should have received a copy of the GNU General Public License +// along with Parity. If not, see . + +import ParamType from './paramType'; + +describe('abi/spec/paramType/ParamType', () => { + describe('validateType', () => { + it('validates address', () => { + expect(ParamType.validateType('address')).to.be.true; + }); + + it('validates fixedArray', () => { + expect(ParamType.validateType('fixedArray')).to.be.true; + }); + + it('validates array', () => { + expect(ParamType.validateType('array')).to.be.true; + }); + + it('validates fixedBytes', () => { + expect(ParamType.validateType('fixedBytes')).to.be.true; + }); + + it('validates bytes', () => { + expect(ParamType.validateType('bytes')).to.be.true; + }); + + it('validates bool', () => { + expect(ParamType.validateType('bool')).to.be.true; + }); + + it('validates int', () => { + expect(ParamType.validateType('int')).to.be.true; + }); + + it('validates uint', () => { + expect(ParamType.validateType('uint')).to.be.true; + }); + + it('validates string', () => { + expect(ParamType.validateType('string')).to.be.true; + }); + + it('throws an error on invalid types', () => { + expect(() => ParamType.validateType('noMatch')).to.throw(/noMatch/); + }); + }); + + describe('constructor', () => { + it('throws an error on invalid types', () => { + expect(() => new ParamType('noMatch')).to.throw(/noMatch/); + }); + + it('sets the type of the object', () => { + expect((new ParamType('bool', null, 1)).type).to.equal('bool'); + }); + + it('sets the subtype of the object', () => { + expect((new ParamType('array', 'bool', 1)).subtype).to.equal('bool'); + }); + + it('sets the length of the object', () => { + expect((new ParamType('array', 'bool', 1)).length).to.equal(1); + }); + + it('sets the index of the object', () => { + expect((new ParamType('array', 'bool', 1, true)).indexed).to.be.true; + }); + + it('sets default values where none supplied', () => { + expect(Object.values(new ParamType('string'))).to.deep.equal(['string', null, 0, false]); + }); + }); +}); diff --git a/js/src/abi/spec/paramType/types.js b/js/src/abi/spec/paramType/types.js new file mode 100644 index 000000000..d789a6ed8 --- /dev/null +++ b/js/src/abi/spec/paramType/types.js @@ -0,0 +1,19 @@ +// Copyright 2015, 2016 Ethcore (UK) Ltd. +// This file is part of Parity. + +// Parity is free software: you can redistribute it and/or modify +// it under the terms of the GNU General Public License as published by +// the Free Software Foundation, either version 3 of the License, or +// (at your option) any later version. + +// Parity is distributed in the hope that it will be useful, +// but WITHOUT ANY WARRANTY; without even the implied warranty of +// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the +// GNU General Public License for more details. + +// You should have received a copy of the GNU General Public License +// along with Parity. If not, see . + +const TYPES = ['address', 'bytes', 'int', 'uint', 'bool', 'string', 'array', 'fixedBytes', 'fixedArray']; + +export default TYPES; diff --git a/js/src/abi/token/index.js b/js/src/abi/token/index.js new file mode 100644 index 000000000..4b822b4bd --- /dev/null +++ b/js/src/abi/token/index.js @@ -0,0 +1,17 @@ +// Copyright 2015, 2016 Ethcore (UK) Ltd. +// This file is part of Parity. + +// Parity is free software: you can redistribute it and/or modify +// it under the terms of the GNU General Public License as published by +// the Free Software Foundation, either version 3 of the License, or +// (at your option) any later version. + +// Parity is distributed in the hope that it will be useful, +// but WITHOUT ANY WARRANTY; without even the implied warranty of +// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the +// GNU General Public License for more details. + +// You should have received a copy of the GNU General Public License +// along with Parity. If not, see . + +export default from './token'; diff --git a/js/src/abi/token/token.js b/js/src/abi/token/token.js new file mode 100644 index 000000000..84c675ee6 --- /dev/null +++ b/js/src/abi/token/token.js @@ -0,0 +1,42 @@ +// Copyright 2015, 2016 Ethcore (UK) Ltd. +// This file is part of Parity. + +// Parity is free software: you can redistribute it and/or modify +// it under the terms of the GNU General Public License as published by +// the Free Software Foundation, either version 3 of the License, or +// (at your option) any later version. + +// Parity is distributed in the hope that it will be useful, +// but WITHOUT ANY WARRANTY; without even the implied warranty of +// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the +// GNU General Public License for more details. + +// You should have received a copy of the GNU General Public License +// along with Parity. If not, see . + +import TYPES from '../spec/paramType/types'; + +export default class Token { + constructor (type, value) { + Token.validateType(type); + + this._type = type; + this._value = value; + } + + get type () { + return this._type; + } + + get value () { + return this._value; + } + + static validateType (type) { + if (TYPES.filter((_type) => type === _type).length) { + return true; + } + + throw new Error(`Invalid type ${type} received for Token`); + } +} diff --git a/js/src/abi/token/token.spec.js b/js/src/abi/token/token.spec.js new file mode 100644 index 000000000..2abaad6ac --- /dev/null +++ b/js/src/abi/token/token.spec.js @@ -0,0 +1,75 @@ +// Copyright 2015, 2016 Ethcore (UK) Ltd. +// This file is part of Parity. + +// Parity is free software: you can redistribute it and/or modify +// it under the terms of the GNU General Public License as published by +// the Free Software Foundation, either version 3 of the License, or +// (at your option) any later version. + +// Parity is distributed in the hope that it will be useful, +// but WITHOUT ANY WARRANTY; without even the implied warranty of +// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the +// GNU General Public License for more details. + +// You should have received a copy of the GNU General Public License +// along with Parity. If not, see . + +import Token from './token'; + +describe('abi/token/token', () => { + describe('validateType', () => { + it('validates address', () => { + expect(Token.validateType('address')).to.be.true; + }); + + it('validates fixedArray', () => { + expect(Token.validateType('fixedArray')).to.be.true; + }); + + it('validates array', () => { + expect(Token.validateType('array')).to.be.true; + }); + + it('validates fixedBytes', () => { + expect(Token.validateType('fixedBytes')).to.be.true; + }); + + it('validates bytes', () => { + expect(Token.validateType('bytes')).to.be.true; + }); + + it('validates bool', () => { + expect(Token.validateType('bool')).to.be.true; + }); + + it('validates int', () => { + expect(Token.validateType('int')).to.be.true; + }); + + it('validates uint', () => { + expect(Token.validateType('uint')).to.be.true; + }); + + it('validates string', () => { + expect(Token.validateType('string')).to.be.true; + }); + + it('throws an error on invalid types', () => { + expect(() => Token.validateType('noMatch')).to.throw(/noMatch/); + }); + }); + + describe('constructor', () => { + it('throws an error on invalid types', () => { + expect(() => new Token('noMatch', '1')).to.throw(/noMatch/); + }); + + it('sets the type of the object', () => { + expect((new Token('bool', '1')).type).to.equal('bool'); + }); + + it('sets the value of the object', () => { + expect((new Token('bool', '1')).value).to.equal('1'); + }); + }); +}); diff --git a/js/src/abi/util/address.js b/js/src/abi/util/address.js new file mode 100644 index 000000000..f0e188f2c --- /dev/null +++ b/js/src/abi/util/address.js @@ -0,0 +1,65 @@ +// Copyright 2015, 2016 Ethcore (UK) Ltd. +// This file is part of Parity. + +// Parity is free software: you can redistribute it and/or modify +// it under the terms of the GNU General Public License as published by +// the Free Software Foundation, either version 3 of the License, or +// (at your option) any later version. + +// Parity is distributed in the hope that it will be useful, +// but WITHOUT ANY WARRANTY; without even the implied warranty of +// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the +// GNU General Public License for more details. + +// You should have received a copy of the GNU General Public License +// along with Parity. If not, see . + +import { keccak_256 } from 'js-sha3'; // eslint-disable-line camelcase + +export function isChecksumValid (_address) { + const address = _address.replace('0x', ''); + const hash = keccak_256(address.toLowerCase(address)); + + for (let n = 0; n < 40; n++) { + const hashval = parseInt(hash[n], 16); + const isLower = address[n].toUpperCase() !== address[n]; + const isUpper = address[n].toLowerCase() !== address[n]; + + if ((hashval > 7 && isLower) || (hashval <= 7 && isUpper)) { + return false; + } + } + + return true; +} + +export function isAddress (address) { + if (address && address.length === 42) { + if (!/^(0x)?[0-9a-f]{40}$/i.test(address)) { + return false; + } else if (/^(0x)?[0-9a-f]{40}$/.test(address) || /^(0x)?[0-9A-F]{40}$/.test(address)) { + return true; + } + + return isChecksumValid(address); + } + + return false; +} + +export function toChecksumAddress (_address) { + const address = (_address || '').toLowerCase(); + + if (!isAddress(address)) { + return ''; + } + + const hash = keccak_256(address.slice(-40)); + let result = '0x'; + + for (let n = 0; n < 40; n++) { + result = `${result}${parseInt(hash[n], 16) > 7 ? address[n + 2].toUpperCase() : address[n + 2]}`; + } + + return result; +} diff --git a/js/src/abi/util/address.spec.js b/js/src/abi/util/address.spec.js new file mode 100644 index 000000000..9b0ec38cb --- /dev/null +++ b/js/src/abi/util/address.spec.js @@ -0,0 +1,100 @@ +// Copyright 2015, 2016 Ethcore (UK) Ltd. +// This file is part of Parity. + +// Parity is free software: you can redistribute it and/or modify +// it under the terms of the GNU General Public License as published by +// the Free Software Foundation, either version 3 of the License, or +// (at your option) any later version. + +// Parity is distributed in the hope that it will be useful, +// but WITHOUT ANY WARRANTY; without even the implied warranty of +// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the +// GNU General Public License for more details. + +// You should have received a copy of the GNU General Public License +// along with Parity. If not, see . + +import { isChecksumValid, isAddress, toChecksumAddress } from './address'; + +describe('abi/util/address', () => { + const value = '63Cf90D3f0410092FC0fca41846f596223979195'; + const address = `0x${value}`; + const lowercase = `0x${value.toLowerCase()}`; + const uppercase = `0x${value.toUpperCase()}`; + const invalid = '0x' + value.split('').map((char) => { + if (char >= 'a' && char <= 'f') { + return char.toUpperCase(); + } else if (char >= 'A' && char <= 'F') { + return char.toLowerCase(); + } + + return char; + }).join(''); + const invalidhex = '0x01234567890123456789012345678901234567gh'; + + describe('isChecksumValid', () => { + it('returns false when fully lowercase', () => { + expect(isChecksumValid(lowercase)).to.be.false; + }); + + it('returns false when fully uppercase', () => { + expect(isChecksumValid(uppercase)).to.be.false; + }); + + it('returns false on a mixed-case address', () => { + expect(isChecksumValid(invalid)).to.be.false; + }); + + it('returns true on a checksummed address', () => { + expect(isChecksumValid(address)).to.be.true; + }); + }); + + describe('isAddress', () => { + it('returns true when fully lowercase', () => { + expect(isAddress(lowercase)).to.be.true; + }); + + it('returns true when fully uppercase', () => { + expect(isAddress(uppercase)).to.be.true; + }); + + it('returns true when checksummed', () => { + expect(isAddress(address)).to.be.true; + }); + + it('returns false when invalid checksum', () => { + expect(isAddress(invalid)).to.be.false; + }); + + it('returns false on valid length, non-hex', () => { + expect(isAddress(invalidhex)).to.be.false; + }); + }); + + describe('toChecksumAddress', () => { + it('returns empty when no address specified', () => { + expect(toChecksumAddress()).to.equal(''); + }); + + it('returns empty on invalid address structure', () => { + expect(toChecksumAddress('0xnotaddress')).to.equal(''); + }); + + it('returns formatted address on checksum input', () => { + expect(toChecksumAddress(address)).to.equal(address); + }); + + it('returns formatted address on lowercase input', () => { + expect(toChecksumAddress(lowercase)).to.equal(address); + }); + + it('returns formatted address on uppercase input', () => { + expect(toChecksumAddress(uppercase)).to.equal(address); + }); + + it('returns formatted address on mixed input', () => { + expect(toChecksumAddress(invalid)).to.equal(address); + }); + }); +}); diff --git a/js/src/abi/util/pad.js b/js/src/abi/util/pad.js new file mode 100644 index 000000000..a7d940431 --- /dev/null +++ b/js/src/abi/util/pad.js @@ -0,0 +1,75 @@ +// Copyright 2015, 2016 Ethcore (UK) Ltd. +// This file is part of Parity. + +// Parity is free software: you can redistribute it and/or modify +// it under the terms of the GNU General Public License as published by +// the Free Software Foundation, either version 3 of the License, or +// (at your option) any later version. + +// Parity is distributed in the hope that it will be useful, +// but WITHOUT ANY WARRANTY; without even the implied warranty of +// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the +// GNU General Public License for more details. + +// You should have received a copy of the GNU General Public License +// along with Parity. If not, see . + +import BigNumber from 'bignumber.js'; +import utf8 from 'utf8'; + +import { isArray } from './types'; + +const ZERO_64 = '0000000000000000000000000000000000000000000000000000000000000000'; + +export function padAddress (_input) { + const input = _input.substr(0, 2) === '0x' ? _input.substr(2) : _input; + + return `${ZERO_64}${input}`.slice(-64); +} + +export function padBool (input) { + return `${ZERO_64}${input ? '1' : '0'}`.slice(-64); +} + +export function padU32 (input) { + let bn = new BigNumber(input); + + if (bn.lessThan(0)) { + bn = new BigNumber('ffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffff', 16) + .plus(bn).plus(1); + } + + return `${ZERO_64}${bn.toString(16)}`.slice(-64); +} + +function stringToBytes (input) { + if (isArray(input)) { + return input; + } else if (input.substr(0, 2) === '0x') { + return input.substr(2).toLowerCase().match(/.{1,2}/g).map((value) => parseInt(value, 16)); + } else { + return input.split('').map((char) => char.charCodeAt(0)); + } +} + +export function padBytes (_input) { + const input = stringToBytes(_input); + + return `${padU32(input.length)}${padFixedBytes(input)}`; +} + +export function padFixedBytes (_input) { + const input = stringToBytes(_input); + const sinput = input.map((code) => `0${code.toString(16)}`.slice(-2)).join(''); + const max = Math.floor((sinput.length + 63) / 64) * 64; + + return `${sinput}${ZERO_64}`.substr(0, max); +} + +export function padString (input) { + const array = utf8.encode(input) + .split('') + .map((char) => char.charCodeAt(0)); + + return padBytes(array); +} diff --git a/js/src/abi/util/pad.spec.js b/js/src/abi/util/pad.spec.js new file mode 100644 index 000000000..96c733682 --- /dev/null +++ b/js/src/abi/util/pad.spec.js @@ -0,0 +1,124 @@ +// Copyright 2015, 2016 Ethcore (UK) Ltd. +// This file is part of Parity. + +// Parity is free software: you can redistribute it and/or modify +// it under the terms of the GNU General Public License as published by +// the Free Software Foundation, either version 3 of the License, or +// (at your option) any later version. + +// Parity is distributed in the hope that it will be useful, +// but WITHOUT ANY WARRANTY; without even the implied warranty of +// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the +// GNU General Public License for more details. + +// You should have received a copy of the GNU General Public License +// along with Parity. If not, see . + +import BigNumber from 'bignumber.js'; +import { padAddress, padBool, padBytes, padFixedBytes, padString, padU32 } from './pad'; + +describe('abi/util/pad', () => { + const SHORT15 = '1234567890abcdef'; + const BYTES15 = [0x12, 0x34, 0x56, 0x78, 0x90, 0xab, 0xcd, 0xef]; + const LONG15 = `${SHORT15}000000000000000000000000000000000000000000000000`; + const PAD123 = '0000000000000000000000000000000000000000000000000000000000000123'; + + describe('padAddress', () => { + it('pads to 64 characters', () => { + expect(padAddress('123')).to.equal(PAD123); + }); + + it('strips leading 0x when passed in', () => { + expect(padFixedBytes(`0x${PAD123}`)).to.equal(PAD123); + }); + }); + + describe('padBool', () => { + const TRUE = '0000000000000000000000000000000000000000000000000000000000000001'; + const FALSE = '0000000000000000000000000000000000000000000000000000000000000000'; + + it('pads true to 64 characters', () => { + expect(padBool(true)).to.equal(TRUE); + }); + + it('pads false to 64 characters', () => { + expect(padBool(false)).to.equal(FALSE); + }); + }); + + describe('padU32', () => { + it('left pads length < 64 bytes to 64 bytes', () => { + expect(padU32(1)).to.equal('0000000000000000000000000000000000000000000000000000000000000001'); + }); + + it('pads hex representation', () => { + expect(padU32(0x123)).to.equal(PAD123); + }); + + it('pads decimal representation', () => { + expect(padU32(291)).to.equal(PAD123); + }); + + it('pads string representation', () => { + expect(padU32('0x123')).to.equal(PAD123); + }); + + it('pads BigNumber representation', () => { + expect(padU32(new BigNumber(0x123))).to.equal(PAD123); + }); + + it('converts negative numbers to 2s complement', () => { + expect(padU32(-123)).to.equal('ffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffff85'); + }); + }); + + describe('padFixedBytes', () => { + it('right pads length < 64 bytes to 64 bytes (string)', () => { + expect(padFixedBytes(`0x${SHORT15}`)).to.equal(LONG15); + }); + + it('right pads length < 64 bytes to 64 bytes (array)', () => { + expect(padFixedBytes(BYTES15)).to.equal(LONG15); + }); + + it('right pads length > 64 bytes (64 byte multiples)', () => { + expect(padFixedBytes(`0x${LONG15}${SHORT15}`)).to.equal(`${LONG15}${LONG15}`); + }); + + it('strips leading 0x when passed in', () => { + expect(padFixedBytes(`0x${SHORT15}`)).to.equal(LONG15); + }); + }); + + describe('padBytes', () => { + it('right pads length < 64, adding the length (string)', () => { + const result = padBytes(`0x${SHORT15}`); + + expect(result.length).to.equal(128); + expect(result).to.equal(`${padU32(8)}${LONG15}`); + }); + + it('right pads length < 64, adding the length (array)', () => { + const result = padBytes(BYTES15); + + expect(result.length).to.equal(128); + expect(result).to.equal(`${padU32(8)}${LONG15}`); + }); + + it('right pads length > 64, adding the length', () => { + const result = padBytes(`0x${LONG15}${SHORT15}`); + + expect(result.length).to.equal(192); + expect(result).to.equal(`${padU32(0x28)}${LONG15}${LONG15}`); + }); + }); + + describe('padString', () => { + it('correctly converts & pads strings', () => { + const result = padString('gavofyork'); + + expect(result.length).to.equal(128); + expect(result).to.equal(padBytes('0x6761766f66796f726b')); + }); + }); +}); diff --git a/js/src/abi/util/signature.js b/js/src/abi/util/signature.js new file mode 100644 index 000000000..10aedf13f --- /dev/null +++ b/js/src/abi/util/signature.js @@ -0,0 +1,31 @@ +// Copyright 2015, 2016 Ethcore (UK) Ltd. +// This file is part of Parity. + +// Parity is free software: you can redistribute it and/or modify +// it under the terms of the GNU General Public License as published by +// the Free Software Foundation, either version 3 of the License, or +// (at your option) any later version. + +// Parity is distributed in the hope that it will be useful, +// but WITHOUT ANY WARRANTY; without even the implied warranty of +// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the +// GNU General Public License for more details. + +// You should have received a copy of the GNU General Public License +// along with Parity. If not, see . + +import { keccak_256 } from 'js-sha3'; // eslint-disable-line camelcase +import { fromParamType } from '../spec/paramType/format'; + +export function eventSignature (name, params) { + const types = (params || []).map(fromParamType).join(','); + const id = `${name || ''}(${types})`; + + return { id, signature: keccak_256(id) }; +} + +export function methodSignature (name, params) { + const { id, signature } = eventSignature(name, params); + + return { id, signature: signature.substr(0, 8) }; +} diff --git a/js/src/abi/util/signature.spec.js b/js/src/abi/util/signature.spec.js new file mode 100644 index 000000000..61664b8fc --- /dev/null +++ b/js/src/abi/util/signature.spec.js @@ -0,0 +1,68 @@ +// Copyright 2015, 2016 Ethcore (UK) Ltd. +// This file is part of Parity. + +// Parity is free software: you can redistribute it and/or modify +// it under the terms of the GNU General Public License as published by +// the Free Software Foundation, either version 3 of the License, or +// (at your option) any later version. + +// Parity is distributed in the hope that it will be useful, +// but WITHOUT ANY WARRANTY; without even the implied warranty of +// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the +// GNU General Public License for more details. + +// You should have received a copy of the GNU General Public License +// along with Parity. If not, see . + +import { eventSignature, methodSignature } from './signature'; + +describe('abi/util/signature', () => { + describe('eventSignature', () => { + it('encodes signature baz() correctly', () => { + expect(eventSignature('baz', [])) + .to.deep.equal({ id: 'baz()', signature: 'a7916fac4f538170f7cd12c148552e2cba9fcd72329a2dd5b07a6fa906488ddf' }); + }); + + it('encodes signature baz(uint32) correctly', () => { + expect(eventSignature('baz', [{ type: 'uint', length: 32 }])) + .to.deep.equal({ id: 'baz(uint32)', signature: '7d68785e8fc871be024b75964bd86d093511d4bc2dc7cf7bea32c48a0efaecb1' }); + }); + + it('encodes signature baz(uint32, bool) correctly', () => { + expect(eventSignature('baz', [{ type: 'uint', length: 32 }, { type: 'bool' }])) + .to.deep.equal({ id: 'baz(uint32,bool)', signature: 'cdcd77c0992ec5bbfc459984220f8c45084cc24d9b6efed1fae540db8de801d2' }); + }); + + it('encodes no-name signature correctly as ()', () => { + expect(eventSignature(undefined, [])) + .to.deep.equal({ id: '()', signature: '861731d50c3880a2ca1994d5ec287b94b2f4bd832a67d3e41c08177bdd5674fe' }); + }); + + it('encodes no-params signature correctly as ()', () => { + expect(eventSignature(undefined, undefined)) + .to.deep.equal({ id: '()', signature: '861731d50c3880a2ca1994d5ec287b94b2f4bd832a67d3e41c08177bdd5674fe' }); + }); + }); + + describe('methodSignature', () => { + it('encodes signature baz() correctly', () => { + expect(methodSignature('baz', [])).to.deep.equal({ id: 'baz()', signature: 'a7916fac' }); + }); + + it('encodes signature baz(uint32) correctly', () => { + expect(methodSignature('baz', [{ type: 'uint', length: 32 }])).to.deep.equal({ id: 'baz(uint32)', signature: '7d68785e' }); + }); + + it('encodes signature baz(uint32, bool) correctly', () => { + expect(methodSignature('baz', [{ type: 'uint', length: 32 }, { type: 'bool' }])).to.deep.equal({ id: 'baz(uint32,bool)', signature: 'cdcd77c0' }); + }); + + it('encodes no-name signature correctly as ()', () => { + expect(methodSignature(undefined, [])).to.deep.equal({ id: '()', signature: '861731d5' }); + }); + + it('encodes no-params signature correctly as ()', () => { + expect(methodSignature(undefined, undefined)).to.deep.equal({ id: '()', signature: '861731d5' }); + }); + }); +}); diff --git a/js/src/abi/util/slice.js b/js/src/abi/util/slice.js new file mode 100644 index 000000000..417efea54 --- /dev/null +++ b/js/src/abi/util/slice.js @@ -0,0 +1,35 @@ +// Copyright 2015, 2016 Ethcore (UK) Ltd. +// This file is part of Parity. + +// Parity is free software: you can redistribute it and/or modify +// it under the terms of the GNU General Public License as published by +// the Free Software Foundation, either version 3 of the License, or +// (at your option) any later version. + +// Parity is distributed in the hope that it will be useful, +// but WITHOUT ANY WARRANTY; without even the implied warranty of +// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the +// GNU General Public License for more details. + +// You should have received a copy of the GNU General Public License +// along with Parity. If not, see . + +import { padAddress } from './pad'; + +export function sliceData (_data) { + if (!_data || !_data.length) { + return []; + } + + let data = (_data.substr(0, 2) === '0x') ? _data.substr(2) : _data; + + if (!data.length) { + data = padAddress(''); + } + + if (data.length % 64) { + throw new Error(`Invalid data length (not mod 64) passed to sliceData, ${data}, % 64 == ${data.length % 64}`); + } + + return data.match(/.{1,64}/g); +} diff --git a/js/src/abi/util/slice.spec.js b/js/src/abi/util/slice.spec.js new file mode 100644 index 000000000..92608c590 --- /dev/null +++ b/js/src/abi/util/slice.spec.js @@ -0,0 +1,48 @@ +// Copyright 2015, 2016 Ethcore (UK) Ltd. +// This file is part of Parity. + +// Parity is free software: you can redistribute it and/or modify +// it under the terms of the GNU General Public License as published by +// the Free Software Foundation, either version 3 of the License, or +// (at your option) any later version. + +// Parity is distributed in the hope that it will be useful, +// but WITHOUT ANY WARRANTY; without even the implied warranty of +// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the +// GNU General Public License for more details. + +// You should have received a copy of the GNU General Public License +// along with Parity. If not, see . + +import { sliceData } from './slice'; + +describe('abi/util/slice', () => { + describe('sliceData', () => { + const slice1 = '131a3afc00d1b1e3461b955e53fc866dcf303b3eb9f4c16f89e388930f48134b'; + const slice2 = '2124768576358735263578356373526387638357635873563586353756358763'; + + it('throws an error on mod 64 != 0', () => { + expect(() => sliceData('123')).to.throw(/sliceData/); + }); + + it('returns an empty array when length === 0', () => { + expect(sliceData('')).to.deep.equal([]); + }); + + it('returns an array with the slices otherwise', () => { + const sliced = sliceData(`${slice1}${slice2}`); + + expect(sliced.length).to.equal(2); + expect(sliced[0]).to.equal(slice1); + expect(sliced[1]).to.equal(slice2); + }); + + it('removes leading 0x when passed in', () => { + const sliced = sliceData(`0x${slice1}${slice2}`); + + expect(sliced.length).to.equal(2); + expect(sliced[0]).to.equal(slice1); + expect(sliced[1]).to.equal(slice2); + }); + }); +}); diff --git a/js/src/abi/util/sliceAs.js b/js/src/abi/util/sliceAs.js new file mode 100644 index 000000000..47c3e9758 --- /dev/null +++ b/js/src/abi/util/sliceAs.js @@ -0,0 +1,47 @@ +// Copyright 2015, 2016 Ethcore (UK) Ltd. +// This file is part of Parity. + +// Parity is free software: you can redistribute it and/or modify +// it under the terms of the GNU General Public License as published by +// the Free Software Foundation, either version 3 of the License, or +// (at your option) any later version. + +// Parity is distributed in the hope that it will be useful, +// but WITHOUT ANY WARRANTY; without even the implied warranty of +// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the +// GNU General Public License for more details. + +// You should have received a copy of the GNU General Public License +// along with Parity. If not, see . + +import BigNumber from 'bignumber.js'; + +import { toChecksumAddress } from './address'; + +export function asU32 (slice) { + // TODO: validation + + return new BigNumber(slice, 16); +} + +export function asI32 (slice) { + if (new BigNumber(slice.substr(0, 1), 16).toString(2)[0] === '1') { + return new BigNumber(slice, 16) + .minus(new BigNumber('ffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffff', 16)) + .minus(1); + } + + return new BigNumber(slice, 16); +} + +export function asAddress (slice) { + // TODO: address validation? + + return toChecksumAddress(`0x${slice.slice(-40)}`); +} + +export function asBool (slice) { + // TODO: everything else should be 0 + + return new BigNumber(slice[63]).eq(1); +} diff --git a/js/src/abi/util/sliceAs.spec.js b/js/src/abi/util/sliceAs.spec.js new file mode 100644 index 000000000..af6886008 --- /dev/null +++ b/js/src/abi/util/sliceAs.spec.js @@ -0,0 +1,54 @@ +// Copyright 2015, 2016 Ethcore (UK) Ltd. +// This file is part of Parity. + +// Parity is free software: you can redistribute it and/or modify +// it under the terms of the GNU General Public License as published by +// the Free Software Foundation, either version 3 of the License, or +// (at your option) any later version. + +// Parity is distributed in the hope that it will be useful, +// but WITHOUT ANY WARRANTY; without even the implied warranty of +// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the +// GNU General Public License for more details. + +// You should have received a copy of the GNU General Public License +// along with Parity. If not, see . + +import { asAddress, asBool, asI32, asU32 } from './sliceAs'; + +describe('abi/util/sliceAs', () => { + const MAX_INT = 'ffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffff'; + + describe('asAddress', () => { + it('correctly returns the last 0x40 characters', () => { + const address = '1111111111222222222233333333334444444444'; + expect(asAddress(`000000000000000000000000${address}`)).to.equal(`0x${address}`); + }); + }); + + describe('asBool', () => { + it('correctly returns true', () => { + expect(asBool('0000000000000000000000000000000000000000000000000000000000000001')).to.be.true; + }); + + it('correctly returns false', () => { + expect(asBool('0000000000000000000000000000000000000000000000000000000000000000')).to.be.false; + }); + }); + + describe('asI32', () => { + it('correctly decodes positive numbers', () => { + expect(asI32('000000000000000000000000000000000000000000000000000000000000007b').toString()).to.equal('123'); + }); + + it('correctly decodes negative numbers', () => { + expect(asI32('ffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffff85').toString()).to.equal('-123'); + }); + }); + + describe('asU32', () => { + it('returns a maxium U32', () => { + expect(asU32(MAX_INT).toString(16)).to.equal(MAX_INT); + }); + }); +}); diff --git a/js/src/abi/util/types.js b/js/src/abi/util/types.js new file mode 100644 index 000000000..649f26db6 --- /dev/null +++ b/js/src/abi/util/types.js @@ -0,0 +1,27 @@ +// Copyright 2015, 2016 Ethcore (UK) Ltd. +// This file is part of Parity. + +// Parity is free software: you can redistribute it and/or modify +// it under the terms of the GNU General Public License as published by +// the Free Software Foundation, either version 3 of the License, or +// (at your option) any later version. + +// Parity is distributed in the hope that it will be useful, +// but WITHOUT ANY WARRANTY; without even the implied warranty of +// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the +// GNU General Public License for more details. + +// You should have received a copy of the GNU General Public License +// along with Parity. If not, see . + +export function isArray (test) { + return Object.prototype.toString.call(test) === '[object Array]'; +} + +export function isString (test) { + return Object.prototype.toString.call(test) === '[object String]'; +} + +export function isInstanceOf (test, clazz) { + return test instanceof clazz; +} diff --git a/js/src/abi/util/types.spec.js b/js/src/abi/util/types.spec.js new file mode 100644 index 000000000..2e1a538a6 --- /dev/null +++ b/js/src/abi/util/types.spec.js @@ -0,0 +1,62 @@ +// Copyright 2015, 2016 Ethcore (UK) Ltd. +// This file is part of Parity. + +// Parity is free software: you can redistribute it and/or modify +// it under the terms of the GNU General Public License as published by +// the Free Software Foundation, either version 3 of the License, or +// (at your option) any later version. + +// Parity is distributed in the hope that it will be useful, +// but WITHOUT ANY WARRANTY; without even the implied warranty of +// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the +// GNU General Public License for more details. + +// You should have received a copy of the GNU General Public License +// along with Parity. If not, see . + +import { isArray, isString, isInstanceOf } from './types'; +import Token from '../token'; + +describe('abi/util/types', () => { + describe('isArray', () => { + it('correctly identifies empty arrays as Array', () => { + expect(isArray([])).to.be.true; + }); + + it('correctly identifies non-empty arrays as Array', () => { + expect(isArray([1, 2, 3])).to.be.true; + }); + + it('correctly identifies strings as non-Array', () => { + expect(isArray('not an array')).to.be.false; + }); + + it('correctly identifies objects as non-Array', () => { + expect(isArray({})).to.be.false; + }); + }); + + describe('isString', () => { + it('correctly identifies empty string as string', () => { + expect(isString('')).to.be.true; + }); + + it('correctly identifies string as string', () => { + expect(isString('123')).to.be.true; + }); + }); + + describe('isInstanceOf', () => { + it('correctly identifies build-in instanceof', () => { + expect(isInstanceOf(new String('123'), String)).to.be.true; // eslint-disable-line no-new-wrappers + }); + + it('correctly identifies own instanceof', () => { + expect(isInstanceOf(new Token('int', 123), Token)).to.be.true; + }); + + it('correctly reports false for own', () => { + expect(isInstanceOf({ type: 'int' }, Token)).to.be.false; + }); + }); +}); diff --git a/js/src/api/README.md b/js/src/api/README.md new file mode 100644 index 000000000..691a24cca --- /dev/null +++ b/js/src/api/README.md @@ -0,0 +1,145 @@ +# ethapi-js + +A thin, fast, low-level Promise-based wrapper around the Ethereum APIs. + +[![Build Status](https://travis-ci.org/jacogr/ethapi-js.svg?branch=master)](https://travis-ci.org/jacogr/ethapi-js) +[![Coverage Status](https://coveralls.io/repos/github/jacogr/ethapi-js/badge.svg?branch=master)](https://coveralls.io/github/jacogr/ethapi-js?branch=master) +[![Dependency Status](https://david-dm.org/jacogr/ethapi-js.svg)](https://david-dm.org/jacogr/ethapi-js) +[![devDependency Status](https://david-dm.org/jacogr/ethapi-js/dev-status.svg)](https://david-dm.org/jacogr/ethapi-js#info=devDependencies) + +## contributing + +Clone the repo and install dependencies via `npm install`. Tests can be executed via + +- `npm run testOnce` (100% covered unit tests) +- `npm run testE2E` (E2E against a running RPC-enabled testnet Parity/Geth instance, `parity --testnet` and for WebScokets, `geth --testnet --ws --wsorigins '*' --rpc`) +- setting the environment `DEBUG=true` will display the RPC POST bodies and responses on E2E tests + +## installation + +Install the package with `npm install --save ethapi-js` from the [npm registry ethapi-js](https://www.npmjs.com/package/ethapi-js) + +## usage + +### initialisation + +```javascript +// import the actual EthApi class +import EthApi from 'ethapi-js'; + +// do the setup +const transport = new EthApi.Transport.Http('http://localhost:8545'); // or .Ws('ws://localhost:8546') +const ethapi = new EthApi(transport); +``` + +You will require native Promises and fetch support (latest browsers only), they can be utilised by + +```javascript +import 'isomorphic-fetch'; + +import es6Promise from 'es6-promise'; +es6Promise.polyfill(); +``` + +### making calls + +perform a call + +```javascript +ethapi.eth + .coinbase() + .then((coinbase) => { + console.log(`The coinbase is ${coinbase}`); + }); +``` + +multiple promises + +```javascript +Promise + .all([ + ethapi.eth.coinbase(), + ethapi.net.listening() + ]) + .then(([coinbase, listening]) => { + // do stuff here + }); +``` + +chaining promises + +```javascript +ethapi.eth + .newFilter({...}) + .then((filterId) => ethapi.eth.getFilterChanges(filterId)) + .then((changes) => { + console.log(changes); + }); +``` + +### contracts + +attach contract + +```javascript +const abi = [{ name: 'callMe', inputs: [{ type: 'bool', ...}, { type: 'string', ...}]}, ...abi...]; +const contract = new ethapi.newContract(abi); +``` + +deploy + +```javascript +contract + .deploy('0xc0de', [params], 'superPassword') + .then((address) => { + console.log(`the contract was deployed at ${address}`); + }); +``` + +attach a contract at address + +```javascript +// via the constructor & .at function +const contract = api.newContract(abi).at('0xa9280...7347b'); +// or on an already initialised contract +contract.at('0xa9280...7347b'); +// perform calls here +``` + +find & call a function + +```javascript +contract.named + .callMe + .call({ gas: 21000 }, [true, 'someString']) // or estimateGas or sendTransaction + .then((result) => { + console.log(`the result was ${result}`); + }); +``` + +parse events from transaction receipt + +```javascript +contract + .parseTransactionEvents(txReceipt) + .then((receipt) => { + receipt.logs.forEach((log) => { + console.log('log parameters', log.params); + }); + }); +``` + +## apis + +APIs implement the calls as exposed in the [Ethcore JSON Ethereum RPC](https://github.com/ethcore/ethereum-rpc-json/) definitions. Mapping follows the naming conventions of the originals, i.e. `eth_call` becomes `eth.call`, `personal_accounts` becomes `personal.accounts`, etc. + +- [ethapi.db](https://github.com/ethcore/ethereum-rpc-json/blob/master/interfaces.md#db) +- [ethapi.eth](https://github.com/ethcore/ethereum-rpc-json/blob/master/interfaces.md#eth) +- [ethapi.ethcore](https://github.com/ethcore/ethereum-rpc-json/blob/master/interfaces.md#ethcore) +- [ethapi.net](https://github.com/ethcore/ethereum-rpc-json/blob/master/interfaces.md#net) +- [ethapi.personal](https://github.com/ethcore/ethereum-rpc-json/blob/master/interfaces.md#personal) +- [ethapi.shh](https://github.com/ethcore/ethereum-rpc-json/blob/master/interfaces.md#shh) +- [ethapi.trace](https://github.com/ethcore/ethereum-rpc-json/blob/master/interfaces.md#trace) +- [ethapi.web3](https://github.com/ethcore/ethereum-rpc-json/blob/master/interfaces.md#web3) + +As a verification step, all exposed interfaces are tested for existing and pointing to the correct endpoints by using the generated interfaces from the above repo. diff --git a/js/src/api/api.js b/js/src/api/api.js new file mode 100644 index 000000000..c82129772 --- /dev/null +++ b/js/src/api/api.js @@ -0,0 +1,125 @@ +// Copyright 2015, 2016 Ethcore (UK) Ltd. +// This file is part of Parity. + +// Parity is free software: you can redistribute it and/or modify +// it under the terms of the GNU General Public License as published by +// the Free Software Foundation, either version 3 of the License, or +// (at your option) any later version. + +// Parity is distributed in the hope that it will be useful, +// but WITHOUT ANY WARRANTY; without even the implied warranty of +// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the +// GNU General Public License for more details. + +// You should have received a copy of the GNU General Public License +// along with Parity. If not, see . + +import { Http, Ws } from './transport'; +import Contract from './contract'; + +import { Db, Eth, Ethcore, Net, Personal, Shh, Trace, Web3 } from './rpc'; +import Subscriptions from './subscriptions'; +import util from './util'; +import { isFunction } from './util/types'; + +export default class Api { + constructor (transport) { + if (!transport || !isFunction(transport.execute)) { + throw new Error('EthApi needs transport with execute() function defined'); + } + + this._transport = transport; + + this._db = new Db(transport); + this._eth = new Eth(transport); + this._ethcore = new Ethcore(transport); + this._net = new Net(transport); + this._personal = new Personal(transport); + this._shh = new Shh(transport); + this._trace = new Trace(transport); + this._web3 = new Web3(transport); + + this._subscriptions = new Subscriptions(this); + } + + get db () { + return this._db; + } + + get eth () { + return this._eth; + } + + get ethcore () { + return this._ethcore; + } + + get net () { + return this._net; + } + + get personal () { + return this._personal; + } + + get shh () { + return this._shh; + } + + get trace () { + return this._trace; + } + + get transport () { + return this._transport; + } + + get web3 () { + return this._web3; + } + + get util () { + return util; + } + + newContract (abi, address) { + return new Contract(this, abi).at(address); + } + + subscribe (subscriptionName, callback) { + return this._subscriptions.subscribe(subscriptionName, callback); + } + + unsubscribe (subscriptionName, subscriptionId) { + return this._subscriptions.unsubscribe(subscriptionName, subscriptionId); + } + + pollMethod (method, input, validate) { + const [_group, endpoint] = method.split('_'); + const group = `_${_group}`; + + return new Promise((resolve, reject) => { + const timeout = () => { + this[group][endpoint](input) + .then((result) => { + if (validate ? validate(result) : result) { + resolve(result); + } else { + setTimeout(timeout, 500); + } + }) + .catch((error) => { + console.error('pollMethod', error); + reject(error); + }); + }; + + timeout(); + }); + } + + static Transport = { + Http: Http, + Ws: Ws + } +} diff --git a/js/src/api/api.spec.js b/js/src/api/api.spec.js new file mode 100644 index 000000000..bbd140d4d --- /dev/null +++ b/js/src/api/api.spec.js @@ -0,0 +1,47 @@ +// Copyright 2015, 2016 Ethcore (UK) Ltd. +// This file is part of Parity. + +// Parity is free software: you can redistribute it and/or modify +// it under the terms of the GNU General Public License as published by +// the Free Software Foundation, either version 3 of the License, or +// (at your option) any later version. + +// Parity is distributed in the hope that it will be useful, +// but WITHOUT ANY WARRANTY; without even the implied warranty of +// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the +// GNU General Public License for more details. + +// You should have received a copy of the GNU General Public License +// along with Parity. If not, see . + +import { TEST_HTTP_URL, endpointTest } from '../../test/mockRpc'; + +import Api from './api'; + +import ethereumRpc from '../jsonrpc/'; + +describe('api/Api', () => { + describe('constructor', () => { + it('requires defined/non-null transport object', () => { + expect(() => new Api()).to.throw(/Api needs transport/); + expect(() => new Api(null)).to.throw(/Api needs transport/); + }); + + it('requires an execute function on the transport object', () => { + expect(() => new Api({})).to.throw(/Api needs transport/); + expect(() => new Api({ execute: true })).to.throw(/Api needs transport/); + }); + }); + + describe('interface', () => { + const api = new Api(new Api.Transport.Http(TEST_HTTP_URL)); + + Object.keys(ethereumRpc).sort().forEach((endpoint) => { + describe(endpoint, () => { + Object.keys(ethereumRpc[endpoint]).sort().forEach((method) => { + endpointTest(api, endpoint, method); + }); + }); + }); + }); +}); diff --git a/js/src/api/contract/contract.js b/js/src/api/contract/contract.js new file mode 100644 index 000000000..40caa7643 --- /dev/null +++ b/js/src/api/contract/contract.js @@ -0,0 +1,319 @@ +// Copyright 2015, 2016 Ethcore (UK) Ltd. +// This file is part of Parity. + +// Parity is free software: you can redistribute it and/or modify +// it under the terms of the GNU General Public License as published by +// the Free Software Foundation, either version 3 of the License, or +// (at your option) any later version. + +// Parity is distributed in the hope that it will be useful, +// but WITHOUT ANY WARRANTY; without even the implied warranty of +// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the +// GNU General Public License for more details. + +// You should have received a copy of the GNU General Public License +// along with Parity. If not, see . + +import Abi from '../../abi'; +import Api from '../api'; +import { isInstanceOf } from '../util/types'; + +let nextSubscriptionId = 0; + +export default class Contract { + constructor (api, abi) { + if (!isInstanceOf(api, Api)) { + throw new Error('API instance needs to be provided to Contract'); + } else if (!abi) { + throw new Error('ABI needs to be provided to Contract instance'); + } + + this._api = api; + this._abi = new Abi(abi); + + this._subscriptions = {}; + this._constructors = this._abi.constructors.map(this._bindFunction); + this._functions = this._abi.functions.map(this._bindFunction); + this._events = this._abi.events.map(this._bindEvent); + + this._instance = {}; + + this._events.forEach((evt) => { + this._instance[evt.name] = evt; + }); + this._functions.forEach((fn) => { + this._instance[fn.name] = fn; + }); + + this._sendSubscriptionChanges(); + } + + get address () { + return this._address; + } + + get constructors () { + return this._constructors; + } + + get events () { + return this._events; + } + + get functions () { + return this._functions; + } + + get instance () { + this._instance.address = this._address; + return this._instance; + } + + get api () { + return this._api; + } + + get abi () { + return this._abi; + } + + at (address) { + this._address = address; + return this; + } + + deploy (options, values, statecb) { + let gas; + + const setState = (state) => { + if (!statecb) { + return; + } + + return statecb(null, state); + }; + + setState({ state: 'estimateGas' }); + + return this._api.eth + .estimateGas(this._encodeOptions(this.constructors[0], options, values)) + .then((_gas) => { + gas = _gas.mul(1.2); + options.gas = gas.toFixed(0); + + setState({ state: 'postTransaction', gas }); + return this._api.eth.postTransaction(this._encodeOptions(this.constructors[0], options, values)); + }) + .then((requestId) => { + setState({ state: 'checkRequest', requestId }); + return this._pollCheckRequest(requestId); + }) + .then((txhash) => { + setState({ state: 'getTransactionReceipt', txhash }); + return this._pollTransactionReceipt(txhash, gas); + }) + .then((receipt) => { + if (receipt.gasUsed.eq(gas)) { + throw new Error(`Contract not deployed, gasUsed == ${gas.toFixed(0)}`); + } + + setState({ state: 'hasReceipt', receipt }); + this._address = receipt.contractAddress; + return this._address; + }) + .then((address) => { + setState({ state: 'getCode' }); + return this._api.eth.getCode(this._address); + }) + .then((code) => { + if (code === '0x') { + throw new Error('Contract not deployed, getCode returned 0x'); + } + + setState({ state: 'completed' }); + return this._address; + }); + } + + parseEventLogs (logs) { + return logs.map((log) => { + const signature = log.topics[0].substr(2); + const event = this.events.find((evt) => evt.signature === signature); + + if (!event) { + throw new Error(`Unable to find event matching signature ${signature}`); + } + + const decoded = event.decodeLog(log.topics, log.data); + + log.params = {}; + log.event = event.name; + + decoded.params.forEach((param) => { + log.params[param.name] = param.token.value; + }); + + return log; + }); + } + + parseTransactionEvents (receipt) { + receipt.logs = this.parseEventLogs(receipt.logs); + + return receipt; + } + + _pollCheckRequest = (requestId) => { + return this._api.pollMethod('eth_checkRequest', requestId); + } + + _pollTransactionReceipt = (txhash, gas) => { + return this.api.pollMethod('eth_getTransactionReceipt', txhash, (receipt) => { + if (!receipt || !receipt.blockNumber || receipt.blockNumber.eq(0)) { + return false; + } + + return true; + }); + } + + _encodeOptions (func, options, values) { + const tokens = func ? this._abi.encodeTokens(func.inputParamTypes(), values) : null; + const call = tokens ? func.encodeCall(tokens) : null; + + if (options.data && options.data.substr(0, 2) === '0x') { + options.data = options.data.substr(2); + } + options.data = `0x${options.data || ''}${call || ''}`; + + return options; + } + + _addOptionsTo (options = {}) { + return Object.assign({ + to: this._address + }, options); + } + + _bindFunction = (func) => { + func.call = (options, values = []) => { + return this._api.eth + .call(this._encodeOptions(func, this._addOptionsTo(options), values)) + .then((encoded) => func.decodeOutput(encoded)) + .then((tokens) => tokens.map((token) => token.value)) + .then((returns) => returns.length === 1 ? returns[0] : returns); + }; + + if (!func.constant) { + func.postTransaction = (options, values = []) => { + return this._api.eth + .postTransaction(this._encodeOptions(func, this._addOptionsTo(options), values)); + }; + + func.estimateGas = (options, values = []) => { + return this._api.eth + .estimateGas(this._encodeOptions(func, this._addOptionsTo(options), values)); + }; + } + + return func; + } + + _bindEvent = (event) => { + event.subscribe = (options = {}, callback) => { + return this._subscribe(event, options, callback); + }; + + event.unsubscribe = (subscriptionId) => { + return this.unsubscribe(subscriptionId); + }; + + return event; + } + + subscribe (eventName = null, options = {}, callback) { + return new Promise((resolve, reject) => { + let event = null; + + if (eventName) { + event = this._events.find((evt) => evt.name === eventName); + + if (!event) { + const events = this._events.map((evt) => evt.name).join(', '); + reject(new Error(`${eventName} is not a valid eventName, subscribe using one of ${events} (or null to include all)`)); + return; + } + } + + return this._subscribe(event, options, callback).then(resolve).catch(reject); + }); + } + + _subscribe (event = null, _options, callback) { + const subscriptionId = nextSubscriptionId++; + const options = Object.assign({}, _options, { + address: this._address, + topics: [event ? event.signature : null] + }); + + return this._api.eth + .newFilter(options) + .then((filterId) => { + return this._api.eth + .getFilterLogs(filterId) + .then((logs) => { + callback(null, this.parseEventLogs(logs)); + this._subscriptions[subscriptionId] = { + options, + callback, + filterId + }; + + return subscriptionId; + }); + }); + } + + unsubscribe (subscriptionId) { + return this._api.eth + .uninstallFilter(this._subscriptions[subscriptionId].filterId) + .then(() => { + delete this._subscriptions[subscriptionId]; + }) + .catch((error) => { + console.error('unsubscribe', error); + }); + } + + _sendSubscriptionChanges = () => { + const subscriptions = Object.values(this._subscriptions); + const timeout = () => setTimeout(this._sendSubscriptionChanges, 1000); + + Promise + .all( + subscriptions.map((subscription) => { + return this._api.eth.getFilterChanges(subscription.filterId); + }) + ) + .then((logsArray) => { + logsArray.forEach((logs, idx) => { + if (!logs || !logs.length) { + return; + } + + try { + subscriptions[idx].callback(null, this.parseEventLogs(logs)); + } catch (error) { + this.unsubscribe(idx); + console.error('_sendSubscriptionChanges', error); + } + }); + + timeout(); + }) + .catch((error) => { + console.error('_sendSubscriptionChanges', error); + timeout(); + }); + } +} diff --git a/js/src/api/contract/contract.spec.js b/js/src/api/contract/contract.spec.js new file mode 100644 index 000000000..7ac3e099c --- /dev/null +++ b/js/src/api/contract/contract.spec.js @@ -0,0 +1,539 @@ +// Copyright 2015, 2016 Ethcore (UK) Ltd. +// This file is part of Parity. + +// Parity is free software: you can redistribute it and/or modify +// it under the terms of the GNU General Public License as published by +// the Free Software Foundation, either version 3 of the License, or +// (at your option) any later version. + +// Parity is distributed in the hope that it will be useful, +// but WITHOUT ANY WARRANTY; without even the implied warranty of +// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the +// GNU General Public License for more details. + +// You should have received a copy of the GNU General Public License +// along with Parity. If not, see . + +import BigNumber from 'bignumber.js'; +import sinon from 'sinon'; + +import { TEST_HTTP_URL, mockHttp } from '../../../test/mockRpc'; + +import Abi from '../../abi'; + +import Api from '../api'; +import Contract from './contract'; +import { isInstanceOf, isFunction } from '../util/types'; + +const transport = new Api.Transport.Http(TEST_HTTP_URL); +const eth = new Api(transport); + +describe('api/contract/Contract', () => { + const ADDR = '0x0123456789'; + const ABI = [ + { + type: 'function', name: 'test', + inputs: [{ name: 'boolin', type: 'bool' }, { name: 'stringin', type: 'string' }], + outputs: [{ type: 'uint' }] + }, + { + type: 'function', name: 'test2', + outputs: [{ type: 'uint' }, { type: 'uint' }] + }, + { type: 'constructor' }, + { type: 'event', name: 'baz' }, + { type: 'event', name: 'foo' } + ]; + const VALUES = [true, 'jacogr']; + const ENCODED = '0x023562050000000000000000000000000000000000000000000000000000000000000001000000000000000000000000000000000000000000000000000000000000004000000000000000000000000000000000000000000000000000000000000000066a61636f67720000000000000000000000000000000000000000000000000000'; + const RETURN1 = '0000000000000000000000000000000000000000000000000000000000123456'; + const RETURN2 = '0000000000000000000000000000000000000000000000000000000000456789'; + let scope; + + describe('constructor', () => { + it('needs an EthAbi instance', () => { + expect(() => new Contract()).to.throw(/API instance needs to be provided to Contract/); + }); + + it('needs an ABI', () => { + expect(() => new Contract(eth)).to.throw(/ABI needs to be provided to Contract instance/); + }); + + describe('internal setup', () => { + const contract = new Contract(eth, ABI); + + it('sets EthApi & parsed interface', () => { + expect(contract.address).to.not.be.ok; + expect(contract.api).to.deep.equal(eth); + expect(isInstanceOf(contract.abi, Abi)).to.be.ok; + }); + + it('attaches functions', () => { + expect(contract.functions.length).to.equal(2); + expect(contract.functions[0].name).to.equal('test'); + }); + + it('attaches constructors', () => { + expect(contract.constructors.length).to.equal(1); + }); + + it('attaches events', () => { + expect(contract.events.length).to.equal(2); + expect(contract.events[0].name).to.equal('baz'); + }); + }); + }); + + describe('at', () => { + it('sets returns the functions, events & sets the address', () => { + const contract = new Contract(eth, [ + { + constant: true, + inputs: [{ + name: '_who', + type: 'address' + }], + name: 'balanceOf', + outputs: [{ + name: '', + type: 'uint256' + }], + type: 'function' + }, + { + anonymous: false, + inputs: [{ + indexed: false, + name: 'amount', + type: 'uint256' + }], + name: 'Drained', + type: 'event' + } + ]); + contract.at('6789'); + + expect(Object.keys(contract.instance)).to.deep.equal(['Drained', 'balanceOf', 'address']); + expect(contract.address).to.equal('6789'); + }); + }); + + describe('parseTransactionEvents', () => { + it('checks for unmatched signatures', () => { + const contract = new Contract(eth, [{ anonymous: false, name: 'Message', type: 'event' }]); + expect(() => contract.parseTransactionEvents({ + logs: [{ + data: '0x000000000000000000000000000000000000000000000000000000000000000000000000000000000000000063cf90d3f0410092fc0fca41846f5962239791950000000000000000000000000000000000000000000000000000000056e6c85f0000000000000000000000000000000000000000000000000001000000004fcd00000000000000000000000000000000000000000000000000000000000000a0000000000000000000000000000000000000000000000000000000000000000d706f7374286d6573736167652900000000000000000000000000000000000000', + topics: [ + '0x954ba6c157daf8a26539574ffa64203c044691aa57251af95f4b48d85ec00dd5', + '0x0000000000000000000000000000000000000000000000000001000000004fe0' + ] + }] + })).to.throw(/event matching signature/); + }); + + it('parses a transaction log into the data', () => { + const contract = new Contract(eth, [ + { + anonymous: false, name: 'Message', type: 'event', + inputs: [ + { indexed: true, name: 'postId', type: 'uint256' }, + { indexed: false, name: 'parentId', type: 'uint256' }, + { indexed: false, name: 'sender', type: 'address' }, + { indexed: false, name: 'at', type: 'uint256' }, + { indexed: false, name: 'messageId', type: 'uint256' }, + { indexed: false, name: 'message', type: 'string' } + ] + } + ]); + const decoded = contract.parseTransactionEvents({ + blockHash: '0xa9280530a3b47bee2fc80f2862fd56502ae075350571d724d6442ea4c597347b', + blockNumber: '0x4fcd', + cumulativeGasUsed: '0xb57f', + gasUsed: '0xb57f', + logs: [{ + address: '0x22bff18ec62281850546a664bb63a5c06ac5f76c', + blockHash: '0xa9280530a3b47bee2fc80f2862fd56502ae075350571d724d6442ea4c597347b', + blockNumber: '0x4fcd', + data: '0x000000000000000000000000000000000000000000000000000000000000000000000000000000000000000063cf90d3f0410092fc0fca41846f5962239791950000000000000000000000000000000000000000000000000000000056e6c85f0000000000000000000000000000000000000000000000000001000000004fcd00000000000000000000000000000000000000000000000000000000000000a0000000000000000000000000000000000000000000000000000000000000000d706f7374286d6573736167652900000000000000000000000000000000000000', + logIndex: '0x0', + topics: [ + '0x954ba6c157daf8a26539574ffa64203c044691aa57251af95f4b48d85ec00dd5', + '0x0000000000000000000000000000000000000000000000000001000000004fe0' + ], + transactionHash: '0xca16f537d761d13e4e80953b754e2b15541f267d6cad9381f750af1bae1e4917', + transactionIndex: '0x0' + }], + to: '0x22bff18ec62281850546a664bb63a5c06ac5f76c', + transactionHash: '0xca16f537d761d13e4e80953b754e2b15541f267d6cad9381f750af1bae1e4917', + transactionIndex: '0x0' + }); + const log = decoded.logs[0]; + + expect(log.event).to.equal('Message'); + expect(log.address).to.equal('0x22bff18ec62281850546a664bb63a5c06ac5f76c'); + expect(log.params).to.deep.equal({ + at: new BigNumber('1457965151'), + message: 'post(message)', + messageId: new BigNumber('281474976731085'), + parentId: new BigNumber(0), + postId: new BigNumber('281474976731104'), + sender: '0x63Cf90D3f0410092FC0fca41846f596223979195' + }); + }); + }); + + describe('_pollTransactionReceipt', () => { + const contract = new Contract(eth, ABI); + const ADDRESS = '0xD337e80eEdBdf86eDBba021797d7e4e00Bb78351'; + const BLOCKNUMBER = '555000'; + const RECEIPT = { contractAddress: ADDRESS.toLowerCase(), blockNumber: BLOCKNUMBER }; + const EXPECT = { contractAddress: ADDRESS, blockNumber: new BigNumber(BLOCKNUMBER) }; + + let scope; + let receipt; + + describe('success', () => { + before(() => { + scope = mockHttp([ + { method: 'eth_getTransactionReceipt', reply: { result: null } }, + { method: 'eth_getTransactionReceipt', reply: { result: null } }, + { method: 'eth_getTransactionReceipt', reply: { result: RECEIPT } } + ]); + + return contract + ._pollTransactionReceipt('0x123') + .then((_receipt) => { + receipt = _receipt; + }); + }); + + it('sends multiple getTransactionReceipt calls', () => { + expect(scope.isDone()).to.be.true; + }); + + it('passes the txhash through', () => { + expect(scope.body.eth_getTransactionReceipt.params[0]).to.equal('0x123'); + }); + + it('receives the final receipt', () => { + expect(receipt).to.deep.equal(EXPECT); + }); + }); + + describe('error', () => { + before(() => { + scope = mockHttp([{ method: 'eth_getTransactionReceipt', reply: { error: { code: -1, message: 'failure' } } }]); + }); + + it('returns the errors', () => { + return contract + ._pollTransactionReceipt('0x123') + .catch((error) => { + expect(error.message).to.match(/failure/); + }); + }); + }); + }); + + describe('deploy', () => { + const contract = new Contract(eth, ABI); + const ADDRESS = '0xD337e80eEdBdf86eDBba021797d7e4e00Bb78351'; + const RECEIPT_PEND = { contractAddress: ADDRESS.toLowerCase(), gasUsed: 50, blockNumber: 0 }; + const RECEIPT_DONE = { contractAddress: ADDRESS.toLowerCase(), gasUsed: 50, blockNumber: 2500 }; + const RECEIPT_EXCP = { contractAddress: ADDRESS.toLowerCase(), gasUsed: 1200, blockNumber: 2500 }; + + let scope; + + describe('success', () => { + before(() => { + scope = mockHttp([ + { method: 'eth_estimateGas', reply: { result: 1000 } }, + { method: 'eth_postTransaction', reply: { result: '0x678' } }, + { method: 'eth_checkRequest', reply: { result: null } }, + { method: 'eth_checkRequest', reply: { result: '0x890' } }, + { method: 'eth_getTransactionReceipt', reply: { result: null } }, + { method: 'eth_getTransactionReceipt', reply: { result: RECEIPT_PEND } }, + { method: 'eth_getTransactionReceipt', reply: { result: RECEIPT_DONE } }, + { method: 'eth_getCode', reply: { result: '0x456' } } + ]); + + return contract.deploy({ data: '0x123' }, []); + }); + + it('calls estimateGas, postTransaction, checkRequest, getTransactionReceipt & getCode in order', () => { + expect(scope.isDone()).to.be.true; + }); + + it('passes the options through to postTransaction (incl. gas calculation)', () => { + expect(scope.body.eth_postTransaction.params).to.deep.equal([ + { data: '0x123', gas: '0x4b0' } + ]); + }); + + it('sets the address of the contract', () => { + expect(contract.address).to.equal(ADDRESS); + }); + }); + + describe('error', () => { + it('fails when gasUsed == gas', () => { + mockHttp([ + { method: 'eth_estimateGas', reply: { result: 1000 } }, + { method: 'eth_postTransaction', reply: { result: '0x678' } }, + { method: 'eth_checkRequest', reply: { result: '0x789' } }, + { method: 'eth_getTransactionReceipt', reply: { result: RECEIPT_EXCP } } + ]); + + return contract + .deploy({ data: '0x123' }, []) + .catch((error) => { + expect(error.message).to.match(/not deployed, gasUsed/); + }); + }); + + it('fails when no code was deployed', () => { + mockHttp([ + { method: 'eth_estimateGas', reply: { result: 1000 } }, + { method: 'eth_postTransaction', reply: { result: '0x678' } }, + { method: 'eth_checkRequest', reply: { result: '0x789' } }, + { method: 'eth_getTransactionReceipt', reply: { result: RECEIPT_DONE } }, + { method: 'eth_getCode', reply: { result: '0x' } } + ]); + + return contract + .deploy({ data: '0x123' }, []) + .catch((error) => { + expect(error.message).to.match(/not deployed, getCode/); + }); + }); + }); + }); + + describe('bindings', () => { + let contract; + let cons; + let func; + + beforeEach(() => { + contract = new Contract(eth, ABI); + contract.at(ADDR); + cons = contract.constructors[0]; + func = contract.functions.find((fn) => fn.name === 'test'); + }); + + describe('_addOptionsTo', () => { + it('works on no object specified', () => { + expect(contract._addOptionsTo()).to.deep.equal({ to: ADDR }); + }); + + it('uses the contract address when none specified', () => { + expect(contract._addOptionsTo({ from: 'me' })).to.deep.equal({ to: ADDR, from: 'me' }); + }); + + it('overrides the contract address when specified', () => { + expect(contract._addOptionsTo({ to: 'you', from: 'me' })).to.deep.equal({ to: 'you', from: 'me' }); + }); + }); + + describe('attachments', () => { + it('attaches .call, .postTransaction & .estimateGas to constructors', () => { + expect(isFunction(cons.call)).to.be.true; + expect(isFunction(cons.postTransaction)).to.be.true; + expect(isFunction(cons.estimateGas)).to.be.true; + }); + + it('attaches .call, .postTransaction & .estimateGas to functions', () => { + expect(isFunction(func.call)).to.be.true; + expect(isFunction(func.postTransaction)).to.be.true; + expect(isFunction(func.estimateGas)).to.be.true; + }); + + it('attaches .call only to constant functions', () => { + func = (new Contract(eth, [{ type: 'function', name: 'test', constant: true }])).functions[0]; + + expect(isFunction(func.call)).to.be.true; + expect(isFunction(func.postTransaction)).to.be.false; + expect(isFunction(func.estimateGas)).to.be.false; + }); + }); + + describe('postTransaction', () => { + beforeEach(() => { + scope = mockHttp([{ method: 'eth_postTransaction', reply: { result: ['hashId'] } }]); + }); + + it('encodes options and mades an eth_postTransaction call', () => { + return func + .postTransaction({ someExtras: 'foo' }, VALUES) + .then(() => { + expect(scope.isDone()).to.be.true; + expect(scope.body.eth_postTransaction.params[0]).to.deep.equal({ + someExtras: 'foo', + to: ADDR, + data: ENCODED + }); + }); + }); + }); + + describe('estimateGas', () => { + beforeEach(() => { + scope = mockHttp([{ method: 'eth_estimateGas', reply: { result: ['0x123'] } }]); + }); + + it('encodes options and mades an eth_estimateGas call', () => { + return func + .estimateGas({ someExtras: 'foo' }, VALUES) + .then((amount) => { + expect(scope.isDone()).to.be.true; + expect(amount.toString(16)).to.equal('123'); + expect(scope.body.eth_estimateGas.params).to.deep.equal([{ + someExtras: 'foo', + to: ADDR, + data: ENCODED + }]); + }); + }); + }); + + describe('call', () => { + it('encodes options and mades an eth_call call', () => { + scope = mockHttp([{ method: 'eth_call', reply: { result: RETURN1 } }]); + + return func + .call({ someExtras: 'foo' }, VALUES) + .then((result) => { + expect(scope.isDone()).to.be.true; + expect(scope.body.eth_call.params).to.deep.equal([{ + someExtras: 'foo', + to: ADDR, + data: ENCODED + }, 'latest']); + expect(result.toString(16)).to.equal('123456'); + }); + }); + + it('encodes options and mades an eth_call call (multiple returns)', () => { + scope = mockHttp([{ method: 'eth_call', reply: { result: `${RETURN1}${RETURN2}` } }]); + + return contract.functions[1] + .call({}, []) + .then((result) => { + expect(scope.isDone()).to.be.true; + expect(result.length).to.equal(2); + expect(result[0].toString(16)).to.equal('123456'); + expect(result[1].toString(16)).to.equal('456789'); + }); + }); + }); + }); + + describe('subscribe', () => { + const abi = [ + { + anonymous: false, name: 'Message', type: 'event', + inputs: [ + { indexed: true, name: 'postId', type: 'uint256' }, + { indexed: false, name: 'parentId', type: 'uint256' }, + { indexed: false, name: 'sender', type: 'address' }, + { indexed: false, name: 'at', type: 'uint256' }, + { indexed: false, name: 'messageId', type: 'uint256' }, + { indexed: false, name: 'message', type: 'string' } + ] + } + ]; + const logs = [{ + address: '0x22bff18ec62281850546a664bb63a5c06ac5f76c', + blockHash: '0xa9280530a3b47bee2fc80f2862fd56502ae075350571d724d6442ea4c597347b', + blockNumber: '0x4fcd', + data: '0x000000000000000000000000000000000000000000000000000000000000000000000000000000000000000063cf90d3f0410092fc0fca41846f5962239791950000000000000000000000000000000000000000000000000000000056e6c85f0000000000000000000000000000000000000000000000000001000000004fcd00000000000000000000000000000000000000000000000000000000000000a0000000000000000000000000000000000000000000000000000000000000000d706f7374286d6573736167652900000000000000000000000000000000000000', + logIndex: '0x0', + topics: [ + '0x954ba6c157daf8a26539574ffa64203c044691aa57251af95f4b48d85ec00dd5', + '0x0000000000000000000000000000000000000000000000000001000000004fe0' + ], + transactionHash: '0xca16f537d761d13e4e80953b754e2b15541f267d6cad9381f750af1bae1e4917', + transactionIndex: '0x0' + }]; + const parsed = [{ + address: '0x22bfF18ec62281850546a664bb63a5C06AC5F76C', + blockHash: '0xa9280530a3b47bee2fc80f2862fd56502ae075350571d724d6442ea4c597347b', + blockNumber: new BigNumber(20429), + data: '0x000000000000000000000000000000000000000000000000000000000000000000000000000000000000000063cf90d3f0410092fc0fca41846f5962239791950000000000000000000000000000000000000000000000000000000056e6c85f0000000000000000000000000000000000000000000000000001000000004fcd00000000000000000000000000000000000000000000000000000000000000a0000000000000000000000000000000000000000000000000000000000000000d706f7374286d6573736167652900000000000000000000000000000000000000', + event: 'Message', + logIndex: new BigNumber(0), + params: { + at: new BigNumber(1457965151), + message: 'post(message)', + messageId: new BigNumber(281474976731085), + parentId: new BigNumber(0), + postId: new BigNumber(281474976731104), + sender: '0x63Cf90D3f0410092FC0fca41846f596223979195' + }, + topics: [ + '0x954ba6c157daf8a26539574ffa64203c044691aa57251af95f4b48d85ec00dd5', '0x0000000000000000000000000000000000000000000000000001000000004fe0' + ], + transactionHash: '0xca16f537d761d13e4e80953b754e2b15541f267d6cad9381f750af1bae1e4917', + transactionIndex: new BigNumber(0) + }]; + let contract; + + beforeEach(() => { + contract = new Contract(eth, abi); + contract.at(ADDR); + }); + + describe('invalid events', () => { + it('fails to subscribe to an invalid names', () => { + return contract + .subscribe('invalid') + .catch((error) => { + expect(error.message).to.match(/invalid is not a valid eventName/); + }); + }); + }); + + describe('valid events', () => { + let cbb; + let cbe; + + beforeEach(() => { + scope = mockHttp([ + { method: 'eth_newFilter', reply: { result: '0x123' } }, + { method: 'eth_getFilterLogs', reply: { result: logs } }, + { method: 'eth_newFilter', reply: { result: '0x123' } }, + { method: 'eth_getFilterLogs', reply: { result: logs } } + ]); + cbb = sinon.stub(); + cbe = sinon.stub(); + + return contract.subscribe('Message', {}, cbb); + }); + + it('sets the subscriptionId returned', () => { + return contract + .subscribe('Message', {}, cbe) + .then((subscriptionId) => { + expect(subscriptionId).to.equal(1); + }); + }); + + it('creates a new filter and retrieves the logs on it', () => { + return contract + .subscribe('Message', {}, cbe) + .then((subscriptionId) => { + expect(scope.isDone()).to.be.true; + }); + }); + + it('returns the logs to the callback', () => { + return contract + .subscribe('Message', {}, cbe) + .then((subscriptionId) => { + expect(cbe).to.have.been.calledWith(null, parsed); + }); + }); + }); + }); +}); diff --git a/js/src/api/contract/index.js b/js/src/api/contract/index.js new file mode 100644 index 000000000..18051a69f --- /dev/null +++ b/js/src/api/contract/index.js @@ -0,0 +1,17 @@ +// Copyright 2015, 2016 Ethcore (UK) Ltd. +// This file is part of Parity. + +// Parity is free software: you can redistribute it and/or modify +// it under the terms of the GNU General Public License as published by +// the Free Software Foundation, either version 3 of the License, or +// (at your option) any later version. + +// Parity is distributed in the hope that it will be useful, +// but WITHOUT ANY WARRANTY; without even the implied warranty of +// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the +// GNU General Public License for more details. + +// You should have received a copy of the GNU General Public License +// along with Parity. If not, see . + +export default from './contract'; diff --git a/js/src/api/format/input.js b/js/src/api/format/input.js new file mode 100644 index 000000000..16ca2b7f2 --- /dev/null +++ b/js/src/api/format/input.js @@ -0,0 +1,139 @@ +// Copyright 2015, 2016 Ethcore (UK) Ltd. +// This file is part of Parity. + +// Parity is free software: you can redistribute it and/or modify +// it under the terms of the GNU General Public License as published by +// the Free Software Foundation, either version 3 of the License, or +// (at your option) any later version. + +// Parity is distributed in the hope that it will be useful, +// but WITHOUT ANY WARRANTY; without even the implied warranty of +// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the +// GNU General Public License for more details. + +// You should have received a copy of the GNU General Public License +// along with Parity. If not, see . + +import BigNumber from 'bignumber.js'; + +import { isArray, isHex, isInstanceOf, isString } from '../util/types'; + +export function inAddress (address) { + // TODO: address validation if we have upper-lower addresses + return inHex(address); +} + +export function inBlockNumber (blockNumber) { + if (isString(blockNumber)) { + switch (blockNumber) { + case 'earliest': + case 'latest': + case 'pending': + return blockNumber; + } + } + + return inNumber16(blockNumber); +} + +export function inData (data) { + if (data && data.length && !isHex(data)) { + data = data.split('').map((chr) => { + return `0${chr.charCodeAt(0).toString(16)}`.slice(-2); + }).join(''); + } + + return inHex(data); +} + +export function inTopics (_topics) { + let topics = (_topics || []) + .filter((topic) => topic) + .map(inHex); + + while (topics.length < 4) { + topics.push(null); + } + + return topics; +} + +export function inFilter (options) { + if (options) { + Object.keys(options).forEach((key) => { + switch (key) { + case 'address': + if (isArray(options[key])) { + options[key] = options[key].map(inAddress); + } else { + options[key] = inAddress(options[key]); + } + break; + + case 'fromBlock': + case 'toBlock': + options[key] = inBlockNumber(options[key]); + break; + + case 'limit': + options[key] = inNumber10(options[key]); + break; + + case 'topics': + options[key] = inTopics(options[key]); + } + }); + } + + return options; +} + +export function inHex (str) { + if (str && str.substr(0, 2) === '0x') { + return str.toLowerCase(); + } + + return `0x${(str || '').toLowerCase()}`; +} + +export function inNumber10 (number) { + if (isInstanceOf(number, BigNumber)) { + return number.toNumber(); + } + + return (new BigNumber(number || 0)).toNumber(); +} + +export function inNumber16 (number) { + if (isInstanceOf(number, BigNumber)) { + return inHex(number.toString(16)); + } + + return inHex((new BigNumber(number || 0)).toString(16)); +} + +export function inOptions (options) { + if (options) { + Object.keys(options).forEach((key) => { + switch (key) { + case 'from': + case 'to': + options[key] = inAddress(options[key]); + break; + + case 'gas': + case 'gasPrice': + case 'value': + case 'nonce': + options[key] = inNumber16(options[key]); + break; + + case 'data': + options[key] = inData(options[key]); + break; + } + }); + } + + return options; +} diff --git a/js/src/api/format/input.spec.js b/js/src/api/format/input.spec.js new file mode 100644 index 000000000..219886d05 --- /dev/null +++ b/js/src/api/format/input.spec.js @@ -0,0 +1,245 @@ +// Copyright 2015, 2016 Ethcore (UK) Ltd. +// This file is part of Parity. + +// Parity is free software: you can redistribute it and/or modify +// it under the terms of the GNU General Public License as published by +// the Free Software Foundation, either version 3 of the License, or +// (at your option) any later version. + +// Parity is distributed in the hope that it will be useful, +// but WITHOUT ANY WARRANTY; without even the implied warranty of +// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the +// GNU General Public License for more details. + +// You should have received a copy of the GNU General Public License +// along with Parity. If not, see . + +import BigNumber from 'bignumber.js'; + +import { inAddress, inBlockNumber, inData, inFilter, inHex, inNumber10, inNumber16, inOptions } from './input'; +import { isAddress } from '../../../test/types'; + +describe('api/format/input', () => { + const address = '0x63cf90d3f0410092fc0fca41846f596223979195'; + + describe('inAddress', () => { + const address = '63cf90d3f0410092fc0fca41846f596223979195'; + + it('adds the leading 0x as required', () => { + expect(inAddress(address)).to.equal(`0x${address}`); + }); + + it('returns verified addresses as-is', () => { + expect(inAddress(`0x${address}`)).to.equal(`0x${address}`); + }); + + it('returns lowercase equivalents', () => { + expect(inAddress(address.toUpperCase())).to.equal(`0x${address}`); + }); + + it('returns 0x on null addresses', () => { + expect(inAddress()).to.equal('0x'); + }); + }); + + describe('inBlockNumber()', () => { + it('returns earliest as-is', () => { + expect(inBlockNumber('earliest')).to.equal('earliest'); + }); + + it('returns latest as-is', () => { + expect(inBlockNumber('latest')).to.equal('latest'); + }); + + it('returns pending as-is', () => { + expect(inBlockNumber('pending')).to.equal('pending'); + }); + + it('formats existing BigNumber into hex', () => { + expect(inBlockNumber(new BigNumber(0x123456))).to.equal('0x123456'); + }); + + it('formats hex strings into hex', () => { + expect(inBlockNumber('0x123456')).to.equal('0x123456'); + }); + + it('formats numbers into hex', () => { + expect(inBlockNumber(0x123456)).to.equal('0x123456'); + }); + }); + + describe('inData', () => { + it('formats to hex', () => { + expect(inData('123456')).to.equal('0x123456'); + }); + + it('converts a string to a hex representation', () => { + expect(inData('jaco')).to.equal('0x6a61636f'); + }); + }); + + describe('inHex', () => { + it('leaves leading 0x as-is', () => { + expect(inHex('0x123456')).to.equal('0x123456'); + }); + + it('adds a leading 0x', () => { + expect(inHex('123456')).to.equal('0x123456'); + }); + + it('returns uppercase as lowercase (leading 0x)', () => { + expect(inHex('0xABCDEF')).to.equal('0xabcdef'); + }); + + it('returns uppercase as lowercase (no leading 0x)', () => { + expect(inHex('ABCDEF')).to.equal('0xabcdef'); + }); + + it('handles empty & null', () => { + expect(inHex()).to.equal('0x'); + expect(inHex('')).to.equal('0x'); + }); + }); + + describe('inFilter', () => { + ['address'].forEach((input) => { + it(`formats ${input} address as address`, () => { + const block = {}; + block[input] = address; + const formatted = inFilter(block)[input]; + + expect(isAddress(formatted)).to.be.true; + expect(formatted).to.equal(address); + }); + }); + + ['fromBlock', 'toBlock'].forEach((input) => { + it(`formats ${input} number as blockNumber`, () => { + const block = {}; + block[input] = 0x123; + const formatted = inFilter(block)[input]; + + expect(formatted).to.equal('0x123'); + }); + }); + + it('ignores and passes through unknown keys', () => { + expect(inFilter({ someRandom: 'someRandom' })).to.deep.equal({ someRandom: 'someRandom' }); + }); + + it('formats an filter options object with relevant entries converted', () => { + expect( + inFilter({ + address: address, + fromBlock: 'latest', + toBlock: 0x101, + extraData: 'someExtraStuffInHere', + limit: 0x32 + }) + ).to.deep.equal({ + address: address, + fromBlock: 'latest', + toBlock: '0x101', + extraData: 'someExtraStuffInHere', + limit: 50 + }); + }); + }); + + describe('inNumber10()', () => { + it('formats existing BigNumber into number', () => { + expect(inNumber10(new BigNumber(123))).to.equal(123); + }); + + it('formats hex strings into decimal', () => { + expect(inNumber10('0x0a')).to.equal(10); + }); + + it('formats numbers into number', () => { + expect(inNumber10(123)).to.equal(123); + }); + + it('formats undefined into 0', () => { + expect(inNumber10()).to.equal(0); + }); + }); + + describe('inNumber16()', () => { + it('formats existing BigNumber into hex', () => { + expect(inNumber16(new BigNumber(0x123456))).to.equal('0x123456'); + }); + + it('formats hex strings into hex', () => { + expect(inNumber16('0x123456')).to.equal('0x123456'); + }); + + it('formats numbers into hex', () => { + expect(inNumber16(0x123456)).to.equal('0x123456'); + }); + + it('formats undefined into 0', () => { + expect(inNumber16()).to.equal('0x0'); + }); + }); + + describe('inOptions', () => { + ['data'].forEach((input) => { + it(`converts ${input} to hex data`, () => { + const block = {}; + block[input] = '1234'; + const formatted = inData(block[input]); + + expect(formatted).to.equal('0x1234'); + }); + }); + + ['from', 'to'].forEach((input) => { + it(`formats ${input} address as address`, () => { + const block = {}; + block[input] = address; + const formatted = inOptions(block)[input]; + + expect(isAddress(formatted)).to.be.true; + expect(formatted).to.equal(address); + }); + }); + + ['gas', 'gasPrice', 'value', 'nonce'].forEach((input) => { + it(`formats ${input} number as hexnumber`, () => { + const block = {}; + block[input] = 0x123; + const formatted = inOptions(block)[input]; + + expect(formatted).to.equal('0x123'); + }); + }); + + it('ignores and passes through unknown keys', () => { + expect(inOptions({ someRandom: 'someRandom' })).to.deep.equal({ someRandom: 'someRandom' }); + }); + + it('formats an options object with relevant entries converted', () => { + expect( + inOptions({ + from: address, + to: address, + gas: new BigNumber('0x100'), + gasPrice: 0x101, + value: 258, + nonce: '0x104', + data: '0123456789', + extraData: 'someExtraStuffInHere' + }) + ).to.deep.equal({ + from: address, + to: address, + gas: '0x100', + gasPrice: '0x101', + value: '0x102', + nonce: '0x104', + data: '0x0123456789', + extraData: 'someExtraStuffInHere' + }); + }); + }); +}); diff --git a/js/src/api/format/output.js b/js/src/api/format/output.js new file mode 100644 index 000000000..611c2e74e --- /dev/null +++ b/js/src/api/format/output.js @@ -0,0 +1,165 @@ +// Copyright 2015, 2016 Ethcore (UK) Ltd. +// This file is part of Parity. + +// Parity is free software: you can redistribute it and/or modify +// it under the terms of the GNU General Public License as published by +// the Free Software Foundation, either version 3 of the License, or +// (at your option) any later version. + +// Parity is distributed in the hope that it will be useful, +// but WITHOUT ANY WARRANTY; without even the implied warranty of +// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the +// GNU General Public License for more details. + +// You should have received a copy of the GNU General Public License +// along with Parity. If not, see . + +import BigNumber from 'bignumber.js'; + +import { toChecksumAddress } from '../../abi/util/address'; + +export function outAccountInfo (infos) { + const ret = {}; + + Object.keys(infos).forEach((address) => { + const info = infos[address]; + + ret[outAddress(address)] = { + name: info.name, + uuid: info.uuid, + meta: JSON.parse(info.meta) + }; + }); + + return ret; +} + +export function outAddress (address) { + return toChecksumAddress(address); +} + +export function outBlock (block) { + if (block) { + Object.keys(block).forEach((key) => { + switch (key) { + case 'author': + case 'miner': + block[key] = outAddress(block[key]); + break; + + case 'difficulty': + case 'gasLimit': + case 'gasUsed': + case 'nonce': + case 'number': + case 'totalDifficulty': + block[key] = outNumber(block[key]); + break; + + case 'timestamp': + block[key] = outDate(block[key]); + break; + } + }); + } + + return block; +} + +export function outDate (date) { + return new Date(outNumber(date).toNumber() * 1000); +} + +export function outLog (log) { + Object.keys(log).forEach((key) => { + switch (key) { + case 'blockNumber': + case 'logIndex': + case 'transactionIndex': + log[key] = outNumber(log[key]); + break; + + case 'address': + log[key] = outAddress(log[key]); + break; + } + }); + + return log; +} + +export function outNumber (number) { + return new BigNumber(number || 0); +} + +export function outPeers (peers) { + return { + active: outNumber(peers.active), + connected: outNumber(peers.connected), + max: outNumber(peers.max) + }; +} + +export function outReceipt (receipt) { + if (receipt) { + Object.keys(receipt).forEach((key) => { + switch (key) { + case 'blockNumber': + case 'cumulativeGasUsed': + case 'gasUsed': + case 'transactionIndex': + receipt[key] = outNumber(receipt[key]); + break; + + case 'contractAddress': + receipt[key] = outAddress(receipt[key]); + break; + } + }); + } + + return receipt; +} + +export function outSignerRequest (request) { + if (request) { + Object.keys(request).forEach((key) => { + switch (key) { + case 'id': + request[key] = outNumber(request[key]); + break; + + case 'payload': + request[key].transaction = outTransaction(request[key].transaction); + break; + } + }); + } + + return request; +} + +export function outTransaction (tx) { + if (tx) { + Object.keys(tx).forEach((key) => { + switch (key) { + case 'blockNumber': + case 'gasPrice': + case 'gas': + case 'nonce': + case 'transactionIndex': + case 'value': + tx[key] = outNumber(tx[key]); + break; + + case 'creates': + case 'from': + case 'to': + tx[key] = outAddress(tx[key]); + break; + } + }); + } + + return tx; +} diff --git a/js/src/api/format/output.spec.js b/js/src/api/format/output.spec.js new file mode 100644 index 000000000..af1280f3b --- /dev/null +++ b/js/src/api/format/output.spec.js @@ -0,0 +1,247 @@ +// Copyright 2015, 2016 Ethcore (UK) Ltd. +// This file is part of Parity. + +// Parity is free software: you can redistribute it and/or modify +// it under the terms of the GNU General Public License as published by +// the Free Software Foundation, either version 3 of the License, or +// (at your option) any later version. + +// Parity is distributed in the hope that it will be useful, +// but WITHOUT ANY WARRANTY; without even the implied warranty of +// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the +// GNU General Public License for more details. + +// You should have received a copy of the GNU General Public License +// along with Parity. If not, see . + +import BigNumber from 'bignumber.js'; + +import { outBlock, outAccountInfo, outAddress, outDate, outNumber, outPeers, outReceipt, outTransaction } from './output'; +import { isAddress, isBigNumber, isInstanceOf } from '../../../test/types'; + +describe('api/format/output', () => { + const address = '0x63cf90d3f0410092fc0fca41846f596223979195'; + const checksum = '0x63Cf90D3f0410092FC0fca41846f596223979195'; + + describe('outAccountInfo', () => { + it('returns meta objects parsed', () => { + expect(outAccountInfo( + { '0x63cf90d3f0410092fc0fca41846f596223979195': { + name: 'name', uuid: 'uuid', meta: '{"name":"456"}' } + } + )).to.deep.equal({ + '0x63Cf90D3f0410092FC0fca41846f596223979195': { + name: 'name', uuid: 'uuid', meta: { name: '456' } + } + }); + }); + }); + + describe('outAddress', () => { + it('retuns the address as checksummed', () => { + expect(outAddress(address)).to.equal(checksum); + }); + + it('retuns the checksum as checksummed', () => { + expect(outAddress(checksum)).to.equal(checksum); + }); + }); + + describe('outBlock', () => { + ['author', 'miner'].forEach((input) => { + it(`formats ${input} address as address`, () => { + const block = {}; + block[input] = address; + const formatted = outBlock(block)[input]; + + expect(isAddress(formatted)).to.be.true; + expect(formatted).to.equal(checksum); + }); + }); + + ['difficulty', 'gasLimit', 'gasUsed', 'number', 'nonce', 'totalDifficulty'].forEach((input) => { + it(`formats ${input} number as hexnumber`, () => { + const block = {}; + block[input] = 0x123; + const formatted = outBlock(block)[input]; + + expect(isInstanceOf(formatted, BigNumber)).to.be.true; + expect(formatted.toString(16)).to.equal('123'); + }); + }); + + ['timestamp'].forEach((input) => { + it(`formats ${input} number as Date`, () => { + const block = {}; + block[input] = 0x57513668; + const formatted = outBlock(block)[input]; + + expect(isInstanceOf(formatted, Date)).to.be.true; + expect(formatted.getTime()).to.equal(1464940136000); + }); + }); + + it('ignores and passes through unknown keys', () => { + expect(outBlock({ someRandom: 'someRandom' })).to.deep.equal({ someRandom: 'someRandom' }); + }); + + it('formats a block with all the info converted', () => { + expect( + outBlock({ + author: address, + miner: address, + difficulty: '0x100', + gasLimit: '0x101', + gasUsed: '0x102', + number: '0x103', + nonce: '0x104', + totalDifficulty: '0x105', + timestamp: '0x57513668', + extraData: 'someExtraStuffInHere' + }) + ).to.deep.equal({ + author: checksum, + miner: checksum, + difficulty: new BigNumber('0x100'), + gasLimit: new BigNumber('0x101'), + gasUsed: new BigNumber('0x102'), + number: new BigNumber('0x103'), + nonce: new BigNumber('0x104'), + totalDifficulty: new BigNumber('0x105'), + timestamp: new Date('2016-06-03T07:48:56.000Z'), + extraData: 'someExtraStuffInHere' + }); + }); + }); + + describe('outDate', () => { + it('converts a second date in unix timestamp', () => { + expect(outDate(0x57513668)).to.deep.equal(new Date('2016-06-03T07:48:56.000Z')); + }); + }); + + describe('outNumber', () => { + it('returns a BigNumber equalling the value', () => { + const bn = outNumber('0x123456'); + + expect(isBigNumber(bn)).to.be.true; + expect(bn.eq(0x123456)).to.be.true; + }); + + it('assumes 0 when ivalid input', () => { + expect(outNumber().eq(0)).to.be.true; + }); + }); + + describe('outPeers', () => { + it('converts all internal numbers to BigNumbers', () => { + expect(outPeers({ active: 789, connected: '456', max: 0x7b })).to.deep.equal({ + active: new BigNumber(789), + connected: new BigNumber(456), + max: new BigNumber(123) + }); + }); + }); + + describe('outReceipt', () => { + ['contractAddress'].forEach((input) => { + it(`formats ${input} address as address`, () => { + const block = {}; + block[input] = address; + const formatted = outReceipt(block)[input]; + + expect(isAddress(formatted)).to.be.true; + expect(formatted).to.equal(checksum); + }); + }); + + ['blockNumber', 'cumulativeGasUsed', 'cumulativeGasUsed', 'gasUsed', 'transactionIndex'].forEach((input) => { + it(`formats ${input} number as hexnumber`, () => { + const block = {}; + block[input] = 0x123; + const formatted = outReceipt(block)[input]; + + expect(isInstanceOf(formatted, BigNumber)).to.be.true; + expect(formatted.toString(16)).to.equal('123'); + }); + }); + + it('ignores and passes through unknown keys', () => { + expect(outReceipt({ someRandom: 'someRandom' })).to.deep.equal({ someRandom: 'someRandom' }); + }); + + it('formats a receipt with all the info converted', () => { + expect( + outReceipt({ + contractAddress: address, + blockNumber: '0x100', + cumulativeGasUsed: '0x101', + gasUsed: '0x102', + transactionIndex: '0x103', + extraData: 'someExtraStuffInHere' + }) + ).to.deep.equal({ + contractAddress: checksum, + blockNumber: new BigNumber('0x100'), + cumulativeGasUsed: new BigNumber('0x101'), + gasUsed: new BigNumber('0x102'), + transactionIndex: new BigNumber('0x103'), + extraData: 'someExtraStuffInHere' + }); + }); + }); + + describe('outTransaction', () => { + ['from', 'to'].forEach((input) => { + it(`formats ${input} address as address`, () => { + const block = {}; + block[input] = address; + const formatted = outTransaction(block)[input]; + + expect(isAddress(formatted)).to.be.true; + expect(formatted).to.equal(checksum); + }); + }); + + ['blockNumber', 'gasPrice', 'gas', 'nonce', 'transactionIndex', 'value'].forEach((input) => { + it(`formats ${input} number as hexnumber`, () => { + const block = {}; + block[input] = 0x123; + const formatted = outTransaction(block)[input]; + + expect(isInstanceOf(formatted, BigNumber)).to.be.true; + expect(formatted.toString(16)).to.equal('123'); + }); + }); + + it('ignores and passes through unknown keys', () => { + expect(outTransaction({ someRandom: 'someRandom' })).to.deep.equal({ someRandom: 'someRandom' }); + }); + + it('formats a transaction with all the info converted', () => { + expect( + outTransaction({ + from: address, + to: address, + blockNumber: '0x100', + gasPrice: '0x101', + gas: '0x102', + nonce: '0x103', + transactionIndex: '0x104', + value: '0x105', + extraData: 'someExtraStuffInHere' + }) + ).to.deep.equal({ + from: checksum, + to: checksum, + blockNumber: new BigNumber('0x100'), + gasPrice: new BigNumber('0x101'), + gas: new BigNumber('0x102'), + nonce: new BigNumber('0x103'), + transactionIndex: new BigNumber('0x104'), + value: new BigNumber('0x105'), + extraData: 'someExtraStuffInHere' + }); + }); + }); +}); diff --git a/js/src/api/index.js b/js/src/api/index.js new file mode 100644 index 000000000..b03419eac --- /dev/null +++ b/js/src/api/index.js @@ -0,0 +1,17 @@ +// Copyright 2015, 2016 Ethcore (UK) Ltd. +// This file is part of Parity. + +// Parity is free software: you can redistribute it and/or modify +// it under the terms of the GNU General Public License as published by +// the Free Software Foundation, either version 3 of the License, or +// (at your option) any later version. + +// Parity is distributed in the hope that it will be useful, +// but WITHOUT ANY WARRANTY; without even the implied warranty of +// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the +// GNU General Public License for more details. + +// You should have received a copy of the GNU General Public License +// along with Parity. If not, see . + +export default from './api'; diff --git a/js/src/api/rpc/db/db.js b/js/src/api/rpc/db/db.js new file mode 100644 index 000000000..9b2031600 --- /dev/null +++ b/js/src/api/rpc/db/db.js @@ -0,0 +1,43 @@ +// Copyright 2015, 2016 Ethcore (UK) Ltd. +// This file is part of Parity. + +// Parity is free software: you can redistribute it and/or modify +// it under the terms of the GNU General Public License as published by +// the Free Software Foundation, either version 3 of the License, or +// (at your option) any later version. + +// Parity is distributed in the hope that it will be useful, +// but WITHOUT ANY WARRANTY; without even the implied warranty of +// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the +// GNU General Public License for more details. + +// You should have received a copy of the GNU General Public License +// along with Parity. If not, see . + +import { inHex } from '../../format/input'; + +export default class Db { + constructor (transport) { + this._transport = transport; + } + + getHex (dbName, keyName) { + return this._transport + .execute('db_getHex', dbName, keyName); + } + + getString (dbName, keyName) { + return this._transport + .execute('db_getString', dbName, keyName); + } + + putHex (dbName, keyName, hexData) { + return this._transport + .execute('db_putHex', dbName, keyName, inHex(hexData)); + } + + putString (dbName, keyName, stringData) { + return this._transport + .execute('db_putString', dbName, keyName, stringData); + } +} diff --git a/js/src/api/rpc/db/db.spec.js b/js/src/api/rpc/db/db.spec.js new file mode 100644 index 000000000..4379b51c4 --- /dev/null +++ b/js/src/api/rpc/db/db.spec.js @@ -0,0 +1,38 @@ +// Copyright 2015, 2016 Ethcore (UK) Ltd. +// This file is part of Parity. + +// Parity is free software: you can redistribute it and/or modify +// it under the terms of the GNU General Public License as published by +// the Free Software Foundation, either version 3 of the License, or +// (at your option) any later version. + +// Parity is distributed in the hope that it will be useful, +// but WITHOUT ANY WARRANTY; without even the implied warranty of +// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the +// GNU General Public License for more details. + +// You should have received a copy of the GNU General Public License +// along with Parity. If not, see . + +import { TEST_HTTP_URL, mockHttp } from '../../../../test/mockRpc'; + +import Http from '../../transport/http'; +import Db from './db'; + +const instance = new Db(new Http(TEST_HTTP_URL)); + +describe('api/rpc/Db', () => { + let scope; + + describe('putHex', () => { + beforeEach(() => { + scope = mockHttp([{ method: 'db_putHex', reply: { result: [] } }]); + }); + + it('formats the inputs correctly', () => { + return instance.putHex('db', 'key', '1234').then(() => { + expect(scope.body.db_putHex.params).to.deep.equal(['db', 'key', '0x1234']); + }); + }); + }); +}); diff --git a/js/src/api/rpc/db/index.js b/js/src/api/rpc/db/index.js new file mode 100644 index 000000000..cd7c2b0b8 --- /dev/null +++ b/js/src/api/rpc/db/index.js @@ -0,0 +1,17 @@ +// Copyright 2015, 2016 Ethcore (UK) Ltd. +// This file is part of Parity. + +// Parity is free software: you can redistribute it and/or modify +// it under the terms of the GNU General Public License as published by +// the Free Software Foundation, either version 3 of the License, or +// (at your option) any later version. + +// Parity is distributed in the hope that it will be useful, +// but WITHOUT ANY WARRANTY; without even the implied warranty of +// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the +// GNU General Public License for more details. + +// You should have received a copy of the GNU General Public License +// along with Parity. If not, see . + +export default from './db'; diff --git a/js/src/api/rpc/eth/eth.e2e.js b/js/src/api/rpc/eth/eth.e2e.js new file mode 100644 index 000000000..973ea1c51 --- /dev/null +++ b/js/src/api/rpc/eth/eth.e2e.js @@ -0,0 +1,170 @@ +// Copyright 2015, 2016 Ethcore (UK) Ltd. +// This file is part of Parity. + +// Parity is free software: you can redistribute it and/or modify +// it under the terms of the GNU General Public License as published by +// the Free Software Foundation, either version 3 of the License, or +// (at your option) any later version. + +// Parity is distributed in the hope that it will be useful, +// but WITHOUT ANY WARRANTY; without even the implied warranty of +// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the +// GNU General Public License for more details. + +// You should have received a copy of the GNU General Public License +// along with Parity. If not, see . + +import { createHttpApi } from '../../../../test/e2e/ethapi'; +import { isAddress } from '../../../../test/types'; + +describe('ethapi.eth', () => { + const ethapi = createHttpApi(); + const address = '0x63cf90d3f0410092fc0fca41846f596223979195'; + + let latestBlockNumber; + let latestBlockHash; + + describe('accounts', () => { + it('returns the available accounts', () => { + return ethapi.eth.accounts().then((accounts) => { + accounts.forEach((account) => { + expect(isAddress(account)).to.be.true; + }); + }); + }); + }); + + describe('blockNumber', () => { + it('returns the current blockNumber', () => { + return ethapi.eth.blockNumber().then((blockNumber) => { + latestBlockNumber = blockNumber; + expect(blockNumber.gt(0xabcde)).to.be.true; + }); + }); + }); + + describe('coinbase', () => { + it('returns the coinbase', () => { + return ethapi.eth.coinbase().then((coinbase) => { + expect(isAddress(coinbase)).to.be.true; + }); + }); + }); + + describe('gasPrice', () => { + it('returns the current gasPrice', () => { + return ethapi.eth.gasPrice().then((gasPrice) => { + expect(gasPrice.gt(0)).to.be.true; + }); + }); + }); + + describe('getBalance', () => { + it('returns the balance for latest block', () => { + return ethapi.eth.getBalance(address).then((balance) => { + expect(balance.gt(0)).to.be.true; + }); + }); + + it('returns the balance for a very early block', () => { + const atBlock = '0x65432'; + const atValue = '18e07120a6e164fee1b'; + + return ethapi.eth + .getBalance(address, atBlock) + .then((balance) => { + expect(balance.toString(16)).to.equal(atValue); + }) + .catch((error) => { + // Parity doesn't support pruned-before-block balance lookups + expect(error.message).to.match(/not supported/); + }); + }); + + it('returns the balance for a recent/out-of-pruning-range block', () => { + return ethapi.eth + .getBalance(address, latestBlockNumber.minus(1000)) + .then((balance) => { + expect(balance.gt(0)).to.be.true; + }); + }); + }); + + describe('getBlockByNumber', () => { + it('returns the latest block', () => { + return ethapi.eth.getBlockByNumber().then((block) => { + expect(block).to.be.ok; + }); + }); + + it('returns a block by blockNumber', () => { + return ethapi.eth.getBlockByNumber(latestBlockNumber).then((block) => { + latestBlockHash = block.hash; + expect(block).to.be.ok; + }); + }); + + it('returns a block by blockNumber (full)', () => { + return ethapi.eth.getBlockByNumber(latestBlockNumber, true).then((block) => { + expect(block).to.be.ok; + }); + }); + }); + + describe('getBlockByHash', () => { + it('returns the specified block', () => { + return ethapi.eth.getBlockByHash(latestBlockHash).then((block) => { + expect(block).to.be.ok; + expect(block.hash).to.equal(latestBlockHash); + }); + }); + + it('returns the specified block (full)', () => { + return ethapi.eth.getBlockByHash(latestBlockHash, true).then((block) => { + expect(block).to.be.ok; + expect(block.hash).to.equal(latestBlockHash); + }); + }); + }); + + describe('getBlockTransactionCountByHash', () => { + it('returns the transactions of the specified hash', () => { + return ethapi.eth.getBlockTransactionCountByHash(latestBlockHash).then((count) => { + expect(count).to.be.ok; + expect(count.gte(0)).to.be.true; + }); + }); + }); + + describe('getBlockTransactionCountByNumber', () => { + it('returns the transactions of latest', () => { + return ethapi.eth.getBlockTransactionCountByNumber().then((count) => { + expect(count).to.be.ok; + expect(count.gte(0)).to.be.true; + }); + }); + + it('returns the transactions of a specified number', () => { + return ethapi.eth.getBlockTransactionCountByNumber(latestBlockNumber).then((count) => { + expect(count).to.be.ok; + expect(count.gte(0)).to.be.true; + }); + }); + }); + + describe('getTransactionCount', () => { + it('returns the count for an address', () => { + return ethapi.eth.getTransactionCount(address).then((count) => { + expect(count).to.be.ok; + expect(count.gte(0x1000c2)).to.be.ok; + }); + }); + + it('returns the count for an address at specified blockNumber', () => { + return ethapi.eth.getTransactionCount(address, latestBlockNumber).then((count) => { + expect(count).to.be.ok; + expect(count.gte(0x1000c2)).to.be.ok; + }); + }); + }); +}); diff --git a/js/src/api/rpc/eth/eth.js b/js/src/api/rpc/eth/eth.js new file mode 100644 index 000000000..60eb172a3 --- /dev/null +++ b/js/src/api/rpc/eth/eth.js @@ -0,0 +1,329 @@ +// Copyright 2015, 2016 Ethcore (UK) Ltd. +// This file is part of Parity. + +// Parity is free software: you can redistribute it and/or modify +// it under the terms of the GNU General Public License as published by +// the Free Software Foundation, either version 3 of the License, or +// (at your option) any later version. + +// Parity is distributed in the hope that it will be useful, +// but WITHOUT ANY WARRANTY; without even the implied warranty of +// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the +// GNU General Public License for more details. + +// You should have received a copy of the GNU General Public License +// along with Parity. If not, see . + +import { inAddress, inBlockNumber, inData, inFilter, inHex, inNumber16, inOptions } from '../../format/input'; +import { outAddress, outBlock, outLog, outNumber, outReceipt, outTransaction } from '../../format/output'; + +export default class Eth { + constructor (transport) { + this._transport = transport; + } + + accounts () { + return this._transport + .execute('eth_accounts') + .then((accounts) => (accounts || []).map(outAddress)); + } + + blockNumber () { + return this._transport + .execute('eth_blockNumber') + .then(outNumber); + } + + call (options, blockNumber = 'latest') { + return this._transport + .execute('eth_call', inOptions(options), inBlockNumber(blockNumber)); + } + + checkRequest (requestId) { + return this._transport + .execute('eth_checkRequest', inNumber16(requestId)); + } + + coinbase () { + return this._transport + .execute('eth_coinbase') + .then(outAddress); + } + + compileLLL (code) { + return this._transport + .execute('eth_compileLLL', inData(code)); + } + + compileSerpent (code) { + return this._transport + .execute('eth_compileSerpent', inData(code)); + } + + compileSolidity (code) { + return this._transport + .execute('eth_compileSolidity', inData(code)); + } + + estimateGas (options) { + return this._transport + .execute('eth_estimateGas', inOptions(options)) + .then(outNumber); + } + + fetchQueuedTransactions () { + return this._transport + .execute('eth_fetchQueuedTransactions'); + } + + flush () { + return this._transport + .execute('eth_flush'); + } + + gasPrice () { + return this._transport + .execute('eth_gasPrice') + .then(outNumber); + } + + getBalance (address, blockNumber = 'latest') { + return this._transport + .execute('eth_getBalance', inAddress(address), inBlockNumber(blockNumber)) + .then(outNumber); + } + + getBlockByHash (hash, full = false) { + return this._transport + .execute('eth_getBlockByHash', inHex(hash), full) + .then(outBlock); + } + + getBlockByNumber (blockNumber = 'latest', full = false) { + return this._transport + .execute('eth_getBlockByNumber', inBlockNumber(blockNumber), full) + .then(outBlock); + } + + getBlockTransactionCountByHash (hash) { + return this._transport + .execute('eth_getBlockTransactionCountByHash', inHex(hash)) + .then(outNumber); + } + + getBlockTransactionCountByNumber (blockNumber = 'latest') { + return this._transport + .execute('eth_getBlockTransactionCountByNumber', inBlockNumber(blockNumber)) + .then(outNumber); + } + + getCode (address, blockNumber = 'latest') { + return this._transport + .execute('eth_getCode', inAddress(address), inBlockNumber(blockNumber)); + } + + getCompilers () { + return this._transport + .execute('eth_getCompilers'); + } + + getFilterChanges (filterId) { + return this._transport + .execute('eth_getFilterChanges', inNumber16(filterId)) + .then((logs) => logs.map(outLog)); + } + + getFilterChangesEx (filterId) { + return this._transport + .execute('eth_getFilterChangesEx', inNumber16(filterId)); + } + + getFilterLogs (filterId) { + return this._transport + .execute('eth_getFilterLogs', inNumber16(filterId)) + .then((logs) => logs.map(outLog)); + } + + getFilterLogsEx (filterId) { + return this._transport + .execute('eth_getFilterLogsEx', inNumber16(filterId)); + } + + getLogs (options) { + return this._transport + .execute('eth_getLogs', inFilter(options)); + } + + getLogsEx (options) { + return this._transport + .execute('eth_getLogsEx', inFilter(options)); + } + + getStorageAt (address, index = 0, blockNumber = 'latest') { + return this._transport + .execute('eth_getStorageAt', inAddress(address), inNumber16(index), inBlockNumber(blockNumber)); + } + + getTransactionByBlockHashAndIndex (hash, index = 0) { + return this._transport + .execute('eth_getTransactionByBlockHashAndIndex', inHex(hash), inNumber16(index)) + .then(outTransaction); + } + + getTransactionByBlockNumberAndIndex (blockNumber = 'latest', index = 0) { + return this._transport + .execute('eth_getTransactionByBlockNumberAndIndex', inBlockNumber(blockNumber), inNumber16(index)) + .then(outTransaction); + } + + getTransactionByHash (hash) { + return this._transport + .execute('eth_getTransactionByHash', inHex(hash)) + .then(outTransaction); + } + + getTransactionCount (address, blockNumber = 'latest') { + return this._transport + .execute('eth_getTransactionCount', inAddress(address), inBlockNumber(blockNumber)) + .then(outNumber); + } + + getTransactionReceipt (txhash) { + return this._transport + .execute('eth_getTransactionReceipt', inHex(txhash)) + .then(outReceipt); + } + + getUncleByBlockHashAndIndex (hash, index = 0) { + return this._transport + .execute('eth_getUncleByBlockHashAndIndex', inHex(hash), inNumber16(index)); + } + + getUncleByBlockNumberAndIndex (blockNumber = 'latest', index = 0) { + return this._transport + .execute('eth_getUncleByBlockNumberAndIndex', inBlockNumber(blockNumber), inNumber16(index)); + } + + getUncleCountByBlockHash (hash) { + return this._transport + .execute('eth_getUncleCountByBlockHash', inHex(hash)) + .then(outNumber); + } + + getUncleCountByBlockNumber (blockNumber = 'latest') { + return this._transport + .execute('eth_getUncleCountByBlockNumber', inBlockNumber(blockNumber)) + .then(outNumber); + } + + getWork () { + return this._transport + .execute('eth_getWork'); + } + + hashrate () { + return this._transport + .execute('eth_hashrate') + .then(outNumber); + } + + inspectTransaction () { + return this._transport + .execute('eth_inspectTransaction'); + } + + mining () { + return this._transport + .execute('eth_mining'); + } + + newBlockFilter () { + return this._transport + .execute('eth_newBlockFilter'); + } + + newFilter (options) { + return this._transport + .execute('eth_newFilter', inFilter(options)); + } + + newFilterEx (options) { + return this._transport + .execute('eth_newFilterEx', inFilter(options)); + } + + newPendingTransactionFilter () { + return this._transport + .execute('eth_newPendingTransactionFilter'); + } + + notePassword () { + return this._transport + .execute('eth_notePassword'); + } + + pendingTransactions () { + return this._transport + .execute('eth_pendingTransactions'); + } + + postTransaction (options) { + return this._transport + .execute('eth_postTransaction', inOptions(options)); + } + + protocolVersion () { + return this._transport + .execute('eth_protocolVersion'); + } + + register () { + return this._transport + .execute('eth_register'); + } + + sendRawTransaction (data) { + return this._transport + .execute('eth_sendRawTransaction', inData(data)); + } + + sendTransaction (options) { + return this._transport + .execute('eth_sendTransaction', inOptions(options)); + } + + sign () { + return this._transport + .execute('eth_sign'); + } + + signTransaction () { + return this._transport + .execute('eth_signTransaction'); + } + + submitHashrate (hashrate, clientId) { + return this._transport + .execute('eth_submitHashrate', inNumber16(hashrate), clientId); + } + + submitWork (nonce, powHash, mixDigest) { + return this._transport + .execute('eth_submitWork', inNumber16(nonce), powHash, mixDigest); + } + + syncing () { + return this._transport + .execute('eth_syncing'); + } + + uninstallFilter (filterId) { + return this._transport + .execute('eth_uninstallFilter', inHex(filterId)); + } + + unregister () { + return this._transport + .execute('eth_unregister'); + } +} diff --git a/js/src/api/rpc/eth/eth.spec.js b/js/src/api/rpc/eth/eth.spec.js new file mode 100644 index 000000000..65377db50 --- /dev/null +++ b/js/src/api/rpc/eth/eth.spec.js @@ -0,0 +1,474 @@ +// Copyright 2015, 2016 Ethcore (UK) Ltd. +// This file is part of Parity. + +// Parity is free software: you can redistribute it and/or modify +// it under the terms of the GNU General Public License as published by +// the Free Software Foundation, either version 3 of the License, or +// (at your option) any later version. + +// Parity is distributed in the hope that it will be useful, +// but WITHOUT ANY WARRANTY; without even the implied warranty of +// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the +// GNU General Public License for more details. + +// You should have received a copy of the GNU General Public License +// along with Parity. If not, see . + +import { TEST_HTTP_URL, mockHttp } from '../../../../test/mockRpc'; +import { isBigNumber } from '../../../../test/types'; + +import Http from '../../transport/http'; +import Eth from './eth'; + +const instance = new Eth(new Http(TEST_HTTP_URL)); + +describe('rpc/Eth', () => { + const address = '0x63Cf90D3f0410092FC0fca41846f596223979195'; + let scope; + + describe('accounts', () => { + beforeEach(() => { + scope = mockHttp([{ method: 'eth_accounts', reply: { result: [address.toLowerCase()] } }]); + }); + + it('returns a list of accounts, formatted', () => { + return instance.accounts().then((accounts) => { + expect(accounts).to.deep.equal([address]); + }); + }); + }); + + describe('blockNumber', () => { + beforeEach(() => { + scope = mockHttp([{ method: 'eth_blockNumber', reply: { result: '0x123456' } }]); + }); + + it('returns the current blockNumber, formatted', () => { + return instance.blockNumber().then((blockNumber) => { + expect(isBigNumber(blockNumber)).to.be.true; + expect(blockNumber.toString(16)).to.equal('123456'); + }); + }); + }); + + describe('call', () => { + beforeEach(() => { + scope = mockHttp([{ method: 'eth_call', reply: { result: [] } }]); + }); + + it('formats the input options & blockNumber', () => { + return instance.call({ data: '12345678' }, 'earliest').then(() => { + expect(scope.body.eth_call.params).to.deep.equal([{ data: '0x12345678' }, 'earliest']); + }); + }); + + it('provides a latest blockNumber when not specified', () => { + return instance.call({ data: '12345678' }).then(() => { + expect(scope.body.eth_call.params).to.deep.equal([{ data: '0x12345678' }, 'latest']); + }); + }); + }); + + describe('coinbase', () => { + beforeEach(() => { + scope = mockHttp([{ method: 'eth_coinbase', reply: { result: address.toLowerCase() } }]); + }); + + it('returns the coinbase, formatted', () => { + return instance.coinbase().then((account) => { + expect(account).to.deep.equal(address); + }); + }); + }); + + ['LLL', 'Serpent', 'Solidity'].forEach((type) => { + const method = `compile${type}`; + + describe(method, () => { + beforeEach(() => { + scope = mockHttp([{ method: `eth_${method}`, reply: { result: '0x123' } }]); + }); + + it('formats the input as data, returns the output', () => { + return instance[method]('0xabcdef').then((result) => { + expect(scope.body[`eth_${method}`].params).to.deep.equal(['0xabcdef']); + expect(result).to.equal('0x123'); + }); + }); + }); + }); + + describe('estimateGas', () => { + beforeEach(() => { + scope = mockHttp([{ method: 'eth_estimateGas', reply: { result: '0x123' } }]); + }); + + it('converts the options correctly', () => { + return instance.estimateGas({ gas: 21000 }).then(() => { + expect(scope.body.eth_estimateGas.params).to.deep.equal([{ gas: '0x5208' }]); + }); + }); + + it('returns the gas used', () => { + return instance.estimateGas({}).then((gas) => { + expect(isBigNumber(gas)).to.be.true; + expect(gas.toString(16)).to.deep.equal('123'); + }); + }); + }); + + describe('gasPrice', () => { + beforeEach(() => { + scope = mockHttp([{ method: 'eth_gasPrice', reply: { result: '0x123' } }]); + }); + + it('returns the fomratted price', () => { + return instance.gasPrice().then((price) => { + expect(isBigNumber(price)).to.be.true; + expect(price.toString(16)).to.deep.equal('123'); + }); + }); + }); + + describe('getBalance', () => { + beforeEach(() => { + scope = mockHttp([{ method: 'eth_getBalance', reply: { result: '0x123' } }]); + }); + + it('passes in the address (default blockNumber)', () => { + return instance.getBalance(address).then(() => { + expect(scope.body.eth_getBalance.params).to.deep.equal([address.toLowerCase(), 'latest']); + }); + }); + + it('passes in the address & blockNumber', () => { + return instance.getBalance(address, 0x456).then(() => { + expect(scope.body.eth_getBalance.params).to.deep.equal([address.toLowerCase(), '0x456']); + }); + }); + + it('returns the balance', () => { + return instance.getBalance(address, 0x123).then((balance) => { + expect(isBigNumber(balance)).to.be.true; + expect(balance.toString(16)).to.deep.equal('123'); + }); + }); + }); + + describe('getBlockByHash', () => { + beforeEach(() => { + scope = mockHttp([{ method: 'eth_getBlockByHash', reply: { result: { miner: address.toLowerCase() } } }]); + }); + + it('formats the input hash as a hash, default full', () => { + return instance.getBlockByHash('1234').then(() => { + expect(scope.body.eth_getBlockByHash.params).to.deep.equal(['0x1234', false]); + }); + }); + + it('formats the input hash as a hash, full true', () => { + return instance.getBlockByHash('1234', true).then(() => { + expect(scope.body.eth_getBlockByHash.params).to.deep.equal(['0x1234', true]); + }); + }); + + it('formats the output into block', () => { + return instance.getBlockByHash('1234').then((block) => { + expect(block.miner).to.equal(address); + }); + }); + }); + + describe('getBlockByNumber', () => { + beforeEach(() => { + scope = mockHttp([{ method: 'eth_getBlockByNumber', reply: { result: { miner: address.toLowerCase() } } }]); + }); + + it('assumes blockNumber latest & full false', () => { + return instance.getBlockByNumber().then(() => { + expect(scope.body.eth_getBlockByNumber.params).to.deep.equal(['latest', false]); + }); + }); + + it('uses input blockNumber & full false', () => { + return instance.getBlockByNumber('0x1234').then(() => { + expect(scope.body.eth_getBlockByNumber.params).to.deep.equal(['0x1234', false]); + }); + }); + + it('formats the input blockNumber, full true', () => { + return instance.getBlockByNumber(0x1234, true).then(() => { + expect(scope.body.eth_getBlockByNumber.params).to.deep.equal(['0x1234', true]); + }); + }); + + it('formats the output into block', () => { + return instance.getBlockByNumber(0x1234).then((block) => { + expect(block.miner).to.equal(address); + }); + }); + }); + + describe('getBlockTransactionCountByHash', () => { + beforeEach(() => { + scope = mockHttp([{ method: 'eth_getBlockTransactionCountByHash', reply: { result: '0x123' } }]); + }); + + it('formats input hash properly', () => { + return instance.getBlockTransactionCountByHash('abcdef').then(() => { + expect(scope.body.eth_getBlockTransactionCountByHash.params).to.deep.equal(['0xabcdef']); + }); + }); + + it('formats the output number', () => { + return instance.getBlockTransactionCountByHash('0x1234').then((count) => { + expect(isBigNumber(count)).to.be.true; + expect(count.toString(16)).to.equal('123'); + }); + }); + }); + + describe('getBlockTransactionCountByNumber', () => { + beforeEach(() => { + scope = mockHttp([{ method: 'eth_getBlockTransactionCountByNumber', reply: { result: '0x123' } }]); + }); + + it('specified blockNumber latest when none specified', () => { + return instance.getBlockTransactionCountByNumber().then(() => { + expect(scope.body.eth_getBlockTransactionCountByNumber.params).to.deep.equal(['latest']); + }); + }); + + it('formats input blockNumber properly', () => { + return instance.getBlockTransactionCountByNumber(0xabcdef).then(() => { + expect(scope.body.eth_getBlockTransactionCountByNumber.params).to.deep.equal(['0xabcdef']); + }); + }); + + it('formats the output number', () => { + return instance.getBlockTransactionCountByNumber('0x1234').then((count) => { + expect(isBigNumber(count)).to.be.true; + expect(count.toString(16)).to.equal('123'); + }); + }); + }); + + describe('getCode', () => { + beforeEach(() => { + scope = mockHttp([{ method: 'eth_getCode', reply: { result: '0x1234567890' } }]); + }); + + it('passes in the address (default blockNumber)', () => { + return instance.getCode(address).then(() => { + expect(scope.body.eth_getCode.params).to.deep.equal([address.toLowerCase(), 'latest']); + }); + }); + + it('passes in the address & blockNumber', () => { + return instance.getCode(address, 0x456).then(() => { + expect(scope.body.eth_getCode.params).to.deep.equal([address.toLowerCase(), '0x456']); + }); + }); + + it('returns the code', () => { + return instance.getCode(address, 0x123).then((code) => { + expect(code).to.equal('0x1234567890'); + }); + }); + }); + + describe('getStorageAt', () => { + beforeEach(() => { + scope = mockHttp([{ method: 'eth_getStorageAt', reply: { result: '0x1234567890' } }]); + }); + + it('passes in the address (default index& blockNumber)', () => { + return instance.getStorageAt(address).then(() => { + expect(scope.body.eth_getStorageAt.params).to.deep.equal([address.toLowerCase(), '0x0', 'latest']); + }); + }); + + it('passes in the address, index & blockNumber', () => { + return instance.getStorageAt(address, 15, 0x456).then(() => { + expect(scope.body.eth_getStorageAt.params).to.deep.equal([address.toLowerCase(), '0xf', '0x456']); + }); + }); + + it('returns the storage', () => { + return instance.getStorageAt(address, 0x123).then((storage) => { + expect(storage).to.equal('0x1234567890'); + }); + }); + }); + + describe('getTransactionByBlockHashAndIndex', () => { + beforeEach(() => { + scope = mockHttp([{ method: 'eth_getTransactionByBlockHashAndIndex', reply: { result: { to: address.toLowerCase() } } }]); + }); + + it('passes in the hash (default index)', () => { + return instance.getTransactionByBlockHashAndIndex('12345').then(() => { + expect(scope.body.eth_getTransactionByBlockHashAndIndex.params).to.deep.equal(['0x12345', '0x0']); + }); + }); + + it('passes in the hash & specified index', () => { + return instance.getTransactionByBlockHashAndIndex('6789', 0x456).then(() => { + expect(scope.body.eth_getTransactionByBlockHashAndIndex.params).to.deep.equal(['0x6789', '0x456']); + }); + }); + + it('returns the formatted transaction', () => { + return instance.getTransactionByBlockHashAndIndex('6789', 0x123).then((tx) => { + expect(tx).to.deep.equal({ to: address }); + }); + }); + }); + + describe('getTransactionByBlockNumberAndIndex', () => { + beforeEach(() => { + scope = mockHttp([{ method: 'eth_getTransactionByBlockNumberAndIndex', reply: { result: { to: address.toLowerCase() } } }]); + }); + + it('passes in the default parameters', () => { + return instance.getTransactionByBlockNumberAndIndex().then(() => { + expect(scope.body.eth_getTransactionByBlockNumberAndIndex.params).to.deep.equal(['latest', '0x0']); + }); + }); + + it('passes in the blockNumber & specified index', () => { + return instance.getTransactionByBlockNumberAndIndex('0x6789', 0x456).then(() => { + expect(scope.body.eth_getTransactionByBlockNumberAndIndex.params).to.deep.equal(['0x6789', '0x456']); + }); + }); + + it('returns the formatted transaction', () => { + return instance.getTransactionByBlockNumberAndIndex('0x6789', 0x123).then((tx) => { + expect(tx).to.deep.equal({ to: address }); + }); + }); + }); + + describe('getTransactionByHash', () => { + beforeEach(() => { + scope = mockHttp([{ method: 'eth_getTransactionByHash', reply: { result: { to: address.toLowerCase() } } }]); + }); + + it('passes in the hash', () => { + return instance.getTransactionByHash('12345').then(() => { + expect(scope.body.eth_getTransactionByHash.params).to.deep.equal(['0x12345']); + }); + }); + + it('returns the formatted transaction', () => { + return instance.getTransactionByHash('6789').then((tx) => { + expect(tx).to.deep.equal({ to: address }); + }); + }); + }); + + describe('getTransactionCount', () => { + beforeEach(() => { + scope = mockHttp([{ method: 'eth_getTransactionCount', reply: { result: '0x123' } }]); + }); + + it('passes in the address (default blockNumber)', () => { + return instance.getTransactionCount(address).then(() => { + expect(scope.body.eth_getTransactionCount.params).to.deep.equal([address.toLowerCase(), 'latest']); + }); + }); + + it('passes in the address & blockNumber', () => { + return instance.getTransactionCount(address, 0x456).then(() => { + expect(scope.body.eth_getTransactionCount.params).to.deep.equal([address.toLowerCase(), '0x456']); + }); + }); + + it('returns the count, formatted', () => { + return instance.getTransactionCount(address, 0x123).then((count) => { + expect(isBigNumber(count)).to.be.true; + expect(count.toString(16)).to.equal('123'); + }); + }); + }); + + describe('getUncleByBlockHashAndIndex', () => { + beforeEach(() => { + scope = mockHttp([{ method: 'eth_getUncleByBlockHashAndIndex', reply: { result: [] } }]); + }); + + it('passes in the hash (default index)', () => { + return instance.getUncleByBlockHashAndIndex('12345').then(() => { + expect(scope.body.eth_getUncleByBlockHashAndIndex.params).to.deep.equal(['0x12345', '0x0']); + }); + }); + + it('passes in the hash & specified index', () => { + return instance.getUncleByBlockHashAndIndex('6789', 0x456).then(() => { + expect(scope.body.eth_getUncleByBlockHashAndIndex.params).to.deep.equal(['0x6789', '0x456']); + }); + }); + }); + + describe('getUncleByBlockNumberAndIndex', () => { + beforeEach(() => { + scope = mockHttp([{ method: 'eth_getUncleByBlockNumberAndIndex', reply: { result: [] } }]); + }); + + it('passes in the default parameters', () => { + return instance.getUncleByBlockNumberAndIndex().then(() => { + expect(scope.body.eth_getUncleByBlockNumberAndIndex.params).to.deep.equal(['latest', '0x0']); + }); + }); + + it('passes in the blockNumber & specified index', () => { + return instance.getUncleByBlockNumberAndIndex('0x6789', 0x456).then(() => { + expect(scope.body.eth_getUncleByBlockNumberAndIndex.params).to.deep.equal(['0x6789', '0x456']); + }); + }); + }); + + describe('getUncleCountByBlockHash', () => { + beforeEach(() => { + scope = mockHttp([{ method: 'eth_getUncleCountByBlockHash', reply: { result: '0x123' } }]); + }); + + it('passes in the hash', () => { + return instance.getUncleCountByBlockHash('12345').then(() => { + expect(scope.body.eth_getUncleCountByBlockHash.params).to.deep.equal(['0x12345']); + }); + }); + + it('formats the output number', () => { + return instance.getUncleCountByBlockHash('0x1234').then((count) => { + expect(isBigNumber(count)).to.be.true; + expect(count.toString(16)).to.equal('123'); + }); + }); + }); + + describe('getUncleCountByBlockNumber', () => { + beforeEach(() => { + scope = mockHttp([{ method: 'eth_getUncleCountByBlockNumber', reply: { result: '0x123' } }]); + }); + + it('passes in the default parameters', () => { + return instance.getUncleCountByBlockNumber().then(() => { + expect(scope.body.eth_getUncleCountByBlockNumber.params).to.deep.equal(['latest']); + }); + }); + + it('passes in the blockNumber', () => { + return instance.getUncleCountByBlockNumber('0x6789').then(() => { + expect(scope.body.eth_getUncleCountByBlockNumber.params).to.deep.equal(['0x6789']); + }); + }); + + it('formats the output number', () => { + return instance.getUncleCountByBlockNumber('0x1234').then((count) => { + expect(isBigNumber(count)).to.be.true; + expect(count.toString(16)).to.equal('123'); + }); + }); + }); +}); diff --git a/js/src/api/rpc/eth/index.js b/js/src/api/rpc/eth/index.js new file mode 100644 index 000000000..0d0e8c557 --- /dev/null +++ b/js/src/api/rpc/eth/index.js @@ -0,0 +1,17 @@ +// Copyright 2015, 2016 Ethcore (UK) Ltd. +// This file is part of Parity. + +// Parity is free software: you can redistribute it and/or modify +// it under the terms of the GNU General Public License as published by +// the Free Software Foundation, either version 3 of the License, or +// (at your option) any later version. + +// Parity is distributed in the hope that it will be useful, +// but WITHOUT ANY WARRANTY; without even the implied warranty of +// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the +// GNU General Public License for more details. + +// You should have received a copy of the GNU General Public License +// along with Parity. If not, see . + +export default from './eth'; diff --git a/js/src/api/rpc/ethcore/ethcore.e2e.js b/js/src/api/rpc/ethcore/ethcore.e2e.js new file mode 100644 index 000000000..ee4056b50 --- /dev/null +++ b/js/src/api/rpc/ethcore/ethcore.e2e.js @@ -0,0 +1,61 @@ +// Copyright 2015, 2016 Ethcore (UK) Ltd. +// This file is part of Parity. + +// Parity is free software: you can redistribute it and/or modify +// it under the terms of the GNU General Public License as published by +// the Free Software Foundation, either version 3 of the License, or +// (at your option) any later version. + +// Parity is distributed in the hope that it will be useful, +// but WITHOUT ANY WARRANTY; without even the implied warranty of +// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the +// GNU General Public License for more details. + +// You should have received a copy of the GNU General Public License +// along with Parity. If not, see . + +import { createHttpApi } from '../../../../test/e2e/ethapi'; + +describe('ethapi.ethcore', () => { + const ethapi = createHttpApi(); + + describe('gasFloorTarget', () => { + it('returns and translates the target', () => { + return ethapi.ethcore.gasFloorTarget().then((value) => { + expect(value.gt(0)).to.be.true; + }); + }); + }); + + describe('netChain', () => { + it('returns and the chain', () => { + return ethapi.ethcore.netChain().then((value) => { + expect(value).to.equal('morden'); + }); + }); + }); + + describe('netPort', () => { + it('returns and translates the port', () => { + return ethapi.ethcore.netPort().then((value) => { + expect(value.gt(0)).to.be.true; + }); + }); + }); + + describe('transactionsLimit', () => { + it('returns and translates the limit', () => { + return ethapi.ethcore.transactionsLimit().then((value) => { + expect(value.gt(0)).to.be.true; + }); + }); + }); + + describe('rpcSettings', () => { + it('returns and translates the settings', () => { + return ethapi.ethcore.rpcSettings().then((value) => { + expect(value).to.be.ok; + }); + }); + }); +}); diff --git a/js/src/api/rpc/ethcore/ethcore.js b/js/src/api/rpc/ethcore/ethcore.js new file mode 100644 index 000000000..1ccc95bba --- /dev/null +++ b/js/src/api/rpc/ethcore/ethcore.js @@ -0,0 +1,168 @@ +// Copyright 2015, 2016 Ethcore (UK) Ltd. +// This file is part of Parity. + +// Parity is free software: you can redistribute it and/or modify +// it under the terms of the GNU General Public License as published by +// the Free Software Foundation, either version 3 of the License, or +// (at your option) any later version. + +// Parity is distributed in the hope that it will be useful, +// but WITHOUT ANY WARRANTY; without even the implied warranty of +// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the +// GNU General Public License for more details. + +// You should have received a copy of the GNU General Public License +// along with Parity. If not, see . + +import { inAddress, inData, inNumber16 } from '../../format/input'; +import { outAddress, outNumber, outPeers } from '../../format/output'; + +export default class Ethcore { + constructor (transport) { + this._transport = transport; + } + + acceptNonReservedPeers () { + return this._transport + .execute('ethcore_acceptNonReservedPeers'); + } + + addReservedPeer (encode) { + return this._transport + .execute('ethcore_addReservedPeer', encode); + } + + defaultExtraData () { + return this._transport + .execute('ethcore_defaultExtraData'); + } + + devLogs () { + return this._transport + .execute('ethcore_devLogs'); + } + + devLogsLevels () { + return this._transport + .execute('ethcore_devLogsLevels'); + } + + dropNonReservedPeers () { + return this._transport + .execute('ethcore_dropNonReservedPeers'); + } + + extraData () { + return this._transport + .execute('ethcore_extraData'); + } + + gasFloorTarget () { + return this._transport + .execute('ethcore_gasFloorTarget') + .then(outNumber); + } + + generateSecretPhrase () { + return this._transport + .execute('ethcore_generateSecretPhrase'); + } + + hashContent (url) { + return this._transport + .execute('ethcore_hashContent', url); + } + + minGasPrice () { + return this._transport + .execute('ethcore_minGasPrice') + .then(outNumber); + } + + netChain () { + return this._transport + .execute('ethcore_netChain'); + } + + netPeers () { + return this._transport + .execute('ethcore_netPeers') + .then(outPeers); + } + + netMaxPeers () { + return this._transport + .execute('ethcore_netMaxPeers') + .then(outNumber); + } + + netPort () { + return this._transport + .execute('ethcore_netPort') + .then(outNumber); + } + + nodeName () { + return this._transport + .execute('ethcore_nodeName'); + } + + phraseToAddress (phrase) { + return this._transport + .execute('ethcore_phraseToAddress', phrase) + .then(outAddress); + } + + registryAddress () { + return this._transport + .execute('ethcore_registryAddress') + .then(outAddress); + } + + removeReservedPeer (encode) { + return this._transport + .execute('ethcore_removeReservedPeer', encode); + } + + rpcSettings () { + return this._transport + .execute('ethcore_rpcSettings'); + } + + setAuthor (address) { + return this._transport + .execute('ethcore_setAuthor', inAddress(address)); + } + + setExtraData (data) { + return this._transport + .execute('ethcore_setExtraData', inData(data)); + } + + setGasFloorTarget (quantity) { + return this._transport + .execute('ethcore_setGasFloorTarget', inNumber16(quantity)); + } + + setMinGasPrice (quantity) { + return this._transport + .execute('ethcore_setMinGasPrice', inNumber16(quantity)); + } + + setTransactionsLimit (quantity) { + return this._transport + .execute('ethcore_setTransactionsLimit', inNumber16(quantity)); + } + + transactionsLimit () { + return this._transport + .execute('ethcore_transactionsLimit') + .then(outNumber); + } + + unsignedTransactionsCount () { + return this._transport + .execute('ethcore_unsignedTransactionsCount') + .then(outNumber); + } +} diff --git a/js/src/api/rpc/ethcore/ethcore.spec.js b/js/src/api/rpc/ethcore/ethcore.spec.js new file mode 100644 index 000000000..fd34550a7 --- /dev/null +++ b/js/src/api/rpc/ethcore/ethcore.spec.js @@ -0,0 +1,92 @@ +// Copyright 2015, 2016 Ethcore (UK) Ltd. +// This file is part of Parity. + +// Parity is free software: you can redistribute it and/or modify +// it under the terms of the GNU General Public License as published by +// the Free Software Foundation, either version 3 of the License, or +// (at your option) any later version. + +// Parity is distributed in the hope that it will be useful, +// but WITHOUT ANY WARRANTY; without even the implied warranty of +// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the +// GNU General Public License for more details. + +// You should have received a copy of the GNU General Public License +// along with Parity. If not, see . + +import { TEST_HTTP_URL, mockHttp } from '../../../../test/mockRpc'; +import { isBigNumber } from '../../../../test/types'; + +import Http from '../../transport/http'; +import Ethcore from './ethcore'; + +const instance = new Ethcore(new Http(TEST_HTTP_URL)); + +describe('api/rpc/Ethcore', () => { + describe('gasFloorTarget', () => { + it('returns the gasfloor, formatted', () => { + mockHttp([{ method: 'ethcore_gasFloorTarget', reply: { result: '0x123456' } }]); + + return instance.gasFloorTarget().then((count) => { + expect(isBigNumber(count)).to.be.true; + expect(count.eq(0x123456)).to.be.true; + }); + }); + }); + + describe('minGasPrice', () => { + it('returns the min gasprice, formatted', () => { + mockHttp([{ method: 'ethcore_minGasPrice', reply: { result: '0x123456' } }]); + + return instance.minGasPrice().then((count) => { + expect(isBigNumber(count)).to.be.true; + expect(count.eq(0x123456)).to.be.true; + }); + }); + }); + + describe('netMaxPeers', () => { + it('returns the max peers, formatted', () => { + mockHttp([{ method: 'ethcore_netMaxPeers', reply: { result: 25 } }]); + + return instance.netMaxPeers().then((count) => { + expect(isBigNumber(count)).to.be.true; + expect(count.eq(25)).to.be.true; + }); + }); + }); + + describe('newPeers', () => { + it('returns the peer structure, formatted', () => { + mockHttp([{ method: 'ethcore_netPeers', reply: { result: { active: 123, connected: 456, max: 789 } } }]); + + return instance.netPeers().then((peers) => { + expect(peers.active.eq(123)).to.be.true; + expect(peers.connected.eq(456)).to.be.true; + expect(peers.max.eq(789)).to.be.true; + }); + }); + }); + + describe('netPort', () => { + it('returns the connected port, formatted', () => { + mockHttp([{ method: 'ethcore_netPort', reply: { result: 33030 } }]); + + return instance.netPort().then((count) => { + expect(isBigNumber(count)).to.be.true; + expect(count.eq(33030)).to.be.true; + }); + }); + }); + + describe('transactionsLimit', () => { + it('returns the tx limit, formatted', () => { + mockHttp([{ method: 'ethcore_transactionsLimit', reply: { result: 1024 } }]); + + return instance.transactionsLimit().then((count) => { + expect(isBigNumber(count)).to.be.true; + expect(count.eq(1024)).to.be.true; + }); + }); + }); +}); diff --git a/js/src/api/rpc/ethcore/index.js b/js/src/api/rpc/ethcore/index.js new file mode 100644 index 000000000..2372a2171 --- /dev/null +++ b/js/src/api/rpc/ethcore/index.js @@ -0,0 +1,17 @@ +// Copyright 2015, 2016 Ethcore (UK) Ltd. +// This file is part of Parity. + +// Parity is free software: you can redistribute it and/or modify +// it under the terms of the GNU General Public License as published by +// the Free Software Foundation, either version 3 of the License, or +// (at your option) any later version. + +// Parity is distributed in the hope that it will be useful, +// but WITHOUT ANY WARRANTY; without even the implied warranty of +// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the +// GNU General Public License for more details. + +// You should have received a copy of the GNU General Public License +// along with Parity. If not, see . + +export default from './ethcore'; diff --git a/js/src/api/rpc/index.js b/js/src/api/rpc/index.js new file mode 100644 index 000000000..e7e94b9ed --- /dev/null +++ b/js/src/api/rpc/index.js @@ -0,0 +1,24 @@ +// Copyright 2015, 2016 Ethcore (UK) Ltd. +// This file is part of Parity. + +// Parity is free software: you can redistribute it and/or modify +// it under the terms of the GNU General Public License as published by +// the Free Software Foundation, either version 3 of the License, or +// (at your option) any later version. + +// Parity is distributed in the hope that it will be useful, +// but WITHOUT ANY WARRANTY; without even the implied warranty of +// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the +// GNU General Public License for more details. + +// You should have received a copy of the GNU General Public License +// along with Parity. If not, see . + +export Db from './db'; +export Eth from './eth'; +export Ethcore from './ethcore'; +export Net from './net'; +export Personal from './personal'; +export Shh from './shh'; +export Trace from './trace'; +export Web3 from './web3'; diff --git a/js/src/api/rpc/net/index.js b/js/src/api/rpc/net/index.js new file mode 100644 index 000000000..0739111e6 --- /dev/null +++ b/js/src/api/rpc/net/index.js @@ -0,0 +1,17 @@ +// Copyright 2015, 2016 Ethcore (UK) Ltd. +// This file is part of Parity. + +// Parity is free software: you can redistribute it and/or modify +// it under the terms of the GNU General Public License as published by +// the Free Software Foundation, either version 3 of the License, or +// (at your option) any later version. + +// Parity is distributed in the hope that it will be useful, +// but WITHOUT ANY WARRANTY; without even the implied warranty of +// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the +// GNU General Public License for more details. + +// You should have received a copy of the GNU General Public License +// along with Parity. If not, see . + +export default from './net'; diff --git a/js/src/api/rpc/net/net.e2e.js b/js/src/api/rpc/net/net.e2e.js new file mode 100644 index 000000000..51d84f7a1 --- /dev/null +++ b/js/src/api/rpc/net/net.e2e.js @@ -0,0 +1,46 @@ +// Copyright 2015, 2016 Ethcore (UK) Ltd. +// This file is part of Parity. + +// Parity is free software: you can redistribute it and/or modify +// it under the terms of the GNU General Public License as published by +// the Free Software Foundation, either version 3 of the License, or +// (at your option) any later version. + +// Parity is distributed in the hope that it will be useful, +// but WITHOUT ANY WARRANTY; without even the implied warranty of +// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the +// GNU General Public License for more details. + +// You should have received a copy of the GNU General Public License +// along with Parity. If not, see . + +import { createHttpApi } from '../../../../test/e2e/ethapi'; +import { isBoolean } from '../../../../test/types'; + +describe('ethapi.net', () => { + const ethapi = createHttpApi(); + + describe('listening', () => { + it('returns the listening status', () => { + return ethapi.net.listening().then((status) => { + expect(isBoolean(status)).to.be.true; + }); + }); + }); + + describe('peerCount', () => { + it('returns the peer count', () => { + return ethapi.net.peerCount().then((count) => { + expect(count.gte(0)).to.be.true; + }); + }); + }); + + describe('version', () => { + it('returns the version', () => { + return ethapi.net.version().then((version) => { + expect(version).to.be.ok; + }); + }); + }); +}); diff --git a/js/src/api/rpc/net/net.js b/js/src/api/rpc/net/net.js new file mode 100644 index 000000000..96e99dc51 --- /dev/null +++ b/js/src/api/rpc/net/net.js @@ -0,0 +1,39 @@ +// Copyright 2015, 2016 Ethcore (UK) Ltd. +// This file is part of Parity. + +// Parity is free software: you can redistribute it and/or modify +// it under the terms of the GNU General Public License as published by +// the Free Software Foundation, either version 3 of the License, or +// (at your option) any later version. + +// Parity is distributed in the hope that it will be useful, +// but WITHOUT ANY WARRANTY; without even the implied warranty of +// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the +// GNU General Public License for more details. + +// You should have received a copy of the GNU General Public License +// along with Parity. If not, see . + +import { outNumber } from '../../format/output'; + +export default class Net { + constructor (transport) { + this._transport = transport; + } + + listening () { + return this._transport + .execute('net_listening'); + } + + peerCount () { + return this._transport + .execute('net_peerCount') + .then(outNumber); + } + + version () { + return this._transport + .execute('net_version'); + } +} diff --git a/js/src/api/rpc/net/net.spec.js b/js/src/api/rpc/net/net.spec.js new file mode 100644 index 000000000..55029a29e --- /dev/null +++ b/js/src/api/rpc/net/net.spec.js @@ -0,0 +1,36 @@ +// Copyright 2015, 2016 Ethcore (UK) Ltd. +// This file is part of Parity. + +// Parity is free software: you can redistribute it and/or modify +// it under the terms of the GNU General Public License as published by +// the Free Software Foundation, either version 3 of the License, or +// (at your option) any later version. + +// Parity is distributed in the hope that it will be useful, +// but WITHOUT ANY WARRANTY; without even the implied warranty of +// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the +// GNU General Public License for more details. + +// You should have received a copy of the GNU General Public License +// along with Parity. If not, see . + +import { TEST_HTTP_URL, mockHttp } from '../../../../test/mockRpc'; +import { isBigNumber } from '../../../../test/types'; + +import Http from '../../transport/http'; +import Net from './net'; + +const instance = new Net(new Http(TEST_HTTP_URL)); + +describe('api/rpc/Net', () => { + describe('peerCount', () => { + it('returns the connected peers, formatted', () => { + mockHttp([{ method: 'net_peerCount', reply: { result: '0x123456' } }]); + + return instance.peerCount().then((count) => { + expect(isBigNumber(count)).to.be.true; + expect(count.eq(0x123456)).to.be.true; + }); + }); + }); +}); diff --git a/js/src/api/rpc/personal/index.js b/js/src/api/rpc/personal/index.js new file mode 100644 index 000000000..a9ed260f2 --- /dev/null +++ b/js/src/api/rpc/personal/index.js @@ -0,0 +1,17 @@ +// Copyright 2015, 2016 Ethcore (UK) Ltd. +// This file is part of Parity. + +// Parity is free software: you can redistribute it and/or modify +// it under the terms of the GNU General Public License as published by +// the Free Software Foundation, either version 3 of the License, or +// (at your option) any later version. + +// Parity is distributed in the hope that it will be useful, +// but WITHOUT ANY WARRANTY; without even the implied warranty of +// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the +// GNU General Public License for more details. + +// You should have received a copy of the GNU General Public License +// along with Parity. If not, see . + +export default from './personal'; diff --git a/js/src/api/rpc/personal/personal.e2e.js b/js/src/api/rpc/personal/personal.e2e.js new file mode 100644 index 000000000..f7fe42c54 --- /dev/null +++ b/js/src/api/rpc/personal/personal.e2e.js @@ -0,0 +1,53 @@ +// Copyright 2015, 2016 Ethcore (UK) Ltd. +// This file is part of Parity. + +// Parity is free software: you can redistribute it and/or modify +// it under the terms of the GNU General Public License as published by +// the Free Software Foundation, either version 3 of the License, or +// (at your option) any later version. + +// Parity is distributed in the hope that it will be useful, +// but WITHOUT ANY WARRANTY; without even the implied warranty of +// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the +// GNU General Public License for more details. + +// You should have received a copy of the GNU General Public License +// along with Parity. If not, see . + +import { createHttpApi } from '../../../../test/e2e/ethapi'; +import { isAddress, isBoolean } from '../../../../test/types'; + +describe.skip('ethapi.personal', () => { + const ethapi = createHttpApi(); + const password = 'P@55word'; + let address; + + describe('newAccount', () => { + it('creates a new account', () => { + return ethapi.personal.newAccount(password).then((_address) => { + address = _address; + expect(isAddress(address)).to.be.ok; + }); + }); + }); + + describe('listAccounts', () => { + it('has the newly-created account', () => { + return ethapi.personal.listAccounts(password).then((accounts) => { + expect(accounts.filter((_address) => _address === address)).to.deep.equal([address]); + accounts.forEach((account) => { + expect(isAddress(account)).to.be.true; + }); + }); + }); + }); + + describe('unlockAccount', () => { + it('unlocks the newly-created account', () => { + return ethapi.personal.unlockAccount(address, password).then((result) => { + expect(isBoolean(result)).to.be.true; + expect(result).to.be.true; + }); + }); + }); +}); diff --git a/js/src/api/rpc/personal/personal.js b/js/src/api/rpc/personal/personal.js new file mode 100644 index 000000000..2609bc509 --- /dev/null +++ b/js/src/api/rpc/personal/personal.js @@ -0,0 +1,112 @@ +// Copyright 2015, 2016 Ethcore (UK) Ltd. +// This file is part of Parity. + +// Parity is free software: you can redistribute it and/or modify +// it under the terms of the GNU General Public License as published by +// the Free Software Foundation, either version 3 of the License, or +// (at your option) any later version. + +// Parity is distributed in the hope that it will be useful, +// but WITHOUT ANY WARRANTY; without even the implied warranty of +// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the +// GNU General Public License for more details. + +// You should have received a copy of the GNU General Public License +// along with Parity. If not, see . + +import { inAddress, inNumber10, inNumber16, inOptions } from '../../format/input'; +import { outAccountInfo, outAddress, outSignerRequest } from '../../format/output'; + +export default class Personal { + constructor (transport) { + this._transport = transport; + } + + accountsInfo () { + return this._transport + .execute('personal_accountsInfo') + .then(outAccountInfo); + } + + confirmRequest (requestId, options, password) { + return this._transport + .execute('personal_confirmRequest', inNumber16(requestId), options, password); + } + + generateAuthorizationToken () { + return this._transport + .execute('personal_generateAuthorizationToken'); + } + + listAccounts () { + return this._transport + .execute('personal_listAccounts') + .then((accounts) => (accounts || []).map(outAddress)); + } + + listGethAccounts () { + return this._transport + .execute('personal_listGethAccounts') + .then((accounts) => (accounts || []).map(outAddress)); + } + + importGethAccounts (accounts) { + return this._transport + .execute('personal_importGethAccounts', (accounts || []).map(inAddress)) + .then((accounts) => (accounts || []).map(outAddress)); + } + + newAccount (password) { + return this._transport + .execute('personal_newAccount', password) + .then(outAddress); + } + + newAccountFromPhrase (phrase, password) { + return this._transport + .execute('personal_newAccountFromPhrase', phrase, password) + .then(outAddress); + } + + newAccountFromWallet (json, password) { + return this._transport + .execute('personal_newAccountFromWallet', json, password) + .then(outAddress); + } + + rejectRequest (requestId) { + return this._transport + .execute('personal_rejectRequest', inNumber16(requestId)); + } + + requestsToConfirm () { + return this._transport + .execute('personal_requestsToConfirm') + .then((requests) => (requests || []).map(outSignerRequest)); + } + + setAccountName (address, name) { + return this._transport + .execute('personal_setAccountName', inAddress(address), name); + } + + setAccountMeta (address, meta) { + return this._transport + .execute('personal_setAccountMeta', inAddress(address), JSON.stringify(meta)); + } + + signAndSendTransaction (options, password) { + return this._transport + .execute('personal_signAndSendTransaction', inOptions(options), password); + } + + signerEnabled () { + return this._transport + .execute('personal_signerEnabled'); + } + + unlockAccount (account, password, duration = 1) { + return this._transport + .execute('personal_unlockAccount', inAddress(account), password, inNumber10(duration)); + } +} diff --git a/js/src/api/rpc/personal/personal.spec.js b/js/src/api/rpc/personal/personal.spec.js new file mode 100644 index 000000000..70734c7ee --- /dev/null +++ b/js/src/api/rpc/personal/personal.spec.js @@ -0,0 +1,97 @@ +// Copyright 2015, 2016 Ethcore (UK) Ltd. +// This file is part of Parity. + +// Parity is free software: you can redistribute it and/or modify +// it under the terms of the GNU General Public License as published by +// the Free Software Foundation, either version 3 of the License, or +// (at your option) any later version. + +// Parity is distributed in the hope that it will be useful, +// but WITHOUT ANY WARRANTY; without even the implied warranty of +// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the +// GNU General Public License for more details. + +// You should have received a copy of the GNU General Public License +// along with Parity. If not, see . + +import { TEST_HTTP_URL, mockHttp } from '../../../../test/mockRpc'; + +import Http from '../../transport/http'; +import Personal from './personal'; + +const instance = new Personal(new Http(TEST_HTTP_URL)); + +describe('rpc/Personal', () => { + const account = '0x63cf90d3f0410092fc0fca41846f596223979195'; + const checksum = '0x63Cf90D3f0410092FC0fca41846f596223979195'; + let scope; + + describe('accountsInfo', () => { + it('retrieves the available account info', () => { + scope = mockHttp([{ method: 'personal_accountsInfo', reply: { + result: { + '0x63cf90d3f0410092fc0fca41846f596223979195': { + name: 'name', uuid: 'uuid', meta: '{"data":"data"}' + } + } + } }]); + + return instance.accountsInfo().then((result) => { + expect(result).to.deep.equal({ + '0x63Cf90D3f0410092FC0fca41846f596223979195': { + name: 'name', uuid: 'uuid', meta: { + data: 'data' + } + } + }); + }); + }); + }); + + describe('listAccounts', () => { + it('retrieves a list of available accounts', () => { + scope = mockHttp([{ method: 'personal_listAccounts', reply: { result: [account] } }]); + + return instance.listAccounts().then((result) => { + expect(result).to.deep.equal([checksum]); + }); + }); + + it('returns an empty list when none available', () => { + scope = mockHttp([{ method: 'personal_listAccounts', reply: { result: null } }]); + + return instance.listAccounts().then((result) => { + expect(result).to.deep.equal([]); + }); + }); + }); + + describe('newAccount', () => { + it('passes the password, returning the address', () => { + scope = mockHttp([{ method: 'personal_newAccount', reply: { result: account } }]); + + return instance.newAccount('password').then((result) => { + expect(scope.body.personal_newAccount.params).to.deep.equal(['password']); + expect(result).to.equal(checksum); + }); + }); + }); + + describe('unlockAccount', () => { + beforeEach(() => { + scope = mockHttp([{ method: 'personal_unlockAccount', reply: { result: [] } }]); + }); + + it('passes account, password & duration', () => { + return instance.unlockAccount(account, 'password', 0xf).then(() => { + expect(scope.body.personal_unlockAccount.params).to.deep.equal([account, 'password', 15]); + }); + }); + + it('provides a default duration when not specified', () => { + return instance.unlockAccount(account, 'password').then(() => { + expect(scope.body.personal_unlockAccount.params).to.deep.equal([account, 'password', 1]); + }); + }); + }); +}); diff --git a/js/src/api/rpc/shh/index.js b/js/src/api/rpc/shh/index.js new file mode 100644 index 000000000..7b323aeed --- /dev/null +++ b/js/src/api/rpc/shh/index.js @@ -0,0 +1,17 @@ +// Copyright 2015, 2016 Ethcore (UK) Ltd. +// This file is part of Parity. + +// Parity is free software: you can redistribute it and/or modify +// it under the terms of the GNU General Public License as published by +// the Free Software Foundation, either version 3 of the License, or +// (at your option) any later version. + +// Parity is distributed in the hope that it will be useful, +// but WITHOUT ANY WARRANTY; without even the implied warranty of +// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the +// GNU General Public License for more details. + +// You should have received a copy of the GNU General Public License +// along with Parity. If not, see . + +export default from './shh'; diff --git a/js/src/api/rpc/shh/shh.js b/js/src/api/rpc/shh/shh.js new file mode 100644 index 000000000..ad545cac5 --- /dev/null +++ b/js/src/api/rpc/shh/shh.js @@ -0,0 +1,71 @@ +// Copyright 2015, 2016 Ethcore (UK) Ltd. +// This file is part of Parity. + +// Parity is free software: you can redistribute it and/or modify +// it under the terms of the GNU General Public License as published by +// the Free Software Foundation, either version 3 of the License, or +// (at your option) any later version. + +// Parity is distributed in the hope that it will be useful, +// but WITHOUT ANY WARRANTY; without even the implied warranty of +// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the +// GNU General Public License for more details. + +// You should have received a copy of the GNU General Public License +// along with Parity. If not, see . + +export default class Personal { + constructor (transport) { + this._transport = transport; + } + + addToGroup (identity) { + return this._transport + .execute('shh_addToGroup', identity); + } + + getFilterChanges (filterId) { + return this._transport + .execute('shh_getFilterChanges', filterId); + } + + getMessages (filterId) { + return this._transport + .execute('shh_getMessages', filterId); + } + + hasIdentity (identity) { + return this._transport + .execute('shh_hasIdentity', identity); + } + + newFilter (options) { + return this._transport + .execute('shh_newFilter', options); + } + + newGroup () { + return this._transport + .execute('shh_newGroup'); + } + + newIdentity () { + return this._transport + .execute('shh_newIdentity'); + } + + post (options) { + return this._transport + .execute('shh_post', options); + } + + uninstallFilter (filterId) { + return this._transport + .execute('shh_uninstallFilter', filterId); + } + + version () { + return this._transport + .execute('shh_version'); + } +} diff --git a/js/src/api/rpc/trace/index.js b/js/src/api/rpc/trace/index.js new file mode 100644 index 000000000..a14087709 --- /dev/null +++ b/js/src/api/rpc/trace/index.js @@ -0,0 +1,17 @@ +// Copyright 2015, 2016 Ethcore (UK) Ltd. +// This file is part of Parity. + +// Parity is free software: you can redistribute it and/or modify +// it under the terms of the GNU General Public License as published by +// the Free Software Foundation, either version 3 of the License, or +// (at your option) any later version. + +// Parity is distributed in the hope that it will be useful, +// but WITHOUT ANY WARRANTY; without even the implied warranty of +// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the +// GNU General Public License for more details. + +// You should have received a copy of the GNU General Public License +// along with Parity. If not, see . + +export default from './trace'; diff --git a/js/src/api/rpc/trace/trace.e2e.js b/js/src/api/rpc/trace/trace.e2e.js new file mode 100644 index 000000000..1a0720927 --- /dev/null +++ b/js/src/api/rpc/trace/trace.e2e.js @@ -0,0 +1,35 @@ +// Copyright 2015, 2016 Ethcore (UK) Ltd. +// This file is part of Parity. + +// Parity is free software: you can redistribute it and/or modify +// it under the terms of the GNU General Public License as published by +// the Free Software Foundation, either version 3 of the License, or +// (at your option) any later version. + +// Parity is distributed in the hope that it will be useful, +// but WITHOUT ANY WARRANTY; without even the implied warranty of +// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the +// GNU General Public License for more details. + +// You should have received a copy of the GNU General Public License +// along with Parity. If not, see . + +import { createHttpApi } from '../../../../test/e2e/ethapi'; + +describe('ethapi.trace', () => { + const ethapi = createHttpApi(); + + describe('block', () => { + it('returns the latest block', () => { + return ethapi.trace.block().then((block) => { + expect(block).to.be.ok; + }); + }); + + it('returns a specified block', () => { + return ethapi.trace.block('0x65432').then((block) => { + expect(block).to.be.ok; + }); + }); + }); +}); diff --git a/js/src/api/rpc/trace/trace.js b/js/src/api/rpc/trace/trace.js new file mode 100644 index 000000000..4cf8fc8e5 --- /dev/null +++ b/js/src/api/rpc/trace/trace.js @@ -0,0 +1,43 @@ +// Copyright 2015, 2016 Ethcore (UK) Ltd. +// This file is part of Parity. + +// Parity is free software: you can redistribute it and/or modify +// it under the terms of the GNU General Public License as published by +// the Free Software Foundation, either version 3 of the License, or +// (at your option) any later version. + +// Parity is distributed in the hope that it will be useful, +// but WITHOUT ANY WARRANTY; without even the implied warranty of +// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the +// GNU General Public License for more details. + +// You should have received a copy of the GNU General Public License +// along with Parity. If not, see . + +import { inBlockNumber, inHex, inNumber16 } from '../../format/input'; + +export default class Trace { + constructor (transport) { + this._transport = transport; + } + + filter (filterObj) { + return this._transport + .execute('trace_filter', filterObj); + } + + get (txHash, position) { + return this._transport + .execute('trace_get', inHex(txHash), inNumber16(position)); + } + + transaction (txHash) { + return this._transport + .execute('trace_transaction', inHex(txHash)); + } + + block (blockNumber = 'latest') { + return this._transport + .execute('trace_block', inBlockNumber(blockNumber)); + } +} diff --git a/js/src/api/rpc/trace/trace.spec.js b/js/src/api/rpc/trace/trace.spec.js new file mode 100644 index 000000000..4a38f7a3f --- /dev/null +++ b/js/src/api/rpc/trace/trace.spec.js @@ -0,0 +1,44 @@ +// Copyright 2015, 2016 Ethcore (UK) Ltd. +// This file is part of Parity. + +// Parity is free software: you can redistribute it and/or modify +// it under the terms of the GNU General Public License as published by +// the Free Software Foundation, either version 3 of the License, or +// (at your option) any later version. + +// Parity is distributed in the hope that it will be useful, +// but WITHOUT ANY WARRANTY; without even the implied warranty of +// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the +// GNU General Public License for more details. + +// You should have received a copy of the GNU General Public License +// along with Parity. If not, see . + +import { TEST_HTTP_URL, mockHttp } from '../../../../test/mockRpc'; + +import Http from '../../transport/http'; +import Trace from './trace'; + +const instance = new Trace(new Http(TEST_HTTP_URL)); + +describe('api/rpc/Trace', () => { + let scope; + + describe('block', () => { + beforeEach(() => { + scope = mockHttp([{ method: 'trace_block', reply: { result: [] } }]); + }); + + it('assumes latest blockNumber when not specified', () => { + return instance.block().then(() => { + expect(scope.body.trace_block.params).to.deep.equal(['latest']); + }); + }); + + it('passed specified blockNumber', () => { + return instance.block(0x123).then(() => { + expect(scope.body.trace_block.params).to.deep.equal(['0x123']); + }); + }); + }); +}); diff --git a/js/src/api/rpc/web3/index.js b/js/src/api/rpc/web3/index.js new file mode 100644 index 000000000..d0bb22941 --- /dev/null +++ b/js/src/api/rpc/web3/index.js @@ -0,0 +1,17 @@ +// Copyright 2015, 2016 Ethcore (UK) Ltd. +// This file is part of Parity. + +// Parity is free software: you can redistribute it and/or modify +// it under the terms of the GNU General Public License as published by +// the Free Software Foundation, either version 3 of the License, or +// (at your option) any later version. + +// Parity is distributed in the hope that it will be useful, +// but WITHOUT ANY WARRANTY; without even the implied warranty of +// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the +// GNU General Public License for more details. + +// You should have received a copy of the GNU General Public License +// along with Parity. If not, see . + +export default from './web3'; diff --git a/js/src/api/rpc/web3/web3.e2e.js b/js/src/api/rpc/web3/web3.e2e.js new file mode 100644 index 000000000..15cc1934f --- /dev/null +++ b/js/src/api/rpc/web3/web3.e2e.js @@ -0,0 +1,44 @@ +// Copyright 2015, 2016 Ethcore (UK) Ltd. +// This file is part of Parity. + +// Parity is free software: you can redistribute it and/or modify +// it under the terms of the GNU General Public License as published by +// the Free Software Foundation, either version 3 of the License, or +// (at your option) any later version. + +// Parity is distributed in the hope that it will be useful, +// but WITHOUT ANY WARRANTY; without even the implied warranty of +// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the +// GNU General Public License for more details. + +// You should have received a copy of the GNU General Public License +// along with Parity. If not, see . + +import { createHttpApi } from '../../../../test/e2e/ethapi'; +import { isHexNumber } from '../../../../test/types'; + +describe('ethapi.web3', () => { + const ethapi = createHttpApi(); + + describe('clientVersion', () => { + it('returns the client version', () => { + return ethapi.web3.clientVersion().then((version) => { + const [client] = version.split('/'); + + expect(client === 'Parity' || client === 'Geth').to.be.ok; + }); + }); + }); + + describe('sha3', () => { + it('returns a keccak256 sha', () => { + const sha = '0xa7916fac4f538170f7cd12c148552e2cba9fcd72329a2dd5b07a6fa906488ddf'; + const hexStr = 'baz()'.split('').map((char) => char.charCodeAt(0).toString(16)).join(''); + + return ethapi.web3.sha3(`0x${hexStr}`).then((hash) => { + expect(isHexNumber(hash)).to.be.true; + expect(hash).to.equal(sha); + }); + }); + }); +}); diff --git a/js/src/api/rpc/web3/web3.js b/js/src/api/rpc/web3/web3.js new file mode 100644 index 000000000..eb52a8bb7 --- /dev/null +++ b/js/src/api/rpc/web3/web3.js @@ -0,0 +1,33 @@ +// Copyright 2015, 2016 Ethcore (UK) Ltd. +// This file is part of Parity. + +// Parity is free software: you can redistribute it and/or modify +// it under the terms of the GNU General Public License as published by +// the Free Software Foundation, either version 3 of the License, or +// (at your option) any later version. + +// Parity is distributed in the hope that it will be useful, +// but WITHOUT ANY WARRANTY; without even the implied warranty of +// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the +// GNU General Public License for more details. + +// You should have received a copy of the GNU General Public License +// along with Parity. If not, see . + +import { inHex } from '../../format/input'; + +export default class Web3 { + constructor (transport) { + this._transport = transport; + } + + clientVersion () { + return this._transport + .execute('web3_clientVersion'); + } + + sha3 (hexStr) { + return this._transport + .execute('web3_sha3', inHex(hexStr)); + } +} diff --git a/js/src/api/rpc/web3/web3.spec.js b/js/src/api/rpc/web3/web3.spec.js new file mode 100644 index 000000000..eb4a59cd1 --- /dev/null +++ b/js/src/api/rpc/web3/web3.spec.js @@ -0,0 +1,38 @@ +// Copyright 2015, 2016 Ethcore (UK) Ltd. +// This file is part of Parity. + +// Parity is free software: you can redistribute it and/or modify +// it under the terms of the GNU General Public License as published by +// the Free Software Foundation, either version 3 of the License, or +// (at your option) any later version. + +// Parity is distributed in the hope that it will be useful, +// but WITHOUT ANY WARRANTY; without even the implied warranty of +// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the +// GNU General Public License for more details. + +// You should have received a copy of the GNU General Public License +// along with Parity. If not, see . + +import { TEST_HTTP_URL, mockHttp } from '../../../../test/mockRpc'; + +import Http from '../../transport/http'; +import Web3 from './web3'; + +const instance = new Web3(new Http(TEST_HTTP_URL)); + +describe('api/rpc/Web3', () => { + let scope; + + describe('sha3', () => { + beforeEach(() => { + scope = mockHttp([{ method: 'web3_sha3', reply: { result: [] } }]); + }); + + it('formats the inputs correctly', () => { + return instance.sha3('1234').then(() => { + expect(scope.body.web3_sha3.params).to.deep.equal(['0x1234']); + }); + }); + }); +}); diff --git a/js/src/api/subscriptions/eth.js b/js/src/api/subscriptions/eth.js new file mode 100644 index 000000000..28d9cc6ff --- /dev/null +++ b/js/src/api/subscriptions/eth.js @@ -0,0 +1,62 @@ +// Copyright 2015, 2016 Ethcore (UK) Ltd. +// This file is part of Parity. + +// Parity is free software: you can redistribute it and/or modify +// it under the terms of the GNU General Public License as published by +// the Free Software Foundation, either version 3 of the License, or +// (at your option) any later version. + +// Parity is distributed in the hope that it will be useful, +// but WITHOUT ANY WARRANTY; without even the implied warranty of +// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the +// GNU General Public License for more details. + +// You should have received a copy of the GNU General Public License +// along with Parity. If not, see . + +import BigNumber from 'bignumber.js'; + +export default class Eth { + constructor (updateSubscriptions, api) { + this._api = api; + this._updateSubscriptions = updateSubscriptions; + this._started = false; + + this._lastBlock = new BigNumber(-1); + } + + get isStarted () { + return this._started; + } + + start () { + this._started = true; + + return this._blockNumber(); + } + + _blockNumber = () => { + const nextTimeout = (timeout = 1000) => { + setTimeout(() => { + this._blockNumber(); + }, timeout); + }; + + if (!this._api.transport.isConnected) { + nextTimeout(500); + return; + } + + return this._api.eth + .blockNumber() + .then((blockNumber) => { + if (!blockNumber.eq(this._lastBlock)) { + this._lastBlock = blockNumber; + this._updateSubscriptions('eth_blockNumber', null, blockNumber); + } + + nextTimeout(); + }) + .catch(nextTimeout); + } +} diff --git a/js/src/api/subscriptions/eth.spec.js b/js/src/api/subscriptions/eth.spec.js new file mode 100644 index 000000000..24398483e --- /dev/null +++ b/js/src/api/subscriptions/eth.spec.js @@ -0,0 +1,101 @@ +// Copyright 2015, 2016 Ethcore (UK) Ltd. +// This file is part of Parity. + +// Parity is free software: you can redistribute it and/or modify +// it under the terms of the GNU General Public License as published by +// the Free Software Foundation, either version 3 of the License, or +// (at your option) any later version. + +// Parity is distributed in the hope that it will be useful, +// but WITHOUT ANY WARRANTY; without even the implied warranty of +// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the +// GNU General Public License for more details. + +// You should have received a copy of the GNU General Public License +// along with Parity. If not, see . + +import BigNumber from 'bignumber.js'; +import sinon from 'sinon'; +import 'sinon-as-promised'; + +import Eth from './eth'; + +const START_BLOCK = 5000; + +function stubApi (blockNumber) { + const _calls = { + blockNumber: [] + }; + + return { + _calls, + transport: { + isConnected: true + }, + eth: { + blockNumber: () => { + const stub = sinon.stub().resolves(new BigNumber(blockNumber || START_BLOCK))(); + _calls.blockNumber.push(stub); + return stub; + } + } + }; +} + +describe('api/subscriptions/eth', () => { + let api; + let eth; + let cb; + + beforeEach(() => { + api = stubApi(); + cb = sinon.stub(); + eth = new Eth(cb, api); + }); + + describe('constructor', () => { + it('starts the instance in a stopped state', () => { + expect(eth.isStarted).to.be.false; + }); + }); + + describe('start', () => { + describe('blockNumber available', () => { + beforeEach(() => { + return eth.start(); + }); + + it('sets the started status', () => { + expect(eth.isStarted).to.be.true; + }); + + it('calls eth_blockNumber', () => { + expect(api._calls.blockNumber.length).to.be.ok; + }); + + it('updates subscribers', () => { + expect(cb).to.have.been.calledWith('eth_blockNumber', null, new BigNumber(START_BLOCK)); + }); + }); + + describe('blockNumber not available', () => { + beforeEach(() => { + api = stubApi(-1); + eth = new Eth(cb, api); + return eth.start(); + }); + + it('sets the started status', () => { + expect(eth.isStarted).to.be.true; + }); + + it('calls eth_blockNumber', () => { + expect(api._calls.blockNumber.length).to.be.ok; + }); + + it('does not update subscribers', () => { + expect(cb).not.to.been.called; + }); + }); + }); +}); diff --git a/js/src/api/subscriptions/index.js b/js/src/api/subscriptions/index.js new file mode 100644 index 000000000..3e627e62a --- /dev/null +++ b/js/src/api/subscriptions/index.js @@ -0,0 +1,19 @@ +// Copyright 2015, 2016 Ethcore (UK) Ltd. +// This file is part of Parity. + +// Parity is free software: you can redistribute it and/or modify +// it under the terms of the GNU General Public License as published by +// the Free Software Foundation, either version 3 of the License, or +// (at your option) any later version. + +// Parity is distributed in the hope that it will be useful, +// but WITHOUT ANY WARRANTY; without even the implied warranty of +// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the +// GNU General Public License for more details. + +// You should have received a copy of the GNU General Public License +// along with Parity. If not, see . + +export Logging from './logging'; + +export default from './manager'; diff --git a/js/src/api/subscriptions/logging.js b/js/src/api/subscriptions/logging.js new file mode 100644 index 000000000..b03558207 --- /dev/null +++ b/js/src/api/subscriptions/logging.js @@ -0,0 +1,44 @@ +// Copyright 2015, 2016 Ethcore (UK) Ltd. +// This file is part of Parity. + +// Parity is free software: you can redistribute it and/or modify +// it under the terms of the GNU General Public License as published by +// the Free Software Foundation, either version 3 of the License, or +// (at your option) any later version. + +// Parity is distributed in the hope that it will be useful, +// but WITHOUT ANY WARRANTY; without even the implied warranty of +// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the +// GNU General Public License for more details. + +// You should have received a copy of the GNU General Public License +// along with Parity. If not, see . + +let instance = null; + +export default class Logging { + constructor (updateSubscriptions) { + this._updateSubscriptions = updateSubscriptions; + + instance = this; + } + + get isStarted () { + return true; + } + + start () { + } + + static send (method, params, json) { + if (!instance) { + return; + } + + return instance._updateSubscriptions('logging', null, { + method, + params, + json + }); + } +} diff --git a/js/src/api/subscriptions/logging.spec.js b/js/src/api/subscriptions/logging.spec.js new file mode 100644 index 000000000..2a1cabb46 --- /dev/null +++ b/js/src/api/subscriptions/logging.spec.js @@ -0,0 +1,49 @@ +// Copyright 2015, 2016 Ethcore (UK) Ltd. +// This file is part of Parity. + +// Parity is free software: you can redistribute it and/or modify +// it under the terms of the GNU General Public License as published by +// the Free Software Foundation, either version 3 of the License, or +// (at your option) any later version. + +// Parity is distributed in the hope that it will be useful, +// but WITHOUT ANY WARRANTY; without even the implied warranty of +// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the +// GNU General Public License for more details. + +// You should have received a copy of the GNU General Public License +// along with Parity. If not, see . + +import sinon from 'sinon'; + +import Logging from './logging'; + +describe('api/subscriptions/logging', () => { + let cb; + let logging; + + beforeEach(() => { + cb = sinon.stub(); + logging = new Logging(cb); + }); + + describe('constructor', () => { + it('starts the instance in a started state', () => { + expect(logging.isStarted).to.be.true; + }); + }); + + describe('send', () => { + const method = 'method'; + const params = 'params'; + const json = 'json'; + + beforeEach(() => { + Logging.send(method, params, json); + }); + + it('calls the subscription update', () => { + expect(cb).to.have.been.calledWith('logging', null, { method, params, json }); + }); + }); +}); diff --git a/js/src/api/subscriptions/manager.js b/js/src/api/subscriptions/manager.js new file mode 100644 index 000000000..6f9b7cf7e --- /dev/null +++ b/js/src/api/subscriptions/manager.js @@ -0,0 +1,134 @@ +// Copyright 2015, 2016 Ethcore (UK) Ltd. +// This file is part of Parity. + +// Parity is free software: you can redistribute it and/or modify +// it under the terms of the GNU General Public License as published by +// the Free Software Foundation, either version 3 of the License, or +// (at your option) any later version. + +// Parity is distributed in the hope that it will be useful, +// but WITHOUT ANY WARRANTY; without even the implied warranty of +// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the +// GNU General Public License for more details. + +// You should have received a copy of the GNU General Public License +// along with Parity. If not, see . + +import { isError } from '../util/types'; + +import Eth from './eth'; +import Logging from './logging'; +import Personal from './personal'; +import Signer from './signer'; + +const events = { + 'logging': { module: 'logging' }, + 'eth_blockNumber': { module: 'eth' }, + 'personal_accountsInfo': { module: 'personal' }, + 'personal_listAccounts': { module: 'personal' }, + 'personal_requestsToConfirm': { module: 'signer' } +}; + +let nextSubscriptionId = 0; + +export default class Manager { + constructor (api) { + this._api = api; + + this.subscriptions = {}; + this.values = {}; + + Object.keys(events).forEach((subscriptionName) => { + this.subscriptions[subscriptionName] = {}; + this.values[subscriptionName] = { + error: null, + data: null + }; + }); + + this._logging = new Logging(this._updateSubscriptions); + this._eth = new Eth(this._updateSubscriptions, api); + this._personal = new Personal(this._updateSubscriptions, api, this); + this._signer = new Signer(this._updateSubscriptions, api, this); + } + + _validateType (subscriptionName) { + const subscription = events[subscriptionName]; + + if (!subscription) { + return new Error(`${subscriptionName} is not a valid interface, subscribe using one of ${Object.keys(events).join(', ')}`); + } + + return subscription; + } + + subscribe (subscriptionName, callback) { + return new Promise((resolve, reject) => { + const subscription = this._validateType(subscriptionName); + + if (isError(subscription)) { + reject(subscription); + return; + } + + const subscriptionId = nextSubscriptionId++; + const { error, data } = this.values[subscriptionName]; + const engine = this[`_${subscription.module}`]; + + this.subscriptions[subscriptionName][subscriptionId] = callback; + + if (!engine.isStarted) { + engine.start(); + } else { + this._sendData(subscriptionName, subscriptionId, callback, error, data); + } + + resolve(subscriptionId); + }); + } + + unsubscribe (subscriptionName, subscriptionId) { + return new Promise((resolve, reject) => { + const subscription = this._validateType(subscriptionName); + + if (isError(subscription)) { + reject(subscription); + return; + } + + if (!this.subscriptions[subscriptionName][subscriptionId]) { + reject(new Error(`Cannot find subscription ${subscriptionId} for type ${subscriptionName}`)); + return; + } + + delete this.subscriptions[subscriptionName][subscriptionId]; + resolve(); + }); + } + + _sendData (subscriptionName, subscriptionId, callback, error, data) { + try { + callback(error, data); + } catch (error) { + console.error(`Unable to update callback for ${subscriptionName}, subscriptionId ${subscriptionId}`, error); + this.unsubscribe(subscriptionName, subscriptionId); + } + } + + _updateSubscriptions = (subscriptionName, error, data) => { + if (!this.subscriptions[subscriptionName]) { + throw new Error(`Cannot find entry point for subscriptions of type ${subscriptionName}`); + } + + this.values[subscriptionName] = { error, data }; + Object.keys(this.subscriptions[subscriptionName]).forEach((subscriptionId) => { + const callback = this.subscriptions[subscriptionName][subscriptionId]; + + this._sendData(subscriptionName, subscriptionId, callback, error, data); + }); + } +} + +export { + events +}; diff --git a/js/src/api/subscriptions/manager.spec.js b/js/src/api/subscriptions/manager.spec.js new file mode 100644 index 000000000..4b6b84b84 --- /dev/null +++ b/js/src/api/subscriptions/manager.spec.js @@ -0,0 +1,98 @@ +// Copyright 2015, 2016 Ethcore (UK) Ltd. +// This file is part of Parity. + +// Parity is free software: you can redistribute it and/or modify +// it under the terms of the GNU General Public License as published by +// the Free Software Foundation, either version 3 of the License, or +// (at your option) any later version. + +// Parity is distributed in the hope that it will be useful, +// but WITHOUT ANY WARRANTY; without even the implied warranty of +// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the +// GNU General Public License for more details. + +// You should have received a copy of the GNU General Public License +// along with Parity. If not, see . + +import sinon from 'sinon'; + +import Manager, { events } from './manager'; + +function newStub () { + const start = () => manager._updateSubscriptions(manager.__test, null, 'test'); + const manager = new Manager({ + transport: { + isConnected: true + } + }); + + manager._eth = { + isStarted: false, + start + }; + + manager._personal = { + isStarted: false, + start + }; + + manager._signer = { + isStarted: false, + start + }; + + return manager; +} + +describe('api/subscriptions/manager', () => { + let manager; + + beforeEach(() => { + manager = newStub(); + }); + + describe('constructor', () => { + it('sets up the subscription types & defaults', () => { + expect(Object.keys(manager.subscriptions)).to.deep.equal(Object.keys(events)); + expect(Object.keys(manager.values)).to.deep.equal(Object.keys(events)); + }); + }); + + describe('subscriptions', () => { + Object + .keys(events) + .filter((eventName) => eventName.indexOf('_') !== -1) + .forEach((eventName) => { + const { module } = events[eventName]; + let engine; + let cb; + let subscriptionId; + + describe(eventName, () => { + beforeEach(() => { + engine = manager[`_${module}`]; + manager.__test = eventName; + cb = sinon.stub(); + sinon.spy(engine, 'start'); + return manager + .subscribe(eventName, cb) + .then((_subscriptionId) => { + subscriptionId = _subscriptionId; + }); + }); + + it(`puts the ${module} engine in a started state`, () => { + expect(engine.start).to.have.been.called; + }); + + it('returns a subscriptionId', () => { + expect(subscriptionId).to.be.ok; + }); + + it('calls the subscription callback with updated values', () => { + expect(cb).to.have.been.calledWith(null, 'test'); + }); + }); + }); + }); +}); diff --git a/js/src/api/subscriptions/personal.js b/js/src/api/subscriptions/personal.js new file mode 100644 index 000000000..d65419962 --- /dev/null +++ b/js/src/api/subscriptions/personal.js @@ -0,0 +1,77 @@ +// Copyright 2015, 2016 Ethcore (UK) Ltd. +// This file is part of Parity. + +// Parity is free software: you can redistribute it and/or modify +// it under the terms of the GNU General Public License as published by +// the Free Software Foundation, either version 3 of the License, or +// (at your option) any later version. + +// Parity is distributed in the hope that it will be useful, +// but WITHOUT ANY WARRANTY; without even the implied warranty of +// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the +// GNU General Public License for more details. + +// You should have received a copy of the GNU General Public License +// along with Parity. If not, see . + +export default class Personal { + constructor (updateSubscriptions, api, subscriber) { + this._subscriber = subscriber; + this._api = api; + this._updateSubscriptions = updateSubscriptions; + this._started = false; + } + + get isStarted () { + return this._started; + } + + start () { + this._started = true; + + return Promise.all([ + this._listAccounts(), + this._accountsInfo(), + this._loggingSubscribe() + ]); + } + + _listAccounts = () => { + return this._api.personal + .listAccounts() + .then((accounts) => { + this._updateSubscriptions('personal_listAccounts', null, accounts); + }); + } + + _accountsInfo = () => { + return this._api.personal + .accountsInfo() + .then((info) => { + this._updateSubscriptions('personal_accountsInfo', null, info); + }); + } + + _loggingSubscribe () { + return this._subscriber.subscribe('logging', (error, data) => { + if (error || !data) { + return; + } + + switch (data.method) { + case 'personal_importGethAccounts': + case 'personal_newAccount': + case 'personal_newAccountFromPhrase': + case 'personal_newAccountFromWallet': + this._listAccounts(); + this._accountsInfo(); + return; + + case 'personal_setAccountName': + case 'personal_setAccountMeta': + this._accountsInfo(); + return; + } + }); + } +} diff --git a/js/src/api/subscriptions/personal.spec.js b/js/src/api/subscriptions/personal.spec.js new file mode 100644 index 000000000..1a77b5f61 --- /dev/null +++ b/js/src/api/subscriptions/personal.spec.js @@ -0,0 +1,122 @@ +// Copyright 2015, 2016 Ethcore (UK) Ltd. +// This file is part of Parity. + +// Parity is free software: you can redistribute it and/or modify +// it under the terms of the GNU General Public License as published by +// the Free Software Foundation, either version 3 of the License, or +// (at your option) any later version. + +// Parity is distributed in the hope that it will be useful, +// but WITHOUT ANY WARRANTY; without even the implied warranty of +// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the +// GNU General Public License for more details. + +// You should have received a copy of the GNU General Public License +// along with Parity. If not, see . + +import sinon from 'sinon'; +import 'sinon-as-promised'; + +import Personal from './personal'; + +const TEST_INFO = { + '0xfa64203C044691aA57251aF95f4b48d85eC00Dd5': { + name: 'test' + } +}; +const TEST_LIST = ['0xfa64203C044691aA57251aF95f4b48d85eC00Dd5']; + +function stubApi (accounts, info) { + const _calls = { + accountsInfo: [], + listAccounts: [] + }; + + return { + _calls, + personal: { + accountsInfo: () => { + const stub = sinon.stub().resolves(info || TEST_INFO)(); + _calls.accountsInfo.push(stub); + return stub; + }, + + listAccounts: () => { + const stub = sinon.stub().resolves(accounts || TEST_LIST)(); + _calls.listAccounts.push(stub); + return stub; + } + } + }; +} + +function stubLogging () { + return { + subscribe: sinon.stub() + }; +} + +describe('api/subscriptions/personal', () => { + let api; + let cb; + let logging; + let personal; + + beforeEach(() => { + api = stubApi(); + cb = sinon.stub(); + logging = stubLogging(); + personal = new Personal(cb, api, logging); + }); + + describe('constructor', () => { + it('starts the instance in a stopped state', () => { + expect(personal.isStarted).to.be.false; + }); + }); + + describe('start', () => { + describe('info available', () => { + beforeEach(() => { + return personal.start(); + }); + + it('sets the started status', () => { + expect(personal.isStarted).to.be.true; + }); + + it('calls personal_accountsInfo', () => { + expect(api._calls.accountsInfo.length).to.be.ok; + }); + + it('calls personal_listAccounts', () => { + expect(api._calls.listAccounts.length).to.be.ok; + }); + + it('updates subscribers', () => { + expect(cb.firstCall).to.have.been.calledWith('personal_listAccounts', null, TEST_LIST); + expect(cb.secondCall).to.have.been.calledWith('personal_accountsInfo', null, TEST_INFO); + }); + }); + + describe('info not available', () => { + beforeEach(() => { + api = stubApi([], {}); + personal = new Personal(cb, api, logging); + return personal.start(); + }); + + it('sets the started status', () => { + expect(personal.isStarted).to.be.true; + }); + + it('calls personal_accountsInfo', () => { + expect(api._calls.accountsInfo.length).to.be.ok; + }); + + it('calls personal_listAccounts', () => { + expect(api._calls.listAccounts.length).to.be.ok; + }); + }); + }); +}); diff --git a/js/src/api/subscriptions/signer.js b/js/src/api/subscriptions/signer.js new file mode 100644 index 000000000..af745261b --- /dev/null +++ b/js/src/api/subscriptions/signer.js @@ -0,0 +1,76 @@ +// Copyright 2015, 2016 Ethcore (UK) Ltd. +// This file is part of Parity. + +// Parity is free software: you can redistribute it and/or modify +// it under the terms of the GNU General Public License as published by +// the Free Software Foundation, either version 3 of the License, or +// (at your option) any later version. + +// Parity is distributed in the hope that it will be useful, +// but WITHOUT ANY WARRANTY; without even the implied warranty of +// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the +// GNU General Public License for more details. + +// You should have received a copy of the GNU General Public License +// along with Parity. If not, see . + +export default class Signer { + constructor (updateSubscriptions, api, subscriber) { + this._subscriber = subscriber; + this._api = api; + this._updateSubscriptions = updateSubscriptions; + this._started = false; + } + + get isStarted () { + return this._started; + } + + start () { + this._started = true; + + return Promise.all([ + this._listRequests(true), + this._loggingSubscribe() + ]); + } + + _listRequests = (doTimeout) => { + const nextTimeout = (timeout = 1000) => { + if (doTimeout) { + setTimeout(() => { + this._listRequests(true); + }, timeout); + } + }; + + if (!this._api.transport.isConnected) { + nextTimeout(500); + return; + } + + return this._api.personal + .requestsToConfirm() + .then((requests) => { + this._updateSubscriptions('personal_requestsToConfirm', null, requests); + nextTimeout(); + }) + .catch(nextTimeout); + } + + _loggingSubscribe () { + return this._subscriber.subscribe('logging', (error, data) => { + if (error || !data) { + return; + } + + switch (data.method) { + case 'eth_postTransaction': + case 'eth_sendTranasction': + case 'eth_sendRawTransaction': + this._listRequests(false); + return; + } + }); + } +} diff --git a/js/src/api/transport/http/http.e2e.js b/js/src/api/transport/http/http.e2e.js new file mode 100644 index 000000000..8f0aa48ad --- /dev/null +++ b/js/src/api/transport/http/http.e2e.js @@ -0,0 +1,29 @@ +// Copyright 2015, 2016 Ethcore (UK) Ltd. +// This file is part of Parity. + +// Parity is free software: you can redistribute it and/or modify +// it under the terms of the GNU General Public License as published by +// the Free Software Foundation, either version 3 of the License, or +// (at your option) any later version. + +// Parity is distributed in the hope that it will be useful, +// but WITHOUT ANY WARRANTY; without even the implied warranty of +// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the +// GNU General Public License for more details. + +// You should have received a copy of the GNU General Public License +// along with Parity. If not, see . + +import Http from './http'; + +const http = new Http('http://localhost:8545'); + +describe('transport/Http', () => { + it('connects and makes a call to web3_clientVersion', () => { + return http.execute('web3_clientVersion').then((version) => { + const [client] = version.split('/'); + + expect(client === 'Geth' || client === 'Parity').to.be.ok; + }); + }); +}); diff --git a/js/src/api/transport/http/http.js b/js/src/api/transport/http/http.js new file mode 100644 index 000000000..65ba089cc --- /dev/null +++ b/js/src/api/transport/http/http.js @@ -0,0 +1,76 @@ +// Copyright 2015, 2016 Ethcore (UK) Ltd. +// This file is part of Parity. + +// Parity is free software: you can redistribute it and/or modify +// it under the terms of the GNU General Public License as published by +// the Free Software Foundation, either version 3 of the License, or +// (at your option) any later version. + +// Parity is distributed in the hope that it will be useful, +// but WITHOUT ANY WARRANTY; without even the implied warranty of +// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the +// GNU General Public License for more details. + +// You should have received a copy of the GNU General Public License +// along with Parity. If not, see . + +import { Logging } from '../../subscriptions'; +import JsonRpcBase from '../jsonRpcBase'; + +/* global fetch */ +export default class Http extends JsonRpcBase { + constructor (url) { + super(); + + this._connected = true; + this._url = url; + } + + _encodeOptions (method, params) { + const json = this.encode(method, params); + + this.log(json); + + return { + method: 'POST', + headers: { + 'Accept': 'application/json', + 'Content-Type': 'application/json', + 'Content-Length': json.length + }, + body: json + }; + } + + execute (method, ...params) { + const request = this._encodeOptions(method, params); + + return fetch(this._url, request) + .catch((error) => { + this._connected = false; + throw error; + }) + .then((response) => { + this._connected = true; + + if (response.status !== 200) { + this._connected = false; + this.error(JSON.stringify({ status: response.status, statusText: response.statusText })); + throw new Error(`${response.status}: ${response.statusText}`); + } + + return response.json(); + }) + .then((response) => { + Logging.send(method, params, { request, response }); + + if (response.error) { + this.error(JSON.stringify(response)); + throw new Error(`${response.error.code}: ${response.error.message}`); + } + + this.log(JSON.stringify(response)); + return response.result; + }); + } +} diff --git a/js/src/api/transport/http/http.spec.js b/js/src/api/transport/http/http.spec.js new file mode 100644 index 000000000..94441bc51 --- /dev/null +++ b/js/src/api/transport/http/http.spec.js @@ -0,0 +1,122 @@ +// Copyright 2015, 2016 Ethcore (UK) Ltd. +// This file is part of Parity. + +// Parity is free software: you can redistribute it and/or modify +// it under the terms of the GNU General Public License as published by +// the Free Software Foundation, either version 3 of the License, or +// (at your option) any later version. + +// Parity is distributed in the hope that it will be useful, +// but WITHOUT ANY WARRANTY; without even the implied warranty of +// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the +// GNU General Public License for more details. + +// You should have received a copy of the GNU General Public License +// along with Parity. If not, see . + +import { TEST_HTTP_URL, mockHttp } from '../../../../test/mockRpc'; +import Http from './http'; + +const transport = new Http(TEST_HTTP_URL); + +describe('api/transport/Http', () => { + describe('instance', () => { + it('encodes the options correctly', () => { + const opt = transport._encodeOptions('someMethod', ['param']); + const enc = { + method: 'POST', + headers: { + 'Accept': 'application/json', + 'Content-Type': 'application/json', + 'Content-Length': 65 + }, + body: `{"jsonrpc":"2.0","method":"someMethod","params":["param"],"id":${transport._id - 1}}` + }; + + expect(opt).to.deep.equal(enc); + }); + }); + + describe('transport', () => { + const RESULT = ['this is some result']; + + let scope; + let result; + + beforeEach(() => { + scope = mockHttp([{ method: 'eth_call', reply: { result: RESULT } }]); + + return transport + .execute('eth_call', 1, 2, 3, 'test') + .then((_result) => { + result = _result; + }); + }); + + it('makes POST', () => { + expect(scope.isDone()).to.be.true; + }); + + it('sets jsonrpc', () => { + expect(scope.body.eth_call.jsonrpc).to.equal('2.0'); + }); + + it('sets the method', () => { + expect(scope.body.eth_call.method).to.equal('eth_call'); + }); + + it('passes the params', () => { + expect(scope.body.eth_call.params).to.deep.equal([1, 2, 3, 'test']); + }); + + it('increments the id', () => { + expect(scope.body.eth_call.id).not.to.equal(0); + }); + + it('passes the actual result back', () => { + expect(result).to.deep.equal(RESULT); + }); + }); + + describe('HTTP errors', () => { + let scope; + let error; + + beforeEach(() => { + scope = mockHttp([{ method: 'eth_call', reply: {}, code: 500 }]); + + return transport + .execute('eth_call') + .catch((_error) => { + error = _error; + }); + }); + + it('returns HTTP errors as throws', () => { + expect(scope.isDone()).to.be.true; + expect(error.message).to.match(/Internal Server Error/); + }); + }); + + describe('RPC errors', () => { + const ERROR = { code: -1, message: 'ERROR: RPC failure' }; + + let scope; + let error; + + beforeEach(() => { + scope = mockHttp([{ method: 'eth_call', reply: { error: ERROR } }]); + + return transport + .execute('eth_call') + .catch((_error) => { + error = _error; + }); + }); + + it('returns RPC errors as throws', () => { + expect(scope.isDone()).to.be.true; + expect(error.message).to.match(/RPC failure/); + }); + }); +}); diff --git a/js/src/api/transport/http/index.js b/js/src/api/transport/http/index.js new file mode 100644 index 000000000..5ce7a652e --- /dev/null +++ b/js/src/api/transport/http/index.js @@ -0,0 +1,17 @@ +// Copyright 2015, 2016 Ethcore (UK) Ltd. +// This file is part of Parity. + +// Parity is free software: you can redistribute it and/or modify +// it under the terms of the GNU General Public License as published by +// the Free Software Foundation, either version 3 of the License, or +// (at your option) any later version. + +// Parity is distributed in the hope that it will be useful, +// but WITHOUT ANY WARRANTY; without even the implied warranty of +// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the +// GNU General Public License for more details. + +// You should have received a copy of the GNU General Public License +// along with Parity. If not, see . + +export default from './http'; diff --git a/js/src/api/transport/index.js b/js/src/api/transport/index.js new file mode 100644 index 000000000..8f67fba4d --- /dev/null +++ b/js/src/api/transport/index.js @@ -0,0 +1,18 @@ +// Copyright 2015, 2016 Ethcore (UK) Ltd. +// This file is part of Parity. + +// Parity is free software: you can redistribute it and/or modify +// it under the terms of the GNU General Public License as published by +// the Free Software Foundation, either version 3 of the License, or +// (at your option) any later version. + +// Parity is distributed in the hope that it will be useful, +// but WITHOUT ANY WARRANTY; without even the implied warranty of +// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the +// GNU General Public License for more details. + +// You should have received a copy of the GNU General Public License +// along with Parity. If not, see . + +export Http from './http'; +export Ws from './ws'; diff --git a/js/src/api/transport/jsonRpcBase.js b/js/src/api/transport/jsonRpcBase.js new file mode 100644 index 000000000..dc2f9bc8e --- /dev/null +++ b/js/src/api/transport/jsonRpcBase.js @@ -0,0 +1,62 @@ +// Copyright 2015, 2016 Ethcore (UK) Ltd. +// This file is part of Parity. + +// Parity is free software: you can redistribute it and/or modify +// it under the terms of the GNU General Public License as published by +// the Free Software Foundation, either version 3 of the License, or +// (at your option) any later version. + +// Parity is distributed in the hope that it will be useful, +// but WITHOUT ANY WARRANTY; without even the implied warranty of +// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the +// GNU General Public License for more details. + +// You should have received a copy of the GNU General Public License +// along with Parity. If not, see . + +export default class JsonRpcBase { + constructor () { + this._id = 1; + this._debug = false; + this._connected = false; + } + + encode (method, params) { + const json = JSON.stringify({ + jsonrpc: '2.0', + method: method, + params: params, + id: this._id++ + }); + + return json; + } + + get id () { + return this._id; + } + + get isDebug () { + return this._debug; + } + + get isConnected () { + return this._connected; + } + + setDebug (flag) { + this._debug = flag; + } + + error (error) { + if (this.isDebug) { + console.error(error); + } + } + + log (log) { + if (this.isDebug) { + console.log(log); + } + } +} diff --git a/js/src/api/transport/jsonRpcBase.spec.js b/js/src/api/transport/jsonRpcBase.spec.js new file mode 100644 index 000000000..78fc6549c --- /dev/null +++ b/js/src/api/transport/jsonRpcBase.spec.js @@ -0,0 +1,88 @@ +// Copyright 2015, 2016 Ethcore (UK) Ltd. +// This file is part of Parity. + +// Parity is free software: you can redistribute it and/or modify +// it under the terms of the GNU General Public License as published by +// the Free Software Foundation, either version 3 of the License, or +// (at your option) any later version. + +// Parity is distributed in the hope that it will be useful, +// but WITHOUT ANY WARRANTY; without even the implied warranty of +// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the +// GNU General Public License for more details. + +// You should have received a copy of the GNU General Public License +// along with Parity. If not, see . + +import sinon from 'sinon'; + +import JsonRpcBase from './jsonRpcBase'; + +const base = new JsonRpcBase(); + +describe('api/transport/JsonRpcBase', () => { + describe('encode', () => { + it('encodes the body correctly, incrementing id', () => { + const id = base.id; + const bdy = base.encode('someMethod', ['param1', 'param2']); + const enc = `{"jsonrpc":"2.0","method":"someMethod","params":["param1","param2"],"id":${id}}`; + + expect(bdy).to.equal(enc); + expect(base.id - id).to.equal(1); + }); + }); + + describe('setDebug', () => { + it('starts with disabled flag', () => { + expect(base.isDebug).to.be.false; + }); + + it('true flag switches on', () => { + base.setDebug(true); + expect(base.isDebug).to.be.true; + }); + + it('false flag switches off', () => { + base.setDebug(true); + expect(base.isDebug).to.be.true; + base.setDebug(false); + expect(base.isDebug).to.be.false; + }); + + describe('logging', () => { + beforeEach(() => { + sinon.spy(console, 'log'); + sinon.spy(console, 'error'); + }); + + afterEach(() => { + console.log.restore(); + console.error.restore(); + }); + + it('does not log errors with flag off', () => { + base.setDebug(false); + base.log('error'); + expect(console.log).to.not.be.called; + }); + + it('does not log errors with flag off', () => { + base.setDebug(false); + base.error('error'); + expect(console.error).to.not.be.called; + }); + + it('does log errors with flag on', () => { + base.setDebug(true); + base.log('error'); + expect(console.log).to.be.called; + }); + + it('does log errors with flag on', () => { + base.setDebug(true); + base.error('error'); + expect(console.error).to.be.called; + }); + }); + }); +}); diff --git a/js/src/api/transport/ws/index.js b/js/src/api/transport/ws/index.js new file mode 100644 index 000000000..7b6e35934 --- /dev/null +++ b/js/src/api/transport/ws/index.js @@ -0,0 +1,17 @@ +// Copyright 2015, 2016 Ethcore (UK) Ltd. +// This file is part of Parity. + +// Parity is free software: you can redistribute it and/or modify +// it under the terms of the GNU General Public License as published by +// the Free Software Foundation, either version 3 of the License, or +// (at your option) any later version. + +// Parity is distributed in the hope that it will be useful, +// but WITHOUT ANY WARRANTY; without even the implied warranty of +// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the +// GNU General Public License for more details. + +// You should have received a copy of the GNU General Public License +// along with Parity. If not, see . + +export default from './ws'; diff --git a/js/src/api/transport/ws/ws.e2e.js b/js/src/api/transport/ws/ws.e2e.js new file mode 100644 index 000000000..19e4ab8eb --- /dev/null +++ b/js/src/api/transport/ws/ws.e2e.js @@ -0,0 +1,29 @@ +// Copyright 2015, 2016 Ethcore (UK) Ltd. +// This file is part of Parity. + +// Parity is free software: you can redistribute it and/or modify +// it under the terms of the GNU General Public License as published by +// the Free Software Foundation, either version 3 of the License, or +// (at your option) any later version. + +// Parity is distributed in the hope that it will be useful, +// but WITHOUT ANY WARRANTY; without even the implied warranty of +// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the +// GNU General Public License for more details. + +// You should have received a copy of the GNU General Public License +// along with Parity. If not, see . + +import Ws from './ws'; + +const ws = new Ws('ws://localhost:8546/'); + +describe('transport/Ws', () => { + it('connects and makes a call to web3_clientVersion', () => { + return ws.execute('web3_clientVersion').then((version) => { + const [client] = version.split('/'); + + expect(client === 'Geth' || client === 'Parity').to.be.ok; + }); + }); +}); diff --git a/js/src/api/transport/ws/ws.js b/js/src/api/transport/ws/ws.js new file mode 100644 index 000000000..ecab2a5a2 --- /dev/null +++ b/js/src/api/transport/ws/ws.js @@ -0,0 +1,148 @@ +// Copyright 2015, 2016 Ethcore (UK) Ltd. +// This file is part of Parity. + +// Parity is free software: you can redistribute it and/or modify +// it under the terms of the GNU General Public License as published by +// the Free Software Foundation, either version 3 of the License, or +// (at your option) any later version. + +// Parity is distributed in the hope that it will be useful, +// but WITHOUT ANY WARRANTY; without even the implied warranty of +// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the +// GNU General Public License for more details. + +// You should have received a copy of the GNU General Public License +// along with Parity. If not, see . + +import { keccak_256 } from 'js-sha3'; // eslint-disable-line camelcase + +import { Logging } from '../../subscriptions'; +import JsonRpcBase from '../jsonRpcBase'; + +/* global WebSocket */ +export default class Ws extends JsonRpcBase { + constructor (url, token) { + super(); + + this._url = url; + this._token = token; + this._messages = {}; + + this._connecting = true; + this._lastError = null; + this._autoConnect = false; + + this._connect(); + } + + updateToken (token) { + this._token = token; + this._autoConnect = false; + + this._connect(); + } + + _connect () { + const time = parseInt(new Date().getTime() / 1000, 10); + const sha3 = keccak_256(`${this._token}:${time}`); + const hash = `${sha3}_${time}`; + + if (this._ws) { + this._ws.onerror = null; + this._ws.onopen = null; + this._ws.onclose = null; + this._ws.onmessage = null; + this._ws = null; + } + + this._connecting = true; + this._connected = false; + this._lastError = null; + + this._ws = new WebSocket(this._url, hash); + this._ws.onerror = this._onError; + this._ws.onopen = this._onOpen; + this._ws.onclose = this._onClose; + this._ws.onmessage = this._onMessage; + } + + _onOpen = (event) => { + console.log('ws:onOpen', event); + this._connected = true; + this._connecting = false; + this._autoConnect = true; + + Object.keys(this._messages) + .filter((id) => this._messages[id].queued) + .forEach(this._send); + } + + _onClose = (event) => { + console.log('ws:onClose', event); + this._connected = false; + this._connecting = false; + + if (this._autoConnect) { + this._connect(); + } + } + + _onError = (event) => { + console.error('ws:onError', event); + this._lastError = event; + } + + _onMessage = (event) => { + const result = JSON.parse(event.data); + const { method, params, json, resolve, reject } = this._messages[result.id]; + + Logging.send(method, params, { json, result }); + + if (result.error) { + this.error(event.data); + + reject(new Error(`${result.error.code}: ${result.error.message}`)); + delete this._messages[result.id]; + return; + } + + resolve(result.result); + delete this._messages[result.id]; + } + + _send = (id) => { + const message = this._messages[id]; + + message.queued = !this._connected; + + if (this._connected) { + this._ws.send(message.json); + } + } + + execute (method, ...params) { + return new Promise((resolve, reject) => { + const id = this.id; + const json = this.encode(method, params); + + this._messages[id] = { id, method, params, json, resolve, reject }; + this._send(id); + }); + } + + get token () { + return this._token; + } + + get isAutoConnect () { + return this._autoConnect; + } + + get isConnecting () { + return this._connecting; + } + + get lastError () { + return this._lastError; + } +} diff --git a/js/src/api/transport/ws/ws.spec.js b/js/src/api/transport/ws/ws.spec.js new file mode 100644 index 000000000..c417e8911 --- /dev/null +++ b/js/src/api/transport/ws/ws.spec.js @@ -0,0 +1,85 @@ +// Copyright 2015, 2016 Ethcore (UK) Ltd. +// This file is part of Parity. + +// Parity is free software: you can redistribute it and/or modify +// it under the terms of the GNU General Public License as published by +// the Free Software Foundation, either version 3 of the License, or +// (at your option) any later version. + +// Parity is distributed in the hope that it will be useful, +// but WITHOUT ANY WARRANTY; without even the implied warranty of +// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the +// GNU General Public License for more details. + +// You should have received a copy of the GNU General Public License +// along with Parity. If not, see . + +import { TEST_WS_URL, mockWs } from '../../../../test/mockRpc'; +import Ws from './ws'; + +describe('api/transport/Ws', () => { + let transport; + let scope; + + describe('transport', () => { + let result; + + beforeEach(() => { + scope = mockWs([{ method: 'test_anyCall', reply: 'TestResult' }]); + transport = new Ws(TEST_WS_URL); + + return transport + .execute('test_anyCall', 1, 2, 3) + .then((_result) => { + result = _result; + }); + }); + + afterEach(() => { + scope.stop(); + }); + + it('makes call', () => { + expect(scope.isDone()).to.be.true; + }); + + it('sets jsonrpc', () => { + expect(scope.body.test_anyCall.jsonrpc).to.equal('2.0'); + }); + + it('sets the method', () => { + expect(scope.body.test_anyCall.method).to.equal('test_anyCall'); + }); + + it('passes the params', () => { + expect(scope.body.test_anyCall.params).to.deep.equal([1, 2, 3]); + }); + + it('increments the id', () => { + expect(scope.body.test_anyCall.id).not.to.equal(0); + }); + + it('passes the actual result back', () => { + expect(result).to.equal('TestResult'); + }); + }); + + describe('errors', () => { + beforeEach(() => { + scope = mockWs([{ method: 'test_anyCall', reply: { error: { code: 1, message: 'TestError' } } }]); + transport = new Ws(TEST_WS_URL); + }); + + afterEach(() => { + scope.stop(); + }); + + it('returns RPC errors when encountered', () => { + return transport + .execute('test_anyCall') + .catch((error) => { + expect(error).to.match(/TestError/); + }); + }); + }); +}); diff --git a/js/src/api/util/decode.js b/js/src/api/util/decode.js new file mode 100644 index 000000000..065d516e9 --- /dev/null +++ b/js/src/api/util/decode.js @@ -0,0 +1,83 @@ +// Copyright 2015, 2016 Ethcore (UK) Ltd. +// This file is part of Parity. + +// Parity is free software: you can redistribute it and/or modify +// it under the terms of the GNU General Public License as published by +// the Free Software Foundation, either version 3 of the License, or +// (at your option) any later version. + +// Parity is distributed in the hope that it will be useful, +// but WITHOUT ANY WARRANTY; without even the implied warranty of +// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the +// GNU General Public License for more details. + +// You should have received a copy of the GNU General Public License +// along with Parity. If not, see . + +import { isHex } from './types'; + +import Func from '../../abi/spec/function'; +import { fromParamType, toParamType } from '../../abi/spec/paramType/format'; + +export function decodeCallData (data) { + if (!isHex(data)) { + throw new Error('Input to decodeCallData should be a hex value'); + } + + if (data.substr(0, 2) === '0x') { + return decodeCallData(data.slice(2)); + } else if (data.length < 8) { + throw new Error('Input to decodeCallData should be method signature + data'); + } + + const signature = data.substr(0, 8); + const paramdata = data.substr(8); + + return { + signature: `0x${signature}`, + paramdata: `0x${paramdata}` + }; +} + +export function decodeMethodInput (methodAbi, paramdata) { + if (!methodAbi) { + throw new Error('decodeMethodInput should receive valid method-specific ABI'); + } else if (!paramdata) { + throw new Error('decodeMethodInput should receive valid parameter input data'); + } else if (!isHex(paramdata)) { + throw new Error('Input to decodeMethodInput should be a hex value'); + } else if (paramdata.substr(0, 2) === '0x') { + return decodeMethodInput(methodAbi, paramdata.slice(2)); + } else if (paramdata.length % 64 !== 0) { + throw new Error('Parameter length in decodeMethodInput not a multiple of 64 characters'); + } + + return new Func(methodAbi).decodeInput(paramdata).map((decoded) => decoded.value); +} + +// takes a method in form name(...,types) and returns the inferred abi definition +export function methodToAbi (method) { + const length = method.length; + const typesStart = method.indexOf('('); + const typesEnd = method.indexOf(')'); + + if (typesStart === -1) { + throw new Error(`Missing start ( in call to decodeMethod with ${method}`); + } else if (typesEnd === -1) { + throw new Error(`Missing end ) in call to decodeMethod with ${method}`); + } else if (typesEnd < typesStart) { + throw new Error(`End ) is before start ( in call to decodeMethod with ${method}`); + } else if (typesEnd !== length - 1) { + throw new Error(`Extra characters after end ) in call to decodeMethod with ${method}`); + } + + const name = method.substr(0, typesStart); + const types = method.substr(typesStart + 1, length - (typesStart + 1) - 1).split(','); + const inputs = types.map((_type) => { + const type = fromParamType(toParamType(_type)); + + return { type }; + }); + + return { type: 'function', name, inputs }; +} diff --git a/js/src/api/util/decode.spec.js b/js/src/api/util/decode.spec.js new file mode 100644 index 000000000..665af6d03 --- /dev/null +++ b/js/src/api/util/decode.spec.js @@ -0,0 +1,101 @@ +// Copyright 2015, 2016 Ethcore (UK) Ltd. +// This file is part of Parity. + +// Parity is free software: you can redistribute it and/or modify +// it under the terms of the GNU General Public License as published by +// the Free Software Foundation, either version 3 of the License, or +// (at your option) any later version. + +// Parity is distributed in the hope that it will be useful, +// but WITHOUT ANY WARRANTY; without even the implied warranty of +// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the +// GNU General Public License for more details. + +// You should have received a copy of the GNU General Public License +// along with Parity. If not, see . + +import BigNumber from 'bignumber.js'; +import { decodeCallData, decodeMethodInput, methodToAbi } from './decode'; + +describe('api/util/decode', () => { + const METH = '0x70a08231'; + const ENCO = '0x70a082310000000000000000000000005A5eFF38DA95b0D58b6C616f2699168B480953C9'; + const DATA = '0x0000000000000000000000005A5eFF38DA95b0D58b6C616f2699168B480953C9'; + + describe('decodeCallData', () => { + it('throws on non-hex inputs', () => { + expect(() => decodeCallData('invalid')).to.throw(/should be a hex value/); + }); + + it('throws when invalid signature length', () => { + expect(() => decodeCallData(METH.slice(-6))).to.throw(/should be method signature/); + }); + + it('splits valid inputs properly', () => { + expect(decodeCallData(ENCO)).to.deep.equal({ + signature: METH, + paramdata: DATA + }); + }); + }); + + describe('decodeMethodInput', () => { + it('expects a valid ABI', () => { + expect(() => decodeMethodInput(null, null)).to.throw(/should receive valid method/); + }); + + it('expects valid parameter data', () => { + expect(() => decodeMethodInput({}, null)).to.throw(/should receive valid parameter/); + }); + + it('expect valid hex parameter data', () => { + expect(() => decodeMethodInput({}, 'invalid')).to.throw(/should be a hex value/); + }); + + it('throws on invalid lengths', () => { + expect(() => decodeMethodInput({}, DATA.slice(-32))).to.throw(/not a multiple of/); + }); + + it('correctly decodes valid inputs', () => { + expect(decodeMethodInput({ + type: 'function', + inputs: [ + { type: 'uint' } + ] + }, DATA)).to.deep.equal([ new BigNumber('0x5a5eff38da95b0d58b6c616f2699168b480953c9') ]); + }); + }); + + describe('methodToAbi', () => { + it('throws when no start ( specified', () => { + expect(() => methodToAbi('invalid,uint,bool)')).to.throw(/Missing start \(/); + }); + + it('throws when no end ) specified', () => { + expect(() => methodToAbi('invalid(uint,bool')).to.throw(/Missing end \)/); + }); + + it('throws when end ) is not in the last position', () => { + expect(() => methodToAbi('invalid(uint,bool)2')).to.throw(/Extra characters after end \)/); + }); + + it('throws when start ( is after end )', () => { + expect(() => methodToAbi('invalid)uint,bool(')).to.throw(/End \) is before start \(/); + }); + + it('throws when invalid types are present', () => { + expect(() => methodToAbi('method(invalidType,bool,uint)')).to.throw(/Cannot convert invalidType/); + }); + + it('returns a valid methodabi for a valid method', () => { + expect(methodToAbi('valid(uint,bool)')).to.deep.equals({ + type: 'function', + name: 'valid', + inputs: [ + { type: 'uint256' }, + { type: 'bool' } + ] + }); + }); + }); +}); diff --git a/js/src/api/util/format.js b/js/src/api/util/format.js new file mode 100644 index 000000000..1b68e1138 --- /dev/null +++ b/js/src/api/util/format.js @@ -0,0 +1,19 @@ +// Copyright 2015, 2016 Ethcore (UK) Ltd. +// This file is part of Parity. + +// Parity is free software: you can redistribute it and/or modify +// it under the terms of the GNU General Public License as published by +// the Free Software Foundation, either version 3 of the License, or +// (at your option) any later version. + +// Parity is distributed in the hope that it will be useful, +// but WITHOUT ANY WARRANTY; without even the implied warranty of +// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the +// GNU General Public License for more details. + +// You should have received a copy of the GNU General Public License +// along with Parity. If not, see . + +export function bytesToHex (bytes) { + return '0x' + bytes.map((b) => ('0' + b.toString(16)).slice(-2)).join(''); +} diff --git a/js/src/api/util/format.spec.js b/js/src/api/util/format.spec.js new file mode 100644 index 000000000..c779d71ef --- /dev/null +++ b/js/src/api/util/format.spec.js @@ -0,0 +1,29 @@ +// Copyright 2015, 2016 Ethcore (UK) Ltd. +// This file is part of Parity. + +// Parity is free software: you can redistribute it and/or modify +// it under the terms of the GNU General Public License as published by +// the Free Software Foundation, either version 3 of the License, or +// (at your option) any later version. + +// Parity is distributed in the hope that it will be useful, +// but WITHOUT ANY WARRANTY; without even the implied warranty of +// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the +// GNU General Public License for more details. + +// You should have received a copy of the GNU General Public License +// along with Parity. If not, see . + +import { bytesToHex } from './format'; + +describe('api/util/format', () => { + describe('bytesToHex', () => { + it('correctly converts an empty array', () => { + expect(bytesToHex([])).to.equal('0x'); + }); + + it('correctly converts a non-empty array', () => { + expect(bytesToHex([0, 15, 16])).to.equal('0x000f10'); + }); + }); +}); diff --git a/js/src/api/util/identity.js b/js/src/api/util/identity.js new file mode 100644 index 000000000..6a25590e3 --- /dev/null +++ b/js/src/api/util/identity.js @@ -0,0 +1,9 @@ +import blockies from 'blockies'; + +export function createIdentityImg (address, scale = 8) { + return blockies({ + seed: (address || '').toLowerCase(), + size: 8, + scale + }).toDataURL(); +} diff --git a/js/src/api/util/index.js b/js/src/api/util/index.js new file mode 100644 index 000000000..fb0f79076 --- /dev/null +++ b/js/src/api/util/index.js @@ -0,0 +1,41 @@ +// Copyright 2015, 2016 Ethcore (UK) Ltd. +// This file is part of Parity. + +// Parity is free software: you can redistribute it and/or modify +// it under the terms of the GNU General Public License as published by +// the Free Software Foundation, either version 3 of the License, or +// (at your option) any later version. + +// Parity is distributed in the hope that it will be useful, +// but WITHOUT ANY WARRANTY; without even the implied warranty of +// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the +// GNU General Public License for more details. + +// You should have received a copy of the GNU General Public License +// along with Parity. If not, see . + +import { isAddress as isAddressValid, toChecksumAddress } from '../../abi/util/address'; +import { decodeCallData, decodeMethodInput, methodToAbi } from './decode'; +import { bytesToHex } from './format'; +import { fromWei, toWei } from './wei'; +import { sha3 } from './sha3'; +import { isArray, isFunction, isHex, isInstanceOf, isString } from './types'; +import { createIdentityImg } from './identity'; + +export default { + isAddressValid, + isArray, + isFunction, + isHex, + isInstanceOf, + isString, + bytesToHex, + createIdentityImg, + decodeCallData, + decodeMethodInput, + methodToAbi, + fromWei, + toChecksumAddress, + toWei, + sha3 +}; diff --git a/js/src/api/util/sha3.js b/js/src/api/util/sha3.js new file mode 100644 index 000000000..fcbda091a --- /dev/null +++ b/js/src/api/util/sha3.js @@ -0,0 +1,21 @@ +// Copyright 2015, 2016 Ethcore (UK) Ltd. +// This file is part of Parity. + +// Parity is free software: you can redistribute it and/or modify +// it under the terms of the GNU General Public License as published by +// the Free Software Foundation, either version 3 of the License, or +// (at your option) any later version. + +// Parity is distributed in the hope that it will be useful, +// but WITHOUT ANY WARRANTY; without even the implied warranty of +// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the +// GNU General Public License for more details. + +// You should have received a copy of the GNU General Public License +// along with Parity. If not, see . + +import { keccak_256 } from 'js-sha3'; // eslint-disable-line camelcase + +export function sha3 (value) { + return `0x${keccak_256(value)}`; +} diff --git a/js/src/api/util/sha3.spec.js b/js/src/api/util/sha3.spec.js new file mode 100644 index 000000000..bede8bd60 --- /dev/null +++ b/js/src/api/util/sha3.spec.js @@ -0,0 +1,25 @@ +// Copyright 2015, 2016 Ethcore (UK) Ltd. +// This file is part of Parity. + +// Parity is free software: you can redistribute it and/or modify +// it under the terms of the GNU General Public License as published by +// the Free Software Foundation, either version 3 of the License, or +// (at your option) any later version. + +// Parity is distributed in the hope that it will be useful, +// but WITHOUT ANY WARRANTY; without even the implied warranty of +// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the +// GNU General Public License for more details. + +// You should have received a copy of the GNU General Public License +// along with Parity. If not, see . + +import { sha3 } from './sha3'; + +describe('api/util/sha3', () => { + describe('sha3', () => { + it('constructs a correct sha3 value', () => { + expect(sha3('jacogr')).to.equal('0x2f4ff4b5a87abbd2edfed699db48a97744e028c7f7ce36444d40d29d792aa4dc'); + }); + }); +}); diff --git a/js/src/api/util/types.js b/js/src/api/util/types.js new file mode 100644 index 000000000..a34f30649 --- /dev/null +++ b/js/src/api/util/types.js @@ -0,0 +1,56 @@ +// Copyright 2015, 2016 Ethcore (UK) Ltd. +// This file is part of Parity. + +// Parity is free software: you can redistribute it and/or modify +// it under the terms of the GNU General Public License as published by +// the Free Software Foundation, either version 3 of the License, or +// (at your option) any later version. + +// Parity is distributed in the hope that it will be useful, +// but WITHOUT ANY WARRANTY; without even the implied warranty of +// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the +// GNU General Public License for more details. + +// You should have received a copy of the GNU General Public License +// along with Parity. If not, see . + +const HEXDIGITS = ['0', '1', '2', '3', '4', '5', '6', '7', '8', '9', 'a', 'b', 'c', 'd', 'e', 'f']; + +export function isArray (test) { + return Object.prototype.toString.call(test) === '[object Array]'; +} + +export function isError (test) { + return Object.prototype.toString.call(test) === '[object Error]'; +} + +export function isFunction (test) { + return Object.prototype.toString.call(test) === '[object Function]'; +} + +export function isHex (_test) { + if (_test.substr(0, 2) === '0x') { + return isHex(_test.slice(2)); + } + + const test = _test.toLowerCase(); + let hex = true; + + for (let idx = 0; hex && idx < test.length; idx++) { + hex = HEXDIGITS.includes(test[idx]); + } + + return hex; +} + +export function isObject (test) { + return Object.prototype.toString.call(test) === '[object Object]'; +} + +export function isString (test) { + return Object.prototype.toString.call(test) === '[object String]'; +} + +export function isInstanceOf (test, clazz) { + return test instanceof clazz; +} diff --git a/js/src/api/util/types.spec.js b/js/src/api/util/types.spec.js new file mode 100644 index 000000000..252876be3 --- /dev/null +++ b/js/src/api/util/types.spec.js @@ -0,0 +1,112 @@ +// Copyright 2015, 2016 Ethcore (UK) Ltd. +// This file is part of Parity. + +// Parity is free software: you can redistribute it and/or modify +// it under the terms of the GNU General Public License as published by +// the Free Software Foundation, either version 3 of the License, or +// (at your option) any later version. + +// Parity is distributed in the hope that it will be useful, +// but WITHOUT ANY WARRANTY; without even the implied warranty of +// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the +// GNU General Public License for more details. + +// You should have received a copy of the GNU General Public License +// along with Parity. If not, see . + +import sinon from 'sinon'; + +import { isArray, isError, isFunction, isHex, isInstanceOf, isObject, isString } from './types'; +import Eth from '../rpc/eth'; + +describe('api/util/types', () => { + describe('isArray', () => { + it('correctly identifies null as false', () => { + expect(isArray(null)).to.be.false; + }); + + it('correctly identifies empty array as true', () => { + expect(isArray([])).to.be.true; + }); + + it('correctly identifies array as true', () => { + expect(isArray([1, 2, 3])).to.be.true; + }); + }); + + describe('isError', () => { + it('correctly identifies null as false', () => { + expect(isError(null)).to.be.false; + }); + + it('correctly identifies Error as true', () => { + expect(isError(new Error('an error'))).to.be.true; + }); + }); + + describe('isFunction', () => { + it('correctly identifies null as false', () => { + expect(isFunction(null)).to.be.false; + }); + + it('correctly identifies function as true', () => { + expect(isFunction(sinon.stub())).to.be.true; + }); + }); + + describe('isHex', () => { + it('correctly identifies hex by leading 0x', () => { + expect(isHex('0x123')).to.be.true; + }); + + it('correctly identifies hex without leading 0x', () => { + expect(isHex('123')).to.be.true; + }); + + it('correctly identifies non-hex values', () => { + expect(isHex('123j')).to.be.false; + }); + }); + + describe('isInstanceOf', () => { + it('correctly identifies build-in instanceof', () => { + expect(isInstanceOf(new String('123'), String)).to.be.true; // eslint-disable-line no-new-wrappers + }); + + it('correctly identifies own instanceof', () => { + expect(isInstanceOf(new Eth({}), Eth)).to.be.true; + }); + + it('correctly reports false for own', () => { + expect(isInstanceOf({}, Eth)).to.be.false; + }); + }); + + describe('isObject', () => { + it('correctly identifies empty object as object', () => { + expect(isObject({})).to.be.true; + }); + + it('correctly identifies non-empty object as object', () => { + expect(isObject({ data: '123' })).to.be.true; + }); + + it('correctly identifies Arrays as non-objects', () => { + expect(isObject([1, 2, 3])).to.be.false; + }); + + it('correctly identifies Strings as non-objects', () => { + expect(isObject('123')).to.be.false; + }); + }); + + describe('isString', () => { + it('correctly identifies empty string as string', () => { + expect(isString('')).to.be.true; + }); + + it('correctly identifies string as string', () => { + expect(isString('123')).to.be.true; + }); + }); +}); diff --git a/js/src/api/util/wei.js b/js/src/api/util/wei.js new file mode 100644 index 000000000..d04e73921 --- /dev/null +++ b/js/src/api/util/wei.js @@ -0,0 +1,37 @@ +// Copyright 2015, 2016 Ethcore (UK) Ltd. +// This file is part of Parity. + +// Parity is free software: you can redistribute it and/or modify +// it under the terms of the GNU General Public License as published by +// the Free Software Foundation, either version 3 of the License, or +// (at your option) any later version. + +// Parity is distributed in the hope that it will be useful, +// but WITHOUT ANY WARRANTY; without even the implied warranty of +// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the +// GNU General Public License for more details. + +// You should have received a copy of the GNU General Public License +// along with Parity. If not, see . + +import BigNumber from 'bignumber.js'; + +const UNITS = ['wei', 'ada', 'babbage', 'shannon', 'szabo', 'finney', 'ether', 'kether', 'mether', 'gether', 'tether']; + +export function _getUnitMultiplier (unit) { + const position = UNITS.indexOf(unit.toLowerCase()); + + if (position === -1) { + throw new Error(`Unknown unit ${unit} passed to wei formatter`); + } + + return 10 ** (position * 3); +} + +export function fromWei (value, unit = 'ether') { + return new BigNumber(value).div(_getUnitMultiplier(unit)); +} + +export function toWei (value, unit = 'ether') { + return new BigNumber(value).mul(_getUnitMultiplier(unit)); +} diff --git a/js/src/api/util/wei.spec.js b/js/src/api/util/wei.spec.js new file mode 100644 index 000000000..8d0b1f09c --- /dev/null +++ b/js/src/api/util/wei.spec.js @@ -0,0 +1,57 @@ +// Copyright 2015, 2016 Ethcore (UK) Ltd. +// This file is part of Parity. + +// Parity is free software: you can redistribute it and/or modify +// it under the terms of the GNU General Public License as published by +// the Free Software Foundation, either version 3 of the License, or +// (at your option) any later version. + +// Parity is distributed in the hope that it will be useful, +// but WITHOUT ANY WARRANTY; without even the implied warranty of +// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the +// GNU General Public License for more details. + +// You should have received a copy of the GNU General Public License +// along with Parity. If not, see . + +import { _getUnitMultiplier, fromWei, toWei } from './wei'; + +describe('api/util/wei', () => { + describe('_getUnitMultiplier', () => { + it('returns 10^0 for wei', () => { + expect(_getUnitMultiplier('wei')).to.equal(10 ** 0); + }); + + it('returns 10^15 for finney', () => { + expect(_getUnitMultiplier('finney')).to.equal(10 ** 15); + }); + + it('returns 10^18 for ether', () => { + expect(_getUnitMultiplier('ether')).to.equal(10 ** 18); + }); + + it('throws an error on invalid units', () => { + expect(() => _getUnitMultiplier('invalid')).to.throw(/passed to wei formatter/); + }); + }); + + describe('fromWei', () => { + it('formats into ether when nothing specified', () => { + expect(fromWei('1230000000000000000').toString()).to.equal('1.23'); + }); + + it('formats into finney when specified', () => { + expect(fromWei('1230000000000000000', 'finney').toString()).to.equal('1230'); + }); + }); + + describe('toWei', () => { + it('formats from ether when nothing specified', () => { + expect(toWei(1.23).toString()).to.equal('1230000000000000000'); + }); + + it('formats from finney when specified', () => { + expect(toWei(1230, 'finney').toString()).to.equal('1230000000000000000'); + }); + }); +}); diff --git a/js/src/contracts/abi/basiccoin.json b/js/src/contracts/abi/basiccoin.json new file mode 100644 index 000000000..0bdc66666 --- /dev/null +++ b/js/src/contracts/abi/basiccoin.json @@ -0,0 +1 @@ +[{"constant":false,"inputs":[{"name":"_spender","type":"address"},{"name":"_value","type":"uint256"}],"name":"approve","outputs":[{"name":"","type":"bool"}],"payable":false,"type":"function"},{"constant":false,"inputs":[{"name":"_new","type":"address"}],"name":"setOwner","outputs":[],"payable":false,"type":"function"},{"constant":true,"inputs":[],"name":"totalSupply","outputs":[{"name":"","type":"uint256"}],"payable":false,"type":"function"},{"constant":false,"inputs":[{"name":"_from","type":"address"},{"name":"_to","type":"address"},{"name":"_value","type":"uint256"}],"name":"transferFrom","outputs":[{"name":"","type":"bool"}],"payable":false,"type":"function"},{"constant":true,"inputs":[],"name":"base","outputs":[{"name":"","type":"uint256"}],"payable":false,"type":"function"},{"constant":true,"inputs":[{"name":"_who","type":"address"}],"name":"balanceOf","outputs":[{"name":"","type":"uint256"}],"payable":false,"type":"function"},{"constant":true,"inputs":[],"name":"owner","outputs":[{"name":"","type":"address"}],"payable":false,"type":"function"},{"constant":false,"inputs":[{"name":"_to","type":"address"},{"name":"_value","type":"uint256"}],"name":"transfer","outputs":[{"name":"","type":"bool"}],"payable":false,"type":"function"},{"constant":true,"inputs":[{"name":"_owner","type":"address"},{"name":"_spender","type":"address"}],"name":"allowance","outputs":[{"name":"","type":"uint256"}],"payable":false,"type":"function"},{"inputs":[{"name":"_totalSupply","type":"uint256"},{"name":"_owner","type":"address"}],"type":"constructor"},{"payable":false,"type":"fallback"},{"anonymous":false,"inputs":[{"indexed":true,"name":"from","type":"address"},{"indexed":true,"name":"to","type":"address"},{"indexed":false,"name":"value","type":"uint256"}],"name":"Transfer","type":"event"},{"anonymous":false,"inputs":[{"indexed":true,"name":"owner","type":"address"},{"indexed":true,"name":"spender","type":"address"},{"indexed":false,"name":"value","type":"uint256"}],"name":"Approval","type":"event"},{"anonymous":false,"inputs":[{"indexed":true,"name":"old","type":"address"},{"indexed":true,"name":"current","type":"address"}],"name":"NewOwner","type":"event"}] diff --git a/js/src/contracts/abi/basiccoinmanager.json b/js/src/contracts/abi/basiccoinmanager.json new file mode 100644 index 000000000..cafe09735 --- /dev/null +++ b/js/src/contracts/abi/basiccoinmanager.json @@ -0,0 +1 @@ +[{"constant":true,"inputs":[{"name":"_owner","type":"address"}],"name":"countByOwner","outputs":[{"name":"","type":"uint256"}],"payable":false,"type":"function"},{"constant":true,"inputs":[],"name":"count","outputs":[{"name":"","type":"uint256"}],"payable":false,"type":"function"},{"constant":false,"inputs":[{"name":"_new","type":"address"}],"name":"setOwner","outputs":[],"payable":false,"type":"function"},{"constant":true,"inputs":[],"name":"base","outputs":[{"name":"","type":"uint256"}],"payable":false,"type":"function"},{"constant":true,"inputs":[],"name":"owner","outputs":[{"name":"","type":"address"}],"payable":false,"type":"function"},{"constant":true,"inputs":[{"name":"_index","type":"uint256"}],"name":"get","outputs":[{"name":"coin","type":"address"},{"name":"owner","type":"address"},{"name":"tokenreg","type":"address"}],"payable":false,"type":"function"},{"constant":false,"inputs":[],"name":"drain","outputs":[],"payable":false,"type":"function"},{"constant":false,"inputs":[{"name":"_totalSupply","type":"uint256"},{"name":"_tla","type":"string"},{"name":"_name","type":"string"},{"name":"_tokenreg","type":"address"}],"name":"deploy","outputs":[{"name":"","type":"bool"}],"payable":true,"type":"function"},{"constant":true,"inputs":[{"name":"_owner","type":"address"},{"name":"_index","type":"uint256"}],"name":"getByOwner","outputs":[{"name":"coin","type":"address"},{"name":"owner","type":"address"},{"name":"tokenreg","type":"address"}],"payable":false,"type":"function"},{"anonymous":false,"inputs":[{"indexed":true,"name":"owner","type":"address"},{"indexed":true,"name":"tokenreg","type":"address"},{"indexed":true,"name":"coin","type":"address"}],"name":"Created","type":"event"},{"anonymous":false,"inputs":[{"indexed":true,"name":"old","type":"address"},{"indexed":true,"name":"current","type":"address"}],"name":"NewOwner","type":"event"}] diff --git a/js/src/contracts/abi/dappreg.json b/js/src/contracts/abi/dappreg.json new file mode 100644 index 000000000..f6bec35af --- /dev/null +++ b/js/src/contracts/abi/dappreg.json @@ -0,0 +1 @@ +[{"constant":true,"inputs":[{"name":"_id","type":"bytes32"},{"name":"_key","type":"bytes32"}],"name":"meta","outputs":[{"name":"","type":"bytes32"}],"payable":false,"type":"function"},{"constant":true,"inputs":[],"name":"count","outputs":[{"name":"","type":"uint256"}],"payable":false,"type":"function"},{"constant":false,"inputs":[{"name":"_new","type":"address"}],"name":"setOwner","outputs":[],"payable":false,"type":"function"},{"constant":false,"inputs":[{"name":"_id","type":"bytes32"}],"name":"unregister","outputs":[],"payable":false,"type":"function"},{"constant":false,"inputs":[{"name":"_fee","type":"uint256"}],"name":"setFee","outputs":[],"payable":false,"type":"function"},{"constant":true,"inputs":[],"name":"owner","outputs":[{"name":"","type":"address"}],"payable":false,"type":"function"},{"constant":true,"inputs":[{"name":"_id","type":"bytes32"}],"name":"get","outputs":[{"name":"id","type":"bytes32"},{"name":"owner","type":"address"}],"payable":false,"type":"function"},{"constant":false,"inputs":[{"name":"_id","type":"bytes32"},{"name":"_key","type":"bytes32"},{"name":"_value","type":"bytes32"}],"name":"setMeta","outputs":[],"payable":false,"type":"function"},{"constant":false,"inputs":[],"name":"drain","outputs":[],"payable":false,"type":"function"},{"constant":false,"inputs":[{"name":"_id","type":"bytes32"},{"name":"_owner","type":"address"}],"name":"setDappOwner","outputs":[],"payable":false,"type":"function"},{"constant":true,"inputs":[],"name":"fee","outputs":[{"name":"","type":"uint256"}],"payable":false,"type":"function"},{"constant":true,"inputs":[{"name":"_index","type":"uint256"}],"name":"at","outputs":[{"name":"id","type":"bytes32"},{"name":"owner","type":"address"}],"payable":false,"type":"function"},{"constant":false,"inputs":[{"name":"_id","type":"bytes32"}],"name":"register","outputs":[],"payable":true,"type":"function"},{"anonymous":false,"inputs":[{"indexed":true,"name":"id","type":"bytes32"},{"indexed":true,"name":"key","type":"bytes32"},{"indexed":false,"name":"value","type":"bytes32"}],"name":"MetaChanged","type":"event"},{"anonymous":false,"inputs":[{"indexed":true,"name":"id","type":"bytes32"},{"indexed":true,"name":"owner","type":"address"}],"name":"OwnerChanged","type":"event"},{"anonymous":false,"inputs":[{"indexed":true,"name":"id","type":"bytes32"},{"indexed":true,"name":"owner","type":"address"}],"name":"Registered","type":"event"},{"anonymous":false,"inputs":[{"indexed":true,"name":"id","type":"bytes32"}],"name":"Unregistered","type":"event"},{"anonymous":false,"inputs":[{"indexed":true,"name":"old","type":"address"},{"indexed":true,"name":"current","type":"address"}],"name":"NewOwner","type":"event"}] diff --git a/js/src/contracts/abi/eip20.json b/js/src/contracts/abi/eip20.json new file mode 100644 index 000000000..6937e28c8 --- /dev/null +++ b/js/src/contracts/abi/eip20.json @@ -0,0 +1,163 @@ +[ + { + "constant": false, + "inputs": [ + { + "name": "_spender", + "type": "address" + }, + { + "name": "_value", + "type": "uint256" + } + ], + "name": "approve", + "outputs": [ + { + "name": "success", + "type": "bool" + } + ], + "type": "function" + }, + { + "constant": true, + "inputs": [], + "name": "totalSupply", + "outputs": [ + { + "name": "total", + "type": "uint256" + } + ], + "type": "function" + }, + { + "constant": false, + "inputs": [ + { + "name": "_from", + "type": "address" + }, + { + "name": "_to", + "type": "address" + }, + { + "name": "_value", + "type": "uint256" + } + ], + "name": "transferFrom", + "outputs": [ + { + "name": "success", + "type": "bool" + } + ], + "type": "function" + }, + { + "constant": true, + "inputs": [ + { + "name": "_owner", + "type": "address" + } + ], + "name": "balanceOf", + "outputs": [ + { + "name": "balance", + "type": "uint256" + } + ], + "type": "function" + }, + { + "constant": false, + "inputs": [ + { + "name": "_to", + "type": "address" + }, + { + "name": "_value", + "type": "uint256" + } + ], + "name": "transfer", + "outputs": [ + { + "name": "success", + "type": "bool" + } + ], + "type": "function" + }, + { + "constant": true, + "inputs": [ + { + "name": "_owner", + "type": "address" + }, + { + "name": "_spender", + "type": "address" + } + ], + "name": "allowance", + "outputs": [ + { + "name": "remaining", + "type": "uint256" + } + ], + "type": "function" + }, + { + "anonymous": false, + "inputs": [ + { + "indexed": true, + "name": "from", + "type": "address" + }, + { + "indexed": true, + "name": "to", + "type": "address" + }, + { + "indexed": false, + "name": "value", + "type": "uint256" + } + ], + "name": "Transfer", + "type": "event" + }, + { + "anonymous": false, + "inputs": [ + { + "indexed": true, + "name": "owner", + "type": "address" + }, + { + "indexed": true, + "name": "spender", + "type": "address" + }, + { + "indexed": false, + "name": "value", + "type": "uint256" + } + ], + "name": "Approval", + "type": "event" + } +] diff --git a/js/src/contracts/abi/gavcoin.json b/js/src/contracts/abi/gavcoin.json new file mode 100644 index 000000000..5170326f5 --- /dev/null +++ b/js/src/contracts/abi/gavcoin.json @@ -0,0 +1 @@ +[{"constant":false,"inputs":[{"name":"_spender","type":"address"},{"name":"_value","type":"uint256"}],"name":"approve","outputs":[{"name":"success","type":"bool"}],"type":"function"},{"constant":true,"inputs":[],"name":"totalSupply","outputs":[{"name":"","type":"uint256"}],"type":"function"},{"constant":false,"inputs":[{"name":"_from","type":"address"},{"name":"_to","type":"address"},{"name":"_value","type":"uint256"}],"name":"transferFrom","outputs":[{"name":"success","type":"bool"}],"type":"function"},{"constant":false,"inputs":[{"name":"_who","type":"address"},{"name":"_maxPrice","type":"uint256"}],"name":"buyin","outputs":[],"type":"function"},{"constant":true,"inputs":[],"name":"remaining","outputs":[{"name":"","type":"uint256"}],"type":"function"},{"constant":false,"inputs":[{"name":"_price","type":"uint256"},{"name":"_units","type":"uint256"}],"name":"refund","outputs":[{"name":"","type":"bool"}],"type":"function"},{"constant":true,"inputs":[{"name":"_who","type":"address"}],"name":"balanceOf","outputs":[{"name":"","type":"uint256"}],"type":"function"},{"constant":true,"inputs":[],"name":"price","outputs":[{"name":"","type":"uint256"}],"type":"function"},{"constant":false,"inputs":[{"name":"_to","type":"address"},{"name":"_value","type":"uint256"}],"name":"transfer","outputs":[{"name":"success","type":"bool"}],"type":"function"},{"constant":true,"inputs":[{"name":"_owner","type":"address"},{"name":"_spender","type":"address"}],"name":"allowance","outputs":[{"name":"remaining","type":"uint256"}],"type":"function"},{"anonymous":false,"inputs":[{"indexed":true,"name":"from","type":"address"},{"indexed":true,"name":"to","type":"address"},{"indexed":false,"name":"value","type":"uint256"}],"name":"Transfer","type":"event"},{"anonymous":false,"inputs":[{"indexed":true,"name":"owner","type":"address"},{"indexed":true,"name":"spender","type":"address"},{"indexed":false,"name":"value","type":"uint256"}],"name":"Approval","type":"event"},{"anonymous":false,"inputs":[{"indexed":true,"name":"buyer","type":"address"},{"indexed":true,"name":"price","type":"uint256"},{"indexed":true,"name":"amount","type":"uint256"}],"name":"Buyin","type":"event"},{"anonymous":false,"inputs":[{"indexed":true,"name":"buyer","type":"address"},{"indexed":true,"name":"price","type":"uint256"},{"indexed":true,"name":"amount","type":"uint256"}],"name":"Refund","type":"event"},{"anonymous":false,"inputs":[{"indexed":true,"name":"price","type":"uint256"}],"name":"NewTranch","type":"event"}] diff --git a/js/src/contracts/abi/githubhint.json b/js/src/contracts/abi/githubhint.json new file mode 100644 index 000000000..bdbc162e4 --- /dev/null +++ b/js/src/contracts/abi/githubhint.json @@ -0,0 +1,81 @@ +[ + { + "constant":false, + "inputs":[ + { + "name":"_content", + "type":"bytes32" + }, + { + "name":"_url", + "type":"string" + } + ], + "name":"hintURL", + "outputs":[ + + ], + "type":"function" + }, + { + "constant":false, + "inputs":[ + { + "name":"_content", + "type":"bytes32" + }, + { + "name":"_accountSlashRepo", + "type":"string" + }, + { + "name":"_commit", + "type":"bytes20" + } + ], + "name":"hint", + "outputs":[ + + ], + "type":"function" + }, + { + "constant":true, + "inputs":[ + { + "name":"", + "type":"bytes32" + } + ], + "name":"entries", + "outputs":[ + { + "name":"accountSlashRepo", + "type":"string" + }, + { + "name":"commit", + "type":"bytes20" + }, + { + "name":"owner", + "type":"address" + } + ], + "type":"function" + }, + { + "constant":false, + "inputs":[ + { + "name":"_content", + "type":"bytes32" + } + ], + "name":"unhint", + "outputs":[ + + ], + "type":"function" + } +] diff --git a/js/src/contracts/abi/index.js b/js/src/contracts/abi/index.js new file mode 100644 index 000000000..599f8a13b --- /dev/null +++ b/js/src/contracts/abi/index.js @@ -0,0 +1,39 @@ +// Copyright 2015, 2016 Ethcore (UK) Ltd. +// This file is part of Parity. + +// Parity is free software: you can redistribute it and/or modify +// it under the terms of the GNU General Public License as published by +// the Free Software Foundation, either version 3 of the License, or +// (at your option) any later version. + +// Parity is distributed in the hope that it will be useful, +// but WITHOUT ANY WARRANTY; without even the implied warranty of +// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the +// GNU General Public License for more details. + +// You should have received a copy of the GNU General Public License +// along with Parity. If not, see . + +import basiccoin from './basiccoin.json'; +import basiccoinmanager from './basiccoinmanager.json'; +import dappreg from './dappreg.json'; +import eip20 from './eip20.json'; +import gavcoin from './gavcoin.json'; +import githubhint from './githubhint.json'; +import owned from './owned.json'; +import registry from './registry.json'; +import signaturereg from './signaturereg.json'; +import tokenreg from './tokenreg.json'; + +export { + basiccoin, + basiccoinmanager, + dappreg, + eip20, + gavcoin, + githubhint, + owned, + registry, + signaturereg, + tokenreg +}; diff --git a/js/src/contracts/abi/owned.json b/js/src/contracts/abi/owned.json new file mode 100644 index 000000000..ccfeed85d --- /dev/null +++ b/js/src/contracts/abi/owned.json @@ -0,0 +1 @@ +[{"constant":false,"inputs":[{"name":"_new","type":"address"}],"name":"setOwner","outputs":[],"type":"function"},{"constant":true,"inputs":[],"name":"owner","outputs":[{"name":"","type":"address"}],"type":"function"},{"anonymous":false,"inputs":[{"indexed":true,"name":"old","type":"address"},{"indexed":true,"name":"current","type":"address"}],"name":"NewOwner","type":"event"}] diff --git a/js/src/contracts/abi/registry.json b/js/src/contracts/abi/registry.json new file mode 100644 index 000000000..f97dc20c7 --- /dev/null +++ b/js/src/contracts/abi/registry.json @@ -0,0 +1 @@ +[{"constant":false,"inputs":[{"name":"_new","type":"address"}],"name":"setOwner","outputs":[],"type":"function"},{"constant":false,"inputs":[{"name":"_name","type":"string"}],"name":"confirmReverse","outputs":[{"name":"success","type":"bool"}],"type":"function"},{"constant":false,"inputs":[{"name":"_name","type":"bytes32"}],"name":"reserve","outputs":[{"name":"success","type":"bool"}],"type":"function"},{"constant":false,"inputs":[{"name":"_name","type":"bytes32"},{"name":"_key","type":"string"},{"name":"_value","type":"bytes32"}],"name":"set","outputs":[{"name":"success","type":"bool"}],"type":"function"},{"constant":false,"inputs":[{"name":"_name","type":"bytes32"}],"name":"drop","outputs":[{"name":"success","type":"bool"}],"type":"function"},{"constant":true,"inputs":[{"name":"_name","type":"bytes32"},{"name":"_key","type":"string"}],"name":"getAddress","outputs":[{"name":"","type":"address"}],"type":"function"},{"constant":false,"inputs":[{"name":"_amount","type":"uint256"}],"name":"setFee","outputs":[],"type":"function"},{"constant":false,"inputs":[{"name":"_name","type":"bytes32"},{"name":"_to","type":"address"}],"name":"transfer","outputs":[{"name":"success","type":"bool"}],"type":"function"},{"constant":true,"inputs":[],"name":"owner","outputs":[{"name":"","type":"address"}],"type":"function"},{"constant":true,"inputs":[{"name":"_name","type":"bytes32"}],"name":"reserved","outputs":[{"name":"reserved","type":"bool"}],"type":"function"},{"constant":false,"inputs":[],"name":"drain","outputs":[],"type":"function"},{"constant":false,"inputs":[{"name":"_name","type":"string"},{"name":"_who","type":"address"}],"name":"proposeReverse","outputs":[{"name":"success","type":"bool"}],"type":"function"},{"constant":true,"inputs":[{"name":"_name","type":"bytes32"},{"name":"_key","type":"string"}],"name":"getUint","outputs":[{"name":"","type":"uint256"}],"type":"function"},{"constant":true,"inputs":[{"name":"_name","type":"bytes32"},{"name":"_key","type":"string"}],"name":"get","outputs":[{"name":"","type":"bytes32"}],"type":"function"},{"constant":true,"inputs":[],"name":"fee","outputs":[{"name":"","type":"uint256"}],"type":"function"},{"constant":true,"inputs":[{"name":"","type":"address"}],"name":"reverse","outputs":[{"name":"","type":"string"}],"type":"function"},{"constant":false,"inputs":[{"name":"_name","type":"bytes32"},{"name":"_key","type":"string"},{"name":"_value","type":"uint256"}],"name":"setUint","outputs":[{"name":"success","type":"bool"}],"type":"function"},{"constant":false,"inputs":[],"name":"removeReverse","outputs":[],"type":"function"},{"constant":false,"inputs":[{"name":"_name","type":"bytes32"},{"name":"_key","type":"string"},{"name":"_value","type":"address"}],"name":"setAddress","outputs":[{"name":"success","type":"bool"}],"type":"function"},{"anonymous":false,"inputs":[{"indexed":false,"name":"amount","type":"uint256"}],"name":"Drained","type":"event"},{"anonymous":false,"inputs":[{"indexed":false,"name":"amount","type":"uint256"}],"name":"FeeChanged","type":"event"},{"anonymous":false,"inputs":[{"indexed":true,"name":"name","type":"bytes32"},{"indexed":true,"name":"owner","type":"address"}],"name":"Reserved","type":"event"},{"anonymous":false,"inputs":[{"indexed":true,"name":"name","type":"bytes32"},{"indexed":true,"name":"oldOwner","type":"address"},{"indexed":true,"name":"newOwner","type":"address"}],"name":"Transferred","type":"event"},{"anonymous":false,"inputs":[{"indexed":true,"name":"name","type":"bytes32"},{"indexed":true,"name":"owner","type":"address"}],"name":"Dropped","type":"event"},{"anonymous":false,"inputs":[{"indexed":true,"name":"name","type":"bytes32"},{"indexed":true,"name":"owner","type":"address"},{"indexed":true,"name":"key","type":"string"},{"indexed":false,"name":"plainKey","type":"string"}],"name":"DataChanged","type":"event"},{"anonymous":false,"inputs":[{"indexed":true,"name":"name","type":"string"},{"indexed":true,"name":"reverse","type":"address"}],"name":"ReverseProposed","type":"event"},{"anonymous":false,"inputs":[{"indexed":true,"name":"name","type":"string"},{"indexed":true,"name":"reverse","type":"address"}],"name":"ReverseConfirmed","type":"event"},{"anonymous":false,"inputs":[{"indexed":true,"name":"name","type":"string"},{"indexed":true,"name":"reverse","type":"address"}],"name":"ReverseRemoved","type":"event"},{"anonymous":false,"inputs":[{"indexed":true,"name":"old","type":"address"},{"indexed":true,"name":"current","type":"address"}],"name":"NewOwner","type":"event"}] diff --git a/js/src/contracts/abi/signaturereg.json b/js/src/contracts/abi/signaturereg.json new file mode 100644 index 000000000..a9b109ecd --- /dev/null +++ b/js/src/contracts/abi/signaturereg.json @@ -0,0 +1,128 @@ +[ + { + "constant": false, + "inputs": [ + { + "name": "_new", + "type": "address" + } + ], + "name": "setOwner", + "outputs": [], + "payable": false, + "type": "function" + }, + { + "constant": true, + "inputs": [], + "name": "totalSignatures", + "outputs": [ + { + "name": "", + "type": "uint256" + } + ], + "payable": false, + "type": "function" + }, + { + "constant": true, + "inputs": [], + "name": "owner", + "outputs": [ + { + "name": "", + "type": "address" + } + ], + "payable": false, + "type": "function" + }, + { + "constant": false, + "inputs": [], + "name": "drain", + "outputs": [], + "payable": false, + "type": "function" + }, + { + "constant": true, + "inputs": [ + { + "name": "", + "type": "bytes4" + } + ], + "name": "entries", + "outputs": [ + { + "name": "", + "type": "string" + } + ], + "payable": false, + "type": "function" + }, + { + "constant": false, + "inputs": [ + { + "name": "_method", + "type": "string" + } + ], + "name": "register", + "outputs": [ + { + "name": "", + "type": "bool" + } + ], + "payable": false, + "type": "function" + }, + { + "inputs": [], + "type": "constructor" + }, + { + "anonymous": false, + "inputs": [ + { + "indexed": true, + "name": "creator", + "type": "address" + }, + { + "indexed": true, + "name": "signature", + "type": "bytes4" + }, + { + "indexed": false, + "name": "method", + "type": "string" + } + ], + "name": "Registered", + "type": "event" + }, + { + "anonymous": false, + "inputs": [ + { + "indexed": true, + "name": "old", + "type": "address" + }, + { + "indexed": true, + "name": "current", + "type": "address" + } + ], + "name": "NewOwner", + "type": "event" + } +] diff --git a/js/src/contracts/abi/tokenreg.json b/js/src/contracts/abi/tokenreg.json new file mode 100644 index 000000000..e56a13eec --- /dev/null +++ b/js/src/contracts/abi/tokenreg.json @@ -0,0 +1 @@ +[{"constant":true,"inputs":[{"name":"_id","type":"uint256"}],"name":"token","outputs":[{"name":"addr","type":"address"},{"name":"tla","type":"string"},{"name":"base","type":"uint256"},{"name":"name","type":"string"},{"name":"owner","type":"address"}],"type":"function"},{"constant":false,"inputs":[{"name":"_new","type":"address"}],"name":"setOwner","outputs":[],"type":"function"},{"constant":false,"inputs":[{"name":"_addr","type":"address"},{"name":"_tla","type":"string"},{"name":"_base","type":"uint256"},{"name":"_name","type":"string"}],"name":"register","outputs":[{"name":"","type":"bool"}],"type":"function"},{"constant":false,"inputs":[{"name":"_fee","type":"uint256"}],"name":"setFee","outputs":[],"type":"function"},{"constant":true,"inputs":[{"name":"_id","type":"uint256"},{"name":"_key","type":"bytes32"}],"name":"meta","outputs":[{"name":"","type":"bytes32"}],"type":"function"},{"constant":false,"inputs":[{"name":"_addr","type":"address"},{"name":"_tla","type":"string"},{"name":"_base","type":"uint256"},{"name":"_name","type":"string"},{"name":"_owner","type":"address"}],"name":"registerAs","outputs":[{"name":"","type":"bool"}],"type":"function"},{"constant":true,"inputs":[{"name":"_tla","type":"string"}],"name":"fromTLA","outputs":[{"name":"id","type":"uint256"},{"name":"addr","type":"address"},{"name":"base","type":"uint256"},{"name":"name","type":"string"},{"name":"owner","type":"address"}],"type":"function"},{"constant":true,"inputs":[],"name":"owner","outputs":[{"name":"","type":"address"}],"type":"function"},{"constant":false,"inputs":[],"name":"drain","outputs":[],"type":"function"},{"constant":true,"inputs":[],"name":"tokenCount","outputs":[{"name":"","type":"uint256"}],"type":"function"},{"constant":false,"inputs":[{"name":"_id","type":"uint256"}],"name":"unregister","outputs":[],"type":"function"},{"constant":true,"inputs":[{"name":"_addr","type":"address"}],"name":"fromAddress","outputs":[{"name":"id","type":"uint256"},{"name":"tla","type":"string"},{"name":"base","type":"uint256"},{"name":"name","type":"string"},{"name":"owner","type":"address"}],"type":"function"},{"constant":false,"inputs":[{"name":"_id","type":"uint256"},{"name":"_key","type":"bytes32"},{"name":"_value","type":"bytes32"}],"name":"setMeta","outputs":[],"type":"function"},{"constant":true,"inputs":[],"name":"fee","outputs":[{"name":"","type":"uint256"}],"type":"function"},{"anonymous":false,"inputs":[{"indexed":true,"name":"tla","type":"string"},{"indexed":true,"name":"id","type":"uint256"},{"indexed":false,"name":"addr","type":"address"},{"indexed":false,"name":"name","type":"string"}],"name":"Registered","type":"event"},{"anonymous":false,"inputs":[{"indexed":true,"name":"tla","type":"string"},{"indexed":true,"name":"id","type":"uint256"}],"name":"Unregistered","type":"event"},{"anonymous":false,"inputs":[{"indexed":true,"name":"id","type":"uint256"},{"indexed":true,"name":"key","type":"bytes32"},{"indexed":false,"name":"value","type":"bytes32"}],"name":"MetaChanged","type":"event"},{"anonymous":false,"inputs":[{"indexed":true,"name":"old","type":"address"},{"indexed":true,"name":"current","type":"address"}],"name":"NewOwner","type":"event"}] diff --git a/js/src/contracts/contracts.js b/js/src/contracts/contracts.js new file mode 100644 index 000000000..c9d1c2390 --- /dev/null +++ b/js/src/contracts/contracts.js @@ -0,0 +1,58 @@ +// Copyright 2015, 2016 Ethcore (UK) Ltd. +// This file is part of Parity. + +// Parity is free software: you can redistribute it and/or modify +// it under the terms of the GNU General Public License as published by +// the Free Software Foundation, either version 3 of the License, or +// (at your option) any later version. + +// Parity is distributed in the hope that it will be useful, +// but WITHOUT ANY WARRANTY; without even the implied warranty of +// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the +// GNU General Public License for more details. + +// You should have received a copy of the GNU General Public License +// along with Parity. If not, see . + +import DappReg from './dappreg'; +import Registry from './registry'; +import SignatureReg from './signaturereg'; +import TokenReg from './tokenreg'; + +let instance = null; + +export default class Contracts { + constructor (api) { + instance = this; + + this._api = api; + this._registry = new Registry(api); + this._dappreg = new DappReg(api, this._registry); + this._signaturereg = new SignatureReg(api, this._registry); + this._tokenreg = new TokenReg(api, this._registry); + } + + get registry () { + return this._registry; + } + + get dappReg () { + return this._dappreg; + } + + get signatureReg () { + return this._signaturereg; + } + + get tokenReg () { + return this._tokenreg; + } + + static create (api) { + return new Contracts(api); + } + + static get () { + return instance; + } +} diff --git a/js/src/contracts/dappreg.js b/js/src/contracts/dappreg.js new file mode 100644 index 000000000..ef07e95c5 --- /dev/null +++ b/js/src/contracts/dappreg.js @@ -0,0 +1,72 @@ +// Copyright 2015, 2016 Ethcore (UK) Ltd. +// This file is part of Parity. + +// Parity is free software: you can redistribute it and/or modify +// it under the terms of the GNU General Public License as published by +// the Free Software Foundation, either version 3 of the License, or +// (at your option) any later version. + +// Parity is distributed in the hope that it will be useful, +// but WITHOUT ANY WARRANTY; without even the implied warranty of +// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the +// GNU General Public License for more details. + +// You should have received a copy of the GNU General Public License +// along with Parity. If not, see . + +// Copyright 2015, 2016 Ethcore (UK) Ltd. +// This file is part of Parity. + +// Parity is free software: you can redistribute it and/or modify +// it under the terms of the GNU General Public License as published by +// the Free Software Foundation, either version 3 of the License, or +// (at your option) any later version. + +// Parity is distributed in the hope that it will be useful, +// but WITHOUT ANY WARRANTY; without even the implied warranty of +// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the +// GNU General Public License for more details. + +// You should have received a copy of the GNU General Public License +// along with Parity. If not, see . + +export default class DappReg { + constructor (api, registry) { + this._api = api; + this._registry = registry; + + this.getInstance(); + } + + getInstance () { + return this._registry.getContractInstance('dappreg'); + } + + count () { + return this.getInstance().then((instance) => { + return instance.count.call(); + }); + } + + at (index) { + return this.getInstance().then((instance) => { + return instance.at.call({}, [index]); + }); + } + + get (id) { + return this.getInstance().then((instance) => { + return instance.get.call({}, [id]); + }); + } + + meta (id, key) { + return this.getInstance().then((instance) => { + return instance.meta.call({}, [id, key]); + }); + } + + getImage (id) { + return this.meta(id, 'IMG'); + } +} diff --git a/js/src/contracts/index.js b/js/src/contracts/index.js new file mode 100644 index 000000000..075837ce6 --- /dev/null +++ b/js/src/contracts/index.js @@ -0,0 +1,17 @@ +// Copyright 2015, 2016 Ethcore (UK) Ltd. +// This file is part of Parity. + +// Parity is free software: you can redistribute it and/or modify +// it under the terms of the GNU General Public License as published by +// the Free Software Foundation, either version 3 of the License, or +// (at your option) any later version. + +// Parity is distributed in the hope that it will be useful, +// but WITHOUT ANY WARRANTY; without even the implied warranty of +// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the +// GNU General Public License for more details. + +// You should have received a copy of the GNU General Public License +// along with Parity. If not, see . + +export default from './contracts'; diff --git a/js/src/contracts/registry.js b/js/src/contracts/registry.js new file mode 100644 index 000000000..85b9d6bb5 --- /dev/null +++ b/js/src/contracts/registry.js @@ -0,0 +1,76 @@ +// Copyright 2015, 2016 Ethcore (UK) Ltd. +// This file is part of Parity. + +// Parity is free software: you can redistribute it and/or modify +// it under the terms of the GNU General Public License as published by +// the Free Software Foundation, either version 3 of the License, or +// (at your option) any later version. + +// Parity is distributed in the hope that it will be useful, +// but WITHOUT ANY WARRANTY; without even the implied warranty of +// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the +// GNU General Public License for more details. + +// You should have received a copy of the GNU General Public License +// along with Parity. If not, see . + +import * as abis from './abi'; + +export default class Registry { + constructor (api) { + this._api = api; + this._contracts = []; + this._instance = null; + + this.getInstance(); + } + + getInstance () { + return new Promise((resolve, reject) => { + if (this._instance) { + resolve(this._instance); + return; + } + + this._api.ethcore + .registryAddress() + .then((address) => { + this._instance = this._api.newContract(abis.registry, address).instance; + resolve(this._instance); + }) + .catch(reject); + }); + } + + getContractInstance (_name) { + const name = _name.toLowerCase(); + + return new Promise((resolve, reject) => { + if (this._contracts[name]) { + resolve(this._contracts[name]); + return; + } + + this + .lookupAddress(name) + .then((address) => { + this._contracts[name] = this._api.newContract(abis[name], address).instance; + resolve(this._contracts[name]); + }) + .catch(reject); + }); + } + + lookupAddress (_name) { + const name = _name.toLowerCase(); + const sha3 = this._api.util.sha3(name); + + return this.getInstance().then((instance) => { + return instance.getAddress.call({}, [sha3, 'A']); + }) + .then((address) => { + console.log('lookupAddress', name, sha3, address); + return address; + }); + } +} diff --git a/js/src/contracts/signaturereg.js b/js/src/contracts/signaturereg.js new file mode 100644 index 000000000..459f165b1 --- /dev/null +++ b/js/src/contracts/signaturereg.js @@ -0,0 +1,34 @@ +// Copyright 2015, 2016 Ethcore (UK) Ltd. +// This file is part of Parity. + +// Parity is free software: you can redistribute it and/or modify +// it under the terms of the GNU General Public License as published by +// the Free Software Foundation, either version 3 of the License, or +// (at your option) any later version. + +// Parity is distributed in the hope that it will be useful, +// but WITHOUT ANY WARRANTY; without even the implied warranty of +// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the +// GNU General Public License for more details. + +// You should have received a copy of the GNU General Public License +// along with Parity. If not, see . + +export default class SignatureReg { + constructor (api, registry) { + this._api = api; + this._registry = registry; + + this.getInstance(); + } + + getInstance () { + return this._registry.getContractInstance('signaturereg'); + } + + lookup (signature) { + return this.getInstance().then((instance) => { + return instance.entries.call({}, [signature]); + }); + } +} diff --git a/js/src/contracts/tokenreg.js b/js/src/contracts/tokenreg.js new file mode 100644 index 000000000..78bd5e4d9 --- /dev/null +++ b/js/src/contracts/tokenreg.js @@ -0,0 +1,40 @@ +// Copyright 2015, 2016 Ethcore (UK) Ltd. +// This file is part of Parity. + +// Parity is free software: you can redistribute it and/or modify +// it under the terms of the GNU General Public License as published by +// the Free Software Foundation, either version 3 of the License, or +// (at your option) any later version. + +// Parity is distributed in the hope that it will be useful, +// but WITHOUT ANY WARRANTY; without even the implied warranty of +// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the +// GNU General Public License for more details. + +// You should have received a copy of the GNU General Public License +// along with Parity. If not, see . + +export default class TokenReg { + constructor (api, registry) { + this._api = api; + this._registry = registry; + + this.getInstance(); + } + + getInstance () { + return this._registry.getContractInstance('tokenreg'); + } + + tokenCount () { + return this.getInstance().then((instance) => { + return instance.tokenCount.call(); + }); + } + + token (index) { + return this.getInstance().then((instance) => { + return instance.token.call({}, [index]); + }); + } +} diff --git a/js/src/dapps/basiccoin.html b/js/src/dapps/basiccoin.html new file mode 100644 index 000000000..9bcc368f3 --- /dev/null +++ b/js/src/dapps/basiccoin.html @@ -0,0 +1,16 @@ + + + + + + + Basic Token Deployment + + +
+ + + + + + diff --git a/js/src/dapps/basiccoin.js b/js/src/dapps/basiccoin.js new file mode 100644 index 000000000..e02990d14 --- /dev/null +++ b/js/src/dapps/basiccoin.js @@ -0,0 +1,47 @@ +// Copyright 2015, 2016 Ethcore (UK) Ltd. +// This file is part of Parity. + +// Parity is free software: you can redistribute it and/or modify +// it under the terms of the GNU General Public License as published by +// the Free Software Foundation, either version 3 of the License, or +// (at your option) any later version. + +// Parity is distributed in the hope that it will be useful, +// but WITHOUT ANY WARRANTY; without even the implied warranty of +// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the +// GNU General Public License for more details. + +// You should have received a copy of the GNU General Public License +// along with Parity. If not, see . + +import ReactDOM from 'react-dom'; +import React from 'react'; +import { createHashHistory } from 'history'; +import { Redirect, Router, Route, useRouterHistory } from 'react-router'; + +import injectTapEventPlugin from 'react-tap-event-plugin'; +injectTapEventPlugin(); + +import Deploy from './basiccoin/Deploy'; +import Application from './basiccoin/Application'; +import Overview from './basiccoin/Overview'; +import Transfer from './basiccoin/Transfer'; + +const routerHistory = useRouterHistory(createHashHistory)({}); + +import '../../assets/fonts/Roboto/font.css'; +import '../../assets/fonts/RobotoMono/font.css'; +import './style.css'; +import './basiccoin.html'; + +ReactDOM.render( + + + + + + + + , + document.querySelector('#container') +); diff --git a/js/src/dapps/basiccoin/AddressSelect/addressSelect.css b/js/src/dapps/basiccoin/AddressSelect/addressSelect.css new file mode 100644 index 000000000..ddfd334e8 --- /dev/null +++ b/js/src/dapps/basiccoin/AddressSelect/addressSelect.css @@ -0,0 +1,23 @@ +/* Copyright 2015, 2016 Ethcore (UK) Ltd. +/* This file is part of Parity. +/* +/* Parity is free software: you can redistribute it and/or modify +/* it under the terms of the GNU General Public License as published by +/* the Free Software Foundation, either version 3 of the License, or +/* (at your option) any later version. +/* +/* Parity is distributed in the hope that it will be useful, +/* but WITHOUT ANY WARRANTY; without even the implied warranty of +/* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the +/* GNU General Public License for more details. +/* +/* You should have received a copy of the GNU General Public License +/* along with Parity. If not, see . +*/ + +.iconMenu { +} + +.iconMenu option { + padding-left: 30px; +} diff --git a/js/src/dapps/basiccoin/AddressSelect/addressSelect.js b/js/src/dapps/basiccoin/AddressSelect/addressSelect.js new file mode 100644 index 000000000..529e7753d --- /dev/null +++ b/js/src/dapps/basiccoin/AddressSelect/addressSelect.js @@ -0,0 +1,93 @@ +// Copyright 2015, 2016 Ethcore (UK) Ltd. +// This file is part of Parity. + +// Parity is free software: you can redistribute it and/or modify +// it under the terms of the GNU General Public License as published by +// the Free Software Foundation, either version 3 of the License, or +// (at your option) any later version. + +// Parity is distributed in the hope that it will be useful, +// but WITHOUT ANY WARRANTY; without even the implied warranty of +// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the +// GNU General Public License for more details. + +// You should have received a copy of the GNU General Public License +// along with Parity. If not, see . + +import React, { Component, PropTypes } from 'react'; + +import { api } from '../parity'; +import styles from './addressSelect.css'; + +export default class AddressSelect extends Component { + static contextTypes = { + accounts: PropTypes.object.isRequired + } + + static propTypes = { + addresses: PropTypes.array.isRequired, + onChange: PropTypes.func.isRequired + } + + state = { + selected: null + } + + componentDidMount () { + const { addresses } = this.props; + + this.onChange({ + target: { + value: addresses[0] + } + }); + } + + componentWillReceiveProps (newProps) { + const { addresses } = this.props; + let changed = addresses.length !== newProps.addresses.length; + + if (!changed) { + changed = addresses.filter((address, index) => newProps.addresses[index] !== address).length; + } + + if (changed) { + this.onChange({ target: { value: newProps.addresses[0] } }); + } + } + + render () { + const { addresses } = this.props; + const { selectedAddress } = this.state; + const style = { + background: `rgba(255, 255, 255, 0.75) url(${api.util.createIdentityImg(selectedAddress, 3)}) no-repeat 98% center` + }; + + return ( + + ); + } + + renderOption = (address) => { + const { accounts } = this.context; + const account = accounts[address]; + + return ( + + ); + } + + onChange = (event) => { + this.setState({ selectedAddress: event.target.value }); + this.props.onChange(event); + } +} diff --git a/js/src/dapps/basiccoin/AddressSelect/index.js b/js/src/dapps/basiccoin/AddressSelect/index.js new file mode 100644 index 000000000..58059cd2e --- /dev/null +++ b/js/src/dapps/basiccoin/AddressSelect/index.js @@ -0,0 +1,17 @@ +// Copyright 2015, 2016 Ethcore (UK) Ltd. +// This file is part of Parity. + +// Parity is free software: you can redistribute it and/or modify +// it under the terms of the GNU General Public License as published by +// the Free Software Foundation, either version 3 of the License, or +// (at your option) any later version. + +// Parity is distributed in the hope that it will be useful, +// but WITHOUT ANY WARRANTY; without even the implied warranty of +// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the +// GNU General Public License for more details. + +// You should have received a copy of the GNU General Public License +// along with Parity. If not, see . + +export default from './addressSelect'; diff --git a/js/src/dapps/basiccoin/Application/Header/header.css b/js/src/dapps/basiccoin/Application/Header/header.css new file mode 100644 index 000000000..5416d7c05 --- /dev/null +++ b/js/src/dapps/basiccoin/Application/Header/header.css @@ -0,0 +1,71 @@ +/* Copyright 2015, 2016 Ethcore (UK) Ltd. +/* This file is part of Parity. +/* +/* Parity is free software: you can redistribute it and/or modify +/* it under the terms of the GNU General Public License as published by +/* the Free Software Foundation, either version 3 of the License, or +/* (at your option) any later version. +/* +/* Parity is distributed in the hope that it will be useful, +/* but WITHOUT ANY WARRANTY; without even the implied warranty of +/* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the +/* GNU General Public License for more details. +/* +/* You should have received a copy of the GNU General Public License +/* along with Parity. If not, see . +*/ + +.header { +} + +.titlebar { + padding: 0.5em 1em; + margin: 0; + color: white; +} + +.navigation { + table-layout: fixed; + width: 100%; + border-spacing: 0.25em; + border-collapse: separate; + border-color: white; + background: white; +} + +.navigation tr { + height: 10em; +} + +.title { + font-size: 1.25em; + margin-bottom: 0.25em; +} + +.byline { + font-size: 1em; + opacity: 0.75; + margin-bottom: 0.25em; +} + +.description { + font-size: 0.5em; + opacity: 0.5; + line-height: 1.5em; +} + +.navNext, +.navCurrent { + color: white; + padding: 1em 2em; + vertical-align: middle; +} + +.navNext:hover { + cursor: pointer; + opacity: 0.8; +} + +.navCurrent { + font-size: 2em; +} diff --git a/js/src/dapps/basiccoin/Application/Header/header.js b/js/src/dapps/basiccoin/Application/Header/header.js new file mode 100644 index 000000000..90fa909ef --- /dev/null +++ b/js/src/dapps/basiccoin/Application/Header/header.js @@ -0,0 +1,80 @@ +// Copyright 2015, 2016 Ethcore (UK) Ltd. +// This file is part of Parity. + +// Parity is free software: you can redistribute it and/or modify +// it under the terms of the GNU General Public License as published by +// the Free Software Foundation, either version 3 of the License, or +// (at your option) any later version. + +// Parity is distributed in the hope that it will be useful, +// but WITHOUT ANY WARRANTY; without even the implied warranty of +// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the +// GNU General Public License for more details. + +// You should have received a copy of the GNU General Public License +// along with Parity. If not, see . + +import React, { Component, PropTypes } from 'react'; + +import PAGES from '../pages'; +import styles from './header.css'; + +export default class Header extends Component { + static contextTypes = { + router: PropTypes.object.isRequired + } + + render () { + const path = (window.location.hash || '').split('?')[0].split('/')[1]; + const offset = PAGES.findIndex((header) => header.path === path); + + return ( +
+ + + + { this.renderHeader(0, offset) } + { this.renderHeader(1, offset) } + + + { this.renderHeader(2, offset) } + + +
+
+ ); + } + + renderHeader (position, offset) { + const index = (position + offset) % PAGES.length; + const page = PAGES[index]; + const background = `rgba(102, 34, 34, ${1 - (0.1 * position)})`; + + return ( + +
+ { page.title } +
+
+ { page.byline } +
+
+ { position ? null : page.description } +
+ + ); + } + + onNavigate = (route) => { + const { router } = this.context; + + return (event) => { + router.push(`/${route}`); + }; + } +} diff --git a/js/src/dapps/basiccoin/Application/Header/index.js b/js/src/dapps/basiccoin/Application/Header/index.js new file mode 100644 index 000000000..4a5121906 --- /dev/null +++ b/js/src/dapps/basiccoin/Application/Header/index.js @@ -0,0 +1,17 @@ +// Copyright 2015, 2016 Ethcore (UK) Ltd. +// This file is part of Parity. + +// Parity is free software: you can redistribute it and/or modify +// it under the terms of the GNU General Public License as published by +// the Free Software Foundation, either version 3 of the License, or +// (at your option) any later version. + +// Parity is distributed in the hope that it will be useful, +// but WITHOUT ANY WARRANTY; without even the implied warranty of +// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the +// GNU General Public License for more details. + +// You should have received a copy of the GNU General Public License +// along with Parity. If not, see . + +export default from './header'; diff --git a/js/src/dapps/basiccoin/Application/Loading/index.js b/js/src/dapps/basiccoin/Application/Loading/index.js new file mode 100644 index 000000000..0468cab37 --- /dev/null +++ b/js/src/dapps/basiccoin/Application/Loading/index.js @@ -0,0 +1,17 @@ +// Copyright 2015, 2016 Ethcore (UK) Ltd. +// This file is part of Parity. + +// Parity is free software: you can redistribute it and/or modify +// it under the terms of the GNU General Public License as published by +// the Free Software Foundation, either version 3 of the License, or +// (at your option) any later version. + +// Parity is distributed in the hope that it will be useful, +// but WITHOUT ANY WARRANTY; without even the implied warranty of +// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the +// GNU General Public License for more details. + +// You should have received a copy of the GNU General Public License +// along with Parity. If not, see . + +export default from './loading'; diff --git a/js/src/dapps/basiccoin/Application/Loading/loading.css b/js/src/dapps/basiccoin/Application/Loading/loading.css new file mode 100644 index 000000000..915cc77dc --- /dev/null +++ b/js/src/dapps/basiccoin/Application/Loading/loading.css @@ -0,0 +1,24 @@ +/* Copyright 2015, 2016 Ethcore (UK) Ltd. +/* This file is part of Parity. +/* +/* Parity is free software: you can redistribute it and/or modify +/* it under the terms of the GNU General Public License as published by +/* the Free Software Foundation, either version 3 of the License, or +/* (at your option) any later version. +/* +/* Parity is distributed in the hope that it will be useful, +/* but WITHOUT ANY WARRANTY; without even the implied warranty of +/* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the +/* GNU General Public License for more details. +/* +/* You should have received a copy of the GNU General Public License +/* along with Parity. If not, see . +*/ + +.body { + width: 100%; + text-align: center; + padding-top: 5em; + font-size: 2em; + color: #999; +} diff --git a/js/src/dapps/basiccoin/Application/Loading/loading.js b/js/src/dapps/basiccoin/Application/Loading/loading.js new file mode 100644 index 000000000..e698a0e80 --- /dev/null +++ b/js/src/dapps/basiccoin/Application/Loading/loading.js @@ -0,0 +1,29 @@ +// Copyright 2015, 2016 Ethcore (UK) Ltd. +// This file is part of Parity. + +// Parity is free software: you can redistribute it and/or modify +// it under the terms of the GNU General Public License as published by +// the Free Software Foundation, either version 3 of the License, or +// (at your option) any later version. + +// Parity is distributed in the hope that it will be useful, +// but WITHOUT ANY WARRANTY; without even the implied warranty of +// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the +// GNU General Public License for more details. + +// You should have received a copy of the GNU General Public License +// along with Parity. If not, see . + +import React, { Component } from 'react'; + +import styles from './loading.css'; + +export default class Loading extends Component { + render () { + return ( +
+ Attaching to contract ... +
+ ); + } +} diff --git a/js/src/dapps/basiccoin/Application/application.css b/js/src/dapps/basiccoin/Application/application.css new file mode 100644 index 000000000..97b66be87 --- /dev/null +++ b/js/src/dapps/basiccoin/Application/application.css @@ -0,0 +1,28 @@ +/* Copyright 2015, 2016 Ethcore (UK) Ltd. +/* This file is part of Parity. +/* +/* Parity is free software: you can redistribute it and/or modify +/* it under the terms of the GNU General Public License as published by +/* the Free Software Foundation, either version 3 of the License, or +/* (at your option) any later version. +/* +/* Parity is distributed in the hope that it will be useful, +/* but WITHOUT ANY WARRANTY; without even the implied warranty of +/* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the +/* GNU General Public License for more details. +/* +/* You should have received a copy of the GNU General Public License +/* along with Parity. If not, see . +*/ + +.container { + color: #444; + font-family: 'Roboto'; + vertical-align: middle; + min-height: 100vh; + position:relative; +} + +.body { + padding: 0 0.25em; +} diff --git a/js/src/dapps/basiccoin/Application/application.js b/js/src/dapps/basiccoin/Application/application.js new file mode 100644 index 000000000..1e268e720 --- /dev/null +++ b/js/src/dapps/basiccoin/Application/application.js @@ -0,0 +1,107 @@ +// Copyright 2015, 2016 Ethcore (UK) Ltd. +// This file is part of Parity. + +// Parity is free software: you can redistribute it and/or modify +// it under the terms of the GNU General Public License as published by +// the Free Software Foundation, either version 3 of the License, or +// (at your option) any later version. + +// Parity is distributed in the hope that it will be useful, +// but WITHOUT ANY WARRANTY; without even the implied warranty of +// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the +// GNU General Public License for more details. + +// You should have received a copy of the GNU General Public License +// along with Parity. If not, see . + +import React, { Component, PropTypes } from 'react'; + +import { api } from '../parity'; +import { attachInstances } from '../services'; + +import Header from './Header'; +import Loading from './Loading'; + +import styles from './application.css'; + +export default class Application extends Component { + static childContextTypes = { + accounts: PropTypes.object, + managerInstance: PropTypes.object, + registryInstance: PropTypes.object, + tokenregInstance: PropTypes.object + } + + static propTypes = { + children: PropTypes.node.isRequired + } + + state = { + accounts: null, + loading: true, + managerInstance: null, + registryInstance: null, + tokenregInstance: null + } + + componentDidMount () { + this.attachInstance(); + } + + render () { + const { children } = this.props; + const { loading } = this.state; + + if (loading) { + return ( + + ); + } + + return ( +
+
+
+ { children } +
+
+ ); + } + + getChildContext () { + const { accounts, managerInstance, registryInstance, tokenregInstance } = this.state; + + return { + accounts, + managerInstance, + registryInstance, + tokenregInstance + }; + } + + attachInstance () { + Promise + .all([ + attachInstances(), + api.personal.accountsInfo() + ]) + .then(([{ managerInstance, registryInstance, tokenregInstance }, accountsInfo]) => { + this.setState({ + loading: false, + managerInstance, + registryInstance, + tokenregInstance, + accounts: Object + .keys(accountsInfo) + .filter((address) => !accountsInfo[address].meta.deleted) + .sort((a, b) => { + return (accountsInfo[b].uuid || '').localeCompare(accountsInfo[a].uuid || ''); + }) + .reduce((accounts, address) => { + accounts[address] = Object.assign(accountsInfo[address], { address }); + return accounts; + }, {}) + }); + }); + } +} diff --git a/js/src/dapps/basiccoin/Application/index.js b/js/src/dapps/basiccoin/Application/index.js new file mode 100644 index 000000000..236578226 --- /dev/null +++ b/js/src/dapps/basiccoin/Application/index.js @@ -0,0 +1,17 @@ +// Copyright 2015, 2016 Ethcore (UK) Ltd. +// This file is part of Parity. + +// Parity is free software: you can redistribute it and/or modify +// it under the terms of the GNU General Public License as published by +// the Free Software Foundation, either version 3 of the License, or +// (at your option) any later version. + +// Parity is distributed in the hope that it will be useful, +// but WITHOUT ANY WARRANTY; without even the implied warranty of +// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the +// GNU General Public License for more details. + +// You should have received a copy of the GNU General Public License +// along with Parity. If not, see . + +export default from './application'; diff --git a/js/src/dapps/basiccoin/Application/pages.js b/js/src/dapps/basiccoin/Application/pages.js new file mode 100644 index 000000000..5ab422ee4 --- /dev/null +++ b/js/src/dapps/basiccoin/Application/pages.js @@ -0,0 +1,38 @@ +// Copyright 2015, 2016 Ethcore (UK) Ltd. +// This file is part of Parity. + +// Parity is free software: you can redistribute it and/or modify +// it under the terms of the GNU General Public License as published by +// the Free Software Foundation, either version 3 of the License, or +// (at your option) any later version. + +// Parity is distributed in the hope that it will be useful, +// but WITHOUT ANY WARRANTY; without even the implied warranty of +// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the +// GNU General Public License for more details. + +// You should have received a copy of the GNU General Public License +// along with Parity. If not, see . + +const PAGES = [ + { + path: 'overview', + title: 'Overview', + byline: 'Display all the current information relating to your own deployed tokens', + description: 'View the total number of tokens in circulation, the number of different tokens associated with your accounts as well as the types of tokens created by you. In addition view the balances associated with your accounts in reltion to the total in circulation.' + }, + { + path: 'transfer', + title: 'Transfer', + byline: 'Send tokens associated with your accounts to other addresses', + description: 'Send any tokens created byt you or received from others. In addition have a bird\'s eye view of all events relating to token transfers, be it yours, created byt others, either local or globally available on the network.' + }, + { + path: 'deploy', + title: 'Deploy', + byline: 'Deploy a new token to the network', + description: 'Token registration has never been this easy. Select the name for your token, the TLA and the number of tokens in circulation. Start sending the tokens to contacts right from this interface. Optionally you can register the token with the Token Registry which would allow you to transaction in tokens from anywhere these transactions are allowed.' + } +]; + +export default PAGES; diff --git a/js/src/dapps/basiccoin/Container/container.css b/js/src/dapps/basiccoin/Container/container.css new file mode 100644 index 000000000..7cc00e11e --- /dev/null +++ b/js/src/dapps/basiccoin/Container/container.css @@ -0,0 +1,26 @@ +/* Copyright 2015, 2016 Ethcore (UK) Ltd. +/* This file is part of Parity. +/* +/* Parity is free software: you can redistribute it and/or modify +/* it under the terms of the GNU General Public License as published by +/* the Free Software Foundation, either version 3 of the License, or +/* (at your option) any later version. +/* +/* Parity is distributed in the hope that it will be useful, +/* but WITHOUT ANY WARRANTY; without even the implied warranty of +/* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the +/* GNU General Public License for more details. +/* +/* You should have received a copy of the GNU General Public License +/* along with Parity. If not, see . +*/ + +.content { + padding: 4em; + margin-bottom: 0.25em; + text-align: center; +} + +.content:nth-child(odd) { + background: rgba(102, 34, 34, 0.075); +} diff --git a/js/src/dapps/basiccoin/Container/container.js b/js/src/dapps/basiccoin/Container/container.js new file mode 100644 index 000000000..82805e71b --- /dev/null +++ b/js/src/dapps/basiccoin/Container/container.js @@ -0,0 +1,37 @@ +// Copyright 2015, 2016 Ethcore (UK) Ltd. +// This file is part of Parity. + +// Parity is free software: you can redistribute it and/or modify +// it under the terms of the GNU General Public License as published by +// the Free Software Foundation, either version 3 of the License, or +// (at your option) any later version. + +// Parity is distributed in the hope that it will be useful, +// but WITHOUT ANY WARRANTY; without even the implied warranty of +// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the +// GNU General Public License for more details. + +// You should have received a copy of the GNU General Public License +// along with Parity. If not, see . + +import React, { Component, PropTypes } from 'react'; + +import styles from './container.css'; + +export default class Container extends Component { + static propTypes = { + className: PropTypes.string, + children: PropTypes.node.isRequired + } + + render () { + const { className, children } = this.props; + const classes = `${styles.content} ${className}`; + + return ( +
+ { children } +
+ ); + } +} diff --git a/js/src/dapps/basiccoin/Container/index.js b/js/src/dapps/basiccoin/Container/index.js new file mode 100644 index 000000000..87fbc567e --- /dev/null +++ b/js/src/dapps/basiccoin/Container/index.js @@ -0,0 +1,17 @@ +// Copyright 2015, 2016 Ethcore (UK) Ltd. +// This file is part of Parity. + +// Parity is free software: you can redistribute it and/or modify +// it under the terms of the GNU General Public License as published by +// the Free Software Foundation, either version 3 of the License, or +// (at your option) any later version. + +// Parity is distributed in the hope that it will be useful, +// but WITHOUT ANY WARRANTY; without even the implied warranty of +// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the +// GNU General Public License for more details. + +// You should have received a copy of the GNU General Public License +// along with Parity. If not, see . + +export default from './container'; diff --git a/js/src/dapps/basiccoin/Deploy/Deployment/deployment.css b/js/src/dapps/basiccoin/Deploy/Deployment/deployment.css new file mode 100644 index 000000000..f24729cf7 --- /dev/null +++ b/js/src/dapps/basiccoin/Deploy/Deployment/deployment.css @@ -0,0 +1,19 @@ +/* Copyright 2015, 2016 Ethcore (UK) Ltd. +/* This file is part of Parity. +/* +/* Parity is free software: you can redistribute it and/or modify +/* it under the terms of the GNU General Public License as published by +/* the Free Software Foundation, either version 3 of the License, or +/* (at your option) any later version. +/* +/* Parity is distributed in the hope that it will be useful, +/* but WITHOUT ANY WARRANTY; without even the implied warranty of +/* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the +/* GNU General Public License for more details. +/* +/* You should have received a copy of the GNU General Public License +/* along with Parity. If not, see . +*/ + +@import '../../_form.css'; +@import '../../_status.css'; diff --git a/js/src/dapps/basiccoin/Deploy/Deployment/deployment.js b/js/src/dapps/basiccoin/Deploy/Deployment/deployment.js new file mode 100644 index 000000000..f9232789b --- /dev/null +++ b/js/src/dapps/basiccoin/Deploy/Deployment/deployment.js @@ -0,0 +1,320 @@ +// Copyright 2015, 2016 Ethcore (UK) Ltd. +// This file is part of Parity. + +// Parity is free software: you can redistribute it and/or modify +// it under the terms of the GNU General Public License as published by +// the Free Software Foundation, either version 3 of the License, or +// (at your option) any later version. + +// Parity is distributed in the hope that it will be useful, +// but WITHOUT ANY WARRANTY; without even the implied warranty of +// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the +// GNU General Public License for more details. + +// You should have received a copy of the GNU General Public License +// along with Parity. If not, see . + +import React, { Component, PropTypes } from 'react'; + +import { api } from '../../parity'; +import AddressSelect from '../../AddressSelect'; +import Container from '../../Container'; +import styles from './deployment.css'; + +const ERRORS = { + name: 'specify a valid name >2 & <32 characters', + tla: 'specify a valid TLA, 3 characters in length', + usedtla: 'the TLA used is not available for registration', + supply: 'supply needs to be valid >999 & <1 trillion' +}; + +export default class Deployment extends Component { + static contextTypes = { + accounts: PropTypes.object.isRequired, + router: PropTypes.object.isRequired, + managerInstance: PropTypes.object.isRequired, + registryInstance: PropTypes.object.isRequired, + tokenregInstance: PropTypes.object.isRequired + } + + state = { + base: null, + deployBusy: false, + deployDone: false, + deployError: null, + deployState: null, + globalReg: false, + globalFee: 0, + globalFeeText: '1.000', + fromAddress: null, + name: '', + nameError: ERRORS.name, + tla: '', + tlaError: ERRORS.tla, + totalSupply: '5000000', + totalSupplyError: null, + signerRequestId: null, + txHash: null + } + + componentDidMount () { + const { managerInstance, tokenregInstance } = this.context; + + Promise + .all([ + managerInstance.base.call(), + tokenregInstance.fee.call() + ]) + .then(([base, globalFee]) => { + this.setState({ + base, + baseText: base.toFormat(0), + globalFee, + globalFeeText: api.util.fromWei(globalFee).toFormat(3) + }); + }); + } + + render () { + const { deployBusy } = this.state; + + return deployBusy + ? this.renderDeploying() + : this.renderForm(); + } + + renderDeploying () { + const { deployDone, deployError, deployState } = this.state; + + if (deployDone) { + return ( + +
+ Your token has been deployed +
+
+ ); + } + + if (deployError) { + return ( + +
+ Your deployment has encountered an error +
+
+ { deployError } +
+
+ ); + } + + return ( + +
+ Your token is currently being deployed to the network +
+
+ { deployState } +
+
+ ); + } + + renderForm () { + const { accounts } = this.context; + const { baseText, name, nameError, tla, tlaError, totalSupply, totalSupplyError } = this.state; + const hasError = !!(nameError || tlaError || totalSupplyError); + const error = `${styles.input} ${styles.error}`; + const addresses = Object.keys(accounts).filter((address) => accounts[address].uuid); + + //
+ // + // + //
+ // register on network (fee: { globalFeeText }ETH) + //
+ //
+ + return ( + +
+
+ + +
+ the owner account to eploy from +
+
+
+ + +
+ { nameError || 'an identifying name for the token' } +
+
+
+ + +
+ { tlaError || 'unique network acronym for this token' } +
+
+
+ + +
+ { totalSupplyError || `number of tokens (base: ${baseText})` } +
+
+
+
+
+
+ ); + } + + onChangeFrom = (event) => { + const fromAddress = event.target.value; + + this.setState({ fromAddress }); + } + + onChangeName = (event) => { + const name = event.target.value; + const nameError = name && (name.length > 2) && (name.length < 32) + ? null + : ERRORS.name; + + this.setState({ name, nameError }); + } + + onChangeRegistrar = (event) => { + this.setState({ globalReg: event.target.value === 'yes' }, this.testTlaAvailability); + } + + onChangeSupply = (event) => { + const totalSupply = parseInt(event.target.value, 10); + const totalSupplyError = isFinite(totalSupply) && totalSupply > 999 + ? null + : ERRORS.supply; + + this.setState({ totalSupply, totalSupplyError }); + } + + onChangeTla = (event) => { + const _tla = event.target.value; + const tla = _tla && (_tla.length > 3) + ? _tla.substr(0, 3) + : _tla; + const tlaError = tla && (tla.length === 3) + ? null + : ERRORS.tla; + + this.setState({ tla, tlaError }, this.testTlaAvailability); + } + + testTlaAvailability = () => { + const { registryInstance, tokenregInstance } = this.context; + const { globalReg, tla, tlaError } = this.state; + const tokenreg = globalReg ? tokenregInstance : registryInstance; + + if (tlaError && tlaError !== ERRORS.usedtla) { + return; + } + + tokenreg + .fromTLA.call({}, [tla]) + .then(([id, addr, base, name, owner]) => { + if (owner !== '0x0000000000000000000000000000000000000000') { + this.setState({ tlaError: ERRORS.usedtla }); + } else if (tlaError === ERRORS.usedtla) { + this.setState({ tlaError: null }); + } + }) + .catch((error) => { + console.log('testTlaAvailability', error); + }); + } + + onDeploy = () => { + const { managerInstance, registryInstance, tokenregInstance } = this.context; + const { base, deployBusy, fromAddress, globalReg, globalFee, name, nameError, tla, tlaError, totalSupply, totalSupplyError } = this.state; + const hasError = !!(nameError || tlaError || totalSupplyError); + + if (hasError || deployBusy) { + return; + } + + const tokenreg = (globalReg ? tokenregInstance : registryInstance).address; + const values = [base.mul(totalSupply), tla, name, tokenreg]; + const options = { + from: fromAddress, + value: globalReg ? globalFee : 0 + }; + + this.setState({ deployBusy: true, deployState: 'Estimating gas for the transaction' }); + + managerInstance + .deploy.estimateGas(options, values) + .then((gas) => { + this.setState({ deployState: 'Gas estimated, Posting transaction to the network' }); + + const gasPassed = gas.mul(1.2); + options.gas = gasPassed.toFixed(0); + console.log(`gas estimated at ${gas.toFormat(0)}, passing ${gasPassed.toFormat(0)}`); + + return managerInstance.deploy.postTransaction(options, values); + }) + .then((signerRequestId) => { + this.setState({ signerRequestId, deployState: 'Transaction posted, Waiting for transaction authorization' }); + + return api.pollMethod('eth_checkRequest', signerRequestId); + }) + .then((txHash) => { + this.setState({ txHash, deployState: 'Transaction authorized, Waiting for network confirmations' }); + + return api.pollMethod('eth_getTransactionReceipt', txHash, (receipt) => { + if (!receipt || !receipt.blockNumber || receipt.blockNumber.eq(0)) { + return false; + } + + return true; + }); + }) + .then((txReceipt) => { + this.setState({ txReceipt, deployDone: true, deployState: 'Network confirmed, Received transaction receipt' }); + }) + .catch((error) => { + console.error('onDeploy', error); + this.setState({ deployError: error.message }); + }); + } +} diff --git a/js/src/dapps/basiccoin/Deploy/Deployment/index.js b/js/src/dapps/basiccoin/Deploy/Deployment/index.js new file mode 100644 index 000000000..927cff569 --- /dev/null +++ b/js/src/dapps/basiccoin/Deploy/Deployment/index.js @@ -0,0 +1,17 @@ +// Copyright 2015, 2016 Ethcore (UK) Ltd. +// This file is part of Parity. + +// Parity is free software: you can redistribute it and/or modify +// it under the terms of the GNU General Public License as published by +// the Free Software Foundation, either version 3 of the License, or +// (at your option) any later version. + +// Parity is distributed in the hope that it will be useful, +// but WITHOUT ANY WARRANTY; without even the implied warranty of +// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the +// GNU General Public License for more details. + +// You should have received a copy of the GNU General Public License +// along with Parity. If not, see . + +export default from './deployment'; diff --git a/js/src/dapps/basiccoin/Deploy/Event/event.css b/js/src/dapps/basiccoin/Deploy/Event/event.css new file mode 100644 index 000000000..52eada915 --- /dev/null +++ b/js/src/dapps/basiccoin/Deploy/Event/event.css @@ -0,0 +1,58 @@ +/* Copyright 2015, 2016 Ethcore (UK) Ltd. +/* This file is part of Parity. +/* +/* Parity is free software: you can redistribute it and/or modify +/* it under the terms of the GNU General Public License as published by +/* the Free Software Foundation, either version 3 of the License, or +/* (at your option) any later version. +/* +/* Parity is distributed in the hope that it will be useful, +/* but WITHOUT ANY WARRANTY; without even the implied warranty of +/* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the +/* GNU General Public License for more details. +/* +/* You should have received a copy of the GNU General Public License +/* along with Parity. If not, see . +*/ + +a.link, a.link:visited { + text-decoration: none; +} + +a.link:hover { + text-decoration: underline; +} + +a.link, a.link:hover, a.link:visited { + color: #822; + cursor: pointer; +} + +.mined { +} + +.pending { + opacity: 0.5; +} + +.mined td, +.pending td { + padding: 0.5em 1em; + line-height: 24px; +} + +.blocknumber { + text-align: right; +} + +.address { + text-align: left; +} + +.description { + text-align: left; +} + +.center { + text-align: right; +} diff --git a/js/src/dapps/basiccoin/Deploy/Event/event.js b/js/src/dapps/basiccoin/Deploy/Event/event.js new file mode 100644 index 000000000..889822208 --- /dev/null +++ b/js/src/dapps/basiccoin/Deploy/Event/event.js @@ -0,0 +1,103 @@ +// Copyright 2015, 2016 Ethcore (UK) Ltd. +// This file is part of Parity. + +// Parity is free software: you can redistribute it and/or modify +// it under the terms of the GNU General Public License as published by +// the Free Software Foundation, either version 3 of the License, or +// (at your option) any later version. + +// Parity is distributed in the hope that it will be useful, +// but WITHOUT ANY WARRANTY; without even the implied warranty of +// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the +// GNU General Public License for more details. + +// You should have received a copy of the GNU General Public License +// along with Parity. If not, see . + +import moment from 'moment'; +import React, { Component, PropTypes } from 'react'; + +import { api } from '../../parity'; +import { getCoin, txLink } from '../../services'; +import IdentityIcon from '../../IdentityIcon'; + +import styles from './event.css'; + +export default class Event extends Component { + static contextTypes = { + accounts: PropTypes.object.isRequired, + registryInstance: PropTypes.object.isRequired, + tokenregInstance: PropTypes.object.isRequired + } + + static propTypes = { + event: PropTypes.object.isRequired + } + + state = { + block: null, + coin: {} + } + + componentDidMount () { + this.lookup(); + } + + render () { + const { event } = this.props; + const { block, coin } = this.state; + const isPending = event.type === 'pending'; + + return ( + + +
{ (isPending || !block) ? '' : moment(block.timestamp).fromNow() }
+
{ isPending ? 'Pending' : event.blockNumber.toFormat() }
+ + { event.event } + +
{ isPending ? '' : coin.tla }
+
{ isPending ? '' : coin.name }
+ + + { this.renderAddress(event.params.owner) } + + + { isPending || !coin.isGlobal ? '' : 'global' } + + ); + } + + renderAddress (address) { + const { accounts } = this.context; + const account = accounts[address]; + + return ( +
+ + { account ? account.name : address } +
+ ); + } + + renderHash (hash) { + return `${hash.substr(0, 10)}...${hash.slice(-10)}`; + } + + lookup () { + const { event } = this.props; + + if (event.type === 'pending') { + return; + } + + Promise + .all([ + api.eth.getBlockByNumber(event.blockNumber), + getCoin(event.params.tokenreg, event.params.coin) + ]) + .then(([block, coin]) => { + this.setState({ block, coin }); + }); + } +} diff --git a/js/src/dapps/basiccoin/Deploy/Event/index.js b/js/src/dapps/basiccoin/Deploy/Event/index.js new file mode 100644 index 000000000..0925882d3 --- /dev/null +++ b/js/src/dapps/basiccoin/Deploy/Event/index.js @@ -0,0 +1,17 @@ +// Copyright 2015, 2016 Ethcore (UK) Ltd. +// This file is part of Parity. + +// Parity is free software: you can redistribute it and/or modify +// it under the terms of the GNU General Public License as published by +// the Free Software Foundation, either version 3 of the License, or +// (at your option) any later version. + +// Parity is distributed in the hope that it will be useful, +// but WITHOUT ANY WARRANTY; without even the implied warranty of +// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the +// GNU General Public License for more details. + +// You should have received a copy of the GNU General Public License +// along with Parity. If not, see . + +export default from './event'; diff --git a/js/src/dapps/basiccoin/Deploy/Events/events.css b/js/src/dapps/basiccoin/Deploy/Events/events.css new file mode 100644 index 000000000..3cefaacb0 --- /dev/null +++ b/js/src/dapps/basiccoin/Deploy/Events/events.css @@ -0,0 +1,33 @@ +/* Copyright 2015, 2016 Ethcore (UK) Ltd. +/* This file is part of Parity. +/* +/* Parity is free software: you can redistribute it and/or modify +/* it under the terms of the GNU General Public License as published by +/* the Free Software Foundation, either version 3 of the License, or +/* (at your option) any later version. +/* +/* Parity is distributed in the hope that it will be useful, +/* but WITHOUT ANY WARRANTY; without even the implied warranty of +/* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the +/* GNU General Public License for more details. +/* +/* You should have received a copy of the GNU General Public License +/* along with Parity. If not, see . +*/ + +.statusHeader { + font-size: 1.25em; +} + +.eventList { + border: none; + margin: 0 auto; + border-collapse: collapse; +} + +.eventList tr:nth-child(even) { + background: rgba(102, 34, 34, 0.075); +} + +.eventList tr:nth-child(odd) { +} diff --git a/js/src/dapps/basiccoin/Deploy/Events/events.js b/js/src/dapps/basiccoin/Deploy/Events/events.js new file mode 100644 index 000000000..a21672a8e --- /dev/null +++ b/js/src/dapps/basiccoin/Deploy/Events/events.js @@ -0,0 +1,141 @@ +// Copyright 2015, 2016 Ethcore (UK) Ltd. +// This file is part of Parity. + +// Parity is free software: you can redistribute it and/or modify +// it under the terms of the GNU General Public License as published by +// the Free Software Foundation, either version 3 of the License, or +// (at your option) any later version. + +// Parity is distributed in the hope that it will be useful, +// but WITHOUT ANY WARRANTY; without even the implied warranty of +// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the +// GNU General Public License for more details. + +// You should have received a copy of the GNU General Public License +// along with Parity. If not, see . + +import React, { Component, PropTypes } from 'react'; + +import { api } from '../../parity'; +import Container from '../../Container'; +import Event from '../Event'; + +import styles from './events.css'; + +export default class Events extends Component { + static contextTypes = { + managerInstance: PropTypes.object.isRequired + } + + state = { + blocks: {}, + loading: true, + events: [], + minedEvents: [], + pendingEvents: [] + } + + componentDidMount () { + const { managerInstance } = this.context; + const options = { + fromBlock: 0, + toBlock: 'pending', + limit: 50 + }; + + managerInstance.Created + .subscribe(options, this.receiveCreatedEvents) + .then((subscriptionIdCreated) => { + this.setState({ subscriptionIdCreated }); + }); + } + + componentWillUnmount () { + const { managerInstance } = this.context; + const { subscriptionIdCreated } = this.state; + + managerInstance.Created.unsubscribe(subscriptionIdCreated); + } + + render () { + const { loading } = this.state; + + return ( + + { loading ? this.renderLoading() : this.renderEvents() } + + ); + } + + renderEvents () { + const { events } = this.state; + + return events.length + ? this.renderEventsList() + : this.renderEventsNone(); + } + + renderEventsNone () { + return ( +
+ There are currently no events available +
+ ); + } + + renderEventsList () { + const { events } = this.state; + const rows = events.map((event) => { + return ( + + ); + }); + + return ( + + + { rows } + +
+ ); + } + + renderLoading () { + return ( +
+ Loading events +
+ ); + } + + logToEvent = (log) => { + log.key = api.util.sha3(JSON.stringify(log)); + + return log; + } + + receiveCreatedEvents = (error, logs) => { + if (error) { + console.error('receiveLogs', error); + return; + } + + const { minedEvents, pendingEvents } = this.state; + const minedNew = logs + .filter((log) => log.type === 'mined') + .map(this.logToEvent) + .filter((log) => !minedEvents.find((event) => event.transactionHash === log.transactionHash)) + .reverse() + .concat(minedEvents); + const pendingNew = logs + .filter((log) => log.type === 'pending') + .map(this.logToEvent) + .filter((log) => !pendingEvents.find((event) => event.transactionHash === log.transactionHash)) + .reverse() + .concat(pendingEvents) + .filter((log) => !minedNew.find((event) => event.transactionHash === log.transactionHash)); + const events = [].concat(pendingNew).concat(minedNew); + + this.setState({ loading: false, events, minedEvents: minedNew, pendingEvents: pendingNew }); + } +} diff --git a/js/src/dapps/basiccoin/Deploy/Events/index.js b/js/src/dapps/basiccoin/Deploy/Events/index.js new file mode 100644 index 000000000..88ad6d407 --- /dev/null +++ b/js/src/dapps/basiccoin/Deploy/Events/index.js @@ -0,0 +1,17 @@ +// Copyright 2015, 2016 Ethcore (UK) Ltd. +// This file is part of Parity. + +// Parity is free software: you can redistribute it and/or modify +// it under the terms of the GNU General Public License as published by +// the Free Software Foundation, either version 3 of the License, or +// (at your option) any later version. + +// Parity is distributed in the hope that it will be useful, +// but WITHOUT ANY WARRANTY; without even the implied warranty of +// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the +// GNU General Public License for more details. + +// You should have received a copy of the GNU General Public License +// along with Parity. If not, see . + +export default from './events'; diff --git a/js/src/dapps/basiccoin/Deploy/deploy.js b/js/src/dapps/basiccoin/Deploy/deploy.js new file mode 100644 index 000000000..34c7eed8a --- /dev/null +++ b/js/src/dapps/basiccoin/Deploy/deploy.js @@ -0,0 +1,31 @@ +// Copyright 2015, 2016 Ethcore (UK) Ltd. +// This file is part of Parity. + +// Parity is free software: you can redistribute it and/or modify +// it under the terms of the GNU General Public License as published by +// the Free Software Foundation, either version 3 of the License, or +// (at your option) any later version. + +// Parity is distributed in the hope that it will be useful, +// but WITHOUT ANY WARRANTY; without even the implied warranty of +// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the +// GNU General Public License for more details. + +// You should have received a copy of the GNU General Public License +// along with Parity. If not, see . + +import React, { Component } from 'react'; + +import Deployment from './Deployment'; +import Events from './Events'; + +export default class Deploy extends Component { + render () { + return ( +
+ + +
+ ); + } +} diff --git a/js/src/dapps/basiccoin/Deploy/index.js b/js/src/dapps/basiccoin/Deploy/index.js new file mode 100644 index 000000000..53b4dbcfe --- /dev/null +++ b/js/src/dapps/basiccoin/Deploy/index.js @@ -0,0 +1,17 @@ +// Copyright 2015, 2016 Ethcore (UK) Ltd. +// This file is part of Parity. + +// Parity is free software: you can redistribute it and/or modify +// it under the terms of the GNU General Public License as published by +// the Free Software Foundation, either version 3 of the License, or +// (at your option) any later version. + +// Parity is distributed in the hope that it will be useful, +// but WITHOUT ANY WARRANTY; without even the implied warranty of +// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the +// GNU General Public License for more details. + +// You should have received a copy of the GNU General Public License +// along with Parity. If not, see . + +export default from './deploy'; diff --git a/js/src/dapps/basiccoin/IdentityIcon/identityIcon.css b/js/src/dapps/basiccoin/IdentityIcon/identityIcon.css new file mode 100644 index 000000000..6abbcf808 --- /dev/null +++ b/js/src/dapps/basiccoin/IdentityIcon/identityIcon.css @@ -0,0 +1,22 @@ +/* Copyright 2015, 2016 Ethcore (UK) Ltd. +/* This file is part of Parity. +/* +/* Parity is free software: you can redistribute it and/or modify +/* it under the terms of the GNU General Public License as published by +/* the Free Software Foundation, either version 3 of the License, or +/* (at your option) any later version. +/* +/* Parity is distributed in the hope that it will be useful, +/* but WITHOUT ANY WARRANTY; without even the implied warranty of +/* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the +/* GNU General Public License for more details. +/* +/* You should have received a copy of the GNU General Public License +/* along with Parity. If not, see . +*/ + +.icon { + width: 24px; + height: 24px; + margin: 0 0.5em -4px 0; +} diff --git a/js/src/dapps/basiccoin/IdentityIcon/identityIcon.js b/js/src/dapps/basiccoin/IdentityIcon/identityIcon.js new file mode 100644 index 000000000..ee1374f7e --- /dev/null +++ b/js/src/dapps/basiccoin/IdentityIcon/identityIcon.js @@ -0,0 +1,38 @@ +// Copyright 2015, 2016 Ethcore (UK) Ltd. +// This file is part of Parity. + +// Parity is free software: you can redistribute it and/or modify +// it under the terms of the GNU General Public License as published by +// the Free Software Foundation, either version 3 of the License, or +// (at your option) any later version. + +// Parity is distributed in the hope that it will be useful, +// but WITHOUT ANY WARRANTY; without even the implied warranty of +// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the +// GNU General Public License for more details. + +// You should have received a copy of the GNU General Public License +// along with Parity. If not, see . + +import React, { Component, PropTypes } from 'react'; + +import { api } from '../parity'; +import styles from './identityIcon.css'; + +export default class IdentityIcon extends Component { + static propTypes = { + address: PropTypes.string.isRequired, + className: PropTypes.string + } + + render () { + const { address, className } = this.props; + const classes = `${styles.icon} ${className}`; + + return ( + + ); + } +} diff --git a/js/src/dapps/basiccoin/IdentityIcon/index.js b/js/src/dapps/basiccoin/IdentityIcon/index.js new file mode 100644 index 000000000..76c107bfb --- /dev/null +++ b/js/src/dapps/basiccoin/IdentityIcon/index.js @@ -0,0 +1,17 @@ +// Copyright 2015, 2016 Ethcore (UK) Ltd. +// This file is part of Parity. + +// Parity is free software: you can redistribute it and/or modify +// it under the terms of the GNU General Public License as published by +// the Free Software Foundation, either version 3 of the License, or +// (at your option) any later version. + +// Parity is distributed in the hope that it will be useful, +// but WITHOUT ANY WARRANTY; without even the implied warranty of +// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the +// GNU General Public License for more details. + +// You should have received a copy of the GNU General Public License +// along with Parity. If not, see . + +export default from './identityIcon'; diff --git a/js/src/dapps/basiccoin/Overview/Owner/index.js b/js/src/dapps/basiccoin/Overview/Owner/index.js new file mode 100644 index 000000000..4f38b38b9 --- /dev/null +++ b/js/src/dapps/basiccoin/Overview/Owner/index.js @@ -0,0 +1,17 @@ +// Copyright 2015, 2016 Ethcore (UK) Ltd. +// This file is part of Parity. + +// Parity is free software: you can redistribute it and/or modify +// it under the terms of the GNU General Public License as published by +// the Free Software Foundation, either version 3 of the License, or +// (at your option) any later version. + +// Parity is distributed in the hope that it will be useful, +// but WITHOUT ANY WARRANTY; without even the implied warranty of +// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the +// GNU General Public License for more details. + +// You should have received a copy of the GNU General Public License +// along with Parity. If not, see . + +export default from './owner'; diff --git a/js/src/dapps/basiccoin/Overview/Owner/owner.css b/js/src/dapps/basiccoin/Overview/Owner/owner.css new file mode 100644 index 000000000..13e89d061 --- /dev/null +++ b/js/src/dapps/basiccoin/Overview/Owner/owner.css @@ -0,0 +1,48 @@ +/* Copyright 2015, 2016 Ethcore (UK) Ltd. +/* This file is part of Parity. +/* +/* Parity is free software: you can redistribute it and/or modify +/* it under the terms of the GNU General Public License as published by +/* the Free Software Foundation, either version 3 of the License, or +/* (at your option) any later version. +/* +/* Parity is distributed in the hope that it will be useful, +/* but WITHOUT ANY WARRANTY; without even the implied warranty of +/* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the +/* GNU General Public License for more details. +/* +/* You should have received a copy of the GNU General Public License +/* along with Parity. If not, see . +*/ + +.info { +} + +.owner { + vertical-align: top; + text-align: right; +} + +.owner>div { + border-radius: 1px; + padding: 1em 2em; + white-space: nowrap; +} + +.tokens { + text-align: left; +} + +.tokens>div { + border-radius: 1px; + background: #988; + padding: 1em; + margin: 0 0 0.25em 0.25em; + display: inline-block; + white-space: nowrap; + color: white; +} + +.icon { + margin: 0 0 -4px 1em; +} diff --git a/js/src/dapps/basiccoin/Overview/Owner/owner.js b/js/src/dapps/basiccoin/Overview/Owner/owner.js new file mode 100644 index 000000000..cc3003693 --- /dev/null +++ b/js/src/dapps/basiccoin/Overview/Owner/owner.js @@ -0,0 +1,74 @@ +// Copyright 2015, 2016 Ethcore (UK) Ltd. +// This file is part of Parity. + +// Parity is free software: you can redistribute it and/or modify +// it under the terms of the GNU General Public License as published by +// the Free Software Foundation, either version 3 of the License, or +// (at your option) any later version. + +// Parity is distributed in the hope that it will be useful, +// but WITHOUT ANY WARRANTY; without even the implied warranty of +// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the +// GNU General Public License for more details. + +// You should have received a copy of the GNU General Public License +// along with Parity. If not, see . + +import React, { Component, PropTypes } from 'react'; + +import IdentityIcon from '../../IdentityIcon'; +import Token from '../Token'; +import styles from './owner.css'; + +export default class Owner extends Component { + static contextTypes = { + accounts: PropTypes.object.isRequired, + managerInstance: PropTypes.object.isRequired + } + + static propTypes = { + address: PropTypes.string.isRequired, + tokens: PropTypes.array.isRequired + } + + state = { + tokens: [] + } + + render () { + const { accounts } = this.context; + const { address, tokens } = this.props; + + if (!tokens.length) { + return null; + } + + return ( + + +
+ { accounts[address].name } + +
+ + + { this.renderTokens() } + + + ); + } + + renderTokens () { + const { tokens } = this.props; + + return tokens.map((token) => ( +
+ +
+ )); + } +} diff --git a/js/src/dapps/basiccoin/Overview/Token/index.js b/js/src/dapps/basiccoin/Overview/Token/index.js new file mode 100644 index 000000000..4b822b4bd --- /dev/null +++ b/js/src/dapps/basiccoin/Overview/Token/index.js @@ -0,0 +1,17 @@ +// Copyright 2015, 2016 Ethcore (UK) Ltd. +// This file is part of Parity. + +// Parity is free software: you can redistribute it and/or modify +// it under the terms of the GNU General Public License as published by +// the Free Software Foundation, either version 3 of the License, or +// (at your option) any later version. + +// Parity is distributed in the hope that it will be useful, +// but WITHOUT ANY WARRANTY; without even the implied warranty of +// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the +// GNU General Public License for more details. + +// You should have received a copy of the GNU General Public License +// along with Parity. If not, see . + +export default from './token'; diff --git a/js/src/dapps/basiccoin/Overview/Token/token.css b/js/src/dapps/basiccoin/Overview/Token/token.css new file mode 100644 index 000000000..5324aa7c5 --- /dev/null +++ b/js/src/dapps/basiccoin/Overview/Token/token.css @@ -0,0 +1,53 @@ +/* Copyright 2015, 2016 Ethcore (UK) Ltd. +/* This file is part of Parity. +/* +/* Parity is free software: you can redistribute it and/or modify +/* it under the terms of the GNU General Public License as published by +/* the Free Software Foundation, either version 3 of the License, or +/* (at your option) any later version. +/* +/* Parity is distributed in the hope that it will be useful, +/* but WITHOUT ANY WARRANTY; without even the implied warranty of +/* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the +/* GNU General Public License for more details. +/* +/* You should have received a copy of the GNU General Public License +/* along with Parity. If not, see . +*/ + +.info { +} + +.info>div { + display: inline-block; + padding: 0.25em 0.5em; + vertical-align: middle; +} + +.address { +} + +.tla { + background: #766; + border-radius: 1px; +} + +.name { +} + +.supply { +} + +.supply div { + display: block; + text-align: center; +} + +.supply .info { + font-size: 0.75em; + opacity: 0.75; +} + +.global { + font-size: 0.75em; +} diff --git a/js/src/dapps/basiccoin/Overview/Token/token.js b/js/src/dapps/basiccoin/Overview/Token/token.js new file mode 100644 index 000000000..b0d4a965a --- /dev/null +++ b/js/src/dapps/basiccoin/Overview/Token/token.js @@ -0,0 +1,68 @@ +// Copyright 2015, 2016 Ethcore (UK) Ltd. +// This file is part of Parity. + +// Parity is free software: you can redistribute it and/or modify +// it under the terms of the GNU General Public License as published by +// the Free Software Foundation, either version 3 of the License, or +// (at your option) any later version. + +// Parity is distributed in the hope that it will be useful, +// but WITHOUT ANY WARRANTY; without even the implied warranty of +// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the +// GNU General Public License for more details. + +// You should have received a copy of the GNU General Public License +// along with Parity. If not, see . + +import React, { Component, PropTypes } from 'react'; + +import { totalSupply, getCoin } from '../../services'; +import styles from './token.css'; + +export default class Token extends Component { + static propTypes = { + address: PropTypes.string.isRequired, + tokenreg: PropTypes.string.isRequired + } + + state = { + coin: null, + totalSupply: null + } + + componentDidMount () { + this.lookupToken(); + } + + render () { + const { coin, totalSupply } = this.state; + + if (!coin) { + return null; + } + + return ( +
+
{ coin.tla }
+
{ coin.name }
+
+
{ totalSupply.div(1000000).toFormat(0) }
+
total supply
+
+
+ ); + } + + lookupToken () { + const { address, tokenreg } = this.props; + + Promise + .all([ + getCoin(tokenreg, address), + totalSupply(address) + ]) + .then(([coin, totalSupply]) => { + this.setState({ coin, totalSupply }); + }); + } +} diff --git a/js/src/dapps/basiccoin/Overview/index.js b/js/src/dapps/basiccoin/Overview/index.js new file mode 100644 index 000000000..d61fb3fb4 --- /dev/null +++ b/js/src/dapps/basiccoin/Overview/index.js @@ -0,0 +1,17 @@ +// Copyright 2015, 2016 Ethcore (UK) Ltd. +// This file is part of Parity. + +// Parity is free software: you can redistribute it and/or modify +// it under the terms of the GNU General Public License as published by +// the Free Software Foundation, either version 3 of the License, or +// (at your option) any later version. + +// Parity is distributed in the hope that it will be useful, +// but WITHOUT ANY WARRANTY; without even the implied warranty of +// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the +// GNU General Public License for more details. + +// You should have received a copy of the GNU General Public License +// along with Parity. If not, see . + +export default from './overview'; diff --git a/js/src/dapps/basiccoin/Overview/overview.css b/js/src/dapps/basiccoin/Overview/overview.css new file mode 100644 index 000000000..9757a8145 --- /dev/null +++ b/js/src/dapps/basiccoin/Overview/overview.css @@ -0,0 +1,26 @@ +/* Copyright 2015, 2016 Ethcore (UK) Ltd. +/* This file is part of Parity. +/* +/* Parity is free software: you can redistribute it and/or modify +/* it under the terms of the GNU General Public License as published by +/* the Free Software Foundation, either version 3 of the License, or +/* (at your option) any later version. +/* +/* Parity is distributed in the hope that it will be useful, +/* but WITHOUT ANY WARRANTY; without even the implied warranty of +/* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the +/* GNU General Public License for more details. +/* +/* You should have received a copy of the GNU General Public License +/* along with Parity. If not, see . +*/ + +@import '../_status.css'; + +.body { +} + +.ownerTable { + margin: 0 auto; + border-collapse: collapse; +} diff --git a/js/src/dapps/basiccoin/Overview/overview.js b/js/src/dapps/basiccoin/Overview/overview.js new file mode 100644 index 000000000..46831d782 --- /dev/null +++ b/js/src/dapps/basiccoin/Overview/overview.js @@ -0,0 +1,107 @@ +// Copyright 2015, 2016 Ethcore (UK) Ltd. +// This file is part of Parity. + +// Parity is free software: you can redistribute it and/or modify +// it under the terms of the GNU General Public License as published by +// the Free Software Foundation, either version 3 of the License, or +// (at your option) any later version. + +// Parity is distributed in the hope that it will be useful, +// but WITHOUT ANY WARRANTY; without even the implied warranty of +// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the +// GNU General Public License for more details. + +// You should have received a copy of the GNU General Public License +// along with Parity. If not, see . + +import BigNumber from 'bignumber.js'; +import React, { Component, PropTypes } from 'react'; + +import { loadOwnedTokens } from '../services'; +import Container from '../Container'; +import Owner from './Owner'; + +import styles from './overview.css'; + +export default class Overview extends Component { + static contextTypes = { + accounts: PropTypes.object.isRequired, + managerInstance: PropTypes.object.isRequired + } + + state = { + loading: true, + total: new BigNumber(0), + tokenOwners: [] + } + + componentDidMount () { + this.loadOwners(); + } + + render () { + const { loading } = this.state; + + return ( + + { loading ? this.renderLoading() : this.renderBody() } + + ); + } + + renderLoading () { + return ( +
+ Loading tokens +
+ ); + } + + renderBody () { + const { total } = this.state; + let owners = null; + + if (total.gt(0)) { + owners = ( + + + { this.renderOwners() } + +
+ ); + } + + return ( +
+
+ You have { total.toFormat(0) } tokens created by your accounts +
+ { owners } +
+ ); + } + + renderOwners () { + const { tokens } = this.state; + + return Object.keys(tokens).map((address) => ( + + )); + } + + loadOwners () { + const { accounts } = this.context; + const addresses = Object + .values(accounts) + .filter((account) => account.uuid) + .map((account) => account.address); + + loadOwnedTokens(addresses) + .then(({ tokens, total }) => { + this.setState({ tokens, total, loading: false }); + }); + } +} diff --git a/js/src/dapps/basiccoin/Transfer/Event/event.js b/js/src/dapps/basiccoin/Transfer/Event/event.js new file mode 100644 index 000000000..190035b1d --- /dev/null +++ b/js/src/dapps/basiccoin/Transfer/Event/event.js @@ -0,0 +1,106 @@ +// Copyright 2015, 2016 Ethcore (UK) Ltd. +// This file is part of Parity. + +// Parity is free software: you can redistribute it and/or modify +// it under the terms of the GNU General Public License as published by +// the Free Software Foundation, either version 3 of the License, or +// (at your option) any later version. + +// Parity is distributed in the hope that it will be useful, +// but WITHOUT ANY WARRANTY; without even the implied warranty of +// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the +// GNU General Public License for more details. + +// You should have received a copy of the GNU General Public License +// along with Parity. If not, see . + +// TODO: This is a copy & paste for Deploy/Event -> render() different. Not very DRY +import moment from 'moment'; +import React, { Component, PropTypes } from 'react'; + +import { api } from '../../parity'; +import { txLink } from '../../services'; +import IdentityIcon from '../../IdentityIcon'; +import styles from '../../Deploy/Event/event.css'; + +export default class Event extends Component { + static contextTypes = { + accounts: PropTypes.object.isRequired, + registryInstance: PropTypes.object.isRequired, + tokenregInstance: PropTypes.object.isRequired + } + + static propTypes = { + event: PropTypes.object.isRequired, + token: PropTypes.object.isRequired + } + + state = { + block: null + } + + componentDidMount () { + this.lookup(); + } + + render () { + const { event, token } = this.props; + const { block } = this.state; + const isPending = event.type === 'pending'; + + return ( + + +
{ (isPending || !block) ? '' : moment(block.timestamp).fromNow() }
+
{ isPending ? 'Pending' : event.blockNumber.toFormat() }
+ + { event.event } + +
{ isPending ? '' : token.coin.tla }
+
{ isPending ? '' : token.coin.name }
+ + + { this.renderAddress(event.params.from) } + + +
{ event.params.value.div(1000000).toFormat(6) }
+
⇒
+ + + + { this.renderAddress(event.params.to) } + + + ); + } + + renderAddress (address) { + const { accounts } = this.context; + const account = accounts[address]; + + return ( +
+ + { account ? account.name : address } +
+ ); + } + + renderHash (hash) { + return `${hash.substr(0, 10)}...${hash.slice(-10)}`; + } + + lookup () { + const { event } = this.props; + + if (event.type === 'pending') { + return; + } + + api.eth + .getBlockByNumber(event.blockNumber) + .then((block) => { + this.setState({ block }); + }); + } +} diff --git a/js/src/dapps/basiccoin/Transfer/Event/index.js b/js/src/dapps/basiccoin/Transfer/Event/index.js new file mode 100644 index 000000000..0925882d3 --- /dev/null +++ b/js/src/dapps/basiccoin/Transfer/Event/index.js @@ -0,0 +1,17 @@ +// Copyright 2015, 2016 Ethcore (UK) Ltd. +// This file is part of Parity. + +// Parity is free software: you can redistribute it and/or modify +// it under the terms of the GNU General Public License as published by +// the Free Software Foundation, either version 3 of the License, or +// (at your option) any later version. + +// Parity is distributed in the hope that it will be useful, +// but WITHOUT ANY WARRANTY; without even the implied warranty of +// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the +// GNU General Public License for more details. + +// You should have received a copy of the GNU General Public License +// along with Parity. If not, see . + +export default from './event'; diff --git a/js/src/dapps/basiccoin/Transfer/Events/events.js b/js/src/dapps/basiccoin/Transfer/Events/events.js new file mode 100644 index 000000000..dcead03bb --- /dev/null +++ b/js/src/dapps/basiccoin/Transfer/Events/events.js @@ -0,0 +1,145 @@ +// Copyright 2015, 2016 Ethcore (UK) Ltd. +// This file is part of Parity. + +// Parity is free software: you can redistribute it and/or modify +// it under the terms of the GNU General Public License as published by +// the Free Software Foundation, either version 3 of the License, or +// (at your option) any later version. + +// Parity is distributed in the hope that it will be useful, +// but WITHOUT ANY WARRANTY; without even the implied warranty of +// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the +// GNU General Public License for more details. + +// You should have received a copy of the GNU General Public License +// along with Parity. If not, see . + +import React, { Component } from 'react'; + +import { api } from '../../parity'; +import { loadAllTokens, subscribeEvents, unsubscribeEvents } from '../../services'; +import Container from '../../Container'; +import Event from '../Event'; + +import styles from '../../Deploy/Events/events.css'; + +export default class Events extends Component { + state = { + subscriptionId: 0, + loading: true, + events: [], + pendingEvents: [], + minedEvents: [], + tokens: [] + } + + componentDidMount () { + loadAllTokens() + .then((tokens) => { + const addresses = tokens.map((token) => token.address); + this.setState({ tokens }); + return subscribeEvents(addresses, this.eventCallback); + }) + .then((subscriptionId) => { + this.setState({ subscriptionId, loading: false }); + }) + .catch((error) => { + console.error('componentDidMount', error); + }); + } + + componentWillUnmount () { + const { subscriptionId } = this.state; + + if (subscriptionId) { + unsubscribeEvents(subscriptionId); + } + } + + render () { + const { loading } = this.state; + + return ( + + { loading ? this.renderLoading() : this.renderEvents() } + + ); + } + + renderLoading () { + return ( +
+ Attaching events +
+ ); + } + + renderEvents () { + const { events } = this.state; + + return events.length + ? this.renderEventsList() + : this.renderEventsNone(); + } + + renderEventsNone () { + return ( +
+ There are currently no events available +
+ ); + } + + renderEventsList () { + const { events, tokens } = this.state; + const rows = events.map((event) => { + const token = tokens.find((token) => token.address === event.address); + + return ( + + ); + }); + + return ( + + + { rows } + +
+ ); + } + + logToEvent = (log) => { + log.key = api.util.sha3(JSON.stringify(log)); + + return log; + } + + eventCallback = (error, logs) => { + if (error) { + console.error('eventCallback', error); + return; + } + + const { minedEvents, pendingEvents } = this.state; + const minedNew = logs + .filter((log) => log.type === 'mined') + .map(this.logToEvent) + .filter((log) => !minedEvents.find((event) => event.transactionHash === log.transactionHash)) + .reverse() + .concat(minedEvents); + const pendingNew = logs + .filter((log) => log.type === 'pending') + .map(this.logToEvent) + .filter((log) => !pendingEvents.find((event) => event.transactionHash === log.transactionHash)) + .reverse() + .concat(pendingEvents) + .filter((log) => !minedNew.find((event) => event.transactionHash === log.transactionHash)); + const events = [].concat(pendingNew).concat(minedNew); + console.log('*** events', events.map((event) => event.address)); + this.setState({ loading: false, events, minedEvents: minedNew, pendingEvents: pendingNew }); + } +} diff --git a/js/src/dapps/basiccoin/Transfer/Events/index.js b/js/src/dapps/basiccoin/Transfer/Events/index.js new file mode 100644 index 000000000..88ad6d407 --- /dev/null +++ b/js/src/dapps/basiccoin/Transfer/Events/index.js @@ -0,0 +1,17 @@ +// Copyright 2015, 2016 Ethcore (UK) Ltd. +// This file is part of Parity. + +// Parity is free software: you can redistribute it and/or modify +// it under the terms of the GNU General Public License as published by +// the Free Software Foundation, either version 3 of the License, or +// (at your option) any later version. + +// Parity is distributed in the hope that it will be useful, +// but WITHOUT ANY WARRANTY; without even the implied warranty of +// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the +// GNU General Public License for more details. + +// You should have received a copy of the GNU General Public License +// along with Parity. If not, see . + +export default from './events'; diff --git a/js/src/dapps/basiccoin/Transfer/Send/index.js b/js/src/dapps/basiccoin/Transfer/Send/index.js new file mode 100644 index 000000000..ad3107789 --- /dev/null +++ b/js/src/dapps/basiccoin/Transfer/Send/index.js @@ -0,0 +1,17 @@ +// Copyright 2015, 2016 Ethcore (UK) Ltd. +// This file is part of Parity. + +// Parity is free software: you can redistribute it and/or modify +// it under the terms of the GNU General Public License as published by +// the Free Software Foundation, either version 3 of the License, or +// (at your option) any later version. + +// Parity is distributed in the hope that it will be useful, +// but WITHOUT ANY WARRANTY; without even the implied warranty of +// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the +// GNU General Public License for more details. + +// You should have received a copy of the GNU General Public License +// along with Parity. If not, see . + +export default from './send'; diff --git a/js/src/dapps/basiccoin/Transfer/Send/send.css b/js/src/dapps/basiccoin/Transfer/Send/send.css new file mode 100644 index 000000000..f24729cf7 --- /dev/null +++ b/js/src/dapps/basiccoin/Transfer/Send/send.css @@ -0,0 +1,19 @@ +/* Copyright 2015, 2016 Ethcore (UK) Ltd. +/* This file is part of Parity. +/* +/* Parity is free software: you can redistribute it and/or modify +/* it under the terms of the GNU General Public License as published by +/* the Free Software Foundation, either version 3 of the License, or +/* (at your option) any later version. +/* +/* Parity is distributed in the hope that it will be useful, +/* but WITHOUT ANY WARRANTY; without even the implied warranty of +/* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the +/* GNU General Public License for more details. +/* +/* You should have received a copy of the GNU General Public License +/* along with Parity. If not, see . +*/ + +@import '../../_form.css'; +@import '../../_status.css'; diff --git a/js/src/dapps/basiccoin/Transfer/Send/send.js b/js/src/dapps/basiccoin/Transfer/Send/send.js new file mode 100644 index 000000000..aee860fe2 --- /dev/null +++ b/js/src/dapps/basiccoin/Transfer/Send/send.js @@ -0,0 +1,327 @@ +// Copyright 2015, 2016 Ethcore (UK) Ltd. +// This file is part of Parity. + +// Parity is free software: you can redistribute it and/or modify +// it under the terms of the GNU General Public License as published by +// the Free Software Foundation, either version 3 of the License, or +// (at your option) any later version. + +// Parity is distributed in the hope that it will be useful, +// but WITHOUT ANY WARRANTY; without even the implied warranty of +// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the +// GNU General Public License for more details. + +// You should have received a copy of the GNU General Public License +// along with Parity. If not, see . + +import BigNumber from 'bignumber.js'; +import React, { Component, PropTypes } from 'react'; + +import { eip20 } from '../../../../contracts/abi'; + +import { api } from '../../parity'; +import { loadBalances } from '../../services'; +import AddressSelect from '../../AddressSelect'; +import Container from '../../Container'; + +import styles from './send.css'; + +export default class Send extends Component { + static contextTypes = { + accounts: PropTypes.object.isRequired + } + + state = { + loading: true, + tokens: null, + selectedToken: null, + availableBalances: [], + fromAddress: null, + fromBalance: null, + toAddress: null, + toKnown: true, + amount: 0, + amountError: null, + sendBusy: false, + sendError: null, + sendState: null, + sendDone: false, + signerRequestId: null, + txHash: null, + txReceipt: null + } + + componentDidMount () { + this.loadBalances(); + this.onAmountChange({ target: { value: '0' } }); + } + + render () { + const { loading } = this.state; + + return loading + ? this.renderLoading() + : this.renderBody(); + } + + renderBody () { + const { sendBusy } = this.state; + + return sendBusy + ? this.renderSending() + : this.renderForm(); + } + + renderSending () { + const { sendDone, sendError, sendState } = this.state; + + if (sendDone) { + return ( + +
+ Your token value transfer has been completed +
+
+ ); + } + + if (sendError) { + return ( + +
+ Your deployment has encountered an error +
+
+ { sendError } +
+
+ ); + } + + return ( + +
+ Your token value is being transferred +
+
+ { sendState } +
+
+ ); + } + + renderLoading () { + return ( + +
+ Loading available tokens +
+
+ ); + } + + renderForm () { + const { accounts } = this.context; + const { availableBalances, fromAddress, amount, amountError, toKnown, toAddress } = this.state; + const fromBalance = availableBalances.find((balance) => balance.address === fromAddress); + const fromAddresses = availableBalances.map((balance) => balance.address); + const toAddresses = Object.keys(accounts); + const toInput = toKnown + ? + : ; + const hasError = amountError; + const error = `${styles.input} ${styles.error}`; + const maxAmountHint = `Value to transfer (max: ${fromBalance ? fromBalance.balance.div(1000000).toFormat(6) : '1'})`; + + return ( + +
+
+ + +
+ type of token to transfer +
+
+
+ + +
+ account to transfer from +
+
+
+ + +
+ the type of address input +
+
+
+
+
+ + +
+ { amountError || maxAmountHint } +
+
+
+
+
+
+ ); + } + + renderTokens () { + const { tokens } = this.state; + + return tokens.map((token) => ( + + )); + } + + onSelectFrom = (event) => { + const fromAddress = event.target.value; + + this.setState({ fromAddress }); + } + + onChangeTo = (event) => { + const toAddress = event.target.value; + + this.setState({ toAddress }); + } + + onChangeToType = (event) => { + const toKnown = event.target.value === 'known'; + + this.setState({ toKnown }); + } + + onSelectToken = (event) => { + const { tokens } = this.state; + const address = event.target.value; + const selectedToken = tokens.find((_token) => _token.address === address); + const availableBalances = selectedToken.balances.filter((balance) => balance.balance.gt(0)); + + this.setState({ selectedToken, availableBalances }); + this.onSelectFrom({ target: { value: availableBalances[0].address } }); + } + + onAmountChange = (event) => { + const amount = parseFloat(event.target.value); + const amountError = !isFinite(amount) || amount <= 0 + ? 'amount needs to be > 0' + : null; + + this.setState({ amount, amountError }); + } + + onSend = () => { + const { amount, fromAddress, toAddress, amountError, selectedToken, sendBusy } = this.state; + const hasError = amountError; + + if (hasError || sendBusy) { + return; + } + + const values = [toAddress, new BigNumber(amount).mul(1000000).toFixed(0)]; + const options = { + from: fromAddress + }; + const instance = api.newContract(eip20, selectedToken.address).instance; + + this.setState({ sendBusy: true, sendState: 'Estimating gas for the transaction' }); + + instance + .transfer.estimateGas(options, values) + .then((gas) => { + this.setState({ sendState: 'Gas estimated, Posting transaction to the network' }); + + const gasPassed = gas.mul(1.2); + options.gas = gasPassed.toFixed(0); + console.log(`gas estimated at ${gas.toFormat(0)}, passing ${gasPassed.toFormat(0)}`); + + return instance.transfer.postTransaction(options, values); + }) + .then((signerRequestId) => { + this.setState({ signerRequestId, sendState: 'Transaction posted, Waiting for transaction authorization' }); + + return api.pollMethod('eth_checkRequest', signerRequestId); + }) + .then((txHash) => { + this.setState({ txHash, sendState: 'Transaction authorized, Waiting for network confirmations' }); + + return api.pollMethod('eth_getTransactionReceipt', txHash, (receipt) => { + if (!receipt || !receipt.blockNumber || receipt.blockNumber.eq(0)) { + return false; + } + + return true; + }); + }) + .then((txReceipt) => { + this.setState({ txReceipt, sendDone: true, sendState: 'Network confirmed, Received transaction receipt' }); + }) + .catch((error) => { + console.error('onSend', error); + this.setState({ sendError: error.message }); + }); + } + + loadBalances () { + const { accounts } = this.context; + const myAccounts = Object + .values(accounts) + .filter((account) => account.uuid) + .map((account) => account.address); + + loadBalances(myAccounts) + .then((_tokens) => { + const tokens = _tokens.filter((token) => { + for (let index = 0; index < token.balances.length; index++) { + if (token.balances[index].balance.gt(0)) { + return true; + } + } + + return false; + }); + + this.setState({ tokens, loading: false }); + this.onSelectToken({ target: { value: tokens[0].address } }); + }); + } +} diff --git a/js/src/dapps/basiccoin/Transfer/index.js b/js/src/dapps/basiccoin/Transfer/index.js new file mode 100644 index 000000000..24d36d796 --- /dev/null +++ b/js/src/dapps/basiccoin/Transfer/index.js @@ -0,0 +1,17 @@ +// Copyright 2015, 2016 Ethcore (UK) Ltd. +// This file is part of Parity. + +// Parity is free software: you can redistribute it and/or modify +// it under the terms of the GNU General Public License as published by +// the Free Software Foundation, either version 3 of the License, or +// (at your option) any later version. + +// Parity is distributed in the hope that it will be useful, +// but WITHOUT ANY WARRANTY; without even the implied warranty of +// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the +// GNU General Public License for more details. + +// You should have received a copy of the GNU General Public License +// along with Parity. If not, see . + +export default from './transfer'; diff --git a/js/src/dapps/basiccoin/Transfer/transfer.js b/js/src/dapps/basiccoin/Transfer/transfer.js new file mode 100644 index 000000000..842d4b7bc --- /dev/null +++ b/js/src/dapps/basiccoin/Transfer/transfer.js @@ -0,0 +1,31 @@ +// Copyright 2015, 2016 Ethcore (UK) Ltd. +// This file is part of Parity. + +// Parity is free software: you can redistribute it and/or modify +// it under the terms of the GNU General Public License as published by +// the Free Software Foundation, either version 3 of the License, or +// (at your option) any later version. + +// Parity is distributed in the hope that it will be useful, +// but WITHOUT ANY WARRANTY; without even the implied warranty of +// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the +// GNU General Public License for more details. + +// You should have received a copy of the GNU General Public License +// along with Parity. If not, see . + +import React, { Component } from 'react'; + +import Events from './Events'; +import Send from './Send'; + +export default class Transfer extends Component { + render () { + return ( +
+ + +
+ ); + } +} diff --git a/js/src/dapps/basiccoin/_form.css b/js/src/dapps/basiccoin/_form.css new file mode 100644 index 000000000..ffafdbeaf --- /dev/null +++ b/js/src/dapps/basiccoin/_form.css @@ -0,0 +1,105 @@ +/* Copyright 2015, 2016 Ethcore (UK) Ltd. +/* This file is part of Parity. +/* +/* Parity is free software: you can redistribute it and/or modify +/* it under the terms of the GNU General Public License as published by +/* the Free Software Foundation, either version 3 of the License, or +/* (at your option) any later version. +/* +/* Parity is distributed in the hope that it will be useful, +/* but WITHOUT ANY WARRANTY; without even the implied warranty of +/* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the +/* GNU General Public License for more details. +/* +/* You should have received a copy of the GNU General Public License +/* along with Parity. If not, see . +*/ + +.button { + background: #622; +} + +.form { + text-align: left; + margin: 0 auto; + display: inline-block; +} + +.form .input { + margin-bottom: 1.5em; +} + +.form .input * { + display: inline-block; + margin: 0 0.5em; + padding: 0.5em; + font-size: 1em; +} + +.form label { + width: 25em; + opacity: 0.8; + text-align: right; +} + +.form .hint { + width: 25em; + opacity: 0.5; +} + +.form input, +.form select { + width: 18em; + color: #444; + background: rgba(255, 255, 255, 0.75); + border-radius: 1px; + border: 1px solid rgba(0, 0, 0, 0.25); + box-sizing: border-box; + -moz-appearance: none; + -webkit-appearance: none; + appearance: none; +} + +.form select { + height: 36px; +} + +.form input.small { + width: 9em; + margin-right: 9.5em; +} + +.form .error input { + border: 1px solid rgba(255, 0, 0, 0.5); + color: red; + background: rgba(255, 0, 0, 0.1); +} + +.form .error label { + color: red; +} + +.form .error .hint { + color: red; +} + +.buttonRow { + text-align: right; + width: 18em; +} + +.button { + color: white; + border: none; + border-radius: 1px; + padding: 1em 2em !important; + display: inline-block; + margin-left: 1em !important; + cursor: pointer; + position: relative; +} + +.button[disabled] { + opacity: 0.5; + cursor: default; +} diff --git a/js/src/dapps/basiccoin/_status.css b/js/src/dapps/basiccoin/_status.css new file mode 100644 index 000000000..8fd93cfe4 --- /dev/null +++ b/js/src/dapps/basiccoin/_status.css @@ -0,0 +1,36 @@ +/* Copyright 2015, 2016 Ethcore (UK) Ltd. +/* This file is part of Parity. +/* +/* Parity is free software: you can redistribute it and/or modify +/* it under the terms of the GNU General Public License as published by +/* the Free Software Foundation, either version 3 of the License, or +/* (at your option) any later version. +/* +/* Parity is distributed in the hope that it will be useful, +/* but WITHOUT ANY WARRANTY; without even the implied warranty of +/* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the +/* GNU General Public License for more details. +/* +/* You should have received a copy of the GNU General Public License +/* along with Parity. If not, see . +*/ + +.statusHeader { + font-size: 1.25em; + margin-bottom: 1.25em; + opacity: 0.75; +} + +.statusInfo { + margin-bottom: 0.25em; +} + +.statusState { + opacity: 0.75; + margin-top: 1em; +} + +.statusError { + color: red; + margin-top: 1em; +} diff --git a/js/src/dapps/basiccoin/background.jpg b/js/src/dapps/basiccoin/background.jpg new file mode 100644 index 000000000..c873064f5 Binary files /dev/null and b/js/src/dapps/basiccoin/background.jpg differ diff --git a/js/src/dapps/basiccoin/parity.js b/js/src/dapps/basiccoin/parity.js new file mode 100644 index 000000000..f6d59f44d --- /dev/null +++ b/js/src/dapps/basiccoin/parity.js @@ -0,0 +1,21 @@ +// Copyright 2015, 2016 Ethcore (UK) Ltd. +// This file is part of Parity. + +// Parity is free software: you can redistribute it and/or modify +// it under the terms of the GNU General Public License as published by +// the Free Software Foundation, either version 3 of the License, or +// (at your option) any later version. + +// Parity is distributed in the hope that it will be useful, +// but WITHOUT ANY WARRANTY; without even the implied warranty of +// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the +// GNU General Public License for more details. + +// You should have received a copy of the GNU General Public License +// along with Parity. If not, see . + +const { api } = window.parity; + +export { + api +}; diff --git a/js/src/dapps/basiccoin/services.js b/js/src/dapps/basiccoin/services.js new file mode 100644 index 000000000..ca586ea91 --- /dev/null +++ b/js/src/dapps/basiccoin/services.js @@ -0,0 +1,270 @@ +// Copyright 2015, 2016 Ethcore (UK) Ltd. +// This file is part of Parity. + +// Parity is free software: you can redistribute it and/or modify +// it under the terms of the GNU General Public License as published by +// the Free Software Foundation, either version 3 of the License, or +// (at your option) any later version. + +// Parity is distributed in the hope that it will be useful, +// but WITHOUT ANY WARRANTY; without even the implied warranty of +// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the +// GNU General Public License for more details. + +// You should have received a copy of the GNU General Public License +// along with Parity. If not, see . + +import BigNumber from 'bignumber.js'; + +import * as abis from '../../contracts/abi'; +import { api } from './parity'; + +let managerInstance; +let tokenregInstance; +let registryInstance; + +const registries = {}; +const subscriptions = {}; +let nextSubscriptionId = 1000; +let isTest = false; + +export function subscribeEvents (addresses, callback) { + const subscriptionId = nextSubscriptionId++; + const contract = api.newContract(abis.eip20); + const event = contract.events.filter((evt) => evt.name === 'Transfer'); + const options = { + address: addresses, + fromBlock: 0, + toBlock: 'pending', + limit: 50, + topics: [event.signature] + }; + + return api.eth + .newFilter(options) + .then((filterId) => { + subscriptions[subscriptionId] = { subscriptionId, filterId, addresses, callback, contract }; + + return api.eth.getFilterLogs(filterId); + }) + .then((logs) => callback(null, contract.parseEventLogs(logs))) + .then(() => subscriptionId) + .catch((error) => { + console.error('subscribeEvents', error); + throw error; + }); +} + +export function unsubscribeEvents (subscriptionId) { + api.eth + .uninstallFilter(subscriptions[subscriptionId].filterId) + .catch((error) => { + console.error('unsubscribeEvents', error); + }); + + delete subscriptions[subscriptionId]; +} + +function pollEvents () { + const loop = Object.values(subscriptions); + const timeout = () => setTimeout(pollEvents, 1000); + + Promise + .all(loop.map((subscription) => api.eth.getFilterChanges(subscription.filterId))) + .then((logsArray) => { + logsArray.forEach((logs, index) => { + const subscription = loop[index]; + + if (!logs || !logs.length) { + return; + } + + try { + subscription.callback(null, subscription.contract.parseEventLogs(logs)); + } catch (error) { + unsubscribeEvents(loop.subscriptionId); + console.error('pollEvents', error); + } + }); + + timeout(); + }) + .catch((error) => { + console.error('pollEvents', error); + timeout(); + }); +} + +export function attachInstances () { + pollEvents(); + + return Promise + .all([ + api.ethcore.registryAddress(), + api.ethcore.netChain() + ]) + .then(([registryAddress, netChain]) => { + const registry = api.newContract(abis.registry, registryAddress).instance; + isTest = netChain === 'morden' || netChain === 'testnet'; + + console.log(`contract was found at registry=${registryAddress}`); + console.log(`running on ${netChain}, isTest=${isTest}`); + + return Promise + .all([ + registry.getAddress.call({}, [api.util.sha3('playbasiccoinmgr'), 'A']), + registry.getAddress.call({}, [api.util.sha3('basiccoinreg'), 'A']), + registry.getAddress.call({}, [api.util.sha3('tokenreg'), 'A']) + ]); + }) + .then(([managerAddress, registryAddress, tokenregAddress]) => { + console.log(`contracts were found at basiccoinmgr=${managerAddress}, basiccoinreg=${registryAddress}, tokenreg=${registryAddress}`); + + managerInstance = api.newContract(abis.basiccoinmanager, managerAddress).instance; + registryInstance = api.newContract(abis.tokenreg, registryAddress).instance; + tokenregInstance = api.newContract(abis.tokenreg, tokenregAddress).instance; + + registries[registryInstance.address] = registryInstance; + registries[tokenregInstance.address] = tokenregInstance; + + return { + managerInstance, + registryInstance, + tokenregInstance + }; + }) + .catch((error) => { + console.error('attachInstances', error); + throw error; + }); +} + +export function totalSupply (address) { + return api.newContract(abis.eip20, address) + .instance.totalSupply.call(); +} + +export function getCoin (tokenreg, address) { + return registries[tokenreg].fromAddress + .call({}, [address]) + .then(([id, tla, base, name, owner]) => { + return { + id, tla, base, name, owner, + isGlobal: tokenregInstance.address === tokenreg + }; + }) + .catch((error) => { + console.error('getCoin', error); + throw error; + }); +} + +export function loadOwnedTokens (addresses) { + let total = new BigNumber(0); + + return Promise + .all( + addresses.map((address) => managerInstance.countByOwner.call({}, [address])) + ) + .then((counts) => { + return Promise.all( + addresses.reduce((promises, address, index) => { + total = counts[index].add(total); + for (let i = 0; counts[index].gt(i); i++) { + promises.push(managerInstance.getByOwner.call({}, [address, i])); + } + return promises; + }, []) + ); + }) + .then((_tokens) => { + const tokens = _tokens.reduce((tokens, token) => { + const [address, owner, tokenreg] = token; + tokens[owner] = tokens[owner] || []; + tokens[owner].push({ address, owner, tokenreg }); + return tokens; + }, {}); + + return { tokens, total }; + }) + .catch((error) => { + console.error('loadTokens', error); + throw error; + }); +} + +export function loadAllTokens () { + return managerInstance + .count.call() + .then((count) => { + const promises = []; + + for (let index = 0; count.gt(index); index++) { + promises.push(managerInstance.get.call({}, [index])); + } + + return Promise.all(promises); + }) + .then((_tokens) => { + const tokens = []; + + return Promise + .all( + _tokens.map(([address, owner, tokenreg]) => { + const isGlobal = tokenreg === tokenregInstance.address; + tokens.push({ address, owner, tokenreg, isGlobal }); + return registries[tokenreg].fromAddress.call({}, [address]); + }) + ) + .then((coins) => { + return tokens.map((token, index) => { + const [id, tla, base, name, owner] = coins[index]; + token.coin = { id, tla, base, name, owner }; + return token; + }); + }); + }) + .catch((error) => { + console.log('loadAllTokens', error); + throw error; + }); +} + +export function loadBalances (addresses) { + return loadAllTokens() + .then((tokens) => { + return Promise.all( + tokens.map((token) => { + return Promise.all( + addresses.map((address) => loadTokenBalance(token.address, address)) + ); + }) + ) + .then((_balances) => { + return tokens.map((token, tindex) => { + const balances = _balances[tindex]; + token.balances = addresses.map((address, aindex) => { + return { address, balance: balances[aindex] }; + }); + return token; + }); + }); + }) + .catch((error) => { + console.error('loadBalances', error); + throw error; + }); +} + +export function loadTokenBalance (tokenAddress, address) { + return api.newContract(abis.eip20, tokenAddress).instance + .balanceOf.call({}, [address]) + .catch((error) => { + console.error('loadTokenBalance', error); + throw error; + }); +} + +export function txLink (txHash) { + return `https://${isTest ? 'testnet.' : ''}etherscan.io/tx/${txHash}`; +} diff --git a/js/src/dapps/gavcoin.html b/js/src/dapps/gavcoin.html new file mode 100644 index 000000000..928310a52 --- /dev/null +++ b/js/src/dapps/gavcoin.html @@ -0,0 +1,16 @@ + + + + + + + GAVcoin + + +
+ + + + + + diff --git a/js/src/dapps/gavcoin.js b/js/src/dapps/gavcoin.js new file mode 100644 index 000000000..fae095ef2 --- /dev/null +++ b/js/src/dapps/gavcoin.js @@ -0,0 +1,33 @@ +// Copyright 2015, 2016 Ethcore (UK) Ltd. +// This file is part of Parity. + +// Parity is free software: you can redistribute it and/or modify +// it under the terms of the GNU General Public License as published by +// the Free Software Foundation, either version 3 of the License, or +// (at your option) any later version. + +// Parity is distributed in the hope that it will be useful, +// but WITHOUT ANY WARRANTY; without even the implied warranty of +// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the +// GNU General Public License for more details. + +// You should have received a copy of the GNU General Public License +// along with Parity. If not, see . + +import ReactDOM from 'react-dom'; +import React from 'react'; + +import injectTapEventPlugin from 'react-tap-event-plugin'; +injectTapEventPlugin(); + +import Application from './gavcoin/Application'; + +import '../../assets/fonts/Roboto/font.css'; +import '../../assets/fonts/RobotoMono/font.css'; +import './style.css'; +import './gavcoin.html'; + +ReactDOM.render( + , + document.querySelector('#container') +); diff --git a/js/src/dapps/gavcoin/AccountSelector/AccountItem/accountItem.css b/js/src/dapps/gavcoin/AccountSelector/AccountItem/accountItem.css new file mode 100644 index 000000000..3b7457cb7 --- /dev/null +++ b/js/src/dapps/gavcoin/AccountSelector/AccountItem/accountItem.css @@ -0,0 +1,45 @@ +/* Copyright 2015, 2016 Ethcore (UK) Ltd. +/* This file is part of Parity. +/* +/* Parity is free software: you can redistribute it and/or modify +/* it under the terms of the GNU General Public License as published by +/* the Free Software Foundation, either version 3 of the License, or +/* (at your option) any later version. +/* +/* Parity is distributed in the hope that it will be useful, +/* but WITHOUT ANY WARRANTY; without even the implied warranty of +/* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the +/* GNU General Public License for more details. +/* +/* You should have received a copy of the GNU General Public License +/* along with Parity. If not, see . +*/ +.account { + padding: 4px 0 !important; + display: inline-block; +} + +.name { + display: inline-block; + margin-right: 1em; + text-transform: uppercase; +} + +.balance { + display: inline-block; + text-align: right; + color: #aaa; + font-family: 'Roboto Mono', monospace; +} + +.details { + display: inline-block; +} + +.image { + display: inline-block; +} + +.image img { + margin: 0 1em -11px 0; +} diff --git a/js/src/dapps/gavcoin/AccountSelector/AccountItem/accountItem.js b/js/src/dapps/gavcoin/AccountSelector/AccountItem/accountItem.js new file mode 100644 index 000000000..1a55e7f93 --- /dev/null +++ b/js/src/dapps/gavcoin/AccountSelector/AccountItem/accountItem.js @@ -0,0 +1,63 @@ +// Copyright 2015, 2016 Ethcore (UK) Ltd. +// This file is part of Parity. + +// Parity is free software: you can redistribute it and/or modify +// it under the terms of the GNU General Public License as published by +// the Free Software Foundation, either version 3 of the License, or +// (at your option) any later version. + +// Parity is distributed in the hope that it will be useful, +// but WITHOUT ANY WARRANTY; without even the implied warranty of +// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the +// GNU General Public License for more details. + +// You should have received a copy of the GNU General Public License +// along with Parity. If not, see . + +import React, { Component, PropTypes } from 'react'; + +import IdentityIcon from '../../IdentityIcon'; + +import styles from './accountItem.css'; + +export default class AccountItem extends Component { + static propTypes = { + account: PropTypes.object, + gavBalance: PropTypes.bool + }; + + render () { + const { account, gavBalance } = this.props; + + let balance; + let token; + + if (gavBalance) { + if (account.gavBalance) { + balance = account.gavBalance; + token = 'GAV'; + } + } else { + if (account.ethBalance) { + balance = account.ethBalance; + token = 'ETH'; + } + } + + return ( +
+
+ +
+
+
+ { account.name || account.address } +
+
+ { balance } { token } +
+
+
+ ); + } +} diff --git a/js/src/dapps/gavcoin/AccountSelector/AccountItem/index.js b/js/src/dapps/gavcoin/AccountSelector/AccountItem/index.js new file mode 100644 index 000000000..570b3415a --- /dev/null +++ b/js/src/dapps/gavcoin/AccountSelector/AccountItem/index.js @@ -0,0 +1,17 @@ +// Copyright 2015, 2016 Ethcore (UK) Ltd. +// This file is part of Parity. + +// Parity is free software: you can redistribute it and/or modify +// it under the terms of the GNU General Public License as published by +// the Free Software Foundation, either version 3 of the License, or +// (at your option) any later version. + +// Parity is distributed in the hope that it will be useful, +// but WITHOUT ANY WARRANTY; without even the implied warranty of +// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the +// GNU General Public License for more details. + +// You should have received a copy of the GNU General Public License +// along with Parity. If not, see . + +export default from './accountItem'; diff --git a/js/src/dapps/gavcoin/AccountSelector/accountSelector.js b/js/src/dapps/gavcoin/AccountSelector/accountSelector.js new file mode 100644 index 000000000..e37d23598 --- /dev/null +++ b/js/src/dapps/gavcoin/AccountSelector/accountSelector.js @@ -0,0 +1,102 @@ +// Copyright 2015, 2016 Ethcore (UK) Ltd. +// This file is part of Parity. + +// Parity is free software: you can redistribute it and/or modify +// it under the terms of the GNU General Public License as published by +// the Free Software Foundation, either version 3 of the License, or +// (at your option) any later version. + +// Parity is distributed in the hope that it will be useful, +// but WITHOUT ANY WARRANTY; without even the implied warranty of +// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the +// GNU General Public License for more details. + +// You should have received a copy of the GNU General Public License +// along with Parity. If not, see . + +import BigNumber from 'bignumber.js'; +import React, { Component, PropTypes } from 'react'; +import { MenuItem, SelectField } from 'material-ui'; + +import AccountItem from './AccountItem'; + +const NAME_ID = ' '; +let lastSelectedAccount = {}; + +export default class AccountSelect extends Component { + static propTypes = { + accounts: PropTypes.array, + account: PropTypes.object, + anyAccount: PropTypes.bool, + gavBalance: PropTypes.bool, + onSelect: PropTypes.func, + errorText: PropTypes.string, + floatingLabelText: PropTypes.string, + hintText: PropTypes.string + } + + componentDidMount () { + this.props.onSelect(lastSelectedAccount); + } + + render () { + const { account, accounts, anyAccount, errorText, floatingLabelText, gavBalance, hintText } = this.props; + + return ( + + { renderAccounts(accounts, { anyAccount, gavBalance }) } + + ); + } + + onSelect = (event, idx, account) => { + lastSelectedAccount = account || {}; + this.props.onSelect(lastSelectedAccount); + } +} + +function isPositive (numberStr) { + return new BigNumber(numberStr.replace(',', '')).gt(0); +} + +export function renderAccounts (accounts, options = {}) { + return accounts + .filter((account) => { + if (options.anyAccount) { + return true; + } + + if (account.uuid) { + return isPositive(account[options.gavBalance ? 'gavBalance' : 'ethBalance']); + } + + return false; + }) + .map((account) => { + const item = ( + + ); + + return ( + + { item } + + ); + }); +} diff --git a/js/src/dapps/gavcoin/AccountSelector/index.js b/js/src/dapps/gavcoin/AccountSelector/index.js new file mode 100644 index 000000000..de529244a --- /dev/null +++ b/js/src/dapps/gavcoin/AccountSelector/index.js @@ -0,0 +1,22 @@ +// Copyright 2015, 2016 Ethcore (UK) Ltd. +// This file is part of Parity. + +// Parity is free software: you can redistribute it and/or modify +// it under the terms of the GNU General Public License as published by +// the Free Software Foundation, either version 3 of the License, or +// (at your option) any later version. + +// Parity is distributed in the hope that it will be useful, +// but WITHOUT ANY WARRANTY; without even the implied warranty of +// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the +// GNU General Public License for more details. + +// You should have received a copy of the GNU General Public License +// along with Parity. If not, see . + +import { renderAccounts } from './accountSelector'; + +export default from './accountSelector'; +export { + renderAccounts +}; diff --git a/js/src/dapps/gavcoin/AccountSelectorText/accountSelectorText.css b/js/src/dapps/gavcoin/AccountSelectorText/accountSelectorText.css new file mode 100644 index 000000000..0a969770d --- /dev/null +++ b/js/src/dapps/gavcoin/AccountSelectorText/accountSelectorText.css @@ -0,0 +1,28 @@ +/* Copyright 2015, 2016 Ethcore (UK) Ltd. +/* This file is part of Parity. +/* +/* Parity is free software: you can redistribute it and/or modify +/* it under the terms of the GNU General Public License as published by +/* the Free Software Foundation, either version 3 of the License, or +/* (at your option) any later version. +/* +/* Parity is distributed in the hope that it will be useful, +/* but WITHOUT ANY WARRANTY; without even the implied warranty of +/* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the +/* GNU General Public License for more details. +/* +/* You should have received a copy of the GNU General Public License +/* along with Parity. If not, see . +*/ +.addrtext { + position: relative; +} + +.input input { + padding-left: 48px !important; +} + +.addricon { + position: absolute; + top: 37px; +} diff --git a/js/src/dapps/gavcoin/AccountSelectorText/accountSelectorText.js b/js/src/dapps/gavcoin/AccountSelectorText/accountSelectorText.js new file mode 100644 index 000000000..a83b0ec87 --- /dev/null +++ b/js/src/dapps/gavcoin/AccountSelectorText/accountSelectorText.js @@ -0,0 +1,106 @@ +// Copyright 2015, 2016 Ethcore (UK) Ltd. +// This file is part of Parity. + +// Parity is free software: you can redistribute it and/or modify +// it under the terms of the GNU General Public License as published by +// the Free Software Foundation, either version 3 of the License, or +// (at your option) any later version. + +// Parity is distributed in the hope that it will be useful, +// but WITHOUT ANY WARRANTY; without even the implied warranty of +// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the +// GNU General Public License for more details. + +// You should have received a copy of the GNU General Public License +// along with Parity. If not, see . + +import React, { Component, PropTypes } from 'react'; +import { TextField } from 'material-ui'; + +import IdentityIcon from '../IdentityIcon'; +import AccountSelector from '../AccountSelector'; + +import styles from './accountSelectorText.css'; + +const NAME_ID = ' '; + +export default class AccountSelectorText extends Component { + static propTypes = { + accounts: PropTypes.array, + account: PropTypes.object, + errorText: PropTypes.string, + gavBalance: PropTypes.bool, + anyAccount: PropTypes.bool, + floatingLabelText: PropTypes.string, + hintText: PropTypes.string, + selector: PropTypes.bool, + onChange: PropTypes.func + } + + render () { + const { selector } = this.props; + + return selector + ? this.renderDropDown() + : this.renderTextField(); + } + + renderDropDown () { + const { account, accounts, anyAccount, errorText, gavBalance, hintText, floatingLabelText, onChange } = this.props; + + return ( + + ); + } + + renderTextField () { + const { account, errorText, hintText, floatingLabelText } = this.props; + + return ( +
+ + { this.renderAddressIcon() } +
+ ); + } + + renderAddressIcon () { + const { account } = this.props; + + if (!account.address) { + return null; + } + + return ( +
+ +
+ ); + } + + onChangeAddress = (event, address) => { + const lower = address.toLowerCase(); + const account = this.props.accounts.find((_account) => _account.address.toLowerCase() === lower); + + this.props.onChange(account || { address }); + } +} diff --git a/js/src/dapps/gavcoin/AccountSelectorText/index.js b/js/src/dapps/gavcoin/AccountSelectorText/index.js new file mode 100644 index 000000000..c3aab3e52 --- /dev/null +++ b/js/src/dapps/gavcoin/AccountSelectorText/index.js @@ -0,0 +1,17 @@ +// Copyright 2015, 2016 Ethcore (UK) Ltd. +// This file is part of Parity. + +// Parity is free software: you can redistribute it and/or modify +// it under the terms of the GNU General Public License as published by +// the Free Software Foundation, either version 3 of the License, or +// (at your option) any later version. + +// Parity is distributed in the hope that it will be useful, +// but WITHOUT ANY WARRANTY; without even the implied warranty of +// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the +// GNU General Public License for more details. + +// You should have received a copy of the GNU General Public License +// along with Parity. If not, see . + +export default from './accountSelectorText'; diff --git a/js/src/dapps/gavcoin/Accounts/accounts.css b/js/src/dapps/gavcoin/Accounts/accounts.css new file mode 100644 index 000000000..7c4511710 --- /dev/null +++ b/js/src/dapps/gavcoin/Accounts/accounts.css @@ -0,0 +1,47 @@ +/* Copyright 2015, 2016 Ethcore (UK) Ltd. +/* This file is part of Parity. +/* +/* Parity is free software: you can redistribute it and/or modify +/* it under the terms of the GNU General Public License as published by +/* the Free Software Foundation, either version 3 of the License, or +/* (at your option) any later version. +/* +/* Parity is distributed in the hope that it will be useful, +/* but WITHOUT ANY WARRANTY; without even the implied warranty of +/* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the +/* GNU General Public License for more details. +/* +/* You should have received a copy of the GNU General Public License +/* along with Parity. If not, see . +*/ +.accounts { + padding: 4em 2em 0 2em; + text-align: center; + width: 100%; +} + +.account { + margin: 0.5em !important; + background: rgb(50, 100, 150) !important; + display: inline-block !important; +} + +.account img { + margin-bottom: -11px; + margin-left: -11px; +} + +.balance { + font-family: 'Roboto Mono', monospace; + color: rgba(255, 255, 255, 1) !important; +} + +.name { + color: rgba(255, 255, 255, 0.7) !important; + margin-right: 1em; + text-transform: uppercase; +} + +.none { + color: rgba(255, 255, 255, 0.7); +} diff --git a/js/src/dapps/gavcoin/Accounts/accounts.js b/js/src/dapps/gavcoin/Accounts/accounts.js new file mode 100644 index 000000000..f510193ac --- /dev/null +++ b/js/src/dapps/gavcoin/Accounts/accounts.js @@ -0,0 +1,83 @@ +// Copyright 2015, 2016 Ethcore (UK) Ltd. +// This file is part of Parity. + +// Parity is free software: you can redistribute it and/or modify +// it under the terms of the GNU General Public License as published by +// the Free Software Foundation, either version 3 of the License, or +// (at your option) any later version. + +// Parity is distributed in the hope that it will be useful, +// but WITHOUT ANY WARRANTY; without even the implied warranty of +// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the +// GNU General Public License for more details. + +// You should have received a copy of the GNU General Public License +// along with Parity. If not, see . + +import React, { Component, PropTypes } from 'react'; +import { Chip } from 'material-ui'; + +import IdentityIcon from '../IdentityIcon'; + +import styles from './accounts.css'; + +export default class Accounts extends Component { + static contextTypes = { + api: PropTypes.object.isRequired, + instance: PropTypes.object.isRequired + } + + static propTypes = { + accounts: PropTypes.array + } + + render () { + const has = this._hasAccounts(); + + return ( +
+ { has ? this.renderAccounts() : this.renderEmpty() } +
+ ); + } + + renderEmpty () { + return ( +
+ You currently do not have any GAVcoin in any of your addresses, buy some +
+ ); + } + + renderAccounts () { + const { accounts } = this.props; + + return accounts + .filter((account) => account.hasGav) + .map((account) => { + return ( + + + + { account.name } + + + { account.gavBalance } + + + ); + }); + } + + _hasAccounts () { + const { accounts } = this.props; + + if (!accounts || !accounts.length) { + return false; + } + + return accounts.filter((account) => account.hasGav).length !== 0; + } +} diff --git a/js/src/dapps/gavcoin/Accounts/index.js b/js/src/dapps/gavcoin/Accounts/index.js new file mode 100644 index 000000000..e9be1d557 --- /dev/null +++ b/js/src/dapps/gavcoin/Accounts/index.js @@ -0,0 +1,17 @@ +// Copyright 2015, 2016 Ethcore (UK) Ltd. +// This file is part of Parity. + +// Parity is free software: you can redistribute it and/or modify +// it under the terms of the GNU General Public License as published by +// the Free Software Foundation, either version 3 of the License, or +// (at your option) any later version. + +// Parity is distributed in the hope that it will be useful, +// but WITHOUT ANY WARRANTY; without even the implied warranty of +// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the +// GNU General Public License for more details. + +// You should have received a copy of the GNU General Public License +// along with Parity. If not, see . + +export default from './accounts'; diff --git a/js/src/dapps/gavcoin/Actions/ActionBuyIn/actionBuyIn.js b/js/src/dapps/gavcoin/Actions/ActionBuyIn/actionBuyIn.js new file mode 100644 index 000000000..f97e20a6c --- /dev/null +++ b/js/src/dapps/gavcoin/Actions/ActionBuyIn/actionBuyIn.js @@ -0,0 +1,206 @@ +// Copyright 2015, 2016 Ethcore (UK) Ltd. +// This file is part of Parity. + +// Parity is free software: you can redistribute it and/or modify +// it under the terms of the GNU General Public License as published by +// the Free Software Foundation, either version 3 of the License, or +// (at your option) any later version. + +// Parity is distributed in the hope that it will be useful, +// but WITHOUT ANY WARRANTY; without even the implied warranty of +// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the +// GNU General Public License for more details. + +// You should have received a copy of the GNU General Public License +// along with Parity. If not, see . + +import BigNumber from 'bignumber.js'; +import React, { Component, PropTypes } from 'react'; + +import { Dialog, FlatButton, TextField } from 'material-ui'; + +import { api } from '../../parity'; +import AccountSelector from '../../AccountSelector'; +import { ERRORS, validateAccount, validatePositiveNumber } from '../validation'; + +import styles from '../actions.css'; + +const NAME_ID = ' '; + +export default class ActionBuyIn extends Component { + static contextTypes = { + instance: PropTypes.object.isRequired + } + + static propTypes = { + accounts: PropTypes.array, + price: PropTypes.object, + onClose: PropTypes.func + } + + state = { + account: {}, + accountError: ERRORS.invalidAccount, + amount: 0, + amountError: ERRORS.invalidAmount, + maxPrice: api.util.fromWei(this.props.price.mul(1.2)).toString(), + maxPriceError: null, + sending: false, + complete: false + } + + render () { + const { complete } = this.state; + + if (complete) { + return null; + } + + return ( + + { this.renderFields() } + + ); + } + + renderActions () { + const { complete } = this.state; + + if (complete) { + return ( + + ); + } + + const { accountError, amountError, maxPriceError, sending } = this.state; + const hasError = !!(amountError || accountError || maxPriceError); + + return ([ + , + + ]); + } + + renderFields () { + const maxPriceLabel = `maximum price in ETH (current ${api.util.fromWei(this.props.price).toFormat(3)})`; + + return ( +
+ + + +
+ ); + } + + onChangeAddress = (account) => { + this.setState({ + account, + accountError: validateAccount(account) + }, this.validateTotal); + } + + onChangeAmount = (event, amount) => { + this.setState({ + amount, + amountError: validatePositiveNumber(amount) + }, this.validateTotal); + } + + onChangeMaxPrice = (event, maxPrice) => { + this.setState({ + maxPrice, + maxPriceError: validatePositiveNumber(maxPrice) + }); + } + + validateTotal = () => { + const { account, accountError, amount, amountError } = this.state; + + if (accountError || amountError) { + return; + } + + if (new BigNumber(amount).gt(account.ethBalance.replace(',', ''))) { + this.setState({ + amountError: ERRORS.invalidTotal + }); + } + } + + onSend = () => { + const { instance } = this.context; + const maxPrice = api.util.toWei(this.state.maxPrice); + const values = [this.state.account.address, maxPrice.toString()]; + const options = { + from: this.state.account.address, + value: api.util.toWei(this.state.amount).toString() + }; + + this.setState({ + sending: true + }); + + instance.buyin + .estimateGas(options, values) + .then((gasEstimate) => { + options.gas = gasEstimate.mul(1.2).toFixed(0); + console.log(`buyin: gas estimated as ${gasEstimate.toFixed(0)} setting to ${options.gas}`); + + return instance.buyin.postTransaction(options, values); + }) + .then(() => { + this.props.onClose(); + this.setState({ + sending: false, + complete: true + }); + }) + .catch((error) => { + console.error('error', error); + this.setState({ + sending: false + }); + }); + } +} diff --git a/js/src/dapps/gavcoin/Actions/ActionBuyIn/index.js b/js/src/dapps/gavcoin/Actions/ActionBuyIn/index.js new file mode 100644 index 000000000..2c0c2374f --- /dev/null +++ b/js/src/dapps/gavcoin/Actions/ActionBuyIn/index.js @@ -0,0 +1,17 @@ +// Copyright 2015, 2016 Ethcore (UK) Ltd. +// This file is part of Parity. + +// Parity is free software: you can redistribute it and/or modify +// it under the terms of the GNU General Public License as published by +// the Free Software Foundation, either version 3 of the License, or +// (at your option) any later version. + +// Parity is distributed in the hope that it will be useful, +// but WITHOUT ANY WARRANTY; without even the implied warranty of +// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the +// GNU General Public License for more details. + +// You should have received a copy of the GNU General Public License +// along with Parity. If not, see . + +export default from './actionBuyIn'; diff --git a/js/src/dapps/gavcoin/Actions/ActionRefund/actionRefund.js b/js/src/dapps/gavcoin/Actions/ActionRefund/actionRefund.js new file mode 100644 index 000000000..8ebbfb351 --- /dev/null +++ b/js/src/dapps/gavcoin/Actions/ActionRefund/actionRefund.js @@ -0,0 +1,191 @@ +// Copyright 2015, 2016 Ethcore (UK) Ltd. +// This file is part of Parity. + +// Parity is free software: you can redistribute it and/or modify +// it under the terms of the GNU General Public License as published by +// the Free Software Foundation, either version 3 of the License, or +// (at your option) any later version. + +// Parity is distributed in the hope that it will be useful, +// but WITHOUT ANY WARRANTY; without even the implied warranty of +// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the +// GNU General Public License for more details. + +// You should have received a copy of the GNU General Public License +// along with Parity. If not, see . + +import BigNumber from 'bignumber.js'; +import React, { Component, PropTypes } from 'react'; + +import { Dialog, FlatButton, TextField } from 'material-ui'; + +import { api } from '../../parity'; +import AccountSelector from '../../AccountSelector'; +import { ERRORS, validateAccount, validatePositiveNumber } from '../validation'; + +import styles from '../actions.css'; + +const DIVISOR = 10 ** 6; +const NAME_ID = ' '; + +export default class ActionRefund extends Component { + static contextTypes = { + instance: PropTypes.object.isRequired + } + + static propTypes = { + accounts: PropTypes.array, + price: PropTypes.object, + onClose: PropTypes.func + } + + state = { + account: {}, + accountError: ERRORS.invalidAccount, + complete: false, + sending: false, + amount: 0, + amountError: ERRORS.invalidAmount, + price: api.util.fromWei(this.props.price).toString(), + priceError: null + } + + render () { + const { complete } = this.state; + + if (complete) { + return null; + } + + return ( + + { this.renderFields() } + + ); + } + + renderActions () { + if (this.state.complete) { + return ( + + ); + } + + const hasError = !!(this.state.priceError || this.state.amountError || this.state.accountError); + + return ([ + , + + ]); + } + + renderFields () { + const priceLabel = `price in ETH (current ${api.util.fromWei(this.props.price).toFormat(3)})`; + + return ( +
+ + + +
+ ); + } + + onChangeAddress = (account) => { + this.setState({ + account, + accountError: validateAccount(account) + }); + } + + onChangeAmount = (event, amount) => { + this.setState({ + amount, + amountError: validatePositiveNumber(amount) + }); + } + + onChangePrice = (event, price) => { + this.setState({ + price, + priceError: validatePositiveNumber(price) + }); + } + + onSend = () => { + const { instance } = this.context; + const price = api.util.toWei(this.state.price); + const amount = new BigNumber(this.state.amount).mul(DIVISOR); + const values = [price.toString(), amount.toFixed(0)]; + const options = { + from: this.state.account.address + }; + + this.setState({ + sending: true + }); + + instance.refund + .estimateGas(options, values) + .then((gasEstimate) => { + options.gas = gasEstimate.mul(1.2).toFixed(0); + console.log(`refund: gas estimated as ${gasEstimate.toFixed(0)} setting to ${options.gas}`); + + return instance.refund.postTransaction(options, values); + }) + .then(() => { + this.props.onClose(); + this.setState({ + sending: false, + complete: true + }); + }) + .catch((error) => { + console.error('error', error); + this.setState({ + sending: false + }); + }); + } +} diff --git a/js/src/dapps/gavcoin/Actions/ActionRefund/index.js b/js/src/dapps/gavcoin/Actions/ActionRefund/index.js new file mode 100644 index 000000000..d3b5b7e1b --- /dev/null +++ b/js/src/dapps/gavcoin/Actions/ActionRefund/index.js @@ -0,0 +1,17 @@ +// Copyright 2015, 2016 Ethcore (UK) Ltd. +// This file is part of Parity. + +// Parity is free software: you can redistribute it and/or modify +// it under the terms of the GNU General Public License as published by +// the Free Software Foundation, either version 3 of the License, or +// (at your option) any later version. + +// Parity is distributed in the hope that it will be useful, +// but WITHOUT ANY WARRANTY; without even the implied warranty of +// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the +// GNU General Public License for more details. + +// You should have received a copy of the GNU General Public License +// along with Parity. If not, see . + +export default from './actionRefund'; diff --git a/js/src/dapps/gavcoin/Actions/ActionTransfer/actionTransfer.js b/js/src/dapps/gavcoin/Actions/ActionTransfer/actionTransfer.js new file mode 100644 index 000000000..13ecc55e8 --- /dev/null +++ b/js/src/dapps/gavcoin/Actions/ActionTransfer/actionTransfer.js @@ -0,0 +1,220 @@ +// Copyright 2015, 2016 Ethcore (UK) Ltd. +// This file is part of Parity. + +// Parity is free software: you can redistribute it and/or modify +// it under the terms of the GNU General Public License as published by +// the Free Software Foundation, either version 3 of the License, or +// (at your option) any later version. + +// Parity is distributed in the hope that it will be useful, +// but WITHOUT ANY WARRANTY; without even the implied warranty of +// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the +// GNU General Public License for more details. + +// You should have received a copy of the GNU General Public License +// along with Parity. If not, see . + +import BigNumber from 'bignumber.js'; +import React, { Component, PropTypes } from 'react'; + +import { Dialog, FlatButton, TextField, Toggle } from 'material-ui'; + +import AccountSelector from '../../AccountSelector'; +import AccountSelectorText from '../../AccountSelectorText'; +import { ERRORS, validateAccount, validatePositiveNumber } from '../validation'; + +import styles from '../actions.css'; + +const DIVISOR = 10 ** 6; +const NAME_ID = ' '; + +export default class ActionTransfer extends Component { + static contextTypes = { + instance: PropTypes.object.isRequired + } + + static propTypes = { + accounts: PropTypes.array, + price: PropTypes.object, + onClose: PropTypes.func + } + + state = { + fromAccount: {}, + fromAccountError: ERRORS.invalidAccount, + toAccount: {}, + toAccountError: ERRORS.invalidRecipient, + inputAccount: false, + complete: false, + sending: false, + amount: 0, + amountError: ERRORS.invalidAmount + } + + render () { + const { complete } = this.state; + + if (complete) { + return null; + } + + return ( + + { this.renderFields() } + + ); + } + + renderActions () { + const { complete, sending, amountError, fromAccountError, toAccountError } = this.state; + + if (complete) { + return ( + + ); + } + + const hasError = !!(amountError || fromAccountError || toAccountError); + + return ([ + , + + ]); + } + + renderFields () { + const { accounts } = this.props; + const { fromAccount, fromAccountError, toAccount, toAccountError, inputAccount, amount, amountError } = this.state; + + return ( +
+ +
+ + +
+ +
+ ); + } + + onChangeFromAccount = (fromAccount) => { + this.setState({ + fromAccount, + fromAccountError: validateAccount(fromAccount) + }, this.validateTotal); + } + + onChangeToAccount = (toAccount) => { + this.setState({ + toAccount, + toAccountError: validateAccount(toAccount) + }); + } + + onChangeToInput = () => { + this.setState({ + inputAccount: !this.state.inputAccount + }); + } + + onChangeAmount = (event, amount) => { + this.setState({ + amount, + amountError: validatePositiveNumber(amount) + }, this.validateTotal); + } + + validateTotal = () => { + const { fromAccount, fromAccountError, amount, amountError } = this.state; + + if (fromAccountError || amountError) { + return; + } + + if (new BigNumber(amount).gt(fromAccount.gavBalance.replace(',', ''))) { + this.setState({ + amountError: ERRORS.invalidTotal + }); + } + } + + onSend = () => { + const { instance } = this.context; + const amount = new BigNumber(this.state.amount).mul(DIVISOR); + const values = [this.state.toAccount.address, amount.toFixed(0)]; + const options = { + from: this.state.fromAccount.address + }; + + this.setState({ + sending: true + }); + + instance.transfer + .estimateGas(options, values) + .then((gasEstimate) => { + options.gas = gasEstimate.mul(1.2).toFixed(0); + console.log(`transfer: gas estimated as ${gasEstimate.toFixed(0)} setting to ${options.gas}`); + + return instance.transfer.postTransaction(options, values); + }) + .then(() => { + this.props.onClose(); + this.setState({ + sending: false, + complete: true + }); + }) + .catch((error) => { + console.error('error', error); + this.setState({ + sending: false + }); + }); + } +} diff --git a/js/src/dapps/gavcoin/Actions/ActionTransfer/index.js b/js/src/dapps/gavcoin/Actions/ActionTransfer/index.js new file mode 100644 index 000000000..848800e00 --- /dev/null +++ b/js/src/dapps/gavcoin/Actions/ActionTransfer/index.js @@ -0,0 +1,17 @@ +// Copyright 2015, 2016 Ethcore (UK) Ltd. +// This file is part of Parity. + +// Parity is free software: you can redistribute it and/or modify +// it under the terms of the GNU General Public License as published by +// the Free Software Foundation, either version 3 of the License, or +// (at your option) any later version. + +// Parity is distributed in the hope that it will be useful, +// but WITHOUT ANY WARRANTY; without even the implied warranty of +// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the +// GNU General Public License for more details. + +// You should have received a copy of the GNU General Public License +// along with Parity. If not, see . + +export default from './actionTransfer'; diff --git a/js/src/dapps/gavcoin/Actions/StepComplete/index.js b/js/src/dapps/gavcoin/Actions/StepComplete/index.js new file mode 100644 index 000000000..7b95b1937 --- /dev/null +++ b/js/src/dapps/gavcoin/Actions/StepComplete/index.js @@ -0,0 +1,17 @@ +// Copyright 2015, 2016 Ethcore (UK) Ltd. +// This file is part of Parity. + +// Parity is free software: you can redistribute it and/or modify +// it under the terms of the GNU General Public License as published by +// the Free Software Foundation, either version 3 of the License, or +// (at your option) any later version. + +// Parity is distributed in the hope that it will be useful, +// but WITHOUT ANY WARRANTY; without even the implied warranty of +// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the +// GNU General Public License for more details. + +// You should have received a copy of the GNU General Public License +// along with Parity. If not, see . + +export default from './stepComplete'; diff --git a/js/src/dapps/gavcoin/Actions/StepComplete/stepComplete.js b/js/src/dapps/gavcoin/Actions/StepComplete/stepComplete.js new file mode 100644 index 000000000..a066a7c30 --- /dev/null +++ b/js/src/dapps/gavcoin/Actions/StepComplete/stepComplete.js @@ -0,0 +1,29 @@ +// Copyright 2015, 2016 Ethcore (UK) Ltd. +// This file is part of Parity. + +// Parity is free software: you can redistribute it and/or modify +// it under the terms of the GNU General Public License as published by +// the Free Software Foundation, either version 3 of the License, or +// (at your option) any later version. + +// Parity is distributed in the hope that it will be useful, +// but WITHOUT ANY WARRANTY; without even the implied warranty of +// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the +// GNU General Public License for more details. + +// You should have received a copy of the GNU General Public License +// along with Parity. If not, see . + +import React, { Component } from 'react'; + +import styles from '../actions.css'; + +export default class StepComplete extends Component { + render () { + return ( +
+ Your transaction has been posted. Please visit the Parity Signer to authenticate the transfer. +
+ ); + } +} diff --git a/js/src/dapps/gavcoin/Actions/actions.css b/js/src/dapps/gavcoin/Actions/actions.css new file mode 100644 index 000000000..90ca3e18f --- /dev/null +++ b/js/src/dapps/gavcoin/Actions/actions.css @@ -0,0 +1,72 @@ +/* Copyright 2015, 2016 Ethcore (UK) Ltd. +/* This file is part of Parity. +/* +/* Parity is free software: you can redistribute it and/or modify +/* it under the terms of the GNU General Public License as published by +/* the Free Software Foundation, either version 3 of the License, or +/* (at your option) any later version. +/* +/* Parity is distributed in the hope that it will be useful, +/* but WITHOUT ANY WARRANTY; without even the implied warranty of +/* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the +/* GNU General Public License for more details. +/* +/* You should have received a copy of the GNU General Public License +/* along with Parity. If not, see . +*/ +.actions { + text-align: center; + padding: 2em 2em 0 2em; + width: 100%; +} + +.button { + margin: 0 0.5em; +} + +.button button { + background-color: rgba(50, 100, 150, 1) !important; + height: 56px !important; + padding: 0 10px !important; +} + +.button button[disabled] { + background-color: rgba(50, 50, 50, 0.25) !important; +} + +.dialog { +} + +.dialog h3 { + color: rgba(50, 100, 150, 1) !important; + text-transform: uppercase; +} + +.dialogtext { + padding-top: 1em; +} + +.link, .link:hover, .link:visited { + color: rgb(0, 188, 212); + text-decoration: none; +} + +.overlay { + position: relative; +} + +.overlay svg { + opacity: 0; +} + +.toggle { + text-align: right; +} + +.overlaytoggle { + position: absolute !important; + top: 40px; + right: 0; + display: inline-block !important; + width: auto !important; +} diff --git a/js/src/dapps/gavcoin/Actions/actions.js b/js/src/dapps/gavcoin/Actions/actions.js new file mode 100644 index 000000000..e58d2223f --- /dev/null +++ b/js/src/dapps/gavcoin/Actions/actions.js @@ -0,0 +1,76 @@ +// Copyright 2015, 2016 Ethcore (UK) Ltd. +// This file is part of Parity. + +// Parity is free software: you can redistribute it and/or modify +// it under the terms of the GNU General Public License as published by +// the Free Software Foundation, either version 3 of the License, or +// (at your option) any later version. + +// Parity is distributed in the hope that it will be useful, +// but WITHOUT ANY WARRANTY; without even the implied warranty of +// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the +// GNU General Public License for more details. + +// You should have received a copy of the GNU General Public License +// along with Parity. If not, see . + +import React, { Component, PropTypes } from 'react'; + +import { RaisedButton } from 'material-ui'; +import ActionAddShoppingCart from 'material-ui/svg-icons/action/add-shopping-cart'; +// import AvReplay from 'material-ui/svg-icons/av/replay'; +import ContentSend from 'material-ui/svg-icons/content/send'; + +import styles from './actions.css'; + +export default class Actions extends Component { + static propTypes = { + onAction: PropTypes.func.isRequired, + gavBalance: PropTypes.object.isRequired + } + + render () { + const { gavBalance } = this.props; + + return ( +
+ } + label='buy coins' + primary + onTouchTap={ this.onBuyIn } /> + } + label='send coins' + primary + onTouchTap={ this.onTransfer } /> +
+ ); + + // } + // label='claim refund' + // primary + // onTouchTap={ this.onRefund } /> + } + + onBuyIn = () => { + this.props.onAction('BuyIn'); + } + + onTransfer = () => { + const { gavBalance } = this.props; + + if (gavBalance && gavBalance.gt(0)) { + this.props.onAction('Transfer'); + } + } + + onRefund = () => { + this.props.onAction('Refund'); + } +} diff --git a/js/src/dapps/gavcoin/Actions/index.js b/js/src/dapps/gavcoin/Actions/index.js new file mode 100644 index 000000000..865bd65ce --- /dev/null +++ b/js/src/dapps/gavcoin/Actions/index.js @@ -0,0 +1,27 @@ +// Copyright 2015, 2016 Ethcore (UK) Ltd. +// This file is part of Parity. + +// Parity is free software: you can redistribute it and/or modify +// it under the terms of the GNU General Public License as published by +// the Free Software Foundation, either version 3 of the License, or +// (at your option) any later version. + +// Parity is distributed in the hope that it will be useful, +// but WITHOUT ANY WARRANTY; without even the implied warranty of +// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the +// GNU General Public License for more details. + +// You should have received a copy of the GNU General Public License +// along with Parity. If not, see . + +import ActionBuyIn from './ActionBuyIn'; +import ActionRefund from './ActionRefund'; +import ActionTransfer from './ActionTransfer'; + +export default from './actions'; + +export { + ActionBuyIn, + ActionRefund, + ActionTransfer +}; diff --git a/js/src/dapps/gavcoin/Actions/validation.js b/js/src/dapps/gavcoin/Actions/validation.js new file mode 100644 index 000000000..d494c3392 --- /dev/null +++ b/js/src/dapps/gavcoin/Actions/validation.js @@ -0,0 +1,56 @@ +// Copyright 2015, 2016 Ethcore (UK) Ltd. +// This file is part of Parity. + +// Parity is free software: you can redistribute it and/or modify +// it under the terms of the GNU General Public License as published by +// the Free Software Foundation, either version 3 of the License, or +// (at your option) any later version. + +// Parity is distributed in the hope that it will be useful, +// but WITHOUT ANY WARRANTY; without even the implied warranty of +// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the +// GNU General Public License for more details. + +// You should have received a copy of the GNU General Public License +// along with Parity. If not, see . + +import BigNumber from 'bignumber.js'; + +import { api } from '../parity'; + +export const ERRORS = { + invalidAccount: 'please select an account to transact with', + invalidRecipient: 'please select an account to send to', + invalidAddress: 'the address is not in the correct format', + invalidAmount: 'please enter a positive amount > 0', + invalidTotal: 'the amount is greater than the availale balance' +}; + +export function validatePositiveNumber (value) { + let bn = null; + + try { + bn = new BigNumber(value); + } catch (e) { + } + + if (!bn || !bn.gt(0)) { + return ERRORS.invalidAmount; + } + + return null; +} + +export function validateAccount (account) { + if (!account || !account.address) { + return ERRORS.invalidAccount; + } + + if (!api.util.isAddressValid(account.address)) { + return ERRORS.invalidAddress; + } + + account.address = api.util.toChecksumAddress(account.address); + + return null; +} diff --git a/js/src/dapps/gavcoin/Application/application.js b/js/src/dapps/gavcoin/Application/application.js new file mode 100644 index 000000000..04c5abe01 --- /dev/null +++ b/js/src/dapps/gavcoin/Application/application.js @@ -0,0 +1,239 @@ +// Copyright 2015, 2016 Ethcore (UK) Ltd. +// This file is part of Parity. + +// Parity is free software: you can redistribute it and/or modify +// it under the terms of the GNU General Public License as published by +// the Free Software Foundation, either version 3 of the License, or +// (at your option) any later version. + +// Parity is distributed in the hope that it will be useful, +// but WITHOUT ANY WARRANTY; without even the implied warranty of +// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the +// GNU General Public License for more details. + +// You should have received a copy of the GNU General Public License +// along with Parity. If not, see . + +import BigNumber from 'bignumber.js'; +import React, { Component, PropTypes } from 'react'; + +import getMuiTheme from 'material-ui/styles/getMuiTheme'; +import lightBaseTheme from 'material-ui/styles/baseThemes/lightBaseTheme'; + +const muiTheme = getMuiTheme(lightBaseTheme); + +import { api } from '../parity'; + +import * as abis from '../../../contracts/abi'; + +import Accounts from '../Accounts'; +import Actions, { ActionBuyIn, ActionRefund, ActionTransfer } from '../Actions'; +import Events from '../Events'; +import Loading from '../Loading'; +import Status from '../Status'; + +const DIVISOR = 10 ** 6; + +export default class Application extends Component { + static childContextTypes = { + api: PropTypes.object, + contract: PropTypes.object, + instance: PropTypes.object, + muiTheme: PropTypes.object + }; + + state = { + action: null, + address: null, + accounts: [], + blockNumber: new BigNumber(-1), + ethBalance: new BigNumber(0), + gavBalance: new BigNumber(0), + instance: null, + loading: true, + price: null, + remaining: null, + totalSupply: null + } + + componentDidMount () { + this.attachInterface(); + } + + render () { + const { accounts, address, blockNumber, gavBalance, loading, price, remaining, totalSupply } = this.state; + + if (loading) { + return ( + + ); + } + + return ( +
+ { this.renderModals() } + + + + + +
+ ); + } + + renderModals () { + const { action, accounts, price } = this.state; + + switch (action) { + case 'BuyIn': + return ( + + ); + case 'Refund': + return ( + + ); + case 'Transfer': + return ( + + ); + default: + return null; + } + } + + getChildContext () { + const { contract, instance } = this.state; + + return { + api, + contract, + instance, + muiTheme + }; + } + + onAction = (action) => { + this.setState({ + action + }); + } + + onActionClose = () => { + this.setState({ + action: null + }); + } + + onNewBlockNumber = (_error, blockNumber) => { + const { instance, accounts } = this.state; + + if (_error) { + console.error('onNewBlockNumber', _error); + return; + } + + Promise + .all([ + instance.totalSupply.call(), + instance.remaining.call(), + instance.price.call() + ]) + .then(([totalSupply, remaining, price]) => { + this.setState({ + blockNumber, + totalSupply, + remaining, + price + }); + + const gavQueries = accounts.map((account) => instance.balanceOf.call({}, [account.address])); + const ethQueries = accounts.map((account) => api.eth.getBalance(account.address)); + + return Promise.all([ + Promise.all(gavQueries), + Promise.all(ethQueries) + ]); + }) + .then(([gavBalances, ethBalances]) => { + this.setState({ + ethBalance: ethBalances.reduce((total, balance) => total.add(balance), new BigNumber(0)), + gavBalance: gavBalances.reduce((total, balance) => total.add(balance), new BigNumber(0)), + accounts: accounts.map((account, idx) => { + const ethBalance = ethBalances[idx]; + const gavBalance = gavBalances[idx]; + + account.ethBalance = api.util.fromWei(ethBalance).toFormat(3); + account.gavBalance = gavBalance.div(DIVISOR).toFormat(6); + account.hasGav = gavBalance.gt(0); + + return account; + }) + }); + }) + .catch((error) => { + console.error('onNewBlockNumber', error); + }); + } + + attachInterface = () => { + api.ethcore + .registryAddress() + .then((registryAddress) => { + console.log(`the registry was found at ${registryAddress}`); + + const registry = api.newContract(abis.registry, registryAddress).instance; + + return Promise + .all([ + registry.getAddress.call({}, [api.util.sha3('gavcoin'), 'A']), + api.personal.listAccounts(), + api.personal.accountsInfo() + ]); + }) + .then(([address, addresses, infos]) => { + console.log(`gavcoin was found at ${address}`); + + const contract = api.newContract(abis.gavcoin, address); + + this.setState({ + loading: false, + address, + contract, + instance: contract.instance, + accounts: addresses.map((address) => { + const info = infos[address]; + + return { + address, + name: info.name || 'Unnamed', + uuid: info.uuid + }; + }) + }); + + api.subscribe('eth_blockNumber', this.onNewBlockNumber); + }) + .catch((error) => { + console.error('attachInterface', error); + }); + } +} diff --git a/js/src/dapps/gavcoin/Application/index.js b/js/src/dapps/gavcoin/Application/index.js new file mode 100644 index 000000000..236578226 --- /dev/null +++ b/js/src/dapps/gavcoin/Application/index.js @@ -0,0 +1,17 @@ +// Copyright 2015, 2016 Ethcore (UK) Ltd. +// This file is part of Parity. + +// Parity is free software: you can redistribute it and/or modify +// it under the terms of the GNU General Public License as published by +// the Free Software Foundation, either version 3 of the License, or +// (at your option) any later version. + +// Parity is distributed in the hope that it will be useful, +// but WITHOUT ANY WARRANTY; without even the implied warranty of +// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the +// GNU General Public License for more details. + +// You should have received a copy of the GNU General Public License +// along with Parity. If not, see . + +export default from './application'; diff --git a/js/src/dapps/gavcoin/Events/Event/event.js b/js/src/dapps/gavcoin/Events/Event/event.js new file mode 100644 index 000000000..4fb29a382 --- /dev/null +++ b/js/src/dapps/gavcoin/Events/Event/event.js @@ -0,0 +1,129 @@ +// Copyright 2015, 2016 Ethcore (UK) Ltd. +// This file is part of Parity. + +// Parity is free software: you can redistribute it and/or modify +// it under the terms of the GNU General Public License as published by +// the Free Software Foundation, either version 3 of the License, or +// (at your option) any later version. + +// Parity is distributed in the hope that it will be useful, +// but WITHOUT ANY WARRANTY; without even the implied warranty of +// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the +// GNU General Public License for more details. + +// You should have received a copy of the GNU General Public License +// along with Parity. If not, see . + +import React, { Component, PropTypes } from 'react'; + +import IdentityIcon from '../../IdentityIcon'; +import { formatBlockNumber, formatCoins, formatEth } from '../../format'; + +import styles from '../events.css'; + +const EMPTY_COLUMN = ( + +); + +export default class Event extends Component { + static contextTypes = { + accounts: PropTypes.array.isRequired + } + + static propTypes = { + event: PropTypes.object, + value: PropTypes.object, + price: PropTypes.object, + fromAddress: PropTypes.string, + toAddress: PropTypes.string + } + + render () { + const { event, fromAddress, toAddress, price, value } = this.props; + const { blockNumber, state, type } = event; + const cls = `${styles.event} ${styles[state]} ${styles[type.toLowerCase()]}`; + + return ( + + { this.renderBlockNumber(blockNumber) } + { this.renderType(type) } + { this.renderValue(value) } + { this.renderPrice(price) } + { this.renderAddress(fromAddress) } + { this.renderAddress(toAddress) } + + ); + } + + renderBlockNumber (blockNumber) { + return ( + + { formatBlockNumber(blockNumber) } + + ); + } + + renderAddress (address) { + if (!address) { + return EMPTY_COLUMN; + } + + return ( + + + { this.renderAddressName(address) } + + ); + } + + renderAddressName (address) { + const { accounts } = this.context; + const account = accounts.find((_account) => _account.address === address); + + if (account) { + return ( +
+ { account.name } +
+ ); + } + + return ( +
+ { address } +
+ ); + } + + renderPrice (price) { + if (!price) { + return EMPTY_COLUMN; + } + + return ( + + { formatEth(price) } ETH + + ); + } + + renderValue (value) { + if (!value) { + return EMPTY_COLUMN; + } + + return ( + + { formatCoins(value) } GAV + + ); + } + + renderType (type) { + return ( + + { type } + + ); + } +} diff --git a/js/src/dapps/gavcoin/Events/Event/index.js b/js/src/dapps/gavcoin/Events/Event/index.js new file mode 100644 index 000000000..0925882d3 --- /dev/null +++ b/js/src/dapps/gavcoin/Events/Event/index.js @@ -0,0 +1,17 @@ +// Copyright 2015, 2016 Ethcore (UK) Ltd. +// This file is part of Parity. + +// Parity is free software: you can redistribute it and/or modify +// it under the terms of the GNU General Public License as published by +// the Free Software Foundation, either version 3 of the License, or +// (at your option) any later version. + +// Parity is distributed in the hope that it will be useful, +// but WITHOUT ANY WARRANTY; without even the implied warranty of +// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the +// GNU General Public License for more details. + +// You should have received a copy of the GNU General Public License +// along with Parity. If not, see . + +export default from './event'; diff --git a/js/src/dapps/gavcoin/Events/EventBuyin/eventBuyin.js b/js/src/dapps/gavcoin/Events/EventBuyin/eventBuyin.js new file mode 100644 index 000000000..571da1bef --- /dev/null +++ b/js/src/dapps/gavcoin/Events/EventBuyin/eventBuyin.js @@ -0,0 +1,38 @@ +// Copyright 2015, 2016 Ethcore (UK) Ltd. +// This file is part of Parity. + +// Parity is free software: you can redistribute it and/or modify +// it under the terms of the GNU General Public License as published by +// the Free Software Foundation, either version 3 of the License, or +// (at your option) any later version. + +// Parity is distributed in the hope that it will be useful, +// but WITHOUT ANY WARRANTY; without even the implied warranty of +// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the +// GNU General Public License for more details. + +// You should have received a copy of the GNU General Public License +// along with Parity. If not, see . + +import React, { Component, PropTypes } from 'react'; + +import Event from '../Event'; + +export default class EventBuyin extends Component { + static propTypes = { + event: PropTypes.object + } + + render () { + const { event } = this.props; + const { buyer, price, amount } = event.params; + + return ( + + ); + } +} diff --git a/js/src/dapps/gavcoin/Events/EventBuyin/index.js b/js/src/dapps/gavcoin/Events/EventBuyin/index.js new file mode 100644 index 000000000..77edc578a --- /dev/null +++ b/js/src/dapps/gavcoin/Events/EventBuyin/index.js @@ -0,0 +1,17 @@ +// Copyright 2015, 2016 Ethcore (UK) Ltd. +// This file is part of Parity. + +// Parity is free software: you can redistribute it and/or modify +// it under the terms of the GNU General Public License as published by +// the Free Software Foundation, either version 3 of the License, or +// (at your option) any later version. + +// Parity is distributed in the hope that it will be useful, +// but WITHOUT ANY WARRANTY; without even the implied warranty of +// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the +// GNU General Public License for more details. + +// You should have received a copy of the GNU General Public License +// along with Parity. If not, see . + +export default from './eventBuyin'; diff --git a/js/src/dapps/gavcoin/Events/EventNewTranch/eventNewTranch.js b/js/src/dapps/gavcoin/Events/EventNewTranch/eventNewTranch.js new file mode 100644 index 000000000..16fa05351 --- /dev/null +++ b/js/src/dapps/gavcoin/Events/EventNewTranch/eventNewTranch.js @@ -0,0 +1,36 @@ +// Copyright 2015, 2016 Ethcore (UK) Ltd. +// This file is part of Parity. + +// Parity is free software: you can redistribute it and/or modify +// it under the terms of the GNU General Public License as published by +// the Free Software Foundation, either version 3 of the License, or +// (at your option) any later version. + +// Parity is distributed in the hope that it will be useful, +// but WITHOUT ANY WARRANTY; without even the implied warranty of +// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the +// GNU General Public License for more details. + +// You should have received a copy of the GNU General Public License +// along with Parity. If not, see . + +import React, { Component, PropTypes } from 'react'; + +import Event from '../Event'; + +export default class EventNewTranch extends Component { + static propTypes = { + event: PropTypes.object + } + + render () { + const { event } = this.props; + const { price } = event.params; + + return ( + + ); + } +} diff --git a/js/src/dapps/gavcoin/Events/EventNewTranch/index.js b/js/src/dapps/gavcoin/Events/EventNewTranch/index.js new file mode 100644 index 000000000..46d1d2df6 --- /dev/null +++ b/js/src/dapps/gavcoin/Events/EventNewTranch/index.js @@ -0,0 +1,17 @@ +// Copyright 2015, 2016 Ethcore (UK) Ltd. +// This file is part of Parity. + +// Parity is free software: you can redistribute it and/or modify +// it under the terms of the GNU General Public License as published by +// the Free Software Foundation, either version 3 of the License, or +// (at your option) any later version. + +// Parity is distributed in the hope that it will be useful, +// but WITHOUT ANY WARRANTY; without even the implied warranty of +// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the +// GNU General Public License for more details. + +// You should have received a copy of the GNU General Public License +// along with Parity. If not, see . + +export default from './eventNewTranch'; diff --git a/js/src/dapps/gavcoin/Events/EventRefund/eventRefund.js b/js/src/dapps/gavcoin/Events/EventRefund/eventRefund.js new file mode 100644 index 000000000..5c4ed6562 --- /dev/null +++ b/js/src/dapps/gavcoin/Events/EventRefund/eventRefund.js @@ -0,0 +1,38 @@ +// Copyright 2015, 2016 Ethcore (UK) Ltd. +// This file is part of Parity. + +// Parity is free software: you can redistribute it and/or modify +// it under the terms of the GNU General Public License as published by +// the Free Software Foundation, either version 3 of the License, or +// (at your option) any later version. + +// Parity is distributed in the hope that it will be useful, +// but WITHOUT ANY WARRANTY; without even the implied warranty of +// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the +// GNU General Public License for more details. + +// You should have received a copy of the GNU General Public License +// along with Parity. If not, see . + +import React, { Component, PropTypes } from 'react'; + +import Event from '../Event'; + +export default class EventRefund extends Component { + static propTypes = { + event: PropTypes.object + } + + render () { + const { event } = this.props; + const { buyer, price, amount } = event.params; + + return ( + + ); + } +} diff --git a/js/src/dapps/gavcoin/Events/EventRefund/index.js b/js/src/dapps/gavcoin/Events/EventRefund/index.js new file mode 100644 index 000000000..bac0f20dc --- /dev/null +++ b/js/src/dapps/gavcoin/Events/EventRefund/index.js @@ -0,0 +1,17 @@ +// Copyright 2015, 2016 Ethcore (UK) Ltd. +// This file is part of Parity. + +// Parity is free software: you can redistribute it and/or modify +// it under the terms of the GNU General Public License as published by +// the Free Software Foundation, either version 3 of the License, or +// (at your option) any later version. + +// Parity is distributed in the hope that it will be useful, +// but WITHOUT ANY WARRANTY; without even the implied warranty of +// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the +// GNU General Public License for more details. + +// You should have received a copy of the GNU General Public License +// along with Parity. If not, see . + +export default from './eventRefund'; diff --git a/js/src/dapps/gavcoin/Events/EventTransfer/eventTransfer.js b/js/src/dapps/gavcoin/Events/EventTransfer/eventTransfer.js new file mode 100644 index 000000000..64e489cb0 --- /dev/null +++ b/js/src/dapps/gavcoin/Events/EventTransfer/eventTransfer.js @@ -0,0 +1,38 @@ +// Copyright 2015, 2016 Ethcore (UK) Ltd. +// This file is part of Parity. + +// Parity is free software: you can redistribute it and/or modify +// it under the terms of the GNU General Public License as published by +// the Free Software Foundation, either version 3 of the License, or +// (at your option) any later version. + +// Parity is distributed in the hope that it will be useful, +// but WITHOUT ANY WARRANTY; without even the implied warranty of +// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the +// GNU General Public License for more details. + +// You should have received a copy of the GNU General Public License +// along with Parity. If not, see . + +import React, { Component, PropTypes } from 'react'; + +import Event from '../Event'; + +export default class EventTransfer extends Component { + static propTypes = { + event: PropTypes.object + } + + render () { + const { event } = this.props; + const { from, to, value } = event.params; + + return ( + + ); + } +} diff --git a/js/src/dapps/gavcoin/Events/EventTransfer/index.js b/js/src/dapps/gavcoin/Events/EventTransfer/index.js new file mode 100644 index 000000000..a75b45a0a --- /dev/null +++ b/js/src/dapps/gavcoin/Events/EventTransfer/index.js @@ -0,0 +1,17 @@ +// Copyright 2015, 2016 Ethcore (UK) Ltd. +// This file is part of Parity. + +// Parity is free software: you can redistribute it and/or modify +// it under the terms of the GNU General Public License as published by +// the Free Software Foundation, either version 3 of the License, or +// (at your option) any later version. + +// Parity is distributed in the hope that it will be useful, +// but WITHOUT ANY WARRANTY; without even the implied warranty of +// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the +// GNU General Public License for more details. + +// You should have received a copy of the GNU General Public License +// along with Parity. If not, see . + +export default from './eventTransfer'; diff --git a/js/src/dapps/gavcoin/Events/events.css b/js/src/dapps/gavcoin/Events/events.css new file mode 100644 index 000000000..6fca62354 --- /dev/null +++ b/js/src/dapps/gavcoin/Events/events.css @@ -0,0 +1,94 @@ +/* Copyright 2015, 2016 Ethcore (UK) Ltd. +/* This file is part of Parity. +/* +/* Parity is free software: you can redistribute it and/or modify +/* it under the terms of the GNU General Public License as published by +/* the Free Software Foundation, either version 3 of the License, or +/* (at your option) any later version. +/* +/* Parity is distributed in the hope that it will be useful, +/* but WITHOUT ANY WARRANTY; without even the implied warranty of +/* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the +/* GNU General Public License for more details. +/* +/* You should have received a copy of the GNU General Public License +/* along with Parity. If not, see . +*/ +.events { + padding: 4em 2em; +} + +.list { + width: 100%; + border: none; + border-spacing: 0; +} + +.list td { + vertical-align: top; + padding: 4px 0.5em; + max-height: 32px; +} + +.event { + line-height: 32px; + vertical-align: top; +} + +.blocknumber, +.ethvalue, +.gavvalue { + font-family: 'Roboto Mono', monospace; +} + +.blocknumber, +.gavvalue { + text-align: right; +} + +.ethvalue { + text-align: center; +} + +.type { +} + +.account { +} + +.account img { + margin-bottom: -11px; +} + +.address { +} + +.name { + text-transform: uppercase; +} + +.event div { + display: inline; + margin-right: 1em; + vertical-align: top; +} + +.mined { +} + +.pending { + opacity: 0.5; +} + +.buyin { +} + +.refund { +} + +.transfer { +} + +.newtranch { + background: rgba(50, 250, 50, 0.1); +} diff --git a/js/src/dapps/gavcoin/Events/events.js b/js/src/dapps/gavcoin/Events/events.js new file mode 100644 index 000000000..cb287b3a7 --- /dev/null +++ b/js/src/dapps/gavcoin/Events/events.js @@ -0,0 +1,158 @@ +// Copyright 2015, 2016 Ethcore (UK) Ltd. +// This file is part of Parity. + +// Parity is free software: you can redistribute it and/or modify +// it under the terms of the GNU General Public License as published by +// the Free Software Foundation, either version 3 of the License, or +// (at your option) any later version. + +// Parity is distributed in the hope that it will be useful, +// but WITHOUT ANY WARRANTY; without even the implied warranty of +// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the +// GNU General Public License for more details. + +// You should have received a copy of the GNU General Public License +// along with Parity. If not, see . + +import React, { Component, PropTypes } from 'react'; + +import { api } from '../parity'; + +import EventBuyin from './EventBuyin'; +import EventNewTranch from './EventNewTranch'; +import EventRefund from './EventRefund'; +import EventTransfer from './EventTransfer'; + +import styles from './events.css'; + +export default class Events extends Component { + static childContextTypes = { + accounts: PropTypes.array + } + + static contextTypes = { + contract: PropTypes.object.isRequired, + instance: PropTypes.object.isRequired + } + + static propTypes = { + accounts: PropTypes.array + } + + state = { + allEvents: [], + minedEvents: [], + pendingEvents: [] + } + + componentDidMount () { + this.setupFilters(); + } + + render () { + return ( +
+ + + { this.renderEvents() } + +
+
+ ); + } + + renderEvents () { + const { allEvents } = this.state; + + if (!allEvents.length) { + return null; + } + + return allEvents + .map((event) => { + switch (event.type) { + case 'Buyin': + return ; + case 'NewTranch': + return ; + case 'Refund': + return ; + case 'Transfer': + return ; + } + }); + } + + getChildContext () { + const { accounts } = this.props; + + return { + accounts + }; + } + + setupFilters () { + const { contract } = this.context; + + const sortEvents = (a, b) => b.blockNumber.cmp(a.blockNumber) || b.logIndex.cmp(a.logIndex); + const logToEvent = (log) => { + const key = api.util.sha3(JSON.stringify(log)); + const { blockNumber, logIndex, transactionHash, transactionIndex, params, type } = log; + + return { + type: log.event, + state: type, + blockNumber, + logIndex, + transactionHash, + transactionIndex, + params, + key + }; + }; + + const options = { + fromBlock: 0, + toBlock: 'pending', + limit: 50 + }; + + contract.subscribe(null, options, (error, _logs) => { + if (error) { + console.error('setupFilters', error); + return; + } + + if (!_logs.length) { + return; + } + + const logs = _logs.map(logToEvent); + + const minedEvents = logs + .filter((log) => log.state === 'mined') + .reverse() + .concat(this.state.minedEvents) + .sort(sortEvents); + const pendingEvents = logs + .filter((log) => log.state === 'pending') + .reverse() + .concat(this.state.pendingEvents.filter((event) => { + return !logs.find((log) => { + const isMined = (log.state === 'mined') && (log.transactionHash === event.transactionHash); + const isPending = (log.state === 'pending') && (log.key === event.key); + + return isMined || isPending; + }); + })) + .sort(sortEvents); + const allEvents = pendingEvents.concat(minedEvents); + + this.setState({ + allEvents, + minedEvents, + pendingEvents + }); + }); + } +} diff --git a/js/src/dapps/gavcoin/Events/index.js b/js/src/dapps/gavcoin/Events/index.js new file mode 100644 index 000000000..88ad6d407 --- /dev/null +++ b/js/src/dapps/gavcoin/Events/index.js @@ -0,0 +1,17 @@ +// Copyright 2015, 2016 Ethcore (UK) Ltd. +// This file is part of Parity. + +// Parity is free software: you can redistribute it and/or modify +// it under the terms of the GNU General Public License as published by +// the Free Software Foundation, either version 3 of the License, or +// (at your option) any later version. + +// Parity is distributed in the hope that it will be useful, +// but WITHOUT ANY WARRANTY; without even the implied warranty of +// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the +// GNU General Public License for more details. + +// You should have received a copy of the GNU General Public License +// along with Parity. If not, see . + +export default from './events'; diff --git a/js/src/dapps/gavcoin/IdentityIcon/identityIcon.css b/js/src/dapps/gavcoin/IdentityIcon/identityIcon.css new file mode 100644 index 000000000..2b645d823 --- /dev/null +++ b/js/src/dapps/gavcoin/IdentityIcon/identityIcon.css @@ -0,0 +1,23 @@ +/* Copyright 2015, 2016 Ethcore (UK) Ltd. +/* This file is part of Parity. +/* +/* Parity is free software: you can redistribute it and/or modify +/* it under the terms of the GNU General Public License as published by +/* the Free Software Foundation, either version 3 of the License, or +/* (at your option) any later version. +/* +/* Parity is distributed in the hope that it will be useful, +/* but WITHOUT ANY WARRANTY; without even the implied warranty of +/* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the +/* GNU General Public License for more details. +/* +/* You should have received a copy of the GNU General Public License +/* along with Parity. If not, see . +*/ + +.icon { + width: 32px; + height: 32px; + border-radius: 50%; + margin-right: 0.5em; +} diff --git a/js/src/dapps/gavcoin/IdentityIcon/identityIcon.js b/js/src/dapps/gavcoin/IdentityIcon/identityIcon.js new file mode 100644 index 000000000..51f48d46a --- /dev/null +++ b/js/src/dapps/gavcoin/IdentityIcon/identityIcon.js @@ -0,0 +1,36 @@ +// Copyright 2015, 2016 Ethcore (UK) Ltd. +// This file is part of Parity. + +// Parity is free software: you can redistribute it and/or modify +// it under the terms of the GNU General Public License as published by +// the Free Software Foundation, either version 3 of the License, or +// (at your option) any later version. + +// Parity is distributed in the hope that it will be useful, +// but WITHOUT ANY WARRANTY; without even the implied warranty of +// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the +// GNU General Public License for more details. + +// You should have received a copy of the GNU General Public License +// along with Parity. If not, see . + +import React, { Component, PropTypes } from 'react'; + +import { api } from '../parity'; +import styles from './identityIcon.css'; + +export default class IdentityIcon extends Component { + static propTypes = { + address: PropTypes.string.isRequired + } + + render () { + const { address } = this.props; + + return ( + + ); + } +} diff --git a/js/src/dapps/gavcoin/IdentityIcon/index.js b/js/src/dapps/gavcoin/IdentityIcon/index.js new file mode 100644 index 000000000..76c107bfb --- /dev/null +++ b/js/src/dapps/gavcoin/IdentityIcon/index.js @@ -0,0 +1,17 @@ +// Copyright 2015, 2016 Ethcore (UK) Ltd. +// This file is part of Parity. + +// Parity is free software: you can redistribute it and/or modify +// it under the terms of the GNU General Public License as published by +// the Free Software Foundation, either version 3 of the License, or +// (at your option) any later version. + +// Parity is distributed in the hope that it will be useful, +// but WITHOUT ANY WARRANTY; without even the implied warranty of +// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the +// GNU General Public License for more details. + +// You should have received a copy of the GNU General Public License +// along with Parity. If not, see . + +export default from './identityIcon'; diff --git a/js/src/dapps/gavcoin/Loading/index.js b/js/src/dapps/gavcoin/Loading/index.js new file mode 100644 index 000000000..0468cab37 --- /dev/null +++ b/js/src/dapps/gavcoin/Loading/index.js @@ -0,0 +1,17 @@ +// Copyright 2015, 2016 Ethcore (UK) Ltd. +// This file is part of Parity. + +// Parity is free software: you can redistribute it and/or modify +// it under the terms of the GNU General Public License as published by +// the Free Software Foundation, either version 3 of the License, or +// (at your option) any later version. + +// Parity is distributed in the hope that it will be useful, +// but WITHOUT ANY WARRANTY; without even the implied warranty of +// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the +// GNU General Public License for more details. + +// You should have received a copy of the GNU General Public License +// along with Parity. If not, see . + +export default from './loading'; diff --git a/js/src/dapps/gavcoin/Loading/loading.css b/js/src/dapps/gavcoin/Loading/loading.css new file mode 100644 index 000000000..3ec65400f --- /dev/null +++ b/js/src/dapps/gavcoin/Loading/loading.css @@ -0,0 +1,21 @@ +/* Copyright 2015, 2016 Ethcore (UK) Ltd. +/* This file is part of Parity. +/* +/* Parity is free software: you can redistribute it and/or modify +/* it under the terms of the GNU General Public License as published by +/* the Free Software Foundation, either version 3 of the License, or +/* (at your option) any later version. +/* +/* Parity is distributed in the hope that it will be useful, +/* but WITHOUT ANY WARRANTY; without even the implied warranty of +/* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the +/* GNU General Public License for more details. +/* +/* You should have received a copy of the GNU General Public License +/* along with Parity. If not, see . +*/ +.loading { + width: 100%; + text-align: center; + padding-top: 2em; +} diff --git a/js/src/dapps/gavcoin/Loading/loading.js b/js/src/dapps/gavcoin/Loading/loading.js new file mode 100644 index 000000000..9e00cc8f1 --- /dev/null +++ b/js/src/dapps/gavcoin/Loading/loading.js @@ -0,0 +1,31 @@ +// Copyright 2015, 2016 Ethcore (UK) Ltd. +// This file is part of Parity. + +// Parity is free software: you can redistribute it and/or modify +// it under the terms of the GNU General Public License as published by +// the Free Software Foundation, either version 3 of the License, or +// (at your option) any later version. + +// Parity is distributed in the hope that it will be useful, +// but WITHOUT ANY WARRANTY; without even the implied warranty of +// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the +// GNU General Public License for more details. + +// You should have received a copy of the GNU General Public License +// along with Parity. If not, see . + +import React, { Component } from 'react'; + +import { CircularProgress } from 'material-ui'; + +import styles from './loading.css'; + +export default class Loading extends Component { + render () { + return ( +
+ +
+ ); + } +} diff --git a/js/src/dapps/gavcoin/Status/index.js b/js/src/dapps/gavcoin/Status/index.js new file mode 100644 index 000000000..44079b224 --- /dev/null +++ b/js/src/dapps/gavcoin/Status/index.js @@ -0,0 +1,17 @@ +// Copyright 2015, 2016 Ethcore (UK) Ltd. +// This file is part of Parity. + +// Parity is free software: you can redistribute it and/or modify +// it under the terms of the GNU General Public License as published by +// the Free Software Foundation, either version 3 of the License, or +// (at your option) any later version. + +// Parity is distributed in the hope that it will be useful, +// but WITHOUT ANY WARRANTY; without even the implied warranty of +// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the +// GNU General Public License for more details. + +// You should have received a copy of the GNU General Public License +// along with Parity. If not, see . + +export default from './status'; diff --git a/js/src/dapps/gavcoin/Status/status.css b/js/src/dapps/gavcoin/Status/status.css new file mode 100644 index 000000000..bb541b178 --- /dev/null +++ b/js/src/dapps/gavcoin/Status/status.css @@ -0,0 +1,53 @@ +/* Copyright 2015, 2016 Ethcore (UK) Ltd. +/* This file is part of Parity. +/* +/* Parity is free software: you can redistribute it and/or modify +/* it under the terms of the GNU General Public License as published by +/* the Free Software Foundation, either version 3 of the License, or +/* (at your option) any later version. +/* +/* Parity is distributed in the hope that it will be useful, +/* but WITHOUT ANY WARRANTY; without even the implied warranty of +/* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the +/* GNU General Public License for more details. +/* +/* You should have received a copy of the GNU General Public License +/* along with Parity. If not, see . +*/ +.status { + background: rgba(25, 75, 125, 1); + color: rgba(255, 255, 255, 1); + padding: 4em 0 2em 0; + display: flex; + flex-wrap: wrap; +} + +.title { + margin-top: 0; + font-weight: 300; + font-size: 2.5rem; + text-transform: uppercase; +} + +.item { + flex: 0 1 30%; + width: 30%; + margin: 0 1.5%; + text-align: center; +} + +.byline { + font-size: 1.25em; + color: rgba(255, 255, 255, 0.7); +} + +.heading { + text-transform: uppercase; + letter-spacing: 0.25em; + font-size: 1.5em; + color: rgba(255, 255, 255, 0.7); +} + +.hero { + font-size: 4em; +} diff --git a/js/src/dapps/gavcoin/Status/status.js b/js/src/dapps/gavcoin/Status/status.js new file mode 100644 index 000000000..81dd0cb16 --- /dev/null +++ b/js/src/dapps/gavcoin/Status/status.js @@ -0,0 +1,74 @@ +// Copyright 2015, 2016 Ethcore (UK) Ltd. +// This file is part of Parity. + +// Parity is free software: you can redistribute it and/or modify +// it under the terms of the GNU General Public License as published by +// the Free Software Foundation, either version 3 of the License, or +// (at your option) any later version. + +// Parity is distributed in the hope that it will be useful, +// but WITHOUT ANY WARRANTY; without even the implied warranty of +// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the +// GNU General Public License for more details. + +// You should have received a copy of the GNU General Public License +// along with Parity. If not, see . + +import React, { Component, PropTypes } from 'react'; + +import { formatBlockNumber, formatCoins, formatEth } from '../format'; + +import styles from './status.css'; + +export default class Status extends Component { + static propTypes = { + address: PropTypes.string, + gavBalance: PropTypes.object, + blockNumber: PropTypes.object, + totalSupply: PropTypes.object, + remaining: PropTypes.object, + price: PropTypes.object, + children: PropTypes.node + } + + render () { + const { blockNumber, gavBalance, totalSupply, remaining, price } = this.props; + + if (!totalSupply) { + return null; + } + + return ( +
+
+
 
+
+ { formatCoins(remaining, -1) } +
+
+ available for { formatEth(price) }ETH +
+
+
+
GAVcoin
+
+ { formatCoins(totalSupply, -1) } +
+
+ total at { formatBlockNumber(blockNumber) } +
+
+
+
 
+
+ { formatCoins(gavBalance, -1) } +
+
+ coin balance +
+
+ { this.props.children } +
+ ); + } +} diff --git a/js/src/dapps/gavcoin/format/index.js b/js/src/dapps/gavcoin/format/index.js new file mode 100644 index 000000000..5e32012d0 --- /dev/null +++ b/js/src/dapps/gavcoin/format/index.js @@ -0,0 +1,52 @@ +// Copyright 2015, 2016 Ethcore (UK) Ltd. +// This file is part of Parity. + +// Parity is free software: you can redistribute it and/or modify +// it under the terms of the GNU General Public License as published by +// the Free Software Foundation, either version 3 of the License, or +// (at your option) any later version. + +// Parity is distributed in the hope that it will be useful, +// but WITHOUT ANY WARRANTY; without even the implied warranty of +// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the +// GNU General Public License for more details. + +// You should have received a copy of the GNU General Public License +// along with Parity. If not, see . + +import BigNumber from 'bignumber.js'; + +import { api } from '../parity'; + +const DIVISOR = 10 ** 6; +const ZERO = new BigNumber(0); + +export function formatBlockNumber (blockNumber) { + return ZERO.eq(blockNumber || 0) + ? 'Pending' + : `#${blockNumber.toFormat()}`; +} + +export function formatCoins (amount, decimals = 6) { + const adjusted = amount.div(DIVISOR); + + if (decimals === -1) { + if (adjusted.gte(10000)) { + decimals = 0; + } else if (adjusted.gte(1000)) { + decimals = 1; + } else if (adjusted.gte(100)) { + decimals = 2; + } else if (adjusted.gte(10)) { + decimals = 3; + } else { + decimals = 4; + } + } + + return adjusted.toFormat(decimals); +} + +export function formatEth (eth, decimals = 3) { + return api.util.fromWei(eth).toFormat(decimals); +} diff --git a/js/src/dapps/gavcoin/parity.js b/js/src/dapps/gavcoin/parity.js new file mode 100644 index 000000000..f6d59f44d --- /dev/null +++ b/js/src/dapps/gavcoin/parity.js @@ -0,0 +1,21 @@ +// Copyright 2015, 2016 Ethcore (UK) Ltd. +// This file is part of Parity. + +// Parity is free software: you can redistribute it and/or modify +// it under the terms of the GNU General Public License as published by +// the Free Software Foundation, either version 3 of the License, or +// (at your option) any later version. + +// Parity is distributed in the hope that it will be useful, +// but WITHOUT ANY WARRANTY; without even the implied warranty of +// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the +// GNU General Public License for more details. + +// You should have received a copy of the GNU General Public License +// along with Parity. If not, see . + +const { api } = window.parity; + +export { + api +}; diff --git a/js/src/dapps/githubhint.html b/js/src/dapps/githubhint.html new file mode 100644 index 000000000..0084dd051 --- /dev/null +++ b/js/src/dapps/githubhint.html @@ -0,0 +1,16 @@ + + + + + + + GitHub Hint + + +
+ + + + + + diff --git a/js/src/dapps/githubhint.js b/js/src/dapps/githubhint.js new file mode 100644 index 000000000..b73702990 --- /dev/null +++ b/js/src/dapps/githubhint.js @@ -0,0 +1,33 @@ +// Copyright 2015, 2016 Ethcore (UK) Ltd. +// This file is part of Parity. + +// Parity is free software: you can redistribute it and/or modify +// it under the terms of the GNU General Public License as published by +// the Free Software Foundation, either version 3 of the License, or +// (at your option) any later version. + +// Parity is distributed in the hope that it will be useful, +// but WITHOUT ANY WARRANTY; without even the implied warranty of +// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the +// GNU General Public License for more details. + +// You should have received a copy of the GNU General Public License +// along with Parity. If not, see . + +import ReactDOM from 'react-dom'; +import React from 'react'; + +import injectTapEventPlugin from 'react-tap-event-plugin'; +injectTapEventPlugin(); + +import Application from './githubhint/Application'; + +import '../../assets/fonts/Roboto/font.css'; +import '../../assets/fonts/RobotoMono/font.css'; +import './style.css'; +import './githubhint.html'; + +ReactDOM.render( + , + document.querySelector('#container') +); diff --git a/js/src/dapps/githubhint/Application/application.css b/js/src/dapps/githubhint/Application/application.css new file mode 100644 index 000000000..aa4a68dc6 --- /dev/null +++ b/js/src/dapps/githubhint/Application/application.css @@ -0,0 +1,121 @@ +/* Copyright 2015, 2016 Ethcore (UK) Ltd. +/* This file is part of Parity. +/* +/* Parity is free software: you can redistribute it and/or modify +/* it under the terms of the GNU General Public License as published by +/* the Free Software Foundation, either version 3 of the License, or +/* (at your option) any later version. +/* +/* Parity is distributed in the hope that it will be useful, +/* but WITHOUT ANY WARRANTY; without even the implied warranty of +/* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the +/* GNU General Public License for more details. +/* +/* You should have received a copy of the GNU General Public License +/* along with Parity. If not, see . +*/ + +.container { + background: #333; + font-family: 'Roboto'; + vertical-align: middle; + padding: 4em 0; + text-align: center; +} + +.form { + text-align: right; + margin: 0 auto; + border-radius: 5px; + width: 44em; + color: #eee; +} + +.box { + padding: 2em; + background: rgba(255, 255, 255, 0.1); + border-radius: 5px; + margin-bottom: 2em; +} + +.buttons { + text-align: center; + margin: 0 auto 2em auto; +} + +.box .buttons { + text-align: right; + margin: 2em 0 0 0; + position: relative; +} + +.box .buttons .addressSelect { + position: absolute; + top: 0; + left: 0; +} + +.box .description { + margin: 0 0 2em 0; + text-align: center; + opacity: 0.75; +} + +.progress { + margin: 2em 0 0 0; + opacity: 0.75; + text-align: center; +} + +.statusHeader { + font-size: 1em; +} + +.statusState, .statusError { + padding: 1em 0 0 0; +} + +.statusError { + color: #f66; +} + +.capture { +} + +.capture * { + display: inline-block; + padding: 0.75em; + vertical-align: middle; + box-sizing: border-box; + width: 20em; +} + +.capture input { + color: #333; + background: #eee; + border: none; + border-radius: 5px; + width: 100%; + font-size: 1em; + text-align: center; +} + +.capture input[disabled] { + opacity: 0.5; +} + +.capture input.error { + background: #fcc; +} + +.hashError { + padding-top: 0.5em; + color: #f66; + text-align: center; +} + +.hashOk { + padding-top: 0.5em; + opacity: 0.5; + text-align: center; +} diff --git a/js/src/dapps/githubhint/Application/application.js b/js/src/dapps/githubhint/Application/application.js new file mode 100644 index 000000000..ea7e760c5 --- /dev/null +++ b/js/src/dapps/githubhint/Application/application.js @@ -0,0 +1,261 @@ +// Copyright 2015, 2016 Ethcore (UK) Ltd. +// This file is part of Parity. + +// Parity is free software: you can redistribute it and/or modify +// it under the terms of the GNU General Public License as published by +// the Free Software Foundation, either version 3 of the License, or +// (at your option) any later version. + +// Parity is distributed in the hope that it will be useful, +// but WITHOUT ANY WARRANTY; without even the implied warranty of +// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the +// GNU General Public License for more details. + +// You should have received a copy of the GNU General Public License +// along with Parity. If not, see . + +import React, { Component } from 'react'; + +import { api } from '../parity'; +import { attachInterface } from '../services'; +import Button from '../Button'; +import IdentityIcon from '../IdentityIcon'; +import Loading from '../Loading'; + +import styles from './application.css'; + +const INVALID_URL_HASH = '0xc5d2460186f7233c927e7db2dcc703c0e500b653ca82273b7bfad8045d85a470'; +const ZERO_ADDRESS = '0x0000000000000000000000000000000000000000'; + +export default class Application extends Component { + state = { + loading: true, + url: '', + urlError: null, + contentHash: '', + contentHashError: null, + registerBusy: false, + registerError: null, + registerState: '' + } + + componentDidMount () { + attachInterface() + .then((state) => { + this.setState(state, () => { + this.setState({ loading: false }); + }); + }); + } + + render () { + const { loading } = this.state; + + return loading + ? this.renderLoading() + : this.renderPage(); + } + + renderLoading () { + return ( + + ); + } + + renderPage () { + const { registerBusy, url, urlError, contentHash, contentHashError } = this.state; + + return ( +
+
+
+
+ Provide a valid URL to register. The content information can be used in other contracts that allows for reverse lookups, e.g. image registries, dapp registries, etc. +
+
+ +
+
+ { contentHashError || contentHash } +
+ { registerBusy ? this.renderProgress() : this.renderButtons() } +
+
+
+ ); + } + + renderButtons () { + const { accounts, fromAddress, url, urlError, contentHashError } = this.state; + const account = accounts[fromAddress]; + + return ( +
+
+ +
+ +
+ ); + } + + renderProgress () { + const { registerError, registerState } = this.state; + + if (registerError) { + return ( +
+
+ Your registration has encountered an error +
+
+ { registerError } +
+
+ ); + } + + return ( +
+
+ Your URL is being registered +
+
+ { registerState } +
+
+ ); + } + + onClickContentHash = () => { + this.setState({ fileHash: false, commit: '' }); + } + + onClickFileHash = () => { + this.setState({ fileHash: true, commit: 0 }); + } + + onChangeUrl = (event) => { + const url = event.target.value; + let urlError = null; + + if (url && url.length) { + var re = /^https?:\/\/(?:www\.|(?!www))[^\s\.]+\.[^\s]{2,}/g; + urlError = re.test(url) + ? null + : 'not matching rexex'; + } + + this.setState({ url, urlError, contentHashError: 'hash lookup in progress' }, () => { + this.lookupHash(); + }); + } + + onClickRegister = () => { + const { url, urlError, contentHash, contentHashError, fromAddress, instance } = this.state; + + if (!!contentHashError || !!urlError || url.length === 0) { + return; + } + + this.setState({ registerBusy: true, registerState: 'Estimating gas for the transaction' }); + + const values = [contentHash, url]; + const options = { from: fromAddress }; + + instance + .hintURL.estimateGas(options, values) + .then((gas) => { + this.setState({ registerState: 'Gas estimated, Posting transaction to the network' }); + + const gasPassed = gas.mul(1.2); + options.gas = gasPassed.toFixed(0); + console.log(`gas estimated at ${gas.toFormat(0)}, passing ${gasPassed.toFormat(0)}`); + + return instance.hintURL.postTransaction(options, values); + }) + .then((signerRequestId) => { + this.setState({ signerRequestId, registerState: 'Transaction posted, Waiting for transaction authorization' }); + + return api.pollMethod('eth_checkRequest', signerRequestId); + }) + .then((txHash) => { + this.setState({ txHash, registerState: 'Transaction authorized, Waiting for network confirmations' }); + + return api.pollMethod('eth_getTransactionReceipt', txHash, (receipt) => { + if (!receipt || !receipt.blockNumber || receipt.blockNumber.eq(0)) { + return false; + } + + return true; + }); + }) + .then((txReceipt) => { + this.setState({ txReceipt, registerBusy: false, registerState: 'Network confirmed, Received transaction receipt', url: '', contentHash: '' }); + }) + .catch((error) => { + console.error('onSend', error); + this.setState({ registerError: error.message }); + }); + } + + onSelectFromAddress = () => { + const { accounts, fromAddress } = this.state; + const addresses = Object.keys(accounts); + let index = 0; + + addresses.forEach((address, _index) => { + if (address === fromAddress) { + index = _index; + } + }); + + index++; + if (index >= addresses.length) { + index = 0; + } + + this.setState({ fromAddress: addresses[index] }); + } + + lookupHash () { + const { url, instance } = this.state; + + api.ethcore + .hashContent(url) + .then((contentHash) => { + console.log('lookupHash', contentHash); + if (contentHash === INVALID_URL_HASH) { + this.setState({ contentHashError: 'invalid url endpoint', contentHash: null }); + return; + } + + instance.entries + .call({}, [contentHash]) + .then(([accountSlashRepo, commit, owner]) => { + console.log('lookupHash', accountSlashRepo, api.util.bytesToHex(commit), owner); + + if (owner !== ZERO_ADDRESS) { + this.setState({ contentHashError: contentHash, contentHash: null }); + } else { + this.setState({ contentHashError: null, contentHash }); + } + }); + }) + .catch((error) => { + console.error('lookupHash', error); + this.setState({ contentHashError: error.message, contentHash: null }); + }); + } +} diff --git a/js/src/dapps/githubhint/Application/index.js b/js/src/dapps/githubhint/Application/index.js new file mode 100644 index 000000000..236578226 --- /dev/null +++ b/js/src/dapps/githubhint/Application/index.js @@ -0,0 +1,17 @@ +// Copyright 2015, 2016 Ethcore (UK) Ltd. +// This file is part of Parity. + +// Parity is free software: you can redistribute it and/or modify +// it under the terms of the GNU General Public License as published by +// the Free Software Foundation, either version 3 of the License, or +// (at your option) any later version. + +// Parity is distributed in the hope that it will be useful, +// but WITHOUT ANY WARRANTY; without even the implied warranty of +// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the +// GNU General Public License for more details. + +// You should have received a copy of the GNU General Public License +// along with Parity. If not, see . + +export default from './application'; diff --git a/js/src/dapps/githubhint/Button/button.css b/js/src/dapps/githubhint/Button/button.css new file mode 100644 index 000000000..28519094b --- /dev/null +++ b/js/src/dapps/githubhint/Button/button.css @@ -0,0 +1,56 @@ +/* Copyright 2015, 2016 Ethcore (UK) Ltd. +/* This file is part of Parity. +/* +/* Parity is free software: you can redistribute it and/or modify +/* it under the terms of the GNU General Public License as published by +/* the Free Software Foundation, either version 3 of the License, or +/* (at your option) any later version. +/* +/* Parity is distributed in the hope that it will be useful, +/* but WITHOUT ANY WARRANTY; without even the implied warranty of +/* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the +/* GNU General Public License for more details. +/* +/* You should have received a copy of the GNU General Public License +/* along with Parity. If not, see . +*/ + +.button { + background: #08a; + color: white; + border-radius: 5px; + font-size: 1em; + line-height: 24px; + height: 24px; + padding: 0.75em 1.5em; + cursor: pointer; + display: inline-block; + text-align: center; +} + +.button.first { + border-radius: 5px 0 0 5px; +} + +.button.middle { + border-radius: 0; +} + +.button.last { + border-radius: 0 5px 5px 0; +} + +.button.disabled { + opacity: 0.25; + cursor: default; +} + +.button.inverse { + color: #08a; + background: white; +} + +.button * { + display: inline-block; + vertical-align: top; +} diff --git a/js/src/dapps/githubhint/Button/button.js b/js/src/dapps/githubhint/Button/button.js new file mode 100644 index 000000000..42fca1af7 --- /dev/null +++ b/js/src/dapps/githubhint/Button/button.js @@ -0,0 +1,53 @@ +// Copyright 2015, 2016 Ethcore (UK) Ltd. +// This file is part of Parity. + +// Parity is free software: you can redistribute it and/or modify +// it under the terms of the GNU General Public License as published by +// the Free Software Foundation, either version 3 of the License, or +// (at your option) any later version. + +// Parity is distributed in the hope that it will be useful, +// but WITHOUT ANY WARRANTY; without even the implied warranty of +// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the +// GNU General Public License for more details. + +// You should have received a copy of the GNU General Public License +// along with Parity. If not, see . + +import React, { Component, PropTypes } from 'react'; + +import styles from './button.css'; + +export default class Button extends Component { + static propTypes = { + children: PropTypes.node.isRequired, + className: PropTypes.string, + disabled: PropTypes.bool, + invert: PropTypes.bool, + first: PropTypes.bool, + last: PropTypes.bool, + middle: PropTypes.bool, + onClick: PropTypes.func.isRequired + } + + render () { + const { children, className, disabled, invert, first, last, middle } = this.props; + const classes = `${styles.button} ${disabled ? styles.disabled : ''} ${invert ? styles.inverse : ''} ${first ? styles.first : ''} ${last ? styles.last : ''} ${middle ? styles.middle : ''} ${className}`; + + return ( +
+ { children } +
+ ); + } + + onClick = (event) => { + const { disabled, onClick } = this.props; + + if (disabled) { + return; + } + + onClick(event); + } +} diff --git a/js/src/dapps/githubhint/Button/index.js b/js/src/dapps/githubhint/Button/index.js new file mode 100644 index 000000000..f69a65e3d --- /dev/null +++ b/js/src/dapps/githubhint/Button/index.js @@ -0,0 +1,17 @@ +// Copyright 2015, 2016 Ethcore (UK) Ltd. +// This file is part of Parity. + +// Parity is free software: you can redistribute it and/or modify +// it under the terms of the GNU General Public License as published by +// the Free Software Foundation, either version 3 of the License, or +// (at your option) any later version. + +// Parity is distributed in the hope that it will be useful, +// but WITHOUT ANY WARRANTY; without even the implied warranty of +// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the +// GNU General Public License for more details. + +// You should have received a copy of the GNU General Public License +// along with Parity. If not, see . + +export default from './button'; diff --git a/js/src/dapps/githubhint/IdentityIcon/identityIcon.css b/js/src/dapps/githubhint/IdentityIcon/identityIcon.css new file mode 100644 index 000000000..3fa1df99e --- /dev/null +++ b/js/src/dapps/githubhint/IdentityIcon/identityIcon.css @@ -0,0 +1,23 @@ +/* Copyright 2015, 2016 Ethcore (UK) Ltd. +/* This file is part of Parity. +/* +/* Parity is free software: you can redistribute it and/or modify +/* it under the terms of the GNU General Public License as published by +/* the Free Software Foundation, either version 3 of the License, or +/* (at your option) any later version. +/* +/* Parity is distributed in the hope that it will be useful, +/* but WITHOUT ANY WARRANTY; without even the implied warranty of +/* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the +/* GNU General Public License for more details. +/* +/* You should have received a copy of the GNU General Public License +/* along with Parity. If not, see . +*/ + +.icon { + width: 24px; + height: 24px; + border-radius: 50%; + margin-right: 0.5em; +} diff --git a/js/src/dapps/githubhint/IdentityIcon/identityIcon.js b/js/src/dapps/githubhint/IdentityIcon/identityIcon.js new file mode 100644 index 000000000..0bc86731d --- /dev/null +++ b/js/src/dapps/githubhint/IdentityIcon/identityIcon.js @@ -0,0 +1,36 @@ +// Copyright 2015, 2016 Ethcore (UK) Ltd. +// This file is part of Parity. + +// Parity is free software: you can redistribute it and/or modify +// it under the terms of the GNU General Public License as published by +// the Free Software Foundation, either version 3 of the License, or +// (at your option) any later version. + +// Parity is distributed in the hope that it will be useful, +// but WITHOUT ANY WARRANTY; without even the implied warranty of +// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the +// GNU General Public License for more details. + +// You should have received a copy of the GNU General Public License +// along with Parity. If not, see . + +import React, { Component, PropTypes } from 'react'; + +import { api } from '../parity'; +import styles from './identityIcon.css'; + +export default class IdentityIcon extends Component { + static propTypes = { + address: PropTypes.string.isRequired + } + + render () { + const { address } = this.props; + + return ( + + ); + } +} diff --git a/js/src/dapps/githubhint/IdentityIcon/index.js b/js/src/dapps/githubhint/IdentityIcon/index.js new file mode 100644 index 000000000..76c107bfb --- /dev/null +++ b/js/src/dapps/githubhint/IdentityIcon/index.js @@ -0,0 +1,17 @@ +// Copyright 2015, 2016 Ethcore (UK) Ltd. +// This file is part of Parity. + +// Parity is free software: you can redistribute it and/or modify +// it under the terms of the GNU General Public License as published by +// the Free Software Foundation, either version 3 of the License, or +// (at your option) any later version. + +// Parity is distributed in the hope that it will be useful, +// but WITHOUT ANY WARRANTY; without even the implied warranty of +// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the +// GNU General Public License for more details. + +// You should have received a copy of the GNU General Public License +// along with Parity. If not, see . + +export default from './identityIcon'; diff --git a/js/src/dapps/githubhint/Loading/index.js b/js/src/dapps/githubhint/Loading/index.js new file mode 100644 index 000000000..0468cab37 --- /dev/null +++ b/js/src/dapps/githubhint/Loading/index.js @@ -0,0 +1,17 @@ +// Copyright 2015, 2016 Ethcore (UK) Ltd. +// This file is part of Parity. + +// Parity is free software: you can redistribute it and/or modify +// it under the terms of the GNU General Public License as published by +// the Free Software Foundation, either version 3 of the License, or +// (at your option) any later version. + +// Parity is distributed in the hope that it will be useful, +// but WITHOUT ANY WARRANTY; without even the implied warranty of +// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the +// GNU General Public License for more details. + +// You should have received a copy of the GNU General Public License +// along with Parity. If not, see . + +export default from './loading'; diff --git a/js/src/dapps/githubhint/Loading/loading.css b/js/src/dapps/githubhint/Loading/loading.css new file mode 100644 index 000000000..b77d1a237 --- /dev/null +++ b/js/src/dapps/githubhint/Loading/loading.css @@ -0,0 +1,24 @@ +/* Copyright 2015, 2016 Ethcore (UK) Ltd. +/* This file is part of Parity. +/* +/* Parity is free software: you can redistribute it and/or modify +/* it under the terms of the GNU General Public License as published by +/* the Free Software Foundation, either version 3 of the License, or +/* (at your option) any later version. +/* +/* Parity is distributed in the hope that it will be useful, +/* but WITHOUT ANY WARRANTY; without even the implied warranty of +/* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the +/* GNU General Public License for more details. +/* +/* You should have received a copy of the GNU General Public License +/* along with Parity. If not, see . +*/ + +.loading { + width: 100%; + text-align: center; + padding-top: 5em; + font-size: 2em; + color: #999; +} diff --git a/js/src/dapps/githubhint/Loading/loading.js b/js/src/dapps/githubhint/Loading/loading.js new file mode 100644 index 000000000..b884597d7 --- /dev/null +++ b/js/src/dapps/githubhint/Loading/loading.js @@ -0,0 +1,29 @@ +// Copyright 2015, 2016 Ethcore (UK) Ltd. +// This file is part of Parity. + +// Parity is free software: you can redistribute it and/or modify +// it under the terms of the GNU General Public License as published by +// the Free Software Foundation, either version 3 of the License, or +// (at your option) any later version. + +// Parity is distributed in the hope that it will be useful, +// but WITHOUT ANY WARRANTY; without even the implied warranty of +// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the +// GNU General Public License for more details. + +// You should have received a copy of the GNU General Public License +// along with Parity. If not, see . + +import React, { Component } from 'react'; + +import styles from './loading.css'; + +export default class Loading extends Component { + render () { + return ( +
+ Attaching to contract ... +
+ ); + } +} diff --git a/js/src/dapps/githubhint/parity.js b/js/src/dapps/githubhint/parity.js new file mode 100644 index 000000000..f6d59f44d --- /dev/null +++ b/js/src/dapps/githubhint/parity.js @@ -0,0 +1,21 @@ +// Copyright 2015, 2016 Ethcore (UK) Ltd. +// This file is part of Parity. + +// Parity is free software: you can redistribute it and/or modify +// it under the terms of the GNU General Public License as published by +// the Free Software Foundation, either version 3 of the License, or +// (at your option) any later version. + +// Parity is distributed in the hope that it will be useful, +// but WITHOUT ANY WARRANTY; without even the implied warranty of +// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the +// GNU General Public License for more details. + +// You should have received a copy of the GNU General Public License +// along with Parity. If not, see . + +const { api } = window.parity; + +export { + api +}; diff --git a/js/src/dapps/githubhint/services.js b/js/src/dapps/githubhint/services.js new file mode 100644 index 000000000..ee198ff6d --- /dev/null +++ b/js/src/dapps/githubhint/services.js @@ -0,0 +1,64 @@ +// Copyright 2015, 2016 Ethcore (UK) Ltd. +// This file is part of Parity. + +// Parity is free software: you can redistribute it and/or modify +// it under the terms of the GNU General Public License as published by +// the Free Software Foundation, either version 3 of the License, or +// (at your option) any later version. + +// Parity is distributed in the hope that it will be useful, +// but WITHOUT ANY WARRANTY; without even the implied warranty of +// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the +// GNU General Public License for more details. + +// You should have received a copy of the GNU General Public License +// along with Parity. If not, see . + +import * as abis from '../../contracts/abi'; +import { api } from './parity'; + +export function attachInterface () { + return api.ethcore + .registryAddress() + .then((registryAddress) => { + console.log(`the registry was found at ${registryAddress}`); + + const registry = api.newContract(abis.registry, registryAddress).instance; + + return Promise + .all([ + registry.getAddress.call({}, [api.util.sha3('githubhint'), 'A']), + api.personal.listAccounts(), + api.personal.accountsInfo() + ]); + }) + .then(([address, addresses, accountsInfo]) => { + console.log(`githubhint was found at ${address}`); + + const contract = api.newContract(abis.githubhint, address); + const accounts = addresses.reduce((obj, address) => { + const info = accountsInfo[address]; + + return Object.assign(obj, { + [address]: { + address, + name: info.name || 'Unnamed', + uuid: info.uuid + } + }); + }, {}); + const fromAddress = Object.keys(accounts)[0]; + + return { + accounts, + address, + accountsInfo, + contract, + instance: contract.instance, + fromAddress + }; + }) + .catch((error) => { + console.error('attachInterface', error); + }); +} diff --git a/js/src/dapps/registry.html b/js/src/dapps/registry.html new file mode 100644 index 000000000..21b09dc12 --- /dev/null +++ b/js/src/dapps/registry.html @@ -0,0 +1,16 @@ + + + + + + + Token Registry + + +
+ + + + + + diff --git a/js/src/dapps/registry.js b/js/src/dapps/registry.js new file mode 100644 index 000000000..ebcff155a --- /dev/null +++ b/js/src/dapps/registry.js @@ -0,0 +1,37 @@ +// Copyright 2015, 2016 Ethcore (UK) Ltd. +// This file is part of Parity. + +// Parity is free software: you can redistribute it and/or modify +// it under the terms of the GNU General Public License as published by +// the Free Software Foundation, either version 3 of the License, or +// (at your option) any later version. + +// Parity is distributed in the hope that it will be useful, +// but WITHOUT ANY WARRANTY; without even the implied warranty of +// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the +// GNU General Public License for more details. + +// You should have received a copy of the GNU General Public License +// along with Parity. If not, see . + +import React from 'react'; +import ReactDOM from 'react-dom'; +import { Provider } from 'react-redux'; + +import injectTapEventPlugin from 'react-tap-event-plugin'; +injectTapEventPlugin(); + +import store from './registry/store'; +import Container from './registry/Container'; + +import '../../assets/fonts/Roboto/font.css'; +import '../../assets/fonts/RobotoMono/font.css'; +import './style.css'; +import './registry.html'; + +ReactDOM.render( + + + , + document.querySelector('#container') +); diff --git a/js/src/dapps/registry/Accounts/accounts.css b/js/src/dapps/registry/Accounts/accounts.css new file mode 100644 index 000000000..344a92867 --- /dev/null +++ b/js/src/dapps/registry/Accounts/accounts.css @@ -0,0 +1,38 @@ +/* Copyright 2015, 2016 Ethcore (UK) Ltd. +/* This file is part of Parity. +/* +/* Parity is free software: you can redistribute it and/or modify +/* it under the terms of the GNU General Public License as published by +/* the Free Software Foundation, either version 3 of the License, or +/* (at your option) any later version. +/* +/* Parity is distributed in the hope that it will be useful, +/* but WITHOUT ANY WARRANTY; without even the implied warranty of +/* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the +/* GNU General Public License for more details. +/* +/* You should have received a copy of the GNU General Public License +/* along with Parity. If not, see . +*/ + +.button { + /* TODO remove !important once material design lite is used */ + padding: 0 !important; +} + +.icon { + /* TODO remove !important once material design lite is used */ + margin: 0 !important; + width: 30px !important; + height: 30px !important; +} + +.menuIcon { + display: inline-block; + vertical-align: middle; +} +.menuText { + display: inline-block; + line-height: 24px; + vertical-align: top; +} diff --git a/js/src/dapps/registry/Accounts/accounts.js b/js/src/dapps/registry/Accounts/accounts.js new file mode 100644 index 000000000..fe262a84f --- /dev/null +++ b/js/src/dapps/registry/Accounts/accounts.js @@ -0,0 +1,76 @@ +// Copyright 2015, 2016 Ethcore (UK) Ltd. +// This file is part of Parity. + +// Parity is free software: you can redistribute it and/or modify +// it under the terms of the GNU General Public License as published by +// the Free Software Foundation, either version 3 of the License, or +// (at your option) any later version. + +// Parity is distributed in the hope that it will be useful, +// but WITHOUT ANY WARRANTY; without even the implied warranty of +// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the +// GNU General Public License for more details. + +// You should have received a copy of the GNU General Public License +// along with Parity. If not, see . + +import React, { Component, PropTypes } from 'react'; +import IconMenu from 'material-ui/IconMenu'; +import IconButton from 'material-ui/IconButton/IconButton'; +import AccountIcon from 'material-ui/svg-icons/action/account-circle'; +import MenuItem from 'material-ui/MenuItem'; + +import IdentityIcon from '../IdentityIcon'; +import renderAddress from '../ui/address'; + +import styles from './accounts.css'; + +export default class Accounts extends Component { + + static propTypes = { + actions: PropTypes.object.isRequired, + all: PropTypes.object.isRequired, + selected: PropTypes.object + } + + render () { + const { all, selected } = this.props; + + const accountsButton = ( + + { selected + ? () + : () + } + ); + + return ( + + { Object.values(all).map(this.renderAccount) } + + ); + } + + renderAccount = (account) => { + const { all, selected } = this.props; + const isSelected = selected && selected.address === account.address; + + return ( + + { renderAddress(account.address, all, {}) } + + ); + }; + + onAccountSelect = (e, address) => { + this.props.actions.select(address); + }; +} diff --git a/js/src/dapps/registry/Accounts/actions.js b/js/src/dapps/registry/Accounts/actions.js new file mode 100644 index 000000000..4f2bd3c70 --- /dev/null +++ b/js/src/dapps/registry/Accounts/actions.js @@ -0,0 +1,17 @@ +// Copyright 2015, 2016 Ethcore (UK) Ltd. +// This file is part of Parity. + +// Parity is free software: you can redistribute it and/or modify +// it under the terms of the GNU General Public License as published by +// the Free Software Foundation, either version 3 of the License, or +// (at your option) any later version. + +// Parity is distributed in the hope that it will be useful, +// but WITHOUT ANY WARRANTY; without even the implied warranty of +// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the +// GNU General Public License for more details. + +// You should have received a copy of the GNU General Public License +// along with Parity. If not, see . + +export const select = (address) => ({ type: 'accounts select', address }); diff --git a/js/src/dapps/registry/Accounts/index.js b/js/src/dapps/registry/Accounts/index.js new file mode 100644 index 000000000..e9be1d557 --- /dev/null +++ b/js/src/dapps/registry/Accounts/index.js @@ -0,0 +1,17 @@ +// Copyright 2015, 2016 Ethcore (UK) Ltd. +// This file is part of Parity. + +// Parity is free software: you can redistribute it and/or modify +// it under the terms of the GNU General Public License as published by +// the Free Software Foundation, either version 3 of the License, or +// (at your option) any later version. + +// Parity is distributed in the hope that it will be useful, +// but WITHOUT ANY WARRANTY; without even the implied warranty of +// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the +// GNU General Public License for more details. + +// You should have received a copy of the GNU General Public License +// along with Parity. If not, see . + +export default from './accounts'; diff --git a/js/src/dapps/registry/Application/application.css b/js/src/dapps/registry/Application/application.css new file mode 100644 index 000000000..c5a54040e --- /dev/null +++ b/js/src/dapps/registry/Application/application.css @@ -0,0 +1,39 @@ +/* Copyright 2015, 2016 Ethcore (UK) Ltd. +/* This file is part of Parity. +/* +/* Parity is free software: you can redistribute it and/or modify +/* it under the terms of the GNU General Public License as published by +/* the Free Software Foundation, either version 3 of the License, or +/* (at your option) any later version. +/* +/* Parity is distributed in the hope that it will be useful, +/* but WITHOUT ANY WARRANTY; without even the implied warranty of +/* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the +/* GNU General Public License for more details. +/* +/* You should have received a copy of the GNU General Public License +/* along with Parity. If not, see . +*/ + +.header { + display: flex; + justify-content: space-between; + margin: 0; padding: .3em 1em; + color: #fff; + background-color: #333; +} + +.header h1 { + margin-top: 0; + margin-bottom: 0; + line-height: 50px; + font-size: 200%; + text-transform: uppercase; +} + +.address { + margin-bottom: 0; + padding: 1rem; + font-size: 80%; + background-color: #f0f0f0; +} diff --git a/js/src/dapps/registry/Application/application.js b/js/src/dapps/registry/Application/application.js new file mode 100644 index 000000000..3d3d8d582 --- /dev/null +++ b/js/src/dapps/registry/Application/application.js @@ -0,0 +1,88 @@ +// Copyright 2015, 2016 Ethcore (UK) Ltd. +// This file is part of Parity. + +// Parity is free software: you can redistribute it and/or modify +// it under the terms of the GNU General Public License as published by +// the Free Software Foundation, either version 3 of the License, or +// (at your option) any later version. + +// Parity is distributed in the hope that it will be useful, +// but WITHOUT ANY WARRANTY; without even the implied warranty of +// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the +// GNU General Public License for more details. + +// You should have received a copy of the GNU General Public License +// along with Parity. If not, see . + +import React, { Component, PropTypes } from 'react'; + +import getMuiTheme from 'material-ui/styles/getMuiTheme'; +import lightBaseTheme from 'material-ui/styles/baseThemes/lightBaseTheme'; +const muiTheme = getMuiTheme(lightBaseTheme); + +import CircularProgress from 'material-ui/CircularProgress'; +import styles from './application.css'; +import Accounts from '../Accounts'; +import Events from '../Events'; +import Lookup from '../Lookup'; +import Names from '../Names'; +import Records from '../Records'; + +const nullable = (type) => React.PropTypes.oneOfType([ React.PropTypes.oneOf([ null ]), type ]); + +export default class Application extends Component { + static childContextTypes = { + muiTheme: PropTypes.object.isRequired, + api: PropTypes.object.isRequired + }; + getChildContext () { + return { muiTheme, api: window.parity.api }; + } + + static propTypes = { + actions: PropTypes.object.isRequired, + accounts: PropTypes.object.isRequired, + contacts: PropTypes.object.isRequired, + contract: nullable(PropTypes.object.isRequired), + fee: nullable(PropTypes.object.isRequired), + lookup: PropTypes.object.isRequired, + events: PropTypes.object.isRequired, + names: PropTypes.object.isRequired, + records: PropTypes.object.isRequired + }; + + render () { + const { + actions, + accounts, contacts, + contract, fee, + lookup, + events, + names, + records + } = this.props; + + return ( +
+
+

RΞgistry

+ +
+ { contract && fee ? ( +
+ + + + +

+ The Registry is provided by the contract at { contract.address }. +

+
+ ) : ( + + ) } +
+ ); + } + +} diff --git a/js/src/dapps/registry/Application/index.js b/js/src/dapps/registry/Application/index.js new file mode 100644 index 000000000..236578226 --- /dev/null +++ b/js/src/dapps/registry/Application/index.js @@ -0,0 +1,17 @@ +// Copyright 2015, 2016 Ethcore (UK) Ltd. +// This file is part of Parity. + +// Parity is free software: you can redistribute it and/or modify +// it under the terms of the GNU General Public License as published by +// the Free Software Foundation, either version 3 of the License, or +// (at your option) any later version. + +// Parity is distributed in the hope that it will be useful, +// but WITHOUT ANY WARRANTY; without even the implied warranty of +// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the +// GNU General Public License for more details. + +// You should have received a copy of the GNU General Public License +// along with Parity. If not, see . + +export default from './application'; diff --git a/js/src/dapps/registry/Container.js b/js/src/dapps/registry/Container.js new file mode 100644 index 000000000..1464320e6 --- /dev/null +++ b/js/src/dapps/registry/Container.js @@ -0,0 +1,66 @@ +// Copyright 2015, 2016 Ethcore (UK) Ltd. +// This file is part of Parity. + +// Parity is free software: you can redistribute it and/or modify +// it under the terms of the GNU General Public License as published by +// the Free Software Foundation, either version 3 of the License, or +// (at your option) any later version. + +// Parity is distributed in the hope that it will be useful, +// but WITHOUT ANY WARRANTY; without even the implied warranty of +// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the +// GNU General Public License for more details. + +// You should have received a copy of the GNU General Public License +// along with Parity. If not, see . + +import React, { Component, PropTypes } from 'react'; +import { connect } from 'react-redux'; +import { bindActionCreators } from 'redux'; + +import Application from './Application'; +import * as actions from './actions'; + +const nullable = (type) => React.PropTypes.oneOfType([ React.PropTypes.oneOf([ null ]), type ]); + +class Container extends Component { + static propTypes = { + actions: PropTypes.object.isRequired, + accounts: PropTypes.object.isRequired, + contacts: PropTypes.object.isRequired, + contract: nullable(PropTypes.object.isRequired), + owner: nullable(PropTypes.string.isRequired), + fee: nullable(PropTypes.object.isRequired), + lookup: PropTypes.object.isRequired, + events: PropTypes.object.isRequired + }; + + componentDidMount () { + Promise.all([ + this.props.actions.addresses.fetch(), + this.props.actions.fetchContract() + ]).then(() => { + this.props.actions.events.subscribe('Reserved'); + }); + } + + render () { + return (); + } +} + +export default connect( + // redux -> react connection + (state) => state, + // react -> redux connection + (dispatch) => { + const bound = bindActionCreators(actions, dispatch); + bound.addresses = bindActionCreators(actions.addresses, dispatch); + bound.accounts = bindActionCreators(actions.accounts, dispatch); + bound.lookup = bindActionCreators(actions.lookup, dispatch); + bound.events = bindActionCreators(actions.events, dispatch); + bound.names = bindActionCreators(actions.names, dispatch); + bound.records = bindActionCreators(actions.records, dispatch); + return { actions: bound }; + } +)(Container); diff --git a/js/src/dapps/registry/Events/actions.js b/js/src/dapps/registry/Events/actions.js new file mode 100644 index 000000000..e12c98c4f --- /dev/null +++ b/js/src/dapps/registry/Events/actions.js @@ -0,0 +1,85 @@ +// Copyright 2015, 2016 Ethcore (UK) Ltd. +// This file is part of Parity. + +// Parity is free software: you can redistribute it and/or modify +// it under the terms of the GNU General Public License as published by +// the Free Software Foundation, either version 3 of the License, or +// (at your option) any later version. + +// Parity is distributed in the hope that it will be useful, +// but WITHOUT ANY WARRANTY; without even the implied warranty of +// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the +// GNU General Public License for more details. + +// You should have received a copy of the GNU General Public License +// along with Parity. If not, see . + +import { api } from '../parity.js'; + +export const start = (name, from, to) => ({ type: 'events subscribe start', name, from, to }); +export const fail = (name) => ({ type: 'events subscribe fail', name }); +export const success = (name, subscription) => ({ type: 'events subscribe success', name, subscription }); + +export const event = (name, event) => ({ type: 'events event', name, event }); + +export const subscribe = (name, from = 0, to = 'pending') => + (dispatch, getState) => { + const { contract } = getState(); + if (!contract) return; + const opt = { fromBlock: from, toBlock: to, limit: 50 }; + + dispatch(start(name, from, to)); + + contract + .subscribe(name, opt, (error, events) => { + if (error) { + console.error(`error receiving events for ${name}`, error); + return; + } + + events.forEach((e) => { + api.eth.getBlockByNumber(e.blockNumber) + .then((block) => { + const data = { + type: name, + key: '' + e.transactionHash + e.logIndex, + state: e.type, + block: e.blockNumber, + index: e.logIndex, + transaction: e.transactionHash, + parameters: e.params, + timestamp: block.timestamp + }; + dispatch(event(name, data)); + }) + .catch((err) => { + console.error(`could not fetch block ${e.blockNumber}.`); + console.error(err); + }); + }); + }) + .then((subscriptionId) => { + dispatch(success(name, subscriptionId)); + }) + .catch((error) => { + console.error('event subscription failed', error); + dispatch(fail(name)); + }); + }; + +export const unsubscribe = (name) => + (dispatch, getState) => { + const state = getState(); + if (!state.contract) return; + const subscriptions = state.events.subscriptions; + if (!(name in subscriptions) || subscriptions[name] === null) return; + + state.contract + .unsubscribe(subscriptions[name]) + .then(() => { + dispatch({ type: 'events unsubscribe', name }); + }) + .catch((error) => { + console.error('event unsubscribe failed', error); + }); + }; diff --git a/js/src/dapps/registry/Events/events.css b/js/src/dapps/registry/Events/events.css new file mode 100644 index 000000000..66277dae3 --- /dev/null +++ b/js/src/dapps/registry/Events/events.css @@ -0,0 +1,51 @@ +/* Copyright 2015, 2016 Ethcore (UK) Ltd. +/* This file is part of Parity. +/* +/* Parity is free software: you can redistribute it and/or modify +/* it under the terms of the GNU General Public License as published by +/* the Free Software Foundation, either version 3 of the License, or +/* (at your option) any later version. +/* +/* Parity is distributed in the hope that it will be useful, +/* but WITHOUT ANY WARRANTY; without even the implied warranty of +/* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the +/* GNU General Public License for more details. +/* +/* You should have received a copy of the GNU General Public License +/* along with Parity. If not, see . +*/ + +.events { + margin: 1em; +} + +.options { + margin: 0 .5em; +} + +.reserved, .dropped, .dataChanged { +} + +.reserved abbr, .dropped abbr { + cursor: help; +} + +.pending { + opacity: .6; +} + +.owner code { + display: inline-block; + vertical-align: top; + line-height: 32px; + word-wrap: break-word; +} + +.eventsList { + width: 100%; + boder: none; +} + +.eventsList td { + padding: 0.25em 0.5em; +} diff --git a/js/src/dapps/registry/Events/events.js b/js/src/dapps/registry/Events/events.js new file mode 100644 index 000000000..ffb1fc919 --- /dev/null +++ b/js/src/dapps/registry/Events/events.js @@ -0,0 +1,167 @@ +// Copyright 2015, 2016 Ethcore (UK) Ltd. +// This file is part of Parity. + +// Parity is free software: you can redistribute it and/or modify +// it under the terms of the GNU General Public License as published by +// the Free Software Foundation, either version 3 of the License, or +// (at your option) any later version. + +// Parity is distributed in the hope that it will be useful, +// but WITHOUT ANY WARRANTY; without even the implied warranty of +// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the +// GNU General Public License for more details. + +// You should have received a copy of the GNU General Public License +// along with Parity. If not, see . + +import React, { Component, PropTypes } from 'react'; +import { Card, CardHeader, CardActions, CardText } from 'material-ui/Card'; +import Toggle from 'material-ui/Toggle'; +import moment from 'moment'; + +import { bytesToHex } from '../parity'; +import renderHash from '../ui/hash'; +import renderAddress from '../ui/address'; +import styles from './events.css'; + +const inlineButton = { + display: 'inline-block', + width: 'auto', + marginRight: '1em' +}; + +const renderStatus = (timestamp, isPending) => { + timestamp = moment(timestamp); + if (isPending) { + return (pending); + } + return ( + + ); +}; + +const renderEvent = (classNames, verb) => (e, accounts, contacts) => { + const classes = e.state === 'pending' + ? classNames + ' ' + styles.pending : classNames; + + return ( + + { renderAddress(e.parameters.owner, accounts, contacts) } + { verb } + { renderHash(bytesToHex(e.parameters.name)) } + { renderStatus(e.timestamp, e.state === 'pending') } + + ); +}; + +const renderDataChanged = (e, accounts, contacts) => { + let classNames = styles.dataChanged; + if (e.state === 'pending') { + classNames += ' ' + styles.pending; + } + + return ( + + { renderAddress(e.parameters.owner, accounts, contacts) } + updated + + key { new Buffer(e.parameters.plainKey).toString('utf8') } of { renderHash(bytesToHex(e.parameters.name)) } + + { renderStatus(e.timestamp, e.state === 'pending') } + + ); +}; + +const eventTypes = { + Reserved: renderEvent(styles.reserved, 'reserved'), + Dropped: renderEvent(styles.dropped, 'dropped'), + DataChanged: renderDataChanged +}; + +export default class Events extends Component { + + static propTypes = { + actions: PropTypes.object.isRequired, + subscriptions: PropTypes.object.isRequired, + pending: PropTypes.object.isRequired, + events: PropTypes.array.isRequired, + accounts: PropTypes.object.isRequired, + contacts: PropTypes.object.isRequired + } + + render () { + const { subscriptions, pending, accounts, contacts } = this.props; + return ( + + + + + + + + + + + { + this.props.events + .filter((e) => eventTypes[e.type]) + .map((e) => eventTypes[e.type](e, accounts, contacts)) + } + +
+
+
+ ); + } + + onReservedToggle = (e, isToggled) => { + const { pending, subscriptions, actions } = this.props; + if (!pending.Reserved) { + if (isToggled && subscriptions.Reserved === null) { + actions.subscribe('Reserved'); + } else if (!isToggled && subscriptions.Reserved !== null) { + actions.unsubscribe('Reserved'); + } + } + }; + onDroppedToggle = (e, isToggled) => { + const { pending, subscriptions, actions } = this.props; + if (!pending.Dropped) { + if (isToggled && subscriptions.Dropped === null) { + actions.subscribe('Dropped'); + } else if (!isToggled && subscriptions.Dropped !== null) { + actions.unsubscribe('Dropped'); + } + } + }; + onDataChangedToggle = (e, isToggled) => { + const { pending, subscriptions, actions } = this.props; + if (!pending.DataChanged) { + if (isToggled && subscriptions.DataChanged === null) { + actions.subscribe('DataChanged'); + } else if (!isToggled && subscriptions.DataChanged !== null) { + actions.unsubscribe('DataChanged'); + } + } + }; +} diff --git a/js/src/dapps/registry/Events/index.js b/js/src/dapps/registry/Events/index.js new file mode 100644 index 000000000..15d43c375 --- /dev/null +++ b/js/src/dapps/registry/Events/index.js @@ -0,0 +1,17 @@ +// Copyright 2015, 2016 Ethcore (UK) Ltd. +// This file is part of Parity. + +// Parity is free software: you can redistribute it and/or modify +// it under the terms of the GNU General Public License as published by +// the Free Software Foundation, either version 3 of the License, or +// (at your option) any later version. + +// Parity is distributed in the hope that it will be useful, +// but WITHOUT ANY WARRANTY; without even the implied warranty of +// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the +// GNU General Public License for more details. + +// You should have received a copy of the GNU General Public License +// along with Parity. If not, see . + +export default from './events.js'; diff --git a/js/src/dapps/registry/Events/reducers.js b/js/src/dapps/registry/Events/reducers.js new file mode 100644 index 000000000..6ff0011d3 --- /dev/null +++ b/js/src/dapps/registry/Events/reducers.js @@ -0,0 +1,76 @@ +// Copyright 2015, 2016 Ethcore (UK) Ltd. +// This file is part of Parity. + +// Parity is free software: you can redistribute it and/or modify +// it under the terms of the GNU General Public License as published by +// the Free Software Foundation, either version 3 of the License, or +// (at your option) any later version. + +// Parity is distributed in the hope that it will be useful, +// but WITHOUT ANY WARRANTY; without even the implied warranty of +// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the +// GNU General Public License for more details. + +// You should have received a copy of the GNU General Public License +// along with Parity. If not, see . + +const initialState = { + subscriptions: { + Reserved: null, + Dropped: null, + DataChanged: null + }, + pending: { + Reserved: false, + Dropped: false, + DataChanged: false + }, + events: [] +}; + +const sortEvents = (a, b) => { + if (a.state === 'pending' && b.state !== 'pending') return -1; + if (a.state !== 'pending' && b.state === 'pending') return 1; + const d = b.block.minus(a.block).toFixed(0); + if (d === 0) return b.index.minus(a.index).toFixed(0); + return d; +}; + +export default (state = initialState, action) => { + if (!(action.name in state.subscriptions)) { // invalid event name + return state; + } + + if (action.type === 'events subscribe start') { + return { ...state, pending: { ...state.pending, [action.name]: true } }; + } + if (action.type === 'events subscribe fail') { + return { ...state, pending: { ...state.pending, [action.name]: false } }; + } + if (action.type === 'events subscribe success') { + return { + ...state, + pending: { ...state.pending, [action.name]: false }, + subscriptions: { ...state.subscriptions, [action.name]: action.subscription } + }; + } + + if (action.type === 'events unsubscribe') { + return { + ...state, + pending: { ...state.pending, [action.name]: false }, + subscriptions: { ...state.subscriptions, [action.name]: null }, + events: state.events.filter((event) => event.type !== action.name) + }; + } + + if (action.type === 'events event') { + return { ...state, events: state.events + .filter((event) => event.key !== action.event.key) + .concat(action.event) + .sort(sortEvents) + }; + } + + return state; +}; diff --git a/js/src/dapps/registry/IdentityIcon/identityIcon.css b/js/src/dapps/registry/IdentityIcon/identityIcon.css new file mode 100644 index 000000000..3fa1df99e --- /dev/null +++ b/js/src/dapps/registry/IdentityIcon/identityIcon.css @@ -0,0 +1,23 @@ +/* Copyright 2015, 2016 Ethcore (UK) Ltd. +/* This file is part of Parity. +/* +/* Parity is free software: you can redistribute it and/or modify +/* it under the terms of the GNU General Public License as published by +/* the Free Software Foundation, either version 3 of the License, or +/* (at your option) any later version. +/* +/* Parity is distributed in the hope that it will be useful, +/* but WITHOUT ANY WARRANTY; without even the implied warranty of +/* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the +/* GNU General Public License for more details. +/* +/* You should have received a copy of the GNU General Public License +/* along with Parity. If not, see . +*/ + +.icon { + width: 24px; + height: 24px; + border-radius: 50%; + margin-right: 0.5em; +} diff --git a/js/src/dapps/registry/IdentityIcon/identityIcon.js b/js/src/dapps/registry/IdentityIcon/identityIcon.js new file mode 100644 index 000000000..e4baf2705 --- /dev/null +++ b/js/src/dapps/registry/IdentityIcon/identityIcon.js @@ -0,0 +1,39 @@ +// Copyright 2015, 2016 Ethcore (UK) Ltd. +// This file is part of Parity. + +// Parity is free software: you can redistribute it and/or modify +// it under the terms of the GNU General Public License as published by +// the Free Software Foundation, either version 3 of the License, or +// (at your option) any later version. + +// Parity is distributed in the hope that it will be useful, +// but WITHOUT ANY WARRANTY; without even the implied warranty of +// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the +// GNU General Public License for more details. + +// You should have received a copy of the GNU General Public License +// along with Parity. If not, see . + +import React, { Component, PropTypes } from 'react'; + +import { api } from '../parity'; +import styles from './identityIcon.css'; + +export default class IdentityIcon extends Component { + static propTypes = { + address: PropTypes.string.isRequired, + className: PropTypes.string, + style: PropTypes.object + } + + render () { + const { address, className, style } = this.props; + + return ( + + ); + } +} diff --git a/js/src/dapps/registry/IdentityIcon/index.js b/js/src/dapps/registry/IdentityIcon/index.js new file mode 100644 index 000000000..76c107bfb --- /dev/null +++ b/js/src/dapps/registry/IdentityIcon/index.js @@ -0,0 +1,17 @@ +// Copyright 2015, 2016 Ethcore (UK) Ltd. +// This file is part of Parity. + +// Parity is free software: you can redistribute it and/or modify +// it under the terms of the GNU General Public License as published by +// the Free Software Foundation, either version 3 of the License, or +// (at your option) any later version. + +// Parity is distributed in the hope that it will be useful, +// but WITHOUT ANY WARRANTY; without even the implied warranty of +// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the +// GNU General Public License for more details. + +// You should have received a copy of the GNU General Public License +// along with Parity. If not, see . + +export default from './identityIcon'; diff --git a/js/src/dapps/registry/Lookup/actions.js b/js/src/dapps/registry/Lookup/actions.js new file mode 100644 index 000000000..a7df87585 --- /dev/null +++ b/js/src/dapps/registry/Lookup/actions.js @@ -0,0 +1,42 @@ +// Copyright 2015, 2016 Ethcore (UK) Ltd. +// This file is part of Parity. + +// Parity is free software: you can redistribute it and/or modify +// it under the terms of the GNU General Public License as published by +// the Free Software Foundation, either version 3 of the License, or +// (at your option) any later version. + +// Parity is distributed in the hope that it will be useful, +// but WITHOUT ANY WARRANTY; without even the implied warranty of +// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the +// GNU General Public License for more details. + +// You should have received a copy of the GNU General Public License +// along with Parity. If not, see . + +import { sha3 } from '../parity.js'; + +export const clear = () => ({ type: 'lookup clear' }); + +export const start = (name, key) => ({ type: 'lookup start', name, key }); + +export const success = (address) => ({ type: 'lookup success', result: address }); + +export const fail = () => ({ type: 'lookup error' }); + +export const lookup = (name, key) => (dispatch, getState) => { + const { contract } = getState(); + if (!contract) return; + const getAddress = contract.functions + .find((f) => f.name === 'getAddress'); + + name = name.toLowerCase(); + dispatch(start(name, key)); + getAddress.call({}, [sha3(name), key]) + .then((address) => dispatch(success(address))) + .catch((err) => { + console.error(`could not lookup ${key} for ${name}`); + if (err) console.error(err.stack); + dispatch(fail()); + }); +}; diff --git a/js/src/dapps/registry/Lookup/index.js b/js/src/dapps/registry/Lookup/index.js new file mode 100644 index 000000000..f4ade7c00 --- /dev/null +++ b/js/src/dapps/registry/Lookup/index.js @@ -0,0 +1,17 @@ +// Copyright 2015, 2016 Ethcore (UK) Ltd. +// This file is part of Parity. + +// Parity is free software: you can redistribute it and/or modify +// it under the terms of the GNU General Public License as published by +// the Free Software Foundation, either version 3 of the License, or +// (at your option) any later version. + +// Parity is distributed in the hope that it will be useful, +// but WITHOUT ANY WARRANTY; without even the implied warranty of +// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the +// GNU General Public License for more details. + +// You should have received a copy of the GNU General Public License +// along with Parity. If not, see . + +export default from './lookup.js'; diff --git a/js/src/dapps/registry/Lookup/lookup.css b/js/src/dapps/registry/Lookup/lookup.css new file mode 100644 index 000000000..12ddd040c --- /dev/null +++ b/js/src/dapps/registry/Lookup/lookup.css @@ -0,0 +1,28 @@ +/* Copyright 2015, 2016 Ethcore (UK) Ltd. +/* This file is part of Parity. +/* +/* Parity is free software: you can redistribute it and/or modify +/* it under the terms of the GNU General Public License as published by +/* the Free Software Foundation, either version 3 of the License, or +/* (at your option) any later version. +/* +/* Parity is distributed in the hope that it will be useful, +/* but WITHOUT ANY WARRANTY; without even the implied warranty of +/* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the +/* GNU General Public License for more details. +/* +/* You should have received a copy of the GNU General Public License +/* along with Parity. If not, see . +*/ + +.lookup { + margin: 1em; +} + +.box { + margin: 0 1em; +} + +.spacing { + margin-left: 1em; +} diff --git a/js/src/dapps/registry/Lookup/lookup.js b/js/src/dapps/registry/Lookup/lookup.js new file mode 100644 index 000000000..4238f1160 --- /dev/null +++ b/js/src/dapps/registry/Lookup/lookup.js @@ -0,0 +1,98 @@ +// Copyright 2015, 2016 Ethcore (UK) Ltd. +// This file is part of Parity. + +// Parity is free software: you can redistribute it and/or modify +// it under the terms of the GNU General Public License as published by +// the Free Software Foundation, either version 3 of the License, or +// (at your option) any later version. + +// Parity is distributed in the hope that it will be useful, +// but WITHOUT ANY WARRANTY; without even the implied warranty of +// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the +// GNU General Public License for more details. + +// You should have received a copy of the GNU General Public License +// along with Parity. If not, see . + +import React, { Component, PropTypes } from 'react'; +import { Card, CardHeader, CardText } from 'material-ui/Card'; +import TextField from 'material-ui/TextField'; +import RaisedButton from 'material-ui/RaisedButton'; +import SearchIcon from 'material-ui/svg-icons/action/search'; +import renderAddress from '../ui/address.js'; +import renderImage from '../ui/image.js'; + +import recordTypeSelect from '../ui/record-type-select.js'; +import styles from './lookup.css'; + +const nullable = (type) => React.PropTypes.oneOfType([ React.PropTypes.oneOf([ null ]), type ]); + +export default class Lookup extends Component { + + static propTypes = { + actions: PropTypes.object.isRequired, + name: PropTypes.string.isRequired, + type: PropTypes.string.isRequired, + result: nullable(PropTypes.string.isRequired), + accounts: PropTypes.object.isRequired, + contacts: PropTypes.object.isRequired + } + + state = { name: '', type: 'A' }; + + render () { + const name = this.state.name || this.props.name; + const type = this.state.type || this.props.type; + const { result, accounts, contacts } = this.props; + + let output = ''; + if (result) { + if (type === 'A') { + output = ({ renderAddress(result, accounts, contacts, false) }); + } else if (type === 'IMG') { + output = renderImage(result); + } else if (type === 'CONTENT') { + output = (
+ { result } +

This is most likely just the hash of the content you are looking for

+
); + } else { + output = ({ result }); + } + } + + return ( + + +
+ + { recordTypeSelect(type, this.onTypeChange, styles.spacing) } + } + onClick={ this.onLookupClick } + /> +
+ { output } +
+ ); + } + + onNameChange = (e) => { + this.setState({ name: e.target.value }); + }; + onTypeChange = (e, i, type) => { + this.setState({ type }); + this.props.actions.clear(); + }; + onLookupClick = () => { + this.props.actions.lookup(this.state.name, this.state.type); + }; +} diff --git a/js/src/dapps/registry/Lookup/reducers.js b/js/src/dapps/registry/Lookup/reducers.js new file mode 100644 index 000000000..f96e784bd --- /dev/null +++ b/js/src/dapps/registry/Lookup/reducers.js @@ -0,0 +1,53 @@ +// Copyright 2015, 2016 Ethcore (UK) Ltd. +// This file is part of Parity. + +// Parity is free software: you can redistribute it and/or modify +// it under the terms of the GNU General Public License as published by +// the Free Software Foundation, either version 3 of the License, or +// (at your option) any later version. + +// Parity is distributed in the hope that it will be useful, +// but WITHOUT ANY WARRANTY; without even the implied warranty of +// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the +// GNU General Public License for more details. + +// You should have received a copy of the GNU General Public License +// along with Parity. If not, see . + +const initialState = { + pending: false, + name: '', type: '', + result: null +}; + +export default (state = initialState, action) => { + if (action.type === 'lookup clear') { + return { ...state, result: null }; + } + + if (action.type === 'lookup start') { + return { + pending: true, + name: action.name, type: action.entry, + result: null + }; + } + + if (action.type === 'lookup error') { + return { + pending: false, + name: initialState.name, type: initialState.type, + result: null + }; + } + + if (action.type === 'lookup success') { + return { + pending: false, + name: initialState.name, type: initialState.type, + result: action.result + }; + } + + return state; +}; diff --git a/js/src/dapps/registry/Names/actions.js b/js/src/dapps/registry/Names/actions.js new file mode 100644 index 000000000..ee73ad78e --- /dev/null +++ b/js/src/dapps/registry/Names/actions.js @@ -0,0 +1,89 @@ +// Copyright 2015, 2016 Ethcore (UK) Ltd. +// This file is part of Parity. + +// Parity is free software: you can redistribute it and/or modify +// it under the terms of the GNU General Public License as published by +// the Free Software Foundation, either version 3 of the License, or +// (at your option) any later version. + +// Parity is distributed in the hope that it will be useful, +// but WITHOUT ANY WARRANTY; without even the implied warranty of +// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the +// GNU General Public License for more details. + +// You should have received a copy of the GNU General Public License +// along with Parity. If not, see . + +import { sha3, toWei } from '../parity.js'; + +const alreadyQueued = (queue, action, name) => + !!queue.find((entry) => entry.action === action && entry.name === name); + +export const reserveStart = (name) => ({ type: 'names reserve start', name }); + +export const reserveSuccess = (name) => ({ type: 'names reserve success', name }); + +export const reserveFail = (name) => ({ type: 'names reserve fail', name }); + +export const reserve = (name) => (dispatch, getState) => { + const state = getState(); + const account = state.accounts.selected; + const contract = state.contract; + if (!contract || !account) return; + if (alreadyQueued(state.names.queue, 'reserve', name)) return; + const reserve = contract.functions.find((f) => f.name === 'reserve'); + + name = name.toLowerCase(); + const options = { + from: account.address, + value: toWei(1).toString() + }; + const values = [ sha3(name) ]; + + dispatch(reserveStart(name)); + reserve.estimateGas(options, values) + .then((gas) => { + options.gas = gas.mul(1.2).toFixed(0); + return reserve.postTransaction(options, values); + }) + .then((data) => { + dispatch(reserveSuccess(name)); + }).catch((err) => { + console.error(`could not reserve ${name}`); + if (err) console.error(err.stack); + dispatch(reserveFail(name)); + }); +}; + +export const dropStart = (name) => ({ type: 'names drop start', name }); + +export const dropSuccess = (name) => ({ type: 'names drop success', name }); + +export const dropFail = (name) => ({ type: 'names drop fail', name }); + +export const drop = (name) => (dispatch, getState) => { + const state = getState(); + const account = state.accounts.selected; + const contract = state.contract; + if (!contract || !account) return; + if (alreadyQueued(state.names.queue, 'drop', name)) return; + const drop = contract.functions.find((f) => f.name === 'drop'); + + name = name.toLowerCase(); + const options = { from: account.address }; + const values = [ sha3(name) ]; + + dispatch(dropStart(name)); + drop.estimateGas(options, values) + .then((gas) => { + options.gas = gas.mul(1.2).toFixed(0); + return drop.postTransaction(options, values); + }) + .then((data) => { + dispatch(dropSuccess(name)); + }).catch((err) => { + console.error(`could not drop ${name}`); + if (err) console.error(err.stack); + dispatch(reserveFail(name)); + }); +}; diff --git a/js/src/dapps/registry/Names/index.js b/js/src/dapps/registry/Names/index.js new file mode 100644 index 000000000..26195de88 --- /dev/null +++ b/js/src/dapps/registry/Names/index.js @@ -0,0 +1,17 @@ +// Copyright 2015, 2016 Ethcore (UK) Ltd. +// This file is part of Parity. + +// Parity is free software: you can redistribute it and/or modify +// it under the terms of the GNU General Public License as published by +// the Free Software Foundation, either version 3 of the License, or +// (at your option) any later version. + +// Parity is distributed in the hope that it will be useful, +// but WITHOUT ANY WARRANTY; without even the implied warranty of +// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the +// GNU General Public License for more details. + +// You should have received a copy of the GNU General Public License +// along with Parity. If not, see . + +export default from './names.js'; diff --git a/js/src/dapps/registry/Names/names.css b/js/src/dapps/registry/Names/names.css new file mode 100644 index 000000000..a058d41ac --- /dev/null +++ b/js/src/dapps/registry/Names/names.css @@ -0,0 +1,40 @@ +/* Copyright 2015, 2016 Ethcore (UK) Ltd. +/* This file is part of Parity. +/* +/* Parity is free software: you can redistribute it and/or modify +/* it under the terms of the GNU General Public License as published by +/* the Free Software Foundation, either version 3 of the License, or +/* (at your option) any later version. +/* +/* Parity is distributed in the hope that it will be useful, +/* but WITHOUT ANY WARRANTY; without even the implied warranty of +/* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the +/* GNU General Public License for more details. +/* +/* You should have received a copy of the GNU General Public License +/* along with Parity. If not, see . +*/ + +.names { + margin: 1em; +} + +.box { + margin: 0 1em 1em 1em; +} + +.spacing { + margin-left: 1em; +} + +.noSpacing { + margin-top: 0; +} + +.link { + color: #00BCD4; + text-decoration: none; +} +.link:hover { + text-decoration: underline; +} diff --git a/js/src/dapps/registry/Names/names.js b/js/src/dapps/registry/Names/names.js new file mode 100644 index 000000000..907e3bf87 --- /dev/null +++ b/js/src/dapps/registry/Names/names.js @@ -0,0 +1,144 @@ +// Copyright 2015, 2016 Ethcore (UK) Ltd. +// This file is part of Parity. + +// Parity is free software: you can redistribute it and/or modify +// it under the terms of the GNU General Public License as published by +// the Free Software Foundation, either version 3 of the License, or +// (at your option) any later version. + +// Parity is distributed in the hope that it will be useful, +// but WITHOUT ANY WARRANTY; without even the implied warranty of +// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the +// GNU General Public License for more details. + +// You should have received a copy of the GNU General Public License +// along with Parity. If not, see . + +import React, { Component, PropTypes } from 'react'; +import { Card, CardHeader, CardText } from 'material-ui/Card'; +import TextField from 'material-ui/TextField'; +import DropDownMenu from 'material-ui/DropDownMenu'; +import MenuItem from 'material-ui/MenuItem'; +import RaisedButton from 'material-ui/RaisedButton'; +import CheckIcon from 'material-ui/svg-icons/navigation/check'; + +import { fromWei } from '../parity.js'; + +import styles from './names.css'; + +const useSignerText = (

Use the Signer to authenticate the following changes.

); + +const renderNames = (names) => { + const out = []; + for (let name of names) { + out.push(({ name }), ', '); + } + out.pop(); + return out; +}; + +const renderQueue = (queue) => { + if (queue.length === 0) { + return null; + } + + const grouped = queue.reduce((grouped, change) => { + const last = grouped[grouped.length - 1]; + if (last && last.action === change.action) { + last.names.push(change.name); + } else { + grouped.push({ action: change.action, names: [change.name] }); + } + return grouped; + }, []); + + return ( +
    + { grouped.map(({ action, names }) => ( +
  • + { renderNames(names) } + { ' will be ' } + { action === 'reserve' ? 'reserved' : 'dropped' } +
  • + )) } +
+ ); +}; + +export default class Names extends Component { + + static propTypes = { + actions: PropTypes.object.isRequired, + fee: PropTypes.object.isRequired, + hasAccount: PropTypes.bool.isRequired, + pending: PropTypes.bool.isRequired, + queue: PropTypes.array.isRequired + } + + state = { + action: 'reserve', + name: '' + }; + + render () { + const { action, name } = this.state; + const { fee, hasAccount, pending, queue } = this.props; + + return ( + + + + { !hasAccount + ? (

Please select an account first.

) + : (action === 'reserve' + ? (

+ The fee to reserve a name is { fromWei(fee).toFixed(3) }ETH. +

) + : (

To drop a name, you have to be the owner.

) + ) + } + + + + + + } + onClick={ this.onSubmitClick } + /> + { queue.length > 0 + ? (
{ useSignerText }{ renderQueue(queue) }
) + : null + } +
+
+ ); + } + + onNameChange = (e) => { + this.setState({ name: e.target.value }); + }; + onActionChange = (e, i, action) => { + this.setState({ action }); + }; + onSubmitClick = () => { + const { action, name } = this.state; + if (action === 'reserve') { + this.props.actions.reserve(name); + } else if (action === 'drop') { + this.props.actions.drop(name); + } + }; +} diff --git a/js/src/dapps/registry/Names/reducers.js b/js/src/dapps/registry/Names/reducers.js new file mode 100644 index 000000000..1fcee57d8 --- /dev/null +++ b/js/src/dapps/registry/Names/reducers.js @@ -0,0 +1,55 @@ +// Copyright 2015, 2016 Ethcore (UK) Ltd. +// This file is part of Parity. + +// Parity is free software: you can redistribute it and/or modify +// it under the terms of the GNU General Public License as published by +// the Free Software Foundation, either version 3 of the License, or +// (at your option) any later version. + +// Parity is distributed in the hope that it will be useful, +// but WITHOUT ANY WARRANTY; without even the implied warranty of +// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the +// GNU General Public License for more details. + +// You should have received a copy of the GNU General Public License +// along with Parity. If not, see . + +const initialState = { + hasAccount: false, + pending: false, + queue: [] +}; + +export default (state = initialState, action) => { + if (action.type === 'accounts select') { + return { ...state, hasAccount: !!action.address }; + } + + if (action.type === 'names reserve start') { + return { ...state, pending: true }; + } + if (action.type === 'names reserve success') { + return { + ...state, pending: false, + queue: state.queue.concat({ action: 'reserve', name: action.name }) + }; + } + if (action.type === 'names reserve fail') { + return { ...state, pending: false }; + } + + if (action.type === 'names drop start') { + return { ...state, pending: true }; + } + if (action.type === 'names drop success') { + return { + ...state, pending: false, + queue: state.queue.concat({ action: 'drop', name: action.name }) + }; + } + if (action.type === 'names drop fail') { + return { ...state, pending: false }; + } + + return state; +}; diff --git a/js/src/dapps/registry/Records/actions.js b/js/src/dapps/registry/Records/actions.js new file mode 100644 index 000000000..8b1407211 --- /dev/null +++ b/js/src/dapps/registry/Records/actions.js @@ -0,0 +1,38 @@ +import { sha3 } from '../parity.js'; + +export const start = (name, key, value) => ({ type: 'records update start', name, key, value }); + +export const success = () => ({ type: 'records update success' }); + +export const fail = () => ({ type: 'records update error' }); + +export const update = (name, key, value) => (dispatch, getState) => { + const state = getState(); + const account = state.accounts.selected; + const contract = state.contract; + + if (!contract || !account) { + return; + } + + const fnName = key === 'A' ? 'setAddress' : 'set'; + const fn = contract.functions.find((f) => f.name === fnName); + + name = name.toLowerCase(); + const options = { from: account.address }; + const values = [ sha3(name), key, value ]; + + dispatch(start(name, key, value)); + fn.estimateGas(options, values) + .then((gas) => { + options.gas = gas.mul(1.2).toFixed(0); + return fn.postTransaction(options, values); + }) + .then((data) => { + dispatch(success()); + }).catch((err) => { + console.error(`could not update ${key} record of ${name}`); + if (err) console.error(err.stack); + dispatch(fail()); + }); +}; diff --git a/js/src/dapps/registry/Records/index.js b/js/src/dapps/registry/Records/index.js new file mode 100644 index 000000000..e2528968e --- /dev/null +++ b/js/src/dapps/registry/Records/index.js @@ -0,0 +1 @@ +export default from './records.js'; diff --git a/js/src/dapps/registry/Records/records.css b/js/src/dapps/registry/Records/records.css new file mode 100644 index 000000000..72b62595d --- /dev/null +++ b/js/src/dapps/registry/Records/records.css @@ -0,0 +1,28 @@ +/* Copyright 2015, 2016 Ethcore (UK) Ltd. +/* This file is part of Parity. +/* +/* Parity is free software: you can redistribute it and/or modify +/* it under the terms of the GNU General Public License as published by +/* the Free Software Foundation, either version 3 of the License, or +/* (at your option) any later version. +/* +/* Parity is distributed in the hope that it will be useful, +/* but WITHOUT ANY WARRANTY; without even the implied warranty of +/* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the +/* GNU General Public License for more details. +/* +/* You should have received a copy of the GNU General Public License +/* along with Parity. If not, see . +*/ + +.records { + margin: 1em; +} + +.noSpacing { + margin-top: 0; +} + +.spacing { + margin-left: 1em; +} diff --git a/js/src/dapps/registry/Records/records.js b/js/src/dapps/registry/Records/records.js new file mode 100644 index 000000000..355522e60 --- /dev/null +++ b/js/src/dapps/registry/Records/records.js @@ -0,0 +1,76 @@ +import React, { Component, PropTypes } from 'react'; +import { Card, CardHeader, CardText } from 'material-ui/Card'; +import TextField from 'material-ui/TextField'; +import RaisedButton from 'material-ui/RaisedButton'; +import SaveIcon from 'material-ui/svg-icons/content/save'; + +import recordTypeSelect from '../ui/record-type-select.js'; +import styles from './records.css'; + +export default class Records extends Component { + + static propTypes = { + actions: PropTypes.object.isRequired, + hasAccount: PropTypes.bool.isRequired, + pending: PropTypes.bool.isRequired, + name: PropTypes.string.isRequired, + type: PropTypes.string.isRequired, + value: PropTypes.string.isRequired + } + + state = { name: '', type: 'A', value: '' }; + + render () { + const { hasAccount, pending } = this.props; + const name = this.state.name || this.props.name; + const type = this.state.type || this.props.type; + const value = this.state.value || this.props.value; + + return ( + + + + { !hasAccount + ? (

Please select an account first.

) + : (

You can only modify entries of names that you previously registered.

) + } + + { recordTypeSelect(type, this.onTypeChange, styles.spacing) } + + } + onClick={ this.onSaveClick } + /> +
+
+ ); + } + + onNameChange = (e) => { + this.setState({ name: e.target.value }); + }; + onTypeChange = (e, i, type) => { + this.setState({ type }); + }; + onValueChange = (e) => { + this.setState({ value: e.target.value }); + }; + onSaveClick = () => { + const { name, type, value } = this.state; + this.props.actions.update(name, type, value); + }; +} diff --git a/js/src/dapps/registry/Records/reducers.js b/js/src/dapps/registry/Records/reducers.js new file mode 100644 index 000000000..a9a3ae371 --- /dev/null +++ b/js/src/dapps/registry/Records/reducers.js @@ -0,0 +1,29 @@ +const initialState = { + hasAccount: false, + pending: false, + name: '', type: '', value: '' +}; + +export default (state = initialState, action) => { + if (action.type === 'accounts select') { + return { ...state, hasAccount: !!action.address }; + } + + if (action.type === 'records update start') { + return { + ...state, + pending: true, + name: action.name, type: action.entry, value: action.value + }; + } + + if (action.type === 'records update error' && action.type === 'records update success') { + return { + ...state, + pending: false, + name: initialState.name, type: initialState.type, value: initialState.value + }; + } + + return state; +}; diff --git a/js/src/dapps/registry/actions.js b/js/src/dapps/registry/actions.js new file mode 100644 index 000000000..882af0360 --- /dev/null +++ b/js/src/dapps/registry/actions.js @@ -0,0 +1,68 @@ +// Copyright 2015, 2016 Ethcore (UK) Ltd. +// This file is part of Parity. + +// Parity is free software: you can redistribute it and/or modify +// it under the terms of the GNU General Public License as published by +// the Free Software Foundation, either version 3 of the License, or +// (at your option) any later version. + +// Parity is distributed in the hope that it will be useful, +// but WITHOUT ANY WARRANTY; without even the implied warranty of +// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the +// GNU General Public License for more details. + +// You should have received a copy of the GNU General Public License +// along with Parity. If not, see . + +import { registry as registryAbi } from '../../contracts/abi'; + +import { api } from './parity.js'; +import * as addresses from './addresses/actions.js'; +import * as accounts from './Accounts/actions.js'; +import * as lookup from './Lookup/actions.js'; +import * as events from './Events/actions.js'; +import * as names from './Names/actions.js'; +import * as records from './Records/actions.js'; + +export { addresses, accounts, lookup, events, names, records }; + +export const setContract = (contract) => ({ type: 'set contract', contract }); + +export const fetchContract = () => (dispatch) => + api.ethcore.registryAddress() + .then((address) => { + const contract = api.newContract(registryAbi, address); + dispatch(setContract(contract)); + dispatch(fetchFee()); + dispatch(fetchOwner()); + }) + .catch((err) => { + console.error('could not fetch contract'); + if (err) console.error(err.stack); + }); + +export const setFee = (fee) => ({ type: 'set fee', fee }); + +const fetchFee = () => (dispatch, getState) => { + const { contract } = getState(); + if (!contract) return; + contract.instance.fee.call() + .then((fee) => dispatch(setFee(fee))) + .catch((err) => { + console.error('could not fetch fee'); + if (err) console.error(err.stack); + }); +}; + +export const setOwner = (owner) => ({ type: 'set owner', owner }); + +export const fetchOwner = () => (dispatch, getState) => { + const { contract } = getState(); + if (!contract) return; + contract.instance.owner.call() + .then((owner) => dispatch(setOwner(owner))) + .catch((err) => { + console.error('could not fetch owner'); + if (err) console.error(err.stack); + }); +}; diff --git a/js/src/dapps/registry/addresses/accounts-reducer.js b/js/src/dapps/registry/addresses/accounts-reducer.js new file mode 100644 index 000000000..20981877d --- /dev/null +++ b/js/src/dapps/registry/addresses/accounts-reducer.js @@ -0,0 +1,38 @@ +// Copyright 2015, 2016 Ethcore (UK) Ltd. +// This file is part of Parity. + +// Parity is free software: you can redistribute it and/or modify +// it under the terms of the GNU General Public License as published by +// the Free Software Foundation, either version 3 of the License, or +// (at your option) any later version. + +// Parity is distributed in the hope that it will be useful, +// but WITHOUT ANY WARRANTY; without even the implied warranty of +// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the +// GNU General Public License for more details. + +// You should have received a copy of the GNU General Public License +// along with Parity. If not, see . + +const initialState = { + all: {}, + selected: null +}; + +export default (state = initialState, action) => { + if (action.type === 'addresses set') { + const accounts = action.addresses + .filter((address) => address.isAccount) + .reduce((accounts, account) => { + accounts[account.address] = account; + return accounts; + }, {}); + return { ...state, all: accounts }; + } + + if (action.type === 'accounts select' && state.all[action.address]) { + return { ...state, selected: state.all[action.address] }; + } + + return state; +}; diff --git a/js/src/dapps/registry/addresses/actions.js b/js/src/dapps/registry/addresses/actions.js new file mode 100644 index 000000000..b6091acb5 --- /dev/null +++ b/js/src/dapps/registry/addresses/actions.js @@ -0,0 +1,39 @@ +// Copyright 2015, 2016 Ethcore (UK) Ltd. +// This file is part of Parity. + +// Parity is free software: you can redistribute it and/or modify +// it under the terms of the GNU General Public License as published by +// the Free Software Foundation, either version 3 of the License, or +// (at your option) any later version. + +// Parity is distributed in the hope that it will be useful, +// but WITHOUT ANY WARRANTY; without even the implied warranty of +// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the +// GNU General Public License for more details. + +// You should have received a copy of the GNU General Public License +// along with Parity. If not, see . + +import { api } from '../parity'; + +export const set = (addresses) => ({ type: 'addresses set', addresses }); + +export const fetch = () => (dispatch) => { + return Promise + .all([ + api.personal.listAccounts(), + api.personal.accountsInfo() + ]) + .then(([ accounts, data ]) => { + const addresses = Object.keys(data) + .filter((address) => data[address] && !data[address].meta.deleted) + .map((address) => ({ + ...data[address], address, + isAccount: accounts.includes(address) + })); + dispatch(set(addresses)); + }) + .catch((error) => { + console.error('could not fetch addresses', error); + }); +}; diff --git a/js/src/dapps/registry/addresses/contacts-reducer.js b/js/src/dapps/registry/addresses/contacts-reducer.js new file mode 100644 index 000000000..6b0572e54 --- /dev/null +++ b/js/src/dapps/registry/addresses/contacts-reducer.js @@ -0,0 +1,31 @@ +// Copyright 2015, 2016 Ethcore (UK) Ltd. +// This file is part of Parity. + +// Parity is free software: you can redistribute it and/or modify +// it under the terms of the GNU General Public License as published by +// the Free Software Foundation, either version 3 of the License, or +// (at your option) any later version. + +// Parity is distributed in the hope that it will be useful, +// but WITHOUT ANY WARRANTY; without even the implied warranty of +// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the +// GNU General Public License for more details. + +// You should have received a copy of the GNU General Public License +// along with Parity. If not, see . + +const initialState = {}; + +export default (state = initialState, action) => { + if (action.type === 'addresses set') { + const contacts = action.addresses + .filter((address) => !address.isAccount) + .reduce((contacts, contact) => { + contacts[contact.address] = contact; + return contacts; + }, {}); + return contacts; + } + + return state; +}; diff --git a/js/src/dapps/registry/parity.js b/js/src/dapps/registry/parity.js new file mode 100644 index 000000000..cf9819b60 --- /dev/null +++ b/js/src/dapps/registry/parity.js @@ -0,0 +1,23 @@ +// Copyright 2015, 2016 Ethcore (UK) Ltd. +// This file is part of Parity. + +// Parity is free software: you can redistribute it and/or modify +// it under the terms of the GNU General Public License as published by +// the Free Software Foundation, either version 3 of the License, or +// (at your option) any later version. + +// Parity is distributed in the hope that it will be useful, +// but WITHOUT ANY WARRANTY; without even the implied warranty of +// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the +// GNU General Public License for more details. + +// You should have received a copy of the GNU General Public License +// along with Parity. If not, see . + +const api = window.parity.api; +const { bytesToHex, sha3, toWei, fromWei } = window.parity.api.util; + +export { + api, + bytesToHex, sha3, toWei, fromWei +}; diff --git a/js/src/dapps/registry/reducers.js b/js/src/dapps/registry/reducers.js new file mode 100644 index 000000000..e0f620b70 --- /dev/null +++ b/js/src/dapps/registry/reducers.js @@ -0,0 +1,55 @@ +// Copyright 2015, 2016 Ethcore (UK) Ltd. +// This file is part of Parity. + +// Parity is free software: you can redistribute it and/or modify +// it under the terms of the GNU General Public License as published by +// the Free Software Foundation, either version 3 of the License, or +// (at your option) any later version. + +// Parity is distributed in the hope that it will be useful, +// but WITHOUT ANY WARRANTY; without even the implied warranty of +// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the +// GNU General Public License for more details. + +// You should have received a copy of the GNU General Public License +// along with Parity. If not, see . + +import accountsReducer from './addresses/accounts-reducer.js'; +import contactsReducer from './addresses/contacts-reducer.js'; +import lookupReducer from './Lookup/reducers.js'; +import eventsReducer from './Events/reducers.js'; +import namesReducer from './Names/reducers.js'; +import recordsReducer from './Records/reducers.js'; + +const contractReducer = (state = null, action) => + action.type === 'set contract' ? action.contract : state; + +const feeReducer = (state = null, action) => + action.type === 'set fee' ? action.fee : state; + +const ownerReducer = (state = null, action) => + action.type === 'set owner' ? action.owner : state; + +const initialState = { + accounts: accountsReducer(undefined, { type: '' }), + contacts: contactsReducer(undefined, { type: '' }), + contract: contractReducer(undefined, { type: '' }), + fee: feeReducer(undefined, { type: '' }), + owner: ownerReducer(undefined, { type: '' }), + lookup: lookupReducer(undefined, { type: '' }), + events: eventsReducer(undefined, { type: '' }), + names: namesReducer(undefined, { type: '' }), + records: recordsReducer(undefined, { type: '' }) +}; + +export default (state = initialState, action) => ({ + accounts: accountsReducer(state.accounts, action), + contacts: contactsReducer(state.contacts, action), + contract: contractReducer(state.contract, action), + fee: feeReducer(state.fee, action), + owner: ownerReducer(state.owner, action), + lookup: lookupReducer(state.lookup, action), + events: eventsReducer(state.events, action), + names: namesReducer(state.names, action), + records: recordsReducer(state.records, action) +}); diff --git a/js/src/dapps/registry/store.js b/js/src/dapps/registry/store.js new file mode 100644 index 000000000..5fb7d4b6a --- /dev/null +++ b/js/src/dapps/registry/store.js @@ -0,0 +1,22 @@ +// Copyright 2015, 2016 Ethcore (UK) Ltd. +// This file is part of Parity. + +// Parity is free software: you can redistribute it and/or modify +// it under the terms of the GNU General Public License as published by +// the Free Software Foundation, either version 3 of the License, or +// (at your option) any later version. + +// Parity is distributed in the hope that it will be useful, +// but WITHOUT ANY WARRANTY; without even the implied warranty of +// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the +// GNU General Public License for more details. + +// You should have received a copy of the GNU General Public License +// along with Parity. If not, see . + +import { createStore, applyMiddleware } from 'redux'; +import thunk from 'redux-thunk'; + +import reducer from './reducers'; + +export default createStore(reducer, applyMiddleware(thunk)); diff --git a/js/src/dapps/registry/ui/address.js b/js/src/dapps/registry/ui/address.js new file mode 100644 index 000000000..e4a5a953e --- /dev/null +++ b/js/src/dapps/registry/ui/address.js @@ -0,0 +1,47 @@ +// Copyright 2015, 2016 Ethcore (UK) Ltd. +// This file is part of Parity. + +// Parity is free software: you can redistribute it and/or modify +// it under the terms of the GNU General Public License as published by +// the Free Software Foundation, either version 3 of the License, or +// (at your option) any later version. + +// Parity is distributed in the hope that it will be useful, +// but WITHOUT ANY WARRANTY; without even the implied warranty of +// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the +// GNU General Public License for more details. + +// You should have received a copy of the GNU General Public License +// along with Parity. If not, see . + +import React from 'react'; +import renderHash from './hash'; +import IdentityIcon from '../IdentityIcon'; + +const container = { + display: 'inline-block', + verticalAlign: 'middle', + height: '24px' +}; +const align = { + display: 'inline-block', + verticalAlign: 'top', + lineHeight: '24px' +}; + +export default (address, accounts, contacts, shortenHash = true) => { + let caption; + if (accounts[address]) { + caption = ({ accounts[address].name }); + } else if (contacts[address]) { + caption = ({ contacts[address].name }); + } else { + caption = ({ shortenHash ? renderHash(address) : address }); + } + return ( +
+ + { caption } +
+ ); +}; diff --git a/js/src/dapps/registry/ui/hash.js b/js/src/dapps/registry/ui/hash.js new file mode 100644 index 000000000..4035a9bbe --- /dev/null +++ b/js/src/dapps/registry/ui/hash.js @@ -0,0 +1,24 @@ +// Copyright 2015, 2016 Ethcore (UK) Ltd. +// This file is part of Parity. + +// Parity is free software: you can redistribute it and/or modify +// it under the terms of the GNU General Public License as published by +// the Free Software Foundation, either version 3 of the License, or +// (at your option) any later version. + +// Parity is distributed in the hope that it will be useful, +// but WITHOUT ANY WARRANTY; without even the implied warranty of +// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the +// GNU General Public License for more details. + +// You should have received a copy of the GNU General Public License +// along with Parity. If not, see . + +import React from 'react'; + +export default (hash) => { + const shortened = hash.length > (2 + 9 + 9) + ? hash.substr(2, 9) + '...' + hash.slice(-9) + : hash.slice(2); + return ({ shortened }); +}; diff --git a/js/src/dapps/registry/ui/image.js b/js/src/dapps/registry/ui/image.js new file mode 100644 index 000000000..7e7a52a88 --- /dev/null +++ b/js/src/dapps/registry/ui/image.js @@ -0,0 +1,30 @@ +// Copyright 2015, 2016 Ethcore (UK) Ltd. +// This file is part of Parity. + +// Parity is free software: you can redistribute it and/or modify +// it under the terms of the GNU General Public License as published by +// the Free Software Foundation, either version 3 of the License, or +// (at your option) any later version. + +// Parity is distributed in the hope that it will be useful, +// but WITHOUT ANY WARRANTY; without even the implied warranty of +// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the +// GNU General Public License for more details. + +// You should have received a copy of the GNU General Public License +// along with Parity. If not, see . + +import React from 'react'; + +const styles = { + padding: '.5em', + border: '1px solid #777' +}; + +export default (address) => ( + { +); diff --git a/js/src/dapps/registry/ui/record-type-select.js b/js/src/dapps/registry/ui/record-type-select.js new file mode 100644 index 000000000..2cfbdf540 --- /dev/null +++ b/js/src/dapps/registry/ui/record-type-select.js @@ -0,0 +1,27 @@ +// Copyright 2015, 2016 Ethcore (UK) Ltd. +// This file is part of Parity. + +// Parity is free software: you can redistribute it and/or modify +// it under the terms of the GNU General Public License as published by +// the Free Software Foundation, either version 3 of the License, or +// (at your option) any later version. + +// Parity is distributed in the hope that it will be useful, +// but WITHOUT ANY WARRANTY; without even the implied warranty of +// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the +// GNU General Public License for more details. + +// You should have received a copy of the GNU General Public License +// along with Parity. If not, see . + +import React from 'react'; +import DropDownMenu from 'material-ui/DropDownMenu'; +import MenuItem from 'material-ui/MenuItem'; + +export default (value, onSelect, className = '') => ( + + + + + +); diff --git a/js/src/dapps/signaturereg.html b/js/src/dapps/signaturereg.html new file mode 100644 index 000000000..3f74be28a --- /dev/null +++ b/js/src/dapps/signaturereg.html @@ -0,0 +1,16 @@ + + + + + + + Method Signature Registry + + +
+ + + + + + diff --git a/js/src/dapps/signaturereg.js b/js/src/dapps/signaturereg.js new file mode 100644 index 000000000..72ddd0ca7 --- /dev/null +++ b/js/src/dapps/signaturereg.js @@ -0,0 +1,33 @@ +// Copyright 2015, 2016 Ethcore (UK) Ltd. +// This file is part of Parity. + +// Parity is free software: you can redistribute it and/or modify +// it under the terms of the GNU General Public License as published by +// the Free Software Foundation, either version 3 of the License, or +// (at your option) any later version. + +// Parity is distributed in the hope that it will be useful, +// but WITHOUT ANY WARRANTY; without even the implied warranty of +// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the +// GNU General Public License for more details. + +// You should have received a copy of the GNU General Public License +// along with Parity. If not, see . + +import ReactDOM from 'react-dom'; +import React from 'react'; + +import injectTapEventPlugin from 'react-tap-event-plugin'; +injectTapEventPlugin(); + +import Application from './signaturereg/Application'; + +import '../../assets/fonts/Roboto/font.css'; +import '../../assets/fonts/RobotoMono/font.css'; +import './style.css'; +import './signaturereg.html'; + +ReactDOM.render( + , + document.querySelector('#container') +); diff --git a/js/src/dapps/signaturereg/Application/application.css b/js/src/dapps/signaturereg/Application/application.css new file mode 100644 index 000000000..4675b064e --- /dev/null +++ b/js/src/dapps/signaturereg/Application/application.css @@ -0,0 +1,29 @@ +/* Copyright 2015, 2016 Ethcore (UK) Ltd. +/* This file is part of Parity. +/* +/* Parity is free software: you can redistribute it and/or modify +/* it under the terms of the GNU General Public License as published by +/* the Free Software Foundation, either version 3 of the License, or +/* (at your option) any later version. +/* +/* Parity is distributed in the hope that it will be useful, +/* but WITHOUT ANY WARRANTY; without even the implied warranty of +/* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the +/* GNU General Public License for more details. +/* +/* You should have received a copy of the GNU General Public License +/* along with Parity. If not, see . +*/ + +.container { + background: black; + color: #eee; + font-family: 'Roboto'; + vertical-align: middle; +} + +.actions { + position: absolute; + top: 1.5em; + right: 1.5em; +} diff --git a/js/src/dapps/signaturereg/Application/application.js b/js/src/dapps/signaturereg/Application/application.js new file mode 100644 index 000000000..3878af4cf --- /dev/null +++ b/js/src/dapps/signaturereg/Application/application.js @@ -0,0 +1,130 @@ +// Copyright 2015, 2016 Ethcore (UK) Ltd. +// This file is part of Parity. + +// Parity is free software: you can redistribute it and/or modify +// it under the terms of the GNU General Public License as published by +// the Free Software Foundation, either version 3 of the License, or +// (at your option) any later version. + +// Parity is distributed in the hope that it will be useful, +// but WITHOUT ANY WARRANTY; without even the implied warranty of +// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the +// GNU General Public License for more details. + +// You should have received a copy of the GNU General Public License +// along with Parity. If not, see . + +import BigNumber from 'bignumber.js'; +import React, { Component } from 'react'; + +import { attachInterface, attachBlockNumber } from '../services'; +import Button from '../Button'; +import Events from '../Events'; +import Header from '../Header'; +import Import from '../Import'; +import Loading from '../Loading'; + +import styles from './application.css'; + +export default class Application extends Component { + state = { + accounts: {}, + address: null, + fromAddress: null, + accountsInfo: {}, + blockNumber: new BigNumber(0), + contract: null, + instance: null, + loading: true, + totalSignatures: new BigNumber(0), + showImport: false + } + + componentDidMount () { + attachInterface() + .then((state) => { + this.setState(state, () => { + this.setState({ loading: false }); + }); + + return attachBlockNumber(state.instance, (state) => { + this.setState(state); + }); + }) + .catch((error) => { + console.error('componentDidMount', error); + }); + } + + render () { + const { loading } = this.state; + + if (loading) { + return ( + + ); + } + + return ( +
+ { this.renderHeader() } + { this.renderImport() } + { this.renderEvents() } +
+ ); + } + + renderHeader () { + const { blockNumber, totalSignatures } = this.state; + + return ( +
+ ); + } + + renderImport () { + const { accounts, fromAddress, instance, showImport } = this.state; + + if (showImport) { + return ( + + ); + } + + return ( +
+ +
+ ); + } + + renderEvents () { + const { accountsInfo, contract } = this.state; + + return ( + + ); + } + + toggleImport = () => { + this.setState({ + showImport: !this.state.showImport + }); + } + + setFromAddress = (fromAddress) => { + this.setState({ + fromAddress + }); + } +} diff --git a/js/src/dapps/signaturereg/Application/index.js b/js/src/dapps/signaturereg/Application/index.js new file mode 100644 index 000000000..236578226 --- /dev/null +++ b/js/src/dapps/signaturereg/Application/index.js @@ -0,0 +1,17 @@ +// Copyright 2015, 2016 Ethcore (UK) Ltd. +// This file is part of Parity. + +// Parity is free software: you can redistribute it and/or modify +// it under the terms of the GNU General Public License as published by +// the Free Software Foundation, either version 3 of the License, or +// (at your option) any later version. + +// Parity is distributed in the hope that it will be useful, +// but WITHOUT ANY WARRANTY; without even the implied warranty of +// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the +// GNU General Public License for more details. + +// You should have received a copy of the GNU General Public License +// along with Parity. If not, see . + +export default from './application'; diff --git a/js/src/dapps/signaturereg/Button/button.css b/js/src/dapps/signaturereg/Button/button.css new file mode 100644 index 000000000..444359c79 --- /dev/null +++ b/js/src/dapps/signaturereg/Button/button.css @@ -0,0 +1,43 @@ +/* Copyright 2015, 2016 Ethcore (UK) Ltd. +/* This file is part of Parity. +/* +/* Parity is free software: you can redistribute it and/or modify +/* it under the terms of the GNU General Public License as published by +/* the Free Software Foundation, either version 3 of the License, or +/* (at your option) any later version. +/* +/* Parity is distributed in the hope that it will be useful, +/* but WITHOUT ANY WARRANTY; without even the implied warranty of +/* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the +/* GNU General Public License for more details. +/* +/* You should have received a copy of the GNU General Public License +/* along with Parity. If not, see . +*/ + +.button { + background: #f80; + color: white; + border-radius: 5px; + font-size: 1em; + line-height: 24px; + height: 24px; + padding: 0.75em 1.5em; + cursor: pointer; + display: inline-block; +} + +.button.disabled { + opacity: 0.25; + cursor: default; +} + +.button.inverse { + color: #f80; + background: white; +} + +.button * { + display: inline-block; + vertical-align: top; +} diff --git a/js/src/dapps/signaturereg/Button/button.js b/js/src/dapps/signaturereg/Button/button.js new file mode 100644 index 000000000..9cbbcf478 --- /dev/null +++ b/js/src/dapps/signaturereg/Button/button.js @@ -0,0 +1,50 @@ +// Copyright 2015, 2016 Ethcore (UK) Ltd. +// This file is part of Parity. + +// Parity is free software: you can redistribute it and/or modify +// it under the terms of the GNU General Public License as published by +// the Free Software Foundation, either version 3 of the License, or +// (at your option) any later version. + +// Parity is distributed in the hope that it will be useful, +// but WITHOUT ANY WARRANTY; without even the implied warranty of +// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the +// GNU General Public License for more details. + +// You should have received a copy of the GNU General Public License +// along with Parity. If not, see . + +import React, { Component, PropTypes } from 'react'; + +import styles from './button.css'; + +export default class Button extends Component { + static propTypes = { + children: PropTypes.node.isRequired, + className: PropTypes.string, + disabled: PropTypes.bool, + invert: PropTypes.bool, + onClick: PropTypes.func.isRequired + } + + render () { + const { children, className, disabled, invert } = this.props; + const classes = `${styles.button} ${disabled ? styles.disabled : ''} ${invert ? styles.inverse : ''} ${className}`; + + return ( +
+ { children } +
+ ); + } + + onClick = (event) => { + const { disabled, onClick } = this.props; + + if (disabled) { + return; + } + + onClick(event); + } +} diff --git a/js/src/dapps/signaturereg/Button/index.js b/js/src/dapps/signaturereg/Button/index.js new file mode 100644 index 000000000..f69a65e3d --- /dev/null +++ b/js/src/dapps/signaturereg/Button/index.js @@ -0,0 +1,17 @@ +// Copyright 2015, 2016 Ethcore (UK) Ltd. +// This file is part of Parity. + +// Parity is free software: you can redistribute it and/or modify +// it under the terms of the GNU General Public License as published by +// the Free Software Foundation, either version 3 of the License, or +// (at your option) any later version. + +// Parity is distributed in the hope that it will be useful, +// but WITHOUT ANY WARRANTY; without even the implied warranty of +// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the +// GNU General Public License for more details. + +// You should have received a copy of the GNU General Public License +// along with Parity. If not, see . + +export default from './button'; diff --git a/js/src/dapps/signaturereg/Events/events.css b/js/src/dapps/signaturereg/Events/events.css new file mode 100644 index 000000000..5e9960e0e --- /dev/null +++ b/js/src/dapps/signaturereg/Events/events.css @@ -0,0 +1,76 @@ +/* Copyright 2015, 2016 Ethcore (UK) Ltd. +/* This file is part of Parity. +/* +/* Parity is free software: you can redistribute it and/or modify +/* it under the terms of the GNU General Public License as published by +/* the Free Software Foundation, either version 3 of the License, or +/* (at your option) any later version. +/* +/* Parity is distributed in the hope that it will be useful, +/* but WITHOUT ANY WARRANTY; without even the implied warranty of +/* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the +/* GNU General Public License for more details. +/* +/* You should have received a copy of the GNU General Public License +/* along with Parity. If not, see . +*/ + +.events { + padding: 3em; + text-align: center; +} + +.header { + font-size: 1.3em; + line-height: 1.3em; + vertical-align: middle; + text-align: center; + padding-bottom: 24px; + color: #f80; +} + +.events table { + border: none; + margin: 0 auto; + border-collapse: collapse; +} + +.events td { + padding: 0 0.5em 0.5em 0.5em; + white-space: nowrap; + text-align: left; + line-height: 24px; +} + +.events td * { + display: inline-block; + vertical-align: top; +} + +.pending { + opacity: 0.5; +} + +.mined { +} + +td.methodName { + color: #f80; +} + +td.signature { + color: #888; + text-align: right; +} + +td.timestamp { + text-align: right; +} + +td.blockNumber { + text-align: center; +} + +td.owner { + text-overflow: ellipsis; +} diff --git a/js/src/dapps/signaturereg/Events/events.js b/js/src/dapps/signaturereg/Events/events.js new file mode 100644 index 000000000..302b51250 --- /dev/null +++ b/js/src/dapps/signaturereg/Events/events.js @@ -0,0 +1,84 @@ +// Copyright 2015, 2016 Ethcore (UK) Ltd. +// This file is part of Parity. + +// Parity is free software: you can redistribute it and/or modify +// it under the terms of the GNU General Public License as published by +// the Free Software Foundation, either version 3 of the License, or +// (at your option) any later version. + +// Parity is distributed in the hope that it will be useful, +// but WITHOUT ANY WARRANTY; without even the implied warranty of +// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the +// GNU General Public License for more details. + +// You should have received a copy of the GNU General Public License +// along with Parity. If not, see . + +import React, { Component, PropTypes } from 'react'; + +import { formatBlockNumber, formatBlockTimestamp, formatSignature } from '../format'; +import { attachEvents } from '../services'; +import IdentityIcon from '../IdentityIcon'; + +import styles from './events.css'; + +export default class Events extends Component { + static propTypes = { + accountsInfo: PropTypes.object.isRequired, + contract: PropTypes.object.isRequired + } + + state = { + events: [] + } + + componentDidMount () { + const { contract } = this.props; + + attachEvents(contract, (state) => { + this.setState(state); + }); + } + + render () { + const { events } = this.state; + + if (!events.length) { + return null; + } + + return ( +
+ + + { this.renderEvents() } + +
+
+ ); + } + + renderEvents () { + const { accountsInfo } = this.props; + const { events } = this.state; + + return events.map((event) => { + const name = accountsInfo[event.params.creator] + ? accountsInfo[event.params.creator].name + : event.params.creator; + + return ( + + { formatBlockTimestamp(event.block) } + { formatBlockNumber(event.blockNumber) } + + +
{ name }
+ + { formatSignature(event.params.signature) } + { event.params.method } + + ); + }); + } +} diff --git a/js/src/dapps/signaturereg/Events/index.js b/js/src/dapps/signaturereg/Events/index.js new file mode 100644 index 000000000..88ad6d407 --- /dev/null +++ b/js/src/dapps/signaturereg/Events/index.js @@ -0,0 +1,17 @@ +// Copyright 2015, 2016 Ethcore (UK) Ltd. +// This file is part of Parity. + +// Parity is free software: you can redistribute it and/or modify +// it under the terms of the GNU General Public License as published by +// the Free Software Foundation, either version 3 of the License, or +// (at your option) any later version. + +// Parity is distributed in the hope that it will be useful, +// but WITHOUT ANY WARRANTY; without even the implied warranty of +// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the +// GNU General Public License for more details. + +// You should have received a copy of the GNU General Public License +// along with Parity. If not, see . + +export default from './events'; diff --git a/js/src/dapps/signaturereg/Header/header.css b/js/src/dapps/signaturereg/Header/header.css new file mode 100644 index 000000000..8d95b959c --- /dev/null +++ b/js/src/dapps/signaturereg/Header/header.css @@ -0,0 +1,67 @@ +/* Copyright 2015, 2016 Ethcore (UK) Ltd. +/* This file is part of Parity. +/* +/* Parity is free software: you can redistribute it and/or modify +/* it under the terms of the GNU General Public License as published by +/* the Free Software Foundation, either version 3 of the License, or +/* (at your option) any later version. +/* +/* Parity is distributed in the hope that it will be useful, +/* but WITHOUT ANY WARRANTY; without even the implied warranty of +/* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the +/* GNU General Public License for more details. +/* +/* You should have received a copy of the GNU General Public License +/* along with Parity. If not, see . +*/ + +.header { + position: relative; + height: 13.69em; + color: white; + border-bottom: 4px solid white; + overflow: hidden; +} + +.banner { + position: absolute; + top: 0; + left: 0; + right: 0; + bottom: 0; + font-size: 1.3em; + line-height: 1.3em; + padding: 24px; + background: #f80; +} + +.header img, +.content { + position: absolute; + top: 0; + left: 0; + right: 0; + bottom: 0; +} + +.header img { + opacity: 0.2; + width: 100%; +} + +.content { + text-align: center; + padding: 3em; +} + +.hero { + font-size: 5em; + line-height: 1.2em; + vertical-align: middle; +} + +.byline { + font-size: 1.3em; + line-height: 1.3em; + vertical-align: middle; +} diff --git a/js/src/dapps/signaturereg/Header/header.js b/js/src/dapps/signaturereg/Header/header.js new file mode 100644 index 000000000..8dd7cd578 --- /dev/null +++ b/js/src/dapps/signaturereg/Header/header.js @@ -0,0 +1,48 @@ +// Copyright 2015, 2016 Ethcore (UK) Ltd. +// This file is part of Parity. + +// Parity is free software: you can redistribute it and/or modify +// it under the terms of the GNU General Public License as published by +// the Free Software Foundation, either version 3 of the License, or +// (at your option) any later version. + +// Parity is distributed in the hope that it will be useful, +// but WITHOUT ANY WARRANTY; without even the implied warranty of +// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the +// GNU General Public License for more details. + +// You should have received a copy of the GNU General Public License +// along with Parity. If not, see . + +import React, { Component, PropTypes } from 'react'; + +import styles from './header.css'; +import blocks from '../../../../assets/images/dapps/blocks-350.jpg'; + +export default class Header extends Component { + static propTypes = { + blockNumber: PropTypes.object.isRequired, + totalSignatures: PropTypes.object.isRequired + } + + render () { + const { totalSignatures } = this.props; + + return ( +
+
+ contract signature registry +
+ +
+
+ { totalSignatures.toFormat(0) } +
+
+ signatures registered +
+
+
+ ); + } +} diff --git a/js/src/dapps/signaturereg/Header/index.js b/js/src/dapps/signaturereg/Header/index.js new file mode 100644 index 000000000..37b5835f0 --- /dev/null +++ b/js/src/dapps/signaturereg/Header/index.js @@ -0,0 +1,17 @@ +// Copyright 2015, 2016 Ethcore (UK) Ltd. +// This file is part of Parity. + +// Parity is free software: you can redistribute it and/or modify +// it under the terms of the GNU General Public License as published by +// the Free Software Foundation, either version 3 of the License, or +// (at your option) any later version. + +// Parity is distributed in the hope that it will be useful, +// but WITHOUT ANY WARRANTY; without even the implied warranty of +// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the +// GNU General Public License for more details. + +// You should have received a copy of the GNU General Public License +// along with Parity. If not, see . + +export default from './header.js'; diff --git a/js/src/dapps/signaturereg/IdentityIcon/identityIcon.css b/js/src/dapps/signaturereg/IdentityIcon/identityIcon.css new file mode 100644 index 000000000..3fa1df99e --- /dev/null +++ b/js/src/dapps/signaturereg/IdentityIcon/identityIcon.css @@ -0,0 +1,23 @@ +/* Copyright 2015, 2016 Ethcore (UK) Ltd. +/* This file is part of Parity. +/* +/* Parity is free software: you can redistribute it and/or modify +/* it under the terms of the GNU General Public License as published by +/* the Free Software Foundation, either version 3 of the License, or +/* (at your option) any later version. +/* +/* Parity is distributed in the hope that it will be useful, +/* but WITHOUT ANY WARRANTY; without even the implied warranty of +/* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the +/* GNU General Public License for more details. +/* +/* You should have received a copy of the GNU General Public License +/* along with Parity. If not, see . +*/ + +.icon { + width: 24px; + height: 24px; + border-radius: 50%; + margin-right: 0.5em; +} diff --git a/js/src/dapps/signaturereg/IdentityIcon/identityIcon.js b/js/src/dapps/signaturereg/IdentityIcon/identityIcon.js new file mode 100644 index 000000000..0bc86731d --- /dev/null +++ b/js/src/dapps/signaturereg/IdentityIcon/identityIcon.js @@ -0,0 +1,36 @@ +// Copyright 2015, 2016 Ethcore (UK) Ltd. +// This file is part of Parity. + +// Parity is free software: you can redistribute it and/or modify +// it under the terms of the GNU General Public License as published by +// the Free Software Foundation, either version 3 of the License, or +// (at your option) any later version. + +// Parity is distributed in the hope that it will be useful, +// but WITHOUT ANY WARRANTY; without even the implied warranty of +// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the +// GNU General Public License for more details. + +// You should have received a copy of the GNU General Public License +// along with Parity. If not, see . + +import React, { Component, PropTypes } from 'react'; + +import { api } from '../parity'; +import styles from './identityIcon.css'; + +export default class IdentityIcon extends Component { + static propTypes = { + address: PropTypes.string.isRequired + } + + render () { + const { address } = this.props; + + return ( + + ); + } +} diff --git a/js/src/dapps/signaturereg/IdentityIcon/index.js b/js/src/dapps/signaturereg/IdentityIcon/index.js new file mode 100644 index 000000000..76c107bfb --- /dev/null +++ b/js/src/dapps/signaturereg/IdentityIcon/index.js @@ -0,0 +1,17 @@ +// Copyright 2015, 2016 Ethcore (UK) Ltd. +// This file is part of Parity. + +// Parity is free software: you can redistribute it and/or modify +// it under the terms of the GNU General Public License as published by +// the Free Software Foundation, either version 3 of the License, or +// (at your option) any later version. + +// Parity is distributed in the hope that it will be useful, +// but WITHOUT ANY WARRANTY; without even the implied warranty of +// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the +// GNU General Public License for more details. + +// You should have received a copy of the GNU General Public License +// along with Parity. If not, see . + +export default from './identityIcon'; diff --git a/js/src/dapps/signaturereg/Import/import.css b/js/src/dapps/signaturereg/Import/import.css new file mode 100644 index 000000000..8f9788c34 --- /dev/null +++ b/js/src/dapps/signaturereg/Import/import.css @@ -0,0 +1,151 @@ +/* Copyright 2015, 2016 Ethcore (UK) Ltd. +/* This file is part of Parity. +/* +/* Parity is free software: you can redistribute it and/or modify +/* it under the terms of the GNU General Public License as published by +/* the Free Software Foundation, either version 3 of the License, or +/* (at your option) any later version. +/* +/* Parity is distributed in the hope that it will be useful, +/* but WITHOUT ANY WARRANTY; without even the implied warranty of +/* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the +/* GNU General Public License for more details. +/* +/* You should have received a copy of the GNU General Public License +/* along with Parity. If not, see . +*/ + +.modal { + position: fixed; + top: 0; + right: 0; + bottom: 0; + left: 0; +} + +.overlay { + position: absolute; + top: 0; + right: 0; + bottom: 0; + left: 0; + background: rgba(55, 55, 55, 0.75); + text-align: center; +} + +.dialog { + margin-top: 1.5em; + border-radius: 5px; + width: 750px; + background: rgba(0, 0, 0, 0.9); + display: inline-block; +} + +.close { + top: 4px; + right: 4px; + position: absolute; + background: transparent; +} + +.header { + font-size: 1.3em; + line-height: 1.3em; + padding: 16px 24px; + background: #f80; + color: white; + position: relative; + margin-bottom: 24px; + border-radius: 5px 5px 0 0; +} + +.body { + padding: 0 24px; +} + +.body div { + text-align: center; +} + +.info { + padding: 0 24px 24px 24px; + line-height: 1.618em; +} + +.info textarea { + background: rgba(80, 80, 80, 0.25); + border-radius: 5px; + resize: none; + padding: 1em; + color: #eee; + font-size: 0.75em; + font-family: 'Roboto Mono'; + width: 100%; + border: none; + box-sizing: border-box; +} + +.info textarea.error { + background: rgba(255, 0, 0, 0.25); +} + +.info .error { + color: rgba(255, 0, 0, 1); + font-size: 0.75em; + line-height: 1.5em; +} + +.buttonrow { + position: relative; + padding: 24px 0; + margin: 0; + text-align: right !important; +} + +.addressSelect { + position: absolute; + top: 24px; + left: 0; +} + +.keys { + position: absolute; + left: 24px; + top: 16px; + padding: 4px 0; +} + +.fnconstant, +.fnexists, +.fnunknown, +.fntodo { + display: inline-block; + margin: 0.25em; + padding: 0.5em 1em; + border-radius: 2px; + color: white; + line-height: 1em; + font-size: 0.8em; +} + +.fnconstant { + color: #888; + background: #333; +} + +.fnexists { + color: #f80; + background: #333; +} + +.fnunknown { + color: #eee; +} + +.fntodo { + background: #f80; +} + +.hide { + opacity: 0; +} diff --git a/js/src/dapps/signaturereg/Import/import.js b/js/src/dapps/signaturereg/Import/import.js new file mode 100644 index 000000000..dcf2b3f98 --- /dev/null +++ b/js/src/dapps/signaturereg/Import/import.js @@ -0,0 +1,236 @@ +// Copyright 2015, 2016 Ethcore (UK) Ltd. +// This file is part of Parity. + +// Parity is free software: you can redistribute it and/or modify +// it under the terms of the GNU General Public License as published by +// the Free Software Foundation, either version 3 of the License, or +// (at your option) any later version. + +// Parity is distributed in the hope that it will be useful, +// but WITHOUT ANY WARRANTY; without even the implied warranty of +// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the +// GNU General Public License for more details. + +// You should have received a copy of the GNU General Public License +// along with Parity. If not, see . + +import React, { Component, PropTypes } from 'react'; + +import { api } from '../parity'; +import { callRegister, postRegister } from '../services'; +import Button from '../Button'; +import IdentityIcon from '../IdentityIcon'; + +import styles from './import.css'; + +export default class Import extends Component { + static propTypes = { + accounts: PropTypes.object.isRequired, + fromAddress: PropTypes.string.isRequired, + instance: PropTypes.object.isRequired, + visible: PropTypes.bool.isRequired, + onClose: PropTypes.func.isRequired, + onSetFromAddress: PropTypes.func.isRequired + } + + state = { + abi: null, + abiParsed: null, + abiError: 'Please add a valid ABI definition', + functions: null, + fnstate: {} + } + + render () { + const { visible, onClose } = this.props; + const { abiError } = this.state; + + if (!visible) { + return null; + } + + return ( +
+
+
+
+
abi import
+ +
+ { abiError ? this.renderCapture() : this.renderRegister() } +
+
+
+ ); + } + + renderCapture () { + const { abiError } = this.state; + + return ( +
+
+ Provide the ABI (Contract Interface) in the space provided below. Only non-constant functions (names & types) will be imported, while constant functions and existing signatures will be ignored. +
+
+ +
+ { abiError } +
+
+
+ ); + } + + renderRegister () { + const { accounts, fromAddress } = this.props; + + const account = accounts[fromAddress]; + const count = this.countFunctions(); + let buttons = null; + + if (count) { + buttons = ( +
+
+ +
+ +
+ ); + } + + return ( +
+
+ The following functions have been extracted from the ABI provided and the state has been determined from interacting with the signature contract. +
+
+
+ { this.renderFunctions() } +
+
+
+ { count || 'no' } functions available for registration +
+ { buttons } +
+ ); + } + + renderFunctions () { + const { functions, fnstate } = this.state; + + if (!functions) { + return null; + } + + return functions.map((fn) => { + if (fn.constant) { + fnstate[fn.signature] = 'fnconstant'; + } else if (!fnstate[fn.signature]) { + this.testFunction(fn); + } + + return ( +
+ { fn.id } +
+ ); + }); + } + + sortFunctions = (a, b) => { + return a.name.localeCompare(b.name); + } + + countFunctions () { + const { functions, fnstate } = this.state; + + if (!functions) { + return 0; + } + + return functions.filter((fn) => fnstate[fn.signature] === 'fntodo').length; + } + + testFunction (fn) { + const { instance } = this.props; + const { fnstate } = this.state; + + callRegister(instance, fn.id) + .then((result) => { + fnstate[fn.signature] = result ? 'fntodo' : 'fnexists'; + this.setState(fnstate); + }) + .catch((error) => { + console.error(error); + }); + } + + onAbiEdit = (event) => { + let functions = null; + let abiError = null; + let abiParsed = null; + let abi = null; + + try { + abiParsed = JSON.parse(event.target.value); + functions = api.newContract(abiParsed).functions.sort(this.sortFunctions); + abi = JSON.stringify(abiParsed); + } catch (error) { + console.error('onAbiEdit', error); + abiError = error.message; + } + + this.setState({ + functions, + abiError, + abiParsed, + abi + }); + } + + onRegister = () => { + const { instance, fromAddress, onClose } = this.props; + const { functions, fnstate } = this.state; + + Promise + .all( + functions + .filter((fn) => !fn.constant) + .filter((fn) => fnstate[fn.signature] === 'fntodo') + .map((fn) => postRegister(instance, fn.id, { from: fromAddress })) + ) + .then(() => { + onClose(); + }) + .catch((error) => { + console.error('onRegister', error); + }); + } + + onSelectFromAddress = () => { + const { accounts, fromAddress, onSetFromAddress } = this.props; + const addresses = Object.keys(accounts); + let index = 0; + + addresses.forEach((address, _index) => { + if (address === fromAddress) { + index = _index; + } + }); + + index++; + if (index >= addresses.length) { + index = 0; + } + + onSetFromAddress(addresses[index]); + } +} diff --git a/js/src/dapps/signaturereg/Import/index.js b/js/src/dapps/signaturereg/Import/index.js new file mode 100644 index 000000000..d2f352cf3 --- /dev/null +++ b/js/src/dapps/signaturereg/Import/index.js @@ -0,0 +1,17 @@ +// Copyright 2015, 2016 Ethcore (UK) Ltd. +// This file is part of Parity. + +// Parity is free software: you can redistribute it and/or modify +// it under the terms of the GNU General Public License as published by +// the Free Software Foundation, either version 3 of the License, or +// (at your option) any later version. + +// Parity is distributed in the hope that it will be useful, +// but WITHOUT ANY WARRANTY; without even the implied warranty of +// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the +// GNU General Public License for more details. + +// You should have received a copy of the GNU General Public License +// along with Parity. If not, see . + +export default from './import'; diff --git a/js/src/dapps/signaturereg/Loading/index.js b/js/src/dapps/signaturereg/Loading/index.js new file mode 100644 index 000000000..0468cab37 --- /dev/null +++ b/js/src/dapps/signaturereg/Loading/index.js @@ -0,0 +1,17 @@ +// Copyright 2015, 2016 Ethcore (UK) Ltd. +// This file is part of Parity. + +// Parity is free software: you can redistribute it and/or modify +// it under the terms of the GNU General Public License as published by +// the Free Software Foundation, either version 3 of the License, or +// (at your option) any later version. + +// Parity is distributed in the hope that it will be useful, +// but WITHOUT ANY WARRANTY; without even the implied warranty of +// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the +// GNU General Public License for more details. + +// You should have received a copy of the GNU General Public License +// along with Parity. If not, see . + +export default from './loading'; diff --git a/js/src/dapps/signaturereg/Loading/loading.css b/js/src/dapps/signaturereg/Loading/loading.css new file mode 100644 index 000000000..b77d1a237 --- /dev/null +++ b/js/src/dapps/signaturereg/Loading/loading.css @@ -0,0 +1,24 @@ +/* Copyright 2015, 2016 Ethcore (UK) Ltd. +/* This file is part of Parity. +/* +/* Parity is free software: you can redistribute it and/or modify +/* it under the terms of the GNU General Public License as published by +/* the Free Software Foundation, either version 3 of the License, or +/* (at your option) any later version. +/* +/* Parity is distributed in the hope that it will be useful, +/* but WITHOUT ANY WARRANTY; without even the implied warranty of +/* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the +/* GNU General Public License for more details. +/* +/* You should have received a copy of the GNU General Public License +/* along with Parity. If not, see . +*/ + +.loading { + width: 100%; + text-align: center; + padding-top: 5em; + font-size: 2em; + color: #999; +} diff --git a/js/src/dapps/signaturereg/Loading/loading.js b/js/src/dapps/signaturereg/Loading/loading.js new file mode 100644 index 000000000..b884597d7 --- /dev/null +++ b/js/src/dapps/signaturereg/Loading/loading.js @@ -0,0 +1,29 @@ +// Copyright 2015, 2016 Ethcore (UK) Ltd. +// This file is part of Parity. + +// Parity is free software: you can redistribute it and/or modify +// it under the terms of the GNU General Public License as published by +// the Free Software Foundation, either version 3 of the License, or +// (at your option) any later version. + +// Parity is distributed in the hope that it will be useful, +// but WITHOUT ANY WARRANTY; without even the implied warranty of +// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the +// GNU General Public License for more details. + +// You should have received a copy of the GNU General Public License +// along with Parity. If not, see . + +import React, { Component } from 'react'; + +import styles from './loading.css'; + +export default class Loading extends Component { + render () { + return ( +
+ Attaching to contract ... +
+ ); + } +} diff --git a/js/src/dapps/signaturereg/format.js b/js/src/dapps/signaturereg/format.js new file mode 100644 index 000000000..24b6428f4 --- /dev/null +++ b/js/src/dapps/signaturereg/format.js @@ -0,0 +1,40 @@ +// Copyright 2015, 2016 Ethcore (UK) Ltd. +// This file is part of Parity. + +// Parity is free software: you can redistribute it and/or modify +// it under the terms of the GNU General Public License as published by +// the Free Software Foundation, either version 3 of the License, or +// (at your option) any later version. + +// Parity is distributed in the hope that it will be useful, +// but WITHOUT ANY WARRANTY; without even the implied warranty of +// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the +// GNU General Public License for more details. + +// You should have received a copy of the GNU General Public License +// along with Parity. If not, see . + +import BigNumber from 'bignumber.js'; +import moment from 'moment'; + +import { api } from './parity'; + +const ZERO = new BigNumber(0); + +export function formatBlockNumber (blockNumber) { + return ZERO.eq(blockNumber || 0) + ? 'Pending' + : `${blockNumber.toFormat()}`; +} + +export function formatSignature (signature) { + return api.util.bytesToHex(signature); +} + +export function formatBlockTimestamp (block) { + if (!block || !block.timestamp) { + return null; + } + + return moment(block.timestamp).fromNow(); +} diff --git a/js/src/dapps/signaturereg/parity.js b/js/src/dapps/signaturereg/parity.js new file mode 100644 index 000000000..f6d59f44d --- /dev/null +++ b/js/src/dapps/signaturereg/parity.js @@ -0,0 +1,21 @@ +// Copyright 2015, 2016 Ethcore (UK) Ltd. +// This file is part of Parity. + +// Parity is free software: you can redistribute it and/or modify +// it under the terms of the GNU General Public License as published by +// the Free Software Foundation, either version 3 of the License, or +// (at your option) any later version. + +// Parity is distributed in the hope that it will be useful, +// but WITHOUT ANY WARRANTY; without even the implied warranty of +// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the +// GNU General Public License for more details. + +// You should have received a copy of the GNU General Public License +// along with Parity. If not, see . + +const { api } = window.parity; + +export { + api +}; diff --git a/js/src/dapps/signaturereg/services.js b/js/src/dapps/signaturereg/services.js new file mode 100644 index 000000000..3963d394c --- /dev/null +++ b/js/src/dapps/signaturereg/services.js @@ -0,0 +1,173 @@ +// Copyright 2015, 2016 Ethcore (UK) Ltd. +// This file is part of Parity. + +// Parity is free software: you can redistribute it and/or modify +// it under the terms of the GNU General Public License as published by +// the Free Software Foundation, either version 3 of the License, or +// (at your option) any later version. + +// Parity is distributed in the hope that it will be useful, +// but WITHOUT ANY WARRANTY; without even the implied warranty of +// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the +// GNU General Public License for more details. + +// You should have received a copy of the GNU General Public License +// along with Parity. If not, see . + +import * as abis from '../../contracts/abi'; +import { api } from './parity'; + +const sortEvents = (a, b) => b.blockNumber.cmp(a.blockNumber) || b.logIndex.cmp(a.logIndex); + +const logToEvent = (log) => { + const key = api.util.sha3(JSON.stringify(log)); + const { blockNumber, logIndex, transactionHash, transactionIndex, params, type } = log; + + return { + type: log.event, + state: type, + blockNumber, + logIndex, + transactionHash, + transactionIndex, + params, + key + }; +}; + +export function attachInterface (callback) { + return api.ethcore + .registryAddress() + .then((registryAddress) => { + console.log(`the registry was found at ${registryAddress}`); + + const registry = api.newContract(abis.registry, registryAddress).instance; + + return Promise + .all([ + registry.getAddress.call({}, [api.util.sha3('signaturereg'), 'A']), + api.personal.listAccounts(), + api.personal.accountsInfo() + ]); + }) + .then(([address, addresses, accountsInfo]) => { + console.log(`signaturereg was found at ${address}`); + + const contract = api.newContract(abis.signaturereg, address); + const accounts = addresses.reduce((obj, address) => { + const info = accountsInfo[address]; + + return Object.assign(obj, { + [address]: { + address, + name: info.name || 'Unnamed', + uuid: info.uuid + } + }); + }, {}); + const fromAddress = Object.keys(accounts)[0]; + + return { + accounts, + address, + accountsInfo, + contract, + instance: contract.instance, + fromAddress + }; + }) + .catch((error) => { + console.error('attachInterface', error); + }); +} + +export function attachBlockNumber (instance, callback) { + return api.subscribe('eth_blockNumber', (error, blockNumber) => { + if (error) { + console.error('blockNumber', error); + return; + } + + instance.totalSignatures + .call() + .then((totalSignatures) => { + callback({ + blockNumber, + totalSignatures + }); + }) + .catch((error) => { + console.error('totalSignatures', error); + }); + }); +} + +export function attachEvents (contract, callback) { + const blocks = { '0': {} }; + let mined = []; + let pending = []; + let events = []; + + const options = { + fromBlock: 0, + toBlock: 'pending', + limit: 50 + }; + + return contract.subscribe('Registered', options, (error, _logs) => { + if (error) { + console.error('events', error); + return; + } + + const logs = _logs.map(logToEvent); + + mined = logs + .filter((log) => log.state === 'mined') + .map((log) => { + const blockNumber = log.blockNumber.toString(); + + if (!blocks[blockNumber]) { + blocks[blockNumber] = {}; + getBlock(blockNumber).then((block) => { + Object.assign(blocks[blockNumber], block); + }); + } + + return Object.assign(log, { block: blocks[blockNumber] }); + }) + .reverse() + .concat(mined) + .sort(sortEvents); + + pending = logs + .filter((log) => log.state === 'pending') + .reverse() + .filter((event) => !pending.find((log) => log.params.method === event.params.method)) + .concat(pending) + .filter((event) => !mined.find((log) => log.params.method === event.params.method)) + .sort(sortEvents); + + events = pending.concat(mined); + + callback({ events }); + }); +} + +export function getBlock (blockNumber) { + return api.eth.getBlockByNumber(blockNumber); +} + +export function callRegister (instance, id, options = {}) { + return instance.register.call(options, [id]); +} + +export function postRegister (instance, id, options = {}) { + return instance.register + .estimateGas(options, [id]) + .then((gas) => { + options.gas = gas.mul(1.2).toFixed(0); + console.log('postRegister', `gas estimated at ${gas.toFormat(0)}, setting to ${gas.mul(1.2).toFormat(0)}`); + instance.register.postTransaction(options, [id]); + }); +} diff --git a/js/src/dapps/style.css b/js/src/dapps/style.css new file mode 100644 index 000000000..b177a15f9 --- /dev/null +++ b/js/src/dapps/style.css @@ -0,0 +1,46 @@ +/* Copyright 2015, 2016 Ethcore (UK) Ltd. +/* This file is part of Parity. +/* +/* Parity is free software: you can redistribute it and/or modify +/* it under the terms of the GNU General Public License as published by +/* the Free Software Foundation, either version 3 of the License, or +/* (at your option) any later version. +/* +/* Parity is distributed in the hope that it will be useful, +/* but WITHOUT ANY WARRANTY; without even the implied warranty of +/* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the +/* GNU General Public License for more details. +/* +/* You should have received a copy of the GNU General Public License +/* along with Parity. If not, see . +*/ + +:root, +:root body { + background: #fff; + border: 0; + color: #333; + font-size: 16px; + font-family: 'Roboto', sans-serif; + font-weight: 300 !important; + margin: 0; + padding: 0; + vertical-align: top; +} + +:root, +:root body, +:root :global(#container) { + min-height: 100%; + width: 100%; + display: flex; + flex: 1; +} + +:root * { + font-weight: 300 !important; +} + +:root :global(#container) > div { + flex: 1; +} diff --git a/js/src/dapps/tokenreg.html b/js/src/dapps/tokenreg.html new file mode 100644 index 000000000..ecb03d005 --- /dev/null +++ b/js/src/dapps/tokenreg.html @@ -0,0 +1,16 @@ + + + + + + + Token Registry + + +
+ + + + + + diff --git a/js/src/dapps/tokenreg.js b/js/src/dapps/tokenreg.js new file mode 100644 index 000000000..8ca70e114 --- /dev/null +++ b/js/src/dapps/tokenreg.js @@ -0,0 +1,39 @@ +// Copyright 2015, 2016 Ethcore (UK) Ltd. +// This file is part of Parity. + +// Parity is free software: you can redistribute it and/or modify +// it under the terms of the GNU General Public License as published by +// the Free Software Foundation, either version 3 of the License, or +// (at your option) any later version. + +// Parity is distributed in the hope that it will be useful, +// but WITHOUT ANY WARRANTY; without even the implied warranty of +// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the +// GNU General Public License for more details. + +// You should have received a copy of the GNU General Public License +// along with Parity. If not, see . + +import ReactDOM from 'react-dom'; +import React from 'react'; +import { Provider } from 'react-redux'; + +import injectTapEventPlugin from 'react-tap-event-plugin'; +injectTapEventPlugin(); + +import store from './tokenreg/store'; +import Container from './tokenreg/Container'; + +import '../../assets/fonts/Roboto/font.css'; +import '../../assets/fonts/RobotoMono/font.css'; +import './style.css'; +import './tokenreg.html'; + +ReactDOM.render( + ( + + + + ), + document.querySelector('#container') +); diff --git a/js/src/dapps/tokenreg/Accounts/AccountSelector/account-selector.css b/js/src/dapps/tokenreg/Accounts/AccountSelector/account-selector.css new file mode 100644 index 000000000..f6f8476bb --- /dev/null +++ b/js/src/dapps/tokenreg/Accounts/AccountSelector/account-selector.css @@ -0,0 +1,25 @@ +/* Copyright 2015, 2016 Ethcore (UK) Ltd. +/* This file is part of Parity. +/* +/* Parity is free software: you can redistribute it and/or modify +/* it under the terms of the GNU General Public License as published by +/* the Free Software Foundation, either version 3 of the License, or +/* (at your option) any later version. +/* +/* Parity is distributed in the hope that it will be useful, +/* but WITHOUT ANY WARRANTY; without even the implied warranty of +/* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the +/* GNU General Public License for more details. +/* +/* You should have received a copy of the GNU General Public License +/* along with Parity. If not, see . +*/ + +.account-selector { +} + +.avatar > img { + margin: 0; + width: 100%; + height: 100%; +} diff --git a/js/src/dapps/tokenreg/Accounts/AccountSelector/account-selector.js b/js/src/dapps/tokenreg/Accounts/AccountSelector/account-selector.js new file mode 100644 index 000000000..4c8525d7e --- /dev/null +++ b/js/src/dapps/tokenreg/Accounts/AccountSelector/account-selector.js @@ -0,0 +1,120 @@ +// Copyright 2015, 2016 Ethcore (UK) Ltd. +// This file is part of Parity. + +// Parity is free software: you can redistribute it and/or modify +// it under the terms of the GNU General Public License as published by +// the Free Software Foundation, either version 3 of the License, or +// (at your option) any later version. + +// Parity is distributed in the hope that it will be useful, +// but WITHOUT ANY WARRANTY; without even the implied warranty of +// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the +// GNU General Public License for more details. + +// You should have received a copy of the GNU General Public License +// along with Parity. If not, see . + +import React, { Component, PropTypes } from 'react'; +import { List, ListItem } from 'material-ui/List'; +import Subheader from 'material-ui/Subheader'; +import Avatar from 'material-ui/Avatar'; + +import IdentityIcon from '../../IdentityIcon'; + +import styles from './account-selector.css'; + +class AccountSelectorItem extends Component { + + static propTypes = { + onSelectAccount: PropTypes.func.isRequired, + account: PropTypes.object.isRequired + }; + + render () { + const account = this.props.account; + + const props = Object.assign({}, this.props); + delete props.account; + delete props.onSelectAccount; + + const icon = ( + ); + + const avatar = ( + ); + + return ( + + ); + } + + onSelectAccount = () => { + this.props.onSelectAccount(this.props.account.address); + } + +} + +export default class AccountSelector extends Component { + + static propTypes = { + list: PropTypes.array.isRequired, + selected: PropTypes.object.isRequired, + handleSetSelected: PropTypes.func.isRequired + }; + + state = { + open: false + }; + + render () { + const nestedAccounts = this.renderAccounts(this.props.list); + const selectedAccount = ( + + ); + + return ( +
+ + Select an account + { selectedAccount } + +
+ ); + } + + renderAccounts (accounts) { + return accounts + .map((account, index) => ( + + )); + } + + onToggleOpen = () => { + this.setState({ open: !this.state.open }); + } + + onSelectAccount = (address) => { + this.props.handleSetSelected(address); + this.onToggleOpen(); + } + +} diff --git a/js/src/dapps/tokenreg/Accounts/AccountSelector/container.js b/js/src/dapps/tokenreg/Accounts/AccountSelector/container.js new file mode 100644 index 000000000..6438d1dff --- /dev/null +++ b/js/src/dapps/tokenreg/Accounts/AccountSelector/container.js @@ -0,0 +1,45 @@ +// Copyright 2015, 2016 Ethcore (UK) Ltd. +// This file is part of Parity. + +// Parity is free software: you can redistribute it and/or modify +// it under the terms of the GNU General Public License as published by +// the Free Software Foundation, either version 3 of the License, or +// (at your option) any later version. + +// Parity is distributed in the hope that it will be useful, +// but WITHOUT ANY WARRANTY; without even the implied warranty of +// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the +// GNU General Public License for more details. + +// You should have received a copy of the GNU General Public License +// along with Parity. If not, see . + +import React, { Component } from 'react'; +import { connect } from 'react-redux'; + +import AccountSelector from './account-selector'; + +import { setSelectedAccount } from '../actions'; + +class AccountSelectorContainer extends Component { + render () { + return (); + } +} + +const mapStateToProps = (state) => { + const { accounts } = state; + return { ...accounts }; +}; + +const mapDispatchToProps = (dispatch) => { + return { + handleSetSelected: (address) => { + dispatch(setSelectedAccount(address)); + } + }; +}; + +export default connect(mapStateToProps, mapDispatchToProps)(AccountSelectorContainer); diff --git a/js/src/dapps/tokenreg/Accounts/AccountSelector/index.js b/js/src/dapps/tokenreg/Accounts/AccountSelector/index.js new file mode 100644 index 000000000..87fbc567e --- /dev/null +++ b/js/src/dapps/tokenreg/Accounts/AccountSelector/index.js @@ -0,0 +1,17 @@ +// Copyright 2015, 2016 Ethcore (UK) Ltd. +// This file is part of Parity. + +// Parity is free software: you can redistribute it and/or modify +// it under the terms of the GNU General Public License as published by +// the Free Software Foundation, either version 3 of the License, or +// (at your option) any later version. + +// Parity is distributed in the hope that it will be useful, +// but WITHOUT ANY WARRANTY; without even the implied warranty of +// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the +// GNU General Public License for more details. + +// You should have received a copy of the GNU General Public License +// along with Parity. If not, see . + +export default from './container'; diff --git a/js/src/dapps/tokenreg/Accounts/actions.js b/js/src/dapps/tokenreg/Accounts/actions.js new file mode 100644 index 000000000..2e597f2e5 --- /dev/null +++ b/js/src/dapps/tokenreg/Accounts/actions.js @@ -0,0 +1,61 @@ +// Copyright 2015, 2016 Ethcore (UK) Ltd. +// This file is part of Parity. + +// Parity is free software: you can redistribute it and/or modify +// it under the terms of the GNU General Public License as published by +// the Free Software Foundation, either version 3 of the License, or +// (at your option) any later version. + +// Parity is distributed in the hope that it will be useful, +// but WITHOUT ANY WARRANTY; without even the implied warranty of +// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the +// GNU General Public License for more details. + +// You should have received a copy of the GNU General Public License +// along with Parity. If not, see . + +const { api } = window.parity; + +export const SET_ACCOUNTS = 'SET_ACCOUNTS'; +export const setAccounts = (accounts) => ({ + type: SET_ACCOUNTS, + accounts +}); + +export const SET_ACCOUNTS_INFO = 'SET_ACCOUNTS_INFO'; +export const setAccountsInfo = (accountsInfo) => ({ + type: SET_ACCOUNTS_INFO, + accountsInfo +}); + +export const SET_SELECTED_ACCOUNT = 'SET_SELECTED_ACCOUNT'; +export const setSelectedAccount = (address) => ({ + type: SET_SELECTED_ACCOUNT, + address +}); + +export const loadAccounts = () => (dispatch) => { + Promise + .all([ + api.personal.listAccounts(), + api.personal.accountsInfo() + ]) + .then(results => { + const [ accounts, accountsInfo ] = results; + + const accountsList = accounts + .map(address => ({ + ...accountsInfo[address], + address + })); + + console.log('accounts', accountsList); + + dispatch(setAccounts(accountsList)); + dispatch(setAccountsInfo(accountsInfo)); + dispatch(setSelectedAccount(accountsList[0].address)); + }) + .catch(e => { + console.error('loadAccounts error', e); + }); +}; diff --git a/js/src/dapps/tokenreg/Accounts/reducer.js b/js/src/dapps/tokenreg/Accounts/reducer.js new file mode 100644 index 000000000..091df4c8a --- /dev/null +++ b/js/src/dapps/tokenreg/Accounts/reducer.js @@ -0,0 +1,57 @@ +// Copyright 2015, 2016 Ethcore (UK) Ltd. +// This file is part of Parity. + +// Parity is free software: you can redistribute it and/or modify +// it under the terms of the GNU General Public License as published by +// the Free Software Foundation, either version 3 of the License, or +// (at your option) any later version. + +// Parity is distributed in the hope that it will be useful, +// but WITHOUT ANY WARRANTY; without even the implied warranty of +// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the +// GNU General Public License for more details. + +// You should have received a copy of the GNU General Public License +// along with Parity. If not, see . + +import { + SET_ACCOUNTS, + SET_SELECTED_ACCOUNT, + SET_ACCOUNTS_INFO +} from './actions'; + +const initialState = { + list: [], + accountsInfo: {}, + selected: null +}; + +export default (state = initialState, action) => { + switch (action.type) { + case SET_ACCOUNTS: + return { + ...state, + list: [].concat(action.accounts) + }; + + case SET_ACCOUNTS_INFO: + return { + ...state, + accountsInfo: { ...action.accountsInfo } + }; + + case SET_SELECTED_ACCOUNT: { + const address = action.address; + const account = state.list.find(a => a.address === address); + + return { + ...state, + selected: account + }; + } + + default: + return state; + } +}; + diff --git a/js/src/dapps/tokenreg/Actions/Query/index.js b/js/src/dapps/tokenreg/Actions/Query/index.js new file mode 100644 index 000000000..83bc6a337 --- /dev/null +++ b/js/src/dapps/tokenreg/Actions/Query/index.js @@ -0,0 +1,17 @@ +// Copyright 2015, 2016 Ethcore (UK) Ltd. +// This file is part of Parity. + +// Parity is free software: you can redistribute it and/or modify +// it under the terms of the GNU General Public License as published by +// the Free Software Foundation, either version 3 of the License, or +// (at your option) any later version. + +// Parity is distributed in the hope that it will be useful, +// but WITHOUT ANY WARRANTY; without even the implied warranty of +// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the +// GNU General Public License for more details. + +// You should have received a copy of the GNU General Public License +// along with Parity. If not, see . + +export default from './query'; diff --git a/js/src/dapps/tokenreg/Actions/Query/query.js b/js/src/dapps/tokenreg/Actions/Query/query.js new file mode 100644 index 000000000..16d13f2f0 --- /dev/null +++ b/js/src/dapps/tokenreg/Actions/Query/query.js @@ -0,0 +1,207 @@ +// Copyright 2015, 2016 Ethcore (UK) Ltd. +// This file is part of Parity. + +// Parity is free software: you can redistribute it and/or modify +// it under the terms of the GNU General Public License as published by +// the Free Software Foundation, either version 3 of the License, or +// (at your option) any later version. + +// Parity is distributed in the hope that it will be useful, +// but WITHOUT ANY WARRANTY; without even the implied warranty of +// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the +// GNU General Public License for more details. + +// You should have received a copy of the GNU General Public License +// along with Parity. If not, see . + +import React, { Component, PropTypes } from 'react'; + +import { Dialog, FlatButton, SelectField, MenuItem } from 'material-ui'; + +import InputText from '../../Inputs/Text'; +import Loading from '../../Loading'; +import Token from '../../Tokens/Token'; + +import { SIMPLE_TOKEN_ADDRESS_TYPE, SIMPLE_TLA_TYPE } from '../../Inputs/validation'; + +import styles from '../actions.css'; + +const initState = { + queryKey: 'tla', + form: { + valid: false, + value: '' + } +}; + +export default class QueryAction extends Component { + + static propTypes = { + show: PropTypes.bool.isRequired, + loading: PropTypes.bool.isRequired, + + onClose: PropTypes.func.isRequired, + handleQueryToken: PropTypes.func.isRequired, + handleQueryMetaLookup: PropTypes.func.isRequired, + + data: PropTypes.object, + notFound: PropTypes.bool, + metaLoading: PropTypes.bool, + metaData: PropTypes.object + } + + state = initState; + + render () { + return ( + + { this.renderContent() } + + ); + } + + renderActions () { + const { loading, data, notFound } = this.props; + + if (loading) { + return ( + + ); + } + + const complete = data || notFound; + + if (complete) { + return ([ + + ]); + } + + const isValid = this.state.form.valid; + + return ([ + , + + ]); + } + + renderContent () { + const { loading, notFound, data } = this.props; + + if (loading) { + return ( + + ); + } + + if (notFound) { + return ( +

No token has been found in the registry...

+ ); + } + + if (data) { + return this.renderData(); + } + + return this.renderForm(); + } + + renderData () { + const { data } = this.props; + + return ( + + ); + } + + renderForm () { + return ( +
+ + + + + + { + this.state.queryKey !== 'tla' + ? () + : () + } +
+ ); + } + + onQueryKeyChange = (event, index, queryKey) => { + this.setState({ + queryKey, + form: { valid: false, value: '' } + }); + } + + onChange = (valid, value) => { + this.setState({ + form: { + valid, value + } + }); + } + + onQuery = () => { + if (!this.state.form.valid) return; + + const { queryKey, form } = this.state; + + this.props.handleQueryToken(queryKey, form.value); + } + + onClose = () => { + this.setState(initState); + this.props.onClose(); + } + +} diff --git a/js/src/dapps/tokenreg/Actions/Register/index.js b/js/src/dapps/tokenreg/Actions/Register/index.js new file mode 100644 index 000000000..3099d1f42 --- /dev/null +++ b/js/src/dapps/tokenreg/Actions/Register/index.js @@ -0,0 +1,17 @@ +// Copyright 2015, 2016 Ethcore (UK) Ltd. +// This file is part of Parity. + +// Parity is free software: you can redistribute it and/or modify +// it under the terms of the GNU General Public License as published by +// the Free Software Foundation, either version 3 of the License, or +// (at your option) any later version. + +// Parity is distributed in the hope that it will be useful, +// but WITHOUT ANY WARRANTY; without even the implied warranty of +// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the +// GNU General Public License for more details. + +// You should have received a copy of the GNU General Public License +// along with Parity. If not, see . + +export default from './register'; diff --git a/js/src/dapps/tokenreg/Actions/Register/register.js b/js/src/dapps/tokenreg/Actions/Register/register.js new file mode 100644 index 000000000..78d823326 --- /dev/null +++ b/js/src/dapps/tokenreg/Actions/Register/register.js @@ -0,0 +1,217 @@ +// Copyright 2015, 2016 Ethcore (UK) Ltd. +// This file is part of Parity. + +// Parity is free software: you can redistribute it and/or modify +// it under the terms of the GNU General Public License as published by +// the Free Software Foundation, either version 3 of the License, or +// (at your option) any later version. + +// Parity is distributed in the hope that it will be useful, +// but WITHOUT ANY WARRANTY; without even the implied warranty of +// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the +// GNU General Public License for more details. + +// You should have received a copy of the GNU General Public License +// along with Parity. If not, see . + +import React, { Component, PropTypes } from 'react'; + +import { Dialog, FlatButton } from 'material-ui'; + +import AccountSelector from '../../Accounts/AccountSelector'; +import InputText from '../../Inputs/Text'; + +import { TOKEN_ADDRESS_TYPE, TLA_TYPE, UINT_TYPE, STRING_TYPE } from '../../Inputs/validation'; + +import styles from '../actions.css'; + +const defaultField = { value: '', valid: false }; +const initState = { + isFormValid: false, + fields: { + address: { + ...defaultField, + type: TOKEN_ADDRESS_TYPE, + floatingLabelText: 'Token address', + hintText: 'The token address' + }, + tla: { + ...defaultField, + type: TLA_TYPE, + floatingLabelText: 'Token TLA', + hintText: 'The token short name (3 characters)' + }, + base: { + ...defaultField, + type: UINT_TYPE, + floatingLabelText: 'Token Base', + hintText: 'The token precision' + }, + name: { + ...defaultField, + type: STRING_TYPE, + floatingLabelText: 'Token name', + hintText: 'The token name' + } + } +}; + +export default class RegisterAction extends Component { + + static propTypes = { + show: PropTypes.bool.isRequired, + sending: PropTypes.bool.isRequired, + complete: PropTypes.bool.isRequired, + onClose: PropTypes.func.isRequired, + handleRegisterToken: PropTypes.func.isRequired, + + error: PropTypes.object + } + + state = initState; + + render () { + const { sending, error, complete } = this.props; + + return ( + + { this.renderContent() } + + ); + } + + renderActions () { + const { complete, sending, error } = this.props; + + if (error) { + return ( + + ); + } + + if (complete) { + return ( + + ); + } + + const isValid = this.state.isFormValid; + + return ([ + , + + ]); + } + + renderContent () { + const { error, complete } = this.props; + + if (error) return this.renderError(); + if (complete) return this.renderComplete(); + return this.renderForm(); + } + + renderError () { + const { error } = this.props; + + return (
+

{ error.toString() }

+
); + } + + renderComplete () { + return (
+

Your transaction has been posted. Please visit the Parity Signer to authenticate the transfer.

+
); + } + + renderForm () { + return ( +
+ + { this.renderInputs() } +
+ ); + } + + renderInputs () { + const { fields } = this.state; + + return Object.keys(fields).map((fieldKey, index) => { + const onChange = this.onChange.bind(this, fieldKey); + const field = fields[fieldKey]; + + return ( + + ); + }); + } + + onChange (fieldKey, valid, value) { + const { fields } = this.state; + const field = fields[fieldKey]; + + const newFields = { + ...fields, + [ fieldKey ]: { + ...field, + valid, value + } + }; + + const isFormValid = Object.keys(newFields) + .map(key => newFields[key].valid) + .reduce((current, fieldValid) => { + return current && fieldValid; + }, true); + + this.setState({ + fields: newFields, + isFormValid + }); + } + + onRegister = () => { + const { fields } = this.state; + + const data = Object.keys(fields) + .reduce((dataObject, fieldKey) => { + dataObject[fieldKey] = fields[fieldKey].value; + return dataObject; + }, {}); + + this.props.handleRegisterToken(data); + } + + onClose = () => { + this.setState(initState); + this.props.onClose(); + } + +} diff --git a/js/src/dapps/tokenreg/Actions/actions.css b/js/src/dapps/tokenreg/Actions/actions.css new file mode 100644 index 000000000..d8eba57f1 --- /dev/null +++ b/js/src/dapps/tokenreg/Actions/actions.css @@ -0,0 +1,50 @@ +/* Copyright 2015, 2016 Ethcore (UK) Ltd. +/* This file is part of Parity. +/* +/* Parity is free software: you can redistribute it and/or modify +/* it under the terms of the GNU General Public License as published by +/* the Free Software Foundation, either version 3 of the License, or +/* (at your option) any later version. +/* +/* Parity is distributed in the hope that it will be useful, +/* but WITHOUT ANY WARRANTY; without even the implied warranty of +/* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the +/* GNU General Public License for more details. +/* +/* You should have received a copy of the GNU General Public License +/* along with Parity. If not, see . +*/ + +.actions { + padding-top: 2rem; +} + +.button { + margin: 0 0.5em; +} + +.button button { + background-color: #27ae60 !important; + height: 56px !important; + padding: 0 10px !important; +} + +.button button[disabled] { + background-color: rgba(50, 50, 50, 0.25) !important; +} + +.dialog { +} + +.dialog h3 { + color: rgba(50, 100, 150, 1) !important; + text-transform: uppercase; +} + +.dialogtext { + padding-top: 1em; +} + +.error { + color: red; +} diff --git a/js/src/dapps/tokenreg/Actions/actions.js b/js/src/dapps/tokenreg/Actions/actions.js new file mode 100644 index 000000000..0f3390ea4 --- /dev/null +++ b/js/src/dapps/tokenreg/Actions/actions.js @@ -0,0 +1,215 @@ +// Copyright 2015, 2016 Ethcore (UK) Ltd. +// This file is part of Parity. + +// Parity is free software: you can redistribute it and/or modify +// it under the terms of the GNU General Public License as published by +// the Free Software Foundation, either version 3 of the License, or +// (at your option) any later version. + +// Parity is distributed in the hope that it will be useful, +// but WITHOUT ANY WARRANTY; without even the implied warranty of +// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the +// GNU General Public License for more details. + +// You should have received a copy of the GNU General Public License +// along with Parity. If not, see . + +import { getTokenTotalSupply } from '../utils'; + +const { sha3, bytesToHex } = window.parity.api.util; + +export const SET_REGISTER_SENDING = 'SET_REGISTER_SENDING'; +export const setRegisterSending = (isSending) => ({ + type: SET_REGISTER_SENDING, + isSending +}); + +export const SET_REGISTER_ERROR = 'SET_REGISTER_ERROR'; +export const setRegisterError = (e) => ({ + type: SET_REGISTER_ERROR, + error: e +}); + +export const REGISTER_RESET = 'REGISTER_RESET'; +export const registerReset = () => ({ + type: REGISTER_RESET +}); + +export const REGISTER_COMPLETED = 'REGISTER_COMPLETED'; +export const registerCompleted = () => ({ + type: REGISTER_COMPLETED +}); + +export const registerToken = (tokenData) => (dispatch, getState) => { + console.log('registering token', tokenData); + + const state = getState(); + const contractInstance = state.status.contract.instance; + const fee = state.status.contract.fee; + + const { address, base, name, tla } = tokenData; + + dispatch(setRegisterSending(true)); + + const values = [ address, tla, base, name ]; + const options = { + from: state.accounts.selected.address, + value: fee + }; + + Promise.resolve() + .then(() => { + return contractInstance + .fromTLA.call({}, [ tla ]) + .then(([id, address, base, name, owner]) => { + if (owner !== '0x0000000000000000000000000000000000000000') { + throw new Error(`A Token has already been registered with the TLA ${tla}`); + } + }); + }) + .then(() => { + return contractInstance + .fromAddress.call({}, [ address ]) + .then(([id, tla, base, name, owner]) => { + if (owner !== '0x0000000000000000000000000000000000000000') { + throw new Error(`A Token has already been registered with the Address ${address}`); + } + }); + }) + .then(() => { + return contractInstance + .register.estimateGas(options, values); + }) + .then((gasEstimate) => { + options.gas = gasEstimate.mul(1.2).toFixed(0); + console.log(`transfer: gas estimated as ${gasEstimate.toFixed(0)} setting to ${options.gas}`); + + return contractInstance.register.postTransaction(options, values); + }) + .then((result) => { + dispatch(registerCompleted()); + }) + .catch((e) => { + console.error('registerToken error', e); + dispatch(setRegisterError(e)); + }); +}; + +export const SET_QUERY_LOADING = 'SET_QUERY_LOADING'; +export const setQueryLoading = (isLoading) => ({ + type: SET_QUERY_LOADING, + isLoading +}); + +export const SET_QUERY_RESULT = 'SET_QUERY_RESULT'; +export const setQueryResult = (data) => ({ + type: SET_QUERY_RESULT, + data +}); + +export const SET_QUERY_NOT_FOUND = 'SET_QUERY_NOT_FOUND'; +export const setQueryNotFound = () => ({ + type: SET_QUERY_NOT_FOUND +}); + +export const QUERY_RESET = 'QUERY_RESET'; +export const queryReset = () => ({ + type: QUERY_RESET +}); + +export const SET_QUERY_META_LOADING = 'SET_QUERY_META_LOADING'; +export const setQueryMetaLoading = (isLoading) => ({ + type: SET_QUERY_META_LOADING, + isLoading +}); + +export const SET_QUERY_META = 'SET_QUERY_META'; +export const setQueryMeta = (data) => ({ + type: SET_QUERY_META, + data +}); + +export const queryToken = (key, query) => (dispatch, getState) => { + const state = getState(); + const contractInstance = state.status.contract.instance; + + const contractFunc = (key === 'tla') ? 'fromTLA' : 'fromAddress'; + + dispatch(setQueryLoading(true)); + + contractInstance[contractFunc] + .call({}, [ query ]) + .then((result) => { + const data = { + index: result[0].toNumber(), + base: result[2].toNumber(), + name: result[3], + owner: result[4] + }; + + if (key === 'tla') { + data.tla = query; + data.address = result[1]; + } + + if (key === 'address') { + data.address = query; + data.tla = result[1]; + } + + return data; + }) + .then(data => { + return getTokenTotalSupply(data.address) + .then(totalSupply => { + data.totalSupply = totalSupply; + return data; + }); + }) + .then(data => { + if (data.totalSupply === null) { + dispatch(setQueryNotFound()); + dispatch(setQueryLoading(false)); + + return false; + } + + data.totalSupply = data.totalSupply.toNumber(); + dispatch(setQueryResult(data)); + dispatch(setQueryLoading(false)); + }, () => { + dispatch(setQueryNotFound()); + dispatch(setQueryLoading(false)); + }); +}; + +export const queryTokenMeta = (id, query) => (dispatch, getState) => { + console.log('loading token meta', query); + + const state = getState(); + const contractInstance = state.status.contract.instance; + + const key = sha3(query); + + const startDate = Date.now(); + dispatch(setQueryMetaLoading(true)); + + contractInstance + .meta + .call({}, [ id, key ]) + .then((value) => { + const meta = { + key, query, + value: value.find(v => v !== 0) ? bytesToHex(value) : null + }; + + dispatch(setQueryMeta(meta)); + + setTimeout(() => { + dispatch(setQueryMetaLoading(false)); + }, 500 - (Date.now() - startDate)); + }) + .catch((e) => { + console.error('load meta query error', e); + }); +}; diff --git a/js/src/dapps/tokenreg/Actions/component.js b/js/src/dapps/tokenreg/Actions/component.js new file mode 100644 index 000000000..3e7ef0d64 --- /dev/null +++ b/js/src/dapps/tokenreg/Actions/component.js @@ -0,0 +1,119 @@ +// Copyright 2015, 2016 Ethcore (UK) Ltd. +// This file is part of Parity. + +// Parity is free software: you can redistribute it and/or modify +// it under the terms of the GNU General Public License as published by +// the Free Software Foundation, either version 3 of the License, or +// (at your option) any later version. + +// Parity is distributed in the hope that it will be useful, +// but WITHOUT ANY WARRANTY; without even the implied warranty of +// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the +// GNU General Public License for more details. + +// You should have received a copy of the GNU General Public License +// along with Parity. If not, see . + +import React, { Component, PropTypes } from 'react'; + +import { RaisedButton } from 'material-ui'; +import ActionSearchIcon from 'material-ui/svg-icons/action/search'; +import ContentSendIcon from 'material-ui/svg-icons/content/send'; + +import Register from './Register'; +import Query from './Query'; + +import styles from './actions.css'; + +const REGISTER_ACTION = 'REGISTER_ACTION'; +const QUERY_ACTION = 'QUERY_ACTION'; + +export default class Actions extends Component { + + static propTypes = { + handleRegisterToken: PropTypes.func.isRequired, + handleRegisterClose: PropTypes.func.isRequired, + register: PropTypes.object.isRequired, + + handleQueryToken: PropTypes.func.isRequired, + handleQueryClose: PropTypes.func.isRequired, + handleQueryMetaLookup: PropTypes.func.isRequired, + query: PropTypes.object.isRequired + }; + + state = { + show: { + [ REGISTER_ACTION ]: false, + [ QUERY_ACTION ]: false + } + } + + constructor () { + super(); + + this.onShowRegister = this.onShow.bind(this, REGISTER_ACTION); + this.onShowQuery = this.onShow.bind(this, QUERY_ACTION); + } + + render () { + return ( +
+ } + label='Register Token' + primary + onTouchTap={ this.onShowRegister } /> + + } + label='Search Token' + primary + onTouchTap={ this.onShowQuery } /> + + + + +
+ ); + } + + onRegisterClose = () => { + this.onHide(REGISTER_ACTION); + this.props.handleRegisterClose(); + } + + onQueryClose = () => { + this.onHide(QUERY_ACTION); + this.props.handleQueryClose(); + } + + onShow (key) { + this.setState({ + show: { + ...this.state.show, + [ key ]: true + } + }); + } + + onHide (key) { + this.setState({ + show: { + ...this.state.show, + [ key ]: false + } + }); + } + +} diff --git a/js/src/dapps/tokenreg/Actions/container.js b/js/src/dapps/tokenreg/Actions/container.js new file mode 100644 index 000000000..2c3773122 --- /dev/null +++ b/js/src/dapps/tokenreg/Actions/container.js @@ -0,0 +1,62 @@ +// Copyright 2015, 2016 Ethcore (UK) Ltd. +// This file is part of Parity. + +// Parity is free software: you can redistribute it and/or modify +// it under the terms of the GNU General Public License as published by +// the Free Software Foundation, either version 3 of the License, or +// (at your option) any later version. + +// Parity is distributed in the hope that it will be useful, +// but WITHOUT ANY WARRANTY; without even the implied warranty of +// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the +// GNU General Public License for more details. + +// You should have received a copy of the GNU General Public License +// along with Parity. If not, see . + +import React, { Component } from 'react'; +import { connect } from 'react-redux'; + +import Actions from './component'; + +import { registerToken, registerReset, queryToken, queryReset, queryTokenMeta } from './actions'; + +class TokensContainer extends Component { + + render () { + return (); + } +} + +const mapStateToProps = (state) => { + const { register, query } = state.actions; + + return { register, query }; +}; + +const mapDispatchToProps = (dispatch) => { + return { + handleRegisterToken: (tokenData) => { + dispatch(registerToken(tokenData)); + }, + handleRegisterClose: () => { + dispatch(registerReset()); + }, + handleQueryToken: (key, query) => { + dispatch(queryToken(key, query)); + }, + handleQueryClose: () => { + dispatch(queryReset()); + }, + handleQueryMetaLookup: (id, query) => { + dispatch(queryTokenMeta(id, query)); + } + }; +}; + +export default connect( + mapStateToProps, + mapDispatchToProps +)(TokensContainer); diff --git a/js/src/dapps/tokenreg/Actions/index.js b/js/src/dapps/tokenreg/Actions/index.js new file mode 100644 index 000000000..9007217b7 --- /dev/null +++ b/js/src/dapps/tokenreg/Actions/index.js @@ -0,0 +1,17 @@ +// Copyright 2015, 2016 Ethcore (UK) Ltd. +// This file is part of Parity. + +// Parity is free software: you can redistribute it and/or modify +// it under the terms of the GNU General Public License as published by +// the Free Software Foundation, either version 3 of the License, or +// (at your option) any later version. + +// Parity is distributed in the hope that it will be useful, +// but WITHOUT ANY WARRANTY; without even the implied warranty of +// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the +// GNU General Public License for more details. + +// You should have received a copy of the GNU General Public License +// along with Parity. If not, see . + +export default from './container.js'; diff --git a/js/src/dapps/tokenreg/Actions/reducer.js b/js/src/dapps/tokenreg/Actions/reducer.js new file mode 100644 index 000000000..7b7f8341a --- /dev/null +++ b/js/src/dapps/tokenreg/Actions/reducer.js @@ -0,0 +1,155 @@ +// Copyright 2015, 2016 Ethcore (UK) Ltd. +// This file is part of Parity. + +// Parity is free software: you can redistribute it and/or modify +// it under the terms of the GNU General Public License as published by +// the Free Software Foundation, either version 3 of the License, or +// (at your option) any later version. + +// Parity is distributed in the hope that it will be useful, +// but WITHOUT ANY WARRANTY; without even the implied warranty of +// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the +// GNU General Public License for more details. + +// You should have received a copy of the GNU General Public License +// along with Parity. If not, see . + +import { + SET_REGISTER_SENDING, + SET_REGISTER_ERROR, + REGISTER_RESET, + REGISTER_COMPLETED, + + SET_QUERY_LOADING, + SET_QUERY_RESULT, + SET_QUERY_NOT_FOUND, + SET_QUERY_META_LOADING, + SET_QUERY_META, + QUERY_RESET +} from './actions'; + +const initialState = { + register: { + sending: false, + error: null, + complete: false + }, + query: { + loading: false, + data: null, + notFound: false, + metaLoading: false, + metaData: null + } +}; + +export default (state = initialState, action) => { + switch (action.type) { + case SET_REGISTER_SENDING: { + const registerState = state.register; + + return { + ...state, + register: { + ...registerState, + sending: action.isSending + } + }; + } + + case REGISTER_COMPLETED: { + const registerState = state.register; + + return { + ...state, + register: { + ...registerState, + sending: false, + complete: true + } + }; + } + + case SET_REGISTER_ERROR: { + const registerState = state.register; + + return { + ...state, + register: { + ...registerState, + sending: false, + error: action.error + } + }; + } + + case REGISTER_RESET: { + return { + ...state, + register: initialState.register + }; + } + + case SET_QUERY_LOADING: { + return { + ...state, + query: { + ...state.query, + loading: action.isLoading + } + }; + } + + case SET_QUERY_RESULT: { + return { + ...state, + query: { + ...state.query, + data: action.data + } + }; + } + + case SET_QUERY_NOT_FOUND: { + return { + ...state, + query: { + ...state.query, + notFound: true + } + }; + } + + case SET_QUERY_META_LOADING: { + return { + ...state, + query: { + ...state.query, + metaLoading: action.isLoading + } + }; + } + + case SET_QUERY_META: { + return { + ...state, + query: { + ...state.query, + metaData: action.data + } + }; + } + + case QUERY_RESET: { + return { + ...state, + query: { + ...initialState.query + } + }; + } + + default: + return state; + } +}; diff --git a/js/src/dapps/tokenreg/Application/application.css b/js/src/dapps/tokenreg/Application/application.css new file mode 100644 index 000000000..07bc74b40 --- /dev/null +++ b/js/src/dapps/tokenreg/Application/application.css @@ -0,0 +1,22 @@ +/* Copyright 2015, 2016 Ethcore (UK) Ltd. +/* This file is part of Parity. +/* +/* Parity is free software: you can redistribute it and/or modify +/* it under the terms of the GNU General Public License as published by +/* the Free Software Foundation, either version 3 of the License, or +/* (at your option) any later version. +/* +/* Parity is distributed in the hope that it will be useful, +/* but WITHOUT ANY WARRANTY; without even the implied warranty of +/* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the +/* GNU General Public License for more details. +/* +/* You should have received a copy of the GNU General Public License +/* along with Parity. If not, see . +*/ + +.application { + display: flex; + flex-direction: column; + align-items: center; +} diff --git a/js/src/dapps/tokenreg/Application/application.js b/js/src/dapps/tokenreg/Application/application.js new file mode 100644 index 000000000..e48922b05 --- /dev/null +++ b/js/src/dapps/tokenreg/Application/application.js @@ -0,0 +1,72 @@ +// Copyright 2015, 2016 Ethcore (UK) Ltd. +// This file is part of Parity. + +// Parity is free software: you can redistribute it and/or modify +// it under the terms of the GNU General Public License as published by +// the Free Software Foundation, either version 3 of the License, or +// (at your option) any later version. + +// Parity is distributed in the hope that it will be useful, +// but WITHOUT ANY WARRANTY; without even the implied warranty of +// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the +// GNU General Public License for more details. + +// You should have received a copy of the GNU General Public License +// along with Parity. If not, see . + +import React, { Component, PropTypes } from 'react'; +import getMuiTheme from 'material-ui/styles/getMuiTheme'; + +import Loading from '../Loading'; +import Status from '../Status'; +import Tokens from '../Tokens'; +import Actions from '../Actions'; + +import styles from './application.css'; + +const muiTheme = getMuiTheme({ + palette: { + primary1Color: '#27ae60' + } +}); + +export default class Application extends Component { + static childContextTypes = { + muiTheme: PropTypes.object + } + + static propTypes = { + isLoading: PropTypes.bool.isRequired, + + contract: PropTypes.object + }; + + render () { + const { isLoading, contract } = this.props; + + if (isLoading) { + return ( + + ); + } + + return ( +
+ + + + + +
+ ); + } + + getChildContext () { + return { + muiTheme + }; + } + +} diff --git a/js/src/dapps/tokenreg/Application/index.js b/js/src/dapps/tokenreg/Application/index.js new file mode 100644 index 000000000..236578226 --- /dev/null +++ b/js/src/dapps/tokenreg/Application/index.js @@ -0,0 +1,17 @@ +// Copyright 2015, 2016 Ethcore (UK) Ltd. +// This file is part of Parity. + +// Parity is free software: you can redistribute it and/or modify +// it under the terms of the GNU General Public License as published by +// the Free Software Foundation, either version 3 of the License, or +// (at your option) any later version. + +// Parity is distributed in the hope that it will be useful, +// but WITHOUT ANY WARRANTY; without even the implied warranty of +// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the +// GNU General Public License for more details. + +// You should have received a copy of the GNU General Public License +// along with Parity. If not, see . + +export default from './application'; diff --git a/js/src/dapps/tokenreg/Chip/chip.css b/js/src/dapps/tokenreg/Chip/chip.css new file mode 100644 index 000000000..5c28d56ce --- /dev/null +++ b/js/src/dapps/tokenreg/Chip/chip.css @@ -0,0 +1,45 @@ +/* Copyright 2015, 2016 Ethcore (UK) Ltd. +/* This file is part of Parity. +/* +/* Parity is free software: you can redistribute it and/or modify +/* it under the terms of the GNU General Public License as published by +/* the Free Software Foundation, either version 3 of the License, or +/* (at your option) any later version. +/* +/* Parity is distributed in the hope that it will be useful, +/* but WITHOUT ANY WARRANTY; without even the implied warranty of +/* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the +/* GNU General Public License for more details. +/* +/* You should have received a copy of the GNU General Public License +/* along with Parity. If not, see . +*/ + +.chip > span { + flex: 1; + display: flex; + flex-direction: row; +} + +.chip img { + margin-bottom: -11px; + margin-left: -11px; +} + +.value { + font-family: 'Roboto Mono', monospace; + color: rgba(255, 255, 255, 1); + -webkit-user-select: text; + cursor: text; + flex: 1; + white-space: nowrap; + overflow: hidden; + text-overflow: ellipsis; +} + +.label { + color: rgba(255, 255, 255, 0.7); + margin-left: 1em; + margin-right: 0.5em; + text-transform: uppercase; +} diff --git a/js/src/dapps/tokenreg/Chip/chip.js b/js/src/dapps/tokenreg/Chip/chip.js new file mode 100644 index 000000000..ad9886025 --- /dev/null +++ b/js/src/dapps/tokenreg/Chip/chip.js @@ -0,0 +1,68 @@ +// Copyright 2015, 2016 Ethcore (UK) Ltd. +// This file is part of Parity. + +// Parity is free software: you can redistribute it and/or modify +// it under the terms of the GNU General Public License as published by +// the Free Software Foundation, either version 3 of the License, or +// (at your option) any later version. + +// Parity is distributed in the hope that it will be useful, +// but WITHOUT ANY WARRANTY; without even the implied warranty of +// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the +// GNU General Public License for more details. + +// You should have received a copy of the GNU General Public License +// along with Parity. If not, see . + +import React, { Component, PropTypes } from 'react'; + +import { Chip } from 'material-ui'; + +import IdentityIcon from '../IdentityIcon' ; + +import styles from './chip.css'; + +export default class CustomChip extends Component { + static propTypes = { + value: PropTypes.string.isRequired, + label: PropTypes.string.isRequired, + + isAddress: PropTypes.bool, + displayValue: PropTypes.string + }; + + render () { + const { isAddress, value, label } = this.props; + + const displayValue = this.props.displayValue || value; + + return ( + + { this.renderIcon(isAddress, value) } + + { displayValue } + + + { label } + + + ); + } + + renderIcon (isAddress, address) { + if (!isAddress) return; + + return ( + + ); + } +} diff --git a/js/src/dapps/tokenreg/Chip/index.js b/js/src/dapps/tokenreg/Chip/index.js new file mode 100644 index 000000000..6e1ea80fb --- /dev/null +++ b/js/src/dapps/tokenreg/Chip/index.js @@ -0,0 +1,17 @@ +// Copyright 2015, 2016 Ethcore (UK) Ltd. +// This file is part of Parity. + +// Parity is free software: you can redistribute it and/or modify +// it under the terms of the GNU General Public License as published by +// the Free Software Foundation, either version 3 of the License, or +// (at your option) any later version. + +// Parity is distributed in the hope that it will be useful, +// but WITHOUT ANY WARRANTY; without even the implied warranty of +// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the +// GNU General Public License for more details. + +// You should have received a copy of the GNU General Public License +// along with Parity. If not, see . + +export default from './chip'; diff --git a/js/src/dapps/tokenreg/Container.js b/js/src/dapps/tokenreg/Container.js new file mode 100644 index 000000000..9a8d7c636 --- /dev/null +++ b/js/src/dapps/tokenreg/Container.js @@ -0,0 +1,64 @@ +// Copyright 2015, 2016 Ethcore (UK) Ltd. +// This file is part of Parity. + +// Parity is free software: you can redistribute it and/or modify +// it under the terms of the GNU General Public License as published by +// the Free Software Foundation, either version 3 of the License, or +// (at your option) any later version. + +// Parity is distributed in the hope that it will be useful, +// but WITHOUT ANY WARRANTY; without even the implied warranty of +// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the +// GNU General Public License for more details. + +// You should have received a copy of the GNU General Public License +// along with Parity. If not, see . + +import React, { Component, PropTypes } from 'react'; +import { connect } from 'react-redux'; + +import Application from './Application'; + +import { loadContract } from './Status/actions'; +import { loadAccounts } from './Accounts/actions'; + +class Container extends Component { + static propTypes = { + isLoading: PropTypes.bool.isRequired, + contract: PropTypes.object.isRequired, + onLoad: PropTypes.func.isRequired + }; + + componentDidMount () { + this.props.onLoad(); + } + + render () { + const { isLoading, contract } = this.props; + + return (); + } +} + +const mapStateToProps = (state) => { + const { isLoading, contract } = state.status; + + return { + isLoading, + contract + }; +}; + +const mapDispatchToProps = (dispatch) => { + return { + onLoad: () => { + dispatch(loadContract()); + dispatch(loadAccounts()); + } + }; +}; + +export default connect(mapStateToProps, mapDispatchToProps)(Container); diff --git a/js/src/dapps/tokenreg/IdentityIcon/identityIcon.css b/js/src/dapps/tokenreg/IdentityIcon/identityIcon.css new file mode 100644 index 000000000..2b645d823 --- /dev/null +++ b/js/src/dapps/tokenreg/IdentityIcon/identityIcon.css @@ -0,0 +1,23 @@ +/* Copyright 2015, 2016 Ethcore (UK) Ltd. +/* This file is part of Parity. +/* +/* Parity is free software: you can redistribute it and/or modify +/* it under the terms of the GNU General Public License as published by +/* the Free Software Foundation, either version 3 of the License, or +/* (at your option) any later version. +/* +/* Parity is distributed in the hope that it will be useful, +/* but WITHOUT ANY WARRANTY; without even the implied warranty of +/* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the +/* GNU General Public License for more details. +/* +/* You should have received a copy of the GNU General Public License +/* along with Parity. If not, see . +*/ + +.icon { + width: 32px; + height: 32px; + border-radius: 50%; + margin-right: 0.5em; +} diff --git a/js/src/dapps/tokenreg/IdentityIcon/identityIcon.js b/js/src/dapps/tokenreg/IdentityIcon/identityIcon.js new file mode 100644 index 000000000..51f48d46a --- /dev/null +++ b/js/src/dapps/tokenreg/IdentityIcon/identityIcon.js @@ -0,0 +1,36 @@ +// Copyright 2015, 2016 Ethcore (UK) Ltd. +// This file is part of Parity. + +// Parity is free software: you can redistribute it and/or modify +// it under the terms of the GNU General Public License as published by +// the Free Software Foundation, either version 3 of the License, or +// (at your option) any later version. + +// Parity is distributed in the hope that it will be useful, +// but WITHOUT ANY WARRANTY; without even the implied warranty of +// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the +// GNU General Public License for more details. + +// You should have received a copy of the GNU General Public License +// along with Parity. If not, see . + +import React, { Component, PropTypes } from 'react'; + +import { api } from '../parity'; +import styles from './identityIcon.css'; + +export default class IdentityIcon extends Component { + static propTypes = { + address: PropTypes.string.isRequired + } + + render () { + const { address } = this.props; + + return ( + + ); + } +} diff --git a/js/src/dapps/tokenreg/IdentityIcon/index.js b/js/src/dapps/tokenreg/IdentityIcon/index.js new file mode 100644 index 000000000..76c107bfb --- /dev/null +++ b/js/src/dapps/tokenreg/IdentityIcon/index.js @@ -0,0 +1,17 @@ +// Copyright 2015, 2016 Ethcore (UK) Ltd. +// This file is part of Parity. + +// Parity is free software: you can redistribute it and/or modify +// it under the terms of the GNU General Public License as published by +// the Free Software Foundation, either version 3 of the License, or +// (at your option) any later version. + +// Parity is distributed in the hope that it will be useful, +// but WITHOUT ANY WARRANTY; without even the implied warranty of +// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the +// GNU General Public License for more details. + +// You should have received a copy of the GNU General Public License +// along with Parity. If not, see . + +export default from './identityIcon'; diff --git a/js/src/dapps/tokenreg/Inputs/Text/container.js b/js/src/dapps/tokenreg/Inputs/Text/container.js new file mode 100644 index 000000000..450a2b56e --- /dev/null +++ b/js/src/dapps/tokenreg/Inputs/Text/container.js @@ -0,0 +1,37 @@ +// Copyright 2015, 2016 Ethcore (UK) Ltd. +// This file is part of Parity. + +// Parity is free software: you can redistribute it and/or modify +// it under the terms of the GNU General Public License as published by +// the Free Software Foundation, either version 3 of the License, or +// (at your option) any later version. + +// Parity is distributed in the hope that it will be useful, +// but WITHOUT ANY WARRANTY; without even the implied warranty of +// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the +// GNU General Public License for more details. + +// You should have received a copy of the GNU General Public License +// along with Parity. If not, see . + +import React, { Component } from 'react'; +import { connect } from 'react-redux'; + +import InputText from './input-text'; + +class InputTextContainer extends Component { + + render () { + return (); + } +} + +const mapStateToProps = (state) => { + const { contract } = state.status; + + return { contract }; +}; + +export default connect(mapStateToProps)(InputTextContainer); diff --git a/js/src/dapps/tokenreg/Inputs/Text/index.js b/js/src/dapps/tokenreg/Inputs/Text/index.js new file mode 100644 index 000000000..87fbc567e --- /dev/null +++ b/js/src/dapps/tokenreg/Inputs/Text/index.js @@ -0,0 +1,17 @@ +// Copyright 2015, 2016 Ethcore (UK) Ltd. +// This file is part of Parity. + +// Parity is free software: you can redistribute it and/or modify +// it under the terms of the GNU General Public License as published by +// the Free Software Foundation, either version 3 of the License, or +// (at your option) any later version. + +// Parity is distributed in the hope that it will be useful, +// but WITHOUT ANY WARRANTY; without even the implied warranty of +// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the +// GNU General Public License for more details. + +// You should have received a copy of the GNU General Public License +// along with Parity. If not, see . + +export default from './container'; diff --git a/js/src/dapps/tokenreg/Inputs/Text/input-text.js b/js/src/dapps/tokenreg/Inputs/Text/input-text.js new file mode 100644 index 000000000..289c8c37f --- /dev/null +++ b/js/src/dapps/tokenreg/Inputs/Text/input-text.js @@ -0,0 +1,141 @@ +// Copyright 2015, 2016 Ethcore (UK) Ltd. +// This file is part of Parity. + +// Parity is free software: you can redistribute it and/or modify +// it under the terms of the GNU General Public License as published by +// the Free Software Foundation, either version 3 of the License, or +// (at your option) any later version. + +// Parity is distributed in the hope that it will be useful, +// but WITHOUT ANY WARRANTY; without even the implied warranty of +// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the +// GNU General Public License for more details. + +// You should have received a copy of the GNU General Public License +// along with Parity. If not, see . + +import React, { Component, PropTypes } from 'react'; + +import { TextField } from 'material-ui'; +import CheckIcon from 'material-ui/svg-icons/navigation/check'; +import { green500 } from 'material-ui/styles/colors'; + +import Loading from '../../Loading'; + +import { validate } from '../validation'; + +import styles from '../inputs.css'; + +const initState = { + error: null, + value: '', + valid: false, + disabled: false, + loading: false +}; + +export default class InputText extends Component { + + static propTypes = { + validationType: PropTypes.string.isRequired, + onChange: PropTypes.func.isRequired, + onEnter: PropTypes.func, + + floatingLabelText: PropTypes.string, + hintText: PropTypes.string, + + contract: PropTypes.object + } + + state = initState; + + render () { + const { disabled, error } = this.state; + + return ( +
+ + + { this.renderLoading() } + { this.renderIsValid() } +
+ ); + } + + renderLoading () { + if (!this.state.loading) return; + + return ( +
+ +
+ ); + } + + renderIsValid () { + if (this.state.loading || !this.state.valid) return; + + return ( +
+ +
+ ); + } + + onChange = (event) => { + const value = event.target.value; + // So we can focus on the input after async validation + event.persist(); + + const { validationType, contract } = this.props; + + const validation = validate(value, validationType, contract); + + if (validation instanceof Promise) { + this.setState({ disabled: true, loading: true }); + + return validation + .then(validation => { + this.setValidation({ + ...validation, + disabled: false, + loading: false + }); + + event.target.focus(); + }); + } + + this.setValidation(validation); + } + + onKeyDown = (event) => { + if (!this.props.onEnter) return; + if (event.keyCode !== 13) return; + + this.props.onEnter(); + } + + setValidation = (validation) => { + const { value } = validation; + + this.setState({ ...validation }); + + if (validation.valid) { + return this.props.onChange(true, value); + } + + return this.props.onChange(false, value); + } + +} diff --git a/js/src/dapps/tokenreg/Inputs/inputs.css b/js/src/dapps/tokenreg/Inputs/inputs.css new file mode 100644 index 000000000..f8bea1148 --- /dev/null +++ b/js/src/dapps/tokenreg/Inputs/inputs.css @@ -0,0 +1,33 @@ +/* Copyright 2015, 2016 Ethcore (UK) Ltd. +/* This file is part of Parity. +/* +/* Parity is free software: you can redistribute it and/or modify +/* it under the terms of the GNU General Public License as published by +/* the Free Software Foundation, either version 3 of the License, or +/* (at your option) any later version. +/* +/* Parity is distributed in the hope that it will be useful, +/* but WITHOUT ANY WARRANTY; without even the implied warranty of +/* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the +/* GNU General Public License for more details. +/* +/* You should have received a copy of the GNU General Public License +/* along with Parity. If not, see . +*/ + +.input-container { + width: 100%; + position: relative; +} + +.input-loading { + position: absolute; + right: -5px; + top: 20px; +} + +.input-icon { + position: absolute; + right: 5px; + bottom: 10px; +} diff --git a/js/src/dapps/tokenreg/Inputs/validation.js b/js/src/dapps/tokenreg/Inputs/validation.js new file mode 100644 index 000000000..b2e0688a8 --- /dev/null +++ b/js/src/dapps/tokenreg/Inputs/validation.js @@ -0,0 +1,212 @@ +// Copyright 2015, 2016 Ethcore (UK) Ltd. +// This file is part of Parity. + +// Parity is free software: you can redistribute it and/or modify +// it under the terms of the GNU General Public License as published by +// the Free Software Foundation, either version 3 of the License, or +// (at your option) any later version. + +// Parity is distributed in the hope that it will be useful, +// but WITHOUT ANY WARRANTY; without even the implied warranty of +// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the +// GNU General Public License for more details. + +// You should have received a copy of the GNU General Public License +// along with Parity. If not, see . + +import isURL from 'validator/lib/isURL'; + +import { api } from '../parity'; + +import { getTokenTotalSupply } from '../utils'; + +const { + isHex, + isAddressValid, + toChecksumAddress +} = api.util; + +export const ADDRESS_TYPE = 'ADDRESS_TYPE'; +export const TOKEN_ADDRESS_TYPE = 'TOKEN_ADDRESS_TYPE'; +export const SIMPLE_TOKEN_ADDRESS_TYPE = 'SIMPLE_TOKEN_ADDRESS_TYPE'; +export const TLA_TYPE = 'TLA_TYPE'; +export const SIMPLE_TLA_TYPE = 'SIMPLE_TLA_TYPE'; +export const UINT_TYPE = 'UINT_TYPE'; +export const STRING_TYPE = 'STRING_TYPE'; +export const HEX_TYPE = 'HEX_TYPE'; +export const URL_TYPE = 'URL_TYPE'; + +export const ERRORS = { + invalidTLA: 'The TLA should be 3 characters long', + invalidUint: 'Please enter a non-negative integer', + invalidString: 'Please enter at least a character', + invalidAccount: 'Please select an account to transact with', + invalidRecipient: 'Please select an account to send to', + invalidAddress: 'The address is not in the correct format', + invalidTokenAddress: 'The address is not a regular token contract address', + invalidHex: 'Please enter an hexadecimal string (digits and letters from a to z)', + invalidAmount: 'Please enter a positive amount > 0', + invalidTotal: 'The amount is greater than the availale balance', + tlaAlreadyTaken: 'This TLA address is already registered', + addressAlreadyTaken: 'This Token address is already registered', + invalidURL: 'Please enter a valid URL' +}; + +const validateAddress = (address) => { + if (!isAddressValid(address)) { + return { + error: ERRORS.invalidAddress, + valid: false + }; + } + + return { + value: toChecksumAddress(address), + error: null, + valid: true + }; +}; + +const validateTokenAddress = (address, contract, simple) => { + const addressValidation = validateAddress(address); + if (!addressValidation.valid) return addressValidation; + + if (simple) return addressValidation; + + return getTokenTotalSupply(address) + .then(balance => { + if (balance === null) { + return { + error: ERRORS.invalidTokenAddress, + valid: false + }; + } + + return contract.instance + .fromAddress.call({}, [ address ]) + .then(([id, tla, base, name, owner]) => { + if (owner !== '0x0000000000000000000000000000000000000000') { + return { + error: ERRORS.addressAlreadyTaken, + valid: false + }; + } + }); + }) + .then((result) => { + if (result) return result; + return addressValidation; + }); +}; + +const validateTLA = (tla, contract, simple) => { + if (tla.toString().length !== 3) { + return { + error: ERRORS.invalidTLA, + valid: false + }; + } + + const fTLA = tla.toString().toUpperCase(); + + if (simple) { + return { + value: fTLA, + error: null, + valid: true + }; + } + + return contract.instance + .fromTLA.call({}, [ fTLA ]) + .then(([id, address, base, name, owner]) => { + if (owner !== '0x0000000000000000000000000000000000000000') { + return { + error: ERRORS.tlaAlreadyTaken, + valid: false + }; + } + }) + .then((result) => { + if (result) return result; + return { + value: fTLA, + error: null, + valid: true + }; + }); +}; + +const validateUint = (uint) => { + if (!/^\d+$/.test(uint) || parseInt(uint) <= 0) { + return { + error: ERRORS.invalidUint, + valid: false + }; + } + + return { + value: parseInt(uint), + error: null, + valid: true + }; +}; + +const validateString = (string) => { + if (string.toString().length === 0) { + return { + error: ERRORS.invalidString, + valid: false + }; + } + + return { + value: string.toString(), + error: null, + valid: true + }; +}; + +const validateHex = (string) => { + if (!isHex(string.toString())) { + return { + error: ERRORS.invalidHex, + valid: false + }; + } + + return { + value: string.toString(), + error: null, + valid: true + }; +}; + +const validateURL = (string) => { + if (!isURL(string.toString())) { + return { + error: ERRORS.invalidURL, + valid: false + }; + } + + return { + value: string.toString(), + error: null, + valid: true + }; +}; + +export const validate = (value, type, contract) => { + if (type === ADDRESS_TYPE) return validateAddress(value); + if (type === TOKEN_ADDRESS_TYPE) return validateTokenAddress(value, contract); + if (type === SIMPLE_TOKEN_ADDRESS_TYPE) return validateTokenAddress(value, contract, true); + if (type === TLA_TYPE) return validateTLA(value, contract); + if (type === SIMPLE_TLA_TYPE) return validateTLA(value, contract, true); + if (type === UINT_TYPE) return validateUint(value); + if (type === STRING_TYPE) return validateString(value); + if (type === HEX_TYPE) return validateHex(value); + if (type === URL_TYPE) return validateURL(value); + + return { valid: true, error: null }; +}; diff --git a/js/src/dapps/tokenreg/Loading/index.js b/js/src/dapps/tokenreg/Loading/index.js new file mode 100644 index 000000000..0468cab37 --- /dev/null +++ b/js/src/dapps/tokenreg/Loading/index.js @@ -0,0 +1,17 @@ +// Copyright 2015, 2016 Ethcore (UK) Ltd. +// This file is part of Parity. + +// Parity is free software: you can redistribute it and/or modify +// it under the terms of the GNU General Public License as published by +// the Free Software Foundation, either version 3 of the License, or +// (at your option) any later version. + +// Parity is distributed in the hope that it will be useful, +// but WITHOUT ANY WARRANTY; without even the implied warranty of +// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the +// GNU General Public License for more details. + +// You should have received a copy of the GNU General Public License +// along with Parity. If not, see . + +export default from './loading'; diff --git a/js/src/dapps/tokenreg/Loading/loading.css b/js/src/dapps/tokenreg/Loading/loading.css new file mode 100644 index 000000000..dbdb98e42 --- /dev/null +++ b/js/src/dapps/tokenreg/Loading/loading.css @@ -0,0 +1,23 @@ +/* Copyright 2015, 2016 Ethcore (UK) Ltd. +/* This file is part of Parity. +/* +/* Parity is free software: you can redistribute it and/or modify +/* it under the terms of the GNU General Public License as published by +/* the Free Software Foundation, either version 3 of the License, or +/* (at your option) any later version. +/* +/* Parity is distributed in the hope that it will be useful, +/* but WITHOUT ANY WARRANTY; without even the implied warranty of +/* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the +/* GNU General Public License for more details. +/* +/* You should have received a copy of the GNU General Public License +/* along with Parity. If not, see . +*/ + +.loading { + flex: 1; + display: flex; + align-items: center; + justify-content: center; +} diff --git a/js/src/dapps/tokenreg/Loading/loading.js b/js/src/dapps/tokenreg/Loading/loading.js new file mode 100644 index 000000000..3b7619323 --- /dev/null +++ b/js/src/dapps/tokenreg/Loading/loading.js @@ -0,0 +1,34 @@ +// Copyright 2015, 2016 Ethcore (UK) Ltd. +// This file is part of Parity. + +// Parity is free software: you can redistribute it and/or modify +// it under the terms of the GNU General Public License as published by +// the Free Software Foundation, either version 3 of the License, or +// (at your option) any later version. + +// Parity is distributed in the hope that it will be useful, +// but WITHOUT ANY WARRANTY; without even the implied warranty of +// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the +// GNU General Public License for more details. + +// You should have received a copy of the GNU General Public License +// along with Parity. If not, see . + +import React, { Component, PropTypes } from 'react'; +import CircularProgress from 'material-ui/CircularProgress'; + +import styles from './loading.css'; + +export default class Loading extends Component { + static propTypes = { + size: PropTypes.number + }; + + render () { + return ( +
+ +
+ ); + } +} diff --git a/js/src/dapps/tokenreg/Status/actions.js b/js/src/dapps/tokenreg/Status/actions.js new file mode 100644 index 000000000..a479179b2 --- /dev/null +++ b/js/src/dapps/tokenreg/Status/actions.js @@ -0,0 +1,202 @@ +// Copyright 2015, 2016 Ethcore (UK) Ltd. +// This file is part of Parity. + +// Parity is free software: you can redistribute it and/or modify +// it under the terms of the GNU General Public License as published by +// the Free Software Foundation, either version 3 of the License, or +// (at your option) any later version. + +// Parity is distributed in the hope that it will be useful, +// but WITHOUT ANY WARRANTY; without even the implied warranty of +// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the +// GNU General Public License for more details. + +// You should have received a copy of the GNU General Public License +// along with Parity. If not, see . + +import { + registry as registryAbi, + tokenreg as tokenregAbi, + githubhint as githubhintAbi +} from '../../../contracts/abi'; + +import { loadToken, setTokenPending, deleteToken, setTokenData } from '../Tokens/actions'; + +const { api } = window.parity; + +export const SET_LOADING = 'SET_LOADING'; +export const setLoading = (isLoading) => ({ + type: SET_LOADING, + isLoading +}); + +export const FIND_CONTRACT = 'FIND_CONTRACT'; +export const loadContract = () => (dispatch) => { + dispatch(setLoading(true)); + + api.ethcore + .registryAddress() + .then((registryAddress) => { + console.log(`registry found at ${registryAddress}`); + const registry = api.newContract(registryAbi, registryAddress).instance; + + return Promise.all([ + registry.getAddress.call({}, [api.util.sha3('tokenreg'), 'A']), + registry.getAddress.call({}, [api.util.sha3('githubhint'), 'A']) + ]); + }) + .then(([ tokenregAddress, githubhintAddress ]) => { + console.log(`tokenreg was found at ${tokenregAddress}`); + + const tokenregContract = api + .newContract(tokenregAbi, tokenregAddress); + + const githubhintContract = api + .newContract(githubhintAbi, githubhintAddress); + + dispatch(setContractDetails({ + address: tokenregAddress, + instance: tokenregContract.instance, + raw: tokenregContract + })); + + dispatch(setGithubhintDetails({ + address: githubhintAddress, + instance: githubhintContract.instance, + raw: githubhintContract + })); + + dispatch(loadContractDetails()); + dispatch(subscribeEvents()); + }) + .catch((error) => { + console.error('loadContract error', error); + }); +}; + +export const LOAD_CONTRACT_DETAILS = 'LOAD_CONTRACT_DETAILS'; +export const loadContractDetails = () => (dispatch, getState) => { + const state = getState(); + + const instance = state.status.contract.instance; + + Promise + .all([ + api.personal.listAccounts(), + instance.owner.call(), + instance.fee.call() + ]) + .then(([accounts, owner, fee]) => { + console.log(`owner as ${owner}, fee set at ${fee.toFormat()}`); + + const isOwner = accounts.filter(a => a === owner).length > 0; + + dispatch(setContractDetails({ + fee, + owner, + isOwner + })); + + dispatch(setLoading(false)); + }) + .catch((error) => { + console.error('loadContractDetails error', error); + }); +}; + +export const SET_CONTRACT_DETAILS = 'SET_CONTRACT_DETAILS'; +export const setContractDetails = (details) => ({ + type: SET_CONTRACT_DETAILS, + details +}); + +export const SET_GITHUBHINT_CONTRACT = 'SET_GITHUBHINT_CONTRACT'; +export const setGithubhintDetails = (details) => ({ + type: SET_GITHUBHINT_CONTRACT, + details +}); + +export const subscribeEvents = () => (dispatch, getState) => { + const state = getState(); + + const contract = state.status.contract.raw; + const previousSubscriptionId = state.status.subscriptionId; + + if (previousSubscriptionId) { + contract.unsubscribe(previousSubscriptionId); + } + + contract + .subscribe(null, { + fromBlock: 'latest', + toBlock: 'pending', + limit: 50 + }, (error, logs) => { + if (error) { + console.error('setupFilters', error); + return; + } + + if (!logs || logs.length === 0) return; + + logs.forEach(log => { + const event = log.event; + const type = log.type; + const params = log.params; + + if (event === 'Registered' && type === 'pending') { + return dispatch(setTokenData(params.id.toNumber(), { + tla: '...', + base: -1, + address: params.addr, + name: params.name, + isPending: true + })); + } + + if (event === 'Registered' && type === 'mined') { + return dispatch(loadToken(params.id.toNumber())); + } + + if (event === 'Unregistered' && type === 'pending') { + return dispatch(setTokenPending(params.id.toNumber(), true)); + } + + if (event === 'Unregistered' && type === 'mined') { + return dispatch(deleteToken(params.id.toNumber())); + } + + if (event === 'MetaChanged' && type === 'pending') { + return dispatch(setTokenData( + params.id.toNumber(), + { metaPending: true, metaMined: false } + )); + } + + if (event === 'MetaChanged' && type === 'mined') { + setTimeout(() => { + dispatch(setTokenData( + params.id.toNumber(), + { metaPending: false, metaMined: false } + )); + }, 5000); + + return dispatch(setTokenData( + params.id.toNumber(), + { metaPending: false, metaMined: true } + )); + } + + console.log('new log event', log); + }); + }) + .then((subscriptionId) => { + dispatch(setSubscriptionId(subscriptionId)); + }); +}; + +export const SET_SUBSCRIPTION_ID = 'SET_SUBSCRIPTION_ID'; +export const setSubscriptionId = subscriptionId => ({ + type: SET_SUBSCRIPTION_ID, + subscriptionId +}); diff --git a/js/src/dapps/tokenreg/Status/index.js b/js/src/dapps/tokenreg/Status/index.js new file mode 100644 index 000000000..44079b224 --- /dev/null +++ b/js/src/dapps/tokenreg/Status/index.js @@ -0,0 +1,17 @@ +// Copyright 2015, 2016 Ethcore (UK) Ltd. +// This file is part of Parity. + +// Parity is free software: you can redistribute it and/or modify +// it under the terms of the GNU General Public License as published by +// the Free Software Foundation, either version 3 of the License, or +// (at your option) any later version. + +// Parity is distributed in the hope that it will be useful, +// but WITHOUT ANY WARRANTY; without even the implied warranty of +// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the +// GNU General Public License for more details. + +// You should have received a copy of the GNU General Public License +// along with Parity. If not, see . + +export default from './status'; diff --git a/js/src/dapps/tokenreg/Status/reducer.js b/js/src/dapps/tokenreg/Status/reducer.js new file mode 100644 index 000000000..7b018bd85 --- /dev/null +++ b/js/src/dapps/tokenreg/Status/reducer.js @@ -0,0 +1,65 @@ +// Copyright 2015, 2016 Ethcore (UK) Ltd. +// This file is part of Parity. + +// Parity is free software: you can redistribute it and/or modify +// it under the terms of the GNU General Public License as published by +// the Free Software Foundation, either version 3 of the License, or +// (at your option) any later version. + +// Parity is distributed in the hope that it will be useful, +// but WITHOUT ANY WARRANTY; without even the implied warranty of +// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the +// GNU General Public License for more details. + +// You should have received a copy of the GNU General Public License +// along with Parity. If not, see . + +import { + SET_LOADING, + SET_CONTRACT_DETAILS, + SET_GITHUBHINT_CONTRACT, + SET_SUBSCRIPTION_ID +} from './actions'; + +const initialState = { + isLoading: true, + subscriptionId: null, + contract: { + addres: null, + instance: null, + raw: null, + owner: null, + isOwner: false, + fee: null + }, + githubhint: { + address: null, + instance: null, + raw: null + } +}; + +export default (state = initialState, action) => { + switch (action.type) { + case SET_LOADING: + return { ...state, isLoading: action.isLoading }; + + case SET_SUBSCRIPTION_ID: + return { ...state, subscriptionId: action.subscriptionId }; + + case SET_CONTRACT_DETAILS: + return { ...state, contract: { + ...state.contract, + ...action.details + } }; + + case SET_GITHUBHINT_CONTRACT: + return { ...state, githubhint: { + ...state.githubhint, + ...action.details + } }; + + default: + return state; + } +}; diff --git a/js/src/dapps/tokenreg/Status/status.css b/js/src/dapps/tokenreg/Status/status.css new file mode 100644 index 000000000..27ef53607 --- /dev/null +++ b/js/src/dapps/tokenreg/Status/status.css @@ -0,0 +1,36 @@ +/* Copyright 2015, 2016 Ethcore (UK) Ltd. +/* This file is part of Parity. +/* +/* Parity is free software: you can redistribute it and/or modify +/* it under the terms of the GNU General Public License as published by +/* the Free Software Foundation, either version 3 of the License, or +/* (at your option) any later version. +/* +/* Parity is distributed in the hope that it will be useful, +/* but WITHOUT ANY WARRANTY; without even the implied warranty of +/* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the +/* GNU General Public License for more details. +/* +/* You should have received a copy of the GNU General Public License +/* along with Parity. If not, see . +*/ + +.status { + background: rgba(46, 204, 113, 0.85); + color: rgba(255, 255, 255, 1); + padding: 4em 0 2em 0; + + display: flex; + flex-direction: column; + align-items: center; + justify-content: center; + + width: 100%; +} + +.title { + font-size: 3rem; + font-weight: 300; + margin-top: 0; + text-transform: uppercase; +} diff --git a/js/src/dapps/tokenreg/Status/status.js b/js/src/dapps/tokenreg/Status/status.js new file mode 100644 index 000000000..f8c7b347a --- /dev/null +++ b/js/src/dapps/tokenreg/Status/status.js @@ -0,0 +1,50 @@ +// Copyright 2015, 2016 Ethcore (UK) Ltd. +// This file is part of Parity. + +// Parity is free software: you can redistribute it and/or modify +// it under the terms of the GNU General Public License as published by +// the Free Software Foundation, either version 3 of the License, or +// (at your option) any later version. + +// Parity is distributed in the hope that it will be useful, +// but WITHOUT ANY WARRANTY; without even the implied warranty of +// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the +// GNU General Public License for more details. + +// You should have received a copy of the GNU General Public License +// along with Parity. If not, see . + +import React, { Component, PropTypes } from 'react'; + +import Chip from '../Chip'; + +import styles from './status.css'; + +const { api } = window.parity; + +export default class Status extends Component { + static propTypes = { + address: PropTypes.string.isRequired, + fee: PropTypes.object.isRequired + }; + + render () { + const { address, fee } = this.props; + + return ( +
+

Token Registry

+ + + + +
+ ); + } +} diff --git a/js/src/dapps/tokenreg/Tokens/Token/add-meta.js b/js/src/dapps/tokenreg/Tokens/Token/add-meta.js new file mode 100644 index 000000000..09e65954d --- /dev/null +++ b/js/src/dapps/tokenreg/Tokens/Token/add-meta.js @@ -0,0 +1,193 @@ +// Copyright 2015, 2016 Ethcore (UK) Ltd. +// This file is part of Parity. + +// Parity is free software: you can redistribute it and/or modify +// it under the terms of the GNU General Public License as published by +// the Free Software Foundation, either version 3 of the License, or +// (at your option) any later version. + +// Parity is distributed in the hope that it will be useful, +// but WITHOUT ANY WARRANTY; without even the implied warranty of +// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the +// GNU General Public License for more details. + +// You should have received a copy of the GNU General Public License +// along with Parity. If not, see . + +import React, { Component, PropTypes } from 'react'; +import { Dialog, RaisedButton, FlatButton, SelectField, MenuItem } from 'material-ui'; +import AddIcon from 'material-ui/svg-icons/content/add'; + +import InputText from '../../Inputs/Text'; +import { ADDRESS_TYPE } from '../../Inputs/validation'; + +import styles from './token.css'; + +import { metaDataKeys } from '../../constants'; + +const initState = { + showDialog: false, + complete: false, + metaKeyIndex: 0, + + form: { + valid: false, + value: '' + } +}; + +export default class AddMeta extends Component { + static propTypes = { + isTokenOwner: PropTypes.bool, + handleAddMeta: PropTypes.func, + index: PropTypes.number + }; + + state = initState; + + render () { + if (!this.props.isTokenOwner) return null; + + return (
+ } + primary + fullWidth + onTouchTap={ this.onShowDialog } /> + + + { this.renderContent() } + +
); + } + + renderActions () { + const { complete } = this.state; + + if (complete) { + return ( + + ); + } + + const isValid = this.state.form.valid; + + return ([ + , + + ]); + } + + renderContent () { + const { complete } = this.state; + + if (complete) return this.renderComplete(); + return this.renderForm(); + } + + renderComplete () { + if (metaDataKeys[this.state.metaKeyIndex].value === 'IMG') { + return (
+

+ Your transactions has been posted. + Two transactions are needed to add an Image. + Please visit the Parity Signer to authenticate the transfer.

+
); + } + return (
+

Your transaction has been posted. Please visit the Parity Signer to authenticate the transfer.

+
); + } + + renderForm () { + const selectedMeta = metaDataKeys[this.state.metaKeyIndex]; + + return ( +
+ + + { this.renderMetaKeyItems() } + + + + +
+ ); + } + + renderMetaKeyItems () { + return metaDataKeys.map((key, index) => ( + + )); + } + + onShowDialog = () => { + this.setState({ showDialog: true }); + } + + onClose = () => { + this.setState(initState); + } + + onAdd = () => { + const { index } = this.props; + + const keyIndex = this.state.metaKeyIndex; + const key = metaDataKeys[keyIndex].value; + + this.props.handleAddMeta( + index, + key, + this.state.form.value + ); + + this.setState({ complete: true }); + } + + onChange = (valid, value) => { + this.setState({ + form: { + valid, value + } + }); + } + + onMetaKeyChange = (event, metaKeyIndex) => { + this.setState({ metaKeyIndex, form: { + ...[this.state.form], + valid: false, + value: '' + } }); + } + +} diff --git a/js/src/dapps/tokenreg/Tokens/Token/index.js b/js/src/dapps/tokenreg/Tokens/Token/index.js new file mode 100644 index 000000000..4b822b4bd --- /dev/null +++ b/js/src/dapps/tokenreg/Tokens/Token/index.js @@ -0,0 +1,17 @@ +// Copyright 2015, 2016 Ethcore (UK) Ltd. +// This file is part of Parity. + +// Parity is free software: you can redistribute it and/or modify +// it under the terms of the GNU General Public License as published by +// the Free Software Foundation, either version 3 of the License, or +// (at your option) any later version. + +// Parity is distributed in the hope that it will be useful, +// but WITHOUT ANY WARRANTY; without even the implied warranty of +// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the +// GNU General Public License for more details. + +// You should have received a copy of the GNU General Public License +// along with Parity. If not, see . + +export default from './token'; diff --git a/js/src/dapps/tokenreg/Tokens/Token/token.css b/js/src/dapps/tokenreg/Tokens/Token/token.css new file mode 100644 index 000000000..cfd01e3d6 --- /dev/null +++ b/js/src/dapps/tokenreg/Tokens/Token/token.css @@ -0,0 +1,140 @@ +/* Copyright 2015, 2016 Ethcore (UK) Ltd. +/* This file is part of Parity. +/* +/* Parity is free software: you can redistribute it and/or modify +/* it under the terms of the GNU General Public License as published by +/* the Free Software Foundation, either version 3 of the License, or +/* (at your option) any later version. +/* +/* Parity is distributed in the hope that it will be useful, +/* but WITHOUT ANY WARRANTY; without even the implied warranty of +/* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the +/* GNU General Public License for more details. +/* +/* You should have received a copy of the GNU General Public License +/* along with Parity. If not, see . +*/ + +.token{ + position: relative; + display: flex; + + width: 20rem; + margin: 1rem; + padding: 1rem; + padding-bottom: 1.5rem; +} + +.token-container, .token-content, .token-meta { + width: 100%; + + display: flex; + flex-direction: column; + align-items: center; +} + +.token-content, .token-meta { + z-index: 50; +} + +.token-bg { + position: absolute; + top: 0; + bottom: 0; + right: 0; + left: 0; + -webkit-filter: blur(5px); + filter: blur(5px); + background-color: rgba(255, 255, 255, 0.85); +} + +.token-container { + flex: 1; +} + +.full-width .token-container { + flex-direction: row; + align-items: flex-start; +} + +.full-width .token-content { + width: 20rem; + margin-right: 1rem; +} + +.token-content > div, .token-meta > div { + max-width: 100%; +} + +.loading { + margin: 1rem 0; +} + +.name { + padding-bottom: 0.75rem; +} + +.title { + font-size: 2rem; + padding: 0 0 0.5rem; +} + +.meta-query { + font-size: 0.9rem; + margin-top: 1.5rem; + margin-bottom: 0; +} + +.meta-key { + font-weight: bold; +} + +.meta-value { + margin-top: 0.5rem; + max-width: 100%; + overflow-wrap: break-word; +} + +.meta-info { + margin-top: 1.5rem; + margin-bottom: -0.5rem; + font-size: 0.9rem; + color: gray; +} + +.meta-image { + width: 100%; + margin-top: 1rem; +} + +.meta-image img { + width: 100%; +} + +.meta-form { + width: 100%; +} + +.unregister { + margin-top: 1rem; +} + +.add-meta { + margin-top: 1rem; + width: 100%; +} + +.pending { + position: absolute; + top: 0; + bottom: 0; + right: 0; + left: 0; + z-index: 100; + background-color: rgba(0, 0, 0, 0.35); +} + +.dialog h3 { + color: rgba(50, 100, 150, 1) !important; + text-transform: uppercase; +} diff --git a/js/src/dapps/tokenreg/Tokens/Token/token.js b/js/src/dapps/tokenreg/Tokens/Token/token.js new file mode 100644 index 000000000..bee925689 --- /dev/null +++ b/js/src/dapps/tokenreg/Tokens/Token/token.js @@ -0,0 +1,338 @@ +// Copyright 2015, 2016 Ethcore (UK) Ltd. +// This file is part of Parity. + +// Parity is free software: you can redistribute it and/or modify +// it under the terms of the GNU General Public License as published by +// the Free Software Foundation, either version 3 of the License, or +// (at your option) any later version. + +// Parity is distributed in the hope that it will be useful, +// but WITHOUT ANY WARRANTY; without even the implied warranty of +// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the +// GNU General Public License for more details. + +// You should have received a copy of the GNU General Public License +// along with Parity. If not, see . + +import React, { Component, PropTypes } from 'react'; +import Paper from 'material-ui/Paper'; +import { RaisedButton, SelectField, MenuItem } from 'material-ui'; + +import FindIcon from 'material-ui/svg-icons/action/find-in-page'; +import DeleteIcon from 'material-ui/svg-icons/action/delete'; + +import Loading from '../../Loading'; +import Chip from '../../Chip'; +import AddMeta from './add-meta'; + +import styles from './token.css'; + +import { metaDataKeys } from '../../constants'; + +import { api } from '../../parity'; + +export default class Token extends Component { + static propTypes = { + handleMetaLookup: PropTypes.func.isRequired, + address: PropTypes.string.isRequired, + name: PropTypes.string.isRequired, + index: PropTypes.number.isRequired, + owner: PropTypes.string.isRequired, + + handleAddMeta: PropTypes.func, + handleUnregister: PropTypes.func, + + tla: PropTypes.string, + base: PropTypes.number, + totalSupply: PropTypes.number, + meta: PropTypes.object, + isMetaLoading: PropTypes.bool, + ownerAccountInfo: PropTypes.shape({ + name: PropTypes.string, + meta: PropTypes.object + }), + metaPending: PropTypes.bool, + metaMined: PropTypes.bool, + isLoading: PropTypes.bool, + isPending: PropTypes.bool, + isTokenOwner: PropTypes.bool.isRequired, + + fullWidth: PropTypes.bool + }; + + state = { + metaKeyIndex: 0 + }; + + render () { + const { isLoading, fullWidth } = this.props; + + if (isLoading) { + return ( +
+ +
+ ); + } + + if (fullWidth) { + return (
+ { this.renderContent() } +
); + } + + return (
+ +
+ { this.renderContent() } + +
); + } + + renderContent () { + const { address, tla, base, name, meta, owner, totalSupply } = this.props; + + return (
+ { this.renderIsPending() } + +
+
{ tla }
+
"{ name }"
+ + { this.renderBase(base) } + { this.renderTotalSupply(totalSupply, base, tla) } + { this.renderAddress(address) } + { this.renderOwner(owner) } +
+ +
+
+ + + { this.renderMetaKeyItems() } + + + + } + primary + fullWidth + onTouchTap={ this.onMetaLookup } /> +
+ + { this.renderMeta(meta) } + { this.renderAddMeta() } + { this.renderUnregister() } +
+ + { this.renderMetaPending() } + { this.renderMetaMined() } +
); + } + + renderMetaKeyItems () { + return metaDataKeys.map((key, index) => ( + + )); + } + + renderBase (base) { + if (!base || base < 0) return null; + return ( + + ); + } + + renderAddress (address) { + if (!address) return null; + return ( + + ); + } + + renderTotalSupply (totalSupply, base, tla) { + const balance = Math.round((totalSupply / base) * 100) / 100; + + return ( + + ); + } + + renderOwner (owner) { + if (!owner) return null; + + const ownerInfo = this.props.ownerAccountInfo; + + const displayValue = (ownerInfo && ownerInfo.name) + ? ownerInfo.name + : owner; + + return ( + + ); + } + + renderIsPending () { + const { isPending } = this.props; + + if (!isPending) { + return null; + } + + return ( +
+ ); + } + + renderAddMeta () { + if (!this.props.isTokenOwner) { + return null; + } + + return ( + + ); + } + + renderUnregister () { + if (!this.props.isTokenOwner) { + return null; + } + + return ( + } + secondary + fullWidth + onTouchTap={ this.onUnregister } /> + ); + } + + renderMeta (meta) { + const isMetaLoading = this.props.isMetaLoading; + + if (isMetaLoading) { + return (
+ +
); + } + + if (!meta) return; + + const metaData = metaDataKeys.find(m => m.value === meta.query); + + if (!meta.value) { + return (
+

+ No + { metaData.label.toLowerCase() } + meta-data... +

+
); + } + + if (meta.query === 'IMG') { + const imageHash = meta.value.replace(/^0x/, ''); + + return (
+

+ + { metaData.label } + meta-data: +

+
+ +
+
); + } + + if (meta.query === 'A') { + const address = meta.value.slice(0, 42); + + return (
+

+ + { metaData.label } + meta-data: +

+

+ { api.util.toChecksumAddress(address) } +

+
); + } + + return (
+

+ + { metaData.label } + meta-data: +

+

{ meta.value }

+
); + } + + renderMetaPending () { + const isMetaPending = this.props.metaPending; + if (!isMetaPending) return; + + return (
+

+ Meta-Data pending... +

+
); + } + + renderMetaMined () { + const isMetaMined = this.props.metaMined; + if (!isMetaMined) return; + + return (
+

+ Meta-Data saved on the blockchain! +

+
); + } + + onUnregister = () => { + const index = this.props.index; + this.props.handleUnregister(index); + } + + onMetaLookup = () => { + const keyIndex = this.state.metaKeyIndex; + const key = metaDataKeys[keyIndex].value; + const index = this.props.index; + + this.props.handleMetaLookup(index, key); + } + + onMetaKeyChange = (event, metaKeyIndex) => { + this.setState({ metaKeyIndex }); + } +} diff --git a/js/src/dapps/tokenreg/Tokens/actions.js b/js/src/dapps/tokenreg/Tokens/actions.js new file mode 100644 index 000000000..7953bb241 --- /dev/null +++ b/js/src/dapps/tokenreg/Tokens/actions.js @@ -0,0 +1,262 @@ +// Copyright 2015, 2016 Ethcore (UK) Ltd. +// This file is part of Parity. + +// Parity is free software: you can redistribute it and/or modify +// it under the terms of the GNU General Public License as published by +// the Free Software Foundation, either version 3 of the License, or +// (at your option) any later version. + +// Parity is distributed in the hope that it will be useful, +// but WITHOUT ANY WARRANTY; without even the implied warranty of +// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the +// GNU General Public License for more details. + +// You should have received a copy of the GNU General Public License +// along with Parity. If not, see . + +import { getTokenTotalSupply } from '../utils'; + +const { bytesToHex } = window.parity.api.util; + +export const SET_TOKENS_LOADING = 'SET_TOKENS_LOADING'; +export const setTokensLoading = (isLoading) => ({ + type: SET_TOKENS_LOADING, + isLoading +}); + +export const SET_TOKEN_COUNT = 'SET_TOKEN_COUNT'; +export const setTokenCount = (tokenCount) => ({ + type: SET_TOKEN_COUNT, + tokenCount +}); + +export const SET_TOKEN_DATA = 'SET_TOKEN_DATA'; +export const setTokenData = (index, tokenData) => ({ + type: SET_TOKEN_DATA, + index, tokenData +}); + +export const SET_TOKEN_META = 'SET_TOKEN_META'; +export const setTokenMeta = (index, meta) => ({ + type: SET_TOKEN_META, + index, meta +}); + +export const SET_TOKEN_LOADING = 'SET_TOKEN_LOADING'; +export const setTokenLoading = (index, isLoading) => ({ + type: SET_TOKEN_LOADING, + index, isLoading +}); + +export const SET_TOKEN_META_LOADING = 'SET_TOKEN_META_LOADING'; +export const setTokenMetaLoading = (index, isMetaLoading) => ({ + type: SET_TOKEN_META_LOADING, + index, isMetaLoading +}); + +export const SET_TOKEN_PENDING = 'SET_TOKEN_PENDING'; +export const setTokenPending = (index, isPending) => ({ + type: SET_TOKEN_PENDING, + index, isPending +}); + +export const DELETE_TOKEN = 'DELETE_TOKEN'; +export const deleteToken = (index) => ({ + type: DELETE_TOKEN, + index +}); + +export const loadTokens = () => (dispatch, getState) => { + console.log('loading tokens...'); + + const state = getState(); + const contractInstance = state.status.contract.instance; + + dispatch(setTokensLoading(true)); + + contractInstance + .tokenCount + .call() + .then((count) => { + const tokenCount = parseInt(count); + console.log(`token count: ${tokenCount}`); + dispatch(setTokenCount(tokenCount)); + + for (let i = 0; i < tokenCount; i++) { + dispatch(loadToken(i)); + } + + dispatch(setTokensLoading(false)); + }) + .catch((e) => { + console.error('loadTokens error', e); + }); +}; + +export const loadToken = (index) => (dispatch, getState) => { + console.log('loading token', index); + + const state = getState(); + const contractInstance = state.status.contract.instance; + + const userAccounts = state.accounts.list; + const accountsInfo = state.accounts.accountsInfo; + + dispatch(setTokenLoading(index, true)); + + contractInstance + .token + .call({}, [ parseInt(index) ]) + .then((result) => { + const tokenOwner = result[4]; + + const isTokenOwner = userAccounts + .filter(a => a.address === tokenOwner) + .length > 0; + + const data = { + index: parseInt(index), + address: result[0], + tla: result[1], + base: result[2].toNumber(), + name: result[3], + owner: tokenOwner, + ownerAccountInfo: accountsInfo[tokenOwner], + isPending: false, + isTokenOwner + }; + + return data; + }) + .then(data => { + return getTokenTotalSupply(data.address) + .then(totalSupply => { + data.totalSupply = totalSupply; + return data; + }); + }) + .then(data => { + // If no total supply, must not be a proper token + if (data.totalSupply === null) { + dispatch(setTokenData(index, null)); + dispatch(setTokenLoading(index, false)); + return; + } + + data.totalSupply = data.totalSupply.toNumber(); + console.log(`token loaded: #${index}`, data); + dispatch(setTokenData(index, data)); + dispatch(setTokenLoading(index, false)); + }) + .catch((e) => { + dispatch(setTokenData(index, null)); + dispatch(setTokenLoading(index, false)); + + if (!e instanceof TypeError) { + console.error(`loadToken #${index} error`, e); + } + }); +}; + +export const queryTokenMeta = (index, query) => (dispatch, getState) => { + console.log('loading token meta', index, query); + + const state = getState(); + const contractInstance = state.status.contract.instance; + + const startDate = Date.now(); + dispatch(setTokenMetaLoading(index, true)); + + contractInstance + .meta + .call({}, [ index, query ]) + .then((value) => { + const meta = { + query, + value: value.find(v => v !== 0) ? bytesToHex(value) : null + }; + + console.log(`token meta loaded: #${index}`, value); + dispatch(setTokenMeta(index, meta)); + + setTimeout(() => { + dispatch(setTokenMetaLoading(index, false)); + }, 500 - (Date.now() - startDate)); + }) + .catch((e) => { + console.error(`loadToken #${index} error`, e); + }); +}; + +export const addTokenMeta = (index, key, value) => (dispatch, getState) => { + console.log('add token meta', index, key, value); + + const state = getState(); + const contractInstance = state.status.contract.instance; + const token = state.tokens.tokens.find(t => t.index === index); + + const options = { from: token.owner }; + const values = [ index, key, value ]; + + contractInstance + .setMeta + .estimateGas(options, values) + .then((gasEstimate) => { + options.gas = gasEstimate.mul(1.2).toFixed(0); + console.log(`addTokenMeta: gas estimated as ${gasEstimate.toFixed(0)} setting to ${options.gas}`); + + return contractInstance.setMeta.postTransaction(options, values); + }) + .catch((e) => { + console.error(`addTokenMeta: #${index} error`, e); + }); +}; + +export const addGithubhintURL = (from, key, url) => (dispatch, getState) => { + console.log('add githubhint url', key, url); + + const state = getState(); + const contractInstance = state.status.githubhint.instance; + + const options = { from }; + + const values = [ key, url ]; + + contractInstance + .hintURL + .estimateGas(options, values) + .then((gasEstimate) => { + options.gas = gasEstimate.mul(1.2).toFixed(0); + console.log(`transfer: gas estimated as ${gasEstimate.toFixed(0)} setting to ${options.gas}`); + + return contractInstance.hintURL.postTransaction(options, values); + }) + .catch((e) => { + console.error('addGithubhintURL error', e); + }); +}; + +export const unregisterToken = (index) => (dispatch, getState) => { + console.log('unregistering token', index); + + const state = getState(); + const contractInstance = state.status.contract.instance; + + const values = [ index ]; + const options = { + from: state.accounts.selected.address + }; + + contractInstance + .unregister + .estimateGas(options, values) + .then((gasEstimate) => { + options.gas = gasEstimate.mul(1.2).toFixed(0); + console.log(`transfer: gas estimated as ${gasEstimate.toFixed(0)} setting to ${options.gas}`); + + return contractInstance.unregister.postTransaction(options, values); + }) + .catch((e) => { + console.error(`unregisterToken #${index} error`, e); + }); +}; diff --git a/js/src/dapps/tokenreg/Tokens/container.js b/js/src/dapps/tokenreg/Tokens/container.js new file mode 100644 index 000000000..b6171c2cc --- /dev/null +++ b/js/src/dapps/tokenreg/Tokens/container.js @@ -0,0 +1,80 @@ +// Copyright 2015, 2016 Ethcore (UK) Ltd. +// This file is part of Parity. + +// Parity is free software: you can redistribute it and/or modify +// it under the terms of the GNU General Public License as published by +// the Free Software Foundation, either version 3 of the License, or +// (at your option) any later version. + +// Parity is distributed in the hope that it will be useful, +// but WITHOUT ANY WARRANTY; without even the implied warranty of +// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the +// GNU General Public License for more details. + +// You should have received a copy of the GNU General Public License +// along with Parity. If not, see . + +import React, { Component, PropTypes } from 'react'; +import { connect } from 'react-redux'; + +import Tokens from './tokens'; + +import { loadTokens, queryTokenMeta, unregisterToken, addTokenMeta } from './actions'; + +class TokensContainer extends Component { + static propTypes = { + isOwner: PropTypes.bool, + isLoading: PropTypes.bool, + tokens: PropTypes.array, + tokenCount: PropTypes.number, + onLoadTokens: PropTypes.func, + accounts: PropTypes.array + }; + + componentDidMount () { + this.props.onLoadTokens(); + } + + render () { + console.log(this.props); + return ( + + ); + } +} + +const mapStateToProps = (state) => { + const { list } = state.accounts; + const { isLoading, tokens, tokenCount } = state.tokens; + + const { isOwner } = state.status.contract; + + return { isLoading, tokens, tokenCount, isOwner, accounts: list }; +}; + +const mapDispatchToProps = (dispatch) => { + return { + onLoadTokens: () => { + dispatch(loadTokens()); + }, + + handleMetaLookup: (index, query) => { + dispatch(queryTokenMeta(index, query)); + }, + + handleUnregister: (index) => { + dispatch(unregisterToken(index)); + }, + + handleAddMeta: (index, key, value) => { + dispatch(addTokenMeta(index, key, value)); + } + }; +}; + +export default connect( + mapStateToProps, + mapDispatchToProps +)(TokensContainer); diff --git a/js/src/dapps/tokenreg/Tokens/index.js b/js/src/dapps/tokenreg/Tokens/index.js new file mode 100644 index 000000000..87fbc567e --- /dev/null +++ b/js/src/dapps/tokenreg/Tokens/index.js @@ -0,0 +1,17 @@ +// Copyright 2015, 2016 Ethcore (UK) Ltd. +// This file is part of Parity. + +// Parity is free software: you can redistribute it and/or modify +// it under the terms of the GNU General Public License as published by +// the Free Software Foundation, either version 3 of the License, or +// (at your option) any later version. + +// Parity is distributed in the hope that it will be useful, +// but WITHOUT ANY WARRANTY; without even the implied warranty of +// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the +// GNU General Public License for more details. + +// You should have received a copy of the GNU General Public License +// along with Parity. If not, see . + +export default from './container'; diff --git a/js/src/dapps/tokenreg/Tokens/reducer.js b/js/src/dapps/tokenreg/Tokens/reducer.js new file mode 100644 index 000000000..21952105c --- /dev/null +++ b/js/src/dapps/tokenreg/Tokens/reducer.js @@ -0,0 +1,114 @@ +// Copyright 2015, 2016 Ethcore (UK) Ltd. +// This file is part of Parity. + +// Parity is free software: you can redistribute it and/or modify +// it under the terms of the GNU General Public License as published by +// the Free Software Foundation, either version 3 of the License, or +// (at your option) any later version. + +// Parity is distributed in the hope that it will be useful, +// but WITHOUT ANY WARRANTY; without even the implied warranty of +// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the +// GNU General Public License for more details. + +// You should have received a copy of the GNU General Public License +// along with Parity. If not, see . + +import { + SET_TOKENS_LOADING, + SET_TOKEN_COUNT, + SET_TOKEN_DATA, + SET_TOKEN_META, + SET_TOKEN_LOADING, + SET_TOKEN_META_LOADING, + SET_TOKEN_PENDING, + DELETE_TOKEN +} from './actions'; + +const initialState = { + isLoading: true, + tokens: [], + tokenCount: 0 +}; + +export default (state = initialState, action) => { + switch (action.type) { + case SET_TOKENS_LOADING: + return { ...state, isLoading: action.isLoading }; + + case SET_TOKEN_COUNT: + return { ...state, tokenCount: action.tokenCount }; + + case SET_TOKEN_DATA: { + const index = action.index; + const tokens = [].concat(state.tokens); + + tokens[index] = { + ...tokens[index], + ...action.tokenData + }; + + return { ...state, tokens: tokens }; + } + + case SET_TOKEN_META: { + const index = action.index; + const tokens = [].concat(state.tokens); + + tokens[index] = { + ...tokens[index], + meta: action.meta + }; + + return { ...state, tokens: tokens }; + } + + case SET_TOKEN_LOADING: { + const index = action.index; + const tokens = [].concat(state.tokens); + + tokens[index] = { + ...tokens[index], + isLoading: action.isLoading + }; + + return { ...state, tokens: tokens }; + } + + case SET_TOKEN_META_LOADING: { + const index = action.index; + const tokens = [].concat(state.tokens); + + tokens[index] = { + ...tokens[index], + isMetaLoading: action.isMetaLoading + }; + + return { ...state, tokens: tokens }; + } + + case SET_TOKEN_PENDING: { + const index = action.index; + const tokens = [].concat(state.tokens); + + tokens[index] = { + ...tokens[index], + isPending: action.isPending + }; + + return { ...state, tokens: tokens }; + } + + case DELETE_TOKEN: { + const index = action.index; + const tokens = [].concat(state.tokens); + + delete tokens[index]; + + return { ...state, tokens: tokens }; + } + + default: + return state; + } +}; diff --git a/js/src/dapps/tokenreg/Tokens/tokens.css b/js/src/dapps/tokenreg/Tokens/tokens.css new file mode 100644 index 000000000..20aaecef4 --- /dev/null +++ b/js/src/dapps/tokenreg/Tokens/tokens.css @@ -0,0 +1,24 @@ +/* Copyright 2015, 2016 Ethcore (UK) Ltd. +/* This file is part of Parity. +/* +/* Parity is free software: you can redistribute it and/or modify +/* it under the terms of the GNU General Public License as published by +/* the Free Software Foundation, either version 3 of the License, or +/* (at your option) any later version. +/* +/* Parity is distributed in the hope that it will be useful, +/* but WITHOUT ANY WARRANTY; without even the implied warranty of +/* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the +/* GNU General Public License for more details. +/* +/* You should have received a copy of the GNU General Public License +/* along with Parity. If not, see . +*/ + +.tokens { + width: 90%; + padding-top: 2rem; + display: flex; + flex-wrap: wrap; + justify-content: space-around; +} diff --git a/js/src/dapps/tokenreg/Tokens/tokens.js b/js/src/dapps/tokenreg/Tokens/tokens.js new file mode 100644 index 000000000..57c2a2a91 --- /dev/null +++ b/js/src/dapps/tokenreg/Tokens/tokens.js @@ -0,0 +1,68 @@ +// Copyright 2015, 2016 Ethcore (UK) Ltd. +// This file is part of Parity. + +// Parity is free software: you can redistribute it and/or modify +// it under the terms of the GNU General Public License as published by +// the Free Software Foundation, either version 3 of the License, or +// (at your option) any later version. + +// Parity is distributed in the hope that it will be useful, +// but WITHOUT ANY WARRANTY; without even the implied warranty of +// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the +// GNU General Public License for more details. + +// You should have received a copy of the GNU General Public License +// along with Parity. If not, see . + +import React, { Component, PropTypes } from 'react'; + +import Token from './Token'; +import Loading from '../Loading'; + +import styles from './tokens.css'; + +export default class Tokens extends Component { + static propTypes = { + handleAddMeta: PropTypes.func.isRequired, + handleUnregister: PropTypes.func.isRequired, + handleMetaLookup: PropTypes.func.isRequired, + isOwner: PropTypes.bool.isRequired, + isLoading: PropTypes.bool.isRequired, + tokens: PropTypes.array, + accounts: PropTypes.array + }; + + render () { + const { isLoading, tokens } = this.props; + const loading = isLoading ? () : null; + + return ( +
+ { this.renderTokens(tokens) } + { loading } +
+ ); + } + + renderTokens (tokens) { + const { accounts } = this.props; + + return tokens.map((token, index) => { + if (!token || !token.tla) { + return null; + } + + const isTokenOwner = !!accounts.find((account) => account.address === token.owner); + + return ( + + ); + }); + } +} diff --git a/js/src/dapps/tokenreg/constants.js b/js/src/dapps/tokenreg/constants.js new file mode 100644 index 000000000..e9e7bebca --- /dev/null +++ b/js/src/dapps/tokenreg/constants.js @@ -0,0 +1,30 @@ +// Copyright 2015, 2016 Ethcore (UK) Ltd. +// This file is part of Parity. + +// Parity is free software: you can redistribute it and/or modify +// it under the terms of the GNU General Public License as published by +// the Free Software Foundation, either version 3 of the License, or +// (at your option) any later version. + +// Parity is distributed in the hope that it will be useful, +// but WITHOUT ANY WARRANTY; without even the implied warranty of +// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the +// GNU General Public License for more details. + +// You should have received a copy of the GNU General Public License +// along with Parity. If not, see . + +import { HEX_TYPE, ADDRESS_TYPE } from './Inputs/validation'; + +export const metaDataKeys = [ + { + label: 'Image', + value: 'IMG', + validation: HEX_TYPE + }, + { + label: 'Address', + value: 'A', + validation: ADDRESS_TYPE + } +]; diff --git a/js/src/dapps/tokenreg/parity.js b/js/src/dapps/tokenreg/parity.js new file mode 100644 index 000000000..f6d59f44d --- /dev/null +++ b/js/src/dapps/tokenreg/parity.js @@ -0,0 +1,21 @@ +// Copyright 2015, 2016 Ethcore (UK) Ltd. +// This file is part of Parity. + +// Parity is free software: you can redistribute it and/or modify +// it under the terms of the GNU General Public License as published by +// the Free Software Foundation, either version 3 of the License, or +// (at your option) any later version. + +// Parity is distributed in the hope that it will be useful, +// but WITHOUT ANY WARRANTY; without even the implied warranty of +// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the +// GNU General Public License for more details. + +// You should have received a copy of the GNU General Public License +// along with Parity. If not, see . + +const { api } = window.parity; + +export { + api +}; diff --git a/js/src/dapps/tokenreg/reducers.js b/js/src/dapps/tokenreg/reducers.js new file mode 100644 index 000000000..cf533432f --- /dev/null +++ b/js/src/dapps/tokenreg/reducers.js @@ -0,0 +1,28 @@ +// Copyright 2015, 2016 Ethcore (UK) Ltd. +// This file is part of Parity. + +// Parity is free software: you can redistribute it and/or modify +// it under the terms of the GNU General Public License as published by +// the Free Software Foundation, either version 3 of the License, or +// (at your option) any later version. + +// Parity is distributed in the hope that it will be useful, +// but WITHOUT ANY WARRANTY; without even the implied warranty of +// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the +// GNU General Public License for more details. + +// You should have received a copy of the GNU General Public License +// along with Parity. If not, see . + +import { combineReducers } from 'redux'; + +import status from './Status/reducer'; +import tokens from './Tokens/reducer'; +import actions from './Actions/reducer'; +import accounts from './Accounts/reducer'; + +const rootReducer = combineReducers({ + status, tokens, actions, accounts +}); + +export default rootReducer; diff --git a/js/src/dapps/tokenreg/store.js b/js/src/dapps/tokenreg/store.js new file mode 100644 index 000000000..5fb7d4b6a --- /dev/null +++ b/js/src/dapps/tokenreg/store.js @@ -0,0 +1,22 @@ +// Copyright 2015, 2016 Ethcore (UK) Ltd. +// This file is part of Parity. + +// Parity is free software: you can redistribute it and/or modify +// it under the terms of the GNU General Public License as published by +// the Free Software Foundation, either version 3 of the License, or +// (at your option) any later version. + +// Parity is distributed in the hope that it will be useful, +// but WITHOUT ANY WARRANTY; without even the implied warranty of +// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the +// GNU General Public License for more details. + +// You should have received a copy of the GNU General Public License +// along with Parity. If not, see . + +import { createStore, applyMiddleware } from 'redux'; +import thunk from 'redux-thunk'; + +import reducer from './reducers'; + +export default createStore(reducer, applyMiddleware(thunk)); diff --git a/js/src/dapps/tokenreg/utils.js b/js/src/dapps/tokenreg/utils.js new file mode 100644 index 000000000..0f4e192f0 --- /dev/null +++ b/js/src/dapps/tokenreg/utils.js @@ -0,0 +1,37 @@ +// Copyright 2015, 2016 Ethcore (UK) Ltd. +// This file is part of Parity. + +// Parity is free software: you can redistribute it and/or modify +// it under the terms of the GNU General Public License as published by +// the Free Software Foundation, either version 3 of the License, or +// (at your option) any later version. + +// Parity is distributed in the hope that it will be useful, +// but WITHOUT ANY WARRANTY; without even the implied warranty of +// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the +// GNU General Public License for more details. + +// You should have received a copy of the GNU General Public License +// along with Parity. If not, see . + +import { api } from './parity'; + +import { eip20 as eip20Abi } from '../../contracts/abi'; + +export const getTokenTotalSupply = (tokenAddress) => { + return api + .eth + .getCode(tokenAddress) + .then(code => { + if (!code || /^(0x)?0?$/.test(code)) { + return null; + } + + const contract = api.newContract(eip20Abi, tokenAddress); + + return contract + .instance + .totalSupply + .call({}, []); + }); +}; diff --git a/js/src/environment/empty.js b/js/src/environment/empty.js new file mode 100644 index 000000000..e69de29bb diff --git a/js/src/environment/index.js b/js/src/environment/index.js new file mode 100644 index 000000000..d8e81f343 --- /dev/null +++ b/js/src/environment/index.js @@ -0,0 +1,20 @@ +// Copyright 2015, 2016 Ethcore (UK) Ltd. +// This file is part of Parity. + +// Parity is free software: you can redistribute it and/or modify +// it under the terms of the GNU General Public License as published by +// the Free Software Foundation, either version 3 of the License, or +// (at your option) any later version. + +// Parity is distributed in the hope that it will be useful, +// but WITHOUT ANY WARRANTY; without even the implied warranty of +// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the +// GNU General Public License for more details. + +// You should have received a copy of the GNU General Public License +// along with Parity. If not, see . + +// import './integration-tests'; +// import './perf-debug'; + +import './tests'; diff --git a/js/src/environment/integration-tests/fake-backend.js b/js/src/environment/integration-tests/fake-backend.js new file mode 100644 index 000000000..16fcae3ad --- /dev/null +++ b/js/src/environment/integration-tests/fake-backend.js @@ -0,0 +1,84 @@ +// Copyright 2015, 2016 Ethcore (UK) Ltd. +// This file is part of Parity. + +// Parity is free software: you can redistribute it and/or modify +// it under the terms of the GNU General Public License as published by +// the Free Software Foundation, either version 3 of the License, or +// (at your option) any later version. + +// Parity is distributed in the hope that it will be useful, +// but WITHOUT ANY WARRANTY; without even the implied warranty of +// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the +// GNU General Public License for more details. + +// You should have received a copy of the GNU General Public License +// along with Parity. If not, see . + +import sinon from 'sinon/pkg/sinon'; +import mockedResponses from '../../../test/mocked-responses.json'; + +class FakeRpcServer { + constructor () { + this.xhr = null; + this.middlewares = []; + } + + start () { + this.xhr = sinon.useFakeXMLHttpRequest(); + this.xhr.onCreate = this.handleRequest; + return () => this.xhr.restore(); + } + + simpleRpc (rpcMethod, result) { + this.rpc(rpcMethod, req => result); + } + + rpc (rpcMethod, middleware) { + this.middlewares.unshift({ + rpcMethod, middleware + }); + } + + handleRequest = req => { + setTimeout(() => { + req.body = JSON.parse(req.requestBody); + const middlewaresForMethod = this.middlewares + .filter(m => m.rpcMethod === req.body.method); + + const response = middlewaresForMethod + .map(m => m.middleware) + .reduce((replied, middleware) => { + if (replied) { + return replied; + } + + return middleware(req); + }, false); + + if (!response) { + return req.respond(405, { + 'Content-Type': 'application/json' + }, JSON.stringify({ + jsonrpc: '2.0', + id: req.body.id, + result: null + })); + } + + return req.respond(200, { + 'Content-Type': 'application/json' + }, JSON.stringify({ + jsonrpc: '2.0', + id: req.body.id, + result: response + })); + }); + } +} + +const fakeRpc = new FakeRpcServer(); +fakeRpc.start(); +mockedResponses.rpc.forEach(method => fakeRpc.simpleRpc(method.name, method.response)); + +// export fakeRpc to mock stuff in tests +window.fakeRpc = fakeRpc; diff --git a/js/src/environment/integration-tests/index.js b/js/src/environment/integration-tests/index.js new file mode 100644 index 000000000..d62acaa32 --- /dev/null +++ b/js/src/environment/integration-tests/index.js @@ -0,0 +1,17 @@ +// Copyright 2015, 2016 Ethcore (UK) Ltd. +// This file is part of Parity. + +// Parity is free software: you can redistribute it and/or modify +// it under the terms of the GNU General Public License as published by +// the Free Software Foundation, either version 3 of the License, or +// (at your option) any later version. + +// Parity is distributed in the hope that it will be useful, +// but WITHOUT ANY WARRANTY; without even the implied warranty of +// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the +// GNU General Public License for more details. + +// You should have received a copy of the GNU General Public License +// along with Parity. If not, see . + +import './fake-backend'; diff --git a/js/src/environment/perf-debug/index.js b/js/src/environment/perf-debug/index.js new file mode 100644 index 000000000..29f84f50f --- /dev/null +++ b/js/src/environment/perf-debug/index.js @@ -0,0 +1,17 @@ +// Copyright 2015, 2016 Ethcore (UK) Ltd. +// This file is part of Parity. + +// Parity is free software: you can redistribute it and/or modify +// it under the terms of the GNU General Public License as published by +// the Free Software Foundation, either version 3 of the License, or +// (at your option) any later version. + +// Parity is distributed in the hope that it will be useful, +// but WITHOUT ANY WARRANTY; without even the implied warranty of +// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the +// GNU General Public License for more details. + +// You should have received a copy of the GNU General Public License +// along with Parity. If not, see . + +import './why-update'; diff --git a/js/src/environment/perf-debug/why-update.js b/js/src/environment/perf-debug/why-update.js new file mode 100644 index 000000000..35eef2edc --- /dev/null +++ b/js/src/environment/perf-debug/why-update.js @@ -0,0 +1,20 @@ +// Copyright 2015, 2016 Ethcore (UK) Ltd. +// This file is part of Parity. + +// Parity is free software: you can redistribute it and/or modify +// it under the terms of the GNU General Public License as published by +// the Free Software Foundation, either version 3 of the License, or +// (at your option) any later version. + +// Parity is distributed in the hope that it will be useful, +// but WITHOUT ANY WARRANTY; without even the implied warranty of +// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the +// GNU General Public License for more details. + +// You should have received a copy of the GNU General Public License +// along with Parity. If not, see . + +import React from 'react'; +import { whyDidYouUpdate } from 'why-did-you-update'; + +whyDidYouUpdate(React); diff --git a/js/src/environment/tests/index.js b/js/src/environment/tests/index.js new file mode 100644 index 000000000..3e6241fb1 --- /dev/null +++ b/js/src/environment/tests/index.js @@ -0,0 +1,17 @@ +// Copyright 2015, 2016 Ethcore (UK) Ltd. +// This file is part of Parity. + +// Parity is free software: you can redistribute it and/or modify +// it under the terms of the GNU General Public License as published by +// the Free Software Foundation, either version 3 of the License, or +// (at your option) any later version. + +// Parity is distributed in the hope that it will be useful, +// but WITHOUT ANY WARRANTY; without even the implied warranty of +// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the +// GNU General Public License for more details. + +// You should have received a copy of the GNU General Public License +// along with Parity. If not, see . + +import './test-utils'; diff --git a/js/src/environment/tests/test-utils.js b/js/src/environment/tests/test-utils.js new file mode 100644 index 000000000..785a6f3ad --- /dev/null +++ b/js/src/environment/tests/test-utils.js @@ -0,0 +1,46 @@ +// Copyright 2015, 2016 Ethcore (UK) Ltd. +// This file is part of Parity. + +// Parity is free software: you can redistribute it and/or modify +// it under the terms of the GNU General Public License as published by +// the Free Software Foundation, either version 3 of the License, or +// (at your option) any later version. + +// Parity is distributed in the hope that it will be useful, +// but WITHOUT ANY WARRANTY; without even the implied warranty of +// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the +// GNU General Public License for more details. + +// You should have received a copy of the GNU General Public License +// along with Parity. If not, see . + +import { Component } from 'react'; + +const isProd = process.env.NODE_ENV === 'production'; + +// Component utils for integration tests hooks. +const TEST_HOOK = 'data-test'; +Component.prototype._test = isProd ? noop : testHook; +Component.prototype._testInherit = isProd ? noop : testHookInherit; + +function noop (name) {} + +function testHookInherit (name) { + let hook = this.props[TEST_HOOK]; + if (name) { + hook += `-${name}`; + } + return { + [TEST_HOOK]: hook + }; +} + +function testHook (name) { + let hook = this.constructor.name; + if (name) { + hook += `-${name}`; + } + return { + [TEST_HOOK]: hook + }; +} diff --git a/js/src/error_pages.css b/js/src/error_pages.css new file mode 100644 index 000000000..0b098d29a --- /dev/null +++ b/js/src/error_pages.css @@ -0,0 +1,113 @@ +/* Copyright 2015, 2016 Ethcore (UK) Ltd. +/* This file is part of Parity. +/* +/* Parity is free software: you can redistribute it and/or modify +/* it under the terms of the GNU General Public License as published by +/* the Free Software Foundation, either version 3 of the License, or +/* (at your option) any later version. +/* +/* Parity is distributed in the hope that it will be useful, +/* but WITHOUT ANY WARRANTY; without even the implied warranty of +/* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the +/* GNU General Public License for more details. +/* +/* You should have received a copy of the GNU General Public License +/* along with Parity. If not, see . +*/ + +@font-face { + font-family: "Roboto"; + src: url('~roboto-font/fonts/Roboto/roboto-light-webfont.eot'); + src: url('~roboto-font/fonts/Roboto/roboto-light-webfont.eot?#iefix') format('embedded-opentype'), url('~roboto-font/fonts/Roboto/roboto-light-webfont.woff') format('woff'), url('~roboto-font/fonts/Roboto/roboto-light-webfont.ttf') format('truetype'); + font-weight: 300; + font-style: normal; +} + +@font-face { + font-family: 'Roboto Mono'; + src: url('~roboto-mono-webfont/fonts/RobotoMono-Light.ttf') format('truetype'); + font-style: normal; + font-weight: 300; + text-rendering: optimizeLegibility; +} + +:root, :root body { + margin: 0; + padding: 0; + border: 0; + font-size: 100%; + font: inherit; + vertical-align: baseline; + background: rgb(95, 95, 95); + color: rgba(255, 255, 255, 0.75); + font-size: 16px; + font-family: 'Roboto', sans-serif; + font-weight: 300; +} + +:root a, :root a:visited { + text-decoration: none; + cursor: pointer; + color: rgb(0, 151, 167); /* #f80 */ +} + +:root a:hover { + color: rgb(0, 174, 193); +} + +h1,h2,h3,h4,h5,h6 { + font-weight: 300; + text-transform: uppercase; + text-decoration: none; +} + +h1 { + font-size: 24px; + line-height: 36px; + color: rgb(0, 151, 167); +} + +h2 { + font-size: 20px; + line-height: 34px; +} + +code,kbd,pre,samp { + font-family: 'Roboto Mono', monospace; +} + +.parity-navbar { + background: rgb(65, 65, 65); + height: 72px; + padding: 0 1rem; + display: flex; + justify-content: space-between; +} + +.parity-status { + clear: both; + padding: 1rem; + margin: 1rem 0; + text-align: right; + opacity: 0.75; +} + +.parity-box { + margin: 1rem; + padding: 1rem; + background-color: rgb(48, 48, 48); + box-sizing: border-box; + box-shadow: rgba(0, 0, 0, 0.117647) 0px 1px 6px, rgba(0, 0, 0, 0.117647) 0px 1px 4px; + border-radius: 2px; + z-index: 1; + color: #aaa; +} + +.parity-box h1, +.parity-box h2, +.parity-box h3, +.parity-box h4, +.parity-box h5, +.parity-box h6 { + margin: 0; +} diff --git a/js/src/images/dapps/blocks-350.png b/js/src/images/dapps/blocks-350.png new file mode 100644 index 000000000..16e0c213d Binary files /dev/null and b/js/src/images/dapps/blocks-350.png differ diff --git a/js/src/index.html b/js/src/index.html new file mode 100644 index 000000000..4ca2e7c1a --- /dev/null +++ b/js/src/index.html @@ -0,0 +1,20 @@ + + + + + + + Parity + + + +
+ + + + + diff --git a/js/src/index.js b/js/src/index.js new file mode 100644 index 000000000..f432251d0 --- /dev/null +++ b/js/src/index.js @@ -0,0 +1,91 @@ +// Copyright 2015, 2016 Ethcore (UK) Ltd. +// This file is part of Parity. + +// Parity is free software: you can redistribute it and/or modify +// it under the terms of the GNU General Public License as published by +// the Free Software Foundation, either version 3 of the License, or +// (at your option) any later version. + +// Parity is distributed in the hope that it will be useful, +// but WITHOUT ANY WARRANTY; without even the implied warranty of +// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the +// GNU General Public License for more details. + +// You should have received a copy of the GNU General Public License +// along with Parity. If not, see . + +import 'babel-polyfill'; +import 'whatwg-fetch'; + +// redirect when not on 127.0.0.1:8180 +const host = `${window.location.hostname}:${window.location.port}`; +if (host === '127.0.0.1:8080' || host === 'localhost:8080') { + window.location = 'http://127.0.0.1:8180'; +} + +import es6Promise from 'es6-promise'; +es6Promise.polyfill(); + +import React from 'react'; +import ReactDOM from 'react-dom'; +import injectTapEventPlugin from 'react-tap-event-plugin'; +import { createHashHistory } from 'history'; +import { Redirect, Router, Route, useRouterHistory } from 'react-router'; + +import SecureApi from './secureApi'; +import ContractInstances from './contracts'; + +import { initStore } from './redux'; +import { ContextProvider, muiTheme } from './ui'; +import { Accounts, Account, Addresses, Address, Application, Contract, Contracts, Dapp, Dapps, Settings, SettingsBackground, SettingsViews, Signer, Status } from './views'; + +import './environment'; + +import '../assets/fonts/Roboto/font.css'; +import '../assets/fonts/RobotoMono/font.css'; +import styles from './reset.css'; +import './index.html'; + +injectTapEventPlugin(); + +const parityUrl = process.env.PARITY_URL || + ( + process.env.NODE_ENV === 'production' + ? window.location.host + : '127.0.0.1:8180' + ); + +const api = new SecureApi(`ws://${parityUrl}`); +ContractInstances.create(api); + +const store = initStore(api); +store.dispatch({ type: 'initAll', api }); + +const routerHistory = useRouterHistory(createHashHistory)({}); + +ReactDOM.render( + + + + + + + + + + + + + + + + + + + + + + + , + document.querySelector('#container') +); diff --git a/js/src/jsonrpc/README.md b/js/src/jsonrpc/README.md new file mode 100644 index 000000000..8f16ec3ca --- /dev/null +++ b/js/src/jsonrpc/README.md @@ -0,0 +1,20 @@ +# jsonrpc + +JSON file of all ethereum's rpc methods supported by parity + +## interfaces + +[interfaces.md](release/interfaces.md) contains the auto-generated list of interfaces exposed, along with their relevant documentation + +## contributing + +0. Clone the repo +0. Branch +0. Add the missing interfaces only into `src/interfaces/*.js` +0. Parameters (array) & Returns take objects of type + - `{ type: [Array|Boolean|Object|String|...], desc: 'some description' }` + - Types are built-in JS types or those defined in `src/types.js` (e.g. `BlockNumber`, `Quantity`, etc.) + - If a formatter is required, add it as `format: 'string-type'` +0. Run the lint & tests, `npm run lint && npm run testOnce` +0. Generate via `npm run build` which outputs `index.js`, `index.json` & `interfaces.md` (Only required until Travis is fully in-place) +0. Check-in and make a PR diff --git a/js/src/jsonrpc/generator/build-json.js b/js/src/jsonrpc/generator/build-json.js new file mode 100644 index 000000000..362df7bf5 --- /dev/null +++ b/js/src/jsonrpc/generator/build-json.js @@ -0,0 +1,67 @@ +// Copyright 2015, 2016 Ethcore (UK) Ltd. +// This file is part of Parity. + +// Parity is free software: you can redistribute it and/or modify +// it under the terms of the GNU General Public License as published by +// the Free Software Foundation, either version 3 of the License, or +// (at your option) any later version. + +// Parity is distributed in the hope that it will be useful, +// but WITHOUT ANY WARRANTY; without even the implied warranty of +// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the +// GNU General Public License for more details. + +// You should have received a copy of the GNU General Public License +// along with Parity. If not, see . + +import fs from 'fs'; +import path from 'path'; + +import interfaces from '../'; + +const INDEX_JSON = path.join(__dirname, '../../release/index.json'); +const methods = []; + +function formatDescription (obj) { + const optional = obj.optional ? '(optional) ' : ''; + const defaults = obj.default ? `(default: ${obj.default}) ` : ''; + + return `${obj.type.name} - ${optional}${defaults}${obj.desc}`; +} + +function formatType (obj) { + if (obj.type === Object && obj.details) { + const formatted = {}; + + Object.keys(obj.details).sort().forEach((key) => { + formatted[key] = formatType(obj.details[key]); + }); + + return { + desc: formatDescription(obj), + details: formatted + }; + } else if (obj.type && obj.type.name) { + return formatDescription(obj); + } + + return obj; +} + +Object.keys(interfaces).sort().forEach((group) => { + Object.keys(interfaces[group]).sort().forEach((name) => { + const method = interfaces[group][name]; + const deprecated = method.deprecated ? ' (Deprecated and not supported, to be removed in a future version)' : ''; + + methods.push({ + name: `${group}_${name}`, + desc: `${method.desc}${deprecated}`, + params: method.params.map(formatType), + returns: formatType(method.returns), + inputFormatters: method.params.map((param) => param.format || null), + outputFormatter: method.returns.format || null + }); + }); +}); + +fs.writeFileSync(INDEX_JSON, JSON.stringify({ methods: methods }, null, 2), 'utf8'); diff --git a/js/src/jsonrpc/generator/build-markdown.js b/js/src/jsonrpc/generator/build-markdown.js new file mode 100644 index 000000000..278138d54 --- /dev/null +++ b/js/src/jsonrpc/generator/build-markdown.js @@ -0,0 +1,69 @@ +// Copyright 2015, 2016 Ethcore (UK) Ltd. +// This file is part of Parity. + +// Parity is free software: you can redistribute it and/or modify +// it under the terms of the GNU General Public License as published by +// the Free Software Foundation, either version 3 of the License, or +// (at your option) any later version. + +// Parity is distributed in the hope that it will be useful, +// but WITHOUT ANY WARRANTY; without even the implied warranty of +// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the +// GNU General Public License for more details. + +// You should have received a copy of the GNU General Public License +// along with Parity. If not, see . + +import fs from 'fs'; +import path from 'path'; + +import interfaces from '../'; + +const MARKDOWN = path.join(__dirname, '../../release/interfaces.md'); + +let preamble = '# interfaces\n'; +let markdown = ''; + +function formatDescription (obj, prefix = '', indent = '') { + const optional = obj.optional ? '(optional) ' : ''; + const defaults = obj.default ? `(default: ${obj.default}) ` : ''; + + return `${indent}- ${prefix}\`${obj.type.name}\` - ${optional}${defaults}${obj.desc}`; +} + +function formatType (obj) { + if (obj.type === Object && obj.details) { + const sub = Object.keys(obj.details).sort().map((key) => { + return formatDescription(obj.details[key], `\`${key}\`/`, ' '); + }).join('\n'); + + return `${formatDescription(obj)}\n${sub}`; + } else if (obj.type && obj.type.name) { + return formatDescription(obj); + } + + return obj; +} + +Object.keys(interfaces).sort().forEach((group) => { + let content = ''; + + preamble = `${preamble}\n- [${group}](#${group})`; + markdown = `${markdown}\n## ${group}\n`; + + Object.keys(interfaces[group]).sort().forEach((iname) => { + const method = interfaces[group][iname]; + const name = `${group}_${iname}`; + const deprecated = method.deprecated ? ' (Deprecated and not supported, to be removed in a future version)' : ''; + const desc = `${method.desc}${deprecated}`; + const params = method.params.map(formatType).join('\n'); + const returns = formatType(method.returns); + + markdown = `${markdown}\n- [${name}](#${name})`; + content = `${content}### ${name}\n\n${desc}\n\n#### parameters\n\n${params || 'none'}\n\n#### returns\n\n${returns || 'none'}\n\n`; + }); + + markdown = `${markdown}\n\n${content}`; +}); + +fs.writeFileSync(MARKDOWN, `${preamble}\n\n${markdown}`, 'utf8'); diff --git a/js/src/jsonrpc/index.js b/js/src/jsonrpc/index.js new file mode 100644 index 000000000..18bd4302d --- /dev/null +++ b/js/src/jsonrpc/index.js @@ -0,0 +1,35 @@ +// Copyright 2015, 2016 Ethcore (UK) Ltd. +// This file is part of Parity. + +// Parity is free software: you can redistribute it and/or modify +// it under the terms of the GNU General Public License as published by +// the Free Software Foundation, either version 3 of the License, or +// (at your option) any later version. + +// Parity is distributed in the hope that it will be useful, +// but WITHOUT ANY WARRANTY; without even the implied warranty of +// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the +// GNU General Public License for more details. + +// You should have received a copy of the GNU General Public License +// along with Parity. If not, see . + +import db from './interfaces/db'; +import eth from './interfaces/eth'; +import ethcore from './interfaces/ethcore'; +import net from './interfaces/net'; +import personal from './interfaces/personal'; +import shh from './interfaces/shh'; +import trace from './interfaces/trace'; +import web3 from './interfaces/web3'; + +export default { + db: db, + eth: eth, + ethcore: ethcore, + net: net, + personal: personal, + shh: shh, + trace: trace, + web3: web3 +}; diff --git a/js/src/jsonrpc/index.spec.js b/js/src/jsonrpc/index.spec.js new file mode 100644 index 000000000..0f1247477 --- /dev/null +++ b/js/src/jsonrpc/index.spec.js @@ -0,0 +1,62 @@ +// Copyright 2015, 2016 Ethcore (UK) Ltd. +// This file is part of Parity. + +// Parity is free software: you can redistribute it and/or modify +// it under the terms of the GNU General Public License as published by +// the Free Software Foundation, either version 3 of the License, or +// (at your option) any later version. + +// Parity is distributed in the hope that it will be useful, +// but WITHOUT ANY WARRANTY; without even the implied warranty of +// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the +// GNU General Public License for more details. + +// You should have received a copy of the GNU General Public License +// along with Parity. If not, see . + +import interfaces from './'; +import { Address, BlockNumber, Data, Hash, Integer, Quantity } from './types'; + +const flatlist = {}; + +function verifyType (obj) { + if (typeof obj !== 'string') { + expect(obj).to.satisfy(() => { + return obj.type === Array || + obj.type === Boolean || + obj.type === Object || + obj.type === String || + obj.type === Address || + obj.type === BlockNumber || + obj.type === Data || + obj.type === Hash || + obj.type === Integer || + obj.type === Quantity; + }); + } +} + +describe('jsonrpc/interfaces', () => { + Object.keys(interfaces).forEach((group) => { + describe(group, () => { + Object.keys(interfaces[group]).forEach((name) => { + const method = interfaces[group][name]; + + flatlist[`${group}_${name}`] = true; + + describe(name, () => { + it('has the correct interface', () => { + expect(method.desc).to.be.a('string'); + expect(method.params).to.be.an('array'); + expect(method.returns).to.satisfy((returns) => { + return typeof returns === 'string' || typeof returns === 'object'; + }); + + method.params.forEach(verifyType); + verifyType(method.returns); + }); + }); + }); + }); + }); +}); diff --git a/js/src/jsonrpc/interfaces/db.js b/js/src/jsonrpc/interfaces/db.js new file mode 100644 index 000000000..ab4d35b34 --- /dev/null +++ b/js/src/jsonrpc/interfaces/db.js @@ -0,0 +1,103 @@ +// Copyright 2015, 2016 Ethcore (UK) Ltd. +// This file is part of Parity. + +// Parity is free software: you can redistribute it and/or modify +// it under the terms of the GNU General Public License as published by +// the Free Software Foundation, either version 3 of the License, or +// (at your option) any later version. + +// Parity is distributed in the hope that it will be useful, +// but WITHOUT ANY WARRANTY; without even the implied warranty of +// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the +// GNU General Public License for more details. + +// You should have received a copy of the GNU General Public License +// along with Parity. If not, see . + +import { Data } from '../types'; + +export default { + getHex: { + desc: 'Returns binary data from the local database.', + params: [ + { + type: String, + desc: 'Database name' + }, + { + type: String, + desc: 'Key name' + } + ], + returns: { + type: Data, + desc: 'The previously stored data' + }, + deprecated: true + }, + + getString: { + desc: 'Returns string from the local database.', + params: [ + { + type: String, + desc: 'Database name' + }, + { + type: String, + desc: 'Key name' + } + ], + returns: { + type: String, + desc: 'The previously stored string' + }, + deprecated: true + }, + + putHex: { + desc: 'Stores binary data in the local database.', + params: [ + { + type: String, + desc: 'Database name' + }, + { + type: String, + desc: 'Key name' + }, + { + type: Data, + desc: 'The data to store' + } + ], + returns: { + type: Boolean, + desc: '`true` if the value was stored, otherwise `false`' + }, + deprecated: true + }, + + putString: { + desc: 'Stores a string in the local database.', + params: [ + { + type: String, + desc: 'Database name' + }, + { + type: String, + desc: 'Key name' + }, + { + type: String, + desc: 'The string to store' + } + ], + returns: { + type: Boolean, + desc: '`true` if the value was stored, otherwise `false`' + }, + deprecated: true + } +}; diff --git a/js/src/jsonrpc/interfaces/eth.js b/js/src/jsonrpc/interfaces/eth.js new file mode 100644 index 000000000..b8bbcbd61 --- /dev/null +++ b/js/src/jsonrpc/interfaces/eth.js @@ -0,0 +1,1044 @@ +// Copyright 2015, 2016 Ethcore (UK) Ltd. +// This file is part of Parity. + +// Parity is free software: you can redistribute it and/or modify +// it under the terms of the GNU General Public License as published by +// the Free Software Foundation, either version 3 of the License, or +// (at your option) any later version. + +// Parity is distributed in the hope that it will be useful, +// but WITHOUT ANY WARRANTY; without even the implied warranty of +// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the +// GNU General Public License for more details. + +// You should have received a copy of the GNU General Public License +// along with Parity. If not, see . + +import { Address, BlockNumber, Data, Hash, Quantity } from '../types'; + +export default { + accounts: { + desc: 'Returns a list of addresses owned by client.', + params: [], + returns: { + type: Array, + desc: '20 Bytes - addresses owned by the client' + } + }, + + blockNumber: { + desc: 'Returns the number of most recent block.', + params: [], + returns: { + type: Quantity, + desc: 'integer of the current block number the client is on' + } + }, + + call: { + desc: 'Executes a new message call immediately without creating a transaction on the block chain.', + params: [ + { + type: Object, + desc: 'The transaction call object', + format: 'inputCallFormatter', + details: { + from: { + type: Address, + desc: '20 Bytes - The address the transaction is send from', + optional: true + }, + to: { + type: Address, + desc: '20 Bytes - The address the transaction is directed to' + }, + gas: { + type: Quantity, + desc: 'Integer of the gas provided for the transaction execution. eth_call consumes zero gas, but this parameter may be needed by some executions', + optional: true + }, + gasPrice: { + type: Quantity, + desc: 'Integer of the gasPrice used for each paid gas', + optional: true + }, + value: { + type: Quantity, + desc: 'Integer of the value send with this transaction', + optional: true + }, + data: { + type: Data, + desc: 'Hash of the method signature and encoded parameters. For details see [Ethereum Contract ABI](https://github.com/ethereum/wiki/wiki/Ethereum-Contract-ABI)', + optional: true + } + } + }, + { + type: BlockNumber, + desc: 'integer block number, or the string `\'latest\'`, `\'earliest\'` or `\'pending\'`, see the [default block parameter](#the-default-block-parameter)', + format: 'inputDefaultBlockNumberFormatter' + } + ], + returns: { + type: Data, + desc: 'the return value of executed contract' + } + }, + + checkRequest: { + desc: 'Returns the transactionhash of the requestId (received from eth_postTransaction) if the request was confirmed', + params: [ + { + type: Quantity, + desc: 'The requestId to check for' + } + ], + returns: { + type: Hash, + desc: '32 Bytes - the transaction hash, or the zero hash if the transaction is not yet available' + } + }, + + coinbase: { + desc: 'Returns the client coinbase address.', + params: [], + returns: { + type: Address, + desc: 'The current coinbase address' + } + }, + + compileSerpent: { + desc: 'Returns compiled serpent code.', + params: [ + { + type: String, + desc: 'The source code' + } + ], + returns: { + type: Data, + desc: 'The compiled source code' + } + }, + + compileSolidity: { + desc: 'Returns compiled solidity code.', + params: [ + { + type: String, + desc: 'The source code' + } + ], + returns: { + type: Data, + desc: 'The compiled source code' + } + }, + + compileLLL: { + desc: 'Returns compiled LLL code.', + params: [ + { + type: String, + desc: 'The source code' + } + ], + returns: { + type: Data, + desc: 'The compiled source code' + } + }, + + estimateGas: { + desc: 'Makes a call or transaction, which won\'t be added to the blockchain and returns the used gas, which can be used for estimating the used gas.', + params: [ + { + type: Object, + desc: 'see [eth_sendTransaction](#eth_sendTransaction)', + format: 'inputCallFormatter' + } + ], + returns: { + type: Quantity, + desc: 'The amount of gas used', + format: 'utils.toDecimal' + } + }, + + fetchQueuedTransactions: { + desc: '?', + params: [ + '?' + ], + returns: { + type: Boolean, + desc: 'whether the call was successful' + } + }, + + flush: { + desc: '?', + params: [], + returns: { + type: Boolean, + desc: 'whether the call was successful' + } + }, + + gasPrice: { + desc: 'Returns the current price per gas in wei.', + params: [], + returns: { + type: Quantity, + desc: 'integer of the current gas price in wei' + } + }, + + getBalance: { + desc: 'Returns the balance of the account of given address.', + params: [ + { + type: Address, + desc: '20 Bytes - address to check for balance', + format: 'inputAddressFormatter' + }, + { + type: BlockNumber, + desc: 'integer block number, or the string `\'latest\'`, `\'earliest\'` or `\'pending\'`, see the [default block parameter](#the-default-block-parameter)', + format: 'inputDefaultBlockNumberFormatter' + } + ], + returns: { + type: Quantity, + desc: 'integer of the current balance in wei', + format: 'outputBigNumberFormatter' + } + }, + + getBlockByHash: { + desc: 'Returns information about a block by hash.', + params: [ + { + type: Hash, + desc: 'Hash of a block' + }, + { + type: Boolean, + desc: 'If `true` it returns the full transaction objects, if `false` only the hashes of the transactions' + } + ], + returns: { + type: Object, + desc: 'A block object, or `null` when no block was found', + details: { + number: { + type: Quantity, + desc: 'The block number. `null` when its pending block' + }, + hash: { + type: Hash, + desc: '32 Bytes - hash of the block. `null` when its pending block' + }, + parentHash: { + type: Hash, + desc: '32 Bytes - hash of the parent block' + }, + nonce: { + type: Data, + desc: '8 Bytes - hash of the generated proof-of-work. `null` when its pending block' + }, + sha3Uncles: { + type: Data, + desc: '32 Bytes - SHA3 of the uncles data in the block' + }, + logsBloom: { + type: Data, + desc: '256 Bytes - the bloom filter for the logs of the block. `null` when its pending block' + }, + transactionsRoot: { + type: Data, + desc: '32 Bytes - the root of the transaction trie of the block' + }, + stateRoot: { + type: Data, + desc: '32 Bytes - the root of the final state trie of the block' + }, + receiptsRoot: { + type: Data, desc: '32 Bytes - the root of the receipts trie of the block' + }, + miner: { + type: Address, + desc: '20 Bytes - the address of the beneficiary to whom the mining rewards were given' + }, + difficulty: { + type: Quantity, + desc: 'integer of the difficulty for this block' + }, + totalDifficulty: { + type: Quantity, + desc: 'integer of the total difficulty of the chain until this block' + }, + extraData: { + type: Data, + desc: 'the \'extra data\' field of this block' + }, + size: { + type: Quantity, + desc: 'integer the size of this block in bytes' + }, + gasLimit: { + type: Quantity, + desc: 'the maximum gas allowed in this block' + }, + gasUsed: { + type: Quantity, + desc: 'the total used gas by all transactions in this block' + }, + timestamp: { + type: Quantity, + desc: 'the unix timestamp for when the block was collated' + }, + transactions: { + type: Array, + desc: 'Array of transaction objects, or 32 Bytes transaction hashes depending on the last given parameter' + }, + uncles: { + type: Array, + desc: 'Array of uncle hashes' + } + } + } + }, + + getBlockByNumber: { + desc: 'Returns information about a block by block number.', + params: [ + { + type: BlockNumber, + desc: 'integer of a block number, or the string `\'earliest\'`, `\'latest\'` or `\'pending\'`, as in the [default block parameter](#the-default-block-parameter)' + }, + { + type: Boolean, + desc: 'If `true` it returns the full transaction objects, if `false` only the hashes of the transactions' + } + ], + returns: 'See [eth_getBlockByHash](#eth_getblockbyhash)' + }, + + getBlockTransactionCountByHash: { + desc: 'Returns the number of transactions in a block from a block matching the given block hash.', + params: [ + { + type: Hash, + desc: '32 Bytes - hash of a block' + } + ], + returns: { + type: Quantity, + desc: 'integer of the number of transactions in this block' + } + }, + + getBlockTransactionCountByNumber: { + desc: 'Returns the number of transactions in a block from a block matching the given block number.', + params: [ + { + type: BlockNumber, + desc: 'integer of a block number, or the string `\'earliest\'`, `\'latest\'` or `\'pending\'`, as in the [default block parameter](#the-default-block-parameter)' + } + ], + returns: { + type: Quantity, + desc: 'integer of the number of transactions in this block' + } + }, + + getCode: { + desc: 'Returns code at a given address.', + params: [ + { + type: Address, + desc: '20 Bytes - address', + format: 'inputAddressFormatter' + }, + { + type: BlockNumber, + desc: 'integer block number, or the string `\'latest\'`, `\'earliest\'` or `\'pending\'`, see the [default block parameter](#the-default-block-parameter)', + format: 'inputDefaultBlockNumberFormatter' + } + ], + returns: { + type: Data, + desc: 'the code from the given address' + } + }, + + getCompilers: { + desc: 'Returns a list of available compilers in the client.', + params: [], + returns: { + type: Array, + desc: 'Array of available compilers' + } + }, + + getFilterChanges: { + desc: 'Polling method for a filter, which returns an array of logs which occurred since last poll.', + params: [ + { + type: Quantity, + desc: 'The filter id' + } + ], + returns: { + type: Array, + desc: 'Array of log objects, or an empty array if nothing has changed since last poll' + } + }, + + getFilterChangesEx: { + desc: '?', + params: [ + '?' + ], + returns: { + type: Boolean, + desc: 'whether the call was successful' + } + }, + + getFilterLogs: { + desc: 'Returns an array of all logs matching filter with given id.', + params: [ + { + type: Quantity, + desc: 'The filter id' + } + ], + returns: 'See [eth_getFilterChanges](#eth_getfilterchanges)' + }, + + getFilterLogsEx: { + desc: '?', + params: [ + '?' + ], + returns: { + type: Boolean, + desc: 'whether the call was successful' + } + }, + + getLogs: { + desc: 'Returns an array of all logs matching a given filter object.', + params: [ + { + type: Object, + desc: 'The filter object, see [eth_newFilter parameters](#eth_newfilter)' + } + ], + returns: 'See [eth_getFilterChanges](#eth_getfilterchanges)' + }, + + getLogsEx: { + desc: '?', + params: [ + '?' + ], + returns: { + type: Boolean, + desc: 'whether the call was successful' + } + }, + + getStorageAt: { + desc: 'Returns the value from a storage position at a given address.', + params: [ + { + type: Address, + desc: '20 Bytes - address of the storage' + }, + { + type: Quantity, + desc: 'integer of the position in the storage', + format: 'utils.toHex' + }, + { + type: BlockNumber, + desc: 'integer block number, or the string `\'latest\'`, `\'earliest\'` or `\'pending\'`, see the [default block parameter](#the-default-block-parameter)', + format: 'inputDefaultBlockNumberFormatter' + } + ], + returns: { + type: Data, + desc: 'the value at this storage position' + } + }, + + getTransactionByHash: { + desc: 'Returns the information about a transaction requested by transaction hash.', + params: [ + { + type: Hash, + desc: '32 Bytes - hash of a transaction' + } + ], + returns: { + type: Object, + desc: 'A transaction object, or `null` when no transaction was found:', + format: 'outputTransactionFormatter', + details: { + hash: { + type: Hash, + desc: '32 Bytes - hash of the transaction.' + }, + nonce: { + type: Quantity, + desc: 'the number of transactions made by the sender prior to this one.' + }, + blockHash: { + type: Hash, + desc: '32 Bytes - hash of the block where this transaction was in. `null` when its pending.' + }, + blockNumber: { + type: BlockNumber, + desc: 'block number where this transaction was in. `null` when its pending.' + }, + transactionIndex: { + type: Quantity, + desc: 'integer of the transactions index position in the block. `null` when its pending.' + }, + from: { + type: Address, + desc: '20 Bytes - address of the sender.' + }, + to: { + type: Address, + desc: '20 Bytes - address of the receiver. `null` when its a contract creation transaction.' + }, + value: { + type: Quantity, + desc: 'value transferred in Wei.' + }, + gasPrice: { + type: Quantity, + desc: 'gas price provided by the sender in Wei.' + }, + gas: { + type: Quantity, + desc: 'gas provided by the sender.' + }, + input: { + type: Data, + desc: 'the data send along with the transaction.' + } + } + } + }, + + getTransactionByBlockHashAndIndex: { + desc: 'Returns information about a transaction by block hash and transaction index position.', + params: [ + { + type: Hash, + desc: 'hash of a block' + }, + { + type: Quantity, + desc: 'integer of the transaction index position' + } + ], + returns: 'See [eth_getBlockByHash](#eth_gettransactionbyhash)' + }, + + getTransactionByBlockNumberAndIndex: { + desc: 'Returns information about a transaction by block number and transaction index position.', + params: [ + { + type: BlockNumber, + desc: 'a block number, or the string `\'earliest\'`, `\'latest\'` or `\'pending\'`, as in the [default block parameter](#the-default-block-parameter)' + }, + { + type: Quantity, + desc: 'The transaction index position' + } + ], + returns: 'See [eth_getBlockByHash](#eth_gettransactionbyhash)' + }, + + getTransactionCount: { + desc: 'Returns the number of transactions *sent* from an address.', + params: [ + { + type: Address, + desc: '20 Bytes - address' + }, + { + type: BlockNumber, + desc: 'integer block number, or the string `\'latest\'`, `\'earliest\'` or `\'pending\'`, see the [default block parameter](#the-default-block-parameter)', + format: 'inputDefaultBlockNumberFormatter' + } + ], + returns: { + type: Quantity, + desc: 'integer of the number of transactions send from this address', + format: 'utils.toDecimal' + } + }, + + getTransactionReceipt: { + desc: 'Returns the receipt of a transaction by transaction hash.\n**Note** That the receipt is not available for pending transactions.', + params: [ + { + type: Hash, + desc: 'hash of a transaction' + } + ], + returns: { + type: Object, + desc: 'A transaction receipt object, or `null` when no receipt was found:', + format: 'outputTransactionReceiptFormatter', + details: { + transactionHash: { + type: Hash, + desc: '32 Bytes - hash of the transaction.' + }, + transactionIndex: { + type: Quantity, + desc: 'integer of the transactions index position in the block.' + }, + blockHash: { + type: Hash, + desc: '32 Bytes - hash of the block where this transaction was in.' + }, + blockNumber: { + type: BlockNumber, + desc: 'block number where this transaction was in.' + }, + cumulativeGasUsed: { + type: Quantity, + desc: 'The total amount of gas used when this transaction was executed in the block.' + }, + gasUsed: { + type: Quantity, + desc: 'The amount of gas used by this specific transaction alone.' + }, + contractAddress: { + type: Address, + desc: '20 Bytes - The contract address created, if the transaction was a contract creation, otherwise `null`.' + }, + logs: { + type: Array, + desc: 'Array of log objects, which this transaction generated.' + } + } + } + }, + + getUncleByBlockHashAndIndex: { + desc: 'Returns information about a uncle of a block by hash and uncle index position.', + params: [ + { + type: Hash, + desc: 'Hash a block' + }, + { + type: Quantity, + desc: 'The uncle\'s index position' + } + ], + returns: 'See [eth_getBlockByHash](#eth_getblockbyhash)' + }, + + getUncleByBlockNumberAndIndex: { + desc: 'Returns information about a uncle of a block by number and uncle index position.', + params: [ + { + type: BlockNumber, + desc: 'a block number, or the string `\'earliest\'`, `\'latest\'` or `\'pending\'`, as in the [default block parameter](#the-default-block-parameter)' + }, + { + type: Quantity, + desc: 'The uncle\'s index position' + } + ], + returns: 'See [eth_getBlockByHash](#eth_getblockbyhash)' + }, + + getUncleCountByBlockHash: { + desc: 'Returns the number of uncles in a block from a block matching the given block hash.', + params: [ + { + type: Hash, + desc: '32 Bytes - hash of a block' + } + ], + returns: { + type: Quantity, + desc: 'integer of the number of uncles in this block' + } + }, + + getUncleCountByBlockNumber: { + desc: 'Returns the number of uncles in a block from a block matching the given block number.', + params: [ + { + type: BlockNumber, + desc: 'integer of a block number, or the string \'latest\', \'earliest\' or \'pending\', see the [default block parameter](#the-default-block-parameter)' + } + ], + returns: { + type: Quantity, + desc: 'integer of the number of uncles in this block' + } + }, + + getWork: { + desc: 'Returns the hash of the current block, the seedHash, and the boundary condition to be met (\'target\').', + params: [], + returns: { + type: Array, + desc: 'Array with the following properties:' + } + }, + + hashrate: { + desc: 'Returns the number of hashes per second that the node is mining with.', + params: [], + returns: { + type: Quantity, + desc: 'number of hashes per second' + } + }, + + inspectTransaction: { + desc: '?', + params: [ + '?' + ], + returns: { + type: Boolean, + desc: 'whether the call was successful' + } + }, + + mining: { + desc: 'Returns `true` if client is actively mining new blocks.', + params: [], + returns: { + type: Boolean, + desc: '`true` of the client is mining, otherwise `false`' + } + }, + + newBlockFilter: { + desc: 'Creates a filter in the node, to notify when a new block arrives.\nTo check if the state has changed, call [eth_getFilterChanges](#eth_getfilterchanges).', + params: [], + returns: { + type: Quantity, + desc: 'A filter id' + } + }, + + newFilter: { + desc: 'Creates a filter object, based on filter options, to notify when the state changes (logs).\nTo check if the state has changed, call [eth_getFilterChanges](#eth_getfilterchanges).', + params: [{ + type: Object, + desc: 'The filter options:', + details: { + fromBlock: { + type: BlockNumber, + desc: 'Integer block number, or `\'latest\'` for the last mined block or `\'pending\'`, `\'earliest\'` for not yet mined transactions.', + optional: true, + default: 'latest' + }, + toBlock: { + type: BlockNumber, + desc: 'Integer block number, or `\'latest\'` for the last mined block or `\'pending\'`, `\'earliest\'` for not yet mined transactions.', + optional: true, + default: 'latest' + }, + address: { + type: Address, + desc: '20 Bytes - Contract address or a list of addresses from which logs should originate.', + optional: true + }, + topics: { + type: Array, + desc: 'Array of 32 Bytes `DATA` topics. Topics are order-dependent. Each topic can also be an array of DATA with \'or\' options.', + optional: true + }, + limit: { + type: Number, + desc: 'The maximum number of entries to retrieve (latest first)', + optional: true + } + } + }], + returns: { + type: Quantity, + desc: 'The filter id' + } + }, + + newFilterEx: { + desc: '?', + params: [ + '?' + ], + returns: { + type: Boolean, + desc: 'whether the call was successful' + } + }, + + newPendingTransactionFilter: { + desc: 'Creates a filter in the node, to notify when new pending transactions arrive.\nTo check if the state has changed, call [eth_getFilterChanges](#eth_getfilterchanges).', + params: [], + returns: { + type: Quantity, + desc: 'A filter id' + } + }, + + notePassword: { + desc: '?', + params: [ + '?' + ], + returns: { + type: Boolean, + desc: 'whether the call was successful' + } + }, + + pendingTransactions: { + desc: '?', + params: [], + returns: { + type: Boolean, + desc: 'whether the call was successful' + } + }, + + postTransaction: { + desc: 'Posts a transaction to the Signer.', + params: [ + { + type: Object, + desc: 'see [eth_sendTransaction](#eth_sendTransaction)', + format: 'inputCallFormatter' + } + ], + returns: { + type: Quantity, + desc: 'The id of the actual transaction', + format: 'utils.toDecimal' + } + }, + + protocolVersion: { + desc: 'Returns the current ethereum protocol version.', + params: [], + returns: { + type: String, + desc: 'The current ethereum protocol version' + } + }, + + register: { + desc: '?', + params: [ + '?' + ], + returns: { + type: Boolean, + desc: 'whether the call was successful' + } + }, + + sendRawTransaction: { + desc: 'Creates new message call transaction or a contract creation for signed transactions.', + params: [ + { + type: Data, + desc: 'The signed transaction data' + } + ], + returns: { + type: Hash, + desc: '32 Bytes - the transaction hash, or the zero hash if the transaction is not yet available' + } + }, + + sendTransaction: { + desc: 'Creates new message call transaction or a contract creation, if the data field contains code.', + params: [ + { + type: Object, desc: 'The transaction object', + format: 'inputTransactionFormatter', + details: { + from: { + type: Address, + desc: '20 Bytes - The address the transaction is send from' + }, + to: { + type: Address, + desc: '20 Bytes - (optional when creating new contract) The address the transaction is directed to' + }, + gas: { + type: Quantity, + desc: 'Integer of the gas provided for the transaction execution. It will return unused gas.', + optional: true, + default: 90000 + }, + gasPrice: { + type: Quantity, + desc: 'Integer of the gasPrice used for each paid gas', + optional: true, + default: 'To-Be-Determined' + }, + value: { + type: Quantity, + desc: 'Integer of the value send with this transaction', + optional: true + }, + data: { + type: Data, + desc: 'The compiled code of a contract OR the hash of the invoked method signature and encoded parameters. For details see [Ethereum Contract ABI](https://github.com/ethereum/wiki/wiki/Ethereum-Contract-ABI)' + }, + nonce: { + type: Quantity, + desc: 'Integer of a nonce. This allows to overwrite your own pending transactions that use the same nonce.', + optional: true + } + } + } + ], + returns: { + type: Hash, + desc: '32 Bytes - the transaction hash, or the zero hash if the transaction is not yet available' + } + }, + + sign: { + desc: 'Signs data with a given address.\n**Note** the address to sign must be unlocked.', + params: [ + { + type: Address, + desc: '20 Bytes - address', + format: 'inputAddressFormatter' + }, + { + type: Data, + desc: 'Data to sign' + } + ], + returns: { + type: Data, + desc: 'Signed data' + } + }, + + signTransaction: { + desc: '?', + params: [ + '?' + ], + returns: { + type: Boolean, + desc: 'whether the call was successful' + } + }, + + submitWork: { + desc: 'Used for submitting a proof-of-work solution.', + params: [ + { + type: Data, + desc: '8 Bytes - The nonce found (64 bits)' + }, + { + type: Data, + desc: '32 Bytes - The header\'s pow-hash (256 bits)' + }, + { + type: Data, + desc: '32 Bytes - The mix digest (256 bits)' + } + ], + returns: { + type: Boolean, + desc: '`true` if the provided solution is valid, otherwise `false`' + } + }, + + submitHashrate: { + desc: 'Used for submitting mining hashrate.', + params: [ + { + type: Data, + desc: 'a hexadecimal string representation (32 bytes) of the hash rate' + }, + { + type: String, + desc: 'A random hexadecimal(32 bytes) ID identifying the client' + } + ], + returns: { + type: Boolean, + desc: '`true` if submitting went through succesfully and `false` otherwise' + } + }, + + syncing: { + desc: 'Returns an object with data about the sync status or `false`.', + params: [], + returns: { + type: Object, + desc: 'An object with sync status data or `FALSE`, when not syncing', + format: 'outputSyncingFormatter', + details: { + startingBlock: { + type: Quantity, + desc: 'The block at which the import started (will only be reset, after the sync reached his head)' + }, + currentBlock: { + type: Quantity, + desc: 'The current block, same as eth_blockNumber' + }, + highestBlock: { + type: Quantity, + desc: 'The estimated highest block' + } + } + } + }, + + uninstallFilter: { + desc: 'Uninstalls a filter with given id. Should always be called when watch is no longer needed.\nAdditonally Filters timeout when they aren\'t requested with [eth_getFilterChanges](#eth_getfilterchanges) for a period of time.', + params: [ + { + type: Quantity, + desc: 'The filter id' + } + ], + returns: { + type: Boolean, + desc: '`true` if the filter was successfully uninstalled, otherwise `false`' + } + }, + + unregister: { + desc: '?', + params: [ + '?' + ], + returns: { + type: Boolean, + desc: 'whether the call was successful' + } + } +}; diff --git a/js/src/jsonrpc/interfaces/ethcore.js b/js/src/jsonrpc/interfaces/ethcore.js new file mode 100644 index 000000000..5b2910618 --- /dev/null +++ b/js/src/jsonrpc/interfaces/ethcore.js @@ -0,0 +1,315 @@ +// Copyright 2015, 2016 Ethcore (UK) Ltd. +// This file is part of Parity. + +// Parity is free software: you can redistribute it and/or modify +// it under the terms of the GNU General Public License as published by +// the Free Software Foundation, either version 3 of the License, or +// (at your option) any later version. + +// Parity is distributed in the hope that it will be useful, +// but WITHOUT ANY WARRANTY; without even the implied warranty of +// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the +// GNU General Public License for more details. + +// You should have received a copy of the GNU General Public License +// along with Parity. If not, see . + +import { Address, Data, Hash, Quantity } from '../types'; + +export default { + acceptNonReservedPeers: { + desc: '?', + params: [], + returns: { + type: Boolean, + desc: '?' + } + }, + + addReservedPeer: { + desc: '?', + params: [ + { + type: String, + desc: 'Enode' + } + ], + returns: { + type: Boolean, + desc: '?' + } + }, + + defaultExtraData: { + desc: 'Returns the default extra data', + params: [], + returns: { + type: Data, + desc: 'Extra data' + } + }, + + devLogs: { + desc: 'Returns latest logs of your node', + params: [], + returns: { + type: Array, + desc: 'Development logs' + } + }, + + devLogsLevels: { + desc: 'Returns current log level settings', + params: [], + returns: { + type: String, + decs: 'Current log level' + } + }, + + dropNonReservedPeers: { + desc: '?', + params: [], + returns: { + type: Boolean, + desc: '?' + } + }, + + extraData: { + desc: 'Returns currently set extra data', + params: [], + returns: { + type: Data, + desc: 'Extra data' + } + }, + + gasFloorTarget: { + desc: 'Returns current target for gas floor', + params: [], + returns: { + type: Quantity, + desc: 'Gas Floor Target', + format: 'outputBigNumberFormatter' + } + }, + + generateSecretPhrase: { + desc: 'Creates a secret phrase that can be associated with an account', + params: [], + returns: { + type: String, + desc: 'The secret phrase' + } + }, + + hashContent: { + desc: 'Creates a hash of the file as retrieved', + params: [ + { + type: String, + desc: 'The url of the content' + } + ], + returns: { + type: Hash, + desc: 'The hash of the content' + } + }, + + minGasPrice: { + desc: 'Returns currently set minimal gas price', + params: [], + returns: { + type: Quantity, + desc: 'Minimal Gas Price', + format: 'outputBigNumberFormatter' + } + }, + + netChain: { + desc: 'Returns the name of the connected chain.', + params: [], + returns: { + type: String, + desc: 'chain name' + } + }, + + netPeers: { + desc: 'Returns number of peers.', + params: [], + returns: { + type: Quantity, + desc: 'Number of peers' + } + }, + + netMaxPeers: { + desc: 'Returns maximal number of peers.', + params: [], + returns: { + type: Quantity, + desc: 'Maximal number of peers' + } + }, + + netPort: { + desc: 'Returns network port the node is listening on.', + params: [], + returns: { + type: Quantity, + desc: 'Port Number' + } + }, + + nodeName: { + desc: 'Returns node name (identity)', + params: [], + returns: { + type: String, + desc: 'Node name' + } + }, + + phraseToAddress: { + desc: 'Converts a secret phrase into the corresponting address', + params: [ + { + type: String, + desc: 'The secret' + } + ], + returns: { + type: Address, + desc: 'Corresponding address' + } + }, + + removeReservedPeer: { + desc: '?', + params: [ + { + type: String, + desc: 'Encode' + } + ], + returns: { + type: Boolean, + desc: '?' + } + }, + + registryAddress: { + desc: 'The address for the global registry', + params: [], + returns: { + type: Address, + desc: 'The registry address' + } + }, + + rpcSettings: { + desc: 'Returns basic settings of rpc (enabled, port, interface).', + params: [], + returns: { + type: Object, + desc: 'JSON object containing rpc settings' + } + }, + + setAuthor: { + desc: 'Changes author (coinbase) for mined blocks.', + params: [ + { + type: Address, + desc: '20 Bytes - Address', + format: 'inputAddressFormatter' + } + ], + returns: { + type: Boolean, + desc: 'whether the call was successful' + } + }, + + setExtraData: { + desc: 'Changes extra data for newly mined blocks', + params: [ + { + type: Data, + desc: 'Extra Data', + format: 'utils.toHex' + } + ], + returns: { + type: Boolean, + desc: 'whether the call was successful' + } + }, + + setGasFloorTarget: { + desc: 'Changes current gas floor target.', + params: [ + { + type: Quantity, + desc: 'Gas Floor Target', + format: 'utils.toHex' + } + ], + returns: { + type: Boolean, + desc: 'whether the call was successful' + } + }, + + setMinGasPrice: { + desc: 'Changes minimal gas price for transaction to be accepted to the queue.', + params: [ + { + type: Quantity, + desc: 'Minimal gas price', + format: 'utils.toHex' + } + ], + returns: { + type: Boolean, + desc: 'whether the call was successful' + } + }, + + setTransactionsLimit: { + desc: 'Changes limit for transactions in queue.', + params: [ + { + type: Quantity, + desc: 'New Limit', + format: 'utils.toHex' + } + ], + returns: { + type: Boolean, + desc: 'whether the call was successful' + } + }, + + transactionsLimit: { + desc: 'Changes limit for transactions in queue.', + params: [], + returns: { + type: Quantity, + desc: 'Current max number of transactions in queue', + format: 'outputBigNumberFormatter' + } + }, + + unsignedTransactionsCount: { + desc: 'Returns number of unsigned transactions when running with Trusted Signer. Error otherwise', + params: [], + returns: { + type: Quantity, + desc: 'Number of unsigned transactions' + } + } +}; diff --git a/js/src/jsonrpc/interfaces/net.js b/js/src/jsonrpc/interfaces/net.js new file mode 100644 index 000000000..9cc3bc076 --- /dev/null +++ b/js/src/jsonrpc/interfaces/net.js @@ -0,0 +1,47 @@ +// Copyright 2015, 2016 Ethcore (UK) Ltd. +// This file is part of Parity. + +// Parity is free software: you can redistribute it and/or modify +// it under the terms of the GNU General Public License as published by +// the Free Software Foundation, either version 3 of the License, or +// (at your option) any later version. + +// Parity is distributed in the hope that it will be useful, +// but WITHOUT ANY WARRANTY; without even the implied warranty of +// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the +// GNU General Public License for more details. + +// You should have received a copy of the GNU General Public License +// along with Parity. If not, see . + +import { Quantity } from '../types'; + +export default { + listening: { + desc: 'Returns `true` if client is actively listening for network connections.', + params: [], + returns: { + type: Boolean, + desc: '`true` when listening, otherwise `false`.' + } + }, + + peerCount: { + desc: 'Returns number of peers currenly connected to the client.', + params: [], + returns: { + type: Quantity, + desc: 'Integer of the number of connected peers', + format: 'utils.toDecimal' + } + }, + + version: { + desc: 'Returns the current network protocol version.', + params: [], + returns: { + type: String, + desc: 'The current network protocol version' + } + } +}; diff --git a/js/src/jsonrpc/interfaces/personal.js b/js/src/jsonrpc/interfaces/personal.js new file mode 100644 index 000000000..748c8799f --- /dev/null +++ b/js/src/jsonrpc/interfaces/personal.js @@ -0,0 +1,288 @@ +// Copyright 2015, 2016 Ethcore (UK) Ltd. +// This file is part of Parity. + +// Parity is free software: you can redistribute it and/or modify +// it under the terms of the GNU General Public License as published by +// the Free Software Foundation, either version 3 of the License, or +// (at your option) any later version. + +// Parity is distributed in the hope that it will be useful, +// but WITHOUT ANY WARRANTY; without even the implied warranty of +// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the +// GNU General Public License for more details. + +// You should have received a copy of the GNU General Public License +// along with Parity. If not, see . + +import { Address, Data, Quantity } from '../types'; + +export default { + accountsInfo: { + desc: 'returns a map of accounts as an object', + params: [], + returns: { + type: Array, + desc: 'Account metadata', + details: { + name: { + type: String, + desc: 'Account name' + }, + meta: { + type: String, + desc: 'Encoded JSON string the defines additional account metadata' + }, + uuid: { + type: String, + desc: 'The account UUID, or null if not available/unknown/not applicable.' + } + } + } + }, + + generateAuthorizationToken: { + desc: 'Generates a new authorization token', + params: [], + returns: { + type: String, + desc: 'The new authorization token' + } + }, + + requestsToConfirm: { + desc: 'Returns a list of the transactions requiring authorization', + params: [], + returns: { + type: Array, + desc: 'A list of the outstanding transactions' + } + }, + + confirmRequest: { + desc: 'Confirm a request in the signer queue', + params: [ + { + type: Quantity, + desc: 'The request id' + }, + { + type: Object, + desc: 'The request options' + }, + { + type: String, + desc: 'The account password' + } + ], + returns: { + type: Boolean, + desc: 'The status of the confirmation' + } + }, + + rejectRequest: { + desc: 'Rejects a request in the signer queue', + params: [ + { + type: Quantity, + desc: 'The request id' + } + ], + returns: { + type: Boolean, + desc: 'The status of the rejection' + } + }, + + listAccounts: { + desc: 'Returns a list of addresses owned by client.', + params: [], + returns: { + type: Array, + desc: '20 Bytes addresses owned by the client.' + } + }, + + listGethAccounts: { + desc: 'Returns a list of the accounts available from Geth', + params: [], + returns: { + type: Array, + desc: '20 Bytes addresses owned by the client.' + } + }, + + importGethAccounts: { + desc: 'Imports a list of accounts from geth', + params: [ + { + type: Array, + desc: 'List of the geth addresses to import' + } + ], + returns: { + type: Array, + desc: 'Array of the imported addresses' + } + }, + + newAccount: { + desc: 'Creates new account', + params: [ + { + type: String, + desc: 'Password' + } + ], + returns: { + type: Address, + desc: 'The created address' + } + }, + + newAccountFromPhrase: { + desc: 'Creates a new account from a brainwallet passphrase', + params: [ + { + type: String, + desc: 'Phrase' + }, + { + type: String, + desc: 'Password' + } + ], + returns: { + type: Address, + desc: 'The created address' + } + }, + + newAccountFromWallet: { + desc: 'Creates a new account from a JSON import', + params: [ + { + type: String, + desc: 'JSON' + }, + { + type: String, + desc: 'Password' + } + ], + returns: { + type: Address, + desc: 'The created address' + } + }, + + setAccountName: { + desc: 'Sets a name for the account', + params: [ + { + type: Address, + desc: 'Address' + }, + { + type: String, + desc: 'Name' + } + ], + returns: { + type: Object, + desc: 'Returns null in all cases' + } + }, + + setAccountMeta: { + desc: 'Sets metadata for the account', + params: [ + { + type: Address, + desc: 'Address' + }, + { + type: String, + desc: 'Metadata (JSON encoded)' + } + ], + returns: { + type: Object, + desc: 'Returns null in all cases' + } + }, + + signAndSendTransaction: { + desc: 'Sends and signs a transaction given account passphrase. Does not require the account to be unlocked nor unlocks the account for future transactions. ', + params: [ + { + type: Object, + desc: 'The transaction object', + details: { + from: { + type: Address, + desc: '20 Bytes - The address the transaction is send from' + }, + to: { + type: Address, + desc: '20 Bytes - (optional when creating new contract) The address the transaction is directed to' + }, + gas: { + type: Quantity, + desc: 'Integer of the gas provided for the transaction execution. It will return unused gas', + optional: true, + default: 90000 + }, + gasPrice: { + type: Quantity, + desc: 'Integer of the gasPrice used for each paid gas', + optional: true, + default: 'To-Be-Determined' + }, + value: { + type: Quantity, + desc: 'Integer of the value send with this transaction', + optional: true + }, + data: { + type: Data, + desc: 'The compiled code of a contract OR the hash of the invoked method signature and encoded parameters. For details see [Ethereum Contract ABI](https://github.com/ethereum/wiki/wiki/Ethereum-Contract-ABI)' + }, + nonce: { + type: Quantity, + desc: 'Integer of a nonce. This allows to overwrite your own pending transactions that use the same nonce.', + optional: true + } + } + }, + { + type: String, + desc: 'Passphrase to unlock `from` account.' + } + ], + returns: { + type: Data, + desc: '32 Bytes - the transaction hash, or the zero hash if the transaction is not yet available' + } + }, + + signerEnabled: { + desc: 'Returns whether signer is enabled/disabled.', + params: [], + returns: { + type: Boolean, + desc: 'true when enabled, false when disabled' + } + }, + + unlockAccount: { + desc: '?', + params: [ + '?', '?', '?' + ], + returns: { + type: Boolean, + desc: 'whether the call was successful' + } + } +}; diff --git a/js/src/jsonrpc/interfaces/shh.js b/js/src/jsonrpc/interfaces/shh.js new file mode 100644 index 000000000..e9f727dac --- /dev/null +++ b/js/src/jsonrpc/interfaces/shh.js @@ -0,0 +1,169 @@ +// Copyright 2015, 2016 Ethcore (UK) Ltd. +// This file is part of Parity. + +// Parity is free software: you can redistribute it and/or modify +// it under the terms of the GNU General Public License as published by +// the Free Software Foundation, either version 3 of the License, or +// (at your option) any later version. + +// Parity is distributed in the hope that it will be useful, +// but WITHOUT ANY WARRANTY; without even the implied warranty of +// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the +// GNU General Public License for more details. + +// You should have received a copy of the GNU General Public License +// along with Parity. If not, see . + +import { Data, Quantity } from '../types'; + +export default { + version: { + desc: 'Returns the current whisper protocol version.', + params: [], + returns: { + type: String, + desc: 'The current whisper protocol version' + } + }, + + post: { + desc: 'Sends a whisper message.', + params: [ + { + type: Object, desc: 'The whisper post object:', format: 'inputPostFormatter', + details: { + from: { + type: Data, desc: '60 Bytes - The identity of the sender', + optional: true + }, + to: { + type: Data, desc: '60 Bytes - The identity of the receiver. When present whisper will encrypt the message so that only the receiver can decrypt it', + optional: true + }, + topics: { + type: Array, desc: 'Array of `DATA` topics, for the receiver to identify messages' + }, + payload: { + type: Data, desc: 'The payload of the message' + }, + priority: { + type: Quantity, desc: 'The integer of the priority in a rang from ... (?)' + }, + ttl: { + type: Quantity, desc: 'Integer of the time to live in seconds.' + } + } + } + ], + returns: { + type: Boolean, + desc: '`true` if the message was send, otherwise `false`' + } + }, + + newIdentity: { + desc: 'Creates new whisper identity in the client.', + params: [], + returns: { + type: Data, + desc: '60 Bytes - the address of the new identiy' + } + }, + + hasIdentity: { + desc: 'Checks if the client hold the private keys for a given identity.', + params: [ + { + type: Data, + desc: '60 Bytes - The identity address to check' + } + ], + returns: { + type: Boolean, + desc: '`true` if the client holds the privatekey for that identity, otherwise `false`' + } + }, + + newGroup: { + desc: '(?)', + params: [], + returns: { + type: Data, desc: '60 Bytes - the address of the new group. (?)' + } + }, + + addToGroup: { + desc: '(?)', + params: [ + { + type: Data, + desc: '60 Bytes - The identity address to add to a group (?)' + } + ], + returns: { + type: Boolean, + desc: '`true` if the identity was successfully added to the group, otherwise `false` (?)' + } + }, + + newFilter: { + desc: 'Creates filter to notify, when client receives whisper message matching the filter options.', + params: [ + { + type: Object, desc: 'The filter options:', + details: { + to: { + type: Data, desc: '60 Bytes - Identity of the receiver. *When present it will try to decrypt any incoming message if the client holds the private key to this identity.*', + optional: true + }, + topics: { + type: Array, desc: 'Array of `DATA` topics which the incoming message\'s topics should match. You can use the following combinations' + } + } + } + ], + returns: { + type: Quantity, + desc: 'The newly created filter' + } + }, + + uninstallFilter: { + desc: 'Uninstalls a filter with given id. Should always be called when watch is no longer needed.\nAdditonally Filters timeout when they aren\'t requested with [shh_getFilterChanges](#shh_getfilterchanges) for a period of time.', + params: [ + { + type: Quantity, + desc: 'The filter id' + } + ], + returns: { + type: Boolean, + desc: '`true` if the filter was successfully uninstalled, otherwise `false`' + } + }, + + getFilterChanges: { + desc: 'Polling method for whisper filters. Returns new messages since the last call of this method.\n**Note** calling the [shh_getMessages](#shh_getmessages) method, will reset the buffer for this method, so that you won\'t receive duplicate messages.', + params: [ + { + type: Quantity, + desc: 'The filter id' + } + ], + returns: { + type: Array, + desc: 'Array of messages received since last poll' + } + }, + + getMessages: { + desc: 'Get all messages matching a filter. Unlike `shh_getFilterChanges` this returns all messages.', + params: [ + { + type: Quantity, + desc: 'The filter id' + } + ], + returns: 'See [shh_getFilterChanges](#shh_getfilterchanges)' + } +}; diff --git a/js/src/jsonrpc/interfaces/trace.js b/js/src/jsonrpc/interfaces/trace.js new file mode 100644 index 000000000..3dc4451f0 --- /dev/null +++ b/js/src/jsonrpc/interfaces/trace.js @@ -0,0 +1,79 @@ +// Copyright 2015, 2016 Ethcore (UK) Ltd. +// This file is part of Parity. + +// Parity is free software: you can redistribute it and/or modify +// it under the terms of the GNU General Public License as published by +// the Free Software Foundation, either version 3 of the License, or +// (at your option) any later version. + +// Parity is distributed in the hope that it will be useful, +// but WITHOUT ANY WARRANTY; without even the implied warranty of +// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the +// GNU General Public License for more details. + +// You should have received a copy of the GNU General Public License +// along with Parity. If not, see . + +import { BlockNumber, Hash, Integer } from '../types'; + +export default { + filter: { + desc: 'Returns traces matching given filter', + params: [ + { + type: Object, + desc: 'The filter object' + } + ], + returns: { + type: Array, + desc: 'Traces matching given filter' + } + }, + + get: { + desc: 'Returns trace at given position.', + params: [ + { + type: Hash, + desc: 'Transaction hash' + }, + { + type: Integer, + desc: 'Trace position witing transaction' + } + ], + returns: { + type: Object, + desc: 'Trace object' + } + }, + + transaction: { + desc: 'Returns all traces of given transaction', + params: [ + { + type: Hash, + desc: 'Transaction hash' + } + ], + returns: { + type: Array, + desc: 'Traces of given transaction' + } + }, + + block: { + desc: 'Returns traces created at given block', + params: [ + { + type: BlockNumber, + desc: 'Integer block number, or \'latest\' for the last mined block or \'pending\', \'earliest\' for not yet mined transactions' + } + ], + returns: { + type: Array, + desc: 'Block traces' + } + } +}; diff --git a/js/src/jsonrpc/interfaces/web3.js b/js/src/jsonrpc/interfaces/web3.js new file mode 100644 index 000000000..783663716 --- /dev/null +++ b/js/src/jsonrpc/interfaces/web3.js @@ -0,0 +1,42 @@ +// Copyright 2015, 2016 Ethcore (UK) Ltd. +// This file is part of Parity. + +// Parity is free software: you can redistribute it and/or modify +// it under the terms of the GNU General Public License as published by +// the Free Software Foundation, either version 3 of the License, or +// (at your option) any later version. + +// Parity is distributed in the hope that it will be useful, +// but WITHOUT ANY WARRANTY; without even the implied warranty of +// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the +// GNU General Public License for more details. + +// You should have received a copy of the GNU General Public License +// along with Parity. If not, see . + +import { Data } from '../types'; + +export default { + clientVersion: { + desc: 'Returns the current client version.', + params: [], + returns: { + type: String, + desc: 'The current client version' + } + }, + + sha3: { + desc: 'Returns Keccak-256 (*not* the standardized SHA3-256) of the given data.', + params: [ + { + type: String, + desc: 'The data to convert into a SHA3 hash' + } + ], + returns: { + type: Data, + desc: 'The SHA3 result of the given string' + } + } +}; diff --git a/js/src/jsonrpc/rollup.config.js b/js/src/jsonrpc/rollup.config.js new file mode 100644 index 000000000..cc4f8c931 --- /dev/null +++ b/js/src/jsonrpc/rollup.config.js @@ -0,0 +1,12 @@ +import babel from 'rollup-plugin-babel'; + +export default { + entry: 'src/index.js', + dest: 'release/index.js', + format: 'cjs', + plugins: [babel({ + babelrc: false, + presets: ['es2015-rollup', 'stage-0'], + runtimeHelpers: true + })] +}; diff --git a/js/src/jsonrpc/types.js b/js/src/jsonrpc/types.js new file mode 100644 index 000000000..026e010ed --- /dev/null +++ b/js/src/jsonrpc/types.js @@ -0,0 +1,27 @@ +// Copyright 2015, 2016 Ethcore (UK) Ltd. +// This file is part of Parity. + +// Parity is free software: you can redistribute it and/or modify +// it under the terms of the GNU General Public License as published by +// the Free Software Foundation, either version 3 of the License, or +// (at your option) any later version. + +// Parity is distributed in the hope that it will be useful, +// but WITHOUT ANY WARRANTY; without even the implied warranty of +// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the +// GNU General Public License for more details. + +// You should have received a copy of the GNU General Public License +// along with Parity. If not, see . + +export class Address {} + +export class BlockNumber {} + +export class Data {} + +export class Hash {} + +export class Integer {} + +export class Quantity {} diff --git a/js/src/modals/AddAddress/addAddress.js b/js/src/modals/AddAddress/addAddress.js new file mode 100644 index 000000000..5ab8bbe80 --- /dev/null +++ b/js/src/modals/AddAddress/addAddress.js @@ -0,0 +1,151 @@ +// Copyright 2015, 2016 Ethcore (UK) Ltd. +// This file is part of Parity. + +// Parity is free software: you can redistribute it and/or modify +// it under the terms of the GNU General Public License as published by +// the Free Software Foundation, either version 3 of the License, or +// (at your option) any later version. + +// Parity is distributed in the hope that it will be useful, +// but WITHOUT ANY WARRANTY; without even the implied warranty of +// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the +// GNU General Public License for more details. + +// You should have received a copy of the GNU General Public License +// along with Parity. If not, see . + +import React, { Component, PropTypes } from 'react'; +import ContentAdd from 'material-ui/svg-icons/content/add'; +import ContentClear from 'material-ui/svg-icons/content/clear'; + +import { Button, Modal, Form, Input, InputAddress } from '../../ui'; +import { ERRORS, validateAddress, validateName } from '../../util/validation'; + +export default class AddAddress extends Component { + static contextTypes = { + api: PropTypes.object.isRequired + } + + static propTypes = { + contacts: PropTypes.object.isRequired, + onClose: PropTypes.func + }; + + state = { + address: '', + addressError: ERRORS.invalidAddress, + name: '', + nameError: ERRORS.invalidName, + description: '' + }; + + render () { + return ( + + { this.renderFields() } + + ); + } + + renderDialogActions () { + const { addressError, nameError } = this.state; + const hasError = !!(addressError || nameError); + + return ([ +