@alephium/web3 0.2.0-test.0 → 0.2.0

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 (126) hide show
  1. package/.eslintignore +2 -2
  2. package/README.md +2 -135
  3. package/dist/alephium-web3.min.js +1 -1
  4. package/dist/alephium-web3.min.js.LICENSE.txt +0 -17
  5. package/dist/alephium-web3.min.js.map +1 -1
  6. package/dist/src/api/api-alephium.d.ts +152 -24
  7. package/dist/src/api/api-alephium.js +163 -82
  8. package/dist/src/api/api-explorer.d.ts +192 -49
  9. package/dist/src/api/api-explorer.js +195 -34
  10. package/dist/src/api/index.d.ts +40 -5
  11. package/dist/src/api/index.js +115 -7
  12. package/dist/src/api/types.d.ts +23 -0
  13. package/dist/src/api/types.js +235 -0
  14. package/dist/src/api/utils.d.ts +6 -0
  15. package/dist/{scripts/rename-gitignore.js → src/api/utils.js} +11 -6
  16. package/dist/src/contract/contract.d.ts +127 -80
  17. package/dist/src/contract/contract.js +425 -467
  18. package/dist/src/contract/events.d.ts +4 -4
  19. package/dist/src/contract/events.js +2 -1
  20. package/dist/src/contract/index.js +5 -1
  21. package/dist/src/contract/ralph.d.ts +5 -4
  22. package/dist/src/contract/ralph.js +27 -1
  23. package/dist/src/global.d.ts +7 -0
  24. package/dist/src/global.js +54 -0
  25. package/dist/src/index.d.ts +2 -0
  26. package/dist/src/index.js +23 -1
  27. package/dist/src/signer/index.d.ts +0 -1
  28. package/dist/src/signer/index.js +5 -2
  29. package/dist/src/signer/signer.d.ts +59 -60
  30. package/dist/src/signer/signer.js +99 -69
  31. package/dist/src/transaction/index.d.ts +0 -1
  32. package/dist/src/transaction/index.js +5 -2
  33. package/dist/src/transaction/status.d.ts +2 -1
  34. package/dist/src/transaction/status.js +2 -1
  35. package/dist/src/utils/bs58.d.ts +1 -0
  36. package/dist/src/utils/bs58.js +13 -1
  37. package/dist/src/utils/index.d.ts +0 -1
  38. package/dist/src/utils/index.js +5 -2
  39. package/dist/src/utils/subscription.d.ts +0 -3
  40. package/dist/src/utils/subscription.js +0 -1
  41. package/dist/src/utils/utils.d.ts +6 -11
  42. package/dist/src/utils/utils.js +22 -26
  43. package/jest-config.json +11 -0
  44. package/package.json +11 -47
  45. package/src/api/api-alephium.ts +219 -33
  46. package/src/api/api-explorer.ts +275 -52
  47. package/src/api/index.ts +140 -6
  48. package/src/api/types.ts +229 -0
  49. package/{scripts/rename-gitignore.js → src/api/utils.ts} +7 -6
  50. package/src/contract/contract.ts +663 -581
  51. package/src/contract/events.ts +8 -7
  52. package/src/contract/ralph.ts +29 -4
  53. package/src/global.ts +56 -0
  54. package/src/index.ts +7 -0
  55. package/src/signer/index.ts +0 -1
  56. package/src/signer/signer.ts +165 -134
  57. package/src/transaction/index.ts +0 -1
  58. package/src/transaction/status.ts +6 -3
  59. package/src/utils/bs58.ts +11 -0
  60. package/src/utils/index.ts +0 -1
  61. package/src/utils/subscription.ts +1 -5
  62. package/src/utils/utils.ts +15 -23
  63. package/webpack.config.js +3 -0
  64. package/.eslintrc.json +0 -21
  65. package/LICENSE +0 -165
  66. package/contracts/add/add.ral +0 -16
  67. package/contracts/greeter/greeter.ral +0 -7
  68. package/contracts/greeter/greeter_interface.ral +0 -3
  69. package/contracts/greeter_main.ral +0 -9
  70. package/contracts/main.ral +0 -6
  71. package/contracts/sub/sub.ral +0 -9
  72. package/dev/user.conf +0 -29
  73. package/dist/scripts/create-project.d.ts +0 -2
  74. package/dist/scripts/create-project.js +0 -124
  75. package/dist/scripts/rename-gitignore.d.ts +0 -1
  76. package/dist/scripts/start-devnet.d.ts +0 -1
  77. package/dist/scripts/start-devnet.js +0 -131
  78. package/dist/scripts/stop-devnet.d.ts +0 -1
  79. package/dist/scripts/stop-devnet.js +0 -32
  80. package/dist/src/signer/node-wallet.d.ts +0 -13
  81. package/dist/src/signer/node-wallet.js +0 -60
  82. package/dist/src/test/index.d.ts +0 -7
  83. package/dist/src/test/index.js +0 -41
  84. package/dist/src/test/privatekey-wallet.d.ts +0 -12
  85. package/dist/src/test/privatekey-wallet.js +0 -68
  86. package/dist/src/transaction/sign-verify.d.ts +0 -2
  87. package/dist/src/transaction/sign-verify.js +0 -58
  88. package/dist/src/utils/password-crypto.d.ts +0 -2
  89. package/dist/src/utils/password-crypto.js +0 -69
  90. package/gitignore +0 -10
  91. package/scripts/create-project.ts +0 -136
  92. package/scripts/start-devnet.js +0 -141
  93. package/scripts/stop-devnet.js +0 -32
  94. package/src/contract/ralph.test.ts +0 -178
  95. package/src/fixtures/address.json +0 -36
  96. package/src/fixtures/balance.json +0 -9
  97. package/src/fixtures/self-clique.json +0 -19
  98. package/src/fixtures/transaction.json +0 -13
  99. package/src/fixtures/transactions.json +0 -179
  100. package/src/signer/fixtures/genesis.json +0 -26
  101. package/src/signer/fixtures/wallets.json +0 -26
  102. package/src/signer/node-wallet.ts +0 -74
  103. package/src/test/index.ts +0 -32
  104. package/src/test/privatekey-wallet.ts +0 -58
  105. package/src/transaction/sign-verify.test.ts +0 -50
  106. package/src/transaction/sign-verify.ts +0 -39
  107. package/src/utils/address.test.ts +0 -47
  108. package/src/utils/djb2.test.ts +0 -35
  109. package/src/utils/password-crypto.test.ts +0 -27
  110. package/src/utils/password-crypto.ts +0 -77
  111. package/src/utils/utils.test.ts +0 -161
  112. package/templates/base/README.md +0 -34
  113. package/templates/base/package.json +0 -35
  114. package/templates/base/src/greeter.ts +0 -41
  115. package/templates/base/tsconfig.json +0 -19
  116. package/templates/react/README.md +0 -34
  117. package/templates/react/config-overrides.js +0 -18
  118. package/templates/react/package.json +0 -66
  119. package/templates/react/src/App.tsx +0 -42
  120. package/templates/react/src/artifacts/greeter.ral.json +0 -26
  121. package/templates/react/src/artifacts/greeter_main.ral.json +0 -22
  122. package/templates/shared/.eslintrc.json +0 -12
  123. package/templates/shared/scripts/header.js +0 -0
  124. package/test/contract.test.ts +0 -178
  125. package/test/events.test.ts +0 -138
  126. package/test/transaction.test.ts +0 -72
