@alephium/web3 1.3.0 → 1.5.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/dist/alephium-web3.min.js +1 -1
- package/dist/alephium-web3.min.js.map +1 -1
- package/dist/src/address/address.js +11 -5
- package/dist/src/api/api-alephium.d.ts +2 -4
- package/dist/src/api/api-alephium.js +1 -1
- package/dist/src/codec/array-codec.d.ts +4 -12
- package/dist/src/codec/array-codec.js +15 -28
- package/dist/src/codec/asset-output-codec.d.ts +6 -11
- package/dist/src/codec/asset-output-codec.js +32 -71
- package/dist/src/codec/bigint-codec.d.ts +2 -1
- package/dist/src/codec/bigint-codec.js +14 -2
- package/dist/src/codec/bytestring-codec.d.ts +6 -11
- package/dist/src/codec/bytestring-codec.js +9 -23
- package/dist/src/codec/codec.d.ts +54 -5
- package/dist/src/codec/codec.js +112 -14
- package/dist/src/codec/compact-int-codec.d.ts +65 -44
- package/dist/src/codec/compact-int-codec.js +222 -204
- package/dist/src/codec/contract-codec.d.ts +5 -8
- package/dist/src/codec/contract-codec.js +15 -29
- package/dist/src/codec/contract-output-codec.d.ts +4 -10
- package/dist/src/codec/contract-output-codec.js +20 -40
- package/dist/src/codec/contract-output-ref-codec.d.ts +2 -8
- package/dist/src/codec/contract-output-ref-codec.js +7 -17
- package/dist/src/codec/either-codec.d.ts +8 -15
- package/dist/src/codec/either-codec.js +5 -46
- package/dist/src/codec/index.d.ts +4 -3
- package/dist/src/codec/index.js +7 -4
- package/dist/src/codec/input-codec.d.ts +4 -10
- package/dist/src/codec/input-codec.js +11 -46
- package/dist/src/codec/instr-codec.d.ts +633 -40
- package/dist/src/codec/instr-codec.js +1040 -434
- package/dist/src/codec/int-as-4bytes-codec.d.ts +7 -0
- package/dist/src/codec/{signed-int-codec.js → int-as-4bytes-codec.js} +6 -12
- package/dist/src/codec/lockup-script-codec.d.ts +23 -26
- package/dist/src/codec/lockup-script-codec.js +12 -58
- package/dist/src/codec/method-codec.d.ts +6 -18
- package/dist/src/codec/method-codec.js +20 -48
- package/dist/src/codec/option-codec.d.ts +8 -13
- package/dist/src/codec/option-codec.js +14 -32
- package/dist/src/codec/output-codec.d.ts +2 -2
- package/dist/src/codec/output-codec.js +1 -1
- package/dist/src/codec/reader.d.ts +8 -0
- package/dist/src/codec/reader.js +48 -0
- package/dist/src/codec/script-codec.d.ts +6 -14
- package/dist/src/codec/script-codec.js +6 -22
- package/dist/src/codec/signature-codec.d.ts +4 -12
- package/dist/src/codec/signature-codec.js +3 -15
- package/dist/src/codec/timestamp-codec.d.ts +8 -0
- package/dist/src/codec/timestamp-codec.js +39 -0
- package/dist/src/codec/token-codec.d.ts +3 -10
- package/dist/src/codec/token-codec.js +6 -24
- package/dist/src/codec/transaction-codec.d.ts +6 -11
- package/dist/src/codec/transaction-codec.js +24 -49
- package/dist/src/codec/unlock-script-codec.d.ts +25 -36
- package/dist/src/codec/unlock-script-codec.js +26 -147
- package/dist/src/codec/unsigned-tx-codec.d.ts +8 -14
- package/dist/src/codec/unsigned-tx-codec.js +24 -66
- package/dist/src/codec/val.d.ts +27 -0
- package/dist/src/codec/val.js +33 -0
- package/dist/src/contract/contract.d.ts +1 -0
- package/dist/src/contract/contract.js +20 -13
- package/dist/src/contract/index.d.ts +1 -0
- package/dist/src/contract/index.js +1 -0
- package/dist/src/contract/ralph.d.ts +0 -4
- package/dist/src/contract/ralph.js +50 -179
- package/dist/src/contract/script-simulator.d.ts +27 -0
- package/dist/src/contract/script-simulator.js +757 -0
- package/dist/src/exchange/exchange.js +1 -1
- package/package.json +3 -4
- package/src/address/address.ts +15 -9
- package/src/api/api-alephium.ts +2 -4
- package/src/codec/array-codec.ts +16 -34
- package/src/codec/asset-output-codec.ts +38 -83
- package/src/codec/bigint-codec.ts +16 -2
- package/src/codec/bytestring-codec.ts +10 -28
- package/src/codec/codec.ts +121 -15
- package/src/codec/compact-int-codec.ts +230 -207
- package/src/codec/contract-codec.ts +20 -33
- package/src/codec/contract-output-codec.ts +22 -48
- package/src/codec/contract-output-ref-codec.ts +6 -17
- package/src/codec/either-codec.ts +4 -53
- package/src/codec/index.ts +4 -3
- package/src/codec/input-codec.ts +14 -36
- package/src/codec/instr-codec.ts +1229 -455
- package/src/codec/{signed-int-codec.ts → int-as-4bytes-codec.ts} +6 -10
- package/src/codec/lockup-script-codec.ts +28 -76
- package/src/codec/method-codec.ts +23 -61
- package/src/codec/option-codec.ts +13 -36
- package/src/codec/output-codec.ts +2 -2
- package/src/codec/reader.ts +56 -0
- package/src/codec/script-codec.ts +9 -31
- package/src/codec/signature-codec.ts +3 -18
- package/src/codec/timestamp-codec.ts +42 -0
- package/src/codec/token-codec.ts +7 -26
- package/src/codec/transaction-codec.ts +29 -58
- package/src/codec/unlock-script-codec.ts +44 -171
- package/src/codec/unsigned-tx-codec.ts +34 -63
- package/src/codec/val.ts +40 -0
- package/src/contract/contract.ts +24 -20
- package/src/contract/index.ts +1 -0
- package/src/contract/ralph.ts +76 -172
- package/src/contract/script-simulator.ts +838 -0
- package/src/exchange/exchange.ts +1 -1
- package/dist/src/codec/long-codec.d.ts +0 -8
- package/dist/src/codec/long-codec.js +0 -55
- package/dist/src/codec/signed-int-codec.d.ts +0 -8
- package/src/codec/long-codec.ts +0 -58
package/src/contract/index.ts
CHANGED
package/src/contract/ralph.ts
CHANGED
|
@@ -17,147 +17,43 @@ along with the library. If not, see <http://www.gnu.org/licenses/>.
|
|
|
17
17
|
*/
|
|
18
18
|
|
|
19
19
|
import { Val, decodeArrayType, toApiAddress, toApiBoolean, toApiByteVec, toApiNumber256, PrimitiveTypes } from '../api'
|
|
20
|
-
import { HexString, binToHex, bs58,
|
|
20
|
+
import { HexString, binToHex, bs58, hexToBinUnsafe, isHexString } from '../utils'
|
|
21
21
|
import { Fields, FieldsSig, Struct } from './contract'
|
|
22
|
-
import {
|
|
23
|
-
|
|
24
|
-
|
|
25
|
-
|
|
26
|
-
|
|
27
|
-
|
|
28
|
-
|
|
29
|
-
|
|
30
|
-
|
|
31
|
-
|
|
32
|
-
|
|
33
|
-
|
|
34
|
-
|
|
35
|
-
|
|
36
|
-
|
|
37
|
-
|
|
38
|
-
|
|
39
|
-
|
|
40
|
-
|
|
41
|
-
|
|
42
|
-
|
|
43
|
-
|
|
44
|
-
|
|
45
|
-
|
|
46
|
-
|
|
47
|
-
|
|
48
|
-
|
|
49
|
-
}
|
|
50
|
-
|
|
51
|
-
export function encodeBool(bool: boolean): Uint8Array {
|
|
52
|
-
return bool ? Uint8Array.from([1]) : Uint8Array.from([0])
|
|
53
|
-
}
|
|
54
|
-
|
|
55
|
-
export function decodeBool(bytes: Uint8Array): boolean {
|
|
56
|
-
if (bytes.length !== 1) {
|
|
57
|
-
throw new Error(`Expected one byte for encoded bool, got ${bytes.length}`)
|
|
58
|
-
}
|
|
59
|
-
return bytes[0] === 1 ? true : false
|
|
60
|
-
}
|
|
61
|
-
|
|
62
|
-
export function encodeI256(i256: bigint): Uint8Array {
|
|
63
|
-
if (i256 >= bigIntZero) {
|
|
64
|
-
return encodeI256Positive(i256)
|
|
65
|
-
} else {
|
|
66
|
-
return encodeI256Negative(i256)
|
|
67
|
-
}
|
|
68
|
-
}
|
|
69
|
-
|
|
70
|
-
// n should be positive
|
|
71
|
-
function toByteArray(n: bigint, signed: boolean, notBit: boolean): Uint8Array {
|
|
72
|
-
let hex = n.toString(16)
|
|
73
|
-
if (hex.length % 2 === 1) {
|
|
74
|
-
hex = '0' + hex
|
|
75
|
-
} else if (signed && hex[0] >= '8') {
|
|
76
|
-
hex = '00' + hex // add the byte for sign
|
|
77
|
-
}
|
|
78
|
-
|
|
79
|
-
const byteLength = hex.length / 2
|
|
80
|
-
const bytes = new Uint8Array(byteLength + 1)
|
|
81
|
-
for (let index = 0; index < byteLength; index++) {
|
|
82
|
-
const offset = index * 2
|
|
83
|
-
const byte = parseInt(hex.slice(offset, offset + 2), 16)
|
|
84
|
-
bytes[`${index + 1}`] = notBit ? ~byte : byte
|
|
85
|
-
}
|
|
86
|
-
|
|
87
|
-
const header = byteLength - 4 + CompactInt.multiBytePrefix
|
|
88
|
-
bytes[0] = header
|
|
89
|
-
return bytes
|
|
90
|
-
}
|
|
91
|
-
|
|
92
|
-
function encodeI256Positive(i256: bigint): Uint8Array {
|
|
93
|
-
if (i256 < Signed.oneByteBound) {
|
|
94
|
-
return new Uint8Array([Number(i256) + CompactInt.oneBytePrefix])
|
|
95
|
-
} else if (i256 < Signed.twoByteBound) {
|
|
96
|
-
const num = Number(i256)
|
|
97
|
-
return new Uint8Array([(num >> 8) + CompactInt.twoBytePrefix, num & 0xff])
|
|
98
|
-
} else if (i256 < Signed.fourByteBound) {
|
|
99
|
-
const num = Number(i256)
|
|
100
|
-
return new Uint8Array([(num >> 24) + CompactInt.fourBytePrefix, (num >> 16) & 0xff, (num >> 8) & 0xff, num & 0xff])
|
|
101
|
-
} else if (i256 < Signed.i256UpperBound) {
|
|
102
|
-
return toByteArray(i256, true, false)
|
|
103
|
-
} else {
|
|
104
|
-
throw Error(`Too large number for i256: ${i256}`)
|
|
105
|
-
}
|
|
106
|
-
}
|
|
107
|
-
|
|
108
|
-
function encodeI256Negative(i256: bigint): Uint8Array {
|
|
109
|
-
if (i256 >= -Signed.oneByteBound) {
|
|
110
|
-
const num = Number(i256)
|
|
111
|
-
return new Uint8Array([(num ^ CompactInt.oneByteNegPrefix) & 0xff])
|
|
112
|
-
} else if (i256 >= -Signed.twoByteBound) {
|
|
113
|
-
const num = Number(i256)
|
|
114
|
-
return new Uint8Array([((num >> 8) ^ CompactInt.twoByteNegPrefix) & 0xff, num & 0xff])
|
|
115
|
-
} else if (i256 >= -Signed.fourByteBound) {
|
|
116
|
-
const num = Number(i256)
|
|
117
|
-
return new Uint8Array([
|
|
118
|
-
((num >> 24) ^ CompactInt.fourByteNegPrefix) & 0xff,
|
|
119
|
-
(num >> 16) & 0xff,
|
|
120
|
-
(num >> 8) & 0xff,
|
|
121
|
-
num & 0xff
|
|
122
|
-
])
|
|
123
|
-
} else if (i256 >= Signed.i256LowerBound) {
|
|
124
|
-
return toByteArray(~i256, true, true)
|
|
125
|
-
} else {
|
|
126
|
-
throw Error(`Too small number for i256: ${i256}`)
|
|
127
|
-
}
|
|
128
|
-
}
|
|
129
|
-
|
|
130
|
-
export function encodeU256(u256: bigint): Uint8Array {
|
|
131
|
-
if (u256 < bigIntZero) {
|
|
132
|
-
throw Error(`Negative number for U256: ${u256}`)
|
|
133
|
-
} else if (u256 < UnSigned.oneByteBound) {
|
|
134
|
-
return new Uint8Array([Number(u256) + CompactInt.oneBytePrefix])
|
|
135
|
-
} else if (u256 < UnSigned.twoByteBound) {
|
|
136
|
-
const num = Number(u256)
|
|
137
|
-
return new Uint8Array([((num >> 8) & 0xff) + CompactInt.twoBytePrefix, num & 0xff])
|
|
138
|
-
} else if (u256 < UnSigned.fourByteBound) {
|
|
139
|
-
const num = Number(u256)
|
|
140
|
-
return new Uint8Array([
|
|
141
|
-
((num >> 24) & 0xff) + CompactInt.fourBytePrefix,
|
|
142
|
-
(num >> 16) & 0xff,
|
|
143
|
-
(num >> 8) & 0xff,
|
|
144
|
-
num & 0xff
|
|
145
|
-
])
|
|
146
|
-
} else if (u256 < UnSigned.u256UpperBound) {
|
|
147
|
-
return toByteArray(u256, false, false)
|
|
148
|
-
} else {
|
|
149
|
-
throw Error(`Too large number for U256: ${u256}`)
|
|
150
|
-
}
|
|
151
|
-
}
|
|
22
|
+
import {
|
|
23
|
+
AddressConstCode,
|
|
24
|
+
BytesConstCode,
|
|
25
|
+
byteStringCodec,
|
|
26
|
+
ConstFalse,
|
|
27
|
+
ConstTrue,
|
|
28
|
+
i256Codec,
|
|
29
|
+
I256Const,
|
|
30
|
+
I256Const0,
|
|
31
|
+
I256Const1,
|
|
32
|
+
I256Const2,
|
|
33
|
+
I256Const3,
|
|
34
|
+
I256Const4,
|
|
35
|
+
I256Const5,
|
|
36
|
+
I256ConstN1,
|
|
37
|
+
i32Codec,
|
|
38
|
+
instrCodec,
|
|
39
|
+
u256Codec,
|
|
40
|
+
U256Const,
|
|
41
|
+
U256Const0,
|
|
42
|
+
U256Const1,
|
|
43
|
+
U256Const2,
|
|
44
|
+
U256Const3,
|
|
45
|
+
U256Const4,
|
|
46
|
+
U256Const5
|
|
47
|
+
} from '../codec'
|
|
48
|
+
import { boolCodec } from '../codec/codec'
|
|
152
49
|
|
|
153
50
|
export function encodeByteVec(hex: string): Uint8Array {
|
|
154
51
|
if (!isHexString(hex)) {
|
|
155
52
|
throw Error(`Given value ${hex} is not a valid hex string`)
|
|
156
53
|
}
|
|
157
54
|
|
|
158
|
-
const
|
|
159
|
-
|
|
160
|
-
return concatBytes([bytes1, bytes0])
|
|
55
|
+
const bytes = hexToBinUnsafe(hex)
|
|
56
|
+
return byteStringCodec.encode(bytes)
|
|
161
57
|
}
|
|
162
58
|
|
|
163
59
|
export function encodeAddress(address: string): Uint8Array {
|
|
@@ -173,15 +69,15 @@ export enum VmValType {
|
|
|
173
69
|
}
|
|
174
70
|
|
|
175
71
|
export function encodeVmBool(bool: boolean): Uint8Array {
|
|
176
|
-
return new Uint8Array([VmValType.Bool, ...
|
|
72
|
+
return new Uint8Array([VmValType.Bool, ...boolCodec.encode(bool)])
|
|
177
73
|
}
|
|
178
74
|
|
|
179
75
|
export function encodeVmI256(i256: bigint): Uint8Array {
|
|
180
|
-
return new Uint8Array([VmValType.I256, ...
|
|
76
|
+
return new Uint8Array([VmValType.I256, ...i256Codec.encode(i256)])
|
|
181
77
|
}
|
|
182
78
|
|
|
183
79
|
export function encodeVmU256(u256: bigint): Uint8Array {
|
|
184
|
-
return new Uint8Array([VmValType.U256, ...
|
|
80
|
+
return new Uint8Array([VmValType.U256, ...u256Codec.encode(u256)])
|
|
185
81
|
}
|
|
186
82
|
|
|
187
83
|
export function encodeVmByteVec(bytes: string): Uint8Array {
|
|
@@ -220,36 +116,44 @@ function invalidScriptField(tpe: string, value: Val): Error {
|
|
|
220
116
|
return Error(`Invalid script field ${value} for type ${tpe}`)
|
|
221
117
|
}
|
|
222
118
|
|
|
223
|
-
enum Instruction {
|
|
224
|
-
trueConst = 3,
|
|
225
|
-
falseConst = 4,
|
|
226
|
-
i256Const0 = 5,
|
|
227
|
-
i256Const1 = 6,
|
|
228
|
-
i256Const2 = 7,
|
|
229
|
-
i256Const3 = 8,
|
|
230
|
-
i256Const4 = 9,
|
|
231
|
-
i256Const5 = 10,
|
|
232
|
-
i256ConstN1 = 11,
|
|
233
|
-
u256Const0 = 12,
|
|
234
|
-
u256Const1 = 13,
|
|
235
|
-
u256Const2 = 14,
|
|
236
|
-
u256Const3 = 15,
|
|
237
|
-
u256Const4 = 16,
|
|
238
|
-
u256Const5 = 17,
|
|
239
|
-
i256Const = 18,
|
|
240
|
-
u256Const = 19,
|
|
241
|
-
bytesConst = 20,
|
|
242
|
-
addressConst = 21
|
|
243
|
-
}
|
|
244
|
-
|
|
245
|
-
// TODO: optimize
|
|
246
119
|
function encodeScriptFieldI256(value: bigint): Uint8Array {
|
|
247
|
-
|
|
120
|
+
switch (value) {
|
|
121
|
+
case 0n:
|
|
122
|
+
return instrCodec.encode(I256Const0)
|
|
123
|
+
case 1n:
|
|
124
|
+
return instrCodec.encode(I256Const1)
|
|
125
|
+
case 2n:
|
|
126
|
+
return instrCodec.encode(I256Const2)
|
|
127
|
+
case 3n:
|
|
128
|
+
return instrCodec.encode(I256Const3)
|
|
129
|
+
case 4n:
|
|
130
|
+
return instrCodec.encode(I256Const4)
|
|
131
|
+
case 5n:
|
|
132
|
+
return instrCodec.encode(I256Const5)
|
|
133
|
+
case -1n:
|
|
134
|
+
return instrCodec.encode(I256ConstN1)
|
|
135
|
+
default:
|
|
136
|
+
return instrCodec.encode(I256Const(value))
|
|
137
|
+
}
|
|
248
138
|
}
|
|
249
139
|
|
|
250
|
-
// TODO: optimize
|
|
251
140
|
function encodeScriptFieldU256(value: bigint): Uint8Array {
|
|
252
|
-
|
|
141
|
+
switch (value) {
|
|
142
|
+
case 0n:
|
|
143
|
+
return instrCodec.encode(U256Const0)
|
|
144
|
+
case 1n:
|
|
145
|
+
return instrCodec.encode(U256Const1)
|
|
146
|
+
case 2n:
|
|
147
|
+
return instrCodec.encode(U256Const2)
|
|
148
|
+
case 3n:
|
|
149
|
+
return instrCodec.encode(U256Const3)
|
|
150
|
+
case 4n:
|
|
151
|
+
return instrCodec.encode(U256Const4)
|
|
152
|
+
case 5n:
|
|
153
|
+
return instrCodec.encode(U256Const5)
|
|
154
|
+
default:
|
|
155
|
+
return instrCodec.encode(U256Const(value))
|
|
156
|
+
}
|
|
253
157
|
}
|
|
254
158
|
|
|
255
159
|
export function encodeScriptFieldAsString(tpe: string, value: Val): string {
|
|
@@ -259,7 +163,7 @@ export function encodeScriptFieldAsString(tpe: string, value: Val): string {
|
|
|
259
163
|
export function encodeScriptField(tpe: string, value: Val): Uint8Array {
|
|
260
164
|
switch (tpe) {
|
|
261
165
|
case 'Bool':
|
|
262
|
-
const byte = toApiBoolean(value) ?
|
|
166
|
+
const byte = toApiBoolean(value) ? ConstTrue.code : ConstFalse.code
|
|
263
167
|
return new Uint8Array([byte])
|
|
264
168
|
case 'I256':
|
|
265
169
|
const i256 = toApiNumber256(value)
|
|
@@ -269,10 +173,10 @@ export function encodeScriptField(tpe: string, value: Val): Uint8Array {
|
|
|
269
173
|
return encodeScriptFieldU256(BigInt(u256))
|
|
270
174
|
case 'Address':
|
|
271
175
|
const address = toApiAddress(value)
|
|
272
|
-
return new Uint8Array([
|
|
176
|
+
return new Uint8Array([AddressConstCode, ...encodeAddress(address)])
|
|
273
177
|
default: // ByteVec or Contract
|
|
274
178
|
const hexStr = toApiByteVec(value)
|
|
275
|
-
return new Uint8Array([
|
|
179
|
+
return new Uint8Array([BytesConstCode, ...encodeByteVec(hexStr)])
|
|
276
180
|
}
|
|
277
181
|
|
|
278
182
|
throw invalidScriptField(tpe, value)
|
|
@@ -375,11 +279,11 @@ export function tryDecodeMapDebugLog(
|
|
|
375
279
|
export function decodePrimitive(value: Uint8Array, type: string): Val {
|
|
376
280
|
switch (type) {
|
|
377
281
|
case 'Bool':
|
|
378
|
-
return
|
|
282
|
+
return boolCodec.decode(value)
|
|
379
283
|
case 'I256':
|
|
380
|
-
return
|
|
284
|
+
return i256Codec.decode(value)
|
|
381
285
|
case 'U256':
|
|
382
|
-
return
|
|
286
|
+
return u256Codec.decode(value)
|
|
383
287
|
case 'ByteVec':
|
|
384
288
|
return binToHex(value)
|
|
385
289
|
case 'Address':
|
|
@@ -396,10 +300,10 @@ export function encodeMapKey(value: Val, type: string): Uint8Array {
|
|
|
396
300
|
return new Uint8Array([byte])
|
|
397
301
|
case 'I256':
|
|
398
302
|
const i256 = toApiNumber256(value)
|
|
399
|
-
return
|
|
303
|
+
return i256Codec.encode(BigInt(i256))
|
|
400
304
|
case 'U256':
|
|
401
305
|
const u256 = toApiNumber256(value)
|
|
402
|
-
return
|
|
306
|
+
return u256Codec.encode(BigInt(u256))
|
|
403
307
|
case 'ByteVec':
|
|
404
308
|
const hexStr = toApiByteVec(value)
|
|
405
309
|
return hexToBinUnsafe(hexStr)
|
|
@@ -527,7 +431,7 @@ function _encodeField<T>(fieldName: string, encodeFunc: () => T): T {
|
|
|
527
431
|
}
|
|
528
432
|
|
|
529
433
|
function encodeFields(fields: { name: string; type: string; value: Val }[]): Uint8Array {
|
|
530
|
-
const prefix =
|
|
434
|
+
const prefix = i32Codec.encode(fields.length)
|
|
531
435
|
return fields.reduce((acc, field) => {
|
|
532
436
|
const encoded = _encodeField(field.name, () => encodeContractField(field.type, field.value))
|
|
533
437
|
const bytes = new Uint8Array(acc.byteLength + encoded.byteLength)
|