@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
|
@@ -983,7 +983,7 @@ export declare class HttpClient<SecurityDataType = unknown> {
|
|
|
983
983
|
}
|
|
984
984
|
/**
|
|
985
985
|
* @title Alephium API
|
|
986
|
-
* @version 2.8.
|
|
986
|
+
* @version 2.8.3
|
|
987
987
|
* @baseUrl ../
|
|
988
988
|
*/
|
|
989
989
|
export declare class Api<SecurityDataType extends unknown> extends HttpClient<SecurityDataType> {
|
|
@@ -0,0 +1,17 @@
|
|
|
1
|
+
/// <reference types="node" />
|
|
2
|
+
import { Parser } from 'binary-parser';
|
|
3
|
+
import { DecodedCompactInt } from './compact-int-codec';
|
|
4
|
+
import { Codec } from './codec';
|
|
5
|
+
export interface DecodedArray<T> {
|
|
6
|
+
length: DecodedCompactInt;
|
|
7
|
+
value: T[];
|
|
8
|
+
}
|
|
9
|
+
export declare class ArrayCodec<T> implements Codec<T[]> {
|
|
10
|
+
private childCodec;
|
|
11
|
+
parser: Parser;
|
|
12
|
+
constructor(childCodec: Codec<T>, parser?: Parser);
|
|
13
|
+
encode(input: T[]): Buffer;
|
|
14
|
+
decode(input: Buffer): T[];
|
|
15
|
+
static arrayParser(parser: Parser): Parser;
|
|
16
|
+
fromArray(inputs: T[]): DecodedArray<T>;
|
|
17
|
+
}
|
|
@@ -0,0 +1,58 @@
|
|
|
1
|
+
"use strict";
|
|
2
|
+
Object.defineProperty(exports, "__esModule", { value: true });
|
|
3
|
+
exports.ArrayCodec = 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
|
+
class ArrayCodec {
|
|
24
|
+
constructor(childCodec, parser = ArrayCodec.arrayParser(childCodec.parser)) {
|
|
25
|
+
this.childCodec = childCodec;
|
|
26
|
+
this.parser = parser;
|
|
27
|
+
}
|
|
28
|
+
encode(input) {
|
|
29
|
+
const result = [...compact_int_codec_1.compactSignedIntCodec.encodeI256(BigInt(input.length))];
|
|
30
|
+
for (const element of input) {
|
|
31
|
+
result.push(...this.childCodec.encode(element));
|
|
32
|
+
}
|
|
33
|
+
return Buffer.from(result);
|
|
34
|
+
}
|
|
35
|
+
decode(input) {
|
|
36
|
+
const result = this.parser.parse(input);
|
|
37
|
+
return result.value.map((v) => this.childCodec.decode(v.value));
|
|
38
|
+
}
|
|
39
|
+
static arrayParser(parser) {
|
|
40
|
+
return new binary_parser_1.Parser()
|
|
41
|
+
.nest('length', {
|
|
42
|
+
type: compact_int_codec_1.compactSignedIntCodec.parser
|
|
43
|
+
})
|
|
44
|
+
.array('value', {
|
|
45
|
+
length: function (ctx) {
|
|
46
|
+
return compact_int_codec_1.compactSignedIntCodec.toI32(this['length']);
|
|
47
|
+
},
|
|
48
|
+
type: parser
|
|
49
|
+
});
|
|
50
|
+
}
|
|
51
|
+
fromArray(inputs) {
|
|
52
|
+
return {
|
|
53
|
+
length: compact_int_codec_1.compactUnsignedIntCodec.decode(compact_int_codec_1.compactUnsignedIntCodec.encodeU32(inputs.length)),
|
|
54
|
+
value: inputs
|
|
55
|
+
};
|
|
56
|
+
}
|
|
57
|
+
}
|
|
58
|
+
exports.ArrayCodec = ArrayCodec;
|
|
@@ -0,0 +1,27 @@
|
|
|
1
|
+
/// <reference types="node" />
|
|
2
|
+
import { Parser } from 'binary-parser';
|
|
3
|
+
import { ArrayCodec, DecodedArray } from './array-codec';
|
|
4
|
+
import { DecodedCompactInt } from './compact-int-codec';
|
|
5
|
+
import { ByteString } from './bytestring-codec';
|
|
6
|
+
import { LockupScript } from './lockup-script-codec';
|
|
7
|
+
import { FixedAssetOutput } from '../api/api-alephium';
|
|
8
|
+
import { Codec } from './codec';
|
|
9
|
+
import { Token } from './token-codec';
|
|
10
|
+
export interface AssetOutput {
|
|
11
|
+
amount: DecodedCompactInt;
|
|
12
|
+
lockupScript: LockupScript;
|
|
13
|
+
lockTime: Buffer;
|
|
14
|
+
tokens: DecodedArray<Token>;
|
|
15
|
+
additionalData: ByteString;
|
|
16
|
+
}
|
|
17
|
+
export declare class AssetOutputCodec implements Codec<AssetOutput> {
|
|
18
|
+
parser: Parser;
|
|
19
|
+
encode(input: AssetOutput): Buffer;
|
|
20
|
+
decode(input: Buffer): AssetOutput;
|
|
21
|
+
static toFixedAssetOutputs(txIdBytes: Uint8Array, outputs: AssetOutput[]): FixedAssetOutput[];
|
|
22
|
+
static toFixedAssetOutput(txIdBytes: Uint8Array, output: AssetOutput, index: number): FixedAssetOutput;
|
|
23
|
+
static fromFixedAssetOutputs(fixedOutputs: FixedAssetOutput[]): AssetOutput[];
|
|
24
|
+
static fromFixedAssetOutput(fixedOutput: FixedAssetOutput): AssetOutput;
|
|
25
|
+
}
|
|
26
|
+
export declare const assetOutputCodec: AssetOutputCodec;
|
|
27
|
+
export declare const assetOutputsCodec: ArrayCodec<AssetOutput>;
|
|
@@ -0,0 +1,135 @@
|
|
|
1
|
+
"use strict";
|
|
2
|
+
Object.defineProperty(exports, "__esModule", { value: true });
|
|
3
|
+
exports.assetOutputsCodec = exports.assetOutputCodec = exports.AssetOutputCodec = 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 compact_int_codec_1 = require("./compact-int-codec");
|
|
24
|
+
const signed_int_codec_1 = require("./signed-int-codec");
|
|
25
|
+
const long_codec_1 = require("./long-codec");
|
|
26
|
+
const bytestring_codec_1 = require("./bytestring-codec");
|
|
27
|
+
const lockup_script_codec_1 = require("./lockup-script-codec");
|
|
28
|
+
const hash_1 = require("./hash");
|
|
29
|
+
const utils_1 = require("../utils");
|
|
30
|
+
const token_codec_1 = require("./token-codec");
|
|
31
|
+
class AssetOutputCodec {
|
|
32
|
+
constructor() {
|
|
33
|
+
this.parser = binary_parser_1.Parser.start()
|
|
34
|
+
.nest('amount', {
|
|
35
|
+
type: compact_int_codec_1.compactUnsignedIntCodec.parser
|
|
36
|
+
})
|
|
37
|
+
.nest('lockupScript', {
|
|
38
|
+
type: lockup_script_codec_1.lockupScriptCodec.parser
|
|
39
|
+
})
|
|
40
|
+
.buffer('lockTime', {
|
|
41
|
+
length: 8
|
|
42
|
+
})
|
|
43
|
+
.nest('tokens', {
|
|
44
|
+
type: token_codec_1.tokensCodec.parser
|
|
45
|
+
})
|
|
46
|
+
.nest('additionalData', {
|
|
47
|
+
type: bytestring_codec_1.byteStringCodec.parser
|
|
48
|
+
});
|
|
49
|
+
}
|
|
50
|
+
encode(input) {
|
|
51
|
+
const amount = Buffer.from(compact_int_codec_1.compactUnsignedIntCodec.encode(input.amount));
|
|
52
|
+
const lockupScript = lockup_script_codec_1.lockupScriptCodec.encode(input.lockupScript);
|
|
53
|
+
const lockTime = Buffer.from(input.lockTime);
|
|
54
|
+
const tokens = Buffer.from(token_codec_1.tokensCodec.encode(input.tokens.value));
|
|
55
|
+
const additionalData = Buffer.from(bytestring_codec_1.byteStringCodec.encode(input.additionalData));
|
|
56
|
+
return Buffer.concat([amount, lockupScript, lockTime, tokens, additionalData]);
|
|
57
|
+
}
|
|
58
|
+
decode(input) {
|
|
59
|
+
return this.parser.parse(input);
|
|
60
|
+
}
|
|
61
|
+
static toFixedAssetOutputs(txIdBytes, outputs) {
|
|
62
|
+
return outputs.map((output, index) => AssetOutputCodec.toFixedAssetOutput(txIdBytes, output, index));
|
|
63
|
+
}
|
|
64
|
+
static toFixedAssetOutput(txIdBytes, output, index) {
|
|
65
|
+
const attoAlphAmount = compact_int_codec_1.compactUnsignedIntCodec.toU256(output.amount).toString();
|
|
66
|
+
const lockTime = Number(long_codec_1.longCodec.decode(output.lockTime));
|
|
67
|
+
const tokens = output.tokens.value.map((token) => {
|
|
68
|
+
return {
|
|
69
|
+
id: token.tokenId.toString('hex'),
|
|
70
|
+
amount: compact_int_codec_1.compactUnsignedIntCodec.toU256(token.amount).toString()
|
|
71
|
+
};
|
|
72
|
+
});
|
|
73
|
+
const message = output.additionalData.value.toString('hex');
|
|
74
|
+
const scriptType = output.lockupScript.scriptType;
|
|
75
|
+
const key = (0, utils_1.binToHex)((0, hash_1.blakeHash)(Buffer.concat([txIdBytes, signed_int_codec_1.signedIntCodec.encode(index)])));
|
|
76
|
+
const outputLockupScript = output.lockupScript.script;
|
|
77
|
+
const address = utils_1.bs58.encode(lockup_script_codec_1.lockupScriptCodec.encode(output.lockupScript));
|
|
78
|
+
let hint = undefined;
|
|
79
|
+
if (scriptType === 0) {
|
|
80
|
+
// P2PKH
|
|
81
|
+
hint = (0, hash_1.createHint)(outputLockupScript.publicKeyHash);
|
|
82
|
+
}
|
|
83
|
+
else if (scriptType === 1) {
|
|
84
|
+
// P2MPKH
|
|
85
|
+
hint = (0, hash_1.createHint)(outputLockupScript.publicKeyHashes.value[0].publicKeyHash);
|
|
86
|
+
}
|
|
87
|
+
else if (scriptType === 2) {
|
|
88
|
+
// P2SH
|
|
89
|
+
hint = (0, hash_1.createHint)(outputLockupScript.scriptHash);
|
|
90
|
+
}
|
|
91
|
+
else if (scriptType === 3) {
|
|
92
|
+
// P2C
|
|
93
|
+
hint = (0, hash_1.createHint)(outputLockupScript.contractId);
|
|
94
|
+
}
|
|
95
|
+
else {
|
|
96
|
+
throw new Error(`TODO: decode output script type: ${scriptType}`);
|
|
97
|
+
}
|
|
98
|
+
return { hint, key, attoAlphAmount, lockTime, tokens, address, message };
|
|
99
|
+
}
|
|
100
|
+
static fromFixedAssetOutputs(fixedOutputs) {
|
|
101
|
+
return fixedOutputs.map((output) => {
|
|
102
|
+
return AssetOutputCodec.fromFixedAssetOutput(output);
|
|
103
|
+
});
|
|
104
|
+
}
|
|
105
|
+
static fromFixedAssetOutput(fixedOutput) {
|
|
106
|
+
const amount = compact_int_codec_1.compactUnsignedIntCodec.decode(compact_int_codec_1.compactUnsignedIntCodec.encodeU256(BigInt(fixedOutput.attoAlphAmount)));
|
|
107
|
+
const lockTime = long_codec_1.longCodec.encode(BigInt(fixedOutput.lockTime));
|
|
108
|
+
const lockupScript = lockup_script_codec_1.lockupScriptCodec.decode(Buffer.from(utils_1.bs58.decode(fixedOutput.address)));
|
|
109
|
+
const tokensValue = fixedOutput.tokens.map((token) => {
|
|
110
|
+
return {
|
|
111
|
+
tokenId: Buffer.from(token.id, 'hex'),
|
|
112
|
+
amount: compact_int_codec_1.compactUnsignedIntCodec.decode(compact_int_codec_1.compactUnsignedIntCodec.encodeU256(BigInt(token.amount)))
|
|
113
|
+
};
|
|
114
|
+
});
|
|
115
|
+
const tokens = {
|
|
116
|
+
length: compact_int_codec_1.compactUnsignedIntCodec.decode(compact_int_codec_1.compactUnsignedIntCodec.encodeU32(tokensValue.length)),
|
|
117
|
+
value: tokensValue
|
|
118
|
+
};
|
|
119
|
+
const additionalDataValue = Buffer.from(fixedOutput.message, 'hex');
|
|
120
|
+
const additionalData = {
|
|
121
|
+
length: compact_int_codec_1.compactUnsignedIntCodec.decode(compact_int_codec_1.compactUnsignedIntCodec.encodeU32(additionalDataValue.length)),
|
|
122
|
+
value: additionalDataValue
|
|
123
|
+
};
|
|
124
|
+
return {
|
|
125
|
+
amount,
|
|
126
|
+
lockupScript,
|
|
127
|
+
lockTime,
|
|
128
|
+
tokens,
|
|
129
|
+
additionalData
|
|
130
|
+
};
|
|
131
|
+
}
|
|
132
|
+
}
|
|
133
|
+
exports.AssetOutputCodec = AssetOutputCodec;
|
|
134
|
+
exports.assetOutputCodec = new AssetOutputCodec();
|
|
135
|
+
exports.assetOutputsCodec = new array_codec_1.ArrayCodec(exports.assetOutputCodec);
|
|
@@ -0,0 +1,85 @@
|
|
|
1
|
+
"use strict";
|
|
2
|
+
Object.defineProperty(exports, "__esModule", { value: true });
|
|
3
|
+
exports.BigIntCodec = 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
|
+
class BigIntCodec {
|
|
22
|
+
static encode(value) {
|
|
23
|
+
// Special case for zero.
|
|
24
|
+
if (value === 0n) {
|
|
25
|
+
return Buffer.from([0]);
|
|
26
|
+
}
|
|
27
|
+
const isNegative = value < 0n;
|
|
28
|
+
let absValue = isNegative ? -value : value;
|
|
29
|
+
const bytes = [];
|
|
30
|
+
// Extract bytes from absolute value.
|
|
31
|
+
while (absValue > 0n) {
|
|
32
|
+
bytes.push(Number(absValue & 0xffn));
|
|
33
|
+
absValue >>= 8n;
|
|
34
|
+
}
|
|
35
|
+
// If the bigint is positive and the most significant byte has its high bit set,
|
|
36
|
+
// prefix the byte array with a zero byte to signify positive value.
|
|
37
|
+
if (!isNegative && (bytes[bytes.length - 1] & 0x80) !== 0) {
|
|
38
|
+
bytes.push(0);
|
|
39
|
+
}
|
|
40
|
+
// If the bigint is negative, compute the two's complement of the byte array.
|
|
41
|
+
if (isNegative) {
|
|
42
|
+
let carry = true;
|
|
43
|
+
for (let i = 0; i < bytes.length; i++) {
|
|
44
|
+
bytes[i] = ~bytes[i] & 0xff; // Invert the bits of the byte.
|
|
45
|
+
if (carry) {
|
|
46
|
+
if (bytes[i] === 0xff) {
|
|
47
|
+
bytes[i] = 0;
|
|
48
|
+
}
|
|
49
|
+
else {
|
|
50
|
+
bytes[i] += 1;
|
|
51
|
+
carry = false;
|
|
52
|
+
}
|
|
53
|
+
}
|
|
54
|
+
}
|
|
55
|
+
// If there's still a carry, and the most significant byte is not 0xFF (to store the negative sign bit),
|
|
56
|
+
// or if no bytes were set (which means the value was -1), append a 0xFF byte to hold the carry.
|
|
57
|
+
if (carry || bytes.length === 0 || (bytes[bytes.length - 1] & 0x80) === 0) {
|
|
58
|
+
bytes.push(0xff);
|
|
59
|
+
}
|
|
60
|
+
}
|
|
61
|
+
// The byte array needs to be reversed since we've constructed it in little-endian order.
|
|
62
|
+
return Buffer.from(new Uint8Array(bytes.reverse()));
|
|
63
|
+
}
|
|
64
|
+
static decode(encoded, signed) {
|
|
65
|
+
// Special case for zero.
|
|
66
|
+
if (encoded.length === 1 && encoded[0] === 0) {
|
|
67
|
+
return 0n;
|
|
68
|
+
}
|
|
69
|
+
// Determine if the number is negative by checking the most significant byte (MSB)
|
|
70
|
+
const isNegative = signed ? encoded[0] === 0xff : signed;
|
|
71
|
+
// Convert the byte array to a bigint
|
|
72
|
+
let value = 0n;
|
|
73
|
+
for (const byte of encoded) {
|
|
74
|
+
value = (value << 8n) | BigInt(byte);
|
|
75
|
+
}
|
|
76
|
+
// If the number is negative, convert from two's complement
|
|
77
|
+
if (isNegative) {
|
|
78
|
+
// Create a mask for the value's bit length
|
|
79
|
+
const mask = (1n << (8n * BigInt(encoded.length))) - 1n;
|
|
80
|
+
value = -(~value & mask) - 1n;
|
|
81
|
+
}
|
|
82
|
+
return value;
|
|
83
|
+
}
|
|
84
|
+
}
|
|
85
|
+
exports.BigIntCodec = BigIntCodec;
|
|
@@ -0,0 +1,16 @@
|
|
|
1
|
+
/// <reference types="node" />
|
|
2
|
+
import { Parser } from 'binary-parser';
|
|
3
|
+
import { DecodedCompactInt } from './compact-int-codec';
|
|
4
|
+
import { Codec } from './codec';
|
|
5
|
+
export interface ByteString {
|
|
6
|
+
length: DecodedCompactInt;
|
|
7
|
+
value: Buffer;
|
|
8
|
+
}
|
|
9
|
+
export declare class ByteStringCodec implements Codec<ByteString> {
|
|
10
|
+
parser: Parser;
|
|
11
|
+
encode(input: ByteString): Buffer;
|
|
12
|
+
decode(input: Buffer): ByteString;
|
|
13
|
+
encodeBuffer(input: Buffer): Buffer;
|
|
14
|
+
decodeBuffer(input: Buffer): Buffer;
|
|
15
|
+
}
|
|
16
|
+
export declare const byteStringCodec: ByteStringCodec;
|
|
@@ -0,0 +1,49 @@
|
|
|
1
|
+
"use strict";
|
|
2
|
+
Object.defineProperty(exports, "__esModule", { value: true });
|
|
3
|
+
exports.byteStringCodec = exports.ByteStringCodec = 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
|
+
class ByteStringCodec {
|
|
24
|
+
constructor() {
|
|
25
|
+
this.parser = new binary_parser_1.Parser()
|
|
26
|
+
.nest('length', {
|
|
27
|
+
type: compact_int_codec_1.compactUnsignedIntCodec.parser
|
|
28
|
+
})
|
|
29
|
+
.buffer('value', {
|
|
30
|
+
length: function (ctx) {
|
|
31
|
+
return compact_int_codec_1.compactUnsignedIntCodec.toU32(this['length']);
|
|
32
|
+
}
|
|
33
|
+
});
|
|
34
|
+
}
|
|
35
|
+
encode(input) {
|
|
36
|
+
return Buffer.from([...compact_int_codec_1.compactUnsignedIntCodec.encode(input.length), ...input.value]);
|
|
37
|
+
}
|
|
38
|
+
decode(input) {
|
|
39
|
+
return this.parser.parse(input);
|
|
40
|
+
}
|
|
41
|
+
encodeBuffer(input) {
|
|
42
|
+
return Buffer.from([...compact_int_codec_1.compactUnsignedIntCodec.encodeU32(input.length), ...input]);
|
|
43
|
+
}
|
|
44
|
+
decodeBuffer(input) {
|
|
45
|
+
return this.decode(input).value;
|
|
46
|
+
}
|
|
47
|
+
}
|
|
48
|
+
exports.ByteStringCodec = ByteStringCodec;
|
|
49
|
+
exports.byteStringCodec = new ByteStringCodec();
|
|
@@ -0,0 +1,47 @@
|
|
|
1
|
+
/// <reference types="node" />
|
|
2
|
+
import { Parser } from 'binary-parser';
|
|
3
|
+
import { Codec } from './codec';
|
|
4
|
+
export declare class CompactInt {
|
|
5
|
+
static readonly oneBytePrefix = 0;
|
|
6
|
+
static readonly oneByteNegPrefix = 192;
|
|
7
|
+
static readonly twoBytePrefix = 64;
|
|
8
|
+
static readonly twoByteNegPrefix = 128;
|
|
9
|
+
static readonly fourBytePrefix = 128;
|
|
10
|
+
static readonly fourByteNegPrefix = 64;
|
|
11
|
+
static readonly multiBytePrefix = 192;
|
|
12
|
+
}
|
|
13
|
+
export interface DecodedCompactInt {
|
|
14
|
+
mode: number;
|
|
15
|
+
rest: Uint8Array;
|
|
16
|
+
}
|
|
17
|
+
export declare class CompactUnsignedIntCodec implements Codec<DecodedCompactInt> {
|
|
18
|
+
private oneByteBound;
|
|
19
|
+
private twoByteBound;
|
|
20
|
+
private fourByteBound;
|
|
21
|
+
parser: Parser;
|
|
22
|
+
encode(input: DecodedCompactInt): Buffer;
|
|
23
|
+
encodeU32(value: number): Buffer;
|
|
24
|
+
encodeU256(value: bigint): Buffer;
|
|
25
|
+
decodeU32(input: Buffer): number;
|
|
26
|
+
decodeU256(input: Buffer): bigint;
|
|
27
|
+
decode(input: Buffer): DecodedCompactInt;
|
|
28
|
+
toU32(value: DecodedCompactInt): number;
|
|
29
|
+
toU256(value: DecodedCompactInt): bigint;
|
|
30
|
+
}
|
|
31
|
+
export declare const compactUnsignedIntCodec: CompactUnsignedIntCodec;
|
|
32
|
+
export declare class CompactSignedIntCodec implements Codec<DecodedCompactInt> {
|
|
33
|
+
private signFlag;
|
|
34
|
+
private oneByteBound;
|
|
35
|
+
private twoByteBound;
|
|
36
|
+
private fourByteBound;
|
|
37
|
+
parser: Parser;
|
|
38
|
+
encode(input: DecodedCompactInt): Buffer;
|
|
39
|
+
decode(input: Buffer): DecodedCompactInt;
|
|
40
|
+
decodeI32(input: Buffer): number;
|
|
41
|
+
encodeI32(value: number): Buffer;
|
|
42
|
+
encodeI256(value: bigint): Buffer;
|
|
43
|
+
decodeI256(input: Buffer): bigint;
|
|
44
|
+
toI32(value: DecodedCompactInt): number;
|
|
45
|
+
toI256(value: DecodedCompactInt): bigint;
|
|
46
|
+
}
|
|
47
|
+
export declare const compactSignedIntCodec: CompactSignedIntCodec;
|