@@ -0,0 +1,229 @@
1
+ /*
2
+ Copyright 2018 - 2022 The Alephium Authors
3
+ This file is part of the alephium project.
4
+
5
+ The library is free software: you can redistribute it and/or modify
6
+ it under the terms of the GNU Lesser General Public License as published by
7
+ the Free Software Foundation, either version 3 of the License, or
8
+ (at your option) any later version.
9
+
10
+ The library is distributed in the hope that it will be useful,
11
+ but WITHOUT ANY WARRANTY; without even the implied warranty of
12
+ MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
13
+ GNU Lesser General Public License for more details.
14
+
15
+ You should have received a copy of the GNU Lesser General Public License
16
+ along with the library. If not, see <http://www.gnu.org/licenses/>.
17
+ */
18
+
19
+ import { assertType, bs58, Eq } from '../utils'
20
+ import * as node from './api-alephium'
21
+
22
+ export type Number256 = bigint
23
+ export type Val = Number256 | boolean | string | Val[]
24
+ export type NamedVals = Record<string, Val>
25
+
26
+ export interface Token {
27
+ id: string
28
+ amount: Number256
29
+ }
30
+ assertType<Eq<keyof Token, keyof node.Token>>
31
+
32
+ export function toApiToken(token: Token): node.Token {
33
+ return { id: token.id, amount: toApiNumber256(token.amount) }
34
+ }
35
+
36
+ export function toApiTokens(tokens?: Token[]): node.Token[] | undefined {
37
+ return tokens?.map(toApiToken)
38
+ }
39
+
40
+ export function fromApiToken(token: node.Token): Token {
41
+ return { id: token.id, amount: fromApiNumber256(token.amount) }
42
+ }
43
+
44
+ export function fromApiTokens(tokens?: node.Token[]): Token[] | undefined {
45
+ return tokens?.map(fromApiToken)
46
+ }
47
+
48
+ export function toApiBoolean(v: Val): boolean {
49
+ if (typeof v === 'boolean') {
50
+ return v
51
+ } else {
52
+ throw new Error(`Invalid boolean value: ${v}`)
53
+ }
54
+ }
55
+
56
+ // TODO: check integer bounds
57
+ export function toApiNumber256(v: Val): string {
58
+ if ((typeof v === 'number' && Number.isInteger(v)) || typeof v === 'bigint') {
59
+ return v.toString()
60
+ } else if (typeof v === 'string') {
61
+ return v
62
+ } else {
63
+ throw new Error(`Invalid 256 bit number: ${v}`)
64
+ }
65
+ }
66
+
67
+ export function toApiNumber256Optional(v?: Val): string | undefined {
68
+ return v === undefined ? undefined : toApiNumber256(v)
69
+ }
70
+
71
+ export function fromApiNumber256(n: string): bigint {
72
+ return BigInt(n)
73
+ }
74
+
75
+ // TODO: check hex string
76
+ export function toApiByteVec(v: Val): string {
77
+ if (typeof v === 'string') {
78
+ // try to convert from address to contract id
79
+ try {
80
+ const address = bs58.decode(v)
81
+ if (address.length == 33 && address[0] == 3) {
82
+ return Buffer.from(address.slice(1)).toString('hex')
83
+ }
84
+ } catch (_) {
85
+ return v as string
86
+ }
87
+ return v as string
88
+ } else {
89
+ throw new Error(`Invalid string: ${v}`)
90
+ }
91
+ }
92
+
93
+ export function toApiAddress(v: Val): string {
94
+ if (typeof v === 'string') {
95
+ try {
96
+ bs58.decode(v)
97
+ return v as string
98
+ } catch (error) {
99
+ throw new Error(`Invalid base58 string: ${v}`)
100
+ }
101
+ } else {
102
+ throw new Error(`Invalid string: ${v}`)
103
+ }
104
+ }
105
+
106
+ export function toApiArray(tpe: string, v: Val): node.Val {
107
+ if (!Array.isArray(v)) {
108
+ throw new Error(`Expected array, got ${v}`)
109
+ }
110
+
111
+ const semiColonIndex = tpe.lastIndexOf(';')
112
+ if (semiColonIndex == -1) {
113
+ throw new Error(`Invalid Val type: ${tpe}`)
114
+ }
115
+
116
+ const subType = tpe.slice(1, semiColonIndex)
117
+ const dim = parseInt(tpe.slice(semiColonIndex + 1, -1))
118
+ if ((v as Val[]).length != dim) {
119
+ throw new Error(`Invalid val dimension: ${v}`)
120
+ } else {
121
+ return { value: (v as Val[]).map((v) => toApiVal(v, subType)), type: 'Array' }
122
+ }
123
+ }
124
+
125
+ export function toApiVal(v: Val, tpe: string): node.Val {
126
+ if (tpe === 'Bool') {
127
+ return { value: toApiBoolean(v), type: tpe }
128
+ } else if (tpe === 'U256' || tpe === 'I256') {
129
+ return { value: toApiNumber256(v), type: tpe }
130
+ } else if (tpe === 'ByteVec') {
131
+ return { value: toApiByteVec(v), type: tpe }
132
+ } else if (tpe === 'Address') {
133
+ return { value: toApiAddress(v), type: tpe }
134
+ } else {
135
+ return toApiArray(tpe, v)
136
+ }
137
+ }
138
+
139
+ function _fromApiVal(vals: node.Val[], valIndex: number, tpe: string): [result: Val, nextIndex: number] {
140
+ if (vals.length === 0) {
141
+ throw new Error('Not enough Vals')
142
+ }
143
+
144
+ const firstVal = vals[`${valIndex}`]
145
+ if (tpe === 'Bool' && firstVal.type === tpe) {
146
+ return [firstVal.value as boolean, valIndex + 1]
147
+ } else if ((tpe === 'U256' || tpe === 'I256') && firstVal.type === tpe) {
148
+ return [fromApiNumber256(firstVal.value as string), valIndex + 1]
149
+ } else if ((tpe === 'ByteVec' || tpe === 'Address') && firstVal.type === tpe) {
150
+ return [firstVal.value as string, valIndex + 1]
151
+ } else {
152
+ const [baseType, dims] = decodeArrayType(tpe)
153
+ const arraySize = dims.reduce((a, b) => a * b)
154
+ const nextIndex = valIndex + arraySize
155
+ const valsToUse = vals.slice(valIndex, nextIndex)
156
+ if (valsToUse.length == arraySize && valsToUse.every((val) => val.type === baseType)) {
157
+ const localVals = valsToUse.map((val) => fromApiVal(val, baseType))
158
+ return [foldVals(localVals, dims), nextIndex]
159
+ } else {
160
+ throw new Error(`Invalid array Val type: ${valsToUse}, ${tpe}`)
161
+ }
162
+ }
163
+ }
164
+
165
+ export function fromApiVals(vals: node.Val[], names: string[], types: string[]): NamedVals {
166
+ let valIndex = 0
167
+ const result: NamedVals = {}
168
+ types.forEach((currentType, index) => {
169
+ const currentName = names[`${index}`]
170
+ const [val, nextIndex] = _fromApiVal(vals, valIndex, currentType)
171
+ valIndex = nextIndex
172
+ result[`${currentName}`] = val
173
+ })
174
+ return result
175
+ }
176
+
177
+ export function fromApiArray(vals: node.Val[], types: string[]): Val[] {
178
+ let valIndex = 0
179
+ const result: Val[] = []
180
+ for (const currentType of types) {
181
+ const [val, nextIndex] = _fromApiVal(vals, valIndex, currentType)
182
+ result.push(val)
183
+ valIndex = nextIndex
184
+ }
185
+ return result
186
+ }
187
+
188
+ export function fromApiVal(v: node.Val, tpe: string): Val {
189
+ if (v.type === 'Bool' && v.type === tpe) {
190
+ return v.value as boolean
191
+ } else if ((v.type === 'U256' || v.type === 'I256') && v.type === tpe) {
192
+ return fromApiNumber256(v.value as string)
193
+ } else if ((v.type === 'ByteVec' || v.type === 'Address') && v.type === tpe) {
194
+ return v.value as string
195
+ } else {
196
+ throw new Error(`Invalid node.Val type: ${v}`)
197
+ }
198
+ }
199
+
200
+ function decodeArrayType(tpe: string): [baseType: string, dims: number[]] {
201
+ const semiColonIndex = tpe.lastIndexOf(';')
202
+ if (semiColonIndex === -1) {
203
+ throw new Error(`Invalid Val type: ${tpe}`)
204
+ }
205
+
206
+ const subType = tpe.slice(1, semiColonIndex)
207
+ const dim = parseInt(tpe.slice(semiColonIndex + 1, -1))
208
+ if (subType[0] == '[') {
209
+ const [baseType, subDim] = decodeArrayType(subType)
210
+ return [baseType, (subDim.unshift(dim), subDim)]
211
+ } else {
212
+ return [subType, [dim]]
213
+ }
214
+ }
215
+
216
+ function foldVals(vals: Val[], dims: number[]): Val {
217
+ if (dims.length == 1) {
218
+ return vals
219
+ } else {
220
+ const result: Val[] = []
221
+ const chunkSize = vals.length / dims[0]
222
+ const chunkDims = dims.slice(1)
223
+ for (let i = 0; i < vals.length; i += chunkSize) {
224
+ const chunk = vals.slice(i, i + chunkSize)
225
+ result.push(foldVals(chunk, chunkDims))
226
+ }
227
+ return result
228
+ }
229
+ }
@@ -16,9 +16,10 @@ You should have received a copy of the GNU Lesser General Public License
16
16
  along with the library. If not, see <http://www.gnu.org/licenses/>.
17
17
  */
18
18
 
19
- const { rename } = require('fs')
20
-
21
- rename(process.argv[2], process.argv[3], function (error) {
22
- if (error) console.log(error)
23
- console.log(`Renamed ${process.argv[2]} to ${process.argv[3]}.`)
24
- })
19
+ export function convertHttpResponse<T>(response: { data: T; error?: { detail: string } }): T {
20
+ if (response.error) {
21
+ throw new Error(`[Node API Error] - ${response.error.detail}`)
22
+ } else {
23
+ return response.data
24
+ }
25
+ }