@alephium/web3 0.2.0-rc.8 → 0.2.0-test.1

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
Files changed (75) hide show
  1. package/.eslintignore +2 -2
  2. package/.eslintrc.json +21 -0
  3. package/LICENSE +165 -0
  4. package/README.md +135 -2
  5. package/contracts/add/add.ral +13 -0
  6. package/contracts/greeter_main.ral +1 -1
  7. package/contracts/main.ral +4 -0
  8. package/contracts/sub/sub.ral +10 -0
  9. package/contracts/test/metadata.ral +18 -0
  10. package/contracts/test/warnings.ral +8 -0
  11. package/dist/alephium-web3.min.js +1 -1
  12. package/dist/alephium-web3.min.js.LICENSE.txt +17 -0
  13. package/dist/alephium-web3.min.js.map +1 -1
  14. package/dist/scripts/create-project.js +1 -1
  15. package/dist/src/api/api-alephium.d.ts +6 -19
  16. package/dist/src/api/api-explorer.d.ts +16 -16
  17. package/dist/src/api/index.js +1 -5
  18. package/dist/src/contract/contract.d.ts +16 -31
  19. package/dist/src/contract/contract.js +96 -123
  20. package/dist/src/contract/index.js +1 -5
  21. package/dist/src/index.d.ts +0 -1
  22. package/dist/src/index.js +1 -19
  23. package/dist/src/signer/index.d.ts +1 -0
  24. package/dist/src/signer/index.js +2 -5
  25. package/dist/src/signer/node-wallet.d.ts +11 -0
  26. package/dist/src/signer/node-wallet.js +57 -0
  27. package/dist/src/signer/signer.js +1 -5
  28. package/dist/src/test/index.d.ts +6 -0
  29. package/dist/src/test/index.js +41 -0
  30. package/dist/src/test/privatekey-wallet.d.ts +11 -0
  31. package/dist/src/test/privatekey-wallet.js +68 -0
  32. package/dist/src/transaction/index.d.ts +1 -0
  33. package/dist/src/transaction/index.js +2 -5
  34. package/dist/src/transaction/sign-verify.d.ts +2 -0
  35. package/dist/src/transaction/sign-verify.js +58 -0
  36. package/dist/src/utils/index.d.ts +1 -0
  37. package/dist/src/utils/index.js +2 -5
  38. package/dist/src/utils/password-crypto.d.ts +2 -0
  39. package/dist/src/utils/password-crypto.js +69 -0
  40. package/dist/src/utils/utils.d.ts +2 -3
  41. package/dist/src/utils/utils.js +15 -16
  42. package/gitignore +9 -0
  43. package/package.json +32 -6
  44. package/scripts/create-project.ts +1 -1
  45. package/src/api/api-alephium.ts +0 -14
  46. package/src/contract/contract.ts +102 -176
  47. package/src/contract/ralph.test.ts +178 -0
  48. package/src/fixtures/address.json +36 -0
  49. package/src/fixtures/balance.json +9 -0
  50. package/src/fixtures/self-clique.json +19 -0
  51. package/src/fixtures/transaction.json +13 -0
  52. package/src/fixtures/transactions.json +179 -0
  53. package/src/index.ts +0 -2
  54. package/src/signer/fixtures/genesis.json +26 -0
  55. package/src/signer/fixtures/wallets.json +26 -0
  56. package/src/signer/index.ts +1 -0
  57. package/src/signer/node-wallet.ts +65 -0
  58. package/src/test/index.ts +31 -0
  59. package/src/test/privatekey-wallet.ts +57 -0
  60. package/src/transaction/index.ts +1 -0
  61. package/src/transaction/sign-verify.test.ts +50 -0
  62. package/src/transaction/sign-verify.ts +39 -0
  63. package/src/utils/address.test.ts +47 -0
  64. package/src/utils/djb2.test.ts +35 -0
  65. package/src/utils/index.ts +1 -0
  66. package/src/utils/password-crypto.test.ts +27 -0
  67. package/src/utils/password-crypto.ts +77 -0
  68. package/src/utils/utils.test.ts +161 -0
  69. package/src/utils/utils.ts +7 -7
  70. package/templates/base/package.json +1 -1
  71. package/templates/react/package.json +1 -1
  72. package/test/contract.test.ts +213 -0
  73. package/test/events.test.ts +141 -0
  74. package/test/transaction.test.ts +73 -0
  75. package/jest-config.json +0 -11
