@alephium/web3 0.37.0 → 0.38.0
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/dist/alephium-web3.min.js +1 -1
- package/dist/alephium-web3.min.js.map +1 -1
- package/dist/src/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/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
|
@@ -375,6 +375,7 @@ export interface CompileContractResult {
|
|
|
375
375
|
enums: Enum[];
|
|
376
376
|
events: EventSig[];
|
|
377
377
|
warnings: string[];
|
|
378
|
+
maps?: MapsSig;
|
|
378
379
|
stdInterfaceId?: string;
|
|
379
380
|
}
|
|
380
381
|
export interface CompileProjectResult {
|
|
@@ -579,6 +580,10 @@ export interface InterCliquePeerInfo {
|
|
|
579
580
|
export interface InternalServerError {
|
|
580
581
|
detail: string;
|
|
581
582
|
}
|
|
583
|
+
export interface MapsSig {
|
|
584
|
+
names: string[];
|
|
585
|
+
types: string[];
|
|
586
|
+
}
|
|
582
587
|
export interface MemPooled {
|
|
583
588
|
type: string;
|
|
584
589
|
}
|
package/dist/src/api/types.d.ts
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
import * as node from './api-alephium';
|
|
2
2
|
export type Number256 = bigint | string;
|
|
3
|
-
export type Val = Number256 | boolean | string | Val[] | ValObject
|
|
3
|
+
export type Val = Number256 | boolean | string | Val[] | ValObject | Map<Val, Val>;
|
|
4
4
|
export interface ValObject extends Record<string, Val> {
|
|
5
5
|
}
|
|
6
6
|
export type NamedVals = Record<string, Val>;
|
|
@@ -0,0 +1,17 @@
|
|
|
1
|
+
import { Buffer } from 'buffer/';
|
|
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,59 @@
|
|
|
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 buffer_1 = require("buffer/");
|
|
22
|
+
const binary_parser_1 = require("binary-parser");
|
|
23
|
+
const compact_int_codec_1 = require("./compact-int-codec");
|
|
24
|
+
class ArrayCodec {
|
|
25
|
+
constructor(childCodec, parser = ArrayCodec.arrayParser(childCodec.parser)) {
|
|
26
|
+
this.childCodec = childCodec;
|
|
27
|
+
this.parser = parser;
|
|
28
|
+
}
|
|
29
|
+
encode(input) {
|
|
30
|
+
const result = [...compact_int_codec_1.compactSignedIntCodec.encodeI256(BigInt(input.length))];
|
|
31
|
+
for (const element of input) {
|
|
32
|
+
result.push(...this.childCodec.encode(element));
|
|
33
|
+
}
|
|
34
|
+
return buffer_1.Buffer.from(result);
|
|
35
|
+
}
|
|
36
|
+
decode(input) {
|
|
37
|
+
const result = this.parser.parse(input);
|
|
38
|
+
return result.value.map((v) => this.childCodec.decode(v.value));
|
|
39
|
+
}
|
|
40
|
+
static arrayParser(parser) {
|
|
41
|
+
return new binary_parser_1.Parser()
|
|
42
|
+
.nest('length', {
|
|
43
|
+
type: compact_int_codec_1.compactSignedIntCodec.parser
|
|
44
|
+
})
|
|
45
|
+
.array('value', {
|
|
46
|
+
length: function (ctx) {
|
|
47
|
+
return compact_int_codec_1.compactSignedIntCodec.toI32(this['length']);
|
|
48
|
+
},
|
|
49
|
+
type: parser
|
|
50
|
+
});
|
|
51
|
+
}
|
|
52
|
+
fromArray(inputs) {
|
|
53
|
+
return {
|
|
54
|
+
length: compact_int_codec_1.compactUnsignedIntCodec.fromU32(inputs.length),
|
|
55
|
+
value: inputs
|
|
56
|
+
};
|
|
57
|
+
}
|
|
58
|
+
}
|
|
59
|
+
exports.ArrayCodec = ArrayCodec;
|
|
@@ -0,0 +1,27 @@
|
|
|
1
|
+
import { Buffer } from 'buffer/';
|
|
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 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 signed_int_codec_1 = require("./signed-int-codec");
|
|
26
|
+
const long_codec_1 = require("./long-codec");
|
|
27
|
+
const bytestring_codec_1 = require("./bytestring-codec");
|
|
28
|
+
const lockup_script_codec_1 = require("./lockup-script-codec");
|
|
29
|
+
const hash_1 = require("./hash");
|
|
30
|
+
const utils_1 = require("../utils");
|
|
31
|
+
const token_codec_1 = require("./token-codec");
|
|
32
|
+
class AssetOutputCodec {
|
|
33
|
+
constructor() {
|
|
34
|
+
this.parser = binary_parser_1.Parser.start()
|
|
35
|
+
.nest('amount', {
|
|
36
|
+
type: compact_int_codec_1.compactUnsignedIntCodec.parser
|
|
37
|
+
})
|
|
38
|
+
.nest('lockupScript', {
|
|
39
|
+
type: lockup_script_codec_1.lockupScriptCodec.parser
|
|
40
|
+
})
|
|
41
|
+
.buffer('lockTime', {
|
|
42
|
+
length: 8
|
|
43
|
+
})
|
|
44
|
+
.nest('tokens', {
|
|
45
|
+
type: token_codec_1.tokensCodec.parser
|
|
46
|
+
})
|
|
47
|
+
.nest('additionalData', {
|
|
48
|
+
type: bytestring_codec_1.byteStringCodec.parser
|
|
49
|
+
});
|
|
50
|
+
}
|
|
51
|
+
encode(input) {
|
|
52
|
+
const amount = buffer_1.Buffer.from(compact_int_codec_1.compactUnsignedIntCodec.encode(input.amount));
|
|
53
|
+
const lockupScript = lockup_script_codec_1.lockupScriptCodec.encode(input.lockupScript);
|
|
54
|
+
const lockTime = buffer_1.Buffer.from(input.lockTime);
|
|
55
|
+
const tokens = buffer_1.Buffer.from(token_codec_1.tokensCodec.encode(input.tokens.value));
|
|
56
|
+
const additionalData = buffer_1.Buffer.from(bytestring_codec_1.byteStringCodec.encode(input.additionalData));
|
|
57
|
+
return buffer_1.Buffer.concat([amount, lockupScript, lockTime, tokens, additionalData]);
|
|
58
|
+
}
|
|
59
|
+
decode(input) {
|
|
60
|
+
return this.parser.parse(input);
|
|
61
|
+
}
|
|
62
|
+
static toFixedAssetOutputs(txIdBytes, outputs) {
|
|
63
|
+
return outputs.map((output, index) => AssetOutputCodec.toFixedAssetOutput(txIdBytes, output, index));
|
|
64
|
+
}
|
|
65
|
+
static toFixedAssetOutput(txIdBytes, output, index) {
|
|
66
|
+
const attoAlphAmount = compact_int_codec_1.compactUnsignedIntCodec.toU256(output.amount).toString();
|
|
67
|
+
const lockTime = Number(long_codec_1.longCodec.decode(output.lockTime));
|
|
68
|
+
const tokens = output.tokens.value.map((token) => {
|
|
69
|
+
return {
|
|
70
|
+
id: token.tokenId.toString('hex'),
|
|
71
|
+
amount: compact_int_codec_1.compactUnsignedIntCodec.toU256(token.amount).toString()
|
|
72
|
+
};
|
|
73
|
+
});
|
|
74
|
+
const message = output.additionalData.value.toString('hex');
|
|
75
|
+
const scriptType = output.lockupScript.scriptType;
|
|
76
|
+
const key = (0, utils_1.binToHex)((0, hash_1.blakeHash)(buffer_1.Buffer.concat([txIdBytes, signed_int_codec_1.signedIntCodec.encode(index)])));
|
|
77
|
+
const outputLockupScript = output.lockupScript.script;
|
|
78
|
+
const address = utils_1.bs58.encode(lockup_script_codec_1.lockupScriptCodec.encode(output.lockupScript));
|
|
79
|
+
let hint = undefined;
|
|
80
|
+
if (scriptType === 0) {
|
|
81
|
+
// P2PKH
|
|
82
|
+
hint = (0, hash_1.createHint)(outputLockupScript.publicKeyHash);
|
|
83
|
+
}
|
|
84
|
+
else if (scriptType === 1) {
|
|
85
|
+
// P2MPKH
|
|
86
|
+
hint = (0, hash_1.createHint)(outputLockupScript.publicKeyHashes.value[0].publicKeyHash);
|
|
87
|
+
}
|
|
88
|
+
else if (scriptType === 2) {
|
|
89
|
+
// P2SH
|
|
90
|
+
hint = (0, hash_1.createHint)(outputLockupScript.scriptHash);
|
|
91
|
+
}
|
|
92
|
+
else if (scriptType === 3) {
|
|
93
|
+
throw new Error(`P2C script type not allowed for asset output`);
|
|
94
|
+
}
|
|
95
|
+
else {
|
|
96
|
+
throw new Error(`Unexpected 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.fromU256(BigInt(fixedOutput.attoAlphAmount));
|
|
107
|
+
const lockTime = long_codec_1.longCodec.encode(BigInt(fixedOutput.lockTime));
|
|
108
|
+
const lockupScript = lockup_script_codec_1.lockupScriptCodec.decode(buffer_1.Buffer.from(utils_1.bs58.decode(fixedOutput.address)));
|
|
109
|
+
const tokensValue = fixedOutput.tokens.map((token) => {
|
|
110
|
+
return {
|
|
111
|
+
tokenId: buffer_1.Buffer.from(token.id, 'hex'),
|
|
112
|
+
amount: compact_int_codec_1.compactUnsignedIntCodec.fromU256(BigInt(token.amount))
|
|
113
|
+
};
|
|
114
|
+
});
|
|
115
|
+
const tokens = {
|
|
116
|
+
length: compact_int_codec_1.compactUnsignedIntCodec.fromU32(tokensValue.length),
|
|
117
|
+
value: tokensValue
|
|
118
|
+
};
|
|
119
|
+
const additionalDataValue = buffer_1.Buffer.from(fixedOutput.message, 'hex');
|
|
120
|
+
const additionalData = {
|
|
121
|
+
length: compact_int_codec_1.compactUnsignedIntCodec.fromU32(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,86 @@
|
|
|
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
|
+
const buffer_1 = require("buffer/");
|
|
22
|
+
class BigIntCodec {
|
|
23
|
+
static encode(value) {
|
|
24
|
+
// Special case for zero.
|
|
25
|
+
if (value === 0n) {
|
|
26
|
+
return buffer_1.Buffer.from([0]);
|
|
27
|
+
}
|
|
28
|
+
const isNegative = value < 0n;
|
|
29
|
+
let absValue = isNegative ? -value : value;
|
|
30
|
+
const bytes = [];
|
|
31
|
+
// Extract bytes from absolute value.
|
|
32
|
+
while (absValue > 0n) {
|
|
33
|
+
bytes.push(Number(absValue & 0xffn));
|
|
34
|
+
absValue >>= 8n;
|
|
35
|
+
}
|
|
36
|
+
// If the bigint is positive and the most significant byte has its high bit set,
|
|
37
|
+
// prefix the byte array with a zero byte to signify positive value.
|
|
38
|
+
if (!isNegative && (bytes[bytes.length - 1] & 0x80) !== 0) {
|
|
39
|
+
bytes.push(0);
|
|
40
|
+
}
|
|
41
|
+
// If the bigint is negative, compute the two's complement of the byte array.
|
|
42
|
+
if (isNegative) {
|
|
43
|
+
let carry = true;
|
|
44
|
+
for (let i = 0; i < bytes.length; i++) {
|
|
45
|
+
bytes[i] = ~bytes[i] & 0xff; // Invert the bits of the byte.
|
|
46
|
+
if (carry) {
|
|
47
|
+
if (bytes[i] === 0xff) {
|
|
48
|
+
bytes[i] = 0;
|
|
49
|
+
}
|
|
50
|
+
else {
|
|
51
|
+
bytes[i] += 1;
|
|
52
|
+
carry = false;
|
|
53
|
+
}
|
|
54
|
+
}
|
|
55
|
+
}
|
|
56
|
+
// If there's still a carry, and the most significant byte is not 0xFF (to store the negative sign bit),
|
|
57
|
+
// or if no bytes were set (which means the value was -1), append a 0xFF byte to hold the carry.
|
|
58
|
+
if (carry || bytes.length === 0 || (bytes[bytes.length - 1] & 0x80) === 0) {
|
|
59
|
+
bytes.push(0xff);
|
|
60
|
+
}
|
|
61
|
+
}
|
|
62
|
+
// The byte array needs to be reversed since we've constructed it in little-endian order.
|
|
63
|
+
return buffer_1.Buffer.from(new Uint8Array(bytes.reverse()));
|
|
64
|
+
}
|
|
65
|
+
static decode(encoded, signed) {
|
|
66
|
+
// Special case for zero.
|
|
67
|
+
if (encoded.length === 1 && encoded[0] === 0) {
|
|
68
|
+
return 0n;
|
|
69
|
+
}
|
|
70
|
+
// Determine if the number is negative by checking the most significant byte (MSB)
|
|
71
|
+
const isNegative = signed ? encoded[0] === 0xff : signed;
|
|
72
|
+
// Convert the byte array to a bigint
|
|
73
|
+
let value = 0n;
|
|
74
|
+
for (const byte of encoded) {
|
|
75
|
+
value = (value << 8n) | BigInt(byte);
|
|
76
|
+
}
|
|
77
|
+
// If the number is negative, convert from two's complement
|
|
78
|
+
if (isNegative) {
|
|
79
|
+
// Create a mask for the value's bit length
|
|
80
|
+
const mask = (1n << (8n * BigInt(encoded.length))) - 1n;
|
|
81
|
+
value = -(~value & mask) - 1n;
|
|
82
|
+
}
|
|
83
|
+
return value;
|
|
84
|
+
}
|
|
85
|
+
}
|
|
86
|
+
exports.BigIntCodec = BigIntCodec;
|
|
@@ -0,0 +1,16 @@
|
|
|
1
|
+
import { Buffer } from 'buffer/';
|
|
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,50 @@
|
|
|
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 buffer_1 = require("buffer/");
|
|
22
|
+
const binary_parser_1 = require("binary-parser");
|
|
23
|
+
const compact_int_codec_1 = require("./compact-int-codec");
|
|
24
|
+
class ByteStringCodec {
|
|
25
|
+
constructor() {
|
|
26
|
+
this.parser = new binary_parser_1.Parser()
|
|
27
|
+
.nest('length', {
|
|
28
|
+
type: compact_int_codec_1.compactUnsignedIntCodec.parser
|
|
29
|
+
})
|
|
30
|
+
.buffer('value', {
|
|
31
|
+
length: function (ctx) {
|
|
32
|
+
return compact_int_codec_1.compactUnsignedIntCodec.toU32(this['length']);
|
|
33
|
+
}
|
|
34
|
+
});
|
|
35
|
+
}
|
|
36
|
+
encode(input) {
|
|
37
|
+
return buffer_1.Buffer.from([...compact_int_codec_1.compactUnsignedIntCodec.encode(input.length), ...input.value]);
|
|
38
|
+
}
|
|
39
|
+
decode(input) {
|
|
40
|
+
return this.parser.parse(input);
|
|
41
|
+
}
|
|
42
|
+
encodeBuffer(input) {
|
|
43
|
+
return buffer_1.Buffer.from([...compact_int_codec_1.compactUnsignedIntCodec.encodeU32(input.length), ...input]);
|
|
44
|
+
}
|
|
45
|
+
decodeBuffer(input) {
|
|
46
|
+
return this.decode(input).value;
|
|
47
|
+
}
|
|
48
|
+
}
|
|
49
|
+
exports.ByteStringCodec = ByteStringCodec;
|
|
50
|
+
exports.byteStringCodec = new ByteStringCodec();
|
|
@@ -0,0 +1,51 @@
|
|
|
1
|
+
import { Buffer } from 'buffer/';
|
|
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
|
+
fromU32(value: number): DecodedCompactInt;
|
|
30
|
+
toU256(value: DecodedCompactInt): bigint;
|
|
31
|
+
fromU256(value: bigint): DecodedCompactInt;
|
|
32
|
+
}
|
|
33
|
+
export declare const compactUnsignedIntCodec: CompactUnsignedIntCodec;
|
|
34
|
+
export declare class CompactSignedIntCodec implements Codec<DecodedCompactInt> {
|
|
35
|
+
private signFlag;
|
|
36
|
+
private oneByteBound;
|
|
37
|
+
private twoByteBound;
|
|
38
|
+
private fourByteBound;
|
|
39
|
+
parser: Parser;
|
|
40
|
+
encode(input: DecodedCompactInt): Buffer;
|
|
41
|
+
decode(input: Buffer): DecodedCompactInt;
|
|
42
|
+
decodeI32(input: Buffer): number;
|
|
43
|
+
encodeI32(value: number): Buffer;
|
|
44
|
+
encodeI256(value: bigint): Buffer;
|
|
45
|
+
decodeI256(input: Buffer): bigint;
|
|
46
|
+
toI32(value: DecodedCompactInt): number;
|
|
47
|
+
fromI32(value: number): DecodedCompactInt;
|
|
48
|
+
toI256(value: DecodedCompactInt): bigint;
|
|
49
|
+
fromI256(value: bigint): DecodedCompactInt;
|
|
50
|
+
}
|
|
51
|
+
export declare const compactSignedIntCodec: CompactSignedIntCodec;
|