@alephium/web3 0.42.0 → 0.44.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/{utils → address}/address.js +15 -19
- package/dist/src/address/index.d.ts +1 -0
- package/dist/src/address/index.js +34 -0
- package/dist/src/api/node-provider.js +10 -9
- package/dist/src/api/types.js +1 -1
- package/dist/src/codec/array-codec.d.ts +2 -3
- package/dist/src/codec/array-codec.js +2 -3
- package/dist/src/codec/asset-output-codec.d.ts +3 -4
- package/dist/src/codec/asset-output-codec.js +13 -15
- package/dist/src/codec/bigint-codec.d.ts +2 -3
- package/dist/src/codec/bigint-codec.js +2 -3
- package/dist/src/codec/bytestring-codec.d.ts +5 -6
- package/dist/src/codec/bytestring-codec.js +7 -7
- package/dist/src/codec/codec.d.ts +2 -3
- package/dist/src/codec/compact-int-codec.d.ts +11 -15
- package/dist/src/codec/compact-int-codec.js +23 -33
- package/dist/src/codec/contract-codec.d.ts +5 -6
- package/dist/src/codec/contract-codec.js +26 -26
- package/dist/src/codec/contract-output-codec.d.ts +2 -3
- package/dist/src/codec/contract-output-codec.js +9 -11
- package/dist/src/codec/contract-output-ref-codec.d.ts +3 -4
- package/dist/src/codec/contract-output-ref-codec.js +2 -2
- package/dist/src/codec/either-codec.d.ts +2 -3
- package/dist/src/codec/either-codec.js +1 -2
- package/dist/src/codec/hash.d.ts +2 -3
- package/dist/src/codec/hash.js +17 -0
- package/dist/src/codec/input-codec.d.ts +3 -4
- package/dist/src/codec/input-codec.js +6 -6
- package/dist/src/codec/instr-codec.d.ts +2 -3
- package/dist/src/codec/instr-codec.js +1 -2
- package/dist/src/codec/lockup-script-codec.d.ts +5 -6
- package/dist/src/codec/lockup-script-codec.js +1 -2
- package/dist/src/codec/long-codec.d.ts +2 -3
- package/dist/src/codec/long-codec.js +1 -2
- package/dist/src/codec/method-codec.d.ts +2 -3
- package/dist/src/codec/method-codec.js +13 -14
- package/dist/src/codec/option-codec.d.ts +3 -4
- package/dist/src/codec/option-codec.js +2 -3
- package/dist/src/codec/script-codec.d.ts +4 -5
- package/dist/src/codec/script-codec.js +2 -4
- package/dist/src/codec/signature-codec.d.ts +3 -4
- package/dist/src/codec/signature-codec.js +17 -0
- package/dist/src/codec/signed-int-codec.d.ts +2 -3
- package/dist/src/codec/signed-int-codec.js +1 -2
- package/dist/src/codec/token-codec.d.ts +3 -4
- package/dist/src/codec/token-codec.js +3 -3
- package/dist/src/codec/transaction-codec.d.ts +4 -5
- package/dist/src/codec/transaction-codec.js +12 -13
- package/dist/src/codec/unlock-script-codec.d.ts +5 -6
- package/dist/src/codec/unlock-script-codec.js +14 -15
- package/dist/src/codec/unsigned-tx-codec.d.ts +4 -5
- package/dist/src/codec/unsigned-tx-codec.js +4 -5
- package/dist/src/contract/contract.d.ts +10 -1
- package/dist/src/contract/contract.js +197 -21
- package/dist/src/contract/ralph.d.ts +2 -1
- package/dist/src/contract/ralph.js +25 -11
- package/dist/src/{utils → exchange}/exchange.js +5 -5
- package/dist/src/exchange/index.d.ts +1 -0
- package/dist/src/exchange/index.js +25 -0
- package/dist/src/index.d.ts +2 -0
- package/dist/src/index.js +2 -0
- package/dist/src/signer/signer.js +4 -5
- package/dist/src/signer/tx-builder.js +3 -3
- package/dist/src/utils/index.d.ts +0 -2
- package/dist/src/utils/index.js +0 -7
- package/dist/src/utils/sign.js +1 -1
- package/dist/src/utils/utils.d.ts +1 -0
- package/dist/src/utils/utils.js +30 -11
- package/dist/src/utils/webcrypto.js +3 -4
- package/package.json +1 -2
- package/src/{utils → address}/address.ts +15 -19
- package/src/address/index.ts +19 -0
- package/src/api/node-provider.ts +2 -9
- package/src/api/types.ts +2 -2
- package/src/codec/array-codec.ts +5 -6
- package/src/codec/asset-output-codec.ts +20 -22
- package/src/codec/bigint-codec.ts +4 -5
- package/src/codec/bytestring-codec.ts +11 -11
- package/src/codec/codec.ts +2 -3
- package/src/codec/compact-int-codec.ts +36 -50
- package/src/codec/contract-codec.ts +30 -29
- package/src/codec/contract-output-codec.ts +13 -15
- package/src/codec/contract-output-ref-codec.ts +5 -5
- package/src/codec/either-codec.ts +3 -4
- package/src/codec/hash.ts +2 -3
- package/src/codec/input-codec.ts +10 -10
- package/src/codec/instr-codec.ts +3 -4
- package/src/codec/lockup-script-codec.ts +8 -9
- package/src/codec/long-codec.ts +3 -4
- package/src/codec/method-codec.ts +16 -17
- package/src/codec/option-codec.ts +4 -5
- package/src/codec/script-codec.ts +7 -9
- package/src/codec/signature-codec.ts +3 -4
- package/src/codec/signed-int-codec.ts +3 -4
- package/src/codec/token-codec.ts +6 -6
- package/src/codec/transaction-codec.ts +17 -18
- package/src/codec/unlock-script-codec.ts +26 -27
- package/src/codec/unsigned-tx-codec.ts +10 -11
- package/src/contract/contract.ts +274 -9
- package/src/contract/ralph.ts +29 -12
- package/src/{utils → exchange}/exchange.ts +3 -11
- package/src/exchange/index.ts +19 -0
- package/src/index.ts +2 -0
- package/src/signer/signer.ts +2 -3
- package/src/signer/tx-builder.ts +2 -2
- package/src/utils/index.ts +0 -2
- package/src/utils/sign.ts +1 -1
- package/src/utils/utils.ts +29 -10
- package/src/utils/webcrypto.ts +3 -4
- package/webpack.config.js +1 -5
- /package/dist/src/{utils → address}/address.d.ts +0 -0
- /package/dist/src/{utils → exchange}/exchange.d.ts +0 -0
|
@@ -15,11 +15,10 @@ GNU Lesser General Public License for more details.
|
|
|
15
15
|
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
|
-
import { Buffer } from 'buffer/'
|
|
19
18
|
import { Parser } from 'binary-parser'
|
|
20
19
|
import { Codec, assert } from './codec'
|
|
21
20
|
import { BigIntCodec } from './bigint-codec'
|
|
22
|
-
import {
|
|
21
|
+
import { binToHex } from '../utils'
|
|
23
22
|
|
|
24
23
|
export class CompactInt {
|
|
25
24
|
static readonly oneBytePrefix = 0x00
|
|
@@ -66,24 +65,24 @@ export class CompactUnsignedIntCodec implements Codec<DecodedCompactInt> {
|
|
|
66
65
|
|
|
67
66
|
parser = compactIntParser
|
|
68
67
|
|
|
69
|
-
encode(input: DecodedCompactInt):
|
|
70
|
-
return
|
|
68
|
+
encode(input: DecodedCompactInt): Uint8Array {
|
|
69
|
+
return new Uint8Array([input.mode, ...input.rest])
|
|
71
70
|
}
|
|
72
71
|
|
|
73
|
-
encodeU32(value: number):
|
|
72
|
+
encodeU32(value: number): Uint8Array {
|
|
74
73
|
if (value < this.oneByteBound) {
|
|
75
|
-
return
|
|
74
|
+
return new Uint8Array([(CompactInt.oneBytePrefix + value) & 0xff])
|
|
76
75
|
} else if (value < this.twoByteBound) {
|
|
77
|
-
return
|
|
76
|
+
return new Uint8Array([(CompactInt.twoBytePrefix + (value >> 8)) & 0xff, value & 0xff])
|
|
78
77
|
} else if (value < this.fourByteBound) {
|
|
79
|
-
return
|
|
78
|
+
return new Uint8Array([
|
|
80
79
|
(CompactInt.fourBytePrefix + (value >> 24)) & 0xff,
|
|
81
80
|
(value >> 16) & 0xff,
|
|
82
81
|
(value >> 8) & 0xff,
|
|
83
82
|
value & 0xff
|
|
84
83
|
])
|
|
85
84
|
} else {
|
|
86
|
-
return
|
|
85
|
+
return new Uint8Array([
|
|
87
86
|
CompactInt.multiBytePrefix,
|
|
88
87
|
(value >> 24) & 0xff,
|
|
89
88
|
(value >> 16) & 0xff,
|
|
@@ -93,7 +92,7 @@ export class CompactUnsignedIntCodec implements Codec<DecodedCompactInt> {
|
|
|
93
92
|
}
|
|
94
93
|
}
|
|
95
94
|
|
|
96
|
-
encodeU256(value: bigint):
|
|
95
|
+
encodeU256(value: bigint): Uint8Array {
|
|
97
96
|
assert(value >= 0n, 'Value should be positive')
|
|
98
97
|
|
|
99
98
|
if (value < this.fourByteBound) {
|
|
@@ -107,40 +106,27 @@ export class CompactUnsignedIntCodec implements Codec<DecodedCompactInt> {
|
|
|
107
106
|
assert(bytes.length <= 32, 'Expect <= 32 bytes for U256')
|
|
108
107
|
|
|
109
108
|
const header = (bytes.length - 4 + CompactInt.multiBytePrefix) & 0xff
|
|
110
|
-
return
|
|
109
|
+
return new Uint8Array([header, ...bytes])
|
|
111
110
|
}
|
|
112
111
|
}
|
|
113
112
|
|
|
114
|
-
|
|
115
|
-
const decoded = this.decode(input)
|
|
116
|
-
return this.toU32(decoded)
|
|
117
|
-
}
|
|
118
|
-
|
|
119
|
-
decodeU256(input: Buffer): bigint {
|
|
113
|
+
decodeU256(input: Uint8Array): bigint {
|
|
120
114
|
const decoded = this.decode(input)
|
|
121
115
|
return this.toU256(decoded)
|
|
122
116
|
}
|
|
123
117
|
|
|
124
|
-
decode(input:
|
|
118
|
+
decode(input: Uint8Array): DecodedCompactInt {
|
|
125
119
|
return this.parser.parse(input)
|
|
126
120
|
}
|
|
127
121
|
|
|
128
|
-
toU32(value: DecodedCompactInt): number {
|
|
129
|
-
const body = Buffer.from([value.mode, ...value.rest])
|
|
130
|
-
return decodePositiveInt(value.mode, body)
|
|
131
|
-
}
|
|
132
|
-
|
|
133
|
-
fromU32(value: number): DecodedCompactInt {
|
|
134
|
-
return this.decode(this.encodeU32(value))
|
|
135
|
-
}
|
|
136
|
-
|
|
137
122
|
toU256(value: DecodedCompactInt): bigint {
|
|
138
123
|
const mode = value.mode & maskRest
|
|
139
124
|
if (fixedSize(mode)) {
|
|
140
|
-
|
|
125
|
+
const body = new Uint8Array([value.mode, ...value.rest])
|
|
126
|
+
return BigInt(decodePositiveInt(value.mode, body))
|
|
141
127
|
} else {
|
|
142
128
|
assert(value.rest.length <= 32, 'Expect <= 32 bytes for U256')
|
|
143
|
-
return BigIntCodec.decode(
|
|
129
|
+
return BigIntCodec.decode(value.rest, false)
|
|
144
130
|
}
|
|
145
131
|
}
|
|
146
132
|
|
|
@@ -159,34 +145,34 @@ export class CompactSignedIntCodec implements Codec<DecodedCompactInt> {
|
|
|
159
145
|
|
|
160
146
|
parser = compactIntParser
|
|
161
147
|
|
|
162
|
-
encode(input: DecodedCompactInt):
|
|
163
|
-
return
|
|
148
|
+
encode(input: DecodedCompactInt): Uint8Array {
|
|
149
|
+
return new Uint8Array([input.mode, ...input.rest])
|
|
164
150
|
}
|
|
165
151
|
|
|
166
|
-
decode(input:
|
|
152
|
+
decode(input: Uint8Array): DecodedCompactInt {
|
|
167
153
|
return this.parser.parse(input)
|
|
168
154
|
}
|
|
169
155
|
|
|
170
|
-
decodeI32(input:
|
|
156
|
+
decodeI32(input: Uint8Array): number {
|
|
171
157
|
const decoded = this.decode(input)
|
|
172
158
|
return this.toI32(decoded)
|
|
173
159
|
}
|
|
174
160
|
|
|
175
|
-
encodeI32(value: number):
|
|
161
|
+
encodeI32(value: number): Uint8Array {
|
|
176
162
|
if (value >= 0) {
|
|
177
163
|
if (value < this.oneByteBound) {
|
|
178
|
-
return
|
|
164
|
+
return new Uint8Array([(CompactInt.oneBytePrefix + value) & 0xff])
|
|
179
165
|
} else if (value < this.twoByteBound) {
|
|
180
|
-
return
|
|
166
|
+
return new Uint8Array([(CompactInt.twoBytePrefix + (value >> 8)) & 0xff, value & 0xff])
|
|
181
167
|
} else if (value < this.fourByteBound) {
|
|
182
|
-
return
|
|
168
|
+
return new Uint8Array([
|
|
183
169
|
(CompactInt.fourBytePrefix + (value >> 24)) & 0xff,
|
|
184
170
|
(value >> 16) & 0xff,
|
|
185
171
|
(value >> 8) & 0xff,
|
|
186
172
|
value & 0xff
|
|
187
173
|
])
|
|
188
174
|
} else {
|
|
189
|
-
return
|
|
175
|
+
return new Uint8Array([
|
|
190
176
|
CompactInt.multiBytePrefix,
|
|
191
177
|
(value >> 24) & 0xff,
|
|
192
178
|
(value >> 16) & 0xff,
|
|
@@ -196,18 +182,18 @@ export class CompactSignedIntCodec implements Codec<DecodedCompactInt> {
|
|
|
196
182
|
}
|
|
197
183
|
} else {
|
|
198
184
|
if (value >= -this.oneByteBound) {
|
|
199
|
-
return
|
|
185
|
+
return new Uint8Array([(value ^ CompactInt.oneByteNegPrefix) & 0xff])
|
|
200
186
|
} else if (value >= -this.twoByteBound) {
|
|
201
|
-
return
|
|
187
|
+
return new Uint8Array([((value >> 8) ^ CompactInt.twoByteNegPrefix) & 0xff, value & 0xff])
|
|
202
188
|
} else if (value >= -this.fourByteBound) {
|
|
203
|
-
return
|
|
189
|
+
return new Uint8Array([
|
|
204
190
|
((value >> 24) ^ CompactInt.fourByteNegPrefix) & 0xff,
|
|
205
191
|
(value >> 16) & 0xff,
|
|
206
192
|
(value >> 8) & 0xff,
|
|
207
193
|
value & 0xff
|
|
208
194
|
])
|
|
209
195
|
} else {
|
|
210
|
-
return
|
|
196
|
+
return new Uint8Array([
|
|
211
197
|
CompactInt.multiBytePrefix,
|
|
212
198
|
(value >> 24) & 0xff,
|
|
213
199
|
(value >> 16) & 0xff,
|
|
@@ -218,23 +204,23 @@ export class CompactSignedIntCodec implements Codec<DecodedCompactInt> {
|
|
|
218
204
|
}
|
|
219
205
|
}
|
|
220
206
|
|
|
221
|
-
encodeI256(value: bigint):
|
|
207
|
+
encodeI256(value: bigint): Uint8Array {
|
|
222
208
|
if (value >= -0x20000000 && value < 0x20000000) {
|
|
223
209
|
return this.encodeI32(Number(value))
|
|
224
210
|
} else {
|
|
225
211
|
const bytes = BigIntCodec.encode(value)
|
|
226
212
|
const header = (bytes.length - 4 + CompactInt.multiBytePrefix) & 0xff
|
|
227
|
-
return
|
|
213
|
+
return new Uint8Array([header, ...bytes])
|
|
228
214
|
}
|
|
229
215
|
}
|
|
230
216
|
|
|
231
|
-
decodeI256(input:
|
|
217
|
+
decodeI256(input: Uint8Array): bigint {
|
|
232
218
|
const decoded = this.decode(input)
|
|
233
219
|
return this.toI256(decoded)
|
|
234
220
|
}
|
|
235
221
|
|
|
236
222
|
toI32(value: DecodedCompactInt): number {
|
|
237
|
-
const body =
|
|
223
|
+
const body = new Uint8Array([value.mode, ...value.rest])
|
|
238
224
|
const mode = value.mode & maskRest
|
|
239
225
|
if (fixedSize(mode)) {
|
|
240
226
|
const isPositive = (value.mode & signFlag) == 0
|
|
@@ -263,7 +249,7 @@ export class CompactSignedIntCodec implements Codec<DecodedCompactInt> {
|
|
|
263
249
|
return BigInt(this.toI32(value))
|
|
264
250
|
} else {
|
|
265
251
|
assert(value.rest.length <= 32, 'Expect <= 32 bytes for I256')
|
|
266
|
-
return BigIntCodec.decode(
|
|
252
|
+
return BigIntCodec.decode(value.rest, true)
|
|
267
253
|
}
|
|
268
254
|
}
|
|
269
255
|
|
|
@@ -274,7 +260,7 @@ export class CompactSignedIntCodec implements Codec<DecodedCompactInt> {
|
|
|
274
260
|
|
|
275
261
|
export const compactSignedIntCodec = new CompactSignedIntCodec()
|
|
276
262
|
|
|
277
|
-
function decodePositiveInt(rawMode: number, body:
|
|
263
|
+
function decodePositiveInt(rawMode: number, body: Uint8Array): number {
|
|
278
264
|
const mode = rawMode & maskRest
|
|
279
265
|
|
|
280
266
|
switch (mode) {
|
|
@@ -288,14 +274,14 @@ function decodePositiveInt(rawMode: number, body: Buffer): number {
|
|
|
288
274
|
return ((body[0] & maskMode) << 24) | ((body[1] & 0xff) << 16) | ((body[2] & 0xff) << 8) | (body[3] & 0xff)
|
|
289
275
|
default:
|
|
290
276
|
if (body.length === 5) {
|
|
291
|
-
return Number(BigInt('0x' + body.slice(1)
|
|
277
|
+
return Number(BigInt('0x' + binToHex(body.slice(1))))
|
|
292
278
|
} else {
|
|
293
279
|
throw new Error(`decodePositiveInt: Expect 4 bytes int, but get ${body.length - 1} bytes int`)
|
|
294
280
|
}
|
|
295
281
|
}
|
|
296
282
|
}
|
|
297
283
|
|
|
298
|
-
function decodeNegativeInt(rawMode: number, body:
|
|
284
|
+
function decodeNegativeInt(rawMode: number, body: Uint8Array) {
|
|
299
285
|
const mode = rawMode & maskRest
|
|
300
286
|
switch (mode) {
|
|
301
287
|
case CompactInt.oneBytePrefix:
|
|
@@ -16,19 +16,19 @@ 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
|
-
import { Buffer } from 'buffer/'
|
|
20
19
|
import { Parser } from 'binary-parser'
|
|
21
20
|
import { ArrayCodec, DecodedArray } from './array-codec'
|
|
22
21
|
import { Codec } from './codec'
|
|
23
22
|
import { compactSignedIntCodec, compactUnsignedIntCodec, DecodedCompactInt } from './compact-int-codec'
|
|
24
23
|
import { Method, MethodCodec, methodCodec } from './method-codec'
|
|
24
|
+
import { concatBytes } from '../utils'
|
|
25
25
|
|
|
26
26
|
const compactSignedIntsCodec = new ArrayCodec(compactSignedIntCodec)
|
|
27
27
|
|
|
28
28
|
export interface HalfDecodedContract {
|
|
29
29
|
fieldLength: DecodedCompactInt
|
|
30
30
|
methodIndexes: DecodedArray<DecodedCompactInt>
|
|
31
|
-
methods:
|
|
31
|
+
methods: Uint8Array
|
|
32
32
|
}
|
|
33
33
|
|
|
34
34
|
export interface Contract {
|
|
@@ -36,24 +36,6 @@ export interface Contract {
|
|
|
36
36
|
methods: Method[]
|
|
37
37
|
}
|
|
38
38
|
|
|
39
|
-
export function toHalfDecoded(contract: Contract): HalfDecodedContract {
|
|
40
|
-
const fieldLength = compactSignedIntCodec.fromI32(contract.fieldLength)
|
|
41
|
-
const methods = contract.methods.map((m) => methodCodec.encode(MethodCodec.fromMethod(m)))
|
|
42
|
-
let count = 0
|
|
43
|
-
const methodIndexes = Array.from(Array(methods.length).keys()).map((index) => {
|
|
44
|
-
count += methods[`${index}`].length
|
|
45
|
-
return count
|
|
46
|
-
})
|
|
47
|
-
return {
|
|
48
|
-
fieldLength,
|
|
49
|
-
methodIndexes: {
|
|
50
|
-
length: compactSignedIntCodec.fromI32(methodIndexes.length),
|
|
51
|
-
value: methodIndexes.map((value) => compactSignedIntCodec.fromI32(value))
|
|
52
|
-
},
|
|
53
|
-
methods: methods.reduce((acc, buffer) => Buffer.concat([acc, buffer]))
|
|
54
|
-
}
|
|
55
|
-
}
|
|
56
|
-
|
|
57
39
|
export class ContractCodec implements Codec<HalfDecodedContract> {
|
|
58
40
|
parser = Parser.start()
|
|
59
41
|
.nest('fieldLength', {
|
|
@@ -64,22 +46,22 @@ export class ContractCodec implements Codec<HalfDecodedContract> {
|
|
|
64
46
|
})
|
|
65
47
|
.buffer('methods', { readUntil: 'eof' })
|
|
66
48
|
|
|
67
|
-
encode(input: HalfDecodedContract):
|
|
68
|
-
return
|
|
69
|
-
|
|
70
|
-
|
|
71
|
-
|
|
49
|
+
encode(input: HalfDecodedContract): Uint8Array {
|
|
50
|
+
return concatBytes([
|
|
51
|
+
compactSignedIntCodec.encode(input.fieldLength),
|
|
52
|
+
compactSignedIntsCodec.encode(input.methodIndexes.value),
|
|
53
|
+
input.methods
|
|
72
54
|
])
|
|
73
55
|
}
|
|
74
56
|
|
|
75
|
-
decode(input:
|
|
57
|
+
decode(input: Uint8Array): HalfDecodedContract {
|
|
76
58
|
return this.parser.parse(input)
|
|
77
59
|
}
|
|
78
60
|
|
|
79
|
-
decodeContract(input:
|
|
61
|
+
decodeContract(input: Uint8Array): Contract {
|
|
80
62
|
const halfDecoded = this.decode(input)
|
|
81
|
-
const fieldLength =
|
|
82
|
-
const methodIndexes = halfDecoded.methodIndexes.value.map((v) =>
|
|
63
|
+
const fieldLength = compactSignedIntCodec.toI32(halfDecoded.fieldLength)
|
|
64
|
+
const methodIndexes = halfDecoded.methodIndexes.value.map((v) => compactSignedIntCodec.toI32(v))
|
|
83
65
|
const methods: Method[] = []
|
|
84
66
|
for (let i = 0, start = 0; i < methodIndexes.length; i++) {
|
|
85
67
|
const end = methodIndexes[i]
|
|
@@ -90,6 +72,25 @@ export class ContractCodec implements Codec<HalfDecodedContract> {
|
|
|
90
72
|
|
|
91
73
|
return { fieldLength, methods }
|
|
92
74
|
}
|
|
75
|
+
|
|
76
|
+
encodeContract(contract: Contract): Uint8Array {
|
|
77
|
+
const fieldLength = compactSignedIntCodec.fromI32(contract.fieldLength)
|
|
78
|
+
const methods = contract.methods.map((m) => methodCodec.encode(MethodCodec.fromMethod(m)))
|
|
79
|
+
let count = 0
|
|
80
|
+
const methodIndexes = Array.from(Array(methods.length).keys()).map((index) => {
|
|
81
|
+
count += methods[`${index}`].length
|
|
82
|
+
return count
|
|
83
|
+
})
|
|
84
|
+
const halfDecoded = {
|
|
85
|
+
fieldLength,
|
|
86
|
+
methodIndexes: {
|
|
87
|
+
length: compactSignedIntCodec.fromI32(methodIndexes.length),
|
|
88
|
+
value: methodIndexes.map((value) => compactSignedIntCodec.fromI32(value))
|
|
89
|
+
},
|
|
90
|
+
methods: concatBytes(methods)
|
|
91
|
+
}
|
|
92
|
+
return this.encode(halfDecoded)
|
|
93
|
+
}
|
|
93
94
|
}
|
|
94
95
|
|
|
95
96
|
export const contractCodec = new ContractCodec()
|
|
@@ -15,16 +15,15 @@ GNU Lesser General Public License for more details.
|
|
|
15
15
|
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
|
-
import { Buffer } from 'buffer/'
|
|
19
18
|
import { Parser } from 'binary-parser'
|
|
20
19
|
import { DecodedArray } from './array-codec'
|
|
21
|
-
import { DecodedCompactInt, compactUnsignedIntCodec } from './compact-int-codec'
|
|
20
|
+
import { DecodedCompactInt, compactSignedIntCodec, compactUnsignedIntCodec } from './compact-int-codec'
|
|
22
21
|
import { P2C } from './lockup-script-codec'
|
|
23
22
|
import { Codec } from './codec'
|
|
24
23
|
import { Token, tokensCodec } from './token-codec'
|
|
25
24
|
import { ContractOutput as ApiContractOutput } from '../api/api-alephium'
|
|
26
25
|
import { blakeHash, createHint } from './hash'
|
|
27
|
-
import { binToHex, bs58 } from '../utils'
|
|
26
|
+
import { binToHex, bs58, concatBytes, hexToBinUnsafe } from '../utils'
|
|
28
27
|
import { signedIntCodec } from './signed-int-codec'
|
|
29
28
|
import { lockupScriptCodec } from './lockup-script-codec'
|
|
30
29
|
|
|
@@ -46,26 +45,26 @@ export class ContractOutputCodec implements Codec<ContractOutput> {
|
|
|
46
45
|
type: tokensCodec.parser
|
|
47
46
|
})
|
|
48
47
|
|
|
49
|
-
encode(input: ContractOutput):
|
|
50
|
-
const amount =
|
|
48
|
+
encode(input: ContractOutput): Uint8Array {
|
|
49
|
+
const amount = compactUnsignedIntCodec.encode(input.amount)
|
|
51
50
|
const lockupScript = input.lockupScript.contractId
|
|
52
|
-
const tokens =
|
|
51
|
+
const tokens = tokensCodec.encode(input.tokens.value)
|
|
53
52
|
|
|
54
|
-
return
|
|
53
|
+
return concatBytes([amount, lockupScript, tokens])
|
|
55
54
|
}
|
|
56
55
|
|
|
57
|
-
decode(input:
|
|
56
|
+
decode(input: Uint8Array): ContractOutput {
|
|
58
57
|
return this.parser.parse(input)
|
|
59
58
|
}
|
|
60
59
|
|
|
61
60
|
static convertToApiContractOutput(txIdBytes: Uint8Array, output: ContractOutput, index: number): ApiContractOutput {
|
|
62
61
|
const hint = createHint(output.lockupScript.contractId)
|
|
63
|
-
const key = binToHex(blakeHash(
|
|
62
|
+
const key = binToHex(blakeHash(concatBytes([txIdBytes, signedIntCodec.encode(index)])))
|
|
64
63
|
const attoAlphAmount = compactUnsignedIntCodec.toU256(output.amount).toString()
|
|
65
|
-
const address = bs58.encode(
|
|
64
|
+
const address = bs58.encode(new Uint8Array([0x03, ...output.lockupScript.contractId]))
|
|
66
65
|
const tokens = output.tokens.value.map((token) => {
|
|
67
66
|
return {
|
|
68
|
-
id: token.tokenId
|
|
67
|
+
id: binToHex(token.tokenId),
|
|
69
68
|
amount: compactUnsignedIntCodec.toU256(token.amount).toString()
|
|
70
69
|
}
|
|
71
70
|
})
|
|
@@ -74,17 +73,16 @@ export class ContractOutputCodec implements Codec<ContractOutput> {
|
|
|
74
73
|
|
|
75
74
|
static convertToOutput(apiContractOutput: ApiContractOutput): ContractOutput {
|
|
76
75
|
const amount: DecodedCompactInt = compactUnsignedIntCodec.fromU256(BigInt(apiContractOutput.attoAlphAmount))
|
|
77
|
-
const lockupScript: P2C = lockupScriptCodec.decode(
|
|
78
|
-
.script as P2C
|
|
76
|
+
const lockupScript: P2C = lockupScriptCodec.decode(bs58.decode(apiContractOutput.address)).script as P2C
|
|
79
77
|
|
|
80
78
|
const tokensValue = apiContractOutput.tokens.map((token) => {
|
|
81
79
|
return {
|
|
82
|
-
tokenId:
|
|
80
|
+
tokenId: hexToBinUnsafe(token.id),
|
|
83
81
|
amount: compactUnsignedIntCodec.fromU256(BigInt(token.amount))
|
|
84
82
|
}
|
|
85
83
|
})
|
|
86
84
|
const tokens: DecodedArray<Token> = {
|
|
87
|
-
length:
|
|
85
|
+
length: compactSignedIntCodec.fromI32(tokensValue.length),
|
|
88
86
|
value: tokensValue
|
|
89
87
|
}
|
|
90
88
|
|
|
@@ -15,25 +15,25 @@ GNU Lesser General Public License for more details.
|
|
|
15
15
|
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
|
-
import { Buffer } from 'buffer/'
|
|
19
18
|
import { Parser } from 'binary-parser'
|
|
20
19
|
import { ArrayCodec } from './array-codec'
|
|
21
20
|
import { Codec } from './codec'
|
|
22
21
|
import { signedIntCodec } from './signed-int-codec'
|
|
22
|
+
import { concatBytes } from '../utils'
|
|
23
23
|
|
|
24
24
|
export interface ContractOutputRef {
|
|
25
25
|
hint: number
|
|
26
|
-
key:
|
|
26
|
+
key: Uint8Array
|
|
27
27
|
}
|
|
28
28
|
|
|
29
29
|
export class ContractOutputRefCodec implements Codec<ContractOutputRef> {
|
|
30
30
|
parser = Parser.start().int32('hint').buffer('key', { length: 32 })
|
|
31
31
|
|
|
32
|
-
encode(input: ContractOutputRef):
|
|
33
|
-
return
|
|
32
|
+
encode(input: ContractOutputRef): Uint8Array {
|
|
33
|
+
return concatBytes([signedIntCodec.encode(input.hint), input.key])
|
|
34
34
|
}
|
|
35
35
|
|
|
36
|
-
decode(input:
|
|
36
|
+
decode(input: Uint8Array): ContractOutputRef {
|
|
37
37
|
return this.parser.parse(input)
|
|
38
38
|
}
|
|
39
39
|
}
|
|
@@ -15,7 +15,6 @@ GNU Lesser General Public License for more details.
|
|
|
15
15
|
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
|
-
import { Buffer } from 'buffer/'
|
|
19
18
|
import { Parser } from 'binary-parser'
|
|
20
19
|
import { Codec } from './codec'
|
|
21
20
|
|
|
@@ -39,17 +38,17 @@ export class EitherCodec<L, R> implements Codec<Either<L, R>> {
|
|
|
39
38
|
})
|
|
40
39
|
) {}
|
|
41
40
|
|
|
42
|
-
encode(input: Either<L, R>):
|
|
41
|
+
encode(input: Either<L, R>): Uint8Array {
|
|
43
42
|
const result = [input.either]
|
|
44
43
|
if (input.either === 0) {
|
|
45
44
|
result.push(...this.leftCodec.encode(input.value as L))
|
|
46
45
|
} else {
|
|
47
46
|
result.push(...this.rightCodec.encode(input.value as R))
|
|
48
47
|
}
|
|
49
|
-
return
|
|
48
|
+
return new Uint8Array(result)
|
|
50
49
|
}
|
|
51
50
|
|
|
52
|
-
decode(input:
|
|
51
|
+
decode(input: Uint8Array): Either<L, R> {
|
|
53
52
|
const result = this.parser.parse(input)
|
|
54
53
|
return {
|
|
55
54
|
...result,
|
package/src/codec/hash.ts
CHANGED
|
@@ -15,14 +15,13 @@ GNU Lesser General Public License for more details.
|
|
|
15
15
|
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
|
-
import { Buffer } from 'buffer/'
|
|
19
18
|
import blake from 'blakejs'
|
|
20
19
|
|
|
21
20
|
export function blakeHash(raw: Uint8Array) {
|
|
22
21
|
return blake.blake2b(raw, undefined, 32)
|
|
23
22
|
}
|
|
24
23
|
|
|
25
|
-
export function djbIntHash(bytes:
|
|
24
|
+
export function djbIntHash(bytes: Uint8Array): number {
|
|
26
25
|
let hash = 5381
|
|
27
26
|
bytes.forEach((byte) => {
|
|
28
27
|
hash = (hash << 5) + hash + (byte & 0xff)
|
|
@@ -30,6 +29,6 @@ export function djbIntHash(bytes: Buffer): number {
|
|
|
30
29
|
return hash
|
|
31
30
|
}
|
|
32
31
|
|
|
33
|
-
export function createHint(input:
|
|
32
|
+
export function createHint(input: Uint8Array): number {
|
|
34
33
|
return djbIntHash(input) | 1
|
|
35
34
|
}
|
package/src/codec/input-codec.ts
CHANGED
|
@@ -15,10 +15,9 @@ GNU Lesser General Public License for more details.
|
|
|
15
15
|
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
|
-
import { Buffer } from 'buffer/'
|
|
19
18
|
import { Parser } from 'binary-parser'
|
|
20
19
|
import { AssetInput } from '../api/api-alephium'
|
|
21
|
-
import { binToHex } from '../utils'
|
|
20
|
+
import { binToHex, concatBytes, hexToBinUnsafe } from '../utils'
|
|
22
21
|
import { UnlockScript, unlockScriptCodec } from './unlock-script-codec'
|
|
23
22
|
import { Codec } from './codec'
|
|
24
23
|
import { signedIntCodec } from './signed-int-codec'
|
|
@@ -27,7 +26,7 @@ import { ArrayCodec } from './array-codec'
|
|
|
27
26
|
export interface Input {
|
|
28
27
|
outputRef: {
|
|
29
28
|
hint: number
|
|
30
|
-
key:
|
|
29
|
+
key: Uint8Array
|
|
31
30
|
}
|
|
32
31
|
unlockScript: UnlockScript
|
|
33
32
|
}
|
|
@@ -41,14 +40,15 @@ export class InputCodec implements Codec<Input> {
|
|
|
41
40
|
type: unlockScriptCodec.parser
|
|
42
41
|
})
|
|
43
42
|
|
|
44
|
-
encode(input: Input):
|
|
45
|
-
return
|
|
46
|
-
|
|
43
|
+
encode(input: Input): Uint8Array {
|
|
44
|
+
return concatBytes([
|
|
45
|
+
signedIntCodec.encode(input.outputRef.hint),
|
|
46
|
+
input.outputRef.key,
|
|
47
47
|
unlockScriptCodec.encode(input.unlockScript)
|
|
48
48
|
])
|
|
49
49
|
}
|
|
50
50
|
|
|
51
|
-
decode(input:
|
|
51
|
+
decode(input: Uint8Array): Input {
|
|
52
52
|
return this.parser.parse(input)
|
|
53
53
|
}
|
|
54
54
|
|
|
@@ -59,7 +59,7 @@ export class InputCodec implements Codec<Input> {
|
|
|
59
59
|
const unlockScript = unlockScriptCodec.encode(input.unlockScript)
|
|
60
60
|
return {
|
|
61
61
|
outputRef: { hint, key },
|
|
62
|
-
unlockScript: unlockScript
|
|
62
|
+
unlockScript: binToHex(unlockScript)
|
|
63
63
|
}
|
|
64
64
|
})
|
|
65
65
|
}
|
|
@@ -67,8 +67,8 @@ export class InputCodec implements Codec<Input> {
|
|
|
67
67
|
static fromAssetInputs(inputs: AssetInput[]): Input[] {
|
|
68
68
|
return inputs.map((input) => {
|
|
69
69
|
const hint = input.outputRef.hint
|
|
70
|
-
const key =
|
|
71
|
-
const unlockScript = unlockScriptCodec.decode(
|
|
70
|
+
const key = hexToBinUnsafe(input.outputRef.key)
|
|
71
|
+
const unlockScript = unlockScriptCodec.decode(hexToBinUnsafe(input.unlockScript))
|
|
72
72
|
return {
|
|
73
73
|
outputRef: { hint, key },
|
|
74
74
|
unlockScript
|
package/src/codec/instr-codec.ts
CHANGED
|
@@ -15,7 +15,6 @@ GNU Lesser General Public License for more details.
|
|
|
15
15
|
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
|
-
import { Buffer } from 'buffer/'
|
|
19
18
|
import { Parser } from 'binary-parser'
|
|
20
19
|
import { ArrayCodec, DecodedArray } from './array-codec'
|
|
21
20
|
import { compactUnsignedIntCodec, compactSignedIntCodec, DecodedCompactInt } from './compact-int-codec'
|
|
@@ -458,7 +457,7 @@ export class InstrCodec implements Codec<Instr> {
|
|
|
458
457
|
}
|
|
459
458
|
})
|
|
460
459
|
|
|
461
|
-
encode(instr: Instr):
|
|
460
|
+
encode(instr: Instr): Uint8Array {
|
|
462
461
|
const instrValue = instr.value
|
|
463
462
|
const result = [instr.code]
|
|
464
463
|
const instrsWithIndex = [0x00, 0x01, 0x16, 0x17, 0xa0, 0xa1, 0xce]
|
|
@@ -480,10 +479,10 @@ export class InstrCodec implements Codec<Instr> {
|
|
|
480
479
|
result.push(...signedIntCodec.encode((instrValue as InstrValueWithIndex).index))
|
|
481
480
|
}
|
|
482
481
|
|
|
483
|
-
return
|
|
482
|
+
return new Uint8Array(result)
|
|
484
483
|
}
|
|
485
484
|
|
|
486
|
-
decode(input:
|
|
485
|
+
decode(input: Uint8Array): Instr {
|
|
487
486
|
return this.parser.parse(input)
|
|
488
487
|
}
|
|
489
488
|
}
|
|
@@ -15,24 +15,23 @@ GNU Lesser General Public License for more details.
|
|
|
15
15
|
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
|
-
import { Buffer } from 'buffer/'
|
|
19
18
|
import { Parser } from 'binary-parser'
|
|
20
19
|
import { DecodedCompactInt, compactSignedIntCodec } from './compact-int-codec'
|
|
21
20
|
import { Codec } from './codec'
|
|
22
21
|
import { ArrayCodec, DecodedArray } from './array-codec'
|
|
23
22
|
|
|
24
23
|
export interface PublicKeyHash {
|
|
25
|
-
publicKeyHash:
|
|
24
|
+
publicKeyHash: Uint8Array
|
|
26
25
|
}
|
|
27
26
|
|
|
28
27
|
class PublicKeyHashCodec implements Codec<PublicKeyHash> {
|
|
29
28
|
parser = Parser.start().buffer('publicKeyHash', { length: 32 })
|
|
30
29
|
|
|
31
|
-
encode(input: PublicKeyHash):
|
|
30
|
+
encode(input: PublicKeyHash): Uint8Array {
|
|
32
31
|
return input.publicKeyHash
|
|
33
32
|
}
|
|
34
33
|
|
|
35
|
-
decode(input:
|
|
34
|
+
decode(input: Uint8Array): PublicKeyHash {
|
|
36
35
|
return this.parser.parse(input)
|
|
37
36
|
}
|
|
38
37
|
}
|
|
@@ -48,11 +47,11 @@ export interface MultiSig {
|
|
|
48
47
|
}
|
|
49
48
|
|
|
50
49
|
export interface P2SH {
|
|
51
|
-
scriptHash:
|
|
50
|
+
scriptHash: Uint8Array
|
|
52
51
|
}
|
|
53
52
|
|
|
54
53
|
export interface P2C {
|
|
55
|
-
contractId:
|
|
54
|
+
contractId: Uint8Array
|
|
56
55
|
}
|
|
57
56
|
|
|
58
57
|
export interface LockupScript {
|
|
@@ -73,7 +72,7 @@ export class LockupScriptCodec implements Codec<LockupScript> {
|
|
|
73
72
|
}
|
|
74
73
|
})
|
|
75
74
|
|
|
76
|
-
encode(input: LockupScript):
|
|
75
|
+
encode(input: LockupScript): Uint8Array {
|
|
77
76
|
const result: number[] = [input.scriptType]
|
|
78
77
|
if (input.scriptType === 0) {
|
|
79
78
|
result.push(...(input.script as PublicKeyHash).publicKeyHash)
|
|
@@ -88,10 +87,10 @@ export class LockupScriptCodec implements Codec<LockupScript> {
|
|
|
88
87
|
throw new Error(`Unsupported script type: ${input.scriptType}`)
|
|
89
88
|
}
|
|
90
89
|
|
|
91
|
-
return
|
|
90
|
+
return new Uint8Array(result)
|
|
92
91
|
}
|
|
93
92
|
|
|
94
|
-
decode(input:
|
|
93
|
+
decode(input: Uint8Array): LockupScript {
|
|
95
94
|
return this.parser.parse(input)
|
|
96
95
|
}
|
|
97
96
|
}
|