@alephium/web3 0.37.0 → 0.38.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.
- package/dist/alephium-web3.min.js +1 -1
- package/dist/alephium-web3.min.js.map +1 -1
- package/dist/src/api/api-alephium.d.ts +5 -0
- package/dist/src/api/types.d.ts +1 -1
- package/dist/src/codec/array-codec.d.ts +17 -0
- package/dist/src/codec/array-codec.js +59 -0
- package/dist/src/codec/asset-output-codec.d.ts +27 -0
- package/dist/src/codec/asset-output-codec.js +135 -0
- package/dist/src/codec/bigint-codec.d.ts +5 -0
- package/dist/src/codec/bigint-codec.js +86 -0
- package/dist/src/codec/bytestring-codec.d.ts +16 -0
- package/dist/src/codec/bytestring-codec.js +50 -0
- package/dist/src/codec/codec.d.ts +8 -0
- package/dist/src/codec/codec.js +9 -0
- package/dist/src/codec/compact-int-codec.d.ts +51 -0
- package/dist/src/codec/compact-int-codec.js +300 -0
- package/dist/src/codec/contract-codec.d.ts +23 -0
- package/dist/src/codec/contract-codec.js +81 -0
- package/dist/src/codec/contract-output-codec.d.ts +21 -0
- package/dist/src/codec/contract-output-codec.js +82 -0
- package/dist/src/codec/contract-output-ref-codec.d.ts +15 -0
- package/dist/src/codec/contract-output-ref-codec.js +38 -0
- package/dist/src/codec/either-codec.d.ts +17 -0
- package/dist/src/codec/either-codec.js +67 -0
- package/dist/src/codec/hash.d.ts +4 -0
- package/dist/src/codec/hash.js +23 -0
- package/dist/src/codec/index.d.ts +23 -0
- package/dist/src/codec/index.js +69 -0
- package/dist/src/codec/input-codec.d.ts +22 -0
- package/dist/src/codec/input-codec.js +71 -0
- package/dist/src/codec/instr-codec.d.ts +230 -0
- package/dist/src/codec/instr-codec.js +471 -0
- package/dist/src/codec/lockup-script-codec.d.ts +28 -0
- package/dist/src/codec/lockup-script-codec.js +80 -0
- package/dist/src/codec/long-codec.d.ts +9 -0
- package/dist/src/codec/long-codec.js +56 -0
- package/dist/src/codec/method-codec.d.ts +31 -0
- package/dist/src/codec/method-codec.js +78 -0
- package/dist/src/codec/option-codec.d.ts +15 -0
- package/dist/src/codec/option-codec.js +55 -0
- package/dist/src/codec/output-codec.d.ts +7 -0
- package/dist/src/codec/output-codec.js +26 -0
- package/dist/src/codec/script-codec.d.ts +16 -0
- package/dist/src/codec/script-codec.js +41 -0
- package/dist/src/codec/signature-codec.d.ts +14 -0
- package/dist/src/codec/signature-codec.js +19 -0
- package/dist/src/codec/signed-int-codec.d.ts +9 -0
- package/dist/src/codec/signed-int-codec.js +39 -0
- package/dist/src/codec/token-codec.d.ts +16 -0
- package/dist/src/codec/token-codec.js +46 -0
- package/dist/src/codec/transaction-codec.d.ts +27 -0
- package/dist/src/codec/transaction-codec.js +128 -0
- package/dist/src/codec/unlock-script-codec.d.ts +40 -0
- package/dist/src/codec/unlock-script-codec.js +170 -0
- package/dist/src/codec/unsigned-tx-codec.d.ts +30 -0
- package/dist/src/codec/unsigned-tx-codec.js +103 -0
- package/dist/src/contract/contract.d.ts +10 -4
- package/dist/src/contract/contract.js +184 -11
- package/dist/src/contract/ralph.d.ts +16 -0
- package/dist/src/contract/ralph.js +127 -1
- package/dist/src/index.d.ts +1 -0
- package/dist/src/index.js +2 -1
- package/dist/src/utils/address.d.ts +2 -0
- package/dist/src/utils/address.js +18 -6
- package/package.json +4 -3
- package/src/api/api-alephium.ts +6 -0
- package/src/api/types.ts +1 -1
- package/src/codec/array-codec.ts +63 -0
- package/src/codec/asset-output-codec.ts +149 -0
- package/src/codec/bigint-codec.ts +92 -0
- package/src/codec/bytestring-codec.ts +56 -0
- package/src/codec/codec.ts +31 -0
- package/src/codec/compact-int-codec.ts +316 -0
- package/src/codec/contract-codec.ts +95 -0
- package/src/codec/contract-output-codec.ts +95 -0
- package/src/codec/contract-output-ref-codec.ts +42 -0
- package/src/codec/either-codec.ts +74 -0
- package/src/codec/hash.ts +35 -0
- package/src/codec/index.ts +41 -0
- package/src/codec/input-codec.ts +81 -0
- package/src/codec/instr-codec.ts +479 -0
- package/src/codec/lockup-script-codec.ts +99 -0
- package/src/codec/long-codec.ts +59 -0
- package/src/codec/method-codec.ts +97 -0
- package/src/codec/option-codec.ts +60 -0
- package/src/codec/output-codec.ts +26 -0
- package/src/codec/script-codec.ts +45 -0
- package/src/codec/signature-codec.ts +40 -0
- package/src/codec/signed-int-codec.ts +37 -0
- package/src/codec/token-codec.ts +51 -0
- package/src/codec/transaction-codec.ts +147 -0
- package/src/codec/unlock-script-codec.ts +194 -0
- package/src/codec/unsigned-tx-codec.ts +124 -0
- package/src/contract/contract.ts +271 -14
- package/src/contract/ralph.ts +140 -2
- package/src/index.ts +1 -1
- package/src/utils/address.ts +17 -5
|
@@ -0,0 +1,300 @@
|
|
|
1
|
+
"use strict";
|
|
2
|
+
Object.defineProperty(exports, "__esModule", { value: true });
|
|
3
|
+
exports.compactSignedIntCodec = exports.CompactSignedIntCodec = exports.compactUnsignedIntCodec = exports.CompactUnsignedIntCodec = exports.CompactInt = void 0;
|
|
4
|
+
/*
|
|
5
|
+
Copyright 2018 - 2022 The Alephium Authors
|
|
6
|
+
This file is part of the alephium project.
|
|
7
|
+
|
|
8
|
+
The library is free software: you can redistribute it and/or modify
|
|
9
|
+
it under the terms of the GNU Lesser General Public License as published by
|
|
10
|
+
the Free Software Foundation, either version 3 of the License, or
|
|
11
|
+
(at your option) any later version.
|
|
12
|
+
|
|
13
|
+
The library is distributed in the hope that it will be useful,
|
|
14
|
+
but WITHOUT ANY WARRANTY; without even the implied warranty of
|
|
15
|
+
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
|
16
|
+
GNU Lesser General Public License for more details.
|
|
17
|
+
|
|
18
|
+
You should have received a copy of the GNU Lesser General Public License
|
|
19
|
+
along with the library. If not, see <http://www.gnu.org/licenses/>.
|
|
20
|
+
*/
|
|
21
|
+
const buffer_1 = require("buffer/");
|
|
22
|
+
const binary_parser_1 = require("binary-parser");
|
|
23
|
+
const codec_1 = require("./codec");
|
|
24
|
+
const bigint_codec_1 = require("./bigint-codec");
|
|
25
|
+
class CompactInt {
|
|
26
|
+
}
|
|
27
|
+
exports.CompactInt = CompactInt;
|
|
28
|
+
CompactInt.oneBytePrefix = 0x00;
|
|
29
|
+
CompactInt.oneByteNegPrefix = 0xc0;
|
|
30
|
+
CompactInt.twoBytePrefix = 0x40;
|
|
31
|
+
CompactInt.twoByteNegPrefix = 0x80;
|
|
32
|
+
CompactInt.fourBytePrefix = 0x80;
|
|
33
|
+
CompactInt.fourByteNegPrefix = 0x40;
|
|
34
|
+
CompactInt.multiBytePrefix = 0xc0;
|
|
35
|
+
const maskRest = 0xc0;
|
|
36
|
+
const maskMode = 0x3f;
|
|
37
|
+
const maskModeNeg = 0xffffffc0;
|
|
38
|
+
const signFlag = 0x20; // 0b00100000
|
|
39
|
+
const compactIntParser = new binary_parser_1.Parser().uint8('mode').buffer('rest', {
|
|
40
|
+
length: function (ctx) {
|
|
41
|
+
const rawMode = this['mode'];
|
|
42
|
+
const mode = rawMode & maskRest;
|
|
43
|
+
switch (mode) {
|
|
44
|
+
case CompactInt.oneBytePrefix:
|
|
45
|
+
return 0;
|
|
46
|
+
case CompactInt.twoBytePrefix:
|
|
47
|
+
return 1;
|
|
48
|
+
case CompactInt.fourBytePrefix:
|
|
49
|
+
return 3;
|
|
50
|
+
default:
|
|
51
|
+
return (rawMode & maskMode) + 4;
|
|
52
|
+
}
|
|
53
|
+
}
|
|
54
|
+
});
|
|
55
|
+
class CompactUnsignedIntCodec {
|
|
56
|
+
constructor() {
|
|
57
|
+
this.oneByteBound = 0x40;
|
|
58
|
+
this.twoByteBound = this.oneByteBound << 8;
|
|
59
|
+
this.fourByteBound = this.oneByteBound << (8 * 3);
|
|
60
|
+
this.parser = compactIntParser;
|
|
61
|
+
}
|
|
62
|
+
encode(input) {
|
|
63
|
+
return buffer_1.Buffer.from([input.mode, ...input.rest]);
|
|
64
|
+
}
|
|
65
|
+
encodeU32(value) {
|
|
66
|
+
if (value < this.oneByteBound) {
|
|
67
|
+
return buffer_1.Buffer.from([(CompactInt.oneBytePrefix + value) & 0xff]);
|
|
68
|
+
}
|
|
69
|
+
else if (value < this.twoByteBound) {
|
|
70
|
+
return buffer_1.Buffer.from([(CompactInt.twoBytePrefix + (value >> 8)) & 0xff, value & 0xff]);
|
|
71
|
+
}
|
|
72
|
+
else if (value < this.fourByteBound) {
|
|
73
|
+
return buffer_1.Buffer.from([
|
|
74
|
+
(CompactInt.fourBytePrefix + (value >> 24)) & 0xff,
|
|
75
|
+
(value >> 16) & 0xff,
|
|
76
|
+
(value >> 8) & 0xff,
|
|
77
|
+
value & 0xff
|
|
78
|
+
]);
|
|
79
|
+
}
|
|
80
|
+
else {
|
|
81
|
+
return buffer_1.Buffer.from([
|
|
82
|
+
CompactInt.multiBytePrefix,
|
|
83
|
+
(value >> 24) & 0xff,
|
|
84
|
+
(value >> 16) & 0xff,
|
|
85
|
+
(value >> 8) & 0xff,
|
|
86
|
+
value & 0xff
|
|
87
|
+
]);
|
|
88
|
+
}
|
|
89
|
+
}
|
|
90
|
+
encodeU256(value) {
|
|
91
|
+
(0, codec_1.assert)(value >= 0n, 'Value should be positive');
|
|
92
|
+
if (value < this.fourByteBound) {
|
|
93
|
+
return this.encodeU32(Number(value));
|
|
94
|
+
}
|
|
95
|
+
else {
|
|
96
|
+
let bytes = bigint_codec_1.BigIntCodec.encode(value);
|
|
97
|
+
if (bytes[0] === 0) {
|
|
98
|
+
bytes = bytes.slice(1);
|
|
99
|
+
}
|
|
100
|
+
(0, codec_1.assert)(bytes.length <= 32, 'Expect <= 32 bytes for U256');
|
|
101
|
+
const header = (bytes.length - 4 + CompactInt.multiBytePrefix) & 0xff;
|
|
102
|
+
return buffer_1.Buffer.concat([buffer_1.Buffer.from([header]), bytes]);
|
|
103
|
+
}
|
|
104
|
+
}
|
|
105
|
+
decodeU32(input) {
|
|
106
|
+
const decoded = this.decode(input);
|
|
107
|
+
return this.toU32(decoded);
|
|
108
|
+
}
|
|
109
|
+
decodeU256(input) {
|
|
110
|
+
const decoded = this.decode(input);
|
|
111
|
+
return this.toU256(decoded);
|
|
112
|
+
}
|
|
113
|
+
decode(input) {
|
|
114
|
+
return this.parser.parse(input);
|
|
115
|
+
}
|
|
116
|
+
toU32(value) {
|
|
117
|
+
const body = buffer_1.Buffer.from([value.mode, ...value.rest]);
|
|
118
|
+
return decodePositiveInt(value.mode, body);
|
|
119
|
+
}
|
|
120
|
+
fromU32(value) {
|
|
121
|
+
return this.decode(this.encodeU32(value));
|
|
122
|
+
}
|
|
123
|
+
toU256(value) {
|
|
124
|
+
const mode = value.mode & maskRest;
|
|
125
|
+
if (fixedSize(mode)) {
|
|
126
|
+
return BigInt(this.toU32(value));
|
|
127
|
+
}
|
|
128
|
+
else {
|
|
129
|
+
(0, codec_1.assert)(value.rest.length <= 32, 'Expect <= 32 bytes for U256');
|
|
130
|
+
return bigint_codec_1.BigIntCodec.decode(buffer_1.Buffer.from(value.rest), false);
|
|
131
|
+
}
|
|
132
|
+
}
|
|
133
|
+
fromU256(value) {
|
|
134
|
+
return this.decode(this.encodeU256(value));
|
|
135
|
+
}
|
|
136
|
+
}
|
|
137
|
+
exports.CompactUnsignedIntCodec = CompactUnsignedIntCodec;
|
|
138
|
+
exports.compactUnsignedIntCodec = new CompactUnsignedIntCodec();
|
|
139
|
+
class CompactSignedIntCodec {
|
|
140
|
+
constructor() {
|
|
141
|
+
this.signFlag = 0x20; // 0b00100000
|
|
142
|
+
this.oneByteBound = 0x20; // 0b00100000
|
|
143
|
+
this.twoByteBound = this.oneByteBound << 8;
|
|
144
|
+
this.fourByteBound = this.oneByteBound << (8 * 3);
|
|
145
|
+
this.parser = compactIntParser;
|
|
146
|
+
}
|
|
147
|
+
encode(input) {
|
|
148
|
+
return buffer_1.Buffer.from([input.mode, ...input.rest]);
|
|
149
|
+
}
|
|
150
|
+
decode(input) {
|
|
151
|
+
return this.parser.parse(input);
|
|
152
|
+
}
|
|
153
|
+
decodeI32(input) {
|
|
154
|
+
const decoded = this.decode(input);
|
|
155
|
+
return this.toI32(decoded);
|
|
156
|
+
}
|
|
157
|
+
encodeI32(value) {
|
|
158
|
+
if (value >= 0) {
|
|
159
|
+
if (value < this.oneByteBound) {
|
|
160
|
+
return buffer_1.Buffer.from([(CompactInt.oneBytePrefix + value) & 0xff]);
|
|
161
|
+
}
|
|
162
|
+
else if (value < this.twoByteBound) {
|
|
163
|
+
return buffer_1.Buffer.from([(CompactInt.twoBytePrefix + (value >> 8)) & 0xff, value & 0xff]);
|
|
164
|
+
}
|
|
165
|
+
else if (value < this.fourByteBound) {
|
|
166
|
+
return buffer_1.Buffer.from([
|
|
167
|
+
(CompactInt.fourBytePrefix + (value >> 24)) & 0xff,
|
|
168
|
+
(value >> 16) & 0xff,
|
|
169
|
+
(value >> 8) & 0xff,
|
|
170
|
+
value & 0xff
|
|
171
|
+
]);
|
|
172
|
+
}
|
|
173
|
+
else {
|
|
174
|
+
return buffer_1.Buffer.from([
|
|
175
|
+
CompactInt.multiBytePrefix,
|
|
176
|
+
(value >> 24) & 0xff,
|
|
177
|
+
(value >> 16) & 0xff,
|
|
178
|
+
(value >> 8) & 0xff,
|
|
179
|
+
value & 0xff
|
|
180
|
+
]);
|
|
181
|
+
}
|
|
182
|
+
}
|
|
183
|
+
else {
|
|
184
|
+
if (value >= -this.oneByteBound) {
|
|
185
|
+
return buffer_1.Buffer.from([(value ^ CompactInt.oneByteNegPrefix) & 0xff]);
|
|
186
|
+
}
|
|
187
|
+
else if (value >= -this.twoByteBound) {
|
|
188
|
+
return buffer_1.Buffer.from([((value >> 8) ^ CompactInt.twoByteNegPrefix) & 0xff, value & 0xff]);
|
|
189
|
+
}
|
|
190
|
+
else if (value >= -this.fourByteBound) {
|
|
191
|
+
return buffer_1.Buffer.from([
|
|
192
|
+
((value >> 24) ^ CompactInt.fourByteNegPrefix) & 0xff,
|
|
193
|
+
(value >> 16) & 0xff,
|
|
194
|
+
(value >> 8) & 0xff,
|
|
195
|
+
value & 0xff
|
|
196
|
+
]);
|
|
197
|
+
}
|
|
198
|
+
else {
|
|
199
|
+
return buffer_1.Buffer.from([
|
|
200
|
+
CompactInt.multiBytePrefix,
|
|
201
|
+
(value >> 24) & 0xff,
|
|
202
|
+
(value >> 16) & 0xff,
|
|
203
|
+
(value >> 8) & 0xff,
|
|
204
|
+
value & 0xff
|
|
205
|
+
]);
|
|
206
|
+
}
|
|
207
|
+
}
|
|
208
|
+
}
|
|
209
|
+
encodeI256(value) {
|
|
210
|
+
if (value >= -0x20000000 && value < 0x20000000) {
|
|
211
|
+
return this.encodeI32(Number(value));
|
|
212
|
+
}
|
|
213
|
+
else {
|
|
214
|
+
const bytes = bigint_codec_1.BigIntCodec.encode(value);
|
|
215
|
+
const header = (bytes.length - 4 + CompactInt.multiBytePrefix) & 0xff;
|
|
216
|
+
return buffer_1.Buffer.concat([buffer_1.Buffer.from([header]), bytes]);
|
|
217
|
+
}
|
|
218
|
+
}
|
|
219
|
+
decodeI256(input) {
|
|
220
|
+
const decoded = this.decode(input);
|
|
221
|
+
return this.toI256(decoded);
|
|
222
|
+
}
|
|
223
|
+
toI32(value) {
|
|
224
|
+
const body = buffer_1.Buffer.from([value.mode, ...value.rest]);
|
|
225
|
+
const mode = value.mode & maskRest;
|
|
226
|
+
if (fixedSize(mode)) {
|
|
227
|
+
const isPositive = (value.mode & signFlag) == 0;
|
|
228
|
+
if (isPositive) {
|
|
229
|
+
return decodePositiveInt(value.mode, body);
|
|
230
|
+
}
|
|
231
|
+
else {
|
|
232
|
+
return decodeNegativeInt(value.mode, body);
|
|
233
|
+
}
|
|
234
|
+
}
|
|
235
|
+
else {
|
|
236
|
+
if (body.length === 5) {
|
|
237
|
+
return ((body[1] & 0xff) << 24) | ((body[2] & 0xff) << 16) | ((body[3] & 0xff) << 8) | (body[4] & 0xff);
|
|
238
|
+
}
|
|
239
|
+
else {
|
|
240
|
+
throw new Error(`Expect 4 bytes int, but get ${body.length - 1} bytes int`);
|
|
241
|
+
}
|
|
242
|
+
}
|
|
243
|
+
}
|
|
244
|
+
fromI32(value) {
|
|
245
|
+
return this.decode(this.encodeI32(value));
|
|
246
|
+
}
|
|
247
|
+
toI256(value) {
|
|
248
|
+
const mode = value.mode & maskRest;
|
|
249
|
+
if (fixedSize(mode)) {
|
|
250
|
+
return BigInt(this.toI32(value));
|
|
251
|
+
}
|
|
252
|
+
else {
|
|
253
|
+
(0, codec_1.assert)(value.rest.length <= 32, 'Expect <= 32 bytes for I256');
|
|
254
|
+
return bigint_codec_1.BigIntCodec.decode(buffer_1.Buffer.from(value.rest), true);
|
|
255
|
+
}
|
|
256
|
+
}
|
|
257
|
+
fromI256(value) {
|
|
258
|
+
return this.decode(this.encodeI256(value));
|
|
259
|
+
}
|
|
260
|
+
}
|
|
261
|
+
exports.CompactSignedIntCodec = CompactSignedIntCodec;
|
|
262
|
+
exports.compactSignedIntCodec = new CompactSignedIntCodec();
|
|
263
|
+
function decodePositiveInt(rawMode, body) {
|
|
264
|
+
const mode = rawMode & maskRest;
|
|
265
|
+
switch (mode) {
|
|
266
|
+
case CompactInt.oneBytePrefix:
|
|
267
|
+
return rawMode;
|
|
268
|
+
case CompactInt.twoBytePrefix:
|
|
269
|
+
(0, codec_1.assert)(body.length === 2, 'Length should be 2');
|
|
270
|
+
return ((body[0] & maskMode) << 8) | (body[1] & 0xff);
|
|
271
|
+
case CompactInt.fourBytePrefix:
|
|
272
|
+
(0, codec_1.assert)(body.length === 4, 'Length should be 4');
|
|
273
|
+
return ((body[0] & maskMode) << 24) | ((body[1] & 0xff) << 16) | ((body[2] & 0xff) << 8) | (body[3] & 0xff);
|
|
274
|
+
default:
|
|
275
|
+
if (body.length === 5) {
|
|
276
|
+
return Number(BigInt('0x' + body.slice(1).toString('hex')));
|
|
277
|
+
}
|
|
278
|
+
else {
|
|
279
|
+
throw new Error(`decodePositiveInt: Expect 4 bytes int, but get ${body.length - 1} bytes int`);
|
|
280
|
+
}
|
|
281
|
+
}
|
|
282
|
+
}
|
|
283
|
+
function decodeNegativeInt(rawMode, body) {
|
|
284
|
+
const mode = rawMode & maskRest;
|
|
285
|
+
switch (mode) {
|
|
286
|
+
case CompactInt.oneBytePrefix:
|
|
287
|
+
return rawMode | maskModeNeg;
|
|
288
|
+
case CompactInt.twoBytePrefix:
|
|
289
|
+
(0, codec_1.assert)(body.length === 2, 'Length should be 2');
|
|
290
|
+
return ((body[0] | maskModeNeg) << 8) | (body[1] & 0xff);
|
|
291
|
+
case CompactInt.fourBytePrefix:
|
|
292
|
+
(0, codec_1.assert)(body.length === 4, 'Length should be 4');
|
|
293
|
+
return ((body[0] | maskModeNeg) << 24) | ((body[1] & 0xff) << 16) | ((body[2] & 0xff) << 8) | (body[3] & 0xff);
|
|
294
|
+
default:
|
|
295
|
+
throw new Error(`decodeNegativeInt: Expect 4 bytes int, but get ${body.length - 1} bytes int`);
|
|
296
|
+
}
|
|
297
|
+
}
|
|
298
|
+
function fixedSize(mode) {
|
|
299
|
+
return mode === CompactInt.oneBytePrefix || mode === CompactInt.twoBytePrefix || mode === CompactInt.fourBytePrefix;
|
|
300
|
+
}
|
|
@@ -0,0 +1,23 @@
|
|
|
1
|
+
import { Buffer } from 'buffer/';
|
|
2
|
+
import { Parser } from 'binary-parser';
|
|
3
|
+
import { DecodedArray } from './array-codec';
|
|
4
|
+
import { Codec } from './codec';
|
|
5
|
+
import { DecodedCompactInt } from './compact-int-codec';
|
|
6
|
+
import { Method } from './method-codec';
|
|
7
|
+
export interface HalfDecodedContract {
|
|
8
|
+
fieldLength: DecodedCompactInt;
|
|
9
|
+
methodIndexes: DecodedArray<DecodedCompactInt>;
|
|
10
|
+
methods: Buffer;
|
|
11
|
+
}
|
|
12
|
+
export interface Contract {
|
|
13
|
+
fieldLength: number;
|
|
14
|
+
methods: Method[];
|
|
15
|
+
}
|
|
16
|
+
export declare function toHalfDecoded(contract: Contract): HalfDecodedContract;
|
|
17
|
+
export declare class ContractCodec implements Codec<HalfDecodedContract> {
|
|
18
|
+
parser: Parser;
|
|
19
|
+
encode(input: HalfDecodedContract): Buffer;
|
|
20
|
+
decode(input: Buffer): HalfDecodedContract;
|
|
21
|
+
decodeContract(input: Buffer): Contract;
|
|
22
|
+
}
|
|
23
|
+
export declare const contractCodec: ContractCodec;
|
|
@@ -0,0 +1,81 @@
|
|
|
1
|
+
"use strict";
|
|
2
|
+
/*
|
|
3
|
+
Copyright 2018 - 2022 The Alephium Authors
|
|
4
|
+
This file is part of the alephium project.
|
|
5
|
+
|
|
6
|
+
The library is free software: you can redistribute it and/or modify
|
|
7
|
+
it under the terms of the GNU Lesser General Public License as published by
|
|
8
|
+
the Free Software Foundation, either version 3 of the License, or
|
|
9
|
+
(at your option) any later version.
|
|
10
|
+
|
|
11
|
+
The library is distributed in the hope that it will be useful,
|
|
12
|
+
but WITHOUT ANY WARRANTY; without even the implied warranty of
|
|
13
|
+
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
|
14
|
+
GNU Lesser General Public License for more details.
|
|
15
|
+
|
|
16
|
+
You should have received a copy of the GNU Lesser General Public License
|
|
17
|
+
along with the library. If not, see <http://www.gnu.org/licenses/>.
|
|
18
|
+
*/
|
|
19
|
+
Object.defineProperty(exports, "__esModule", { value: true });
|
|
20
|
+
exports.contractCodec = exports.ContractCodec = exports.toHalfDecoded = void 0;
|
|
21
|
+
const buffer_1 = require("buffer/");
|
|
22
|
+
const binary_parser_1 = require("binary-parser");
|
|
23
|
+
const array_codec_1 = require("./array-codec");
|
|
24
|
+
const compact_int_codec_1 = require("./compact-int-codec");
|
|
25
|
+
const method_codec_1 = require("./method-codec");
|
|
26
|
+
const compactSignedIntsCodec = new array_codec_1.ArrayCodec(compact_int_codec_1.compactSignedIntCodec);
|
|
27
|
+
function toHalfDecoded(contract) {
|
|
28
|
+
const fieldLength = compact_int_codec_1.compactSignedIntCodec.fromI32(contract.fieldLength);
|
|
29
|
+
const methods = contract.methods.map((m) => method_codec_1.methodCodec.encode(method_codec_1.MethodCodec.fromMethod(m)));
|
|
30
|
+
let count = 0;
|
|
31
|
+
const methodIndexes = Array.from(Array(methods.length).keys()).map((index) => {
|
|
32
|
+
count += methods[`${index}`].length;
|
|
33
|
+
return count;
|
|
34
|
+
});
|
|
35
|
+
return {
|
|
36
|
+
fieldLength,
|
|
37
|
+
methodIndexes: {
|
|
38
|
+
length: compact_int_codec_1.compactSignedIntCodec.fromI32(methodIndexes.length),
|
|
39
|
+
value: methodIndexes.map((value) => compact_int_codec_1.compactSignedIntCodec.fromI32(value))
|
|
40
|
+
},
|
|
41
|
+
methods: methods.reduce((acc, buffer) => buffer_1.Buffer.concat([acc, buffer]))
|
|
42
|
+
};
|
|
43
|
+
}
|
|
44
|
+
exports.toHalfDecoded = toHalfDecoded;
|
|
45
|
+
class ContractCodec {
|
|
46
|
+
constructor() {
|
|
47
|
+
this.parser = binary_parser_1.Parser.start()
|
|
48
|
+
.nest('fieldLength', {
|
|
49
|
+
type: compact_int_codec_1.compactSignedIntCodec.parser
|
|
50
|
+
})
|
|
51
|
+
.nest('methodIndexes', {
|
|
52
|
+
type: compactSignedIntsCodec.parser
|
|
53
|
+
})
|
|
54
|
+
.buffer('methods', { readUntil: 'eof' });
|
|
55
|
+
}
|
|
56
|
+
encode(input) {
|
|
57
|
+
return buffer_1.Buffer.from([
|
|
58
|
+
...compact_int_codec_1.compactSignedIntCodec.encode(input.fieldLength),
|
|
59
|
+
...compactSignedIntsCodec.encode(input.methodIndexes.value),
|
|
60
|
+
...input.methods
|
|
61
|
+
]);
|
|
62
|
+
}
|
|
63
|
+
decode(input) {
|
|
64
|
+
return this.parser.parse(input);
|
|
65
|
+
}
|
|
66
|
+
decodeContract(input) {
|
|
67
|
+
const halfDecoded = this.decode(input);
|
|
68
|
+
const fieldLength = compact_int_codec_1.compactUnsignedIntCodec.toU32(halfDecoded.fieldLength);
|
|
69
|
+
const methodIndexes = halfDecoded.methodIndexes.value.map((v) => compact_int_codec_1.compactUnsignedIntCodec.toU32(v));
|
|
70
|
+
const methods = [];
|
|
71
|
+
for (let i = 0, start = 0; i < methodIndexes.length; i++) {
|
|
72
|
+
const end = methodIndexes[i];
|
|
73
|
+
const method = method_codec_1.MethodCodec.toMethod(method_codec_1.methodCodec.decode(halfDecoded.methods.slice(start, end)));
|
|
74
|
+
methods.push(method);
|
|
75
|
+
start = end;
|
|
76
|
+
}
|
|
77
|
+
return { fieldLength, methods };
|
|
78
|
+
}
|
|
79
|
+
}
|
|
80
|
+
exports.ContractCodec = ContractCodec;
|
|
81
|
+
exports.contractCodec = new ContractCodec();
|
|
@@ -0,0 +1,21 @@
|
|
|
1
|
+
import { Buffer } from 'buffer/';
|
|
2
|
+
import { Parser } from 'binary-parser';
|
|
3
|
+
import { DecodedArray } from './array-codec';
|
|
4
|
+
import { DecodedCompactInt } from './compact-int-codec';
|
|
5
|
+
import { P2C } from './lockup-script-codec';
|
|
6
|
+
import { Codec } from './codec';
|
|
7
|
+
import { Token } from './token-codec';
|
|
8
|
+
import { ContractOutput as ApiContractOutput } from '../api/api-alephium';
|
|
9
|
+
export interface ContractOutput {
|
|
10
|
+
amount: DecodedCompactInt;
|
|
11
|
+
lockupScript: P2C;
|
|
12
|
+
tokens: DecodedArray<Token>;
|
|
13
|
+
}
|
|
14
|
+
export declare class ContractOutputCodec implements Codec<ContractOutput> {
|
|
15
|
+
parser: Parser;
|
|
16
|
+
encode(input: ContractOutput): Buffer;
|
|
17
|
+
decode(input: Buffer): ContractOutput;
|
|
18
|
+
static convertToApiContractOutput(txIdBytes: Uint8Array, output: ContractOutput, index: number): ApiContractOutput;
|
|
19
|
+
static convertToOutput(apiContractOutput: ApiContractOutput): ContractOutput;
|
|
20
|
+
}
|
|
21
|
+
export declare const contractOutputCodec: ContractOutputCodec;
|
|
@@ -0,0 +1,82 @@
|
|
|
1
|
+
"use strict";
|
|
2
|
+
Object.defineProperty(exports, "__esModule", { value: true });
|
|
3
|
+
exports.contractOutputCodec = exports.ContractOutputCodec = void 0;
|
|
4
|
+
/*
|
|
5
|
+
Copyright 2018 - 2022 The Alephium Authors
|
|
6
|
+
This file is part of the alephium project.
|
|
7
|
+
|
|
8
|
+
The library is free software: you can redistribute it and/or modify
|
|
9
|
+
it under the terms of the GNU Lesser General Public License as published by
|
|
10
|
+
the Free Software Foundation, either version 3 of the License, or
|
|
11
|
+
(at your option) any later version.
|
|
12
|
+
|
|
13
|
+
The library is distributed in the hope that it will be useful,
|
|
14
|
+
but WITHOUT ANY WARRANTY; without even the implied warranty of
|
|
15
|
+
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
|
16
|
+
GNU Lesser General Public License for more details.
|
|
17
|
+
|
|
18
|
+
You should have received a copy of the GNU Lesser General Public License
|
|
19
|
+
along with the library. If not, see <http://www.gnu.org/licenses/>.
|
|
20
|
+
*/
|
|
21
|
+
const buffer_1 = require("buffer/");
|
|
22
|
+
const binary_parser_1 = require("binary-parser");
|
|
23
|
+
const compact_int_codec_1 = require("./compact-int-codec");
|
|
24
|
+
const token_codec_1 = require("./token-codec");
|
|
25
|
+
const hash_1 = require("./hash");
|
|
26
|
+
const __1 = require("..");
|
|
27
|
+
const signed_int_codec_1 = require("./signed-int-codec");
|
|
28
|
+
const lockup_script_codec_1 = require("./lockup-script-codec");
|
|
29
|
+
class ContractOutputCodec {
|
|
30
|
+
constructor() {
|
|
31
|
+
this.parser = binary_parser_1.Parser.start()
|
|
32
|
+
.nest('amount', {
|
|
33
|
+
type: compact_int_codec_1.compactUnsignedIntCodec.parser
|
|
34
|
+
})
|
|
35
|
+
.nest('lockupScript', {
|
|
36
|
+
type: binary_parser_1.Parser.start().buffer('contractId', { length: 32 })
|
|
37
|
+
})
|
|
38
|
+
.nest('tokens', {
|
|
39
|
+
type: token_codec_1.tokensCodec.parser
|
|
40
|
+
});
|
|
41
|
+
}
|
|
42
|
+
encode(input) {
|
|
43
|
+
const amount = buffer_1.Buffer.from(compact_int_codec_1.compactUnsignedIntCodec.encode(input.amount));
|
|
44
|
+
const lockupScript = input.lockupScript.contractId;
|
|
45
|
+
const tokens = buffer_1.Buffer.from(token_codec_1.tokensCodec.encode(input.tokens.value));
|
|
46
|
+
return buffer_1.Buffer.concat([amount, lockupScript, tokens]);
|
|
47
|
+
}
|
|
48
|
+
decode(input) {
|
|
49
|
+
return this.parser.parse(input);
|
|
50
|
+
}
|
|
51
|
+
static convertToApiContractOutput(txIdBytes, output, index) {
|
|
52
|
+
const hint = (0, hash_1.createHint)(output.lockupScript.contractId);
|
|
53
|
+
const key = (0, __1.binToHex)((0, hash_1.blakeHash)(buffer_1.Buffer.concat([txIdBytes, signed_int_codec_1.signedIntCodec.encode(index)])));
|
|
54
|
+
const attoAlphAmount = compact_int_codec_1.compactUnsignedIntCodec.toU256(output.amount).toString();
|
|
55
|
+
const address = __1.bs58.encode(buffer_1.Buffer.concat([buffer_1.Buffer.from([0x03]), output.lockupScript.contractId]));
|
|
56
|
+
const tokens = output.tokens.value.map((token) => {
|
|
57
|
+
return {
|
|
58
|
+
id: token.tokenId.toString('hex'),
|
|
59
|
+
amount: compact_int_codec_1.compactUnsignedIntCodec.toU256(token.amount).toString()
|
|
60
|
+
};
|
|
61
|
+
});
|
|
62
|
+
return { hint, key, attoAlphAmount, address, tokens, type: 'ContractOutput' };
|
|
63
|
+
}
|
|
64
|
+
static convertToOutput(apiContractOutput) {
|
|
65
|
+
const amount = compact_int_codec_1.compactUnsignedIntCodec.fromU256(BigInt(apiContractOutput.attoAlphAmount));
|
|
66
|
+
const lockupScript = lockup_script_codec_1.lockupScriptCodec.decode(buffer_1.Buffer.from(__1.bs58.decode(apiContractOutput.address)))
|
|
67
|
+
.script;
|
|
68
|
+
const tokensValue = apiContractOutput.tokens.map((token) => {
|
|
69
|
+
return {
|
|
70
|
+
tokenId: buffer_1.Buffer.from(token.id, 'hex'),
|
|
71
|
+
amount: compact_int_codec_1.compactUnsignedIntCodec.fromU256(BigInt(token.amount))
|
|
72
|
+
};
|
|
73
|
+
});
|
|
74
|
+
const tokens = {
|
|
75
|
+
length: compact_int_codec_1.compactUnsignedIntCodec.fromU32(tokensValue.length),
|
|
76
|
+
value: tokensValue
|
|
77
|
+
};
|
|
78
|
+
return { amount, lockupScript, tokens };
|
|
79
|
+
}
|
|
80
|
+
}
|
|
81
|
+
exports.ContractOutputCodec = ContractOutputCodec;
|
|
82
|
+
exports.contractOutputCodec = new ContractOutputCodec();
|
|
@@ -0,0 +1,15 @@
|
|
|
1
|
+
import { Buffer } from 'buffer/';
|
|
2
|
+
import { Parser } from 'binary-parser';
|
|
3
|
+
import { ArrayCodec } from './array-codec';
|
|
4
|
+
import { Codec } from './codec';
|
|
5
|
+
export interface ContractOutputRef {
|
|
6
|
+
hint: number;
|
|
7
|
+
key: Buffer;
|
|
8
|
+
}
|
|
9
|
+
export declare class ContractOutputRefCodec implements Codec<ContractOutputRef> {
|
|
10
|
+
parser: Parser;
|
|
11
|
+
encode(input: ContractOutputRef): Buffer;
|
|
12
|
+
decode(input: Buffer): ContractOutputRef;
|
|
13
|
+
}
|
|
14
|
+
export declare const contractOutputRefCodec: ContractOutputRefCodec;
|
|
15
|
+
export declare const contractOutputRefsCodec: ArrayCodec<ContractOutputRef>;
|
|
@@ -0,0 +1,38 @@
|
|
|
1
|
+
"use strict";
|
|
2
|
+
Object.defineProperty(exports, "__esModule", { value: true });
|
|
3
|
+
exports.contractOutputRefsCodec = exports.contractOutputRefCodec = exports.ContractOutputRefCodec = void 0;
|
|
4
|
+
/*
|
|
5
|
+
Copyright 2018 - 2022 The Alephium Authors
|
|
6
|
+
This file is part of the alephium project.
|
|
7
|
+
|
|
8
|
+
The library is free software: you can redistribute it and/or modify
|
|
9
|
+
it under the terms of the GNU Lesser General Public License as published by
|
|
10
|
+
the Free Software Foundation, either version 3 of the License, or
|
|
11
|
+
(at your option) any later version.
|
|
12
|
+
|
|
13
|
+
The library is distributed in the hope that it will be useful,
|
|
14
|
+
but WITHOUT ANY WARRANTY; without even the implied warranty of
|
|
15
|
+
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
|
16
|
+
GNU Lesser General Public License for more details.
|
|
17
|
+
|
|
18
|
+
You should have received a copy of the GNU Lesser General Public License
|
|
19
|
+
along with the library. If not, see <http://www.gnu.org/licenses/>.
|
|
20
|
+
*/
|
|
21
|
+
const buffer_1 = require("buffer/");
|
|
22
|
+
const binary_parser_1 = require("binary-parser");
|
|
23
|
+
const array_codec_1 = require("./array-codec");
|
|
24
|
+
const signed_int_codec_1 = require("./signed-int-codec");
|
|
25
|
+
class ContractOutputRefCodec {
|
|
26
|
+
constructor() {
|
|
27
|
+
this.parser = binary_parser_1.Parser.start().int32('hint').buffer('key', { length: 32 });
|
|
28
|
+
}
|
|
29
|
+
encode(input) {
|
|
30
|
+
return buffer_1.Buffer.concat([buffer_1.Buffer.from([...signed_int_codec_1.signedIntCodec.encode(input.hint), ...input.key])]);
|
|
31
|
+
}
|
|
32
|
+
decode(input) {
|
|
33
|
+
return this.parser.parse(input);
|
|
34
|
+
}
|
|
35
|
+
}
|
|
36
|
+
exports.ContractOutputRefCodec = ContractOutputRefCodec;
|
|
37
|
+
exports.contractOutputRefCodec = new ContractOutputRefCodec();
|
|
38
|
+
exports.contractOutputRefsCodec = new array_codec_1.ArrayCodec(exports.contractOutputRefCodec);
|
|
@@ -0,0 +1,17 @@
|
|
|
1
|
+
import { Buffer } from 'buffer/';
|
|
2
|
+
import { Parser } from 'binary-parser';
|
|
3
|
+
import { Codec } from './codec';
|
|
4
|
+
export interface Either<L, R> {
|
|
5
|
+
either: number;
|
|
6
|
+
value: L | R;
|
|
7
|
+
}
|
|
8
|
+
export declare class EitherCodec<L, R> implements Codec<Either<L, R>> {
|
|
9
|
+
private leftCodec;
|
|
10
|
+
private rightCodec;
|
|
11
|
+
parser: Parser;
|
|
12
|
+
constructor(leftCodec: Codec<L>, rightCodec: Codec<R>, parser?: Parser);
|
|
13
|
+
encode(input: Either<L, R>): Buffer;
|
|
14
|
+
decode(input: Buffer): Either<L, R>;
|
|
15
|
+
fromLeft(left: L): Either<L, R>;
|
|
16
|
+
fromRight(right: R): Either<L, R>;
|
|
17
|
+
}
|
|
@@ -0,0 +1,67 @@
|
|
|
1
|
+
"use strict";
|
|
2
|
+
Object.defineProperty(exports, "__esModule", { value: true });
|
|
3
|
+
exports.EitherCodec = void 0;
|
|
4
|
+
/*
|
|
5
|
+
Copyright 2018 - 2022 The Alephium Authors
|
|
6
|
+
This file is part of the alephium project.
|
|
7
|
+
|
|
8
|
+
The library is free software: you can redistribute it and/or modify
|
|
9
|
+
it under the terms of the GNU Lesser General Public License as published by
|
|
10
|
+
the Free Software Foundation, either version 3 of the License, or
|
|
11
|
+
(at your option) any later version.
|
|
12
|
+
|
|
13
|
+
The library is distributed in the hope that it will be useful,
|
|
14
|
+
but WITHOUT ANY WARRANTY; without even the implied warranty of
|
|
15
|
+
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
|
16
|
+
GNU Lesser General Public License for more details.
|
|
17
|
+
|
|
18
|
+
You should have received a copy of the GNU Lesser General Public License
|
|
19
|
+
along with the library. If not, see <http://www.gnu.org/licenses/>.
|
|
20
|
+
*/
|
|
21
|
+
const buffer_1 = require("buffer/");
|
|
22
|
+
const binary_parser_1 = require("binary-parser");
|
|
23
|
+
class EitherCodec {
|
|
24
|
+
constructor(leftCodec, rightCodec, parser = binary_parser_1.Parser.start()
|
|
25
|
+
.uint8('either')
|
|
26
|
+
.choice('value', {
|
|
27
|
+
tag: 'either',
|
|
28
|
+
choices: {
|
|
29
|
+
0: leftCodec.parser,
|
|
30
|
+
1: rightCodec.parser
|
|
31
|
+
}
|
|
32
|
+
})) {
|
|
33
|
+
this.leftCodec = leftCodec;
|
|
34
|
+
this.rightCodec = rightCodec;
|
|
35
|
+
this.parser = parser;
|
|
36
|
+
}
|
|
37
|
+
encode(input) {
|
|
38
|
+
const result = [input.either];
|
|
39
|
+
if (input.either === 0) {
|
|
40
|
+
result.push(...this.leftCodec.encode(input.value));
|
|
41
|
+
}
|
|
42
|
+
else {
|
|
43
|
+
result.push(...this.rightCodec.encode(input.value));
|
|
44
|
+
}
|
|
45
|
+
return buffer_1.Buffer.from(result);
|
|
46
|
+
}
|
|
47
|
+
decode(input) {
|
|
48
|
+
const result = this.parser.parse(input);
|
|
49
|
+
return {
|
|
50
|
+
...result,
|
|
51
|
+
value: result.either === 0 ? this.leftCodec.decode(result.value.value) : this.rightCodec.decode(result.value.value)
|
|
52
|
+
};
|
|
53
|
+
}
|
|
54
|
+
fromLeft(left) {
|
|
55
|
+
return {
|
|
56
|
+
either: 0,
|
|
57
|
+
value: left
|
|
58
|
+
};
|
|
59
|
+
}
|
|
60
|
+
fromRight(right) {
|
|
61
|
+
return {
|
|
62
|
+
either: 1,
|
|
63
|
+
value: right
|
|
64
|
+
};
|
|
65
|
+
}
|
|
66
|
+
}
|
|
67
|
+
exports.EitherCodec = EitherCodec;
|
|
@@ -0,0 +1,23 @@
|
|
|
1
|
+
"use strict";
|
|
2
|
+
var __importDefault = (this && this.__importDefault) || function (mod) {
|
|
3
|
+
return (mod && mod.__esModule) ? mod : { "default": mod };
|
|
4
|
+
};
|
|
5
|
+
Object.defineProperty(exports, "__esModule", { value: true });
|
|
6
|
+
exports.createHint = exports.djbIntHash = exports.blakeHash = void 0;
|
|
7
|
+
const blakejs_1 = __importDefault(require("blakejs"));
|
|
8
|
+
function blakeHash(raw) {
|
|
9
|
+
return blakejs_1.default.blake2b(raw, undefined, 32);
|
|
10
|
+
}
|
|
11
|
+
exports.blakeHash = blakeHash;
|
|
12
|
+
function djbIntHash(bytes) {
|
|
13
|
+
let hash = 5381;
|
|
14
|
+
bytes.forEach((byte) => {
|
|
15
|
+
hash = (hash << 5) + hash + (byte & 0xff);
|
|
16
|
+
});
|
|
17
|
+
return hash;
|
|
18
|
+
}
|
|
19
|
+
exports.djbIntHash = djbIntHash;
|
|
20
|
+
function createHint(input) {
|
|
21
|
+
return djbIntHash(input) | 1;
|
|
22
|
+
}
|
|
23
|
+
exports.createHint = createHint;
|