@alephium/web3 0.2.0-test.1 → 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.
- package/.eslintignore +2 -2
- package/README.md +2 -135
- package/dist/alephium-web3.min.js +1 -1
- package/dist/alephium-web3.min.js.LICENSE.txt +0 -17
- package/dist/alephium-web3.min.js.map +1 -1
- package/dist/src/api/api-alephium.d.ts +115 -17
- package/dist/src/api/api-alephium.js +145 -80
- package/dist/src/api/api-explorer.d.ts +178 -51
- package/dist/src/api/api-explorer.js +172 -37
- package/dist/src/api/index.d.ts +40 -5
- package/dist/src/api/index.js +115 -7
- package/dist/src/api/types.d.ts +23 -0
- package/dist/src/api/types.js +235 -0
- package/dist/src/api/utils.d.ts +6 -0
- package/dist/{scripts/rename-gitignore.js → src/api/utils.js} +11 -6
- package/dist/src/contract/contract.d.ts +68 -55
- package/dist/src/contract/contract.js +235 -384
- package/dist/src/contract/events.d.ts +4 -4
- package/dist/src/contract/events.js +2 -1
- package/dist/src/contract/index.js +5 -1
- package/dist/src/contract/ralph.d.ts +5 -4
- package/dist/src/contract/ralph.js +27 -1
- package/dist/src/global.d.ts +6 -2
- package/dist/src/global.js +19 -3
- package/dist/src/index.d.ts +2 -1
- package/dist/src/index.js +23 -2
- package/dist/src/signer/index.d.ts +0 -1
- package/dist/src/signer/index.js +5 -2
- package/dist/src/signer/signer.d.ts +59 -60
- package/dist/src/signer/signer.js +99 -70
- package/dist/src/transaction/index.d.ts +0 -1
- package/dist/src/transaction/index.js +5 -2
- package/dist/src/transaction/status.d.ts +2 -1
- package/dist/src/transaction/status.js +2 -1
- package/dist/src/utils/bs58.d.ts +1 -0
- package/dist/src/utils/bs58.js +13 -1
- package/dist/src/utils/index.d.ts +0 -1
- package/dist/src/utils/index.js +5 -2
- package/dist/src/utils/subscription.d.ts +0 -2
- package/dist/src/utils/subscription.js +0 -2
- package/dist/src/utils/utils.d.ts +4 -9
- package/dist/src/utils/utils.js +20 -24
- package/jest-config.json +11 -0
- package/package.json +11 -45
- package/src/api/api-alephium.ts +162 -25
- package/src/api/api-explorer.ts +247 -54
- package/src/api/index.ts +140 -6
- package/src/api/types.ts +229 -0
- package/{scripts/rename-gitignore.js → src/api/utils.ts} +7 -6
- package/src/contract/contract.ts +398 -429
- package/src/contract/events.ts +6 -5
- package/src/contract/ralph.ts +29 -4
- package/src/global.ts +23 -3
- package/src/index.ts +7 -1
- package/src/signer/index.ts +0 -1
- package/src/signer/signer.ts +165 -135
- package/src/transaction/index.ts +0 -1
- package/src/transaction/status.ts +5 -2
- package/src/utils/bs58.ts +11 -0
- package/src/utils/index.ts +0 -1
- package/src/utils/subscription.ts +0 -4
- package/src/utils/utils.ts +11 -19
- package/webpack.config.js +3 -0
- package/.eslintrc.json +0 -21
- package/LICENSE +0 -165
- package/contracts/add/add.ral +0 -13
- package/contracts/greeter/greeter.ral +0 -7
- package/contracts/greeter/greeter_interface.ral +0 -4
- package/contracts/greeter_main.ral +0 -7
- package/contracts/main.ral +0 -4
- package/contracts/sub/sub.ral +0 -10
- package/contracts/test/metadata.ral +0 -18
- package/contracts/test/warnings.ral +0 -8
- package/dev/user.conf +0 -29
- package/dist/scripts/create-project.d.ts +0 -2
- package/dist/scripts/create-project.js +0 -125
- package/dist/scripts/rename-gitignore.d.ts +0 -1
- package/dist/scripts/start-devnet.d.ts +0 -1
- package/dist/scripts/start-devnet.js +0 -131
- package/dist/scripts/stop-devnet.d.ts +0 -1
- package/dist/scripts/stop-devnet.js +0 -32
- package/dist/src/signer/node-wallet.d.ts +0 -11
- package/dist/src/signer/node-wallet.js +0 -57
- package/dist/src/test/index.d.ts +0 -6
- package/dist/src/test/index.js +0 -41
- package/dist/src/test/privatekey-wallet.d.ts +0 -11
- package/dist/src/test/privatekey-wallet.js +0 -68
- package/dist/src/transaction/sign-verify.d.ts +0 -2
- package/dist/src/transaction/sign-verify.js +0 -58
- package/dist/src/utils/password-crypto.d.ts +0 -2
- package/dist/src/utils/password-crypto.js +0 -69
- package/gitignore +0 -9
- package/scripts/create-project.ts +0 -137
- package/scripts/start-devnet.js +0 -141
- package/scripts/stop-devnet.js +0 -32
- package/src/contract/ralph.test.ts +0 -178
- package/src/fixtures/address.json +0 -36
- package/src/fixtures/balance.json +0 -9
- package/src/fixtures/self-clique.json +0 -19
- package/src/fixtures/transaction.json +0 -13
- package/src/fixtures/transactions.json +0 -179
- package/src/signer/fixtures/genesis.json +0 -26
- package/src/signer/fixtures/wallets.json +0 -26
- package/src/signer/node-wallet.ts +0 -65
- package/src/test/index.ts +0 -31
- package/src/test/privatekey-wallet.ts +0 -57
- package/src/transaction/sign-verify.test.ts +0 -50
- package/src/transaction/sign-verify.ts +0 -39
- package/src/utils/address.test.ts +0 -47
- package/src/utils/djb2.test.ts +0 -35
- package/src/utils/password-crypto.test.ts +0 -27
- package/src/utils/password-crypto.ts +0 -77
- package/src/utils/utils.test.ts +0 -161
- package/templates/base/README.md +0 -34
- package/templates/base/package.json +0 -35
- package/templates/base/src/greeter.ts +0 -42
- package/templates/base/tsconfig.json +0 -19
- package/templates/react/README.md +0 -34
- package/templates/react/config-overrides.js +0 -18
- package/templates/react/package.json +0 -66
- package/templates/react/src/App.tsx +0 -42
- package/templates/react/src/artifacts/greeter.ral.json +0 -26
- package/templates/react/src/artifacts/greeter_main.ral.json +0 -22
- package/templates/shared/.eslintrc.json +0 -12
- package/templates/shared/scripts/header.js +0 -0
- package/test/contract.test.ts +0 -213
- package/test/events.test.ts +0 -141
- package/test/transaction.test.ts +0 -73
package/src/api/types.ts
ADDED
|
@@ -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
|
-
|
|
20
|
-
|
|
21
|
-
|
|
22
|
-
|
|
23
|
-
|
|
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
|
+
}
|