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