package/.eslintignore CHANGED
@@ -1,2 +1,2 @@
1
- **/dist/
2
- **/templates/
1
+ dist/
2
+ templates/
package/.eslintrc.json ADDED
@@ -0,0 +1,21 @@
1
+ {
2
+ "plugins": ["security", "header"],
3
+ "extends": [
4
+ "prettier",
5
+ "plugin:prettier/recommended",
6
+ "plugin:security/recommended",
7
+ "plugin:@typescript-eslint/recommended"
8
+ ],
9
+ "parser": "@typescript-eslint/parser",
10
+ "rules": {
11
+ "header/header": [2, "scripts/header.js"]
12
+ },
13
+ "overrides": [
14
+ {
15
+ "files": ["*.ts"],
16
+ "rules": {
17
+ "security/detect-non-literal-fs-filename": "off"
18
+ }
19
+ }
20
+ ]
21
+ }
package/LICENSE ADDED
@@ -0,0 +1,165 @@
1
+ GNU LESSER GENERAL PUBLIC LICENSE
2
+ Version 3, 29 June 2007
3
+
4
+ Copyright (C) 2007 Free Software Foundation, Inc. <https://fsf.org/>
5
+ Everyone is permitted to copy and distribute verbatim copies
6
+ of this license document, but changing it is not allowed.
7
+
8
+
9
+ This version of the GNU Lesser General Public License incorporates
10
+ the terms and conditions of version 3 of the GNU General Public
11
+ License, supplemented by the additional permissions listed below.
12
+
13
+ 0. Additional Definitions.
14
+
15
+ As used herein, "this License" refers to version 3 of the GNU Lesser
16
+ General Public License, and the "GNU GPL" refers to version 3 of the GNU
17
+ General Public License.
18
+
19
+ "The Library" refers to a covered work governed by this License,
20
+ other than an Application or a Combined Work as defined below.
21
+
22
+ An "Application" is any work that makes use of an interface provided
23
+ by the Library, but which is not otherwise based on the Library.
24
+ Defining a subclass of a class defined by the Library is deemed a mode
25
+ of using an interface provided by the Library.
26
+
27
+ A "Combined Work" is a work produced by combining or linking an
28
+ Application with the Library. The particular version of the Library
29
+ with which the Combined Work was made is also called the "Linked
30
+ Version".
31
+
32
+ The "Minimal Corresponding Source" for a Combined Work means the
33
+ Corresponding Source for the Combined Work, excluding any source code
34
+ for portions of the Combined Work that, considered in isolation, are
35
+ based on the Application, and not on the Linked Version.
36
+
37
+ The "Corresponding Application Code" for a Combined Work means the
38
+ object code and/or source code for the Application, including any data
39
+ and utility programs needed for reproducing the Combined Work from the
40
+ Application, but excluding the System Libraries of the Combined Work.
41
+
42
+ 1. Exception to Section 3 of the GNU GPL.
43
+
44
+ You may convey a covered work under sections 3 and 4 of this License
45
+ without being bound by section 3 of the GNU GPL.
46
+
47
+ 2. Conveying Modified Versions.
48
+
49
+ If you modify a copy of the Library, and, in your modifications, a
50
+ facility refers to a function or data to be supplied by an Application
51
+ that uses the facility (other than as an argument passed when the
52
+ facility is invoked), then you may convey a copy of the modified
53
+ version:
54
+
55
+ a) under this License, provided that you make a good faith effort to
56
+ ensure that, in the event an Application does not supply the
57
+ function or data, the facility still operates, and performs
58
+ whatever part of its purpose remains meaningful, or
59
+
60
+ b) under the GNU GPL, with none of the additional permissions of
61
+ this License applicable to that copy.
62
+
63
+ 3. Object Code Incorporating Material from Library Header Files.
64
+
65
+ The object code form of an Application may incorporate material from
66
+ a header file that is part of the Library. You may convey such object
67
+ code under terms of your choice, provided that, if the incorporated
68
+ material is not limited to numerical parameters, data structure
69
+ layouts and accessors, or small macros, inline functions and templates
70
+ (ten or fewer lines in length), you do both of the following:
71
+
72
+ a) Give prominent notice with each copy of the object code that the
73
+ Library is used in it and that the Library and its use are
74
+ covered by this License.
75
+
76
+ b) Accompany the object code with a copy of the GNU GPL and this license
77
+ document.
78
+
79
+ 4. Combined Works.
80
+
81
+ You may convey a Combined Work under terms of your choice that,
82
+ taken together, effectively do not restrict modification of the
83
+ portions of the Library contained in the Combined Work and reverse
84
+ engineering for debugging such modifications, if you also do each of
85
+ the following:
86
+
87
+ a) Give prominent notice with each copy of the Combined Work that
88
+ the Library is used in it and that the Library and its use are
89
+ covered by this License.
90
+
91
+ b) Accompany the Combined Work with a copy of the GNU GPL and this license
92
+ document.
93
+
94
+ c) For a Combined Work that displays copyright notices during
95
+ execution, include the copyright notice for the Library among
96
+ these notices, as well as a reference directing the user to the
97
+ copies of the GNU GPL and this license document.
98
+
99
+ d) Do one of the following:
100
+
101
+ 0) Convey the Minimal Corresponding Source under the terms of this
102
+ License, and the Corresponding Application Code in a form
103
+ suitable for, and under terms that permit, the user to
104
+ recombine or relink the Application with a modified version of
105
+ the Linked Version to produce a modified Combined Work, in the
106
+ manner specified by section 6 of the GNU GPL for conveying
107
+ Corresponding Source.
108
+
109
+ 1) Use a suitable shared library mechanism for linking with the
110
+ Library. A suitable mechanism is one that (a) uses at run time
111
+ a copy of the Library already present on the user's computer
112
+ system, and (b) will operate properly with a modified version
113
+ of the Library that is interface-compatible with the Linked
114
+ Version.
115
+
116
+ e) Provide Installation Information, but only if you would otherwise
117
+ be required to provide such information under section 6 of the
118
+ GNU GPL, and only to the extent that such information is
119
+ necessary to install and execute a modified version of the
120
+ Combined Work produced by recombining or relinking the
121
+ Application with a modified version of the Linked Version. (If
122
+ you use option 4d0, the Installation Information must accompany
123
+ the Minimal Corresponding Source and Corresponding Application
124
+ Code. If you use option 4d1, you must provide the Installation
125
+ Information in the manner specified by section 6 of the GNU GPL
126
+ for conveying Corresponding Source.)
127
+
128
+ 5. Combined Libraries.
129
+
130
+ You may place library facilities that are a work based on the
131
+ Library side by side in a single library together with other library
132
+ facilities that are not Applications and are not covered by this
133
+ License, and convey such a combined library under terms of your
134
+ choice, if you do both of the following:
135
+
136
+ a) Accompany the combined library with a copy of the same work based
137
+ on the Library, uncombined with any other library facilities,
138
+ conveyed under the terms of this License.
139
+
140
+ b) Give prominent notice with the combined library that part of it
141
+ is a work based on the Library, and explaining where to find the
142
+ accompanying uncombined form of the same work.
143
+
144
+ 6. Revised Versions of the GNU Lesser General Public License.
145
+
146
+ The Free Software Foundation may publish revised and/or new versions
147
+ of the GNU Lesser General Public License from time to time. Such new
148
+ versions will be similar in spirit to the present version, but may
149
+ differ in detail to address new problems or concerns.
150
+
151
+ Each version is given a distinguishing version number. If the
152
+ Library as you received it specifies that a certain numbered version
153
+ of the GNU Lesser General Public License "or any later version"
154
+ applies to it, you have the option of following the terms and
155
+ conditions either of that published version or of any later version
156
+ published by the Free Software Foundation. If the Library as you
157
+ received it does not specify a version number of the GNU Lesser
158
+ General Public License, you may choose any version of the GNU Lesser
159
+ General Public License ever published by the Free Software Foundation.
160
+
161
+ If the Library as you received it specifies that a proxy can decide
162
+ whether future versions of the GNU Lesser General Public License shall
163
+ apply, that proxy's public statement of acceptance of any version is
164
+ permanent authorization for you to choose that version for the
165
+ Library.
package/README.md CHANGED
@@ -1,3 +1,136 @@
1
- # `@alephium/web3`
1
+ # Alephium Web3
2
2
 
3
- The package is the core of web3 SDKs
3
+ [![Github CI][test-badge]][test-link]
4
+ [![Code Coverage][coverage-badge]][coverage-link]
5
+ [![NPM][npm-badge]][npm-link]
6
+ [![code style: prettier][prettier-badge]][prettier-link]
7
+
8
+ A JavaScript/TypeScript library for building decentralized applications and smart contracts on Alephium.
9
+
10
+ You could run the following command to scaffold a skeleton project for smart contract development:
11
+
12
+ ```
13
+ npx @alephium/web3 <project-dir> [-t template-name]
14
+ ```
15
+
16
+ ## Install
17
+
18
+ ### In Node projects
19
+
20
+ ```shell
21
+ npm install @alephium/web3
22
+ ```
23
+
24
+ 💥 Until our library is stable, breaking changes will be introduced in **minor** versions (instead of the traditional major versions of semver). We recommend allowing patch-level updates and to always read the [release notes][release-notes] for breaking changes.
25
+
26
+ ```js
27
+ // package.json
28
+ {
29
+ "dependencies": {
30
+ "@alephium/web3": "~X.Y.Z"
31
+ }
32
+ }
33
+ ```
34
+
35
+ ### In browser projects
36
+
37
+ All you have to do is to include the library in your HTML document. The `alephium` global variable will be available.
38
+
39
+ ```html
40
+ <script src="alephium-web3.min.js"></script>
41
+ <script>
42
+ const { walletGenerate } = alephium
43
+ const wallet = walletGenerate()
44
+ console.log(wallet)
45
+ </script>
46
+ ```
47
+
48
+ You can either build the library by cloning this repo and running the build script (the file will be located at `/dist/alephium-web3.min.js`), or simply using a CDN.
49
+
50
+ ```shell
51
+ npm run build
52
+ ```
53
+
54
+ #### via UNPKG CDN
55
+
56
+ ```html
57
+ <script src="https://unpkg.com/@alephium/web3@X.Y.Z/dist/alephium-web3.min.js"></script>
58
+ ```
59
+
60
+ #### via jsDelivr CDN
61
+
62
+ ```html
63
+ <script src="https://cdn.jsdelivr.net/npm/@alephium/web3@X.Y.Z/dist/alephium-web3.min.js"></script>
64
+ ```
65
+
66
+ ## Development
67
+
68
+ ### Update schemas
69
+
70
+ One first needs to update the version number of `alephium` and `explorer-backend` in `package.json`. Kindly note that one needs to check the compatibility of both OpenAPI files manually.
71
+
72
+ Typings can automatically generated using the following command:
73
+
74
+ ```shell
75
+ npm run update-schemas
76
+ ```
77
+
78
+ ### Packaging
79
+
80
+ We need to include the `.gitignore` file inside the npm package so that it can be used by the `dist/cli/create-project.js` script. To do that we define the `prepack` and `postpack` npm scripts that will rename the `.gitignore` file to `gitignore`, pack it into the package, and rename it back to `.gitignore`. Similar approach has been followed by `create-react-app`<sup>[1]</sup>.
81
+
82
+ ### Release
83
+
84
+ To release a new version:
85
+
86
+ 1. Create a commit that updates the package version in package.json and package-lock.json and a tag with:
87
+ ```shell
88
+ npm version patch # if you want to bump the patch version, without breaking changes
89
+ npm version minor # if you want to bump the minor version, with breaking changes
90
+ npm version prerelease --preid=rc # if you want to create a release candidate
91
+ npm version prerelease --preid=leman # if you want to create a leman prerelease
92
+ ```
93
+ 2. Push the tag to GitHub and trigger the publish workflow that will publish it on NPM with:
94
+
95
+ ```shell
96
+ git push [remote] <tag>
97
+ ```
98
+
99
+ 3. Unless you are on `master`, create a new branch and push it to GitHub so that the tagged commit belongs to a branch of this repo with:
100
+ ```shell
101
+ git checkout -b <tag>
102
+ git push
103
+ ```
104
+ Otherwise, just push to `master`.
105
+
106
+ ## Build
107
+
108
+ Compile the TypeScript files into JavaScript:
109
+
110
+ ```shell
111
+ npm run build
112
+ ```
113
+
114
+ ## Testing
115
+
116
+ ```shell
117
+ npm run start-devnet # this will start a devnet for smart contract tests
118
+ npm test
119
+ ```
120
+
121
+ or, to watch for changes:
122
+
123
+ ```shell
124
+ npm run test:watch
125
+ ```
126
+
127
+ [test-badge]: https://github.com/alephium/alephium-web3/actions/workflows/test.yml/badge.svg
128
+ [test-link]: https://github.com/alephium/alephium-web3/actions/workflows/test.yml
129
+ [coverage-badge]: https://codecov.io/gh/alephium/alephium-web3/branch/master/graph/badge.svg
130
+ [coverage-link]: https://codecov.io/gh/alephium/alephium-web3
131
+ [npm-badge]: https://img.shields.io/npm/v/@alephium/web3.svg
132
+ [npm-link]: https://www.npmjs.org/package/@alephium/web3
133
+ [prettier-badge]: https://img.shields.io/badge/code_style-prettier-ff69b4.svg
134
+ [prettier-link]: https://github.com/prettier/prettier
135
+ [release-notes]: https://github.com/alephium/alephium-web3/releases
136
+ [1]: https://github.com/facebook/create-react-app/blob/2da5517689b7510ff8d8b0148ce372782cb285d7/packages/react-scripts/scripts/init.js#L264-L278
@@ -0,0 +1,13 @@
1
+ Contract Add(sub: Sub, mut result : U256) {
2
+ event Add(x: U256, y: U256)
3
+
4
+ pub fn add(array: [U256; 2]) -> ([U256; 2]) {
5
+ return addPrivate(array)
6
+ }
7
+
8
+ fn addPrivate(array: [U256; 2]) -> ([U256; 2]) {
9
+ emit Add(array[0], array[1])
10
+ result = result + array[0] + array[1]
11
+ return [result, sub.sub(array)]
12
+ }
13
+ }
@@ -1,4 +1,4 @@
1
- TxScript Main(greeterContractId: ByteVec) {
1
+ TxScript GreeterMain(greeterContractId: ByteVec) {
2
2
  let greeter0 = Greeter(greeterContractId)
3
3
  assert!(greeter0.greet() == 1, 0)
4
4
 
@@ -0,0 +1,4 @@
1
+ TxScript Main(addContractId: ByteVec) {
2
+ let add = Add(addContractId)
3
+ add.add([2, 1])
4
+ }
@@ -0,0 +1,10 @@
1
+ Contract Sub(mut result : U256) {
2
+ event Sub(x: U256, y: U256)
3
+
4
+ @using(externalCallCheck = false)
5
+ pub fn sub(array: [U256; 2]) -> U256 {
6
+ emit Sub(array[0], array[1])
7
+ result = result + array[0] - array[1]
8
+ return result
9
+ }
10
+ }
@@ -0,0 +1,18 @@
1
+ Contract MetaData() {
2
+ @using(preapprovedAssets = true, assetsInContract = false)
3
+ pub fn foo() -> () {
4
+ transferAlph!(callerAddress!(), callerAddress!(), 1 alph)
5
+ return
6
+ }
7
+
8
+ @using(preapprovedAssets = false, assetsInContract = true)
9
+ fn bar() -> () {
10
+ transferAlphToSelf!(selfAddress!(), 1 alph)
11
+ return
12
+ }
13
+
14
+ @using(readonly = true)
15
+ fn baz() -> () {
16
+ return
17
+ }
18
+ }
@@ -0,0 +1,8 @@
1
+ Contract Warnings(@unused a: U256, b: U256) {
2
+ const C = 0
3
+
4
+ @using(readonly = true)
5
+ pub fn foo(@unused x: U256, y: U256) -> () {
6
+ return
7
+ }
8
+ }